Fix comment
[apps/agl-service-can-low-level.git] / src / diagnostic / active-diagnostic-request.cpp
index 7a43a0d..a9c21c6 100644 (file)
  * limitations under the License.
  */
 
+#include <map>
+#include <fnmatch.h>
+
 #include "active-diagnostic-request.hpp"
 
+#include "../configuration.hpp"
+
+#define ERROR_PID 0xFF
+
+std::string active_diagnostic_request_t::prefix_ = "diagnostic_messages";
+
 bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
 {
        return (bus_ == b.bus_ && id_ == b.id_ && handle_ == b.handle_) ? true : false;
@@ -48,9 +57,8 @@ active_diagnostic_request_t::active_diagnostic_request_t()
          in_flight_{false}, frequency_clock_{frequency_clock_t()}, timeout_clock_{frequency_clock_t()}
 {}
 
-active_diagnostic_request_t::active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
-               const std::string& name, bool wait_for_multiple_responses,
-               const DiagnosticResponseDecoder decoder,
+active_diagnostic_request_t::active_diagnostic_request_t(const std::string& bus, DiagnosticRequest* request,
+               const std::string& name, bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
                const DiagnosticResponseCallback callback, float frequencyHz)
        : bus_{bus}, id_{request->arbitration_id}, handle_{nullptr}, name_{name},
          decoder_{decoder}, callback_{callback}, recurring_{frequencyHz ? true : false}, wait_for_multiple_responses_{wait_for_multiple_responses},
@@ -62,9 +70,16 @@ uint32_t active_diagnostic_request_t::get_id() const
        return id_;
 }
 
-std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev()
+const std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev() const
+{
+       return can_bus_t::get_can_device(bus_);
+}
+
+uint16_t active_diagnostic_request_t::get_pid() const
 {
-       return bus_;
+       if (handle_->request.has_pid)
+               return handle_->request.pid;
+       return ERROR_PID;
 }
 
 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
@@ -72,6 +87,26 @@ DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
        return handle_;
 }
 
+const std::string active_diagnostic_request_t::get_name() const
+{
+       return name_;
+}
+
+std::string& active_diagnostic_request_t::get_prefix()
+{
+       return active_diagnostic_request_t::prefix_;
+}
+
+DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
+{
+       return decoder_;
+}
+
+DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
+{
+       return callback_;
+}
+
 bool active_diagnostic_request_t::get_recurring() const
 {
        return recurring_;
@@ -102,6 +137,27 @@ void active_diagnostic_request_t::set_in_flight(bool val)
        in_flight_ = val;
 }
 
+///
+/// @brief Check if requested signal name is a diagnostic message. If the name
+///  begin with the diagnostic message prefix then true else false.
+///
+/// @param[in] name - A signal name.
+///
+/// @return true if name began with the diagnostic message prefix else false.
+///
+bool active_diagnostic_request_t::is_diagnostic_signal(const std::string& name)
+{
+       const std::string p = active_diagnostic_request_t::prefix_ + "*";
+       if(::fnmatch(p.c_str(), name.c_str(), FNM_CASEFOLD) == 0)
+               return true;
+       return false;
+}
+
+/// @brief Check is the request should be sent or not
+///
+/// @return true if the request isn't already running and not
+/// recurring nor completed, or it is recurring its clock elapsed so it's
+/// time to send another one.
 bool active_diagnostic_request_t::should_send()
 {
        return !get_in_flight() && (
@@ -109,6 +165,9 @@ bool active_diagnostic_request_t::should_send()
                        (get_recurring() && get_frequency_clock().elapsed(true)));
 }
 
+/// @brief check if the timeout clock has elapsed
+///
+/// @return true if elapsed, so it is a timeout, else false.
 bool active_diagnostic_request_t::timed_out()
 {
        // don't use staggered start with the timeout clock