summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-01-30 13:18:07 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-01-30 13:34:57 +0000
commitd3eea564254da43803c9ad9a105ef93cdd053227 (patch)
tree30f31d9a1e54ce7a40f18092496ea3bdead07b14
parent768aa1b44852e63ec34845685b7f35aa522ac242 (diff)
Code restructuration
Change-Id: I5acd8288984d42d8487091d38511a05f0fd703c8 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--ll-can-binding.c228
-rw-r--r--ll-can-binding.h10
2 files changed, 114 insertions, 124 deletions
diff --git a/ll-can-binding.c b/ll-can-binding.c
index e570d433..adf878b6 100644
--- a/ll-can-binding.c
+++ b/ll-can-binding.c
@@ -154,8 +154,6 @@ static char* create_name(uint32_t id)
* object.
*
* return : json object
- *
- *
*/
static json_object* create_json_from_openxc_CanMessage(event *event)
{
@@ -271,63 +269,6 @@ static int write_can()
}
/*
- * Read on CAN bus and return how much bytes has been read.
- */
-static int read_can(openxc_CanMessage *can_message)
-{
- ssize_t nbytes;
- int maxdlen;
-
- /* Test that socket is really opened */
- if ( socket_test() < 0)
- {
- if (retry(open_can_dev) < 0)
- {
- ERROR(interface, "read_can: Socket unavailable");
- return -1;
- }
- }
-
- nbytes = read(can_handler.socket, &canfd_frame, CANFD_MTU);
-
- if (nbytes == CANFD_MTU)
- {
- DEBUG(interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
- }
- else if (nbytes == CAN_MTU)
- {
- DEBUG(interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
- }
- else
- {
- if (errno == ENETDOWN)
- ERROR(interface, "read_can: %s interface down", can_handler.device);
- ERROR(interface, "read_can: Error reading CAN bus");
- return -2;
- }
-
- /* CAN frame integrity check */
- if ((size_t)nbytes == CAN_MTU)
- maxdlen = CAN_MAX_DLEN;
- else if ((size_t)nbytes == CANFD_MTU)
- maxdlen = CANFD_MAX_DLEN;
- else
- {
- ERROR(interface, "read_can: CAN frame incomplete");
- return -3;
- }
-
- if (parse_can_frame(can_message, &canfd_frame, maxdlen))
- {
- ERROR(interface, "read_can: Can't parse the can frame. ID: %i, DLC: %i, DATA: %s",
- canfd_frame.can_id, canfd_frame.len, canfd_frame.data);
- return -4;
- }
-
- return 0;
-}
-
-/*
* Parse the CAN frame data payload as a CAN packet
* TODO: parse as an OpenXC Can Message. Don't translate as ASCII and put bytes
* directly into openxc_CanMessage
@@ -399,63 +340,104 @@ static int parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *c
return -3;
}
-/*************************************************************************/
-/*************************************************************************/
-/** **/
-/** **/
-/** SECTION: MANAGING EVENTS **/
-/** **/
-/** **/
-/*************************************************************************/
-/*************************************************************************/
+
/*
- * called on an event on the CAN bus
+ * Read on CAN bus and return how much bytes has been read.
*/
-static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
+static int read_can(openxc_CanMessage *can_message)
{
- openxc_CanMessage can_message;
+ ssize_t nbytes;
+ int maxdlen;
- can_message = openxc_CanMessage_init_default;
+ /* Test that socket is really opened */
+ if ( socket_test() < 0)
+ {
+ if (retry(open_can_dev) < 0)
+ {
+ ERROR(interface, "read_can: Socket unavailable");
+ return -1;
+ }
+ }
- /* read available data */
- if ((revents & EPOLLIN) != 0)
+ nbytes = read(can_handler.socket, &canfd_frame, CANFD_MTU);
+
+ if (nbytes == CANFD_MTU)
{
- read_can(&can_message);
- send_event();
+ DEBUG(interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
+ }
+ else if (nbytes == CAN_MTU)
+ {
+ DEBUG(interface, "read_can: Got a legacy CAN frame with length %d", canfd_frame.len);
+ }
+ else
+ {
+ if (errno == ENETDOWN)
+ ERROR(interface, "read_can: %s interface down", can_handler.device);
+ ERROR(interface, "read_can: Error reading CAN bus");
+ return -2;
}
- /* check if error or hangup */
- if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
+ /* CAN frame integrity check */
+ if ((size_t)nbytes == CAN_MTU)
+ maxdlen = CAN_MAX_DLEN;
+ else if ((size_t)nbytes == CANFD_MTU)
+ maxdlen = CANFD_MAX_DLEN;
+ else
{
- sd_event_source_unref(s);
- close(fd);
- connect_to_event_loop();
+ ERROR(interface, "read_can: CAN frame incomplete");
+ return -3;
+ }
+
+ if (parse_can_frame(can_message, &canfd_frame, maxdlen))
+ {
+ ERROR(interface, "read_can: Can't parse the can frame. ID: %i, DLC: %i, DATA: %s",
+ canfd_frame.can_id, canfd_frame.len, canfd_frame.data);
+ return -4;
}
return 0;
}
+/*************************************************************************/
+/*************************************************************************/
+/** **/
+/** **/
+/** SECTION: MANAGING EVENTS **/
+/** **/
+/** **/
+/*************************************************************************/
+/*************************************************************************/
+static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata);
/*
- * get or create an event handler for the type
+ * Get the event loop running.
+ * Will trigger on_event function on EPOLLIN event on socket
+ *
+ * Return 0 or positive value on success. Else negative value for failure.
*/
-static event *get_event(uint32_t id, enum type type)
+static int connect_to_event_loop()
{
- event *event_elt;
- can_event *list;
+ sd_event *event_loop;
+ sd_event_source *source;
+ int rc;
- /* find the can list by id */
- list = get_event_list_of_id(id);
+ if (can_handler.socket < 0)
+ {
+ return can_handler.socket;
+ }
- /* make the new event */
- event_elt = (event*)calloc(1, sizeof(event));
- event_elt->next = event_elt;
- list->events = event_elt;
- event_elt->name = create_name(id);
- event_elt->afb_event = afb_daemon_make_event(interface->daemon, event_elt->name);
+ event_loop = afb_daemon_get_event_loop(interface->daemon);
+ rc = sd_event_add_io(event_loop, &source, can_handler.socket, EPOLLIN, on_event, NULL);
+ if (rc < 0)
+ {
+ close(can_handler.socket);
+ ERROR(interface, "Can't connect CAN bus %s to the event loop", can_handler.device);
+ } else
+ {
+ NOTICE(interface, "Connected CAN bus %s to the event loop", can_handler.device);
+ }
- return event_elt;
+ return rc;
}
-
/*
* Send all events
*/
@@ -482,34 +464,51 @@ static void send_event()
}
/*
- * Get the event loop running.
- * Will trigger on_event function on EPOLLIN event on socket
- *
- * Return 0 or positive value on success. Else negative value for failure.
+ * called on an event on the CAN bus
*/
-static int connect_to_event_loop()
+static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata)
{
- sd_event *event_loop;
- sd_event_source *source;
- int rc;
+ openxc_CanMessage can_message;
- if (can_handler.socket < 0)
+ can_message = openxc_CanMessage_init_default;
+
+ /* read available data */
+ if ((revents & EPOLLIN) != 0)
{
- return can_handler.socket;
+ read_can(&can_message);
+ send_event();
}
- event_loop = afb_daemon_get_event_loop(interface->daemon);
- rc = sd_event_add_io(event_loop, &source, can_handler.socket, EPOLLIN, on_event, NULL);
- if (rc < 0)
- {
- close(can_handler.socket);
- ERROR(interface, "Can't connect CAN bus %s to the event loop", can_handler.device);
- } else
+ /* check if error or hangup */
+ if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
{
- NOTICE(interface, "Connected CAN bus %s to the event loop", can_handler.device);
+ sd_event_source_unref(s);
+ close(fd);
+ connect_to_event_loop();
}
- return rc;
+ return 0;
+}
+
+/*
+ * get or create an event handler for the type
+ */
+static event *get_event(uint32_t id, enum type type)
+{
+ event *event_elt;
+ can_event *list;
+
+ /* find the can list by id */
+ list = get_event_list_of_id(id);
+
+ /* make the new event */
+ event_elt = (event*)calloc(1, sizeof(event));
+ event_elt->next = event_elt;
+ list->events = event_elt;
+ event_elt->name = create_name(id);
+ event_elt->afb_event = afb_daemon_make_event(interface->daemon, event_elt->name);
+
+ return event_elt;
}
/*************************************************************************/
@@ -617,7 +616,6 @@ static void unsubscribe(struct afb_req req)
}
}
-// TODO: Have to change session management flag to AFB_SESSION_CHECK to use token auth
static const struct afb_verb_desc_v1 verbs[]=
{
{ .name= "subscribe", .session= AFB_SESSION_NONE, .callback= subscribe, .info= "subscribe to notification of CAN bus messages." },
diff --git a/ll-can-binding.h b/ll-can-binding.h
index df2c5cd6..0a6a2306 100644
--- a/ll-can-binding.h
+++ b/ll-can-binding.h
@@ -76,9 +76,6 @@ struct _can_event {
can_event *can_events_list;
-/* TODO : Add help comments :p */
-static int connect_to_event_loop();
-
// Initialize default can_handler values
static struct can_handler can_handler = {
.socket = -1,
@@ -86,12 +83,7 @@ static struct can_handler can_handler = {
.is_fdmode_on = false,
};
-static void send_event();
-
-static int retry( int(*func)());
-
-static int parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *canfd_frame, int maxdlen);
-
+/* Redefining openxc_CanMessage_init_default for C */
#ifdef openxc_CanMessage_init_default
#undef openxc_CanMessage_init_default
#endif