Fix: wrong binding extension
[apps/low-level-can-service.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 <afb/afb-binding.h>
41 #include <afb/afb-service-itf.h>
42
43 #include "ll-can-binding.h"
44 #include "obd2.h"
45
46 /*
47  *       Interface between the daemon and the binding
48  */
49 static const struct afb_binding_interface *interface;
50
51 /********************************************************************************
52 *
53 *               Event management
54 *
55 *********************************************************************************/
56
57 /*
58  * TBF TBF TBF
59  * called on an event on the CAN bus
60  */
61 static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
62 {
63         openxc_CanMessage can_message;
64
65         can_message = openxc_CanMessage_init_default;
66
67         /* read available data */
68         if ((revents & EPOLLIN) != 0)
69         {
70                 read_can(&can_message);
71                 send_event();
72         }
73
74         /* check if error or hangup */
75         if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
76         {
77                 sd_event_source_unref(s);
78                 close(fd);
79                 connect_to_event_loop();
80         }
81
82         return 0;
83 }
84
85 /*
86  * USELESS SINCE THREADS SEPARATION
87  *
88  * Get the event loop running.
89  * Will trigger on_event function on EPOLLIN event on socket
90  *
91  * Return 0 or positive value on success. Else negative value for failure.
92 static int connect_to_event_loop(CanBus &CanBus_handler)
93 {
94         sd_event *event_loop;
95         sd_event_source *source;
96         int rc;
97
98         if (CanBus_handler.socket < 0)
99         {
100                 return CanBus_handler.socket;
101         }
102
103         event_loop = afb_daemon_get_event_loop(interface->daemon);
104         rc = sd_event_add_io(event_loop, &source, CanBus_handler.socket, EPOLLIN, on_event, NULL);
105         if (rc < 0)
106         {
107                 CanBus_handler.close();
108                 ERROR(interface, "Can't connect CAN bus %s to the event loop", CanBus_handler.device);
109         } else
110         {
111                 NOTICE(interface, "Connected CAN bus %s to the event loop", CanBus_handler.device);
112         }
113
114         return rc;
115 }
116  */
117
118 /********************************************************************************
119 *
120 *               Subscription and unsubscription
121 *
122 *********************************************************************************/
123
124 static int subscribe_unsubscribe_signal(struct afb_req request, int subscribe, std::map<CanSignal, struct afb_event>::iterator *s_sig)
125  {
126         if (!afb_event_is_valid(s_sig->second)) {
127                 if (!subscribe)
128                         return 1;
129                 sig->event = afb_daemon_make_event(afbitf->daemon, s_sig->first->genericName);
130                 if (!afb_event_is_valid(s_sig->second)) {
131                         return 0;
132                 }
133         }
134
135         if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s_sig->second)) < 0) {
136                 return 0;
137         }
138
139         return 1;
140  }
141
142 static int subscribe_unsubscribe_signals(struct afb_req request, int subscribe, std:vector<CanSignal> *signals)
143 {
144         std::vector<CanSignal>::iterator signal_i;
145         std::map <CanSignal, struct afb_event>::iterator s_signal_i;
146         
147         for(signal_i=signals.begin(); signal_i != signals.end(); signal_i++)
148         {
149                 s_signal_i = subscribed_signals.find(signal_i);
150                 if(s_signal_i != subscribed_signals.end())
151                         subscribe_unsubscribe_signal(request, subscribe, s_signal_i);
152                 else
153                 {
154                         std::map <CanSignal, struct afb_event>::iterator it = subscribed_signals.begin();
155                         it = subscribed_signals.insert(it, std::pair<CanSignal, struct afb_event>(signal_i, NULL);
156                         subscribe_unsubscribe_signal(request, subscribe, it);
157                 }
158                 return 0;
159         }
160 }
161
162 static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
163 {
164         int i, n, e;
165
166         n = sizeof OBD2_PIDS / sizeof * OBD2_PIDS;
167         e = 0;
168         for (i = 0 ; i < n ; i++)
169                 e += !subscribe_unsubscribe_sig(request, subscribe, &OBD2_PIDS[i]);
170         return e == 0;
171 }
172
173 static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name)
174 {
175          std::vector <CanSignal> *sig;
176
177         if (0 == strcmp(name, "*"))
178                 return subscribe_unsubscribe_all(request, subscribe);
179
180         find_signals(name, sig);
181         if (sig == NULL) {
182                 return 0;
183         }
184
185         return subscribe_unsubscribe_sig(request, subscribe, sig);
186 }
187
188 static void subscribe_unsubscribe(struct afb_req request, int subscribe)
189 {
190         int ok, i, n;
191         struct json_object *args, *a, *x;
192
193         /* makes the subscription/unsubscription */
194         args = afb_req_json(request);
195         if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
196                 ok = subscribe_unsubscribe_all(request, subscribe);
197         } else if (json_object_get_type(a) != json_type_array) {
198                 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
199         } else {
200                 n = json_object_array_length(a);
201                 ok = 0;
202                 for (i = 0 ; i < n ; i++) {
203                         x = json_object_array_get_idx(a, i);
204                         if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
205                                 ok++;
206                 }
207                 ok = (ok == n);
208         }
209
210         /* send the report */
211         if (ok)
212                 afb_req_success(request, NULL, NULL);
213         else
214                 afb_req_fail(request, "error", NULL);
215 }
216
217 static void subscribe(struct afb_req request)
218 {
219         subscribe_unsubscribe(request, 1);
220 }
221
222 static void unsubscribe(struct afb_req request)
223 {
224         subscribe_unsubscribe(request, 0);
225 }
226 static const struct afb_verb_desc_v1 verbs[]=
227 {
228   { .name= "subscribe",    .session= AFB_SESSION_NONE, .callback= subscribe,    .info= "subscribe to notification of CAN bus messages." },
229   { .name= "unsubscribe",  .session= AFB_SESSION_NONE, .callback= unsubscribe,  .info= "unsubscribe a previous subscription." },
230         {NULL}
231 };
232
233 static const struct afb_binding binding_desc = {
234         .type = AFB_BINDING_VERSION_1,
235         .v1 = {
236                 .info = "CAN bus service",
237                 .prefix = "can",
238                 .verbs = verbs
239         }
240 };
241
242 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
243 {
244         interface = itf;
245
246         return &binding_desc;
247 }
248
249 int afbBindingV1ServiceInit(struct afb_service service)
250 {
251         std::ifstream fd_conf;
252         std::string fd_conf_content;
253         json_object jo_canbus;
254
255         /* Open JSON conf file */
256         jo_canbus = json_object_new_object();
257         fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "canbus.json", O_RDONLY, NULL);
258         if (fd_conf)
259         {
260                 fd_conf.seekg(0, std::ios::end);
261                 fd_conf_content.resize(fd_conf.tellg());
262                 fd_conf.seekg(0, std::ios::beg);
263                 fd_conf.read(&fd_conf_content[0], fd_conf_content.size());
264                 fd_conf.close();
265         }
266
267         jo_canbus = json_tokener_parse(&fd_conf_content);
268
269         /* Open CAN socket */
270         CanBus_c CanBus_handler(interface, json_object_get_string(json_object_object_get(jo_canbus, "deviceName"));
271         CanBus_handler.open();
272         CanBus_handler.start_threads();
273 }