2dc380bca0a5725e945cbcbbc5d45605af91d469
[apps/agl-service-can-low-level.git] / src / isotp / receive.c
1 #include <isotp/receive.h>
2 #include <bitfield/bitfield.h>
3 #include <string.h>
4
5 static void isotp_complete_receive(IsoTpReceiveHandle* handle, IsoTpMessage* message) {
6     if(handle->message_received_callback != NULL) {
7         handle->message_received_callback(message);
8     }
9 }
10
11 bool isotp_handle_single_frame(IsoTpReceiveHandle* handle, IsoTpMessage* message) {
12     isotp_complete_receive(handle, message);
13     return true;
14 }
15
16 IsoTpReceiveHandle isotp_receive(IsoTpShims* shims,
17         const uint32_t arbitration_id, IsoTpMessageReceivedHandler callback) {
18     IsoTpReceiveHandle handle = {
19         success: false,
20         completed: false,
21         arbitration_id: arbitration_id,
22         message_received_callback: callback
23     };
24
25     return handle;
26 }
27
28 IsoTpMessage isotp_continue_receive(IsoTpShims* shims,
29         IsoTpReceiveHandle* handle, const uint32_t arbitration_id,
30         const uint8_t data[], const uint8_t size) {
31     IsoTpMessage message = {
32         arbitration_id: arbitration_id,
33         completed: false,
34         payload: {0},
35         size: 0
36     };
37
38     if(size < 1) {
39         return message;
40     }
41
42     if(handle->arbitration_id != arbitration_id) {
43         if(shims->log != NULL)  {
44             // You may turn this on for debugging, but in normal operation it's
45             // very noisy if you are passing all received CAN messages to this
46             // handler.
47             /* shims->log("The arb ID 0x%x doesn't match the expected rx ID 0x%x", */
48                     /* arbitration_id, handle->arbitration_id); */
49         }
50         return message;
51     }
52
53     IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
54             get_nibble(data, size, 0);
55
56     uint8_t payload_length = get_nibble(data, size, 1);
57     uint8_t payload[payload_length];
58     if(payload_length > 0 && size > 0) {
59         memcpy(payload, &data[1], payload_length);
60     }
61
62     // TODO this is set up to handle rx a response with a payload, but not to
63     // handle flow control responses for multi frame messages that we're in the
64     // process of sending
65
66     switch(pci) {
67         case PCI_SINGLE: {
68             if(payload_length > 0) {
69                 memcpy(message.payload, payload, payload_length);
70             }
71             message.size = payload_length;
72             message.completed = true;
73             handle->success = true;
74             handle->completed = true;
75             isotp_handle_single_frame(handle, &message);
76             break;
77          }
78         default:
79             shims->log("Only single frame messages are supported");
80             break;
81     }
82     return message;
83 }