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