summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-01-27 19:11:33 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-01-30 13:34:57 +0000
commit9650f4238498e09230ff76bf458043be9ef00df2 (patch)
tree43ffd18f5fad9ba56a977242656e0c44a0773492
parent48fa36ee26b91de5b64fc0b4263f1a7f12d5b6fc (diff)
Check that socket is opened before read.
And some cleanup Change-Id: Ib3ccf158f141a55e3356f811985d5dcf0dd5fe0c Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--ll-can-binding.c124
-rw-r--r--ll-can-binding.h43
2 files changed, 95 insertions, 72 deletions
diff --git a/ll-can-binding.c b/ll-can-binding.c
index bc52d17f..ae748b9d 100644
--- a/ll-can-binding.c
+++ b/ll-can-binding.c
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
#define _GNU_SOURCE
#include <string.h>
@@ -37,7 +38,7 @@
#include <afb/afb-binding.h>
#include <afb/afb-service-itf.h>
-#include "canLL-binding.h"
+#include "ll-can-binding.h"
/*************************************************************************/
/*************************************************************************/
@@ -57,7 +58,6 @@
* return : 0 if ok, -1 if failed
*
*/
-static int retry( int(*func)());
static int retry( int(*func)())
{
int i;
@@ -74,6 +74,23 @@ static int retry( int(*func)())
}
/*
+ * Test that socket is really opened
+ *
+ * param
+ *
+ * return : 0 or positive int if ok, negative value if failed
+ *
+ */
+static int socket_test()
+{
+ if (can_handler.socket < 0)
+ {
+ return -1;
+ }
+ return 0;
+}
+
+/*
* Browse chained list and return the one with specified id
*
* param uint32_t id : can arbitration identifier
@@ -113,7 +130,7 @@ static can_event *get_event_of_id(uint32_t id)
/*
* Take an id and return it into a char array
*/
-char* create_name(uint32_t id)
+static char* create_name(uint32_t id)
{
char name[32];
size_t nchar;
@@ -130,6 +147,33 @@ char* create_name(uint32_t id)
return NULL;
}
+/*
+ * Create json object that will be pushed through event_loop to any subscriber
+ *
+ * param : openxc_CanMessage structure complete with data to put into json
+ * object.
+ *
+ * return : json object
+ *
+ *
+ */
+static json_object* create_json_from_openxc_CanMessage(event *event)
+{
+ struct json_object *json;
+ openxc_CanMessage can_message;
+
+ /*
+ * TODO: process the openxc_CanMessage struct. Should be a call to a
+ * decoder function relative to that msg
+ */
+ can_message = event->can_message;
+
+ json = json_object_new_object();
+ json_object_object_add(json, "name", json_object_new_string(event->name));
+
+ return json;
+}
+
/*************************************************************************/
/*************************************************************************/
/** **/
@@ -227,13 +271,22 @@ static int write_can()
/*
* Read on CAN bus and return how much bytes has been read.
- * TODO : test that socket is really opened
*/
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)
@@ -249,7 +302,7 @@ static int read_can(openxc_CanMessage *can_message)
if (errno == ENETDOWN)
ERROR(interface, "read_can: %s interface down", can_handler.device);
ERROR(interface, "read_can: Error reading CAN bus");
- return -1;
+ return -2;
}
/* CAN frame integrity check */
@@ -260,7 +313,7 @@ static int read_can(openxc_CanMessage *can_message)
else
{
ERROR(interface, "read_can: CAN frame incomplete");
- return -2;
+ return -3;
}
parse_can_frame(can_message, &canfd_frame, maxdlen);
@@ -275,52 +328,49 @@ static void parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *
{
int i,offset;
int len = (canfd_frame->len > maxdlen) ? maxdlen : canfd_frame->len;
- char buf[CL_CFSZ];
+ can_message->has_id = true;
if (canfd_frame->can_id & CAN_ERR_FLAG)
+ can_message->id = canfd_frame->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+ else if (canfd_frame->can_id & CAN_EFF_FLAG)
{
- can_message->has_id = true;
- can_message->id = canfd_frame->can_id;
- put_eff_id(buf, canfd_frame->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
- buf[8] = '#';
- offset = 9;
- } else if (canfd_frame->can_id & CAN_EFF_FLAG)
+ can_message->has_frame_format = true;
+ can_message->frame_format = openxc_CanMessage_FrameFormat_EXTENDED;
+ can_message->id = canfd_frame->can_id & CAN_EFF_MASK;
+ } else
{
- put_eff_id(buf, canfd_frame->can_id & CAN_EFF_MASK);
- buf[8] = '#';
- offset = 9;
- } else {
- put_sff_id(buf, canfd_frame->can_id & CAN_SFF_MASK);
- buf[3] = '#';
- offset = 4;
+ can_message->has_frame_format = true;
+ can_message->frame_format = openxc_CanMessage_FrameFormat_STANDARD;
+ can_message->id = canfd_frame->can_id & CAN_SFF_MASK;
}
/* standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
if (maxdlen == CAN_MAX_DLEN && canfd_frame->can_id & CAN_RTR_FLAG)
{
- buf[offset++] = 'R';
- /* print a given CAN 2.0B DLC if it's not zero */
+ /* Don't know what to do with that for now as we haven't
+ * len fields in openxc_CanMessage struct
+ *
+ * print a given CAN 2.0B DLC if it's not zero
if (canfd_frame->len && canfd_frame->len <= CAN_MAX_DLC)
buf[offset++] = hex_asc_upper[canfd_frame->len & 0xF];
- buf[offset] = 0;
+ buf[offset] = 0;*/
return;
}
if (maxdlen == CANFD_MAX_DLEN)
{
/* add CAN FD specific escape char and flags */
- buf[offset++] = '#';
- buf[offset++] = hex_asc_upper[canfd_frame->flags & 0xF];
+ canfd_frame->flags & 0xF;
}
for (i = 0; i < len; i++)
{
- put_hex_byte(buf + offset, canfd_frame->data[i]);
- offset += 2;
+ //put_hex_byte(buf + offset, canfd_frame->data[i]);
+ //offset += 2;
}
- buf[offset] = 0;
+// buf[offset] = 0;
return;
}
@@ -381,17 +431,26 @@ static event *get_event(uint32_t id, enum type type)
}
/*
- * Send an event
+ * Send all events
*/
static void send_event()
{
can_event *current;
+ event *events;
+ json_object *object;
- /* search for id */
+ /* Browse can_events */
current = can_events_list;
while(current)
{
- afb_event_push(current->afb_event, object);
+ /* Browse event for each can_events no matter what the id */
+ events = current->events;
+ while(events)
+ {
+ object = create_json_from_openxc_CanMessage(events);
+ afb_event_push(events->afb_event, object);
+ events = events->next;
+ }
current = current->next;
}
}
@@ -427,7 +486,6 @@ static int connect_to_event_loop()
return rc;
}
-
/*************************************************************************/
/*************************************************************************/
/** **/
@@ -522,7 +580,7 @@ static void unsubscribe(struct afb_req req)
afb_req_fail(req, "bad-id", NULL);
else
{
- afb_req_unsubscribe(req, event->event);
+ afb_req_unsubscribe(req, event->afb_event);
afb_req_success(req, NULL, NULL);
}
}
diff --git a/ll-can-binding.h b/ll-can-binding.h
index 3b0bd646..1c993d7f 100644
--- a/ll-can-binding.h
+++ b/ll-can-binding.h
@@ -15,19 +15,6 @@
* limitations under the License.
*/
-/* max. number of CAN interfaces given on the cmdline */
-#define MAXSOCK 16
-
-/* buffer sizes for CAN frame string representations */
-#define CL_ID (sizeof("12345678##1"))
-#define CL_DATA sizeof(".AA")
-#define CL_BINDATA sizeof(".10101010")
-
- /* CAN FD ASCII hex short representation with DATA_SEPERATORs */
-#define CL_CFSZ (2*CL_ID + 64*CL_DATA)
-
-#define CANID_DELIM '#'
-
/*
* Interface between the daemon and the binding
*/
@@ -92,32 +79,6 @@ can_event *can_events_list;
/* TODO : Add help comments :p */
static int connect_to_event_loop();
-const char hex_asc_upper[] = "0123456789ABCDEF";
-
-#define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0F)]
-#define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xF0) >> 4]
-
-static inline void put_hex_byte(char *buf, __u8 byte)
-{
- buf[0] = hex_asc_upper_hi(byte);
- buf[1] = hex_asc_upper_lo(byte);
-}
-
-static inline void _put_id(char *buf, int end_offset, canid_t id)
-{
- /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
- while (end_offset >= 0) {
- buf[end_offset--] = hex_asc_upper[id & 0xF];
- id >>= 4;
- }
-}
-
-#define put_sff_id(buf, id) _put_id(buf, 2, id)
-#define put_eff_id(buf, id) _put_id(buf, 7, id)
-
-static void parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *canfd_frame, int maxdlen);
-
-
// Initialize default can_handler values
static struct can_handler can_handler = {
.socket = -1,
@@ -126,3 +87,7 @@ static struct can_handler can_handler = {
};
static void send_event();
+
+static int retry( int(*func)());
+
+static void parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *canfd_frame, int maxdlen);