Fix: OBD2_PIDS array initialization
[apps/agl-service-can-low-level.git] / src / low-can-binding.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 <unistd.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <net/if.h>
24 #include <linux/can.h>
25 #include <linux/can/raw.h>
26 #include <fcntl.h>
27 #include <systemd/sd-event.h>
28 #include <errno.h>
29 #include <vector>
30 #include <map>
31 #include <queue>
32 #include <string>
33 #include <functional>
34 #include <memory>
35 #include <thread>
36
37 #include <json-c/json.h>
38 #include <openxc.pb.h>
39
40 #include "low-can-binding.hpp"
41 #include "openxc-utils.hpp"
42 #include "obd2.hpp"
43 #include "can-utils.hpp"
44 #include "can-signals.hpp"
45
46 /********************************************************************************
47 *
48 *               Event management
49 *
50 *********************************************************************************/
51
52 /********************************************************************************
53 *
54 *               Subscription and unsubscription
55 *
56 *********************************************************************************/
57
58 static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const CanSignal& sig)
59 {
60         int ret;
61
62         const auto& ss_i = subscribed_signals.find(sig.genericName);
63         if (ss_i != subscribed_signals.end())
64         {
65                 if(!afb_event_is_valid(ss_i->second))
66                 {
67                         if(!subscribe)
68                         {
69                                 NOTICE(interface, "Event isn't valid, it can't be unsubscribed.");
70                                 ret = 1;
71                         }
72                         else
73                         {
74                                 ss_i->second = afb_daemon_make_event(interface->daemon, ss_i->first.c_str());
75                                 if (!afb_event_is_valid(ss_i->second)) 
76                                 {
77                                         ERROR(interface, "Can't create an event, something goes wrong.");
78                                         ret = 0;
79                                 }
80                         }
81                 }
82         }
83         else
84         {
85                 subscribed_signals[sig.genericName] = afb_daemon_make_event(interface->daemon, sig.genericName);
86                 if (!afb_event_is_valid(ss_i->second)) 
87                 {
88                         ERROR(interface, "Can't create an event, something goes wrong.");
89                         ret = 0;
90                 }
91         }
92
93         if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, subscribed_signals[sig.genericName])) < 0)
94         {
95                 ERROR(interface, "Operation goes wrong for signal: %s", sig.genericName);
96                 ret = 0;
97         }
98         else
99                 ret = 1;
100         
101         return ret;
102 }
103
104 static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<CanSignal>& signals)
105 {
106         int ret;
107
108         // TODO: lock the subscribed_signals when insert/remove
109         for(const auto& signal_i : signals)
110         {
111                 ret = subscribe_unsubscribe_signal(request, subscribe, signal_i);
112                 if(ret == 0)
113                         return ret;
114         }
115 }
116
117 static int subscribe_unsubscribe_all(struct afb_req request, bool subscribe)
118 {
119         int e = 0;
120
121         //for (const auto& sig : SIGNALS)
122         //      e += !subscribe_unsubscribe_signals(request, subscribe, sig);
123         e += !subscribe_unsubscribe_signals(request, subscribe, SIGNALS[MESSAGE_SET_ID]);
124         
125         return e == 0;
126 }
127
128 static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, const char *name)
129 {
130         std::vector<CanSignal> sig;
131         int ret = 0;
132
133         if (!::strcmp(name, "*"))
134                 ret = subscribe_unsubscribe_all(request, subscribe);
135         else
136         {
137                 //if(obd2_handler_c.is_obd2_signal(name))
138                 if(false)
139                 {
140                 // TODO
141                 }
142                 else
143                 {
144                         openxc_DynamicField search_key = build_DynamicField(name);
145                         sig = find_can_signals(search_key);
146                         if (sig.empty())
147                                 ret = 0;
148                 }
149                 ret = subscribe_unsubscribe_signals(request, subscribe, sig);
150         }
151         return ret;
152 }
153
154 static void subscribe_unsubscribe(struct afb_req request, bool subscribe)
155 {
156         int ok, i, n;
157         struct json_object *args, *a, *x;
158
159         /* makes the subscription/unsubscription */
160         args = afb_req_json(request);
161         if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
162                 ok = subscribe_unsubscribe_all(request, subscribe);
163         } else if (json_object_get_type(a) != json_type_array) {
164                 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
165         } else {
166                 n = json_object_array_length(a);
167                 ok = 0;
168                 for (i = 0 ; i < n ; i++) {
169                         x = json_object_array_get_idx(a, i);
170                         if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
171                                 ok++;
172                 }
173                 ok = (ok == n);
174         }
175
176         /* send the report */
177         if (ok)
178                 afb_req_success(request, NULL, NULL);
179         else
180                 afb_req_fail(request, "error", NULL);
181 }
182
183 static const struct afb_verb_desc_v1 verbs[]=
184 {
185         { .name= "subscribe",   .session= AFB_SESSION_NONE, .callback= subscribe,       .info= "subscribe to notification of CAN bus messages." },
186         { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe,     .info= "unsubscribe a previous subscription." }
187 };
188
189 static const struct afb_binding binding_desc {
190         AFB_BINDING_VERSION_1,
191         {
192                 "CAN bus service",
193                 "can",
194                 verbs
195         }
196 };
197
198 extern "C"
199 {
200         static void subscribe(struct afb_req request)
201         {
202                 subscribe_unsubscribe(request, true);
203         }
204
205         static void unsubscribe(struct afb_req request)
206         {
207                 subscribe_unsubscribe(request, false);
208         }
209
210         const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
211         {
212                 interface = itf;
213
214                 return &binding_desc;
215         }
216
217         /**
218         * @brief Initialize the binding.
219         * 
220         * @param[in] service Structure which represent the Application Framework Binder.
221         * 
222         * @return Exit code, zero if success.
223         */
224         int afbBindingV1ServiceInit(struct afb_service service)
225         {
226                 int fd_conf;
227                 fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL);
228
229                 /* Open CAN socket */
230                 can_bus_t can_bus_handler(interface, fd_conf);
231                 if(can_bus_handler.init_can_dev() == 0)
232                 {
233                         can_bus_handler.start_threads();
234                         return 0;
235                 }
236
237                 return 1;
238         }
239 }