BOROSOFTWAREAll work →
scantool/diag_dtc.c 100 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 * DTC handling routines
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30
31#include "diag.h"
32#include "diag_dtc.h"
33
34
35
36//do not use *allocs or open handles in diag_dtc_init !
37void diag_dtc_init(void) {
38}
39
40/** DTC decoding routine.
41 *
42 * Generate description (or error) string for given DTCs.
43 * @param data: Data representing the DTC
44 * @param len: length of *data
45 * @param vehicle: (optional) Vehicle name; unused.
46 * @param ecu: (optional) ECU Name; unused
47 * @param protocol: Protocol (see include file)
48 * @param buf: Buffer to write the output
49 * @param bufsize: size of *buf
50 * @return pointer to *buf, which may be useful to printf or fprintf...
51 */
52
53char *diag_dtc_decode(const uint8_t *data, int len,
54 UNUSED(const char *vehicle), UNUSED(const char *ecu),
55 enum diag_dtc_protocol protocol,
56 char *buf, const size_t bufsize) {
57 char area;
58
59 switch (protocol) {
60 case dtc_proto_j2012:
61 if (len != 2) {
62 strncpy(buf, "DTC too short for J1850 decode\n", bufsize);
63 return buf;
64 }
65
66 switch ((data[0] >> 6) & 0x03) { /* Top 2 bits are area */
67 case 0:
68 area = 'P';
69 break;
70 case 1:
71 area = 'C';
72 break;
73 case 2:
74 area = 'B';
75 break;
76 case 3:
77 area = 'U';
78 break;
79 default:
80 fprintf(stderr, "Illegal data[0] value\n");
81 area = 'X';
82 break;
83 }
84 snprintf(buf, bufsize, "%c%02X%02X ", area, data[0] & 0x3f, data[1]&0xff);
85 break;
86
87 case dtc_proto_int8:
88 case dtc_proto_int16:
89 case dtc_proto_int32:
90 case dtc_proto_text:
91 snprintf(buf, bufsize, "Unimplemented Protocol %d\n", protocol);
92 break;
93
94 default:
95 snprintf(buf, bufsize, "Unknown Protocol %d\n", protocol);
96 break;
97 }
98 return buf;
99}
100