Use shared_ptr to set message_set parent
[apps/agl-service-can-low-level.git] / low-can-binding / diagnostic / diagnostic-message.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 #include <string>
22
23 #include "uds/uds.h"
24 #include "../can/can-message-set.hpp"
25 #include "../can/can-message.hpp"
26 #include "active-diagnostic-request.hpp"
27
28 enum UNIT {
29         POURCENT,
30         DEGREES_CELSIUS,
31         KPA,
32         RPM,
33         GRAMS_SEC,
34         SECONDS,
35         KM,
36         KM_H,
37         PA,
38         NM,
39         INVALID
40 };
41
42 class message_set_t;
43
44 /// @brief - A representation of an OBD-II PID Mode 01 (Note : An OBD-II PID mode 01 contains only one information).
45 class diagnostic_message_t
46 {
47         private:
48                 std::shared_ptr<can_message_set_t> parent_; /*!< parent_ - Pointer to the CAN message set holding this diagnostic message */
49                 uint8_t pid_; /*!< pid_ - The 1 byte PID.*/
50                 std::string generic_name_; /*!< generic_name_ - A human readable name to use for this PID when published.*/
51                 int min_; /*!< min_ - Minimum value that can take this pid */
52                 int max_; /*!< max_ - Maximum value that can take this pid */
53                 enum UNIT unit_; /*!< unit_ : Which unit system is used by that pid. See enum UNIT above.*/
54                 float frequency_; /*!< frequency_ - The frequency to request this PID if supported by the vehicle when automatic, recurring OBD-II requests are enabled.*/
55                 DiagnosticResponseDecoder decoder_; /*!< decoder_ - An optional DiagnosticResponseDecoder to parse the payload of responses
56                                                                                          * to this request. If the decoder is NULL, the output will include the raw payload
57                                                                                          * instead of a parsed value.*/
58                 DiagnosticResponseCallback callback_; /*!< callback_ - An optional DiagnosticResponseCallback to be notified whenever a
59                                                                                            * response is received for this request.*/
60
61                 bool supported_; /*!< supported_ - boolean indicating whether this pid is supported by the vehicle or not.*/
62
63                 uint64_t last_timestamp_; /*!< last_timestamp_ - the last time (in microseconds since epoch)
64                                                                 * that the message has been received. */
65
66                 bool received_; /*!< received_ - True if this signal has ever been received. */
67                 float last_value_; /*!< last_value_ - The last received value of the diagnostic message.
68                                                                 * If 'received_' is false, this value is undefined. */
69
70         public:
71                 const char* generic_name = generic_name_.c_str();
72                 diagnostic_message_t(uint8_t pid,
73                                      const std::string& generic_name,
74                                      const int min,
75                                      const int max,
76                                      enum UNIT unit,
77                                      float frequency,
78                                      DiagnosticResponseDecoder decoder,
79                                      DiagnosticResponseCallback callback,
80                                      bool supported,
81                                      bool received);
82
83                 uint32_t get_pid();
84                 const std::string get_generic_name() const;
85                 const std::string get_name() const;
86                 float get_frequency() const;
87                 DiagnosticResponseDecoder get_decoder() const;
88                 DiagnosticResponseCallback get_callback() const;
89                 bool get_supported() const;
90
91                 bool get_received() const;
92                 float get_last_value() const;
93                 std::pair<float, uint64_t> get_last_value_with_timestamp() const;
94
95                 void set_received(bool r);
96                 void set_last_value(float val);
97                 void set_timestamp(uint64_t timestamp);
98
99                 void set_supported(bool value);
100                 void set_parent(std::shared_ptr<can_message_set_t> parent);
101                 const DiagnosticRequest build_diagnostic_request() const;
102
103                 bool is_obd2_response(const can_message_t& can_message);
104                 bool is_obd2_request(const DiagnosticRequest *request);
105 };