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