Spray a bunch of notes and code into the implementation file.
[apps/low-level-can-service.git] / src / obd2 / obd2.c
1 #include <obd2/obd2.h>
2
3 DiagnosticShims diagnostic_init_shims(LogShim log,
4         SendCanMessageShim send_can_message,
5         SetTimerShim set_timer) {
6     DiagnosticShims shims = {
7         isotp_shims: {
8             log: log,
9             send_can_message: send_can_message,
10             set_timer: set_timer
11         },
12         log: log
13     };
14     return shims;
15 }
16
17 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
18         DiagnosticRequest* request, DiagnosticResponseReceived callback) {
19     // TODO hmm, where is message_received coming from? we would need 2 layers
20     // of callbacks. if we do it in the obd2 library, we have to have some
21     // context passed to that message_received handler so we can then retreive
22     // the obd2 callback. there was an option question of if we should pass a
23     // context with that callback, and maybe this answers it.
24     //
25     // alternatively, what if don't hide isotp and allow that to also be
26     // injected. the user has the iso_tp message_received callback, and in that
27     // they call a message_received handler from obd2.
28     //
29     // in fact that makes more sense - all the diagnostic_can_frame_received
30     // function is going to be able to do is call the equivalent function in the
31     // isotp library. it may or may not have a complete ISO-TP message. huh.
32     DiagnosticRequestHandle handle = {
33         // TODO why are teh shims stored as a reference in the isotp handler?
34         // it's just 3 pointers
35         isotp_handler: isotp_init(&shims->isotp_shims, request->arbitration_id,
36                NULL, // TODO need a callback here!
37                NULL, NULL),
38         type: 0 //DIAGNOSTIC_.... // TODO how would we know the type?
39             //does it matter? we were going to have a different callback
40     };
41 }
42
43 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
44         DiagnosticPidRequestType pid_request_type, uint16_t pid,
45         DiagnosticResponseReceived callback) {
46     // decide mode 0x1 / 0x22 based on pid type
47     DiagnosticRequest request = {
48         mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
49         pid: pid
50     };
51
52     return diagnostic_request(shims, &request, callback);
53 }
54
55 void diagnostic_receive_can_frame(DiagnosticRequestHandle* handler,
56         const uint16_t arbitration_id, const uint8_t data[],
57         const uint8_t size) {
58     isotp_receive_can_frame(handler->isotp_handler, arbitration_id, data, size);
59 }
60
61 // TODO everything below here is for future work...not critical for now.
62
63 DiagnosticRequestHandle diagnostic_request_malfunction_indicator_status(
64         DiagnosticShims* shims,
65         DiagnosticMilStatusReceived callback) {
66     // TODO request malfunction indicator light (MIL) status - request mode 1
67     // pid 1, parse first bit
68 }
69
70 DiagnosticRequestHandle diagnostic_request_vin(DiagnosticShims* shims,
71         DiagnosticVinReceived callback) {
72 }
73
74 DiagnosticRequestHandle diagnostic_request_dtc(DiagnosticShims* shims,
75         DiagnosticTroubleCodeType dtc_type,
76         DiagnosticTroubleCodesReceived callback) {
77 }
78
79 bool diagnostic_clear_dtc(DiagnosticShims* shims) {
80 }
81
82 DiagnosticRequestHandle diagnostic_enumerate_pids(DiagnosticShims* shims,
83         DiagnosticRequest* request, DiagnosticPidEnumerationReceived callback) {
84     // before calling the callback, split up the received bytes into 1 or 2 byte
85     // chunks depending on the mode so the final pid list is actual 1 or 2 byte PIDs
86     // TODO request supported PIDs  - request PID 0 and parse 4 bytes in response
87 }