forked from jyoung8607/panda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelm327.c
1578 lines (1352 loc) · 50.2 KB
/
elm327.c
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
#include "driver/uart.h"
//#define ELM_DEBUG
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
int ICACHE_FLASH_ATTR spi_comm(char *dat, int len, uint32_t *recvData, int recvDataLen);
#define ELM_PORT 35000
//Version 1.5 is an invalid version used by many pirate clones
//that only partially support 1.0.
#define IDENT_MSG "ELM327 v1.5\r\r"
#define DEVICE_DESC "Panda\n\n"
#define SHOW_CONNECTION(msg, p_conn) os_printf("%s %p, proto %p, %d.%d.%d.%d:%d disconnect\r\n", \
msg, p_conn, (p_conn)->proto.tcp, (p_conn)->proto.tcp->remote_ip[0], \
(p_conn)->proto.tcp->remote_ip[1], (p_conn)->proto.tcp->remote_ip[2], \
(p_conn)->proto.tcp->remote_ip[3], (p_conn)->proto.tcp->remote_port)
const static char hex_lookup[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
typedef struct __attribute__((packed)) {
bool tx : 1;
bool : 1;
bool ext : 1;
uint32_t addr : 29;
uint8_t len : 4;
uint8_t bus : 8;
uint8_t : 4; //unused
uint16_t ts : 16;
uint8_t data[8];
} panda_can_msg_t;
//TODO: Masking is likely unnecessary for these bit fields. Check.
#define panda_get_can_addr(recv) (((recv)->ext) ? ((recv)->addr & 0x1FFFFFFF) :\
(((recv)->addr >> 18) & 0x7FF))
#define PANDA_CAN_FLAG_TRANSMIT 1
#define PANDA_CAN_FLAG_EXTENDED 4
#define PANDA_USB_CAN_WRITE_BUS_NUM 3
#define PANDA_USB_LIN_WRITE_BUS_NUM 2
#define SAFETY_ELM327 3U
typedef struct _elm_tcp_conn {
struct espconn *conn;
struct _elm_tcp_conn *next;
} elm_tcp_conn_t;
typedef struct __attribute__((packed)) {
uint8_t len;
uint8_t dat[7]; //mode and data
} elm_can_obd_msg;
typedef struct __attribute__((packed)) {
uint8_t priority;
uint8_t receiver;
uint8_t sender;
uint8_t dat[8]; //mode, data, and checksum
} elm_lin_obd_msg;
typedef struct __attribute__((packed)) {
uint16_t usb_ep_num;
uint16_t payload_len;
uint8_t serial_port;
//uint8_t msg[8+3];
elm_lin_obd_msg msg;
} elm_lin_usb_msg;
static struct espconn elm_conn;
static esp_tcp elm_proto;
static elm_tcp_conn_t *connection_list = NULL;
static char stripped_msg[0x100];
static uint16 stripped_msg_len = 0;
static char in_msg[0x100];
static uint16 in_msg_len = 0;
static char rsp_buff[536]; //TCP min MTU
static uint16 rsp_buff_len = 0;
static uint8_t pandaSendData[0x14] = {0};
static uint32_t pandaRecvData[0x40] = {0};
static uint32_t pandaRecvDataDummy[0x40] = {0}; // Used for CAN write operations (no received data)
#define ELM_MODE_SELECTED_PROTOCOL_DEFAULT 6
#define ELM_MODE_TIMEOUT_DEFAULT 20;
#define ELM_MODE_KEEPALIVE_PERIOD_DEFAULT (0x92*20)
static bool elm_mode_echo = true;
static bool elm_mode_linefeed = false;
static bool elm_mode_additional_headers = false;
static bool elm_mode_auto_protocol = true;
static uint8_t elm_selected_protocol = ELM_MODE_SELECTED_PROTOCOL_DEFAULT;
static bool elm_mode_print_spaces = true;
static uint8_t elm_mode_adaptive_timing = 1;
static bool elm_mode_allow_long = false;
static uint16_t elm_mode_timeout = ELM_MODE_TIMEOUT_DEFAULT;
static uint16_t elm_mode_keepalive_period = ELM_MODE_KEEPALIVE_PERIOD_DEFAULT;
bool lin_bus_initialized = false;
/***********************************************
*** ELM CLI response functions ***
*** (for sending data back to the terminal) ***
***********************************************/
// All ELM operations are global, so send data out to all connections
void ICACHE_FLASH_ATTR elm_tcp_tx_flush() {
if(!rsp_buff_len) return; // Was causing small error messages
for(elm_tcp_conn_t *iter = connection_list; iter != NULL; iter = iter->next){
int8_t err = espconn_send(iter->conn, rsp_buff, rsp_buff_len);
if(err){
os_printf(" Wifi %p TX error code %d\n", iter->conn, err);
if(err == ESPCONN_ARG) {
if(iter == connection_list) {
connection_list = iter->next;
} else {
for(elm_tcp_conn_t *iter2 = connection_list; iter2 != NULL; iter2 = iter2->next)
if(iter2->next == iter) {
iter2->next = iter->next;
break;
}
}
os_printf(" deleting orphaned connection. iter: %p; conn: %p\n", iter, iter->conn);
os_free(iter);
}
}
}
rsp_buff_len = 0;
}
static void ICACHE_FLASH_ATTR elm_append_rsp(const char *data, uint16_t len) {
uint16_t overflow_len = 0;
if(rsp_buff_len + len > sizeof(rsp_buff)) {
overflow_len = rsp_buff_len + len - sizeof(rsp_buff);
len = sizeof(rsp_buff) - rsp_buff_len;
}
if(!elm_mode_linefeed) {
memcpy(rsp_buff + rsp_buff_len, data, len);
rsp_buff_len += len;
} else {
for(int i=0; i < len && rsp_buff_len < sizeof(rsp_buff); i++){
rsp_buff[rsp_buff_len++] = data[i];
if(data[i] == '\r' && rsp_buff_len < sizeof(rsp_buff))
rsp_buff[rsp_buff_len++] = '\n';
}
}
if(overflow_len) {
os_printf("Packet full, sending\n");
elm_tcp_tx_flush();
elm_append_rsp(data + len, overflow_len);
}
}
#define elm_append_rsp_const(str) elm_append_rsp(str, sizeof(str)-1)
static void ICACHE_FLASH_ATTR elm_append_rsp_hex_byte(uint8_t num) {
elm_append_rsp(&hex_lookup[num >> 4], 1);
elm_append_rsp(&hex_lookup[num & 0xF], 1);
if(elm_mode_print_spaces) elm_append_rsp_const(" ");
}
void ICACHE_FLASH_ATTR elm_append_rsp_can_msg_addr(const panda_can_msg_t *recv) {
//Show address
uint32_t addr = panda_get_can_addr(recv);
if(recv->ext){
elm_append_rsp_hex_byte(addr>>24);
elm_append_rsp_hex_byte(addr>>16);
elm_append_rsp_hex_byte(addr>>8);
elm_append_rsp_hex_byte(addr);
} else {
elm_append_rsp(&hex_lookup[addr>>8], 1);
elm_append_rsp_hex_byte(addr);
}
}
/***************************************
*** Panda communication functions ***
*** (for controlling the Panda MCU) ***
***************************************/
static int ICACHE_FLASH_ATTR panda_usbemu_ctrl_write(uint8_t request_type, uint8_t request,
uint16_t value, uint16_t index, uint16_t length) {
//self.sock.send(struct.pack("HHBBHHH", 0, 0, request_type, request, value, index, length));
*(uint16_t*)(pandaSendData) = 0;
*(uint16_t*)(pandaSendData+2) = 0;
pandaSendData[4] = request_type;
pandaSendData[5] = request;
*(uint16_t*)(pandaSendData+6) = value;
*(uint16_t*)(pandaSendData+8) = index;
*(uint16_t*)(pandaSendData+10) = length;
int returned_count = spi_comm(pandaSendData, 0x10, pandaRecvData, 0x40);
if(returned_count > 0x40 || returned_count < 0)
return -1;
return returned_count;
}
#define panda_set_can0_cbaud(cbps) panda_usbemu_ctrl_write(0x40, 0xde, 0, cbps, 0)
#define panda_set_can0_kbaud(kbps) panda_usbemu_ctrl_write(0x40, 0xde, 0, kbps*10, 0)
#define panda_set_safety_mode(mode) panda_usbemu_ctrl_write(0x40, 0xdc, mode, 0, 0)
#define panda_kline_wakeup_pulse() panda_usbemu_ctrl_write(0x40, 0xf0, 0, 0, 0)
#define panda_clear_can_rx() panda_usbemu_ctrl_write(0x40, 0xf1, 0xFFFF, 0, 0)
#define panda_clear_lin_txrx() panda_usbemu_ctrl_write(0x40, 0xf2, 2, 0, 0)
static int ICACHE_FLASH_ATTR panda_usbemu_can_read(panda_can_msg_t** can_msgs) {
int returned_count = spi_comm((uint8_t *)((const uint16 []){1,0}), 4, pandaRecvData, 0x40);
if(returned_count > 0x40 || returned_count < 0){
os_printf("CAN read got invalid length\n");
return -1;
}
*can_msgs = (panda_can_msg_t*)(pandaRecvData+1);
return returned_count/sizeof(panda_can_msg_t);
}
static int ICACHE_FLASH_ATTR panda_usbemu_can_write(bool ext, uint32_t addr,
char *candata, uint8_t canlen) {
uint32_t rir;
if(canlen > 8) return 0;
if(ext || addr >= 0x800){
rir = (addr << 3) | PANDA_CAN_FLAG_TRANSMIT | PANDA_CAN_FLAG_EXTENDED;
}else{
rir = (addr << 21) | PANDA_CAN_FLAG_TRANSMIT;
}
#define MAX_CAN_LEN 8
//Wifi USB Wrapper
*(uint16_t*)(pandaSendData) = PANDA_USB_CAN_WRITE_BUS_NUM; //USB Bulk Endpoint ID.
*(uint16_t*)(pandaSendData+2) = MAX_CAN_LEN;
//BULK MESSAGE
*(uint32_t*)(pandaSendData+4) = rir;
*(uint32_t*)(pandaSendData+8) = MAX_CAN_LEN | (0 << 4); //0 is CAN bus number.
//CAN DATA
memcpy(pandaSendData+12, candata, canlen);
memset(pandaSendData+12+canlen, 0, MAX_CAN_LEN-canlen);
for(int i = 12+canlen; i < 20; i++) pandaSendData[i] = 0; //Zero the rest
/* spi_comm will erase data in the recv buffer even if you are only
* interested in sending data that gets no response (like writing
* can data). This behavior becomes problematic when trying to send
* a can message while processsing received can messages. A dummy
* recv buffer is used here so received data is not overwritten. */
int returned_count = spi_comm(pandaSendData, 0x14, pandaRecvDataDummy, 0x40);
if(returned_count)
os_printf("ELM Can send expected 0 bytes back from panda. Got %d bytes instead\n", returned_count);
if(returned_count > 0x40) return 0;
return returned_count;
}
elm_lin_obd_msg lin_last_sent_msg;
uint16_t lin_last_sent_msg_len = 0;
bool lin_await_msg_echo = false;
static int ICACHE_FLASH_ATTR panda_usbemu_kline_read(uint16_t len) {
int returned_count = panda_usbemu_ctrl_write(0xC0, 0xE0, 2, 0, len);
if(returned_count > len || returned_count < 0){
os_printf("LIN read got invalid length\n");
return -1;
}
#ifdef ELM_DEBUG
if(returned_count) {
os_printf("LIN Received %d bytes\n", returned_count);
os_printf(" Data: ");
for(int i = 0; i < returned_count; i++)
os_printf("%02x ", ((char*)(pandaRecvData+1))[i]);
os_printf("\n");
}
#endif
return returned_count;
}
static int ICACHE_FLASH_ATTR panda_usbemu_kline_write(elm_lin_obd_msg *msg) {
elm_lin_usb_msg usb_msg = {};
usb_msg.usb_ep_num = PANDA_USB_LIN_WRITE_BUS_NUM; //USB Bulk Endpoint ID.
usb_msg.payload_len = (msg->priority & 0x07) + 4 + 1; //The +1 is for serial_port
usb_msg.serial_port = 2;
memcpy(&usb_msg.msg, msg, sizeof(elm_lin_obd_msg));
/* spi_comm will erase data in the recv buffer even if you are only
* interested in sending data that gets no response (like writing
* can data). This behavior becomes problematic when trying to send
* a can message while processsing received can messages. A dummy
* recv buffer is used here so received data is not overwritten. */
int returned_count = spi_comm((char*)&usb_msg, sizeof(elm_lin_usb_msg), pandaRecvDataDummy, 0x40);
if(returned_count)
os_printf("ELM LIN send expected 0 bytes back from panda. Got %d bytes instead\n", returned_count);
if(returned_count > 0x40) return 0;
return returned_count;
}
/****************************************
*** Ringbuffer ***
****************************************/
//LIN data is delivered in chunks of arbitrary size. Using a
//ringbuffer to handle it.
uint8_t lin_ringbuff[0x20];
uint8_t lin_ringbuff_start = 0;
uint8_t lin_ringbuff_end = 0;
#define lin_ringbuff_len \
(((sizeof(lin_ringbuff) + lin_ringbuff_end) - lin_ringbuff_start)% sizeof(lin_ringbuff))
#define lin_ringbuff_get(index) (lin_ringbuff[(lin_ringbuff_start + index) % sizeof(lin_ringbuff)])
#define lin_ringbuff_consume(len) lin_ringbuff_start = ((lin_ringbuff_start + len) % sizeof(lin_ringbuff))
#define lin_ringbuff_clear()\
{lin_ringbuff_start = 0; \
lin_ringbuff_end = 0;}
int ICACHE_FLASH_ATTR elm_LIN_ringbuff_memcmp(uint8_t *data, uint16_t len) {
if(len > lin_ringbuff_len) return 1;
for(int i = 0; i < len; i++)
if(lin_ringbuff_get(i) != data[i]) return 1;
return 0; // Going with memcpy ret format where 0 means 'equal'
}
uint16_t ICACHE_FLASH_ATTR elm_LIN_read_into_ringbuff() {
int bytelen = panda_usbemu_kline_read((sizeof(lin_ringbuff) - lin_ringbuff_len) - 1);
if(bytelen < 0) return 0;
for(int j = 0; j < bytelen; j++) {
lin_ringbuff[lin_ringbuff_end % sizeof(lin_ringbuff)] = ((char*)(pandaRecvData+1))[j];
lin_ringbuff_end = (lin_ringbuff_end + 1) % sizeof(lin_ringbuff);
if(lin_ringbuff_start == lin_ringbuff_end) lin_ringbuff_start++;
}
#ifdef ELM_DEBUG
if(bytelen){
os_printf(" RB Data (%d %d %d): ", lin_ringbuff_start, lin_ringbuff_end, lin_ringbuff_len);
for(int i = 0; i < sizeof(lin_ringbuff); i++)
os_printf("%02x ", lin_ringbuff[i]);
os_printf("\n");
}
#endif
return bytelen;
}
/****************************************
*** String parsing utility functions ***
****************************************/
static int8_t ICACHE_FLASH_ATTR elm_decode_hex_char(char b){
if(b >= '0' && b <= '9') return b - '0';
if(b >= 'A' && b <= 'F') return (b - 'A') + 10;
if(b >= 'a' && b <= 'f') return (b - 'a') + 10;
return -1;
}
static uint8_t ICACHE_FLASH_ATTR elm_decode_hex_byte(const char* data) {
return (elm_decode_hex_char(data[0]) << 4) | elm_decode_hex_char(data[1]);
}
static bool ICACHE_FLASH_ATTR elm_check_valid_hex_chars(const char* data, uint8_t len) {
for(int i = 0; i < len; i++){
char b = data[i];
if(!((b >= '0' && b <= '9') || (b >= 'A' && b <= 'F') || (b >= 'a' && b <= 'f')))
return 0;
}
return 1;
}
static uint16_t ICACHE_FLASH_ATTR elm_strip(const char *data, uint16_t lenin,
char *outbuff, uint16_t outbufflen) {
uint16_t count = 0;
for(uint16_t i = 0; i < lenin; i++) {
if(count >= outbufflen) break;
if(data[i] == ' ') continue;
if(data[i] >= 'a' && data[i] <= 'z'){
outbuff[count++] = data[i] - ('a' - 'A');
} else {
outbuff[count++] = data[i];
}
if(data[i] == '\r') break;
}
return count;
}
static int ICACHE_FLASH_ATTR elm_msg_find_cr_or_eos(char *data, uint16_t len){
uint16_t i;
for(i = 0; i < len; i++)
if(data[i] == '\r') {
i++;
break;
}
return i;
}
/*****************************************************
*** ELM protocol specification and implementation ***
*****************************************************/
typedef enum {
AUTO, LIN, CAN11, CAN29, NA
} elm_proto_type_t;
typedef struct elm_protocol {
bool supported;
elm_proto_type_t type;
uint16_t cbaud; //Centibaud (cbaud * 10 = kbaud)
void (*process_obd)(const struct elm_protocol*, const char*, uint16_t);
//init is used to init and de-init a protocol. Init functions should
//not do things that would leave a new protocol in an invalid state
//after the new protocol's init is called (e.g. No arming timers).
void (*init)(const struct elm_protocol*);
char* name;
} elm_protocol_t;
static const elm_protocol_t* ICACHE_FLASH_ATTR elm_current_proto();
void ICACHE_FLASH_ATTR elm_reset_aux_timer();
static void ICACHE_FLASH_ATTR elm_autodetect_cb(bool);
static const elm_protocol_t elm_protocols[];
//(sizeof(elm_protocols)/sizeof(elm_protocol_t))
#define ELM_PROTOCOL_COUNT 13
#define LOOPCOUNT_FULL 4
static int loopcount = 0;
static volatile os_timer_t elm_timeout;
static volatile os_timer_t elm_proto_aux_timeout;
static bool is_auto_detecting = false;
// Used only by elm_timer_cb, so not volatile
static bool did_multimessage = false;
static bool got_msg_this_run = false;
static bool can_tx_worked = false;
static uint8_t elm_msg_mode_ret_filter;
static uint8_t elm_msg_pid_ret_filter;
/*****************************************************
*** ELM protocol specification and implementation ***
*** -> SAE J1850 implementation (Unsupported) ***
*****************************************************/
static void ICACHE_FLASH_ATTR elm_process_obd_cmd_J1850(const elm_protocol_t* proto,
const char *cmd, uint16_t len) {
elm_append_rsp_const("NO DATA\r\r>");
}
/*****************************************************
*** ELM protocol specification and implementation ***
*** -> ISO 14230-4 implementation ***
*****************************************************/
const char *lin_cmd_backup = NULL; //Holds msg while bus init is done
uint16_t lin_cmd_backup_len = 0;
bool lin_waiting_keepalive_echo = false;
static void ICACHE_FLASH_ATTR elm_process_obd_cmd_LIN5baud(const elm_protocol_t* proto,
const char *cmd, uint16_t len) {
elm_append_rsp_const("BUS INIT: ...ERROR\r\r>");
}
bool ICACHE_FLASH_ATTR elm_lin_keepalive_echo() {
if(lin_waiting_keepalive_echo) {
for(int pass = 0; pass < 4 && lin_ringbuff_len < 5; pass++) {
elm_LIN_read_into_ringbuff();
}
lin_waiting_keepalive_echo = false;
//keepalive Echo should always come before other message echo.
if(lin_ringbuff_len >= 5 && !elm_LIN_ringbuff_memcmp("\xc1\x33\xf1\x3e\x23", 5)){
lin_ringbuff_consume(5);
return true;
} else {
os_printf("Keep alive echo failed\n");
return false;
}
}
return true;
}
void ICACHE_FLASH_ATTR elm_LINFast_keepalive_timer_cb(void *arg) {
if(!lin_bus_initialized) {
os_printf("WARNING! Elm LIN keepalive timer running while bus is not initialized\n");
return;
}
if(loopcount) {
os_printf("WARNING! Elm LIN keepalive timer during a tx/rx loop!\n");
return;
}
if(lin_ringbuff_len) {
os_printf("WARNING! lin_ringbuff_len should be 0 when a keepalive echo is processed.\n");
return;
}
if(!elm_lin_keepalive_echo()) {
lin_bus_initialized = false;
return;
}
elm_lin_obd_msg msg = {};
msg.priority = 0xC0 | 1;
msg.receiver = 0x33;
msg.sender = 0xF1;
msg.dat[0] = 0x3E;
msg.dat[1] = msg.dat[0] + msg.priority + msg.receiver + msg.sender; // checksum
#ifdef ELM_DEBUG
os_printf("Sending LIN KEEPALIVE: Priority: %02x; RecvAddr: %02x; SendAddr: %02x; (%02x); ",
msg.priority, msg.receiver, msg.sender, 1);
for(int i = 0; i < 2; i++) os_printf("%02x ", msg.dat[i]);
os_printf("\n");
#endif
lin_waiting_keepalive_echo = true;
panda_usbemu_kline_write(&msg);
elm_reset_aux_timer();
}
static void ICACHE_FLASH_ATTR elm_init_LINFast(const elm_protocol_t* proto){
os_timer_disarm(&elm_proto_aux_timeout);
os_timer_setfn(&elm_proto_aux_timeout, (os_timer_func_t *)elm_LINFast_keepalive_timer_cb, proto);
lin_bus_initialized = false;
lin_await_msg_echo = false;
lin_waiting_keepalive_echo = false;
lin_cmd_backup = NULL;
lin_cmd_backup_len = 0;
lin_ringbuff_clear();
panda_clear_lin_txrx();
}
int ICACHE_FLASH_ATTR elm_LINFast_process_echo() {
if(!elm_lin_keepalive_echo()) {
os_printf("Keepalive echo not detected.\n");
lin_ringbuff_clear();
return -1;
}
if(!lin_await_msg_echo) {
os_printf("Echo abort. Nothing waiting echo\n");
return 1;
}
for(int i = 0; i < 4; i++){
if(lin_ringbuff_len < lin_last_sent_msg_len) elm_LIN_read_into_ringbuff();
if(lin_ringbuff_len >= lin_last_sent_msg_len){
#ifdef ELM_DEBUG
os_printf("Got enough data %d\n", lin_last_sent_msg_len);
#endif
if(!elm_LIN_ringbuff_memcmp((uint8_t*)&lin_last_sent_msg, lin_last_sent_msg_len)) {
#ifdef ELM_DEBUG
os_printf("LIN data was sent successfully.\n");
#endif
lin_ringbuff_consume(lin_last_sent_msg_len);
lin_await_msg_echo = false;
return 1;
} else {
#ifdef ELM_DEBUG
os_printf("Echo not correct.\n");
os_printf(" RB Data (%d %d %d): ", lin_ringbuff_start, lin_ringbuff_end, lin_ringbuff_len);
for(int i = 0; i < sizeof(lin_ringbuff); i++)
os_printf("%02x ", lin_ringbuff[i]);
os_printf("\n");
os_printf(" MSG Data (%d): ", lin_last_sent_msg_len);
for(int i = 0; i < lin_last_sent_msg_len; i++)
os_printf("%02x ", ((uint8_t*)&lin_last_sent_msg)[i]);
os_printf("\n");
#endif
if(lin_bus_initialized || loopcount == 0 && i == 4) {
lin_ringbuff_clear();
return -1;
} else {
os_printf("Lin init echo misaligned? Consuming byte (%02x). Retry.\n", lin_ringbuff_get(0));
lin_ringbuff_consume(1);
continue;
}
}
}
}
return !lin_await_msg_echo; //true if echo handled
}
void ICACHE_FLASH_ATTR elm_LINFast_timer_cb(void *arg){
const elm_protocol_t* proto = (const elm_protocol_t*) arg;
loopcount--;
#ifdef ELM_DEBUG
os_printf("LIN CB call\n");
#endif
if(!lin_bus_initialized) {
os_printf("WARNING: LIN CB called without bus initialized!");
return; // TODO: shoulnd't ever happen. Handle?
}
int echo_result = elm_LINFast_process_echo();
if(echo_result == -1 || (echo_result == 0 && loopcount == 0)) {
if(!is_auto_detecting){
elm_append_rsp_const("BUS ERROR\r\r>");
elm_tcp_tx_flush();
}
loopcount = 0;
lin_bus_initialized = false;
return;
}
if(echo_result == 0) {
#ifdef ELM_DEBUG
os_printf("Not ready to process\n");
#endif
os_timer_arm(&elm_timeout, 30, 0);
return; // Not ready to go on
}
#ifdef ELM_DEBUG
os_printf("Processing ELM %d\n", lin_ringbuff_len);
#endif
if(loopcount>0) {
for(int pass = 0; pass < 16 && loopcount; pass++){
elm_LIN_read_into_ringbuff();
while(lin_ringbuff_len > 0){
//if(lin_ringbuff_len > 0){
if(lin_ringbuff_get(0) & 0x80 != 0x80){
os_printf("Resetting LIN bus due to bad first byte.\n");
loopcount = 0;
lin_bus_initialized = false;
lin_ringbuff_clear();
if(!is_auto_detecting){
elm_append_rsp_const("ERROR\r\r>");
elm_tcp_tx_flush();
}
return;
}
uint8_t newmsg_len = 4 + (lin_ringbuff_get(0) & 0x7);
if(lin_ringbuff_len >= newmsg_len) {
#ifdef ELM_DEBUG
os_printf("Processing LIN MSG. BuffLen %d; expect %d. Dat: ", lin_ringbuff_len, newmsg_len);
for(int i = 0; i < newmsg_len; i++) os_printf("%02x ", lin_ringbuff_get(i));
os_printf("\n");
#endif
got_msg_this_run = true;
loopcount = LOOPCOUNT_FULL;
if(!is_auto_detecting){
if(elm_mode_additional_headers){
for(int i = 0; i < newmsg_len; i++) elm_append_rsp_hex_byte(lin_ringbuff_get(i));
} else {
for(int i = 3; i < newmsg_len - 1; i++) elm_append_rsp_hex_byte(lin_ringbuff_get(i));
}
elm_append_rsp_const("\r");
}
lin_ringbuff_consume(newmsg_len);
//elm_reset_aux_timer();
} else {
break; //Stop consuming data if there is not enough data for the next msg.
}
}
}
os_timer_arm(&elm_timeout, 50, 0);
} else {
bool got_msg_this_run_backup = got_msg_this_run;
if(!got_msg_this_run) {
#ifdef ELM_DEBUG
os_printf(" No data collected\n");
#endif
if(!is_auto_detecting) {
elm_append_rsp_const("NO DATA\r");
}
}
got_msg_this_run = false;
if(!is_auto_detecting) {
elm_append_rsp_const("\r>");
elm_tcp_tx_flush();
} else {
elm_autodetect_cb(got_msg_this_run_backup);
}
//TX RX over, resume Keepalive timer
elm_reset_aux_timer();
}
}
void ICACHE_FLASH_ATTR elm_LINFast_businit_timer_cb(void *arg){
const elm_protocol_t* proto = (const elm_protocol_t*) arg;
loopcount--;
#ifdef ELM_DEBUG
os_printf("LIN INIT CB call\n");
#endif
int echo_result = elm_LINFast_process_echo();
if(echo_result == -1 || (echo_result == 0 && loopcount == 0)) {
#ifdef ELM_DEBUG
os_printf("Init failed with echo test\n");
#endif
loopcount = 0;
lin_bus_initialized = 0;
if(!is_auto_detecting){
if(echo_result == -1)
elm_append_rsp_const("BUS ERROR\r\r>");
else
elm_append_rsp_const("ERROR\r\r>");
elm_tcp_tx_flush();
} else {
elm_autodetect_cb(false);
}
return;
}
if(echo_result == 0) {
#ifdef ELM_DEBUG
os_printf("Not ready to process\n");
#endif
os_timer_arm(&elm_timeout, elm_mode_timeout, 0);
return; // Not ready to go on
}
#ifdef ELM_DEBUG
os_printf("Bus init ready to process %d bytes\n", lin_ringbuff_len);
#endif
if(lin_bus_initialized) return; // TODO: shoulnd't ever happen. Handle?
if(loopcount>0) {
//Keep waiting for response
for(int i = 0; i < 4; i++){
elm_LIN_read_into_ringbuff();
if(lin_ringbuff_len > 0){
if(lin_ringbuff_get(0) & 0x80 != 0x80){
os_printf("Resetting LIN bus due to bad first byte.\n");
loopcount = 0;
lin_ringbuff_clear();
if(!is_auto_detecting){
elm_append_rsp_const("ERROR\r\r>");
elm_tcp_tx_flush();
} else {
elm_autodetect_cb(false);
}
return;
}
uint8_t newmsg_len = 4 + (lin_ringbuff_get(0) & 0x7);
if(lin_ringbuff_len < newmsg_len) {
os_printf("Resetting LIN because returned init data was wrong.\n");
loopcount = 0;
lin_ringbuff_clear();
if(!is_auto_detecting){
elm_append_rsp_const("ERROR\r\r>");
elm_tcp_tx_flush();
} else {
elm_autodetect_cb(false);
}
return;
}
if(!elm_LIN_ringbuff_memcmp("\x83\xF1\x10\xC1\x8F\xE9\xBD", 7)) {
lin_ringbuff_consume(7);
lin_bus_initialized = true;
//lin_ringbuff_clear();
os_printf("BUS INITIALIZED\n");
elm_reset_aux_timer();
if(!is_auto_detecting) {
elm_append_rsp_const("OK\r");
//Do the send that was delayed
if(lin_cmd_backup_len) {
elm_tcp_tx_flush();
proto->process_obd(proto, lin_cmd_backup, lin_cmd_backup_len);
} else {
elm_append_rsp_const("\r>");
elm_tcp_tx_flush();
}
} else {
#ifdef ELM_DEBUG
os_printf("LIN success. Silent because in autodetect.\n");
#endif
elm_autodetect_cb(true);
// TODO: Since bus init is good, is it ok to skip sending the '0100' msg?
}
return;
}
}
}
os_timer_arm(&elm_timeout, elm_mode_timeout, 0);
} else {
#ifdef ELM_DEBUG
os_printf("Fall through on bus init\n");
#endif
if(!is_auto_detecting){
elm_append_rsp_const("ERROR\r\r>");
elm_tcp_tx_flush();
} else {
elm_autodetect_cb(false);
}
elm_reset_aux_timer();
}
}
static void ICACHE_FLASH_ATTR elm_process_obd_cmd_LINFast(const elm_protocol_t* proto,
const char *cmd, uint16_t len) {
elm_lin_obd_msg msg = {};
uint8_t bytelen = (len-1)/2;
if((bytelen > 7 && !elm_mode_allow_long) || bytelen > 8) {
elm_append_rsp_const("?\r\r>");
return;
}
os_timer_disarm(&elm_proto_aux_timeout);
if(!lin_bus_initialized) {
panda_clear_lin_txrx();
if(!is_auto_detecting)
elm_append_rsp_const("BUS INIT: ");
lin_cmd_backup = cmd;
lin_cmd_backup_len = len;
bytelen = 1;
msg.dat[0] = 0x81;
msg.dat[1] = 0x81; // checksum
panda_kline_wakeup_pulse();
} else {
bytelen = MIN(bytelen, 7);
for(int i = 0; i < bytelen; i++){
msg.dat[i] = elm_decode_hex_byte(&cmd[i*2]);
msg.dat[bytelen] += msg.dat[i];
}
elm_msg_mode_ret_filter = msg.dat[0];
elm_msg_pid_ret_filter = msg.dat[1];
}
msg.priority = 0xC0 | bytelen;
msg.receiver = 0x33;
msg.sender = 0xF1;
msg.dat[bytelen] += msg.priority + msg.receiver + msg.sender; // checksum
#ifdef ELM_DEBUG
os_printf("Sending LIN OBD: Priority: %02x; RecvAddr: %02x; SendAddr: %02x; (%02x); ",
msg.priority, msg.receiver, msg.sender, bytelen);
for(int i = 0; i < 8; i++) os_printf("%02x ", msg.dat[i]);
os_printf("\n");
#endif
lin_last_sent_msg_len = (msg.priority & 0x07) + 4;
memcpy(&lin_last_sent_msg, &msg, lin_last_sent_msg_len);
lin_await_msg_echo = true;
panda_usbemu_kline_write(&msg);
loopcount = LOOPCOUNT_FULL + 1;
os_timer_disarm(&elm_timeout);
if(lin_bus_initialized) {
os_timer_setfn(&elm_timeout, (os_timer_func_t *)elm_LINFast_timer_cb, proto);
elm_LINFast_timer_cb((void*)proto);
} else {
os_timer_setfn(&elm_timeout, (os_timer_func_t *)elm_LINFast_businit_timer_cb, proto);
elm_LINFast_businit_timer_cb((void*)proto);
}
}
/*****************************************************
*** ELM protocol specification and implementation ***
*** -> ISO 15765-4 implementation ***
*****************************************************/
void ICACHE_FLASH_ATTR elm_ISO15765_timer_cb(void *arg){
const elm_protocol_t* proto = (const elm_protocol_t*) arg;
loopcount--;
if(loopcount>0) {
for(int pass = 0; pass < 16 && loopcount; pass++){
panda_can_msg_t *can_msgs;
int num_can_msgs = panda_usbemu_can_read(&can_msgs);
#ifdef ELM_DEBUG
if(num_can_msgs) os_printf(" Received %d can messages\n", num_can_msgs);
#endif
if(num_can_msgs < 0) continue;
if(!num_can_msgs) break;
for(int i = 0; i < num_can_msgs; i++){
panda_can_msg_t *recv = &can_msgs[i];
#ifdef ELM_DEBUG
os_printf(" RECV: Bus: %d; Addr: %08x; ext: %d; tx: %d; Len: %d; ",
recv->bus, panda_get_can_addr(recv), recv->ext, recv->tx, recv->len);
for(int j = 0; j < recv->len; j++) os_printf("%02x ", recv->data[j]);
os_printf("Ts: %d\n", recv->ts);
#endif
if (recv->bus==0 && recv->len == 8 &&
(
(proto->type == CAN11 && !recv->ext && (panda_get_can_addr(recv) & 0x7F8) == 0x7E8) ||
(proto->type == CAN29 && recv->ext && (panda_get_can_addr(recv) & 0x1FFFFF00) == 0x18DAF100)
)
) {
if(recv->data[0] <= 7 &&
recv->data[1] == (0x40|elm_msg_mode_ret_filter) &&
recv->data[2] == elm_msg_pid_ret_filter) {
got_msg_this_run = true;
loopcount = LOOPCOUNT_FULL;
#ifdef ELM_DEBUG
os_printf(" CAN msg response, index: %d\n", i);
#endif
if(!is_auto_detecting){
if(elm_mode_additional_headers){
elm_append_rsp_can_msg_addr(recv);
for(int j = 0; j < recv->data[0]+1; j++) elm_append_rsp_hex_byte(recv->data[j]);
} else {
for(int j = 1; j < recv->data[0]+1; j++) elm_append_rsp_hex_byte(recv->data[j]);
}
elm_append_rsp_const("\r");
elm_tcp_tx_flush();
}
} else if((recv->data[0] & 0xF0) == 0x10 &&
recv->data[2] == (0x40|elm_msg_mode_ret_filter) &&
recv->data[3] == elm_msg_pid_ret_filter) {
got_msg_this_run = true;
loopcount = LOOPCOUNT_FULL;
panda_usbemu_can_write(0,
(proto->type==CAN11) ?
0x7E0 | (panda_get_can_addr(recv)&0x7) :
(0x18DA00F1 | (((panda_get_can_addr(recv))&0xFF)<<8)),
"\x30\x00\x00", 3);
did_multimessage = true;
#ifdef ELM_DEBUG
os_printf(" CAN multimsg start response, index: %d, len %d\n", i,
((recv->data[0]&0xF)<<8) | recv->data[1]);
#endif
if(!is_auto_detecting){
if(!elm_mode_additional_headers) {
elm_append_rsp(&hex_lookup[recv->data[0]&0xF], 1);
elm_append_rsp_hex_byte(recv->data[1]);
elm_append_rsp_const("\r0:");
if(elm_mode_print_spaces) elm_append_rsp_const(" ");
for(int j = 2; j < 8; j++) elm_append_rsp_hex_byte(recv->data[j]);
} else {
elm_append_rsp_can_msg_addr(recv);
for(int j = 0; j < 8; j++) elm_append_rsp_hex_byte(recv->data[j]);
}
elm_append_rsp_const("\r");
elm_tcp_tx_flush();
}
} else if (did_multimessage && (recv->data[0] & 0xF0) == 0x20) {
got_msg_this_run = true;
loopcount = LOOPCOUNT_FULL;
#ifdef ELM_DEBUG
os_printf(" CAN multimsg data response, index: %d\n", i);
#endif
if(!is_auto_detecting){
if(!elm_mode_additional_headers) {