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 /********************************************************************************
53 * CanBus method implementation
55 *********************************************************************************/
59 const int canfd_on = 1;
61 struct timeval timeout = {1, 0};
63 DEBUG(interface, "open_can_dev: CAN Handler socket : %d", socket);
67 socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
70 ERROR(interface, "open_can_dev: socket could not be created");
74 /* Set timeout for read */
75 setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
76 /* try to switch the socket into CAN_FD mode */
77 if (setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
79 NOTICE(interface, "open_can_dev: Can not switch into CAN Extended frame format.");
85 /* Attempts to open a socket to CAN bus */
86 strcpy(ifr.ifr_name, device);
87 if(ioctl(socket, SIOCGIFINDEX, &ifr) < 0)
88 ERROR(interface, "open_can_dev: ioctl failed");
91 txAddress.can_family = AF_CAN;
92 txAddress.can_ifindex = ifr.ifr_ifindex;
94 /* And bind it to txAddress */
95 if (bind(socket, (struct sockaddr *)&txAddress, sizeof(txAddress)) < 0)
97 ERROR(interface, "open_can_dev: bind failed");
101 fcntl(socket, F_SETFL, O_NONBLOCK);
117 void CanBus::start_threads()
119 std::queue <canfd_frame> canfd_frame_queue;
120 std::queue <openxc_can_message_type> can_message_queue;
122 th_reading = std::thread(can_reader, interface, socket, canfd_frame_queue);
123 th_decoding = std::thread(can_decoder, interface, canfd_frame_queue, can_message_queue);
124 th_pushing = std::thread(can_event_push, interface, can_message_queue);
127 /********************************************************************************
131 *********************************************************************************/
135 * called on an event on the CAN bus
137 static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
139 openxc_CanMessage can_message;
141 can_message = openxc_CanMessage_init_default;
143 /* read available data */
144 if ((revents & EPOLLIN) != 0)
146 read_can(&can_message);
150 /* check if error or hangup */
151 if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
153 sd_event_source_unref(s);
155 connect_to_event_loop();
162 * Get the event loop running.
163 * Will trigger on_event function on EPOLLIN event on socket
165 * Return 0 or positive value on success. Else negative value for failure.
167 static int connect_to_event_loop(CanBus &CanBus_handler)
169 sd_event *event_loop;
170 sd_event_source *source;
173 if (CanBus_handler.socket < 0)
175 return CanBus_handler.socket;
178 event_loop = afb_daemon_get_event_loop(interface->daemon);
179 rc = sd_event_add_io(event_loop, &source, CanBus_handler.socket, EPOLLIN, on_event, NULL);
182 CanBus_handler.close();
183 ERROR(interface, "Can't connect CAN bus %s to the event loop", CanBus_handler.device);
186 NOTICE(interface, "Connected CAN bus %s to the event loop", CanBus_handler.device);
192 /********************************************************************************
194 * Subscription and unsubscription
196 *********************************************************************************/
198 static int subscribe_unsubscribe_sig(struct afb_req request, int subscribe, struct signal *sig)
200 if (!afb_event_is_valid(sig->event)) {
203 sig->event = afb_daemon_make_event(afbitf->daemon, sig->name);
204 if (!afb_event_is_valid(sig->event)) {
209 if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, sig->event)) < 0) {
216 static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
220 n = sizeof OBD2_PIDS / sizeof * OBD2_PIDS;
222 for (i = 0 ; i < n ; i++)
223 e += !subscribe_unsubscribe_sig(request, subscribe, &OBD2_PIDS[i]);
227 static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name)
231 if (0 == strcmp(name, "*"))
232 return subscribe_unsubscribe_all(request, subscribe);
239 return subscribe_unsubscribe_sig(request, subscribe, sig);
242 static void subscribe_unsubscribe(struct afb_req request, int subscribe)
245 struct json_object *args, *a, *x;
247 /* makes the subscription/unsubscription */
248 args = afb_req_json(request);
249 if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
250 ok = subscribe_unsubscribe_all(request, subscribe);
251 } else if (json_object_get_type(a) != json_type_array) {
252 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
254 n = json_object_array_length(a);
256 for (i = 0 ; i < n ; i++) {
257 x = json_object_array_get_idx(a, i);
258 if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
264 /* send the report */
266 afb_req_success(request, NULL, NULL);
268 afb_req_fail(request, "error", NULL);
271 static void subscribe(struct afb_req request)
273 subscribe_unsubscribe(request, 1);
276 static void unsubscribe(struct afb_req request)
278 subscribe_unsubscribe(request, 0);
280 static const struct afb_verb_desc_v1 verbs[]=
282 { .name= "subscribe", .session= AFB_SESSION_NONE, .callback= subscribe, .info= "subscribe to notification of CAN bus messages." },
283 { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe, .info= "unsubscribe a previous subscription." },
287 static const struct afb_binding binding_desc = {
288 .type = AFB_BINDING_VERSION_1,
290 .info = "CAN bus service",
296 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
300 return &binding_desc;
303 int afbBindingV1ServiceInit(struct afb_service service)
305 /* Open JSON conf file */
307 /* Open CAN socket */
308 CanBus CanBus_handler;
309 CanBus_handler.open();
310 CanBus_handler.start_threads();
312 return connect_to_event_loop(CanBus_handler);