Allocate ISO-TP message buffer on the stack.
[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     char payload_string[message->size * 2 + 1];
25     memset(payload_string, 0, sizeof(payload_string));
26     for(int i = 0; i < message->size; i++) {
27         // TODO, bah this isn't working because snprintf hits the NULL char that
28         // it wrote the last time and stops cold
29         /* snprintf(&payload_string[i * 2], 2, "%02x", message->payload[i]); */
30     }
31     snprintf(destination, destination_length, "ID: 0x%02x, Payload: 0x%s",
32             message->arbitration_id, payload_string);
33 }
34
35 IsoTpMessage isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle,
36         const uint16_t arbitration_id, const uint8_t data[],
37         const uint8_t data_length) {
38     IsoTpMessage message = {
39         arbitration_id: arbitration_id,
40         completed: false,
41         payload: {0},
42         size: 0
43     };
44
45     if(data_length < 1) {
46         return message;
47     }
48
49     if(handle->type == ISOTP_HANDLE_RECEIVING) {
50         if(handle->receive_handle.arbitration_id != arbitration_id) {
51             if(shims->log != NULL)  {
52                 shims->log("The arb ID 0x%x doesn't match the expected rx ID 0x%x",
53                         arbitration_id, handle->receive_handle.arbitration_id);
54             }
55             return message;
56         }
57     } else if(handle->type == ISOTP_HANDLE_SENDING) {
58         if(handle->send_handle.receiving_arbitration_id != arbitration_id) {
59             if(shims->log != NULL) {
60                 shims->log("The arb ID 0x%x doesn't match the expected tx continuation ID 0x%x",
61                         arbitration_id, handle->send_handle.receiving_arbitration_id);
62             }
63             return message;
64         }
65     } else {
66         shims->log("The ISO-TP handle is corrupt");
67         return message;
68     }
69
70     IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
71             get_nibble(data, data_length, 0);
72
73     uint8_t payload_length = get_nibble(data, data_length, 1);
74     uint8_t payload[payload_length];
75     if(payload_length > 0 && data_length > 0) {
76         memcpy(payload, &data[1], payload_length);
77     }
78
79     // TODO this is set up to handle rx a response with a payload, but not to
80     // handle flow control responses for multi frame messages that we're in the
81     // process of sending
82
83     switch(pci) {
84         case PCI_SINGLE: {
85             if(payload_length > 0) {
86                 memcpy(message.payload, payload, payload_length);
87             }
88             message.size = payload_length;
89             message.completed = true;
90             handle->success = true;
91             handle->completed = true;
92             isotp_handle_single_frame(handle, &message);
93             break;
94          }
95         default:
96             shims->log("Only single frame messages are supported");
97             break;
98     }
99     return message;
100 }