Rename from simply obd2 to more general UDS (unified diagnostics).
[apps/low-level-can-service.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: Render a DiagnosticResponse as a string into the given buffer.
87  *
88  * TODO implement this
89  *
90  * message - the response to convert to a string, for debug logging.
91  * destination - the target string buffer.
92  * destination_length - the size of the destination buffer, i.e. the max size
93  *      for the rendered string.
94  */
95 // void diagnostic_response_to_string(const DiagnosticResponse* response,
96         // char* destination, size_t destination_length);
97
98 #ifdef __cplusplus
99 }
100 #endif
101
102 #endif // __UDS_H__