1 #include <isotp/isotp.h>
2 #include <isotp/receive.h>
3 #include <bitfield/bitfield.h>
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;
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){
17 // TODO use CanMessage struct from canutil library - allocate payload buffer
19 // TODO this function should receive uint64_t...
20 IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
21 getBitField(data, 0, 4, false);
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);
33 isotp_handle_single_frame(handler, arbitration_id, payload,
37 handler->shims->log("Only single frame messages are supported");
42 bool isotp_send(const uint8_t* payload, uint16_t size) {
43 // we determine if it's single/multi frame and start the send
46 void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms) {
47 handler->timeout_ms = timeout_ms;
50 IsoTpShims isotp_init_shims(LogShim log, SendCanMessageShim send_can_message,
51 SetTimerShim set_timer) {
54 send_can_message: send_can_message,
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 = {
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,
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);
83 for(int i = 0; i < size; i++) {
84 debug("0x%x", payload[i]);
87 debug("(no payload)");