2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loic Collignon" <loic.collignon@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
24 #include <linux/can.h>
25 #include <linux/can/raw.h>
27 #include <systemd/sd-event.h>
37 #include <json-c/json.h>
38 #include <openxc.pb.h>
40 #include <afb/afb-binding.h>
41 #include <afb/afb-service-itf.h>
43 #include "ll-can-binding.h"
47 * Interface between the daemon and the binding
49 static const struct afb_binding_interface *interface;
51 /********************************************************************************
55 *********************************************************************************/
59 * called on an event on the CAN bus
61 static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
63 openxc_CanMessage can_message;
65 can_message = openxc_CanMessage_init_default;
67 /* read available data */
68 if ((revents & EPOLLIN) != 0)
70 read_can(&can_message);
74 /* check if error or hangup */
75 if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
77 sd_event_source_unref(s);
79 connect_to_event_loop();
86 * USELESS SINCE THREADS SEPARATION
88 * Get the event loop running.
89 * Will trigger on_event function on EPOLLIN event on socket
91 * Return 0 or positive value on success. Else negative value for failure.
92 static int connect_to_event_loop(CanBus &CanBus_handler)
95 sd_event_source *source;
98 if (CanBus_handler.socket < 0)
100 return CanBus_handler.socket;
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);
107 CanBus_handler.close();
108 ERROR(interface, "Can't connect CAN bus %s to the event loop", CanBus_handler.device);
111 NOTICE(interface, "Connected CAN bus %s to the event loop", CanBus_handler.device);
118 /********************************************************************************
120 * Subscription and unsubscription
122 *********************************************************************************/
124 static int subscribe_unsubscribe_sig(struct afb_req request, int subscribe, struct signal *sig)
126 if (!afb_event_is_valid(sig->event)) {
129 sig->event = afb_daemon_make_event(afbitf->daemon, sig->name);
130 if (!afb_event_is_valid(sig->event)) {
135 if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, sig->event)) < 0) {
142 static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
146 n = sizeof OBD2_PIDS / sizeof * OBD2_PIDS;
148 for (i = 0 ; i < n ; i++)
149 e += !subscribe_unsubscribe_sig(request, subscribe, &OBD2_PIDS[i]);
153 static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name)
157 if (0 == strcmp(name, "*"))
158 return subscribe_unsubscribe_all(request, subscribe);
160 sig = getSignal(name);
165 return subscribe_unsubscribe_sig(request, subscribe, sig);
168 static void subscribe_unsubscribe(struct afb_req request, int subscribe)
171 struct json_object *args, *a, *x;
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));
180 n = json_object_array_length(a);
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)))
190 /* send the report */
192 afb_req_success(request, NULL, NULL);
194 afb_req_fail(request, "error", NULL);
197 static void subscribe(struct afb_req request)
199 subscribe_unsubscribe(request, 1);
202 static void unsubscribe(struct afb_req request)
204 subscribe_unsubscribe(request, 0);
206 static const struct afb_verb_desc_v1 verbs[]=
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." },
213 static const struct afb_binding binding_desc = {
214 .type = AFB_BINDING_VERSION_1,
216 .info = "CAN bus service",
222 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
226 return &binding_desc;
229 int afbBindingV1ServiceInit(struct afb_service service)
231 std::ifstream fd_conf;
232 std::string fd_conf_content;
233 json_object jo_canbus;
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);
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());
247 jo_canbus = json_tokener_parse(&fd_conf_content);
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();