Test rx an empty CAN messages.
[apps/agl-service-can-low-level.git] / src / isotp / isotp.c
1 #include <isotp/isotp.h>
2 #include <isotp/receive.h>
3 #include <bitfield/bitfield.h>
4
5 const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT = 100;
6 const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS = true;
7
8 /* void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms) { */
9     /* handler->timeout_ms = timeout_ms; */
10 /* } */
11
12 IsoTpShims isotp_init_shims(LogShim log, SendCanMessageShim send_can_message,
13         SetTimerShim set_timer) {
14     IsoTpShims shims = {
15         log: log,
16         send_can_message: send_can_message,
17         set_timer: set_timer
18     };
19     return shims;
20 }
21
22 void isotp_message_to_string(const IsoTpMessage* message, char* destination,
23         size_t destination_length) {
24     snprintf(destination, destination_length, "ID: 0x%02x, Payload: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
25             message->arbitration_id,
26             message->payload[0],
27             message->payload[1],
28             message->payload[2],
29             message->payload[3],
30             message->payload[4],
31             message->payload[5],
32             message->payload[6],
33             message->payload[7]);
34 }
35
36 IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle,
37         const uint16_t arbitration_id, const uint8_t data[],
38         const uint8_t data_length) {
39     IsoTpMessage message = {
40         arbitration_id: arbitration_id,
41         completed: false,
42         payload: {0},
43         size: 0
44     };
45
46     if(data_length < 1) {
47         return message;
48     }
49
50     if(handle->type == ISOTP_HANDLE_RECEIVING) {
51         if(handle->receive_handle.arbitration_id != arbitration_id) {
52             if(shims->log != NULL)  {
53                 shims->log("The arb ID 0x%x doesn't match the expected rx ID 0x%x",
54                         arbitration_id, handle->receive_handle.arbitration_id);
55             }
56             return message;
57         }
58     } else if(handle->type == ISOTP_HANDLE_SENDING) {
59         // TODO this will need to be tested when we add multi-frame support,
60         // which is when it'll be necessary to pass in CAN messages to SENDING
61         // handles.
62         if(handle->send_handle.receiving_arbitration_id != arbitration_id) {
63             if(shims->log != NULL) {
64                 shims->log("The arb ID 0x%x doesn't match the expected tx continuation ID 0x%x",
65                         arbitration_id, handle->send_handle.receiving_arbitration_id);
66             }
67             return message;
68         }
69     } else {
70         shims->log("The ISO-TP handle is corrupt");
71         return message;
72     }
73
74     IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
75             get_nibble(data, data_length, 0);
76
77     uint8_t payload_length = get_nibble(data, data_length, 1);
78     uint8_t payload[payload_length];
79     if(payload_length > 0 && data_length > 0) {
80         memcpy(payload, &data[1], payload_length);
81     }
82
83     // TODO this is set up to handle rx a response with a payload, but not to
84     // handle flow control responses for multi frame messages that we're in the
85     // process of sending
86
87     switch(pci) {
88         case PCI_SINGLE: {
89             if(payload_length > 0) {
90                 memcpy(message.payload, payload, payload_length);
91             }
92             message.size = payload_length;
93             message.completed = true;
94             handle->success = true;
95             handle->completed = true;
96             isotp_handle_single_frame(handle, &message);
97             break;
98          }
99         default:
100             shims->log("Only single frame messages are supported");
101             break;
102     }
103     return message;
104 }