b301138c555d2ed158d067991599af120eb72a5b
[apps/agl-service-can-low-level.git] / src / obd2 / obd2.h
1 #ifndef __OBD2_H__
2 #define __OBD2_H__
3
4 #include <obd2/obd2_types.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7
8 #define OBD2_FUNCTIONAL_BROADCAST_ID 0x7df
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 /* Public: Initialize an DiagnosticShims with the given callback functions.
15  *
16  * If any callbacks are not to be used, set them to NULL. For documentation of
17  * the function type signatures, see higher up in this header file. This struct
18  * is a handy encapsulation used to pass the shims around to the various
19  * diagnostic_* functions.
20  *
21  * Returns a struct with the fields initailized to the callbacks.
22  */
23 DiagnosticShims diagnostic_init_shims(LogShim log,
24         SendCanMessageShim send_can_message,
25         SetTimerShim set_timer);
26
27 /* Public: Initiate a diagnostic request and return a handle, ready to completly
28  * send the request and process the response via
29  * diagnostic_receive_can_frame(...).
30  *
31  * shims -  Low-level shims required to send CAN messages, etc.
32  * request -
33  * callback - an optional function to be called when the response is receved
34  *      (use NULL if no callback is required).
35  *
36  * Returns a handle to be used with diagnostic_receive_can_frame to complete
37  * sending the request and receive the response. The 'completed' field in the
38  * returned DiagnosticRequestHandle will be true when the message is completely
39  * sent.
40  */
41 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
42         DiagnosticRequest* request, DiagnosticResponseReceived callback);
43
44 /* Public: Request a PID from the given arbitration ID, determining the mode
45  * automatically based on the PID type.
46  *
47  * shims -  Low-level shims required to send CAN messages, etc.
48  * pid_request_type - either DIAGNOSTIC_STANDARD_PID (will use mode 0x1 and 1
49  *      byte PIDs) or DIAGNOSTIC_ENHANCED_PID (will use mode 0x22 and 2 byte
50  *      PIDs)
51  * arbitration_id - The arbitration ID to send the request to.
52  * pid - The PID to request from the other node.
53  * callback - an optional function to be called when the response is receved
54  *      (use NULL if no callback is required).
55  *
56  * Returns a handle to be used with diagnostic_receive_can_frame to complete
57  * sending the request and receive the response. The 'completed' field in the
58  * returned DiagnosticRequestHandle will be true when the message is completely
59  * sent.
60  */
61 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
62         DiagnosticPidRequestType pid_request_type, uint16_t arbitration_id,
63         uint16_t pid, DiagnosticResponseReceived callback);
64
65 /* Public: Continue to send and receive a single diagnostic request, based on a
66  * freshly received CAN message.
67  *
68  * shims -  Low-level shims required to send CAN messages, etc.
69  * handle - A DiagnosticRequestHandle previously returned by one of the
70  *      diagnostic_request*(..) functions.
71  * arbitration_id - The arbitration_id of the received CAN message.
72  * data - The data of the received CAN message.
73  * size - The size of the data in the received CAN message.
74  *
75  * Returns true if the request was completed and response received, or the
76  * request was otherwise cancelled. Check the 'success' field of the handle to
77  * see if it was successful.
78  */
79 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
80         DiagnosticRequestHandle* handle,
81         const uint16_t arbitration_id, const uint8_t data[],
82         const uint8_t size);
83
84 /* Public: Render a DiagnosticResponse as a string into the given buffer.
85  *
86  * TODO implement this
87  *
88  * message - the response to convert to a string, for debug logging.
89  * destination - the target string buffer.
90  * destination_length - the size of the destination buffer, i.e. the max size
91  *      for the rendered string.
92  */
93 // void diagnostic_response_to_string(const DiagnosticResponse* response,
94         // char* destination, size_t destination_length);
95
96 #ifdef __cplusplus
97 }
98 #endif
99
100 #endif // __OBD2_H__