1 #include <isotp/send.h>
3 #define PCI_NIBBLE_INDEX 0
4 #define PAYLOAD_LENGTH_NIBBLE_INDEX 1
5 #define PAYLOAD_BYTE_INDEX 1
7 void isotp_complete_send(IsoTpShims* shims, IsoTpMessage* message,
8 bool status, IsoTpMessageSentHandler callback) {
10 callback(message, status);
14 IsoTpSendHandle isotp_send_single_frame(IsoTpShims* shims, IsoTpMessage* message,
15 IsoTpMessageSentHandler callback) {
16 IsoTpSendHandle handle = {
21 uint8_t can_data[CAN_MESSAGE_BYTE_SIZE] = {0};
22 if(!set_nibble(PCI_NIBBLE_INDEX, PCI_SINGLE, can_data, sizeof(can_data))) {
23 shims->log("Unable to set PCI in CAN data");
27 if(!set_nibble(PAYLOAD_LENGTH_NIBBLE_INDEX, message->size, can_data,
29 shims->log("Unable to set payload length in CAN data");
33 if(message->size > 0) {
34 memcpy(&can_data[1], message->payload, message->size);
37 shims->send_can_message(message->arbitration_id, can_data,
39 handle.success = true;
40 isotp_complete_send(shims, message, true, callback);
44 IsoTpSendHandle isotp_send_multi_frame(IsoTpShims* shims, IsoTpMessage* message,
45 IsoTpMessageSentHandler callback) {
46 // TODO make sure to copy message into a local buffer
47 shims->log("Only single frame messages are supported");
48 IsoTpSendHandle handle = {
52 // TODO need to set sending and receiving arbitration IDs separately if we
53 // can't always just add 0x8 (and I think we can't)
57 IsoTpSendHandle isotp_send(IsoTpShims* shims, const uint16_t arbitration_id,
58 const uint8_t payload[], uint16_t size,
59 IsoTpMessageSentHandler callback) {
60 IsoTpMessage message = {
61 arbitration_id: arbitration_id,
65 memcpy(message.payload, payload, size);
67 return isotp_send_single_frame(shims, &message, callback);
69 return isotp_send_multi_frame(shims, &message, callback);
73 bool isotp_continue_send(IsoTpShims* shims, IsoTpSendHandle* handle,
74 const uint16_t arbitration_id, const uint8_t data[],
76 // TODO this will need to be tested when we add multi-frame support,
77 // which is when it'll be necessary to pass in CAN messages to SENDING
79 if(handle->receiving_arbitration_id != arbitration_id) {
80 if(shims->log != NULL) {
81 shims->log("The arb ID 0x%x doesn't match the expected tx continuation ID 0x%x",
82 arbitration_id, handle->receiving_arbitration_id);