forked from collin80/ESP32RET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ELM327_Emulator.cpp
320 lines (291 loc) · 10.3 KB
/
ELM327_Emulator.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* ELM327_Emu.cpp
*
* Class emulates the serial comm of an ELM327 chip - Used to create an OBDII interface
*
* Created: 3/23/2017
* Author: Collin Kidder
*/
/*
Copyright (c) 2017 Collin Kidder
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 "ELM327_Emulator.h"
#include "BluetoothSerial.h"
#include "config.h"
#include "Logger.h"
#include "utility.h"
#include "esp32_can.h"
#include "can_manager.h"
/*
* Constructor. Nothing at the moment
*/
ELM327Emu::ELM327Emu()
{
tickCounter = 0;
ibWritePtr = 0;
ecuAddress = 0x7E0;
mClient = 0;
bEcho = false;
bHeader = false;
bLineFeed = true;
}
/*
* Initialization of hardware and parameters
*/
void ELM327Emu::setup() {
serialBT.begin(settings.btName);
}
void ELM327Emu::setWiFiClient(WiFiClient *client)
{
mClient = client;
}
/*
* Send a command to ichip. The "AT+i" part will be added.
*/
void ELM327Emu::sendCmd(String cmd) {
txBuffer.sendString("AT");
txBuffer.sendString(cmd);
txBuffer.sendByteToBuffer(13);
sendTxBuffer();
loop(); // parse the response
}
/*
* Called in the main loop (hopefully) in order to process serial input waiting for us
* from the wifi module. It should always terminate its answers with 13 so buffer
* until we get 13 (CR) and then process it.
* But, for now just echo stuff to our serial port for debugging
*/
void ELM327Emu::loop() {
int incoming;
if (!mClient) //bluetooth
{
while (serialBT.available()) {
incoming = serialBT.read();
if (incoming != -1) { //and there is no reason it should be -1
if (incoming == 13 || ibWritePtr > 126) { // on CR or full buffer, process the line
incomingBuffer[ibWritePtr] = 0; //null terminate the string
ibWritePtr = 0; //reset the write pointer
if (Logger::isDebug())
Logger::debug(incomingBuffer);
processCmd();
} else { // add more characters
if (incoming != 10 && incoming != ' ') // don't add a LF character or spaces. Strip them right out
incomingBuffer[ibWritePtr++] = (char)tolower(incoming); //force lowercase to make processing easier
}
}
else return;
}
}
else //wifi
{
while (mClient->available()) {
incoming = mClient->read();
if (incoming != -1) { //and there is no reason it should be -1
if (incoming == 13 || ibWritePtr > 126) { // on CR or full buffer, process the line
incomingBuffer[ibWritePtr] = 0; //null terminate the string
ibWritePtr = 0; //reset the write pointer
if (Logger::isDebug())
Logger::debug(incomingBuffer);
processCmd();
} else { // add more characters
if (incoming != 10 && incoming != ' ') // don't add a LF character or spaces. Strip them right out
incomingBuffer[ibWritePtr++] = (char)tolower(incoming); //force lowercase to make processing easier
}
}
else return;
}
}
}
void ELM327Emu::sendTxBuffer()
{
if (mClient)
{
size_t wifiLength = txBuffer.numAvailableBytes();
uint8_t* buff = txBuffer.getBufferedBytes();
if (mClient->connected())
{
mClient->write(buff, wifiLength);
}
}
else //bluetooth then
{
serialBT.write(txBuffer.getBufferedBytes(), txBuffer.numAvailableBytes());
}
txBuffer.clearBufferedBytes();
}
/*
* There is no need to pass the string in here because it is local to the class so this function can grab it by default
* But, for reference, this cmd processes the command in incomingBuffer
*/
void ELM327Emu::processCmd() {
String retString = processELMCmd(incomingBuffer);
txBuffer.sendString(retString);
sendTxBuffer();
if (Logger::isDebug()) {
char buff[300];
retString = "Reply:" + retString;
retString.toCharArray(buff, 300);
Logger::debug(buff);
}
}
String ELM327Emu::processELMCmd(char *cmd)
{
String retString = String();
String lineEnding;
if (bLineFeed) lineEnding = String("\r\n");
else lineEnding = String("\r");
if (bEcho)
{
retString.concat(cmd);
retString.concat(lineEnding);
}
if (!strncmp(cmd, "at", 2))
{
if (!strcmp(cmd, "atz"))
{ //reset hardware
retString.concat(lineEnding);
retString.concat("ELM327 v1.3a");
}
else if (!strncmp(cmd, "atsh",4)) //set header address (address we send queries to)
{
size_t idSize = strlen(cmd+4);
ecuAddress = Utility::parseHexString(cmd+4, idSize);
Logger::debug("New ECU address: %x", ecuAddress);
retString.concat("OK");
}
else if (!strncmp(cmd, "ate",3))
{ //turn echo on/off
if (cmd[3] == '1') bEcho = true;
if (cmd[3] == '0') bEcho = false;
//retString.concat("OK");
}
else if (!strncmp(cmd, "ath",3))
{ //turn headers on/off
if (cmd[3] == '1') bHeader = true;
else bHeader = false;
retString.concat("OK");
}
else if (!strncmp(cmd, "atl",3))
{ //turn linefeeds on/off
if (cmd[3] == '1') bLineFeed = true;
else bLineFeed = false;
retString.concat("OK");
}
else if (!strcmp(cmd, "at@1"))
{ //send device description
retString.concat("OBDLink MX");
}
else if (!strcmp(cmd, "ati"))
{ //send chip ID
retString.concat("ELM327 v1.3a");
}
else if (!strncmp(cmd, "atat",4))
{ //set adaptive timing
//don't intend to support adaptive timing at all
retString.concat("OK");
}
else if (!strncmp(cmd, "atsp",4))
{ //set protocol
//theoretically we can ignore this
retString.concat("OK");
}
else if (!strcmp(cmd, "atdp"))
{ //show description of protocol
retString.concat("can11/500");
}
else if (!strcmp(cmd, "atdpn"))
{ //show protocol number (same as passed to sp)
retString.concat("6");
}
else if (!strcmp(cmd, "atd"))
{ //set to defaults
retString.concat("OK");
}
else if (!strncmp(cmd, "atm", 3))
{ //turn memory on/off
retString.concat("OK");
}
else if (!strcmp(cmd, "atrv"))
{ //show 12v rail voltage
//TODO: the system should actually have this value so it wouldn't hurt to
//look it up and report the real value.
retString.concat("14.2V");
}
else
{ //by default respond to anything not specifically handled by just saying OK and pretending.
retString.concat("OK");
}
}
else
{ //if no AT then assume it is a PID request. This takes the form of four bytes which form the alpha hex digit encoding for two bytes
//there should be four or six characters here forming the ascii representation of the PID request. Easiest for now is to turn the ascii into
//a 16 bit number and mask off to get the bytes
CAN_FRAME outFrame;
outFrame.id = ecuAddress;
outFrame.extended = false;
outFrame.length = 8;
outFrame.rtr = 0;
outFrame.data.byte[3] = 0xAA; outFrame.data.byte[4] = 0xAA;
outFrame.data.byte[5] = 0xAA; outFrame.data.byte[6] = 0xAA;
outFrame.data.byte[7] = 0xAA;
size_t cmdSize = strlen(cmd);
if (cmdSize == 4) //generic OBDII codes
{
uint32_t valu = strtol((char *) cmd, NULL, 16); //the pid format is always in hex
uint8_t pidnum = (uint8_t)(valu & 0xFF);
uint8_t mode = (uint8_t)((valu >> 8) & 0xFF);
Logger::debug("Mode: %i, PID: %i", mode, pidnum);
outFrame.data.byte[0] = 2;
outFrame.data.byte[1] = mode;
outFrame.data.byte[2] = pidnum;
}
if (cmdSize == 6) //custom PIDs for specific vehicles
{
uint32_t valu = strtol((char *) cmd, NULL, 16); //the pid format is always in hex
uint16_t pidnum = (uint8_t)(valu & 0xFFFF);
uint8_t mode = (uint8_t)((valu >> 16) & 0xFF);
Logger::debug("Mode: %i, PID: %i", mode, pidnum);
outFrame.data.byte[0] = 3;
outFrame.data.byte[1] = mode;
outFrame.data.byte[2] = pidnum >> 8;
outFrame.data.byte[3] = pidnum & 0xFF;
}
canManager.sendFrame(&CAN0, outFrame);
}
retString.concat(lineEnding);
retString.concat(">"); //prompt to show we're ready to receive again
return retString;
}
void ELM327Emu::processCANReply(CAN_FRAME &frame)
{
//at the moment assume anything sent here is a legit reply to something we sent. Package it up properly
//and send it down the line
char buff[8];
if (bHeader)
{
sprintf(buff, "%03X", frame.id);
txBuffer.sendString(buff);
}
for (int i = 0; i < frame.data.byte[0]; i++)
{
sprintf(buff, "%02X", frame.data.byte[1+i]);
txBuffer.sendString(buff);
}
sendTxBuffer();
}