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 uint8_t* data, const uint8_t length) {
12 if(arbitration_id != handler->arbitration_id){
16 // TODO use CanMessage struct from canutil library - allocate payload buffer
18 // TODO this function should receive uint64_t...
19 IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
20 getBitField((uint64_t)data, 0, 2, false);
24 isotp_handle_single_frame(handler, arbitration_id, data, length);
27 handler->shims->log("Only single frame messages are supported");
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
36 void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms) {
37 handler->timeout_ms = timeout_ms;
40 IsoTpShims isotp_init_shims(LogShim log, SendCanMessageShim send_can_message,
41 SetTimerShim set_timer) {
44 send_can_message: send_can_message,
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 = {
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,
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]);