for(const auto& device : devices_name)
{
- can_devices_m_[device] = std::make_shared<can_bus_dev_t>(device, i);
- if (can_devices_m_[device]->open() == 0)
+ can_devices_.push_back(std::make_shared<can_bus_dev_t>(device, i));
+ if (can_devices_[i]->open() == 0)
{
i++;
DEBUG(binder_interface, "Start reading thread");
NOTICE(binder_interface, "%s device opened and reading", device.c_str());
- can_devices_m_[device]->start_reading(*this);
+ can_devices_[i]->start_reading(*this);
}
else
ERROR(binder_interface, "Can't open device %s", device.c_str());
*
* @return map can_bus_dev_m_ map
*/
-std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_bus_t::get_can_devices()
+const std::vector<std::shared_ptr<can_bus_dev_t>>& can_bus_t::get_can_devices() const
{
- return can_devices_m_;
+ return can_devices_;
}
std::condition_variable new_decoded_can_message_; /// < condition_variable use to wait until there is a new vehicle message to read from the queue vehicle_message_q_
std::mutex decoded_can_message_mutex_; /// < mutex protecting the vehicle_message_q_ queue.
- std::queue <openxc_VehicleMessage> vehicle_message_q_; /// < queue that'll store openxc_VehicleMessage to pushed
+ std::queue <openxc_VehicleMessage> vehicle_message_q_; /// < queue that'll store openxc_VehicleMessage to pushed
- std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_devices_m_; /// < Can device map containing all can_bus_dev_t objects initialized during init_can_dev function
+ std::vector<std::shared_ptr<can_bus_dev_t>> can_devices_; /// < Can device map containing all can_bus_dev_t objects initialized during init_can_dev function
public:
can_bus_t(int conf_file);
openxc_VehicleMessage next_vehicle_message();
void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
- std::map<std::string, std::shared_ptr<can_bus_dev_t>> get_can_devices();
+ const std::vector<std::shared_ptr<can_bus_dev_t>>& get_can_devices() const;
};
in_flight_{false}, frequency_clock_{frequency_clock_t()}, timeout_clock_{frequency_clock_t()}
{}
-active_diagnostic_request_t::active_diagnostic_request_t(can_bus_dev_t* bus, DiagnosticRequest* request,
+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,
const DiagnosticResponseCallback callback, float frequencyHz)
in_flight_{false}, frequency_clock_{frequency_clock_t(frequencyHz)}, timeout_clock_{frequency_clock_t(10)}
{}
-can_bus_dev_t* active_diagnostic_request_t::get_can_bus_dev()
+std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev()
{
return bus_;
}
*/
class active_diagnostic_request_t {
private:
- can_bus_dev_t* bus_; /*!< bus_ - The CAN bus this request should be made on, or is currently in flight-on*/
+ std::shared_ptr<can_bus_dev_t> bus_; /*!< bus_ - The CAN bus this request should be made on, or is currently in flight-on*/
uint32_t id_; /*!< id_ - The arbitration ID (aka message ID) for the request.*/
DiagnosticRequestHandle* handle_; /*!< handle_ - A handle for the request to keep track of it between
* sending the frames of the request and receiving all frames of the response.*/
active_diagnostic_request_t();
active_diagnostic_request_t(active_diagnostic_request_t&&) = default;
//active_diagnostic_request_t(const active_diagnostic_request_t&) = default;
- active_diagnostic_request_t(can_bus_dev_t* bus, DiagnosticRequest* request,
+ 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,
const DiagnosticResponseCallback callback, float frequencyHz);
- can_bus_dev_t* get_can_bus_dev();
+ std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
DiagnosticRequestHandle* get_handle();
bool get_recurring() const;
bool get_in_flight() const;
diagnostic_manager_t::diagnostic_manager_t()
: request_list_entries_(MAX_REQUEST_ENTRIES), initialized_{false}
+{}
+
+bool diagnostic_manager_t::initialize(std::shared_ptr<can_bus_dev_t> cbd)
{
+ // Mandatory to set the bus before intiliaze shims.
+ bus_ = cbd;
+
+ init_diagnostic_shims();
reset();
+
+ initialized_ = true;
+ DEBUG(binder_interface, "initialize: Diagnostic Manager initialized");
+ return initialized_;
}
-diagnostic_manager_t::diagnostic_manager_t(can_bus_dev_t& bus)
- : bus_(&bus), request_list_entries_(MAX_REQUEST_ENTRIES), initialized_{false}
+/**
+ * @brief initialize shims used by UDS lib and set initialized_ to true.
+ * It is needed before used the diagnostic manager fully because shims are
+ * required by most member functions.
+ */
+void diagnostic_manager_t::init_diagnostic_shims()
{
- reset();
+ shims_ = diagnostic_init_shims(shims_logger, shims_send, NULL);
+ DEBUG(binder_interface, "init_diagnostic_shims: Shims initialized");
+}
+
+void diagnostic_manager_t::reset()
+{
+ if(initialized_)
+ {
+ DEBUG(binder_interface, "Clearing existing diagnostic requests");
+ cleanup_active_requests(true);
+ }
+
+ for(int i = 0; i < MAX_SIMULTANEOUS_DIAG_REQUESTS; i++)
+ free_request_entries_.push_back(request_list_entries_[i]);
}
+
void diagnostic_manager_t::find_and_erase(active_diagnostic_request_t* entry, std::vector<active_diagnostic_request_t*>& requests_list)
{
auto i = std::find(requests_list.begin(), requests_list.end(), entry);
return false;
}
-void diagnostic_manager_t::reset()
-{
- if(initialized_)
- {
- DEBUG(binder_interface, "Clearing existing diagnostic requests");
- cleanup_active_requests(true);
- }
-
- for(int i = 0; i < MAX_SIMULTANEOUS_DIAG_REQUESTS; i++)
- free_request_entries_.push_back(request_list_entries_[i]);
-}
-
-can_bus_dev_t* diagnostic_manager_t::get_can_bus_dev()
+std::shared_ptr<can_bus_dev_t> diagnostic_manager_t::get_can_bus_dev()
{
return bus_;
}
bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
{
- can_bus_dev_t *can_bus_dev = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev();
+ std::shared_ptr<can_bus_dev_t> can_bus_dev = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev();
return can_bus_dev->shims_send(arbitration_id, data, size);
}
void diagnostic_manager_t::shims_timer()
{}
-/**
- * @brief initialize shims used by UDS lib and set initialized_ to true.
- * It is needed before used the diagnostic manager fully because shims are
- * required by most member functions.
- */
-void diagnostic_manager_t::init_diagnostic_shims()
-{
- shims_ = diagnostic_init_shims(shims_logger, shims_send, NULL);
- initialized_ = true;
-}
private:
DiagnosticShims shims_; /*!< shims_ - An array of shim functions for each CAN bus that plug the diagnostics
* library (uds-c) into the VI's CAN peripheral.*/
- can_bus_dev_t* bus_; /*!< bus_ - A pointer to the CAN bus that should be used for all standard OBD-II requests, if the bus is not
+ std::shared_ptr<can_bus_dev_t> bus_; /*!< bus_ - A pointer to the CAN bus that should be used for all standard OBD-II requests, if the bus is not
* explicitly spcified in the request. If NULL, all requests require an explicit bus.*/
std::vector<active_diagnostic_request_t*> recurring_requests_; /*!< recurringRequests - A queue of active, recurring diagnostic requests. When
* a response is received for a recurring request or it times out, it is
bool initialized_; /*!< * initialized - True if the DiagnosticsManager has been initialized with shims. It will interface with the uds-c lib*/
+ void init_diagnostic_shims();
+ void reset();
public:
diagnostic_manager_t();
- diagnostic_manager_t(can_bus_dev_t& bus);
- void init_diagnostic_shims();
+ bool initialize(std::shared_ptr<can_bus_dev_t> cbd);
- can_bus_dev_t* get_can_bus_dev();
+ std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
active_diagnostic_request_t* get_free_entry();
+ DiagnosticShims& get_shims();
void find_and_erase(active_diagnostic_request_t* entry, std::vector<active_diagnostic_request_t*>& requests_list);
void cancel_request(active_diagnostic_request_t* entry);
bool validate_optional_request_attributes(float frequencyHz);
- void reset();
void checkSupportedPids(const active_diagnostic_request_t& request,
const DiagnosticResponse& response, float parsedPayload);
{
can_bus_t& can_bus_manager = configuration_t::instance().get_can_bus_manager();
- /* Open CAN socket */
+ /// Initialize CAN socket
if(can_bus_manager.init_can_dev() == 0)
{
can_bus_manager.start_threads();
return 0;
}
+ /// Initialize Diagnostic manager that will handle obd2 requests
+ diagnostic_manager_t& diag_manager = configuration_t::instance().get_diagnostic_manager();
+ diag_manager.initialize(can_bus_manager.get_can_devices().front());
+
ERROR(binder_interface, "There was something wrong with CAN device Initialization. Check your config file maybe");
return 1;
}