diff options
author | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2014-01-01 16:08:10 -0500 |
---|---|---|
committer | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2014-01-01 16:10:57 -0500 |
commit | 482c7eafcb35a4031ab6f2241b14ac495735b7cd (patch) | |
tree | 8fdff5d9e5ae49df5ed0909dfdb8fc628674ed80 | |
parent | 8702b34a8ffdf220c9b054d8f1950a82c9387d6a (diff) |
Return completed rx messages instead of relying on callbacks.
-rw-r--r-- | README.mkd | 7 | ||||
-rw-r--r-- | src/isotp/isotp.c | 28 | ||||
-rw-r--r-- | src/isotp/isotp.h | 14 | ||||
-rw-r--r-- | src/isotp/receive.c | 1 | ||||
-rw-r--r-- | src/isotp/send.c | 2 | ||||
-rw-r--r-- | src/isotp/send.h | 3 | ||||
-rw-r--r-- | tests/test_receive.c | 12 |
7 files changed, 34 insertions, 33 deletions
@@ -58,7 +58,8 @@ Then, set up a callback and send an ISO-TP message: Finally, receive an ISO-TP message: // This is your callback for when a complete ISO-TP message is received at - // the arbitration ID you specify + // the arbitration ID you specify - it's optional. The completed message is + // also returned by isotp_receive_can_frame void message_received(const IsoTpMessage* message) { } @@ -69,9 +70,9 @@ Finally, receive an ISO-TP message: } else { while(true) { // Continue to read from CAN, passing off each message to the handle - bool complete = isotp_receive_can_frame(&shims, &handle, 0x100, data, size); + IsoTp message = isotp_receive_can_frame(&shims, &handle, 0x100, data, size); - if(complete && handle.completed) { + if(message.completed && handle.completed) { if(handle.success) { // A message has been received successfully } else { diff --git a/src/isotp/isotp.c b/src/isotp/isotp.c index f7189d2b..9aa051bc 100644 --- a/src/isotp/isotp.c +++ b/src/isotp/isotp.c @@ -33,26 +33,29 @@ void isotp_message_to_string(const IsoTpMessage* message, char* destination, message->arbitration_id, payload_string); } -bool isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle, +IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle, const uint16_t arbitration_id, const uint8_t data[], const uint8_t data_length) { - bool message_completed = false; + IsoTpMessage message = { + arbitration_id: arbitration_id, + completed: false + }; if(data_length < 1) { - return message_completed; + return message; } if(handle->type == ISOTP_HANDLE_RECEIVING) { if(handle->receive_handle.arbitration_id != arbitration_id) { - return message_completed; + return message; } } else if(handle->type == ISOTP_HANDLE_SENDING) { if(handle->send_handle.receiving_arbitration_id != arbitration_id) { - return message_completed; + return message; } } else { shims->log("The ISO-TP handle is corrupt"); - return message_completed; + return message; } IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation) @@ -70,18 +73,15 @@ bool isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle, switch(pci) { case PCI_SINGLE: { - IsoTpMessage message = { - arbitration_id: arbitration_id, - payload: payload, - size: payload_length - }; - - message_completed = isotp_handle_single_frame(handle, &message); + message.payload = payload; + message.size = payload_length; + message.completed = true; + isotp_handle_single_frame(handle, &message); break; } default: shims->log("Only single frame messages are supported"); break; } - return message_completed; + return message; } diff --git a/src/isotp/isotp.h b/src/isotp/isotp.h index 4f58dac4..103c402d 100644 --- a/src/isotp/isotp.h +++ b/src/isotp/isotp.h @@ -18,8 +18,9 @@ const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS; typedef struct { const uint16_t arbitration_id; - const uint8_t* payload; - const uint16_t size; + uint8_t* payload; + uint16_t size; + bool completed; } IsoTpMessage; typedef void (*LogShim)(const char* message, ...); @@ -95,11 +96,8 @@ IsoTpShims isotp_init_shims(LogShim log, /* Public: * - * Returns true if a complete ISO-TP message was sent or received as of - * processing this CAN frame. Check the 'success' and 'completed' flag on the - * handle to make sure. */ -bool isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle, +IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle, const uint16_t arbitration_id, const uint8_t data[], const uint8_t size); @@ -121,8 +119,8 @@ IsoTpHandle isotp_send(IsoTpShims* shims, const uint16_t arbitration_id, const uint8_t* payload, uint16_t size, IsoTpMessageSentHandler callback); -IsoTpHandle isotp_receive(IsoTpShims* shims, const uint16_t arbitration_id, - IsoTpMessageReceivedHandler callback); +IsoTpHandle isotp_receive(IsoTpShims* shims, + const uint16_t arbitration_id, IsoTpMessageReceivedHandler callback); #ifdef __cplusplus } diff --git a/src/isotp/receive.c b/src/isotp/receive.c index e69c59b7..fa96156d 100644 --- a/src/isotp/receive.c +++ b/src/isotp/receive.c @@ -24,4 +24,3 @@ IsoTpHandle isotp_receive(IsoTpShims* shims, }; return handle; } - diff --git a/src/isotp/send.c b/src/isotp/send.c index 67703cae..6cce5c92 100644 --- a/src/isotp/send.c +++ b/src/isotp/send.c @@ -49,6 +49,8 @@ IsoTpHandle isotp_send_multi_frame(IsoTpShims* shims, IsoTpMessage* message, completed: true, type: ISOTP_HANDLE_SENDING }; + // TODO need to set sending and receiving arbitration IDs separately if we + // can't always just add 0x8 (and I think we can't) return handle; } diff --git a/src/isotp/send.h b/src/isotp/send.h index 937e5320..8f707b29 100644 --- a/src/isotp/send.h +++ b/src/isotp/send.h @@ -9,9 +9,6 @@ extern "C" { #endif -IsoTpHandle isotp_send(IsoTpShims* shims, const uint16_t arbitration_id, - const uint8_t* payload, uint16_t size, - IsoTpMessageSentHandler callback); #ifdef __cplusplus } diff --git a/tests/test_receive.c b/tests/test_receive.c index 0559c065..2ce3feff 100644 --- a/tests/test_receive.c +++ b/tests/test_receive.c @@ -28,7 +28,8 @@ extern void setup(); START_TEST (test_receive_wrong_id) { const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0}; - fail_if(isotp_receive_can_frame(&SHIMS, &HANDLE, 0x100, data, 1)); + IsoTpMessage message = isotp_receive_can_frame(&SHIMS, &HANDLE, 0x100, data, 1); + fail_if(message.completed); fail_if(message_was_received); } END_TEST @@ -37,7 +38,8 @@ START_TEST (test_receive_bad_pci) { // 4 is a reserved number for the PCI field - only 0-3 are allowed const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x40}; - fail_if(isotp_receive_can_frame(&SHIMS, &HANDLE, 0x2a, data, 1)); + IsoTpMessage message = isotp_receive_can_frame(&SHIMS, &HANDLE, 0x2a, data, 1); + fail_if(message.completed); fail_if(message_was_received); } END_TEST @@ -45,7 +47,8 @@ END_TEST START_TEST (test_receive_single_frame_empty_payload) { const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x00, 0x12, 0x34}; - fail_unless(isotp_receive_can_frame(&SHIMS, &HANDLE, 0x2a, data, 3)); + IsoTpMessage message = isotp_receive_can_frame(&SHIMS, &HANDLE, 0x2a, data, 3); + fail_unless(message.completed); fail_unless(message_was_received); ck_assert_int_eq(last_message_received_arb_id, 0x2a); ck_assert_int_eq(last_message_received_payload_size, 0); @@ -55,7 +58,8 @@ END_TEST START_TEST (test_receive_single_frame) { const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x02, 0x12, 0x34}; - fail_unless(isotp_receive_can_frame(&SHIMS, &HANDLE, 0x2a, data, 3)); + IsoTpMessage message = isotp_receive_can_frame(&SHIMS, &HANDLE, 0x2a, data, 3); + fail_unless(message.completed); fail_unless(message_was_received); ck_assert_int_eq(last_message_received_arb_id, 0x2a); ck_assert_int_eq(last_message_received_payload_size, 2); |