1605d435450a1da1eb0ae61fdafb59ed9fae2d18
[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 uint16_t MAX_ISO_TP_MESSAGE_SIZE = 4096;
6 const uint16_t MAX_CAN_FRAME_SIZE = 8;
7 const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT = 100;
8 const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS = true;
9
10 /* void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms) { */
11     /* handler->timeout_ms = timeout_ms; */
12 /* } */
13
14 IsoTpShims isotp_init_shims(LogShim log, SendCanMessageShim send_can_message,
15         SetTimerShim set_timer) {
16     IsoTpShims shims = {
17         log: log,
18         send_can_message: send_can_message,
19         set_timer: set_timer
20     };
21     return shims;
22 }
23
24 void isotp_message_to_string(const IsoTpMessage* message, char* destination,
25         size_t destination_length) {
26     char payload_string[message->size * 2 + 1];
27     for(int i = 0; i < message->size; i++) {
28         // TODO, bah this isn't working because snprintf hits the NULL char that
29         // it wrote the last time and stops cold
30         snprintf(&payload_string[i * 2], 2, "%02x", message->payload[i]);
31     }
32     snprintf(destination, destination_length, "ID: 0x%02x, Payload: 0x%s",
33             message->arbitration_id, payload_string);
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     };
43
44     if(data_length < 1) {
45         return message;
46     }
47
48     if(handle->type == ISOTP_HANDLE_RECEIVING) {
49         if(handle->receive_handle.arbitration_id != arbitration_id) {
50             return message;
51         }
52     } else if(handle->type == ISOTP_HANDLE_SENDING) {
53         if(handle->send_handle.receiving_arbitration_id != arbitration_id) {
54             return message;
55         }
56     } else {
57         shims->log("The ISO-TP handle is corrupt");
58         return message;
59     }
60
61     IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
62             get_nibble(data, data_length, 0);
63
64     uint8_t payload_length = get_nibble(data, data_length, 1);
65     uint8_t payload[payload_length];
66     if(payload_length > 0 && data_length > 0) {
67         memcpy(payload, &data[1], payload_length);
68     }
69
70     // TODO this is set up to handle rx a response with a payload, but not to
71     // handle flow control responses for multi frame messages that we're in the
72     // process of sending
73
74     switch(pci) {
75         case PCI_SINGLE: {
76             message.payload = payload;
77             message.size = payload_length;
78             message.completed = true;
79             handle->success = true;
80             handle->completed = true;
81             isotp_handle_single_frame(handle, &message);
82             break;
83          }
84         default:
85             shims->log("Only single frame messages are supported");
86             break;
87     }
88     return message;
89 }