Change way to find signals to make it a little bit more generic.
[apps/agl-service-can-low-level.git] / src / obd2-signals.hpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #pragma once
19
20 #include <vector>
21
22 #include "uds/uds.h"
23 #include "can-bus.hpp"
24
25 #include "low-can-binding.hpp"
26
27 enum UNIT {
28         POURCENT,
29         DEGREES_CELSIUS,
30         KPA,
31         RPM,
32         GRAMS_SEC,
33         SECONDS,
34         KM,
35         KM_H,
36         PA,
37         NM,
38         INVALID
39 };
40
41 /**
42  *      @brief A representation of an OBD-II PID.
43  *
44  * pid - The 1 byte PID.
45  * name - A human readable name to use for this PID when published.
46  * min - minimum value for this pid
47  * max - maximum value for this pid
48  * unit - unit used
49  * frequency - The frequency to request this PID if supported by the vehicle
50  *              when automatic, recurring OBD-II requests are enabled.
51  *      supported - is it supported by the vehicle. Initialized after scan
52  */
53 typedef struct _Obd2Pid {
54         uint8_t pid;
55         const char* generic_name;
56         const int min;
57         const int max;
58         enum UNIT unit;
59         int frequency;
60         bool supported;
61 } Obd2Pid;
62
63 std::vector<Obd2Pid>& get_obd2_signals();
64 uint32_t get_signal_id(const Obd2Pid& sig);
65 void find_obd2_signals(const openxc_DynamicField &key, std::vector<Obd2Pid*>& found_signals);
66
67 /**
68  * @brief - Object to handle obd2 session with pre-scan of supported pid
69  * then request them regularly
70  */
71 class obd2_handler_t {
72         private:
73
74         public:
75                 obd2_handler_t();
76                 /**
77                  * @brief:
78                  *
79                  * Returns
80                  */
81                 void find_obd2_pid(const char *name, std::vector<Obd2Pid> *pids);
82
83                 /**
84                  * @brief Check if a request is an OBD-II PID request.
85                  *
86                  * @return true if the request is a mode 1  request and it has a 1 byte PID.
87                  */
88                 bool is_obd2_request(DiagnosticRequest *request);
89
90                 /**
91                 * @brief Check if requested signal name is an obd2 pid
92                 * 
93                 * @return true if name began with ob2.* else false.
94                 */
95                 bool is_obd2_signal(const char *name);
96
97                 /**
98                 * @brief Decode the payload of an OBD-II PID.
99                 *
100                 * This function matches the type signature for a DiagnosticResponseDecoder, so
101                 * it can be used as the decoder for a DiagnosticRequest. It returns the decoded
102                 * value of the PID, using the standard formulas (see
103                 * http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
104                 *
105                 * @param[in] DiagnosticResponse response - the received DiagnosticResponse (the data is in response.payload,
106                 *      a byte array). This is most often used when the byte order is
107                 *      signiticant, i.e. with many OBD-II PID formulas.
108                 * @param[in] float parsed_payload - the entire payload of the response parsed as an int.
109                 */
110                 float handle_obd2_pid(const DiagnosticResponse* response, float parsedPayload);
111 };