Leave payload parsing to applications using this library.
[apps/agl-service-can-low-level.git] / src / uds / uds.h
1 #ifndef __UDS_H__
2 #define __UDS_H__
3
4 #include <uds/uds_types.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7
8 #define OBD2_FUNCTIONAL_BROADCAST_ID 0x7df
9 #define OBD2_FUNCTIONAL_RESPONSE_START 0x7e8
10 #define OBD2_FUNCTIONAL_RESPONSE_COUNT 8
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 /* Public: Initialize an DiagnosticShims with the given callback functions.
17  *
18  * If any callbacks are not to be used, set them to NULL. For documentation of
19  * the function type signatures, see higher up in this header file. This struct
20  * is a handy encapsulation used to pass the shims around to the various
21  * diagnostic_* functions.
22  *
23  * Returns a struct with the fields initailized to the callbacks.
24  */
25 DiagnosticShims diagnostic_init_shims(LogShim log,
26         SendCanMessageShim send_can_message,
27         SetTimerShim set_timer);
28
29 /* Public: Initiate a diagnostic request and return a handle, ready to completly
30  * send the request and process the response via
31  * diagnostic_receive_can_frame(...).
32  *
33  * shims -  Low-level shims required to send CAN messages, etc.
34  * request -
35  * callback - an optional function to be called when the response is receved
36  *      (use NULL if no callback is required).
37  *
38  * Returns a handle to be used with diagnostic_receive_can_frame to complete
39  * sending the request and receive the response. The 'completed' field in the
40  * returned DiagnosticRequestHandle will be true when the message is completely
41  * sent.
42  */
43 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
44         DiagnosticRequest* request, DiagnosticResponseReceived callback);
45
46 /* Public: Request a PID from the given arbitration ID, determining the mode
47  * automatically based on the PID type.
48  *
49  * shims -  Low-level shims required to send CAN messages, etc.
50  * pid_request_type - either DIAGNOSTIC_STANDARD_PID (will use mode 0x1 and 1
51  *      byte PIDs) or DIAGNOSTIC_ENHANCED_PID (will use mode 0x22 and 2 byte
52  *      PIDs)
53  * arbitration_id - The arbitration ID to send the request to.
54  * pid - The PID to request from the other node.
55  * callback - an optional function to be called when the response is receved
56  *      (use NULL if no callback is required).
57  *
58  * Returns a handle to be used with diagnostic_receive_can_frame to complete
59  * sending the request and receive the response. The 'completed' field in the
60  * returned DiagnosticRequestHandle will be true when the message is completely
61  * sent.
62  */
63 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
64         DiagnosticPidRequestType pid_request_type, uint16_t arbitration_id,
65         uint16_t pid, DiagnosticResponseReceived callback);
66
67 /* Public: Continue to send and receive a single diagnostic request, based on a
68  * freshly received CAN message.
69  *
70  * shims -  Low-level shims required to send CAN messages, etc.
71  * handle - A DiagnosticRequestHandle previously returned by one of the
72  *      diagnostic_request*(..) functions.
73  * arbitration_id - The arbitration_id of the received CAN message.
74  * data - The data of the received CAN message.
75  * size - The size of the data in the received CAN message.
76  *
77  * Returns true if the request was completed and response received, or the
78  * request was otherwise cancelled. Check the 'success' field of the handle to
79  * see if it was successful.
80  */
81 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
82         DiagnosticRequestHandle* handle,
83         const uint16_t arbitration_id, const uint8_t data[],
84         const uint8_t size);
85
86 /* Public: Parse the entier payload of the reponse as a single integer.
87  *
88  * response - the received DiagnosticResponse.
89  */
90 int diagnostic_payload_to_integer(const DiagnosticResponse* response);
91
92 /* Public: Render a DiagnosticResponse as a string into the given buffer.
93  *
94  * response - the response to convert to a string, for debug logging.
95  * destination - the target string buffer.
96  * destination_length - the size of the destination buffer, i.e. the max size
97  *      for the rendered string.
98  */
99 void diagnostic_response_to_string(const DiagnosticResponse* response,
100         char* destination, size_t destination_length);
101
102 /* Public: Render a DiagnosticRequest as a string into the given buffer.
103  *
104  * request - the request to convert to a string, for debug logging.
105  * destination - the target string buffer.
106  * destination_length - the size of the destination buffer, i.e. the max size
107  *      for the rendered string.
108  */
109 void diagnostic_request_to_string(const DiagnosticRequest* request,
110         char* destination, size_t destination_length);
111
112 /* Public: For many OBD-II PIDs with a numerical result, translate a diagnostic
113  * response payload into a meaningful number using the standard formulas.
114  *
115  * Functions pulled from http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01
116  *
117  * Returns the translated value or 0 if the PID is not in the OBD-II standard or
118  * does not use a numerical value (e.g. VIN).
119  */
120 float diagnostic_decode_obd2_pid(const DiagnosticResponse* response);
121
122 #ifdef __cplusplus
123 }
124 #endif
125
126 #endif // __UDS_H__