-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwp2k.c
155 lines (109 loc) · 3.25 KB
/
kwp2k.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char byte;
long GetCommandResponse32(long Command);
long long GetCommandResponse64(long long Command);
void GetCommandResponse(char* Command, char* buff, int BufferLength);
byte GetByte( unsigned long long Value, int BytePosition, int NumberOfBytes )
{
unsigned long long mask;
int nBits;
nBits = (NumberOfBytes-BytePosition)*8;
mask = 0xffULL << nBits;
return (Value & mask) >> nBits;
}
ushort GetWord( unsigned long long Value, int BytePosition, int NumberOfBytes )
{
unsigned long long mask;
int nBits;
nBits = (NumberOfBytes-(BytePosition+1))*8;
mask = 0xffffULL << nBits;
return (Value & mask) >> nBits;
}
int ECUReset( byte ResetMode )
{
int request, response;
request = (0x11 << 8) + ResetMode;
response = GetCommandResponse32(request);
if ((response >> 8) != 0x51)
return 0;
return 1;
}
int ClearDiagnosticInformation( ushort GroupOfDiagnosticInformation )
{
int command, response;
command = (0x14 << 16) + (GroupOfDiagnosticInformation);
response = GetCommandResponse32(command);
if ((response >> 16) != 0x54)
return 0;
return 1;
}
int ReadDiagnosticTroubleCodesByStatus(
byte StatusOfDTCRequest,
ushort GroupOfDTC,
ushort* ArrayOfDTCs
)
{
int iCommand, statusOfDTCRequest, nBytes, nDTCs;
unsigned long long ullBuffer;
char szCommand[10], buffer[100];
char *start;
int i;
statusOfDTCRequest = StatusOfDTCRequest;
iCommand = (0x18 << 24) + (statusOfDTCRequest << 16) + (GroupOfDTC);
snprintf(szCommand, 10, "%X\r", iCommand);
GetCommandResponse(szCommand, buffer, 100);
nBytes = (strlen(buffer)-1)/2;
i = 0;
start = buffer + 4;
ullBuffer = strtoll(buffer, NULL, 16);
if (GetByte(ullBuffer, 1, nBytes) != 0x58)
return 0;
nDTCs = GetByte(ullBuffer, 2, nBytes);
while (i < nDTCs)
{
ullBuffer = strtoll(start, NULL, 16);
nBytes = (strlen(start)-1)/2;
if (ArrayOfDTCs)
ArrayOfDTCs[i] = GetWord(ullBuffer, 1, nBytes);
start+=6;
i++;
}
return nDTCs; // Number of DTCs
}
int ReadDataByCommonIdentifier32( short RecordCommonIdentifier )
{
int response, command;
command = (0x22 << 16) + (RecordCommonIdentifier);
response = GetCommandResponse32(command);
if ((response & 0xff000000) >> 24 != 0x62)
return 0;
return (response & 0x000000ff);
}
int ReadDataByCommonIdentifier64( short RecordCommonIdentifier )
{
int command;
long long response;
command = (0x22 << 16) + (RecordCommonIdentifier);
response = GetCommandResponse64(command);
if ((response & 0xff00000000) >> 32 != 0x62)
return 0;
return (response & 0xffff);
}
int InputOutputControlByCommonIdentifier(
short InputOutputCommonIdentifier,
byte InputOutputControlParameter,
byte ControlState
)
{
long long response, command;
int id, param;
id = InputOutputCommonIdentifier;
param = InputOutputControlParameter;
command = (0x2FLL << 32) + (id << 16) + (param << 8) + (ControlState);
response = GetCommandResponse64(command);
if ((response & 0xff00000000) >> 32 != 0x6F)
return 0;
return 1;
}