forked from lincomatic/open_evse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rapi_proc.cpp
601 lines (550 loc) · 13.9 KB
/
rapi_proc.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// -*- C++ -*-
/*
* Open EVSE Firmware
*
* Copyright (c) 2013-2014 Sam C. Lin <[email protected]>
*
* This file is part of Open EVSE.
* Open EVSE is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
* Open EVSE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Open EVSE; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#if defined(ARDUINO) && (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h" // shouldn't need this but arduino sometimes messes up and puts inside an #ifdef
#endif // ARDUINO
#include "open_evse.h"
#ifdef RAPI
const char RAPI_VER[] PROGMEM = RAPIVER;
// convert 2-digit hex string to uint8_t
uint8_t htou8(const char *s)
{
uint8_t u = 0;
for (int i=0;i < 2;i++) {
if (i == 1) u <<= 4;
char c = s[i];
if ((c >= '0') && (c <= '9')) {
u += c - '0';
}
else if ((c >= 'A') && (c <= 'F')) {
u += c - 'A' + 10;
}
}
return u;
}
// convert decimal string to uint32_t
uint32_t dtou32(const char *s)
{
uint32_t u = 0;
while (*s) {
u *= 10;
u += *(s++) - '0';
}
return u;
}
#ifdef RAPI_I2C
//get data from master - HINT: this is a ISR call!
//HINT2: do not handle stuff here!! this will NOT work
//collect only data here and process it in the main loop!
void receiveEvent(int numBytes)
{
//do nothing here
}
#endif // RAPI_I2C
EvseRapiProcessor::EvseRapiProcessor()
{
}
void EvseRapiProcessor::init()
{
echo = 0;
reset();
}
int EvseRapiProcessor::doCmd()
{
int rc = 1;
int bcnt = available();
if (bcnt) {
for (int i=0;i < bcnt;i++) {
char c = read();
if (echo) write(c);
if (c == ESRAPI_SOC) {
buffer[0] = ESRAPI_SOC;
bufCnt = 1;
}
else if (buffer[0] == ESRAPI_SOC) {
if (bufCnt < ESRAPI_BUFLEN) {
if (c == ESRAPI_EOC) {
buffer[bufCnt++] = 0;
if (!tokenize()) {
rc = processCmd();
}
else {
reset();
response(0);
}
}
else {
buffer[bufCnt++] = c;
}
}
else { // too many chars
reset();
}
}
}
}
return rc;
}
void EvseRapiProcessor::sendEvseState()
{
sprintf(g_sTmp,"%cST %02x%c",ESRAPI_SOC,g_EvseController.GetState(),ESRAPI_EOC);
writeStart();
write(g_sTmp);
writeEnd();
}
void EvseRapiProcessor::setWifiMode(uint8_t mode)
{
sprintf(g_sTmp,"%cWF %02x%c",ESRAPI_SOC,(int)mode,ESRAPI_EOC);
writeStart();
write(g_sTmp);
writeEnd();
}
int EvseRapiProcessor::tokenize()
{
tokens[0] = &buffer[1];
char *s = &buffer[2];
tokenCnt = 1;
uint8_t achkSum = ESRAPI_SOC + buffer[1];
uint8_t xchkSum = ESRAPI_SOC ^ buffer[1];
uint8_t hchkSum;
uint8_t chktype=0; // 0=none,1=additive,2=xor
while (*s) {
if (*s == ' ') {
achkSum += *s;
xchkSum ^= *s;
*s = '\0';
tokens[tokenCnt++] = ++s;
}
else if ((*s == '*') ||// additive checksum
(*s == '^')) { // XOR checksum
if (*s == '*') chktype = 1;
else if (*s == '^') chktype = 2;
*(s++) = '\0';
hchkSum = htou8(s);
break;
}
else {
achkSum += *s;
xchkSum ^= *(s++);
}
}
return ((chktype == 0) ||
((chktype == 1) && (hchkSum == achkSum)) ||
((chktype == 2) && (hchkSum == xchkSum))) ? 0 : 1;
}
int EvseRapiProcessor::processCmd()
{
UNION4B u1,u2,u3;
int rc = 0;
// we use bufCnt as a flag in response() to signify data to write
bufCnt = 0;
char *s = tokens[0];
switch(*(s++)) {
case 'F': // function
switch(*s) {
#ifdef LCD16X2
case 'B': // LCD backlight
g_OBD.LcdSetBacklightColor(dtou32(tokens[1]));
break;
#endif // LCD16X2
case 'D': // disable EVSE
g_EvseController.Disable();
break;
case 'E': // enable EVSE
g_EvseController.Enable();
break;
#ifdef LCD16X2
case 'P': // print to LCD
{
u1.u = dtou32(tokens[1]); // x
u2.u = dtou32(tokens[2]); // y
// now restore the spaces that were replaced w/ nulls by tokenizing
for (u3.i=4;u3.i < tokenCnt;u3.i++) {
*(tokens[u3.i]-1) = ' ';
}
g_OBD.LcdPrint(u1.u,u2.u,tokens[3]);
}
break;
#endif // LCD16X2
case 'R': // reset EVSE
g_EvseController.Reboot();
break;
case 'S': // sleep
g_EvseController.Sleep();
break;
default:
rc = -1; // unknown
}
break;
case 'S': // set parameter
switch(*s) {
#ifdef LCD16X2
case '0': // set LCD type
if (tokenCnt == 2) {
#ifdef RGBLCD
rc = g_EvseController.SetBacklightType((*tokens[1] == '0') ? BKL_TYPE_MONO : BKL_TYPE_RGB);
#endif // RGBLCD
}
break;
#endif // LCD16X2
#ifdef RTC
case '1': // set RTC
if (tokenCnt == 7) {
extern void SetRTC(uint8_t y,uint8_t m,uint8_t d,uint8_t h,uint8_t mn,uint8_t s);
SetRTC(dtou32(tokens[1]),dtou32(tokens[2]),dtou32(tokens[3]),
dtou32(tokens[4]),dtou32(tokens[5]),dtou32(tokens[6]));
}
break;
#endif // RTC
#ifdef AMMETER
case '2': // ammeter calibration mode
if (tokenCnt == 2) {
g_EvseController.EnableAmmeterCal((*tokens[1] == '1') ? 1 : 0);
}
break;
#ifdef TIME_LIMIT
case '3': // set time limit
if (tokenCnt == 2) {
g_EvseController.SetTimeLimit(dtou32(tokens[1]));
}
break;
#endif // TIME_LIMIT
case 'A':
if (tokenCnt == 3) {
g_EvseController.SetCurrentScaleFactor(dtou32(tokens[1]));
g_EvseController.SetAmmeterCurrentOffset(dtou32(tokens[2]));
}
break;
#endif // AMMETER
case 'C': // current capacity
if (tokenCnt == 2) {
rc = g_EvseController.SetCurrentCapacity(dtou32(tokens[1]),1);
}
break;
case 'D': // diode check
if (tokenCnt == 2) {
g_EvseController.EnableDiodeCheck((*tokens[1] == '0') ? 0 : 1);
}
break;
case 'E': // echo
if (tokenCnt == 2) {
echo = ((*tokens[1] == '0') ? 0 : 1);
}
break;
#ifdef GFI_SELFTEST
case 'F': // GFI self test
if (tokenCnt == 2) {
g_EvseController.EnableGfiSelfTest(*tokens[1] == '0' ? 0 : 1);
}
break;
#endif // GFI_SELFTEST
#ifdef ADVPWR
case 'G': // ground check
if (tokenCnt == 2) {
g_EvseController.EnableGndChk(*tokens[1] == '0' ? 0 : 1);
}
break;
#endif // ADVPWR
#ifdef CHARGE_LIMIT
case 'H': // cHarge limit
if (tokenCnt == 2) {
g_EvseController.SetChargeLimit(dtou32(tokens[1]));
}
break;
#endif // CHARGE_LIMIT
#ifdef KWH_RECORDING
case 'K': // set accumulated kwh
g_WattHours_accumulated = dtou32(tokens[1]);
eeprom_write_dword((uint32_t*)EOFS_KWH_ACCUMULATED,g_WattHours_accumulated);
break;
#endif //KWH_RECORDING
case 'L': // service level
if (tokenCnt == 2) {
switch(*tokens[1]) {
case '1':
case '2':
g_EvseController.SetSvcLevel(*tokens[1] - '0',1);
#ifdef ADVPWR
g_EvseController.EnableAutoSvcLevel(0);
#endif
break;
#ifdef ADVPWR
case 'A':
g_EvseController.EnableAutoSvcLevel(1);
break;
#endif // ADVPWR
default:
rc = -1; // unknown
}
}
break;
#ifdef VOLTMETER
case 'M':
if (tokenCnt == 3) {
g_EvseController.SetVoltmeter(dtou32(tokens[1]),dtou32(tokens[2]));
}
break;
#endif // VOLTMETER
#ifdef TEMPERATURE_MONITORING_NY
case 'O':
if (tokenCnt == 3) {
g_TempMonitor.m_ambient_thresh = dtou32(tokens[1]);
g_TempMonitor.m_ir_thresh = dtou32(tokens[2]);
g_TempMonitor.SaveThresh();
}
break;
#endif // TEMPERATURE_MONITORING
#ifdef ADVPWR
case 'R': // stuck relay check
if (tokenCnt == 2) {
g_EvseController.EnableStuckRelayChk(*tokens[1] == '0' ? 0 : 1);
}
break;
#endif // ADVPWR
#ifdef GFI_SELFTEST
case 'S': // GFI self-test
if (tokenCnt == 2) {
g_EvseController.EnableGfiSelfTest(*tokens[1] == '0' ? 0 : 1);
}
break;
#endif // GFI_SELFTEST
#ifdef DELAYTIMER
case 'T': // timer
if (tokenCnt == 5) {
extern DelayTimer g_DelayTimer;
if ((*tokens[1] == '0') && (*tokens[2] == '0') && (*tokens[3] == '0') && (*tokens[4] == '0')) {
g_DelayTimer.Disable();
}
else {
g_DelayTimer.SetStartTimer(dtou32(tokens[1]),dtou32(tokens[2]));
g_DelayTimer.SetStopTimer(dtou32(tokens[3]),dtou32(tokens[4]));
g_DelayTimer.Enable();
}
}
break;
#endif // DELAYTIMER
case 'V': // vent required
if (tokenCnt == 2) {
g_EvseController.EnableVentReq(*tokens[1] == '0' ? 0 : 1);
}
break;
}
break;
case 'G': // get parameter
switch(*s) {
#ifdef TIME_LIMIT
case '3': // get time limit
sprintf(buffer,"%d",(int)g_EvseController.GetTimeLimit());
bufCnt = 1; // flag response text output
break;
#endif // TIME_LIMIT
#ifdef AMMETER
case 'A':
u1.i = g_EvseController.GetCurrentScaleFactor();
u2.i = g_EvseController.GetAmmeterCurrentOffset();
sprintf(buffer,"%d %d",u1.i,u2.i);
bufCnt = 1; // flag response text output
break;
#endif // AMMETER
case 'C': // get current capacity range
if (g_EvseController.GetCurSvcLevel() == 2) {
u1.i = MIN_CURRENT_CAPACITY_L2;
u2.i = MAX_CURRENT_CAPACITY_L2;
}
else {
u1.i = MIN_CURRENT_CAPACITY_L1;
u2.i = MAX_CURRENT_CAPACITY_L1;
}
sprintf(buffer,"%d %d",u1.i,u2.i);
bufCnt = 1; // flag response text output
break;
case 'E': // get settings
u1.u = g_EvseController.GetCurrentCapacity();
u2.u = g_EvseController.GetFlags();
sprintf(buffer,"%d %04x",u1.u,u2.u);
bufCnt = 1; // flag response text output
break;
case 'F': // get fault counters
u1.u = g_EvseController.GetGfiTripCnt();
u2.u = g_EvseController.GetNoGndTripCnt();
u3.u = g_EvseController.GetStuckRelayTripCnt();
sprintf(buffer,"%x %x %x",u1.u,u2.u,u3.u);
bufCnt = 1; // flag response text output
break;
#if defined(AMMETER)||defined(VOLTMETER)
case 'G':
u1.i32 = g_EvseController.GetChargingCurrent();
u2.i32 = (int32_t)g_EvseController.GetVoltage();
sprintf(buffer,"%ld %ld",u1.i32,u2.i32);
bufCnt = 1; // flag response text output
break;
#endif // AMMETER || VOLTMETER
#ifdef CHARGE_LIMIT
case 'H': // get cHarge limit
sprintf(buffer,"%d",(int)g_EvseController.GetChargeLimit());
bufCnt = 1; // flag response text output
break;
#endif // CHARGE_LIMIT
#ifdef VOLTMETER
case 'M':
u1.i = g_EvseController.GetVoltScaleFactor();
u2.i32 = g_EvseController.GetVoltOffset();
sprintf(buffer,"%d %ld",u1.i,u2.i32);
bufCnt = 1; // flag response text output
break;
#endif // VOLTMETER
#ifdef TEMPERATURE_MONITORING
#ifdef TEMPERATURE_MONITORING_NY
case 'O':
u1.i = g_TempMonitor.m_ambient_thresh;
u2.i = g_TempMonitor.m_ir_thresh;
sprintf(buffer,"%d %d",u1.i,u2.i);
bufCnt = 1; // flag response text output
break;
#endif // TEMPERATURE_MONITORING_NY
case 'P':
sprintf(buffer,"%d %d %d",(int)g_TempMonitor.m_DS3231_temperature,
(int)g_TempMonitor.m_MCP9808_temperature,
(int)g_TempMonitor.m_TMP007_temperature);
/* this is bigger than using sprintf
strcpy(buffer,u2a(g_TempMonitor.m_DS3231_temperature));
strcat(buffer,g_sSpace);
strcat(buffer,u2a(g_TempMonitor.m_MCP9808_temperature));
strcat(buffer,g_sSpace);
strcat(buffer,u2a(g_TempMonitor.m_TMP007_temperature));
*/
bufCnt = 1; // flag response text output
break;
#endif // TEMPERATURE_MONITORING
case 'S': // get state
sprintf(buffer,"%d %ld",g_EvseController.GetState(),g_EvseController.GetElapsedChargeTime());
bufCnt = 1; // flag response text output
break;
#ifdef RTC
case 'T': // get time
extern void GetRTC(char *buf);
GetRTC(buffer);
bufCnt = 1; // flag response text output
break;
#endif // RTC
#ifdef KWH_RECORDING
case 'U':
sprintf(buffer,"%lu %lu",g_WattSeconds,g_WattHours_accumulated);
bufCnt = 1;
break;
#endif // KWH_RECORDING
case 'V': // get version
GetVerStr(buffer);
strcat(buffer," ");
strcat_P(buffer,RAPI_VER);
bufCnt = 1; // flag response text output
break;
default:
rc = -1; // unknown
}
break;
default:
rc = -1; // unknown
}
response((rc == 0) ? 1 : 0);
reset();
return rc;
}
void EvseRapiProcessor::response(uint8_t ok)
{
writeStart();
write(ESRAPI_SOC);
write(ok ? "OK " : "NK ");
if (bufCnt) {
write(buffer);
}
write(ESRAPI_EOC);
if (echo) write('\n');
writeEnd();
}
EvseSerialRapiProcessor::EvseSerialRapiProcessor()
{
}
void EvseSerialRapiProcessor::init()
{
EvseRapiProcessor::init();
}
#ifdef RAPI_I2C
EvseI2cRapiProcessor::EvseI2cRapiProcessor()
{
}
void EvseI2cRapiProcessor::init()
{
Wire.begin(RAPI_I2C_LOCAL_ADDR);
Wire.onReceive(receiveEvent); // define the receive function for receiving data from master
EvseRapiProcessor::init();
}
#endif // RAPI_I2C
#ifdef RAPI_SERIAL
EvseSerialRapiProcessor g_ESRP;
#endif
#ifdef RAPI_I2C
EvseI2cRapiProcessor g_EIRP;
#endif
void RapiInit()
{
#ifdef RAPI_SERIAL
g_ESRP.init();
#endif
#ifdef RAPI_I2C
g_EIRP.init();
#endif
}
void RapiDoCmd()
{
#ifdef RAPI_SERIAL
g_ESRP.doCmd();
#endif
#ifdef RAPI_I2C
g_EIRP.doCmd();
#endif
}
void RapiSendEvseState(uint8_t nodupe)
{
static uint8_t evseStateSent = EVSE_STATE_UNKNOWN;
uint8_t es = g_EvseController.GetState();
if (!nodupe || (evseStateSent != es)) {
#ifdef RAPI_SERIAL
g_ESRP.sendEvseState();
#endif
#ifdef RAPI_I2C
g_EIRP.sendEvseState();
#endif
evseStateSent = es;
}
}
void RapiSetWifiMode(uint8_t mode)
{
#ifdef RAPI_SERIAL
g_ESRP.setWifiMode(mode);
#endif
#ifdef RAPI_I2C
g_EIRP.setWifiMode(mode);
#endif
}
#endif // RAPI