summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristopher Peplin <chris.peplin@rhubarbtech.com>2014-01-02 17:14:56 -0500
committerChristopher Peplin <chris.peplin@rhubarbtech.com>2014-01-02 17:14:56 -0500
commit3b25a0491ce9ef9b55c903c6c7f0929bc2910d1a (patch)
tree95b61b97468093c079a19a9ad8b8f2100cf6e983 /src
parente3637d97ecaef1768d3f9ef40cb0204a0e668ff2 (diff)
Allocate ISO-TP message buffer on the stack.
Diffstat (limited to 'src')
-rw-r--r--src/isotp/isotp.c21
-rw-r--r--src/isotp/isotp.h11
-rw-r--r--src/isotp/send.c6
3 files changed, 26 insertions, 12 deletions
diff --git a/src/isotp/isotp.c b/src/isotp/isotp.c
index 1605d435..67c644f1 100644
--- a/src/isotp/isotp.c
+++ b/src/isotp/isotp.c
@@ -2,8 +2,6 @@
#include <isotp/receive.h>
#include <bitfield/bitfield.h>
-const uint16_t MAX_ISO_TP_MESSAGE_SIZE = 4096;
-const uint16_t MAX_CAN_FRAME_SIZE = 8;
const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT = 100;
const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS = true;
@@ -24,10 +22,11 @@ IsoTpShims isotp_init_shims(LogShim log, SendCanMessageShim send_can_message,
void isotp_message_to_string(const IsoTpMessage* message, char* destination,
size_t destination_length) {
char payload_string[message->size * 2 + 1];
+ memset(payload_string, 0, sizeof(payload_string));
for(int i = 0; i < message->size; i++) {
// TODO, bah this isn't working because snprintf hits the NULL char that
// it wrote the last time and stops cold
- snprintf(&payload_string[i * 2], 2, "%02x", message->payload[i]);
+ /* snprintf(&payload_string[i * 2], 2, "%02x", message->payload[i]); */
}
snprintf(destination, destination_length, "ID: 0x%02x, Payload: 0x%s",
message->arbitration_id, payload_string);
@@ -38,7 +37,9 @@ IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle,
const uint8_t data_length) {
IsoTpMessage message = {
arbitration_id: arbitration_id,
- completed: false
+ completed: false,
+ payload: {0},
+ size: 0
};
if(data_length < 1) {
@@ -47,10 +48,18 @@ IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle,
if(handle->type == ISOTP_HANDLE_RECEIVING) {
if(handle->receive_handle.arbitration_id != arbitration_id) {
+ if(shims->log != NULL) {
+ shims->log("The arb ID 0x%x doesn't match the expected rx ID 0x%x",
+ arbitration_id, handle->receive_handle.arbitration_id);
+ }
return message;
}
} else if(handle->type == ISOTP_HANDLE_SENDING) {
if(handle->send_handle.receiving_arbitration_id != arbitration_id) {
+ if(shims->log != NULL) {
+ shims->log("The arb ID 0x%x doesn't match the expected tx continuation ID 0x%x",
+ arbitration_id, handle->send_handle.receiving_arbitration_id);
+ }
return message;
}
} else {
@@ -73,7 +82,9 @@ IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle,
switch(pci) {
case PCI_SINGLE: {
- message.payload = payload;
+ if(payload_length > 0) {
+ memcpy(message.payload, payload, payload_length);
+ }
message.size = payload_length;
message.completed = true;
handle->success = true;
diff --git a/src/isotp/isotp.h b/src/isotp/isotp.h
index c0db0178..adf0f247 100644
--- a/src/isotp/isotp.h
+++ b/src/isotp/isotp.h
@@ -6,19 +6,22 @@
#include <stdio.h>
#define CAN_MESSAGE_BYTE_SIZE 8
+#define MAX_ISO_TP_MESSAGE_SIZE 4096
+// TODO we want to avoid malloc, and we can't be allocated 4K on the stack for
+// each IsoTpMessage, so for now we're setting an artificial max message size
+// here - since we only handle single frame messages, 8 bytes is plenty.
+#define OUR_MAX_ISO_TP_MESSAGE_SIZE 8
#ifdef __cplusplus
extern "C" {
#endif
-const uint16_t MAX_ISO_TP_MESSAGE_SIZE;
-const uint16_t MAX_CAN_FRAME_SIZE;
const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS;
typedef struct {
const uint16_t arbitration_id;
- uint8_t* payload;
+ uint8_t payload[OUR_MAX_ISO_TP_MESSAGE_SIZE];
uint16_t size;
bool completed;
} IsoTpMessage;
@@ -121,7 +124,7 @@ void isotp_message_to_string(const IsoTpMessage* message, char* destination,
size_t destination_length);
IsoTpHandle isotp_send(IsoTpShims* shims, const uint16_t arbitration_id,
- const uint8_t* payload, uint16_t size,
+ const uint8_t payload[], uint16_t size,
IsoTpMessageSentHandler callback);
IsoTpHandle isotp_receive(IsoTpShims* shims,
diff --git a/src/isotp/send.c b/src/isotp/send.c
index 85e35744..b87c5602 100644
--- a/src/isotp/send.c
+++ b/src/isotp/send.c
@@ -44,7 +44,7 @@ IsoTpHandle isotp_send_single_frame(IsoTpShims* shims, IsoTpMessage* message,
IsoTpHandle isotp_send_multi_frame(IsoTpShims* shims, IsoTpMessage* message,
IsoTpMessageSentHandler callback) {
- // TODO make sure to copy payload into a local buffer
+ // TODO make sure to copy message into a local buffer
shims->log("Only single frame messages are supported");
IsoTpHandle handle = {
success: false,
@@ -57,14 +57,14 @@ IsoTpHandle isotp_send_multi_frame(IsoTpShims* shims, IsoTpMessage* message,
}
IsoTpHandle isotp_send(IsoTpShims* shims, const uint16_t arbitration_id,
- const uint8_t* payload, uint16_t size,
+ const uint8_t payload[], uint16_t size,
IsoTpMessageSentHandler callback) {
IsoTpMessage message = {
arbitration_id: arbitration_id,
- payload: payload,
size: size
};
+ memcpy(message.payload, payload, size);
if(size < 8) {
return isotp_send_single_frame(shims, &message, callback);
} else {