Draft work using more generic bitfield functions.
[apps/low-level-can-service.git] / src / isotp / receive.c
1 #include <isotp/receive.h>
2
3 void isotp_handle_single_frame(IsoTpHandler* handler, IsoTpMessage* message) {
4     isotp_complete_receive(handler, message);
5 }
6
7 void isotp_complete_receive(IsoTpHandler* handler, IsoTpMessage* message) {
8     handler->message_received_callback(message);
9 }
10
11 void isotp_receive_can_frame(IsoTpHandler* handler,
12         const uint16_t arbitration_id, const uint64_t data,
13         const uint8_t length) {
14     if(arbitration_id != handler->arbitration_id){
15         return;
16     }
17
18     // TODO use CanMessage struct from canutil library - allocate payload buffer
19     // on stack, 8 bytes
20     IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
21             getBitField(data, 0, 4, false);
22
23     IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
24             getNibble(0, data, 64, LITTE_ENDIAN);
25
26     // TODO this is messed up! need a better API for grabbing bytes
27     uint8_t payload_length = getBitField(data, 4, 4, false);
28     uint8_t payload[payload_length];
29     uint64_t flipped_data = __builtin_bswap64(data);
30     if(payload_length > 0) {
31         memcpy(payload, &(((uint8_t*)&flipped_data)[1]), payload_length);
32     }
33
34     IsoTpMessage message = {
35         arbitration_id: arbitration_id,
36         payload: payload,
37         size: payload_length
38     };
39
40     switch(pci) {
41         case PCI_SINGLE:
42             isotp_handle_single_frame(handler, &message);
43             break;
44         default:
45             handler->shims->log("Only single frame messages are supported");
46             break;
47     }
48 }
49