-
Notifications
You must be signed in to change notification settings - Fork 3
/
isotp.tpp
147 lines (119 loc) · 5.84 KB
/
isotp.tpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
MIT License
Copyright (c) 2018 Antonio Alexander Brewer (tonton81) - https://github.com/tonton81
Designed and tested for PJRC Teensy 4.0.
Forum link : https://forum.pjrc.com/threads/56035-FlexCAN_T4-FlexCAN-for-Teensy-4?highlight=flexcan_t4
Thanks goes to skpang, mjs513, and collin for tech/testing support
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <isotp.h>
#include "Arduino.h"
ISOTP_FUNC void ISOTP_OPT::sendFlowControl(const ISOTP_data &config) {
CAN_message_t msg;
msg.id = config.id;
msg.len = 8;
msg.flags.extended = config.flags.extended;
msg.buf[0] = (3U << 4) | constrain(config.flow_control_type, 0, 2);
msg.buf[1] = config.blockSize;
uint16_t separation_time = config.separation_time;
if ( config.flags.separation_uS ) {
separation_time = constrain(((config.separation_time + 50) / 100 * 100), 100, 900);
separation_time = map(separation_time, 100, 900, 0xF1, 0xF9);
}
else {
separation_time = constrain(separation_time, 0, 127);
}
msg.buf[2] = separation_time;
_isotp_busToWrite->write(msg);
#if defined(ARDUINO_ARCH_ESP32) //ESP32
vTaskDelay(500);
#endif
}
ISOTP_FUNC void ISOTP_OPT::write(const ISOTP_data &config, const uint8_t *buf, uint16_t size) {
CAN_message_t msg;
msg.id = config.id;
msg.len = config.len;
msg.flags.extended = config.flags.extended;
msg.buf[0] = (1U << 4) | size >> 8;
msg.buf[1] = (uint8_t)size;
memmove(&msg.buf[2], &buf[0], 6);
_isotp_busToWrite->write(msg);
#if defined(TEENSYDUINO) // Teensy
delay(constrain(config.separation_time, 0, 127));
#elif defined(ARDUINO_ARCH_ESP32) //ESP32
vTaskDelay(constrain(config.separation_time, 0, 127));
#endif
for ( int sent_bytes = 6, difference = 7, counter = 1; sent_bytes < size; sent_bytes += 7, counter++ ) {
msg.buf[0] = (2U << 4) | (counter & 0xF);
difference = constrain((size - sent_bytes), 1, 7);
memmove(&msg.buf[1], &buf[sent_bytes], difference);
for ( int i = 0; i < (7 - difference); i++ ) msg.buf[difference + i + 1] = padding_value;
if ( !config.flags.usePadding && difference < 7 ) msg.len = difference + 1;
_isotp_busToWrite->write(msg);
#if defined(TEENSYDUINO) // Teensy
delay(constrain(config.separation_time, 0, 127));
#elif defined(ARDUINO_ARCH_ESP32) //ESP32
vTaskDelay(constrain(config.separation_time, 0, 127));
#endif
}
}
extern void __attribute__((weak)) ext_isotp_output1(const ISOTP_data &config, const uint8_t *buf);
ISOTP_FUNC void ISOTP_OPT::_process_frame_data(const CAN_message_t &msg) {
if ( !isotp_enabled ) return;
if ( msg.buf[0] <= 7 ) { /* single frame */
ISOTP_data config;
config.id = msg.id;
config.len = msg.buf[0];
config.flags.extended = msg.flags.extended;
if ( _ISOTP_OBJ->_isotp_handler ) _ISOTP_OBJ->_isotp_handler(config, msg.buf + 1);
}
if ( (msg.buf[0] >> 4) == 1 ) { /* first frame */
if ( (((((uint16_t)msg.buf[0] & 0xF) << 8) | msg.buf[1]) + 9) >= (int)_max_length ) return; /* ISOTP message too large for local buffer */
uint8_t data[_max_length] = { (uint8_t)(msg.id >> 24), (uint8_t)(msg.id >> 16), (uint8_t)(msg.id >> 8), (uint8_t)msg.id, 0, 6, 0 };
memmove(data + 7, &msg.buf[0], 8); /* ID, ID, ID, ID, QPOS, QPOS, SEQUENCE: TOTAL 7 */
_rx_slots.findRemove(data, sizeof(data), 0, 1, 2, 3, 3);
_rx_slots.push_back(data, sizeof(data));
} /* first frame */
if ( (msg.buf[0] >> 4) == 2 ) { /* consecutive frames */
uint8_t data[_max_length] = { (uint8_t)(msg.id >> 24), (uint8_t)(msg.id >> 16), (uint8_t)(msg.id >> 8), (uint8_t)msg.id };
if ( _rx_slots.find(data, sizeof(data), 0, 1, 2, 3, 3) ) {
int pos = ((((uint16_t)data[4] & 0xF) << 8) | data[5]);
data[4] = (pos + 7) >> 8;
data[5] = (pos + 7);
data[6] = (data[6] + 1) & 0xF; /* store only last 4 bits of sequence 0x0 -> 0xF */
if ( (msg.buf[0] & 0xF) != data[6] ) { /* sequence match fail */
_rx_slots.findRemove(data, sizeof(data), 0, 1, 2, 3, 3);
return;
}
data[6] = msg.buf[0] & 0xF;
memmove(data + 9 + pos, &msg.buf[1], 7);
_rx_slots.replace(data, sizeof(data), 0, 1, 2, 3, 3);
if ( (((((uint16_t)data[7] & 0xF) << 8) | data[8]) - pos) <= 7 ) {
_rx_slots.findRemove(data, sizeof(data), 0, 1, 2, 3, 3);
ISOTP_data config;
config.id = ((uint32_t)(data[0] << 24) | data[1] << 16 | data[2] << 8 | data[3]);
config.len = ((((uint16_t)data[7] & 0xF) << 8) | data[8]);
config.flags.extended = msg.flags.extended;
if ( _ISOTP_OBJ->_isotp_handler ) _ISOTP_OBJ->_isotp_handler(config, data + 9);
if ( ext_isotp_output1 ) ext_isotp_output1(config, data + 9);
}
}
} /* consecutive frames */
}
void ext_output2(const CAN_message_t &msg) {
_ISOTP_OBJ->_process_frame_data(msg);
}