5 #include <sys/socket.h>
9 #include <linux/can/raw.h>
11 #include <systemd/sd-event.h>
21 #include <json-c/json.h>
22 #include <openxc.pb.h>
24 #include <afb/afb-binding.h>
25 #include <afb/afb-service-itf.h>
27 #include "ll-can-binding.h"
31 * Interface between the daemon and the binding
33 static const struct afb_binding_interface *interface;
35 /********************************************************************************
37 * CanBus method implementation
39 *********************************************************************************/
43 const int canfd_on = 1;
45 struct timeval timeout = {1, 0};
47 DEBUG(interface, "open_can_dev: CAN Handler socket : %d", socket);
51 socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
54 ERROR(interface, "open_can_dev: socket could not be created");
58 /* Set timeout for read */
59 setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
60 /* try to switch the socket into CAN_FD mode */
61 if (setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
63 NOTICE(interface, "open_can_dev: Can not switch into CAN Extended frame format.");
69 /* Attempts to open a socket to CAN bus */
70 strcpy(ifr.ifr_name, device);
71 if(ioctl(socket, SIOCGIFINDEX, &ifr) < 0)
72 ERROR(interface, "open_can_dev: ioctl failed");
75 txAddress.can_family = AF_CAN;
76 txAddress.can_ifindex = ifr.ifr_ifindex;
78 /* And bind it to txAddress */
79 if (bind(socket, (struct sockaddr *)&txAddress, sizeof(txAddress)) < 0)
81 ERROR(interface, "open_can_dev: bind failed");
85 fcntl(socket, F_SETFL, O_NONBLOCK);
101 void CanBus::start_threads()
103 std::queue <canfd_frame> canfd_frame_queue;
104 std::queue <openxc_can_message_type> can_message_queue;
106 th_reading = std::thread(can_reader, interface, socket, canfd_frame_queue);
107 th_decoding = std::thread(can_decoder, interface, canfd_frame_queue, can_message_queue);
108 th_pushing = std::thread(can_event_push, interface, can_message_queue);
111 /********************************************************************************
115 *********************************************************************************/
119 * called on an event on the CAN bus
121 static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
123 openxc_CanMessage can_message;
125 can_message = openxc_CanMessage_init_default;
127 /* read available data */
128 if ((revents & EPOLLIN) != 0)
130 read_can(&can_message);
134 /* check if error or hangup */
135 if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
137 sd_event_source_unref(s);
139 connect_to_event_loop();
146 * Get the event loop running.
147 * Will trigger on_event function on EPOLLIN event on socket
149 * Return 0 or positive value on success. Else negative value for failure.
151 static int connect_to_event_loop(CanBus &CanBus_handler)
153 sd_event *event_loop;
154 sd_event_source *source;
157 if (CanBus_handler.socket < 0)
159 return CanBus_handler.socket;
162 event_loop = afb_daemon_get_event_loop(interface->daemon);
163 rc = sd_event_add_io(event_loop, &source, CanBus_handler.socket, EPOLLIN, on_event, NULL);
166 CanBus_handler.close();
167 ERROR(interface, "Can't connect CAN bus %s to the event loop", CanBus_handler.device);
170 NOTICE(interface, "Connected CAN bus %s to the event loop", CanBus_handler.device);
176 /********************************************************************************
178 * Subscription and unsubscription
180 *********************************************************************************/
182 static int subscribe_unsubscribe_sig(struct afb_req request, int subscribe, struct signal *sig)
184 if (!afb_event_is_valid(sig->event)) {
187 sig->event = afb_daemon_make_event(afbitf->daemon, sig->name);
188 if (!afb_event_is_valid(sig->event)) {
193 if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, sig->event)) < 0) {
200 static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
204 n = sizeof OBD2_PIDS / sizeof * OBD2_PIDS;
206 for (i = 0 ; i < n ; i++)
207 e += !subscribe_unsubscribe_sig(request, subscribe, &OBD2_PIDS[i]);
211 static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name)
215 if (0 == strcmp(name, "*"))
216 return subscribe_unsubscribe_all(request, subscribe);
223 return subscribe_unsubscribe_sig(request, subscribe, sig);
226 static void subscribe_unsubscribe(struct afb_req request, int subscribe)
229 struct json_object *args, *a, *x;
231 /* makes the subscription/unsubscription */
232 args = afb_req_json(request);
233 if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
234 ok = subscribe_unsubscribe_all(request, subscribe);
235 } else if (json_object_get_type(a) != json_type_array) {
236 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
238 n = json_object_array_length(a);
240 for (i = 0 ; i < n ; i++) {
241 x = json_object_array_get_idx(a, i);
242 if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
248 /* send the report */
250 afb_req_success(request, NULL, NULL);
252 afb_req_fail(request, "error", NULL);
255 static void subscribe(struct afb_req request)
257 subscribe_unsubscribe(request, 1);
260 static void unsubscribe(struct afb_req request)
262 subscribe_unsubscribe(request, 0);
264 static const struct afb_verb_desc_v1 verbs[]=
266 { .name= "subscribe", .session= AFB_SESSION_NONE, .callback= subscribe, .info= "subscribe to notification of CAN bus messages." },
267 { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe, .info= "unsubscribe a previous subscription." },
271 static const struct afb_binding binding_desc = {
272 .type = AFB_BINDING_VERSION_1,
274 .info = "CAN bus service",
280 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
284 return &binding_desc;
287 int afbBindingV1ServiceInit(struct afb_service service)
289 /* Open JSON conf file */
291 /* Open CAN socket */
292 CanBus_handler.open();
293 CanBus_handler.start_threads();
295 return connect_to_event_loop(CanBus_handler);