8cdd4a1bc95e89335931935fc0d8acda6344fa2a
[apps/agl-service-can-low-level.git] / src / obd2 / obd2.h
1 #ifndef __OBD2_H__
2 #define __OBD2_H__
3
4 #include <isotp/isotp.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 // TODO This isn't true for multi frame messages - we may need to dynamically
13 // allocate this in the future
14 #define MAX_OBD2_PAYLOAD_LENGTH 7
15 #define VIN_LENGTH 17
16
17 typedef enum {
18     DIAGNOSTIC_REQUEST_TYPE_PID,
19     DIAGNOSTIC_REQUEST_TYPE_DTC,
20     DIAGNOSTIC_REQUEST_TYPE_MIL_STATUS,
21     DIAGNOSTIC_REQUEST_TYPE_VIN
22 } DiagnosticRequestType;
23
24 typedef struct {
25     DiagnosticRequestType type;
26     uint16_t arbitration_id;
27     uint8_t mode;
28     uint16_t pid;
29     uint8_t pid_length;
30     uint8_t payload[MAX_OBD2_PAYLOAD_LENGTH];
31     uint8_t payload_length;
32 } DiagnosticRequest;
33
34 // Thanks to
35 // http://www.canbushack.com/blog/index.php?title=scanning-for-diagnostic-data&more=1&c=1&tb=1&pb=1
36 // for the list of NRCs
37 typedef enum {
38     NRC_SUCCESS = 0x0,
39     NRC_SERVICE_NOT_SUPPORTED = 0x11,
40     NRC_SUB_FUNCTION_NOT_SUPPORTED = 0x12,
41     NRC_CONDITIONS_NOT_CORRECT = 0x22,
42     NRC_REQUEST_OUT_OF_RANGE = 0x31,
43     NRC_SECURITY_ACCESS_DENIED = 0x33,
44     NRC_INVALID_KEY = 0x35,
45     NRC_TOO_MANY_ATTEMPS = 0x36,
46     NRC_TIME_DELAY_NOT_EXPIRED = 0x37,
47     NRC_RESPONSE_PENDING = 0x78
48 } DiagnosticNegativeResponseCode;
49
50 typedef enum {
51     OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST = 0x1,
52     OBD2_MODE_POWERTRAIN_FREEZE_FRAME_REQUEST = 0x2,
53     OBD2_MODE_EMISSIONS_DTC_REQUEST = 0x3,
54     OBD2_MODE_EMISSIONS_DTC_CLEAR = 0x4,
55     // 0x5 is for non-CAN only
56     // OBD2_MODE_OXYGEN_SENSOR_TEST = 0x5,
57     OBD2_MODE_TEST_RESULTS = 0x6,
58     OBD2_MODE_DRIVE_CYCLE_DTC_REQUEST = 0x7,
59     OBD2_MODE_CONTROL = 0x8,
60     OBD2_MODE_VEHICLE_INFORMATION = 0x9,
61     OBD2_MODE_PERMANENT_DTC_REQUEST = 0xa,
62     // this one isn't technically in OBD2, but both of the enhanced standards
63     // have their PID requests at 0x22
64     OBD2_MODE_ENHANCED_DIAGNOSTIC_REQUEST = 0x22
65 } DiagnosticMode;
66
67 typedef enum {
68     DTC_EMISSIONS,
69     DTC_DRIVE_CYCLE,
70     DTC_PERMANENT
71 } DiagnosticTroubleCodeType;
72
73 typedef struct {
74     uint16_t arbitration_id;
75     uint8_t mode;
76     bool completed;
77     bool success;
78     uint16_t pid;
79     DiagnosticNegativeResponseCode negative_response_code;
80     uint8_t payload[MAX_OBD2_PAYLOAD_LENGTH];
81     uint8_t payload_length;
82 } DiagnosticResponse;
83
84 typedef enum {
85     POWERTRAIN = 0x0,
86     CHASSIS = 0x1,
87     BODY = 0x2,
88     NETWORK = 0x3
89 } DiagnosticTroubleCodeGroup;
90
91 typedef struct {
92     DiagnosticTroubleCodeGroup group;
93     uint8_t group_num;
94     uint8_t code;
95 } DiagnosticTroubleCode;
96
97 typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
98 typedef void (*DiagnosticMilStatusReceived)(bool malfunction_indicator_status);
99 typedef void (*DiagnosticVinReceived)(uint8_t vin[]);
100 typedef void (*DiagnosticTroubleCodesReceived)(
101         DiagnosticMode mode, DiagnosticTroubleCode* codes);
102 typedef void (*DiagnosticPidEnumerationReceived)(
103         const DiagnosticResponse* response, uint16_t* pids);
104
105 // TODO should we enumerate every OBD-II PID? need conversion formulas, too
106 typedef struct {
107     uint16_t pid;
108     uint8_t bytes_returned;
109     float min_value;
110     float max_value;
111 } DiagnosticParameter;
112
113 typedef struct {
114     DiagnosticRequest request;
115     bool success;
116     bool completed;
117
118     IsoTpShims isotp_shims;
119     IsoTpSendHandle isotp_send_handle;
120     IsoTpReceiveHandle isotp_receive_handle;
121     DiagnosticResponseReceived callback;
122     DiagnosticMilStatusReceived mil_status_callback;
123     DiagnosticVinReceived vin_callback;
124 } DiagnosticRequestHandle;
125
126 typedef enum {
127     DIAGNOSTIC_STANDARD_PID,
128     DIAGNOSTIC_ENHANCED_PID
129 } DiagnosticPidRequestType;
130
131 typedef struct {
132     LogShim log;
133     SendCanMessageShim send_can_message;
134     SetTimerShim set_timer;
135 } DiagnosticShims;
136
137 DiagnosticShims diagnostic_init_shims(LogShim log,
138         SendCanMessageShim send_can_message,
139         SetTimerShim set_timer);
140
141 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
142         DiagnosticRequest* request, DiagnosticResponseReceived callback);
143
144 // decide mode 0x1 / 0x22 based on pid type
145 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
146         DiagnosticPidRequestType pid_request_type, uint16_t arbitration_id,
147         uint16_t pid, DiagnosticResponseReceived callback);
148
149 DiagnosticRequestHandle diagnostic_request_malfunction_indicator_status(
150         DiagnosticShims* shims,
151         DiagnosticMilStatusReceived callback);
152
153 DiagnosticRequestHandle diagnostic_request_vin(DiagnosticShims* shims,
154         DiagnosticVinReceived callback);
155
156 DiagnosticRequestHandle diagnostic_request_dtc(DiagnosticShims* shims,
157         DiagnosticTroubleCodeType dtc_type,
158         DiagnosticTroubleCodesReceived callback);
159
160 bool diagnostic_clear_dtc(DiagnosticShims* shims);
161
162 DiagnosticRequestHandle diagnostic_enumerate_pids(DiagnosticShims* shims,
163         DiagnosticRequest* request, DiagnosticPidEnumerationReceived callback);
164
165 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
166         DiagnosticRequestHandle* handle,
167         const uint16_t arbitration_id, const uint8_t data[],
168         const uint8_t size);
169
170 #ifdef __cplusplus
171 }
172 #endif
173
174 #endif // __OBD2_H__