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;
52 /********************************************************************************
56 *********************************************************************************/
60 * called on an event on the CAN bus
62 static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
64 openxc_CanMessage can_message;
66 can_message = openxc_CanMessage_init_default;
68 /* read available data */
69 if ((revents & EPOLLIN) != 0)
71 read_can(&can_message);
75 /* check if error or hangup */
76 if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
78 sd_event_source_unref(s);
80 connect_to_event_loop();
87 * Get the event loop running.
88 * Will trigger on_event function on EPOLLIN event on socket
90 * 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);
117 /********************************************************************************
119 * Subscription and unsubscription
121 *********************************************************************************/
123 static int subscribe_unsubscribe_sig(struct afb_req request, int subscribe, struct signal *sig)
125 if (!afb_event_is_valid(sig->event)) {
128 sig->event = afb_daemon_make_event(afbitf->daemon, sig->name);
129 if (!afb_event_is_valid(sig->event)) {
134 if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, sig->event)) < 0) {
141 static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
145 n = sizeof OBD2_PIDS / sizeof * OBD2_PIDS;
147 for (i = 0 ; i < n ; i++)
148 e += !subscribe_unsubscribe_sig(request, subscribe, &OBD2_PIDS[i]);
152 static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name)
156 if (0 == strcmp(name, "*"))
157 return subscribe_unsubscribe_all(request, subscribe);
164 return subscribe_unsubscribe_sig(request, subscribe, sig);
167 static void subscribe_unsubscribe(struct afb_req request, int subscribe)
170 struct json_object *args, *a, *x;
172 /* makes the subscription/unsubscription */
173 args = afb_req_json(request);
174 if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
175 ok = subscribe_unsubscribe_all(request, subscribe);
176 } else if (json_object_get_type(a) != json_type_array) {
177 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
179 n = json_object_array_length(a);
181 for (i = 0 ; i < n ; i++) {
182 x = json_object_array_get_idx(a, i);
183 if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
189 /* send the report */
191 afb_req_success(request, NULL, NULL);
193 afb_req_fail(request, "error", NULL);
196 static void subscribe(struct afb_req request)
198 subscribe_unsubscribe(request, 1);
201 static void unsubscribe(struct afb_req request)
203 subscribe_unsubscribe(request, 0);
205 static const struct afb_verb_desc_v1 verbs[]=
207 { .name= "subscribe", .session= AFB_SESSION_NONE, .callback= subscribe, .info= "subscribe to notification of CAN bus messages." },
208 { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe, .info= "unsubscribe a previous subscription." },
212 static const struct afb_binding binding_desc = {
213 .type = AFB_BINDING_VERSION_1,
215 .info = "CAN bus service",
221 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
225 return &binding_desc;
228 int afbBindingV1ServiceInit(struct afb_service service)
230 /* Open JSON conf file */
232 /* Open CAN socket */
233 CanBus_t CanBus_handler;
234 CanBus_handler.open();
235 CanBus_handler.start_threads();
237 return connect_to_event_loop(CanBus_handler);