Test basic rx of a single frame message.
[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_receive_can_frame(IsoTpHandler* handler,
11         const uint16_t arbitration_id, const uint64_t data,
12         const uint8_t length) {
13     if(arbitration_id != handler->arbitration_id){
14         return;
15     }
16
17     // TODO use CanMessage struct from canutil library - allocate payload buffer
18     // on stack, 8 bytes
19     // TODO  this function should receive uint64_t...
20     IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
21             getBitField(data, 0, 4, false);
22
23     // TODO this is messed up! need a better API for grabbing bytes
24     uint8_t payload_length = getBitField(data, 4, 4, false);
25     uint8_t payload[payload_length];
26     uint64_t flipped_data = __builtin_bswap64(data);
27     if(payload_length > 0) {
28         memcpy(payload, &(((uint8_t*)&flipped_data)[1]), payload_length);
29     }
30
31     switch(pci) {
32         case PCI_SINGLE:
33             isotp_handle_single_frame(handler, arbitration_id, payload,
34                     payload_length);
35             break;
36         default:
37             handler->shims->log("Only single frame messages are supported");
38             break;
39     }
40 }
41
42 bool isotp_send(const uint8_t* payload, uint16_t size) {
43      // we determine if it's single/multi frame and start the send
44 }
45
46 void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms) {
47     handler->timeout_ms = timeout_ms;
48 }
49
50 IsoTpShims isotp_init_shims(LogShim log, SendCanMessageShim send_can_message,
51         SetTimerShim set_timer) {
52     IsoTpShims shims = {
53         log: log,
54         send_can_message: send_can_message,
55         set_timer: set_timer
56     };
57     return shims;
58 }
59
60 IsoTpHandler isotp_init(IsoTpShims* shims, uint16_t arbitration_id,
61         IsoTpMessageReceivedHandler message_received_callback,
62         IsoTpMessageSentHandler message_sent_callback,
63         IsoTpCanFrameSentHandler can_frame_sent_callback) {
64     IsoTpHandler handler = {
65         shims: shims,
66         arbitration_id: arbitration_id,
67         message_received_callback: message_received_callback,
68         message_sent_callback: message_sent_callback,
69         can_frame_sent_callback: can_frame_sent_callback,
70         timeout_ms: ISO_TP_DEFAULT_RESPONSE_TIMEOUT,
71         frame_padding: ISO_TP_DEFAULT_FRAME_PADDING_STATUS,
72         sending: false
73     };
74     return handler;
75 }
76
77 // TODO this would be better as a "isotp_message_to_string"
78 void log_isotp_message(const uint16_t arbitration_id,
79         const uint8_t* payload, const uint16_t size) {
80     debug("ID: 0x%02x", arbitration_id);
81     if(size > 0) {
82         debug("Payload:");
83         for(int i = 0; i < size; i++) {
84             debug("0x%x", payload[i]);
85         }
86     }  else {
87         debug("(no payload)");
88     }
89 }