BOROSOFTWAREAll work →
scantool/scantool.c 1768 lines
1/*
2 * freediag - Vehicle Diagnostic Utility
3 *
4 *
5 * Copyright (C) 2001 Richard Almeida & Ibex Ltd ([email protected])
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 *************************************************************************
22 *
23 *
24 * Mostly ODBII Compliant Scanner Program (SAE J1978)
25 *
26 * ODBII Scanners are defined in SAE J1978. References in this document
27 * are to SAE J1978 Revised Feb1998. This document is available from
28 * www.sae.org
29 *
30 * From Section 5. Required functions & support - the following are the basic
31 * functions that the scan tool is required to support or provide
32 *
33 * a. Automatic hands-off determination of the communication interface user
34 * b. Obtaining and displaying the status and results of vehicle on-board
35 * diagnostic evaluations
36 * c. Obtaining & Displaying ODB II emissions related DTCs
37 * d. Obtaining & Displaying ODB II emissions related current data
38 * e. Obtaining & Displaying ODB II emissions related freeze frame data
39 * f. Clearing the storage of (c) to (e)
40 * g. Obtaining and displaying ODB II emissions related test params and
41 * results as described in SAE J1979
42 * h. Provide a user manual and/or help facility
43 *
44 * Section 6 - Vehicle interface
45 * Communication Data Link and Physical Layers
46 * SAE J1850 interface
47 * ISO 9141-2 interface
48 * ISO 14230-4
49 *
50 * Section 7.3 - the scan tool must be capable of interfacing with a
51 * vehicle in which multiple modules may be used to support ODBII
52 * requirements
53 * The ODBII Scan tool must alert the user when multiple modules
54 * respond to the same request
55 * Ditto if different values
56 * The tool must provide the user with the ability to select for
57 * display as separate display items the responses received from
58 * multiple modules for the same data item
59 *
60 *
61 * THIS DOESN'T SUPPORT Section 6 as we only have one interface
62 * - It "copes" with 7.3 but doesn't tell user or allow user to select
63 * which module to see responses from
64 *
65 *
66 *************************************************************************
67 *
68 * This file contains the workhorse routines, ie all that execute the
69 * J1979 (ODBII) protocol
70 */
71
72#include <assert.h>
73#include <stdbool.h>
74#include <stdio.h>
75#include <stdlib.h>
76#include <string.h>
77
78#include "diag.h"
79#include "diag_err.h"
80#include "diag_os.h"
81#include "diag_dtc.h"
82#include "diag_l1.h"
83#include "diag_l2.h"
84#include "diag_l3.h"
85
86#include "scantool.h"
87#include "scantool_obd.h"
88#include "scantool_cli.h"
89#include "utlist.h"
90
91
92uint8_t global_O2_sensors; /* O2 sensors bit mask */
93
94/*
95 * Data received from each ecu
96 */
97ecu_data ecu_info[MAX_ECU];
98unsigned int ecu_count; /* How many ecus are active */
99
100
101/* Merge of all the suported mode1 pids by all the ECUs */
102uint8_t merged_mode1_info[0x100];
103uint8_t merged_mode5_info[0x100];
104
105
106/* Prototypes */
107int print_single_dtc(databyte_type d0, databyte_type d1);
108void do_j1979_getmodeinfo(uint8_t mode, int response_offset);
109
110//these are mostly dummy variables only used for some j1979 features.
111//see scantool.h
112const int _RQST_HANDLE_NORMAL = RQST_HANDLE_NORMAL; //Normal mode
113const int _RQST_HANDLE_WATCH = RQST_HANDLE_WATCH; //Watching: add timestamp
114const int _RQST_HANDLE_DECODE = RQST_HANDLE_DECODE; //Just decode what arrived
115const int _RQST_HANDLE_NCMS = RQST_HANDLE_NCMS; //Non cont. mon. tests
116const int _RQST_HANDLE_NCMS2 = RQST_HANDLE_NCMS2; //Same: print fails only
117const int _RQST_HANDLE_O2S = RQST_HANDLE_O2S; //O2 sensor tests
118const int _RQST_HANDLE_READINESS = RQST_HANDLE_READINESS; //Readiness tests
119
120
121struct diag_msg *find_ecu_msg(int byte, databyte_type val) {
122 ecu_data *ep;
123 struct diag_msg *rxmsg = NULL;
124 unsigned int i;
125
126 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
127 if (ep->rxmsg) {
128 /* Some data arrived from this ecu */
129 if (ep->rxmsg->data[byte] == val) {
130 rxmsg = ep->rxmsg;
131 break;
132 }
133 }
134 }
135 return rxmsg;
136}
137
138
139
140/*
141 * ************
142 * Basic routines to connect/interrogate an ECU
143 * ************
144 */
145
146/*
147 * Receive callback routines. If handle is RQST_HANDLE_WATCH then we're in "watch"
148 * mode (set by caller to recv()), else in normal data mode
149 *
150 * We get called by L3/L2 with all the messages received within the
151 * window, i.e we can get responses from many ECUs that all relate to
152 * a single request [ISO9141/14230 uses timing windows to decide when
153 * no more responses will arrive]
154 *
155 * We can [and do] get more than one ecu responding with different bits
156 * of data on certain vehicles
157 */
158void j1979_data_rcv(void *handle, struct diag_msg *msg) {
159 assert(msg != NULL);
160 uint8_t *data;
161 struct diag_msg *tmsg;
162 unsigned int i;
163 int ihandle;
164 ecu_data *ep;
165
166 if (handle != NULL) {
167 ihandle= *(int *)handle;
168 } else {
169 ihandle = RQST_HANDLE_NORMAL;
170 }
171
172 const char *O2_strings[] = {
173 "Test 0",
174 "Rich to lean sensor threshold voltage",
175 "Lean to rich sensor threshold voltage",
176 "Low sensor voltage for switch time calc.",
177 "High sensor voltage for switch time calc.",
178 "Rich to lean sensor switch time",
179 "Lean to rich sensor switch time",
180 "Minimum sensor voltage for test cycle",
181 "Maximum sensor voltage for test cycle",
182 "Time between sensor transitions"
183 };
184
185 DIAG_DBGMDATA(diag_cli_debug, DIAG_DEBUG_DATA, DIAG_DBGLEVEL_V,
186 msg->data, msg->len,
187 "scantool: Got handle %p; %u bytes of data, src=0x%X, dest=0x%X; ",
188 handle, msg->len, msg->src, msg->dest);
189
190 /* Deal with the diag type responses (send/recv/watch) */
191 switch (ihandle) {
192 /* There is no difference between watch and decode ... */
193 case RQST_HANDLE_WATCH:
194 case RQST_HANDLE_DECODE:
195 diag_printmsg(stdout, msg, 0);
196 return;
197 break;
198 default:
199 break;
200 }
201
202
203 /* All other responses are J1979 response messages */
204
205 /* Clear out old messages */
206 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
207 if (ep->rxmsg != NULL) {
208 /* Old msg. release it */
209 diag_freemsg(ep->rxmsg);
210 ep->rxmsg = NULL;
211 }
212 }
213
214 /*
215 * We may get more than one msg here, as more than one
216 * ECU may respond.
217 */
218 LL_FOREACH(msg, tmsg) {
219 uint8_t src = tmsg->src;
220 unsigned int ecu_idx;
221 struct diag_msg *rmsg;
222 bool found=0;
223
224 for (ecu_idx=0, ep=ecu_info; ecu_idx<MAX_ECU; ecu_idx++, ep++) {
225 if (ep->valid) {
226 if (ep->ecu_addr == src) {
227 found = 1;
228 break;
229 }
230 } else {
231 ecu_count++;
232 ep->valid = 1;
233 ep->ecu_addr = src;
234 found = 1;
235 break;
236 }
237 }
238 if (!found) {
239 fprintf(stderr, "ERROR: Too many ECUs responded\n");
240 fprintf(stderr, "ERROR: Info from ECU addr 0x%02X ignored\n", src);
241 return;
242 }
243
244 /* Ok, we now have the ecu_info for this message fragment */
245
246 /* Attach the fragment to the ecu_info */
247 rmsg = diag_dupsinglemsg(tmsg);
248 if (rmsg == NULL) {
249 return;
250 }
251 LL_CONCAT(ep->rxmsg, rmsg);
252
253 /*
254 * Deal with readiness tests, ncms and O2 sensor tests
255 * Note that ecu_count gets to the correct value from
256 * the first response from the ECU which is the mode1pid0
257 * response
258 */
259 data = msg->data;
260 switch (ihandle) {
261 case RQST_HANDLE_READINESS:
262 /* Handled in cmd_test_readiness() */
263 break;
264 case RQST_HANDLE_NCMS:
265 case RQST_HANDLE_NCMS2:
266 /*
267 * Non Continuously Monitored System result
268 * NCMS2 prints everything, NCMS prints just failed
269 * tests
270 */
271 if (data[0] != 0x46) {
272 fprintf(stderr, "Test 0x%02X failed %d\n",
273 data[1], data[2]);
274 return;
275 }
276 if ((data[1] & 0x1f) == 0) {
277 /* no Test support */
278 return;
279 }
280 LL_FOREACH(msg, tmsg) {
281 int val, lim;
282 data = tmsg->data;
283
284 val = (data[3]*255) + data[4];
285 lim = (data[5]*255) + data[6];
286
287 if ((data[2] & 0x80) == 0) {
288 if (ihandle == RQST_HANDLE_NCMS2) {
289 /* Only print fails */
290 if (val > lim) {
291 fprintf(stderr, "Test 0x%X Component 0x%X FAILED ",
292 data[1], data[2] & 0x7f);
293 fprintf(stderr, "Max val %d Current Val %d\n",
294 lim, val);
295
296 }
297 } else {
298 /* Max value test */
299 fprintf(stderr, "Test 0x%X Component 0x%X ",
300 data[1], data[2] & 0x7f);
301
302 if (val > lim) {
303 fprintf(stderr, "FAILED ");
304 } else {
305 fprintf(stderr,
306 "Passed"
307 " ");
308 }
309
310 fprintf(stderr, "Max val %d Current Val %d\n",
311 lim, val);
312 }
313 } else {
314 if (ihandle == RQST_HANDLE_NCMS2) {
315 if (val < lim) {
316 fprintf(stderr, "Test 0x%X Component 0x%X FAILED ",
317 data[1], data[2] & 0x7f);
318 fprintf(stderr, "Min val %d Current Val %d\n",
319 lim, val);
320 }
321 } else {
322 /* Min value test */
323 fprintf(stderr, "Test 0x%X Component 0x%X ",
324 data[1], data[2] & 0x7f);
325 if (val < lim) {
326 fprintf(stderr, "FAILED ");
327 } else {
328 fprintf(stderr,
329 "Passed"
330 " ");
331 }
332
333 fprintf(stderr, "Min val %d Current Val %d\n",
334 lim, val);
335 }
336 }
337 }
338 return;
339 case RQST_HANDLE_O2S:
340 if (ecu_count > 1) {
341 fprintf(stderr, "ECU %u ", ecu_idx);
342 }
343
344 /* O2 Sensor test results */
345 if (msg->data[0] != 0x45) {
346 fprintf(stderr, "Test 0x%02X failed %d\n",
347 msg->data[1], msg->data[2]);
348 return;
349 }
350 if ((data[1] & 0x1f) == 0) {
351 /* No Test support */
352 } else {
353 int val = data[4];
354 int min = data[5];
355 int max = data[6];
356 int failed;
357
358 if ((val < min) || (val > max)) {
359 failed = 1;
360 } else {
361 failed = 0;
362 }
363
364 switch (data[1]) {
365 case 1: /* Constant values voltages */
366 case 2:
367 case 3:
368 case 4:
369 fprintf(stderr, "%s: %f\n", O2_strings[data[1]],
370 data[4]/200.0);
371 break;
372 case 5:
373 case 6:
374 case 9:
375 fprintf(stderr, "%s: actual %2.2f min %2.2f max %2.2f %s\n",
376 O2_strings[data[1]], data[4]/250.0,
377 data[5]/250.0, data[6]/250.0,
378 failed?"FAILED":"Passed" );
379 break;
380 case 7:
381 case 8:
382 fprintf(stderr, "%s: %f %f %f %s\n", O2_strings[data[1]],
383 data[4]/200.,
384 data[5]/200.,
385 data[6]/200.,
386 failed?"FAILED":"Passed" );
387 break;
388 default:
389 fprintf(stderr, "Test %d: actual 0x%X min 0x%X max 0x%X %s\n",
390 data[1], data[4],
391 data[5], data[6],
392 failed?"FAILED":"Passed" );
393 break;
394 }
395 }
396 return;
397 }
398 }
399 return;
400}
401
402
403/*
404 * Receive callback routines, for watching mode, call
405 * L3 (in this case SAE J1979) decode routine, if handle is NULL
406 * just print the data
407 */
408void j1979_watch_rcv(void *handle, struct diag_msg *msg) {
409 struct diag_msg *tmsg;
410 int i=0;
411
412 LL_FOREACH(msg, tmsg) {
413 diag_printmsg_header(stderr, tmsg, 1, i);
414
415 if (handle != NULL) {
416 char buf[256]; /* XXX Can we switch to stdargs for decoders? */
417 diag_l3_decode((struct diag_l3_conn *)handle, tmsg, buf, sizeof(buf));
418 fprintf(stderr, "%s\n", buf);
419 } else {
420 diag_data_dump(stderr, tmsg->data, tmsg->len);
421 fprintf(stderr, "\n");
422 }
423 i++;
424 }
425}
426
427
428void l2raw_data_rcv(UNUSED(void *handle), struct diag_msg *msg) {
429 /*
430 * Layer 2 call back, just print the data, this is used if we
431 * do a "read" and we haven't yet added a L3 protocol
432 */
433 diag_printmsg(stderr, msg, 0);
434 return;
435}
436
437/*
438 * Routine to check the bitmasks of PIDS received in response
439 * to a mode 1 PID 0/0x20/0x40 request
440 */
441int l2_check_pid_bits(uint8_t *data, int pid) {
442 int offset;
443 int bit;
444
445 pid--; /* (bits start at 0, pids at 1) */
446 /*
447 * Bits 1-8 are in byte 1, 9-16 in byte 2 etc
448 * Same code is for PID requests for 0x40 and 0x60
449 */
450 while (pid > 0x20) {
451 pid -= 0x20;
452 }
453 offset = pid/8;
454
455 bit = pid - (offset * 8);
456 bit = 7 - bit;
457
458 if (data[offset] & (1 << bit)) {
459 return 1;
460 }
461
462 return 0;
463}
464
465int l3_do_j1979_rqst(struct diag_l3_conn *d_conn, uint8_t mode, uint8_t p1, uint8_t p2,
466 uint8_t p3, uint8_t p4, uint8_t p5, uint8_t p6, void *handle) {
467 assert(d_conn != NULL);
468 struct diag_msg msg = {0};
469 uint8_t data[7];
470 int ihandle;
471 int rv;
472 ecu_data *ep;
473 unsigned int i;
474
475 uint8_t *rxdata;
476 struct diag_msg *rxmsg;
477
478 if (handle != NULL) {
479 ihandle= *(int *) handle;
480 } else {
481 ihandle = RQST_HANDLE_NORMAL;
482 }
483 /* Lengths of msg for each mode, 0 = this routine doesn't support */
484 // excludes headers and checksum.
485 const uint8_t mode_lengths[] = { 0, 2, 3, 1, 1, 3, 2, 1, 7, 2 };
486#define J1979_MODE_MAX 9
487
488 DIAG_DBGM(diag_cli_debug, DIAG_DEBUG_PROTO, DIAG_DBGLEVEL_V,
489 "j1979_rqst: handle %p conn %p mode %#02X\n",
490 handle, (void *)d_conn, mode);
491
492 /* Put in src/dest etc, L3 or L2 may override/ignore them */
493 msg.src = global_cfg.src;
494 msg.dest = global_cfg.tgt;
495
496 /* XXX add funcmode flags */
497
498 if (mode > J1979_MODE_MAX) {
499 return diag_iseterr(DIAG_ERR_PROTO_NOTSUPP);
500 }
501
502 msg.len = mode_lengths[mode];
503 if (msg.len == 0) {
504 return diag_iseterr(DIAG_ERR_PROTO_NOTSUPP);
505 }
506
507 msg.data = data;
508 data[0] = mode;
509 data[1] = p1;
510 data[2] = p2;
511 data[3] = p3;
512 data[4] = p4;
513 data[5] = p5;
514 data[6] = p6;
515 if ((rv = diag_l3_send(d_conn, &msg))) {
516 return diag_ifwderr(rv);
517 }
518
519 /* And get response(s) within a short while */
520 rv = diag_l3_recv(d_conn, 300, j1979_data_rcv, handle);
521 if (rv < 0) {
522 fprintf(stderr, "Request failed, retrying...\n");
523 if ((rv = diag_l3_send(d_conn, &msg))) {
524 return diag_ifwderr(rv);
525 }
526 rv = diag_l3_recv(d_conn, 300, j1979_data_rcv, handle);
527 if (rv < 0) {
528 fprintf(stderr, "Retry failed, resynching...\n");
529 rv= d_conn->d_l3_proto->diag_l3_proto_timer(d_conn, 6000); //force keepalive
530 if (rv < 0) {
531 fprintf(stderr, "\tfailed, connection to ECU may be lost!\n");
532 return diag_ifwderr(rv);
533 }
534 fprintf(stderr, "\tOK.\n");
535 return DIAG_ERR_TIMEOUT;
536
537 }
538 }
539
540 //This part is super confusing: ihandle comes from the handle from a callback passed
541 //between L2 and L3 with handles to handles etc..
542 switch (ihandle) {
543 /* We dont process the info in watch/decode mode */
544 case RQST_HANDLE_WATCH:
545 case RQST_HANDLE_DECODE:
546 return rv;
547 break;
548 default:
549 break;
550 }
551
552 /*
553 * Go thru the ecu_data and see what was received.
554 */
555 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
556 if (ep->rxmsg) {
557 /* Some data arrived from this ecu */
558 rxmsg = ep->rxmsg;
559 rxdata = ep->rxmsg->data;
560
561 /* A bit of ugliness is required to bail out on NegativeResponse messages when using
562 * an iso14230 L2.
563 */
564 if (d_conn->d_l3l2_conn->l2proto->diag_l2_protocol == DIAG_L2_PROT_ISO14230) {
565 if (rxdata[0] == 0x7f) {
566 return DIAG_ERR_ECUSAIDNO;
567 }
568 }
569 switch (mode) {
570 case 1:
571 if (rxdata[0] != 0x41) {
572 ep->mode1_data[p1].type = TYPE_FAILED;
573 break;
574 }
575 memcpy(ep->mode1_data[p1].data, rxdata,
576 rxmsg->len);
577 ep->mode1_data[p1].len = rxmsg->len;
578 ep->mode1_data[p1].type = TYPE_GOOD;
579
580 break;
581 case 2:
582 if (rxdata[0] != 0x42) {
583 ep->mode2_data[p1].type = TYPE_FAILED;
584 break;
585 }
586 memcpy(ep->mode2_data[p1].data, rxdata,
587 rxmsg->len);
588 ep->mode2_data[p1].len = rxmsg->len;
589 ep->mode2_data[p1].type = TYPE_GOOD;
590 break;
591 }
592 }
593 }
594 return 0;
595}
596
597
598/*
599 * Send some data to the ECU (L3)
600 */
601int l3_do_send(struct diag_l3_conn *d_conn, void *data, size_t len, void *handle) {
602 struct diag_msg msg = {0};
603 int rv;
604 if (len > 255) {
605 return DIAG_ERR_GENERAL;
606 }
607
608 /* Put in src/dest etc, L3 or L2 may override/ignore them */
609 msg.src = global_cfg.src;
610 msg.dest = global_cfg.tgt;
611
612 msg.len = (uint8_t) len;
613 msg.data = (uint8_t *)data;
614 diag_l3_send(d_conn, &msg);
615
616 /* And get response(s) */
617 rv = diag_l3_recv(d_conn, 300, j1979_data_rcv, handle);
618
619 return rv;
620}
621/*
622 * Same but L2 type
623 */
624int l2_do_send(struct diag_l2_conn *d_conn, void *data, size_t len, void *handle) {
625 struct diag_msg msg = {0};
626 int rv;
627 if (len > 255) {
628 return DIAG_ERR_GENERAL;
629 }
630
631 /* Put in src/dest etc, L2 may override/ignore them */
632 msg.src = global_cfg.src;
633 msg.dest = global_cfg.tgt;
634
635 msg.len = len;
636 msg.data = (uint8_t *)data;
637
638 /*
639 * Special case for l2_vag: it requires the command opcode (Block Title)
640 * to be placed in msg.type. The other L2s consider the opcode (Service
641 * ID, etc) to be part of the data.
642 */
643 if (d_conn->l2proto->diag_l2_protocol == DIAG_L2_PROT_VAG) {
644 if (len < 1) {
645 return DIAG_ERR_GENERAL;
646 }
647 msg.type = msg.data[0];
648 msg.len--;
649 msg.data++;
650 }
651
652 diag_l2_send(d_conn, &msg);
653
654 /* And get response(s) */
655 rv = diag_l2_recv(d_conn, 300, l2raw_data_rcv, handle);
656
657 return rv;
658}
659
660
661/*
662 * Clear data that is relevant to an ECU
663 */
664static int clear_data(void) {
665 ecu_count = 0;
666 memset(ecu_info, 0, sizeof(ecu_info));
667
668 memset(merged_mode1_info, 0, sizeof(merged_mode1_info));
669 memset(merged_mode5_info, 0, sizeof(merged_mode5_info));
670
671 return 0;
672}
673
674/*
675 * Common start routine used by all protocols
676 * - initialises the diagnostic layer
677 * - opens a Layer 2 device for the specified Layer 1 protocol
678 * returns L2 file descriptor
679 */
680static struct diag_l2_conn *do_l2_common_start(int L1protocol, int L2protocol,
681 flag_type type, unsigned int bitrate, target_type target, source_type source ) {
682 int rv;
683 struct diag_l0_device *dl0d = global_dl0d;
684 struct diag_l2_conn *d_conn = NULL;
685
686 if (!global_dl0d) {
687 printf("No global L0. Please select + configure L0 first\n");
688 return NULL;
689 }
690 /* Clear out all ECU data as we're starting again */
691 clear_data();
692
693 rv = diag_l2_open(dl0d, L1protocol);
694 if (rv) {
695 if ((rv != DIAG_ERR_BADIFADAPTER) &&
696 (rv != DIAG_ERR_PROTO_NOTSUPP)) {
697 fprintf(stderr, "Failed to open hardware interface\n");
698 }
699
700 return diag_pfwderr(rv);
701 }
702
703 /* Now do the Layer 2 startcommunications */
704
705 d_conn = diag_l2_StartCommunications(dl0d, L2protocol, type,
706 bitrate, target, source);
707
708 if (d_conn == NULL) {
709 diag_l2_close(dl0d);
710 fprintf(stderr, FLFMT "l2_common_start: l2_StartComm failed\n", FL);
711 return NULL;
712 }
713
714
715 /*
716 * Now Get the L2 flags, and if this is a network type where
717 * startcommunications always works, we have to try and see if
718 * the ECU is there
719 *
720 * Some interface types will always return success from StartComms()
721 * (like J1850)
722 * but you only know if the ECU is on the network if you then can
723 * send/receive data to it in the appropriate format.
724 * For those type of interfaces we send a J1979 mode1 pid0 request
725 * (since we are a scantool, thats the correct thing to send)
726 * But this will happen when the caller tries to add a J1979 L3
727 * handler; at the L2 level "mode 1 pid 0" is meaningless.
728 */
729
730 return d_conn;
731}
732
733
734/*
735 * 9141 init
736 */
737int do_l2_9141_start(int destaddr) {
738 struct diag_l2_conn *d_conn;
739
740 d_conn = do_l2_common_start(DIAG_L1_ISO9141, DIAG_L2_PROT_ISO9141,
741 DIAG_L2_TYPE_SLOWINIT, global_cfg.speed, (uint8_t)destaddr,
742 global_cfg.src);
743
744 if (d_conn == NULL) {
745 return diag_iseterr(DIAG_ERR_GENERAL);
746 }
747
748 /* Connected ! */
749 global_l2_conn = d_conn;
750
751 return 0;
752}
753
754/*
755 * 14120 init
756 */
757int do_l2_14230_start(int init_type) {
758 struct diag_l2_conn *d_conn;
759 flag_type flags = 0;
760
761 if (global_cfg.addrtype) {
762 flags = DIAG_L2_TYPE_FUNCADDR;
763 } else {
764 flags = 0;
765 }
766
767 flags |= DIAG_L2_IDLE_J1978; /* Use J1978 idle msgs */
768
769 flags |= (init_type & DIAG_L2_TYPE_INITMASK);
770
771 d_conn = do_l2_common_start(DIAG_L1_ISO14230, DIAG_L2_PROT_ISO14230,
772 flags, global_cfg.speed, global_cfg.tgt, global_cfg.src);
773
774 if (d_conn == NULL) {
775 return diag_iseterr(DIAG_ERR_GENERAL);
776 }
777
778 /* Connected ! */
779 global_l2_conn = d_conn;
780
781 return 0;
782}
783
784/*
785 * J1850 init, J1850 interface type passed as l1_type
786 */
787static int do_l2_j1850_start(int l1_type) {
788 flag_type flags = 0;
789 struct diag_l2_conn *d_conn;
790
791 d_conn = do_l2_common_start(l1_type, DIAG_L2_PROT_SAEJ1850,
792 flags, global_cfg.speed, 0x6a, global_cfg.src);
793
794 if (d_conn == NULL) {
795 return diag_iseterr(DIAG_ERR_GENERAL);
796 }
797
798 /* Connected ! */
799 global_l2_conn = d_conn;
800
801 return 0;
802}
803
804
805/*
806 * Gets the data for every supported test using global L3 connection
807 *
808 * Returns <0 on failure, 0 on good and 1 on interrupted
809 *
810 * If Interruptible is 1, then this is interruptible by the stdin
811 * becoming ready for read (using diag_os_ipending()), which amounts to "was Enter pressed".
812 *
813 * It is used in "Interuptible" mode when doing "monitor" command
814 */
815int do_j1979_getdata(int interruptible) {
816 unsigned int i,j;
817 int rv;
818 struct diag_l3_conn *d_conn;
819 ecu_data *ep;
820 struct diag_msg *msg;
821
822 d_conn = global_l3_conn;
823 if (d_conn == NULL) {
824 return diag_iseterr(DIAG_ERR_GENERAL);
825 }
826
827 diag_os_ipending(); //this is necessary on WIN32 to "purge" the last state of the enter key; we can't just poll stdin.
828
829 /*
830 * Now get all the data supported
831 */
832 for (i=3; i<0x100; i++) {
833 if (merged_mode1_info[i]) {
834 fprintf(stderr, "Requesting Mode 1 Pid 0x%02X...\n", i);
835 rv = l3_do_j1979_rqst(d_conn, 0x1, (uint8_t) i, 0x00,
836 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
837 if (rv < 0) {
838 fprintf(stderr, "Mode 1 Pid 0x%02X request failed (%d)\n",
839 i, rv);
840 } else {
841 msg = find_ecu_msg(0, 0x41);
842 if (msg == NULL) {
843 fprintf(stderr,
844 "Mode 1 Pid 0x%02X request "
845 "no-data (%d)\n",
846 i, rv);
847 }
848 }
849
850 if (interruptible) {
851 if (diag_os_ipending()) {
852 return 1;
853 }
854 }
855 }
856 }
857
858 /* Get mode2/pid2 (DTC that caused freezeframe) */
859 fprintf(stderr, "Requesting Mode 0x02 Pid 0x02 (Freeze frame DTCs)...\n");
860 rv = l3_do_j1979_rqst(d_conn, 0x2, 2, 0x00,
861 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
862
863 if (rv < 0) {
864 fprintf(stderr, "Mode 0x02 Pid 0x02 request failed (%d)\n", rv);
865 return DIAG_ERR_GENERAL;
866 }
867 msg = find_ecu_msg(0, 0x42);
868 if (msg == NULL) {
869 fprintf(stderr, "Mode 0x02 Pid 0x02 request no-data (%d)\n", rv);
870 return DIAG_ERR_GENERAL;
871 }
872 diag_os_ipending(); //again, required for WIN32 to "purge" last keypress
873 /* Now go thru the ECUs that have responded with mode2 info */
874 for (j=0, ep=ecu_info; j<ecu_count; j++, ep++) {
875 if ( (ep->mode2_data[2].type == TYPE_GOOD) &&
876 (ep->mode2_data[2].data[2] |
877 ep->mode2_data[2].data[3]) ) {
878 for (i=3; i<0x100; i++) {
879 if (ep->mode2_info[i]) {
880 fprintf(stderr, "Requesting Mode 0x02 Pid 0x%02X...\n", i);
881 rv = l3_do_j1979_rqst(d_conn, 0x2, (uint8_t)i, 0x00,
882 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
883 if (rv < 0) {
884 fprintf(stderr, "Mode 0x02 Pid 0x%02X request failed (%d)\n", i, rv);
885 } else {
886 msg = find_ecu_msg(0, 0x42);
887 if (msg == NULL) {
888 fprintf(stderr, "Mode 0x02 Pid 0x%02X request no-data (%d)\n", i, rv);
889 return DIAG_ERR_GENERAL;
890 }
891 }
892
893 }
894 if (interruptible) {
895 if (diag_os_ipending()) { // was Enter
896 // pressed
897 return 1;
898 }
899 }
900 }
901 }
902 }
903 return 0;
904}
905
906/*
907 * Find out basic info from the ECU (what it supports, DTCs etc)
908 *
909 * This is the basic work horse routine
910 */
911void do_j1979_basics() {
912 ecu_data *ep;
913 unsigned int i;
914 int o2monitoring = 0;
915
916 /*
917 * Get supported PIDs and Tests etc
918 */
919 do_j1979_getpids();
920
921 global_state = STATE_SCANDONE;
922
923 /*
924 * Get current DTCs/MIL lamp status/Tests supported for this ECU
925 * and test, and wait for those tests to complete
926 */
927 do_j1979_getdtcs();
928
929 /*
930 * Get data supported by ECU, non-interruptibly
931 */
932 do_j1979_getdata(0);
933
934 /*
935 * And now do stuff with that data
936 */
937 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
938 if ( (ep->mode1_data[2].type == TYPE_GOOD) &&
939 (ep->mode1_data[2].data[2] | ep->mode1_data[2].data[3]) ) {
940 fprintf(stderr, "ECU %u Freezeframe data exists, caused by DTC ",
941 i);
942 print_single_dtc(ep->mode1_data[2].data[2], ep->mode1_data[2].data[3]);
943 fprintf(stderr, "\n");
944 }
945
946 if (ep->mode1_data[0x1c].type == TYPE_GOOD) {
947 fprintf(stderr, "ECU %u is ", i);
948 switch (ep->mode1_data[0x1c].data[2]) {
949 case 1:
950 fprintf(stderr, "OBD II (California ARB)");
951 break;
952 case 2:
953 fprintf(stderr, "OBD (Federal EPA)");
954 break;
955 case 3:
956 fprintf(stderr, "OBD and OBD II");
957 break;
958 case 4:
959 fprintf(stderr, "OBD I");
960 break;
961 case 5:
962 fprintf(stderr, "not OBD");
963 break;
964 case 6:
965 fprintf(stderr, "EOBD (Europe)");
966 break;
967 default:
968 fprintf(stderr, "unknown (%d)", ep->mode1_data[0x1c].data[2]);
969 break;
970 }
971 fprintf(stderr, " compliant\n");
972 }
973
974 /*
975 * If ECU supports Oxygen sensor monitoring, then do O2 sensor
976 * stuff
977 */
978 if ( (ep->mode1_data[1].type == TYPE_GOOD) &&
979 (ep->mode1_data[1].data[4] & 0x20) ) {
980 o2monitoring = 1;
981 }
982 }
983 do_j1979_getO2sensors();
984 if (o2monitoring > 0) {
985 do_j1979_O2tests();
986 } else {
987 fprintf(stderr, "Oxygen (O2) sensor monitoring not supported\n");
988 }
989}
990
991int print_single_dtc(databyte_type d0, databyte_type d1) {
992 char buf[256];
993
994 uint8_t db[2];
995 db[0] = d0;
996 db[1] = d1;
997
998 fprintf(stderr, "%s",
999 diag_dtc_decode(db, 2, NULL, NULL, dtc_proto_j2012, buf,
1000 sizeof(buf)));
1001
1002 return 0;
1003}
1004
1005static void print_dtcs(uint8_t *data, uint8_t len) {
1006 /* Print the DTCs just received */
1007 int i, j;
1008
1009 for (i=0, j=1; (i<3) && ((j+1) < len); i++, j+=2) {
1010 if ((data[j] == 0) && (data[j + 1] == 0)) {
1011 continue;
1012 }
1013 print_single_dtc(data[j], data[j+1]);
1014 }
1015}
1016
1017/*
1018 * Get test results for constantly monitored systems
1019 */
1020void do_j1979_cms() {
1021 int rv;
1022 unsigned int i;
1023 struct diag_l3_conn *d_conn;
1024 struct diag_msg *msg;
1025
1026 d_conn = global_l3_conn;
1027
1028 fprintf(stderr, "Requesting Mode 7 (Current cycle emission DTCs)...\n");
1029 rv = l3_do_j1979_rqst(d_conn, 0x07, 0x00, 0x00,
1030 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
1031 if (rv == DIAG_ERR_TIMEOUT) {
1032 /* Didn't get a response, this is valid if there are no DTCs */
1033 fprintf(stderr, "No DTCs stored.\n");
1034 return;
1035 }
1036 if (rv != 0) {
1037 fprintf(stderr, "Failed to get test results for continuously monitored systems\n");
1038 return;
1039 }
1040
1041 fprintf(stderr, "Currently monitored DTCs: ");
1042
1043 for (i=0; i<ecu_count; i++) {
1044 LL_FOREACH(ecu_info[i].rxmsg, msg) {
1045 print_dtcs(msg->data, msg->len);
1046 }
1047
1048 }
1049
1050 fprintf(stderr, "\n");
1051 return;
1052}
1053
1054
1055/*
1056 * Get test results for non-constantly monitored systems
1057 */
1058void do_j1979_ncms(int printall) {
1059 int rv;
1060 struct diag_l3_conn *d_conn;
1061 unsigned int i, j;
1062// int supported=0; //not used ?
1063 ecu_data *ep;
1064
1065 uint8_t merged_mode6_info[0x100];
1066
1067 d_conn = global_l3_conn;
1068
1069 /* Merge all ECU mode6 info into one place*/
1070 memset(merged_mode6_info, 0, sizeof(merged_mode6_info));
1071 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
1072 for (j=0; j<sizeof(ep->mode6_info); j++) {
1073 merged_mode6_info[j] |= ep->mode6_info[j];
1074 }
1075 //if (ep->mode6_info[0] != 0) //XXX not sure what this accomplished
1076 //supported = 1; //this never gets used ...
1077 }
1078
1079 if (merged_mode6_info[0] == 0x00) {
1080 /* Either not supported, or tests havent been done */
1081 fprintf(stderr, "ECU doesn't support non-continuously monitored system tests\n");
1082 return;
1083 }
1084
1085 /*
1086 * Now do the tests
1087 */
1088 for (i=0 ; i < 60; i++) {
1089 if ((merged_mode6_info[i]) && ((i & 0x1f) != 0)) {
1090 /* Do test */
1091 fprintf(stderr, "Requesting Mode 6 TestID 0x%02X...\n", i);
1092 rv = l3_do_j1979_rqst(d_conn, 6, (uint8_t)i, 0x00,
1093 0x00, 0x00, 0x00, 0x00,
1094 (void *)(printall? &_RQST_HANDLE_NCMS:&_RQST_HANDLE_NCMS2));
1095 if (rv < 0) {
1096 fprintf(stderr,
1097 "Mode 6 Test ID 0x%02X failed\n", i);
1098 }
1099 }
1100 }
1101 return;
1102}
1103
1104/*
1105 * Get mode info
1106 * response_offset : index into received packet where the the supported_pid bytemasks start.
1107 * TODO: add return value to signal total failure (if l3_do_j1979_rqst lost the connection to
1108 * the ECU)
1109 */
1110void do_j1979_getmodeinfo(uint8_t mode, int response_offset) {
1111 int rv;
1112 struct diag_l3_conn *d_conn;
1113 int pid;
1114 unsigned int i, j;
1115 ecu_data *ep;
1116 int not_done;
1117 uint8_t *data;
1118
1119 d_conn = global_l3_conn;
1120
1121 /*
1122 * Test 0, 0x20, 0x40, 0x60 (etc) for each mode returns information
1123 * as to which tests are supported. Test 0 will return a bitmask 4
1124 * bytes long showing which of the tests 0->0x1f are supported. Test
1125 * 0x20 will show 0x20->0x3f etc
1126 */
1127 for (pid = 0; pid < 0x100; pid += 0x20) {
1128 /*
1129 * Do Mode 'mode' Pid 'pid' request to find out
1130 * what is supported
1131 */
1132 fprintf(stderr, "Exploring Mode 0x%02X supported PIDs (block 0x%02X)...\n", mode, pid);
1133 rv = l3_do_j1979_rqst(d_conn, mode, (uint8_t) pid, 0x00,
1134 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
1135 if (rv != 0) {
1136 /* No response */
1137 break;
1138 }
1139
1140 /* Process the results */
1141 for (j=0, ep=ecu_info, not_done = 0; j<ecu_count; j++, ep++) {
1142 if (ep->rxmsg == NULL) {
1143 continue;
1144 }
1145 if (ep->rxmsg->data[0] != (mode + 0x40)) {
1146 continue;
1147 }
1148
1149 /* Valid response for this request */
1150
1151 /* Sort out where to store the received data */
1152 switch (mode) {
1153 case 1:
1154 data = ep->mode1_info;
1155 break;
1156 case 2:
1157 data = ep->mode2_info;
1158 break;
1159 case 5:
1160 data = ep->mode5_info;
1161 break;
1162 case 6:
1163 data = ep->mode6_info;
1164 break;
1165 case 8:
1166 data = ep->mode8_info;
1167 break;
1168 case 9:
1169 data = ep->mode9_info;
1170 break;
1171 default:
1172 data = NULL;
1173 break;
1174 }
1175
1176 if (data == NULL) {
1177 break;
1178 }
1179
1180 data[0] = 1; /* Pid 0, 0x20, 0x40 always supported */
1181 for (i=0 ; i<=0x20; i++) {
1182 if (l2_check_pid_bits(
1183 &ep->rxmsg->data[response_offset],
1184 (int)i)) {
1185 data[i + pid] = 1;
1186 }
1187 }
1188 if (data[0x20 + pid] == 1) {
1189 not_done = 1;
1190 }
1191 }
1192
1193 /* Now, check if all ECUs said the next pid isnt supported */
1194 if (not_done == 0) {
1195 break;
1196 }
1197 } //for
1198 return;
1199}
1200
1201
1202/*
1203 * Get the supported PIDs and Tests (Mode 1, 2, 5, 6, 9)
1204 *
1205 * This doesnt get the data for those pids, just the info as to
1206 * what the ECU supports
1207 */
1208void do_j1979_getpids() {
1209 ecu_data *ep;
1210 unsigned int i, j;
1211
1212 do_j1979_getmodeinfo(1, 2);
1213 do_j1979_getmodeinfo(2, 3);
1214 do_j1979_getmodeinfo(5, 3);
1215 do_j1979_getmodeinfo(6, 3);
1216 do_j1979_getmodeinfo(8, 2);
1217 do_j1979_getmodeinfo(9, 3);
1218
1219 /*
1220 * Combine all the supported Mode1 PIDS
1221 * from the ECUs into one bitmask, do same
1222 * for Mode5
1223 */
1224 memset(merged_mode1_info, 0, sizeof(merged_mode1_info));
1225 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
1226 for (j=0; j<sizeof(ep->mode1_info); j++) {
1227 merged_mode1_info[j] |= ep->mode1_info[j];
1228 }
1229 }
1230
1231 memset(merged_mode5_info, 0, sizeof(merged_mode5_info));
1232 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
1233 for (j=0; j<sizeof(ep->mode5_info); j++) {
1234 merged_mode5_info[j] |= ep->mode5_info[j];
1235 }
1236 }
1237 return;
1238}
1239
1240/*
1241 * Do the O2 tests for this O2 sensor
1242 */
1243void do_j1979_O2tests() {
1244 int i;
1245
1246 if (merged_mode5_info[0] == 0) {
1247 fprintf(stderr, "Oxygen (O2) sensor tests not supported\n");
1248 return;
1249 }
1250
1251 for (i=0; i<=7; i++) {
1252 if (global_O2_sensors & (1 << i)) {
1253 do_j1979_getO2tests(i);
1254 }
1255 }
1256 return;
1257}
1258
1259
1260/*
1261 * Do O2 tests for O2Sensor
1262 *
1263 * O2sensor is the bit number
1264 */
1265void do_j1979_getO2tests(int O2sensor) {
1266
1267 int rv;
1268 struct diag_l3_conn *d_conn;
1269 int i;
1270
1271 uint8_t o2s = 1<<O2sensor;
1272
1273 d_conn = global_l3_conn;
1274
1275 for (i=1 ; i<=0x1f; i++) {
1276 fprintf(stderr, "O2 Sensor %d Tests: -\n", O2sensor);
1277 if ((merged_mode5_info[i]) && ((i & 0x1f) != 0)) {
1278 /* Do test for of i + testID */
1279 fprintf(stderr, "Requesting Mode 0x05 TestID 0x%02X...\n", i);
1280 rv = l3_do_j1979_rqst(d_conn, 5, (uint8_t) i, o2s,
1281 0x00, 0x00, 0x00, 0x00,
1282 (void *) &_RQST_HANDLE_O2S);
1283 if ((rv < 0) || (find_ecu_msg(0, 0x45)==NULL)) {
1284 fprintf(stderr, "Mode 5 Test ID 0x%d failed\n", i);
1285 }
1286 /* Receive routine will have printed results */
1287 }
1288 }
1289}
1290
1291/*
1292 * Get current DTCs/MIL lamp status/Tests supported for this ECU
1293 * and test, and wait for those tests to complete
1294 */
1295int do_j1979_getdtcs() {
1296 int rv;
1297 struct diag_l3_conn *d_conn;
1298 ecu_data *ep;
1299 unsigned int i;
1300 int num_dtcs, readiness, mil;
1301
1302 d_conn = global_l3_conn;
1303
1304 if (merged_mode1_info[1] == 0) {
1305 fprintf(stderr, "ECU(s) do not support DTC#/test query - can't do tests\n");
1306 return 0;
1307 }
1308
1309 fprintf(stderr, "Requesting Mode 0x01 PID 0x01 (Current DTCs)...\n");
1310 rv = l3_do_j1979_rqst(d_conn, 1, 1, 0,
1311 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
1312
1313 if ((rv < 0) || (find_ecu_msg(0, 0x41)==NULL)) {
1314 fprintf(stderr, "Mode 1 Pid 1 request failed %d\n", rv);
1315 return -1;
1316 }
1317
1318 /* Go thru the received messages, and see readiness/MIL light */
1319 mil = 0; readiness = 0, num_dtcs = 0;
1320
1321 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
1322 if ((ep->rxmsg) && (ep->rxmsg->data[0] == 0x41)) {
1323 /* Go thru received msgs looking for DTC responses */
1324 if ((ep->mode1_data[1].data[3] & 0xf0) ||
1325 ep->mode1_data[1].data[5]) {
1326 readiness = 1;
1327 }
1328
1329 if (ep->mode1_data[1].data[2] & 0x80) {
1330 mil = 1;
1331 }
1332
1333 num_dtcs += ep->mode1_data[1].data[2] & 0x7f;
1334 }
1335
1336 }
1337 if (readiness == 1) {
1338 fprintf(stderr, "Not all readiness tests have completed\n");
1339 }
1340
1341 if (mil == 1) {
1342 fprintf(stderr, "MIL light ON, ");
1343 } else {
1344 fprintf(stderr, "MIL light OFF, ");
1345 }
1346
1347 fprintf(stderr, "%d stored DTC%c\n", num_dtcs, (num_dtcs==1)?' ':'s');
1348
1349 if (num_dtcs) {
1350 /*
1351 * Do Mode3 command to get DTCs
1352 */
1353
1354 fprintf(stderr, "Requesting Mode 0x03 (Emission DTCs)...\n");
1355 rv = l3_do_j1979_rqst(d_conn, 3, 0x00, 0x00,
1356 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
1357 if ((rv < 0) || (find_ecu_msg(0, 0x43)==NULL)) {
1358 fprintf(stderr, "ECU would not return DTCs\n");
1359 return -1;
1360 }
1361
1362 /* Go thru received msgs looking for DTC responses */
1363 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
1364 if ((ep->rxmsg) && (ep->rxmsg->data[0] == 0x43)) {
1365 struct diag_msg *msg;
1366 LL_FOREACH(ep->rxmsg, msg) {
1367 print_dtcs(msg->data, msg->len);
1368 }
1369 fprintf(stderr, "\n");
1370 }
1371 }
1372 }
1373 return 0;
1374}
1375
1376/*
1377 * Get supported DTCS
1378 */
1379int do_j1979_getO2sensors() {
1380 int rv;
1381 struct diag_l3_conn *d_conn;
1382 unsigned int i, j;
1383 int num_sensors;
1384 ecu_data *ep;
1385
1386 d_conn = global_l3_conn;
1387
1388 global_O2_sensors = 0;
1389 num_sensors = 0;
1390
1391 fprintf(stderr, "Requesting Mode 0x01 PID 0x13 (O2 sensors location)...\n");
1392 rv = l3_do_j1979_rqst(d_conn, 1, 0x13, 0,
1393 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
1394
1395 if ((rv < 0) || (find_ecu_msg(0, 0x41)==NULL)) {
1396 fprintf(stderr, "Mode 1 Pid 0x13 request failed %d\n", rv);
1397 return 0;
1398 }
1399
1400 for (i=0, ep=ecu_info; i<ecu_count; i++, ep++) {
1401 if ((ep->rxmsg) && (ep->rxmsg->data[0] == 0x41)) {
1402 /* Maintain bitmap of sensors */
1403 global_O2_sensors |= ep->rxmsg->data[2];
1404 /* And count additional sensors on this ECU */
1405 for (j=0; j<=7; j++) {
1406 if (ep->rxmsg->data[2] & (1 << j)) {
1407 num_sensors++;
1408 }
1409 }
1410 }
1411 }
1412
1413 fprintf(stderr, "%d Oxygen (O2) sensors in vehicle\n", num_sensors);
1414
1415 return 0;
1416}
1417
1418int diag_cleardtc(void) {
1419 /* Clear DTCs */
1420 struct diag_l3_conn *d_conn;
1421 int rv;
1422 struct diag_msg *rxmsg;
1423
1424 d_conn = global_l3_conn;
1425 fprintf(stderr, "Requesting Mode 0x04 (Clear DTCs)...\n");
1426 rv = l3_do_j1979_rqst(d_conn, 0x04, 0x00, 0x00,
1427 0x00, 0x00, 0x00, 0x00, (void *)&_RQST_HANDLE_NORMAL);
1428
1429 rxmsg = find_ecu_msg(0, 0x44);
1430
1431 if (rxmsg == NULL) {
1432 fprintf(stderr, "ClearDTC requested failed - no appropriate response\n");
1433 return -1;
1434 }
1435
1436 return rv;
1437}
1438
1439typedef int (start_fn)(int);
1440
1441struct protocol {
1442 const char *desc;
1443 start_fn *start;
1444 int flags;
1445};
1446
1447const struct protocol protocols[] = {
1448 {"SAEJ1850-VPW", do_l2_j1850_start, DIAG_L1_J1850_VPW},
1449 {"SAEJ1850-PWM", do_l2_j1850_start, DIAG_L1_J1850_PWM},
1450 {"ISO14230_FAST", do_l2_14230_start, DIAG_L2_TYPE_FASTINIT},
1451 {"ISO9141", do_l2_9141_start, 0x33},
1452 {"ISO14230_SLOW", do_l2_14230_start, DIAG_L2_TYPE_SLOWINIT},
1453};
1454
1455/*
1456 * Connect to ECU by trying all protocols
1457 * - We do the fast initialising protocols before the slow ones
1458 * This will set global_l3_conn. Ret 0 if ok
1459 */
1460int ecu_connect(void) {
1461 int rv = DIAG_ERR_GENERAL;
1462 const struct protocol *p;
1463
1464 if ((global_state >= STATE_CONNECTED) || (global_l3_conn != NULL)) {
1465 printf("ecu_connect() : already connected !\n");
1466 return DIAG_ERR_GENERAL;
1467 }
1468
1469 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
1470 struct diag_l3_conn *d_l3_conn;
1471
1472 fprintf(stderr, "\nTrying %s:\n", p->desc);
1473 rv = p->start(p->flags);
1474 if (rv != 0) {
1475 fprintf(stderr, "%s Failed!\n", p->desc);
1476 continue;
1477 }
1478
1479 // At this point we have a valid L2 connection, but it is possible that
1480 // no communication has been established with an ECU yet (see
1481 // DIAG_L2_FLAG_CONNECTS_ALWAYS) To confirm we really have a connection we
1482 // try to start the J1979 L3 layer and try sending a J1979 keep-alive
1483 // request (service 1 pid 0). diag_l3_start() does exactly that.
1484 fprintf(stderr, "L2 connection OK; trying to add SAE J1979 layer...\n");
1485
1486 d_l3_conn = diag_l3_start("SAEJ1979", global_l2_conn);
1487 if (d_l3_conn == NULL) {
1488 rv = DIAG_ERR_ECUSAIDNO;
1489 fprintf(stderr, "Failed to enable SAEJ1979 mode\n");
1490 // So we'll try another protocol. But close that L2 first:
1491 diag_l2_StopCommunications(global_l2_conn);
1492 diag_l2_close(global_dl0d);
1493
1494 global_l2_conn = NULL;
1495 global_state = STATE_IDLE;
1496 continue;
1497 }
1498 global_l3_conn = d_l3_conn;
1499 global_state = STATE_L3ADDED;
1500
1501 fprintf(stderr, "%s Connected.\n", p->desc);
1502 break;
1503 }
1504
1505 if (diag_cli_debug) {
1506 fprintf(stderr, "debug: L2 connection ID %p, L3 ID %p\n",
1507 (void *)global_l2_conn, (void *)global_l3_conn);
1508 }
1509
1510 return rv ? diag_iseterr(rv) : 0;
1511}
1512
1513
1514/*
1515 * Initialise. ret 0 if ok
1516 */
1517static int do_init(void) {
1518 clear_data();
1519
1520 if (diag_init()) {
1521 fprintf(stderr, "diag_init() failed.\n");
1522 diag_end();
1523 return DIAG_ERR_GENERAL;
1524 }
1525
1526 return 0;
1527}
1528
1529/*
1530 * Explain command line usage
1531 */
1532static void do_usage (void) {
1533 fprintf( stderr, "FreeDiag ScanTool:\n\n" );
1534 fprintf( stderr, " Usage -\n" );
1535 fprintf( stderr, " freediag [-h][-a|-c][-f <file]\n");
1536 fprintf( stderr, " Where:\n" );
1537 fprintf( stderr, "\t-h -- Display this help message\n" );
1538 fprintf( stderr, "\t-c -- Start in command-line interface mode\n" );
1539 fprintf( stderr, "\t (this is the default)\n");
1540 fprintf( stderr, "\t-f <file> Runs the commands from <file> at startup\n");
1541 fprintf( stderr, "\n" );
1542}
1543
1544
1545
1546static void format_o2(char *buf, int maxlen, UNUSED(int english),
1547 const struct pid *p, response *data, int n) {
1548 double v = DATA_SCALED(p, DATA_1(p, n, data));
1549 int t = DATA_1(p, n + 1, data);
1550
1551 if (t == 0xff) {
1552 snprintf(buf, maxlen, p->fmt1, v);
1553 } else {
1554 snprintf(buf, maxlen, p->fmt2, v,
1555 t * p->scale2 + p->offset2);
1556 }
1557}
1558
1559
1560static void format_aux(char *buf, int maxlen, UNUSED(int english), const struct pid *p,
1561 response *data, int n) {
1562 snprintf(buf, maxlen, (DATA_RAW(p, n, data) & 1) ? "PTO Active" : "----");
1563}
1564
1565
1566
1567static void format_fuel(char *buf, int maxlen, UNUSED(int english), const struct pid *p,
1568 response *data, int n) {
1569 int s = DATA_1(p, n, data);
1570
1571 switch (s) {
1572 case 1 << 0:
1573 snprintf(buf, maxlen, "Open");
1574 break;
1575 case 1 << 1:
1576 snprintf(buf, maxlen, "Closed");
1577 break;
1578 case 1 << 2:
1579 snprintf(buf, maxlen, "Open-Driving");
1580 break;
1581 case 1 << 3:
1582 snprintf(buf, maxlen, "Open-Fault");
1583 break;
1584 case 1 << 4:
1585 snprintf(buf, maxlen, "Closed-Fault");
1586 break;
1587 default:
1588 snprintf(buf, maxlen, "Open(rsvd)");
1589 break;
1590 }
1591
1592 /* XXX Fuel system 2 status */
1593}
1594
1595
1596static void format_data(char *buf, int maxlen, int english, const struct pid *p, response *data, int n) {
1597 double v;
1598
1599 v = DATA_SCALED(p, DATA_RAW(p, n, data));
1600 if (english && p->fmt2) {
1601 snprintf(buf, maxlen, p->fmt2, DATA_ENGLISH(p, v));
1602 } else {
1603 snprintf(buf, maxlen, p->fmt1, v);
1604 }
1605}
1606
1607
1608/* conversion factors from the "units" package */
1609
1610static const struct pid pids[] = {
1611 {0x03, "Fuel System Status", format_fuel, 2,
1612 "", 0.0, 0.0,
1613 "", 0.0, 0.0},
1614 {0x04, "Calculated Load Value", format_data, 1,
1615 "%5.1f%%", (100.0/255), 0.0,
1616 "", 0.0, 0.0},
1617 {0x05, "Engine Coolant Temperature", format_data, 1,
1618 "%3.0fC", 1, -40,
1619 "%3.0fF", 1.8, 32},
1620 {0x06, "Short term fuel trim Bank 1", format_data, 1,
1621 "%5.1f%%", (100.0/128), -100,
1622 "", 0.0, 0.0},
1623 {0x07, "Long term fuel trim Bank 1", format_data, 1,
1624 "%5.1f%%", (100.0/128), -100,
1625 "", 0.0, 0.0},
1626 {0x08, "Short term fuel trim Bank 2", format_data, 1,
1627 "%5.1f%%", (100.0/128), -100,
1628 "", 0.0, 0.0},
1629 {0x09, "Long term fuel trim Bank 2", format_data, 1,
1630 "%5.1f%%", (100.0/128), -100,
1631 "", 0.0, 0.0},
1632 {0x0a, "Fuel Pressure", format_data, 1,
1633 "%3.0fkPaG", 3.0, 0.0,
1634 "%4.1fpsig", 0.14503774, 0.0},
1635 {0x0b, "Intake Manifold Pressure", format_data, 1,
1636 "%3.0fkPaA", 1.0, 0.0,
1637 "%4.1finHg", 0.29529983, 0.0},
1638 {0x0c, "Engine RPM", format_data, 2,
1639 "%5.0fRPM", 0.25, 0.0,
1640 "", 0.0, 0.0},
1641 {0x0d, "Vehicle Speed", format_data, 1,
1642 "%3.0fkm/h", 1.0, 0.0,
1643 "%3.0fmph", 0.62137119, 0.0},
1644 {0x0e, "Ignition timing advance Cyl #1", format_data, 1,
1645 "%4.1f deg", 0.5, -64.0,
1646 "", 0.0, 0.0},
1647 {0x0f, "Intake Air Temperature", format_data, 1,
1648 "%3.0fC", 1.0, -40.0,
1649 "%3.0fF", 1.8, 32.0},
1650 {0x10, "Air Flow Rate", format_data, 2,
1651 "%6.2fgm/s", 0.01, 0.0,
1652 "%6.1flb/min", 0.13227736, 0.0},
1653 {0x11, "Absolute Throttle Position", format_data, 1,
1654 "%5.1f%%", (100.0/255), 0.0,
1655 "", 0.0, 0.0},
1656 {0x12, "Commanded Secondary Air Status", format_data, 1,
1657 "", 0, 0,
1658 "", 0, 0}, //can't format bit fields
1659 {0x13, "Location of Oxygen Sensors", format_data, 1,
1660 "", 0, 0,
1661 "", 0, 0}, //can't format bit fields
1662 {0x14, "Bank 1 Sensor 1 Voltage/Trim", format_o2, 2,
1663 "%5.3fV", 0.005, 0.0,
1664 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1665 {0x15, "Bank 1 Sensor 2 Voltage/Trim", format_o2, 2,
1666 "%5.3fV", 0.005, 0.0,
1667 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1668 {0x16, "Bank 1 Sensor 3 Voltage/Trim", format_o2, 2,
1669 "%5.3fV", 0.005, 0.0,
1670 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1671 {0x17, "Bank 1 Sensor 4 Voltage/Trim", format_o2, 2,
1672 "%5.3fV", 0.005, 0.0,
1673 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1674 {0x18, "Bank 2 Sensor 1 Voltage/Trim", format_o2, 2,
1675 "%5.3fV", 0.005, 0.0,
1676 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1677 {0x19, "Bank 2 Sensor 2 Voltage/Trim", format_o2, 2,
1678 "%5.3fV", 0.005, 0.0,
1679 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1680 {0x1a, "Bank 2 Sensor 3 Voltage/Trim", format_o2, 2,
1681 "%5.3fV", 0.005, 0.0,
1682 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1683 {0x1b, "Bank 2 Sensor 4 Voltage/Trim", format_o2, 2,
1684 "%5.3fV", 0.005, 0.0,
1685 "%5.3fV/%5.1f%%", (100.0/128), -100.0},
1686 {0x1e, "Auxiliary Input Status", format_aux, 1,
1687 "", 0.0, 0.0,
1688 "", 0.0, 0.0},
1689};
1690
1691
1692const struct pid *get_pid ( unsigned int i ) {
1693 if (i >= ARRAY_SIZE(pids)) {
1694 return NULL;
1695 }
1696
1697 return &pids[i];
1698}
1699
1700
1701/*
1702 * Main
1703 */
1704
1705int main(int argc, char **argv) {
1706 int i;
1707 char *startfile=NULL; /* optional commands to run at startup */
1708
1709 for ( i = 1 ; i < argc ; i++ ) {
1710 if ( argv[i][0] == '-' || argv[i][0] == '+' ) {
1711 switch ( argv[i][1] ) {
1712 case 'c': break;
1713 case 'f':
1714 i++;
1715 if (i < argc) {
1716 startfile = argv[i];
1717 } else {
1718 do_usage();
1719 exit(1);
1720 }
1721 break;
1722 case 'h': do_usage(); exit(0 );
1723 default: do_usage(); exit(1);
1724 }
1725 } else {
1726 do_usage();
1727 exit(1);
1728 }
1729 }
1730
1731 if (do_init()) {
1732 exit(1);
1733 }
1734
1735 printf("\n**************** %s version %s ****************\n", PROJECT_NAME, PACKAGE_VERSION);
1736 printf("%s: Type HELP for a list of commands\n", SCANTOOL_PROGNAME);
1737 printf("%s: Type SCAN to start ODBII Scan\n", SCANTOOL_PROGNAME);
1738 printf("%s: Then use MONITOR to monitor real-time data\n", SCANTOOL_PROGNAME);
1739 printf("%s: **** IMPORTANT : this is beta software ! Use at your own risk.\n", SCANTOOL_PROGNAME);
1740 printf("%s: **** Remember, \"debug all -1\" displays all debugging info.\n", SCANTOOL_PROGNAME);
1741
1742 progname = SCANTOOL_PROGNAME;
1743 set_init();
1744
1745 bool using_rcfile = 0;
1746 if (!startfile) {
1747 // no file explicitly specified : try .rcfile or .ini
1748 startfile = find_rcfile();
1749 if (startfile) {
1750 using_rcfile = 1;
1751 }
1752 }
1753 scantool_cli(SCANTOOL_PROGNAME, startfile, scantool_cmd_table);
1754
1755 if (using_rcfile) {
1756 free(startfile);
1757 }
1758
1759 set_close();
1760 int rv=diag_end();
1761 if (rv) {
1762 fprintf(stderr, FLFMT "diag_end failed !?\n", FL);
1763 }
1764
1765 /* Done */
1766 exit(0);
1767}
1768