Format
[apps/agl-service-can-low-level.git] / low-can-binding / 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 <string>
21 #include <vector>
22
23 #include "../utils/socketcan-bcm.hpp"
24 #include "uds/uds.h"
25 #include "uds/uds_types.h"
26 #include "../utils/timer.hpp"
27
28 class active_diagnostic_request_t;
29 class diagnostic_manager_t;
30
31 /// @brief The signature for an optional function that can apply the neccessary
32 /// formula to translate the binary payload into meaningful data.
33 ///
34 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
35 ///  a byte array). This is most often used when the byte order is signiticant, i.e. with many OBD-II PID formulas.
36 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
37 ///
38 /// @return float value after decoding.
39 ///
40 typedef float (*DiagnosticResponseDecoder)(const DiagnosticResponse* response,
41                 float parsed_payload);
42
43 /// @brief: The signature for an optional function to handle a new diagnostic
44 /// response.
45 ///
46 /// @param[in] request - The original diagnostic request.
47 /// @param[in] response - The response object that was just received.
48 /// @param[in] parsed_payload - The payload of the response, parsed as a float.
49 ///
50 typedef void (*DiagnosticResponseCallback)(const active_diagnostic_request_t* request,
51                 const DiagnosticResponse* response, float parsed_payload);
52
53 ///
54 /// @brief An active diagnostic request, either recurring or one-time.
55 ///
56 /// Will host a diagnostic_message_t class members to describe an on going
57 ///  diagnostic request on the CAN bus. Diagnostic message will be converted to
58 ///  a DiagnosticRequest using ad-hoc method build_diagnostic_request from diagnostic message.
59 ///  Then missing member, that can not be hosted into a DiagnosticRequest struct, will be passed
60 ///  as argument when adding the request to (non)-recurrent vector. Argument will be used to instanciate
61 ///  an active_diagnostic_request_t object before sending it.
62 ///
63 class active_diagnostic_request_t {
64 private:
65         std::string bus_; ///< bus_ - The CAN bus this request should be made on, or is currently in flight-on
66         uint32_t id_; ///< id_ - The arbitration ID (aka message ID) for the request.
67         DiagnosticRequestHandle* handle_; ///< handle_ - A handle for the request to keep track of it between
68                                                                           ///< sending the frames of the request and receiving all frames of the response.
69         std::string name_; ///< name_ - Human readable name, to be used when publishing received responses.
70         static std::string prefix_; ///< prefix_ - It has to reflect the JSON object which it comes from. It makes easier sorting
71                                                                 ///< incoming CAN messages.
72         DiagnosticResponseDecoder decoder_; ///< decoder_ - An optional DiagnosticResponseDecoder to parse the payload of responses
73                                                                                 ///< to this request. If the decoder is NULL, the output will include the raw payload
74                                                                                 ///< instead of a parsed value.
75         DiagnosticResponseCallback callback_; ///< callback_ - An optional DiagnosticResponseCallback to be notified whenever a
76                                                                                   ///< response is received for this request.
77         bool recurring_; ///< bool recurring_ - If true, this is a recurring request and it will remain as active until explicitly cancelled.
78                                          ///< The frequencyClock attribute controls how often a recurrin request is made.
79         bool wait_for_multiple_responses_; ///< wait_for_multiple_responses_ - False by default, when any response is received for a request
80                                                                            ///< it will be removed from the active list. If true, the request will remain active until the timeout
81                                                                            ///< clock expires, to allow it to receive multiple response (e.g. to a functional broadcast request).
82         frequency_clock_t frequency_clock_; ///< frequency_clock_ - A frequency_clock_t object to control the send rate for a
83                                                                                 ///< recurring request. If the request is not reecurring, this attribute is not used.
84         frequency_clock_t timeout_clock_; ///< timeout_clock_ - A frequency_clock_t object to monitor how long it's been since
85                                                                           ///< this request was sent.
86         utils::socketcan_bcm_t socket_; ///< socket_ - A BCM socket setup to send cyclic message to CAN ID 7DF.
87 public:
88         bool operator==(const active_diagnostic_request_t& b);
89         active_diagnostic_request_t& operator=(const active_diagnostic_request_t& adr);
90
91         active_diagnostic_request_t();
92         active_diagnostic_request_t(active_diagnostic_request_t&&) = default;
93         active_diagnostic_request_t(const std::string& bus, uint32_t id,
94                 const std::string& name, bool wait_for_multiple_responses,
95                 const DiagnosticResponseDecoder decoder,
96                 const DiagnosticResponseCallback callback, float frequencyHz);
97         ~active_diagnostic_request_t();
98
99         uint32_t get_id() const;
100         DiagnosticRequestHandle* get_handle();
101         uint16_t get_pid() const;
102         const std::string get_name() const;
103         static std::string& get_prefix();
104         DiagnosticResponseDecoder& get_decoder();
105         DiagnosticResponseCallback& get_callback();
106         bool get_recurring() const;
107         frequency_clock_t& get_frequency_clock();
108         frequency_clock_t& get_timeout_clock();
109         utils::socketcan_bcm_t& get_socket();
110
111         void set_handle(DiagnosticShims& shims, DiagnosticRequest* request);
112
113         static bool is_diagnostic_signal(const std::string& name);
114
115         bool response_received() const;
116 };