b60989072f8a8ea958f9f8cd3cf89fc7f6953f5b
[apps/low-level-can-service.git] / 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_sig(struct afb_req request, int subscribe, struct signal *sig)
125 {
126         if (!afb_event_is_valid(sig->event)) {
127                 if (!subscribe)
128                         return 1;
129                 sig->event = afb_daemon_make_event(afbitf->daemon, sig->name);
130                 if (!afb_event_is_valid(sig->event)) {
131                         return 0;
132                 }
133         }
134
135         if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, sig->event)) < 0) {
136                 return 0;
137         }
138
139         return 1;
140 }
141
142 static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
143 {
144         int i, n, e;
145
146         n = sizeof OBD2_PIDS / sizeof * OBD2_PIDS;
147         e = 0;
148         for (i = 0 ; i < n ; i++)
149                 e += !subscribe_unsubscribe_sig(request, subscribe, &OBD2_PIDS[i]);
150         return e == 0;
151 }
152
153 static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name)
154 {
155         struct signal *sig;
156
157         if (0 == strcmp(name, "*"))
158                 return subscribe_unsubscribe_all(request, subscribe);
159
160         sig = getSignal(name);
161         if (sig == NULL) {
162                 return 0;
163         }
164
165         return subscribe_unsubscribe_sig(request, subscribe, sig);
166 }
167
168 static void subscribe_unsubscribe(struct afb_req request, int subscribe)
169 {
170         int ok, i, n;
171         struct json_object *args, *a, *x;
172
173         /* makes the subscription/unsubscription */
174         args = afb_req_json(request);
175         if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
176                 ok = subscribe_unsubscribe_all(request, subscribe);
177         } else if (json_object_get_type(a) != json_type_array) {
178                 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
179         } else {
180                 n = json_object_array_length(a);
181                 ok = 0;
182                 for (i = 0 ; i < n ; i++) {
183                         x = json_object_array_get_idx(a, i);
184                         if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
185                                 ok++;
186                 }
187                 ok = (ok == n);
188         }
189
190         /* send the report */
191         if (ok)
192                 afb_req_success(request, NULL, NULL);
193         else
194                 afb_req_fail(request, "error", NULL);
195 }
196
197 static void subscribe(struct afb_req request)
198 {
199         subscribe_unsubscribe(request, 1);
200 }
201
202 static void unsubscribe(struct afb_req request)
203 {
204         subscribe_unsubscribe(request, 0);
205 }
206 static const struct afb_verb_desc_v1 verbs[]=
207 {
208   { .name= "subscribe",    .session= AFB_SESSION_NONE, .callback= subscribe,    .info= "subscribe to notification of CAN bus messages." },
209   { .name= "unsubscribe",  .session= AFB_SESSION_NONE, .callback= unsubscribe,  .info= "unsubscribe a previous subscription." },
210         {NULL}
211 };
212
213 static const struct afb_binding binding_desc = {
214         .type = AFB_BINDING_VERSION_1,
215         .v1 = {
216                 .info = "CAN bus service",
217                 .prefix = "can",
218                 .verbs = verbs
219         }
220 };
221
222 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
223 {
224         interface = itf;
225
226         return &binding_desc;
227 }
228
229 int afbBindingV1ServiceInit(struct afb_service service)
230 {
231         std::ifstream fd_conf;
232         std::string fd_conf_content;
233         json_object jo_canbus;
234
235         /* Open JSON conf file */
236         jo_canbus = json_object_new_object();
237         fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "canbus.json", O_RDONLY, NULL);
238         if (fd_conf)
239         {
240                 fd_conf.seekg(0, std::ios::end);
241                 fd_conf_content.resize(fd_conf.tellg());
242                 fd_conf.seekg(0, std::ios::beg);
243                 fd_conf.read(&fd_conf_content[0], fd_conf_content.size());
244                 fd_conf.close();
245         }
246
247         jo_canbus = json_tokener_parse(&fd_conf_content);
248
249         /* Open CAN socket */
250         CanBus_c CanBus_handler(interface, json_object_get_string(json_object_object_get(jo_canbus, "deviceName"));
251         CanBus_handler.open();
252         CanBus_handler.start_threads();
253 }