Static code review fixes.
[apps/low-level-can-service.git] / CAN-binder / 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/configuration.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(uint8_t pid, const std::string& generic_name, const int min,
38         const int max, enum UNIT unit, float frequency, DiagnosticResponseDecoder decoder,
39         DiagnosticResponseCallback callback, bool supported)
40         : parent_{nullptr}, pid_{pid}, generic_name_{generic_name}, min_{min}, max_{max}, unit_{unit},
41         frequency_{frequency}, decoder_{decoder}, callback_{callback}, supported_{supported}
42 {}
43
44 uint32_t diagnostic_message_t::get_pid()
45 {
46         return (uint32_t)pid_;
47 }
48
49 const std::string diagnostic_message_t::get_generic_name() const
50 {
51         return generic_name_;
52 }
53
54 const std::string diagnostic_message_t::get_name() const
55 {
56         return active_diagnostic_request_t::get_prefix() + "." + generic_name_;
57 }
58
59 float diagnostic_message_t::get_frequency() const
60 {
61         return frequency_;
62 }
63
64 DiagnosticResponseDecoder diagnostic_message_t::get_decoder() const
65 {
66         return decoder_;
67 }
68 DiagnosticResponseCallback diagnostic_message_t::get_callback() const
69 {
70         return callback_;
71 }
72
73 bool diagnostic_message_t::get_supported() const
74 {
75         return supported_;
76 }
77
78 void diagnostic_message_t::set_supported(bool value)
79 {
80         supported_ = value;
81 }
82
83 void diagnostic_message_t::set_parent(can_message_set_t* parent)
84 {
85         parent_ = parent;
86 }
87
88 ///
89 /// @brief Build a DiagnosticRequest struct to be passed
90 ///  to diagnostic manager instance.
91 ///
92 const DiagnosticRequest diagnostic_message_t::build_diagnostic_request()
93 {
94         return {/*arbitration_id: */OBD2_FUNCTIONAL_BROADCAST_ID,
95                         /*mode: */0x1,
96                         /*has_pid: */true,
97                         /*pid: */pid_,
98                         /*pid_length: */0,
99                         /*payload[]: */{0},
100                         /*payload_length: */0,
101                         /*no_frame_padding: */false,
102                         /*DiagnosticRequestType: */DiagnosticRequestType::DIAGNOSTIC_REQUEST_TYPE_PID };
103 }
104
105 ///
106 /// @brief Check if a request is an OBD-II PID request.
107 ///
108 /// @return true if the request is a mode 1 request and it has a 1 byte PID.
109 ///
110 bool diagnostic_message_t::is_obd2_request(const DiagnosticRequest* request)
111 {
112         return request->mode == 0x1 && request->has_pid && request->pid < 0xff;
113 }