-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathELM327_Emu.cpp
188 lines (149 loc) · 5.17 KB
/
ELM327_Emu.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
/*
* ELM327_Emu.cpp
*
* Class emulates the serial comm of an ELM327 chip - Used to create an OBDII interface
*
* Created: 3/18/2014
* Author: Collin Kidder
*/
/*
Copyright (c) 2013-2014 Collin Kidder, Michael Neuweiler, Charles Galpin
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_Emu.h"
/*
* Constructor. Assign serial interface to use for comm with bluetooth adapter we're emulating with
*/
ELM327Emu::ELM327Emu() {
prefsHandler = new PrefHandler(ELM327EMU);
uint8_t sys_type;
sysPrefs->read(EESYS_SYSTEM_TYPE, &sys_type);
if (sys_type == 3 || sys_type == 4)
serialInterface = &Serial2;
else //older hardware used this instead
serialInterface = &Serial3;
commonName = "ELM327 Emulator over Bluetooth";
}
/*
* Constructor. Pass serial interface to use
*/
ELM327Emu::ELM327Emu(USARTClass *which) {
prefsHandler = new PrefHandler(ELM327EMU);
serialInterface = which;
}
/*
* Initialization of hardware and parameters
*/
void ELM327Emu::setup() {
Logger::info("add device: ELM327 emulator (id: %X, %X", ELM327EMU, this);
TickHandler::getInstance()->detach(this);
tickCounter = 0;
ibWritePtr = 0;
serialInterface->begin(9600);
elmProc = new ELM327Processor();
//this isn't a wifi link but the timer interval can be the same
//because it serves a similar function and has similar timing requirements
TickHandler::getInstance()->attach(this, CFG_TICK_INTERVAL_WIFI);
}
/*
* Send a command to ichip. The "AT+i" part will be added.
*/
void ELM327Emu::sendCmd(String cmd) {
serialInterface->write("AT");
serialInterface->print(cmd);
serialInterface->write(13);
loop(); // parse the response
}
/*
*/
void ELM327Emu::handleTick() {
}
/*
* Handle a message sent by the DeviceManager.
*/
void ELM327Emu::handleMessage(uint32_t messageType, void* message) {
Device::handleMessage(messageType, message);
switch (messageType) {
case MSG_SET_PARAM: {
break;
}
case MSG_CONFIG_CHANGE:
break;
case MSG_COMMAND:
sendCmd((char *)message);
break;
}
}
/*
* 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;
while (serialInterface->available()) {
incoming = serialInterface->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(ELM327EMU, 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;
}
}
/*
* 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 = elmProc->processELMCmd(incomingBuffer);
serialInterface->print(retString);
if (Logger::isDebug()) {
char buff[30];
retString.toCharArray(buff, 30);
Logger::debug(ELM327EMU, buff);
}
}
DeviceType ELM327Emu::getType() {
return DEVICE_MISC;
}
DeviceId ELM327Emu::getId() {
return (ELM327EMU);
}
void ELM327Emu::loadConfiguration() {
ELM327Configuration *config = (ELM327Configuration *)getConfiguration();
if (prefsHandler->checksumValid()) { //checksum is good, read in the values stored in EEPROM
Logger::debug(ELM327EMU, "Valid checksum so using stored elm327 emulator config values");
//TODO: implement processing of config params for WIFI
// prefsHandler->read(EESYS_WIFI0_SSID, &config->ssid);
}
}
void ELM327Emu::saveConfiguration() {
ELM327Configuration *config = (ELM327Configuration *) getConfiguration();
//TODO: implement processing of config params for WIFI
// prefsHandler->write(EESYS_WIFI0_SSID, config->ssid);
// prefsHandler->saveChecksum();
}