701248dc1ec012aa20c39bee65b08845fc7ba634
[apps/agl-service-can-low-level.git] / low-can-binding / diagnostic / diagnostic-message.cpp
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 #include <map>
18
19 #include "diagnostic-message.hpp"
20
21 #include "../binding/application.hpp"
22 #include "../utils/signals.hpp"
23
24 const char *UNIT_NAMES[10] = {
25         "POURCENT",
26         "DEGREES_CELSIUS",
27         "KPA",
28         "RPM",
29         "GRAMS_SEC",
30         "SECONDS",
31         "KM",
32         "KM_H",
33         "PA",
34         "NM"
35 };
36
37 diagnostic_message_t::diagnostic_message_t(
38                 uint8_t pid,
39                 const std::string& generic_name,
40                 const int min,
41                 const int max,
42                 enum UNIT unit,
43                 float frequency,
44                 DiagnosticResponseDecoder decoder,
45                 DiagnosticResponseCallback callback,
46                 bool supported,
47                 bool received)
48                 : parent_{nullptr},
49                 pid_{pid},
50                 generic_name_{generic_name},
51                 min_{min},
52                 max_{max},
53                 unit_{unit},
54                 frequency_{frequency},
55                 decoder_{decoder},
56                 callback_{callback},
57                 supported_{supported},
58                 last_timestamp_{0},
59                 received_{received},
60                 last_value_{.0f}
61 {}
62
63 uint32_t diagnostic_message_t::get_pid()
64 {
65         return (uint32_t)pid_;
66 }
67
68 const std::string diagnostic_message_t::get_generic_name() const
69 {
70         return generic_name_;
71 }
72
73 const std::string diagnostic_message_t::get_name() const
74 {
75         return active_diagnostic_request_t::get_prefix() + "." + generic_name_;
76 }
77
78 float diagnostic_message_t::get_frequency() const
79 {
80         return frequency_;
81 }
82
83 DiagnosticResponseDecoder diagnostic_message_t::get_decoder() const
84 {
85         return decoder_;
86 }
87 DiagnosticResponseCallback diagnostic_message_t::get_callback() const
88 {
89         return callback_;
90 }
91
92 bool diagnostic_message_t::get_supported() const
93 {
94         return supported_;
95 }
96
97 bool diagnostic_message_t::get_received() const
98 {
99         return received_;
100 }
101
102 float diagnostic_message_t::get_last_value() const
103 {
104         return last_value_;
105 }
106
107 std::pair<float, uint64_t> diagnostic_message_t::get_last_value_with_timestamp() const
108 {
109         return std::make_pair(last_value_, last_timestamp_);
110 }
111
112 void diagnostic_message_t::set_supported(bool value)
113 {
114         supported_ = value;
115 }
116
117 void diagnostic_message_t::set_parent(can_message_set_t* parent)
118 {
119         parent_ = parent;
120 }
121
122 void diagnostic_message_t::set_received(bool r)
123 {
124         received_ = r;
125 }
126
127 void diagnostic_message_t::set_last_value(float val)
128 {
129         last_value_ = val;
130 }
131
132 void diagnostic_message_t::set_timestamp(uint64_t timestamp)
133 {
134         last_timestamp_ = timestamp;
135 }
136
137 ///
138 /// @brief Build a DiagnosticRequest struct to be passed
139 ///  to diagnostic manager instance.
140 ///
141  const DiagnosticRequest diagnostic_message_t::build_diagnostic_request() const
142 {
143         return {/*arbitration_id: */OBD2_FUNCTIONAL_BROADCAST_ID,
144                         /*mode: */0x1,
145                         /*has_pid: */true,
146                         /*pid: */pid_,
147                         /*pid_length: */0,
148                         /*payload[]: */{0},
149                         /*payload_length: */0,
150                         /*no_frame_padding: */false,
151                         /*DiagnosticRequestType: */DiagnosticRequestType::DIAGNOSTIC_REQUEST_TYPE_PID };
152 }
153
154 ///
155 /// @brief Check if a request is an OBD-II PID request.
156 ///
157 /// @return true if the request is a mode 1 request and it has a 1 byte PID.
158 ///
159 bool diagnostic_message_t::is_obd2_request(const DiagnosticRequest* request)
160 {
161         return request->mode == 0x1 && request->has_pid && request->pid < 0xff;
162 }