diff options
author | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-28 14:15:24 -0500 |
---|---|---|
committer | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2013-12-28 14:15:24 -0500 |
commit | fd2f9c3b0d869e8243c871e66d31da62bc65887a (patch) | |
tree | befc24b26ceda4d0e957edd1efc1cc083c89ee81 /src/isotp/send.c | |
parent | 45530604e0d62843c75a49e25d2269f10dc9eb4f (diff) |
Encapsulate arb_id, payload and size into a data type.
Diffstat (limited to 'src/isotp/send.c')
-rw-r--r-- | src/isotp/send.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/isotp/send.c b/src/isotp/send.c new file mode 100644 index 00000000..b72d1b91 --- /dev/null +++ b/src/isotp/send.c @@ -0,0 +1,54 @@ +#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 + +void isotp_complete_send(IsoTpHandler* handler, IsoTpMessage* message, + bool status) { + handler->message_sent_callback(message, status); +} + +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 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); + } + handler->shims->send_can_message(message->arbitration_id, data_array, sizeof(data_array)); + isotp_complete_send(handler, message, true); + return true; +} + +bool isotp_send_multi_frame(IsoTpHandler* handler, IsoTpMessage* message) { + // TODO make sure to copy payload into a local buffer + handler->shims->log("Only single frame messages are supported"); + return false; +} + +bool isotp_send(IsoTpHandler* handler, const uint8_t* payload, + uint16_t size) { + // we determine if it's single/multi frame and start the send + IsoTpMessage message = { + arbitration_id: handler->arbitration_id, + payload: payload, + size: size + }; + + if(size < 8) { + return isotp_send_single_frame(handler, &message); + } else { + return isotp_send_multi_frame(handler, &message); + } +} |