Separation Generator to a dedicated repo
[apps/low-level-can-service.git] / 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         static int initv2();
34
35         static const struct afb_verb_v2 verbs[]=
36         {
37                 { .verb= "subscribe", .callback= subscribe, .auth= NULL, .session= AFB_SESSION_NONE},
38                 { .verb= "unsubscribe", .callback= unsubscribe, .auth= NULL, .session= AFB_SESSION_NONE},
39                 { .verb= NULL, .callback= NULL, .auth= NULL, .session= 0}
40         };
41
42         const struct afb_binding_v2 afbBindingV2 {
43                 .api = "low-can",
44                 .specification = NULL,
45                 .verbs = verbs,
46                 .preinit = NULL,
47                 .init = initv2,
48                 .onevent = NULL,
49                 .noconcurrency = 0
50         };
51
52         /// @brief Initialize the binding.
53         ///
54         /// @param[in] service Structure which represent the Application Framework Binder.
55         ///
56         /// @return Exit code, zero if success.
57         static int initv2()
58         {
59                 can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
60
61                 can_bus_manager.set_can_devices();
62                 can_bus_manager.start_threads();
63
64                 /// Initialize Diagnostic manager that will handle obd2 requests.
65                 /// We pass by default the first CAN bus device to its Initialization.
66                 /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
67                 if(application_t::instance().get_diagnostic_manager().initialize())
68                         return 0;
69
70                 ERROR("There was something wrong with CAN device Initialization.");
71                 return 1;
72         }
73 };