Separate low_can_subscription class from callbacks
[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 #include "low-can-subscription.hpp"
21
22 #include <map>
23 #include <queue>
24 #include <mutex>
25 #include <vector>
26 #include <json-c/json.h>
27
28 #include "application.hpp"
29 #include "../can/can-bus.hpp"
30
31 extern "C"
32 {
33         #include <afb/afb-service-itf.h>
34 };
35
36 // Interface between the daemon and the binding
37 const struct afb_binding_interface *binder_interface;
38
39 extern "C"
40 {
41         static const struct afb_verb_desc_v1 verbs[]=
42         {
43                 { .name= "subscribe",   .session= AFB_SESSION_NONE, .callback= subscribe,       .info= "subscribe to notification of CAN bus messages." },
44                 { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe,     .info= "unsubscribe a previous subscription." }
45         };
46
47         static const struct afb_binding binding_desc {
48                 AFB_BINDING_VERSION_1,
49                 {
50                         "Low level CAN bus service",
51                         "low-can",
52                         verbs
53                 }
54         };
55
56         const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
57         {
58                 binder_interface = itf;
59
60                 return &binding_desc;
61         }
62
63         /// @brief Initialize the binding.
64         ///
65         /// @param[in] service Structure which represent the Application Framework Binder.
66         ///
67         /// @return Exit code, zero if success.
68         int afbBindingV1ServiceInit(struct afb_service service)
69         {
70                 can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
71
72                 can_bus_manager.set_can_devices();
73                 can_bus_manager.start_threads();
74
75                 /// Initialize Diagnostic manager that will handle obd2 requests.
76                 /// We pass by default the first CAN bus device to its Initialization.
77                 /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
78                 if(application_t::instance().get_diagnostic_manager().initialize())
79                         return 0;
80
81                 ERROR(binder_interface, "%s: There was something wrong with CAN device Initialization.", __FUNCTION__);
82                 return 1;
83         }
84 };