Finish config parser retrieving device mapping.
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / binding / low-can-hat.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loic Collignon" <loic.collignon@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "low-can-hat.hpp"
20
21 #include <map>
22 #include <queue>
23 #include <mutex>
24 #include <vector>
25 #include <json-c/json.h>
26
27 #include "configuration.hpp"
28 #include "../can/can-bus.hpp"
29
30 extern "C"
31 {
32         #include <afb/afb-service-itf.h>
33 };
34
35 // Interface between the daemon and the binding
36 const struct afb_binding_interface *binder_interface;
37
38 extern "C"
39 {
40         static const struct afb_verb_desc_v1 verbs[]=
41         {
42                 { .name= "subscribe",   .session= AFB_SESSION_NONE, .callback= subscribe,       .info= "subscribe to notification of CAN bus messages." },
43                 { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe,     .info= "unsubscribe a previous subscription." }
44         };
45
46         static const struct afb_binding binding_desc {
47                 AFB_BINDING_VERSION_1,
48                 {
49                         "Low level CAN bus service",
50                         "low-can",
51                         verbs
52                 }
53         };
54
55         const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
56         {
57                 binder_interface = itf;
58
59                 return &binding_desc;
60         }
61
62         /// @brief Initialize the binding.
63         ///
64         /// @param[in] service Structure which represent the Application Framework Binder.
65         ///
66         /// @return Exit code, zero if success.
67         int afbBindingV1ServiceInit(struct afb_service service)
68         {
69                 can_bus_t& can_bus_manager = configuration_t::instance().get_can_bus_manager();
70
71                 can_bus_manager.set_can_devices();
72                 can_bus_manager.start_threads();
73
74                 /// Initialize Diagnostic manager that will handle obd2 requests.
75                 /// We pass by default the first CAN bus device to its Initialization.
76                 /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
77                 if(configuration_t::instance().get_diagnostic_manager().initialize())
78                         return 0;
79
80                 ERROR(binder_interface, "%s: There was something wrong with CAN device Initialization.", __FUNCTION__);
81                 return 1;
82         }
83 };