summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Peplin <chris.peplin@rhubarbtech.com>2013-12-29 15:29:08 -0500
committerChristopher Peplin <chris.peplin@rhubarbtech.com>2013-12-29 15:29:08 -0500
commitf13267feabd7684e0de3f59b6c39f23624489274 (patch)
tree29905349a3788569e0af0153346feaa3d3f72fe2
parentfb3c14776f6eaaae69b6691ec912ecdcbce0cba7 (diff)
Use updated API for canutil library.
m---------deps/canutil10
-rw-r--r--src/isotp/isotp.h2
-rw-r--r--src/isotp/receive.c21
-rw-r--r--src/isotp/receive.h3
-rw-r--r--src/isotp/send.c35
-rw-r--r--tests/test_receive.c16
6 files changed, 42 insertions, 45 deletions
diff --git a/deps/canutil b/deps/canutil
-Subproject 0b00bb6c3c5df70317775efd12b6fff418ff454
+Subproject 518430f122772016715660086a630ff595e4fcf
diff --git a/src/isotp/isotp.h b/src/isotp/isotp.h
index a204110f..619dcc69 100644
--- a/src/isotp/isotp.h
+++ b/src/isotp/isotp.h
@@ -4,6 +4,8 @@
#include <stdint.h>
#include <stdbool.h>
+#define CAN_MESSAGE_BYTE_SIZE 8
+
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/src/isotp/receive.c b/src/isotp/receive.c
index 51562fcc..bd94e7aa 100644
--- a/src/isotp/receive.c
+++ b/src/isotp/receive.c
@@ -9,26 +9,19 @@ void isotp_complete_receive(IsoTpHandler* handler, IsoTpMessage* message) {
}
void isotp_receive_can_frame(IsoTpHandler* handler,
- const uint16_t arbitration_id, const uint64_t data,
- const uint8_t length) {
- if(arbitration_id != handler->arbitration_id){
+ const uint16_t arbitration_id, const uint8_t data[],
+ const uint8_t data_length) {
+ if(arbitration_id != handler->arbitration_id || data_length < 1) {
return;
}
- // TODO use CanMessage struct from canutil library - allocate payload buffer
- // on stack, 8 bytes
IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
- getBitField(data, 0, 4, false);
+ get_nibble(data, data_length, 0);
- IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
- getNibble(0, data, 64, LITTE_ENDIAN);
-
- // TODO this is messed up! need a better API for grabbing bytes
- uint8_t payload_length = getBitField(data, 4, 4, false);
+ uint8_t payload_length = get_nibble(data, data_length, 1);
uint8_t payload[payload_length];
- uint64_t flipped_data = __builtin_bswap64(data);
- if(payload_length > 0) {
- memcpy(payload, &(((uint8_t*)&flipped_data)[1]), payload_length);
+ if(payload_length > 0 && data_length > 0) {
+ memcpy(payload, &data[1], payload_length);
}
IsoTpMessage message = {
diff --git a/src/isotp/receive.h b/src/isotp/receive.h
index 0ff9e8f1..feca993c 100644
--- a/src/isotp/receive.h
+++ b/src/isotp/receive.h
@@ -14,7 +14,8 @@ void isotp_handle_single_frame(IsoTpHandler* handler, IsoTpMessage* message);
void isotp_complete_receive(IsoTpHandler* handler, IsoTpMessage* message);
void isotp_receive_can_frame(IsoTpHandler* handler,
- const uint16_t arbitration_id, const uint64_t data, const uint8_t size);
+ const uint16_t arbitration_id, const uint8_t data[],
+ const uint8_t size);
#ifdef __cplusplus
}
diff --git a/src/isotp/send.c b/src/isotp/send.c
index b72d1b91..ae208eb3 100644
--- a/src/isotp/send.c
+++ b/src/isotp/send.c
@@ -1,10 +1,8 @@
#include <isotp/send.h>
-#define PCI_START_BIT 0
-#define PCI_WIDTH 4
-#define PAYLOAD_LENGTH_START_BIT PCI_START_BIT + PCI_WIDTH
-#define PAYLOAD_LENGTH_WIDTH 4
-#define PAYLOAD_START_BIT PAYLOAD_LENGTH_START_BIT + PAYLOAD_LENGTH_WIDTH
+#define PCI_NIBBLE_INDEX 0
+#define PAYLOAD_LENGTH_NIBBLE_INDEX 1
+#define PAYLOAD_BYTE_INDEX 1
void isotp_complete_send(IsoTpHandler* handler, IsoTpMessage* message,
bool status) {
@@ -12,21 +10,24 @@ void isotp_complete_send(IsoTpHandler* handler, IsoTpMessage* message,
}
bool isotp_send_single_frame(IsoTpHandler* handler, IsoTpMessage* message) {
- uint64_t data = 0;
- setBitField(&data, PCI_SINGLE, PCI_START_BIT, PCI_WIDTH);
- setBitField(&data, message->size, PAYLOAD_LENGTH_START_BIT, PAYLOAD_LENGTH_WIDTH);
- // TODO need a better bitfield API to support this - use byte array instead
- // of uint64_t and specify desired total width
- for(int i = 0; i < message->size; i++) {
- setBitField(&data, message->payload[i], PAYLOAD_START_BIT + i * 8, 8);
+ uint8_t can_data[CAN_MESSAGE_BYTE_SIZE] = {0};
+ if(!set_nibble(PCI_NIBBLE_INDEX, PCI_SINGLE, can_data, sizeof(can_data))) {
+ handler->shims->log("Unable to set PCI in CAN data");
+ return false;
}
- uint8_t data_array[message->size + 1];
- for(int i = 0; i < sizeof(data_array); i++) {
- // TODO need getByte(x) function
- data_array[i] = getBitField(data, i * 8, 8, false);
+ if(!set_nibble(PAYLOAD_LENGTH_NIBBLE_INDEX, message->size, can_data,
+ sizeof(can_data))) {
+ handler->shims->log("Unable to set payload length in CAN data");
+ return false;
}
- handler->shims->send_can_message(message->arbitration_id, data_array, sizeof(data_array));
+
+ if(message->size > 0) {
+ memcpy(&can_data[1], message->payload, message->size);
+ }
+
+ handler->shims->send_can_message(message->arbitration_id, can_data,
+ 1 + message->size);
isotp_complete_send(handler, message, true);
return true;
}
diff --git a/tests/test_receive.c b/tests/test_receive.c
index d624a1fc..b116dd23 100644
--- a/tests/test_receive.c
+++ b/tests/test_receive.c
@@ -27,8 +27,8 @@ extern void setup();
START_TEST (test_receive_wrong_id)
{
- const uint64_t data = 0;
- isotp_receive_can_frame(&ISOTP_HANDLER, 0x100, data, sizeof(data));
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0};
+ isotp_receive_can_frame(&ISOTP_HANDLER, 0x100, data, 1);
fail_if(message_was_received);
}
END_TEST
@@ -36,16 +36,16 @@ END_TEST
START_TEST (test_receive_bad_pci)
{
// 4 is a reserved number for the PCI field - only 0-3 are allowed
- const uint64_t data = {0x4000000000000000};
- isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, sizeof(data));
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x40};
+ isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, 1);
fail_if(message_was_received);
}
END_TEST
START_TEST (test_receive_single_frame_empty_payload)
{
- const uint64_t data = {0x0012340000000000};
- isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, sizeof(data));
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x00, 0x12, 0x34};
+ isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, 3);
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);
@@ -54,8 +54,8 @@ END_TEST
START_TEST (test_receive_single_frame)
{
- const uint64_t data = {0x0212340000000000};
- isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, sizeof(data));
+ const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x02, 0x12, 0x34};
+ isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, 3);
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);