f18282f60e255d55e8fcc890e32bff07353730cf
[apps/low-level-can-service.git] / src / diagnostic / active-diagnostic-request.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 "uds/uds_types.h"
24 #include "../can/can-bus-dev.hpp"
25 #include "../can/can-message.hpp"
26
27 #include "../low-can-binding.hpp"
28
29 class active_diagnostic_request_t;
30 class diagnostic_manager_t;
31
32 /* Public: The signature for an optional function that can apply the neccessary
33  * formula to translate the binary payload into meaningful data.
34  *
35  * response - the received DiagnosticResponse (the data is in response.payload,
36  *      a byte array). This is most often used when the byte order is
37  *      signiticant, i.e. with many OBD-II PID formulas.
38  * parsed_payload - the entire payload of the response parsed as an int.
39  */
40 typedef float (*DiagnosticResponseDecoder)(const DiagnosticResponse* response,
41                 float parsed_payload);
42
43 /* Public: The signature for an optional function to handle a new diagnostic
44  * response.
45  *
46  * manager - The DiagnosticsManager providing this response.
47  * request - The original diagnostic request.
48  * response - The response object that was just received.
49  * parsed_payload - The payload of the response, parsed as a float.
50  */
51 typedef void (*DiagnosticResponseCallback)(const active_diagnostic_request_t* request,
52                 const DiagnosticResponse* response, float parsed_payload);
53
54 /**
55  * @brief An active diagnostic request, either recurring or one-time.
56  */
57 class active_diagnostic_request_t {
58 private:
59         std::shared_ptr<can_bus_dev_t> bus_; /*!< bus_ - The CAN bus this request should be made on, or is currently in flight-on*/
60         uint32_t id_; /*!< id_ - The arbitration ID (aka message ID) for the request.*/
61         DiagnosticRequestHandle* handle_; /*!< handle_ - A handle for the request to keep track of it between
62                                                                                 * sending the frames of the request and receiving all frames of the response.*/
63         std::string name_; /*!< name_ - An optional human readable name this response, to be used when publishing received 
64                                                 * responses. If the name is NULL, the published output will use the raw OBD-II response format.*/
65         static std::string prefix_; /*!< prefix_ - generic_name_ will be prefixed with it. It has to reflect the used protocol.
66                                                                  * which make easier to sort message when the come in.*/
67         DiagnosticResponseDecoder decoder_; /*!< decoder_ - An optional DiagnosticResponseDecoder to parse the payload of responses
68                                                                                         * to this request. If the decoder is NULL, the output will include the raw payload
69                                                                                         * instead of a parsed value.*/
70         DiagnosticResponseCallback callback_; /*!< callback_ - An optional DiagnosticResponseCallback to be notified whenever a
71                                                                                         * response is received for this request.*/
72         bool recurring_; /*!< bool recurring_ - If true, this is a recurring request and it will remain as active until explicitly cancelled.
73                                                 * The frequencyClock attribute controls how often a recurrin request is made.*/
74         bool wait_for_multiple_responses_; /*!< wait_for_multiple_responses_ - False by default, when any response is received for a request
75                                                                                 * it will be removed from the active list. If true, the request will remain active until the timeout
76                                                                                 * clock expires, to allow it to receive multiple response (e.g. to a functional broadcast request).*/
77         bool in_flight_; /*!< in_flight_ - True if the request has been sent and we are waiting for a response.*/
78         frequency_clock_t frequency_clock_; /*!< frequency_clock_ - A frequency_clock_t object to control the send rate for a
79                                                                                 * recurring request. If the request is not reecurring, this attribute is not used.*/
80         frequency_clock_t timeout_clock_; /*!< timeout_clock_ - A frequency_clock_t object to monitor how long it's been since
81                                                                         * this request was sent.*/
82 public:
83         bool operator==(const active_diagnostic_request_t& b);
84         active_diagnostic_request_t& operator=(const active_diagnostic_request_t& adr);
85         active_diagnostic_request_t();
86         active_diagnostic_request_t(active_diagnostic_request_t&&) = default;
87         active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
88                 const std::string& name, bool wait_for_multiple_responses,
89                 const DiagnosticResponseDecoder decoder,
90                 const DiagnosticResponseCallback callback, float frequencyHz);
91         
92         uint32_t get_id() const;
93         std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
94         DiagnosticRequestHandle* get_handle();
95         const std::string get_name() const;
96         static std::string& get_prefix();
97         DiagnosticResponseDecoder& get_decoder();
98         DiagnosticResponseCallback& get_callback();
99         bool get_recurring() const;
100         bool get_in_flight() const;
101         frequency_clock_t& get_frequency_clock();
102         frequency_clock_t& get_timeout_clock();
103         
104         void set_handle(DiagnosticShims& shims, DiagnosticRequest* request);
105         void set_in_flight(bool val);
106
107         static bool is_diagnostic_signal(const std::string& name);
108
109         bool should_send();
110
111         bool timed_out();
112         bool response_received() const;
113         bool request_completed();
114 };