1 #include <isotp/isotp.h>
2 #include <isotp/receive.h>
3 #include <bitfield/bitfield.h>
5 const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT = 100;
6 const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS = true;
8 /* void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms) { */
9 /* handler->timeout_ms = timeout_ms; */
12 IsoTpShims isotp_init_shims(LogShim log, SendCanMessageShim send_can_message,
13 SetTimerShim set_timer) {
16 send_can_message: send_can_message,
22 void isotp_message_to_string(const IsoTpMessage* message, char* destination,
23 size_t destination_length) {
24 // TODO why is this still not printing the entire payload?
25 snprintf(destination, destination_length, "ID: 0x%02x, Payload: 0x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
26 message->arbitration_id,
37 IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle,
38 const uint16_t arbitration_id, const uint8_t data[],
39 const uint8_t data_length) {
40 IsoTpMessage message = {
41 arbitration_id: arbitration_id,
51 if(handle->type == ISOTP_HANDLE_RECEIVING) {
52 if(handle->receive_handle.arbitration_id != arbitration_id) {
53 if(shims->log != NULL) {
54 shims->log("The arb ID 0x%x doesn't match the expected rx ID 0x%x",
55 arbitration_id, handle->receive_handle.arbitration_id);
59 } else if(handle->type == ISOTP_HANDLE_SENDING) {
60 if(handle->send_handle.receiving_arbitration_id != arbitration_id) {
61 if(shims->log != NULL) {
62 shims->log("The arb ID 0x%x doesn't match the expected tx continuation ID 0x%x",
63 arbitration_id, handle->send_handle.receiving_arbitration_id);
68 shims->log("The ISO-TP handle is corrupt");
72 IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
73 get_nibble(data, data_length, 0);
75 uint8_t payload_length = get_nibble(data, data_length, 1);
76 uint8_t payload[payload_length];
77 if(payload_length > 0 && data_length > 0) {
78 memcpy(payload, &data[1], payload_length);
81 // TODO this is set up to handle rx a response with a payload, but not to
82 // handle flow control responses for multi frame messages that we're in the
87 if(payload_length > 0) {
88 memcpy(message.payload, payload, payload_length);
90 message.size = payload_length;
91 message.completed = true;
92 handle->success = true;
93 handle->completed = true;
94 isotp_handle_single_frame(handle, &message);
98 shims->log("Only single frame messages are supported");