aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-03-15 13:04:42 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2017-03-16 17:21:57 +0100
commit1d464d2a7380248b04c3e734ed5c6e84bbf53ddf (patch)
treec75ef10c1a45477f4597e4edd97d0a7fc16a4447 /src
parent56c22348d3af944b68bc7395553b9f8b1a817ba6 (diff)
Revert accessing CAN device with a map indexing on dev name
Main reason for that behavior revert is because of generator that rely on CAN device name and so we have point on these object by specify the device name. Instead of shared pointer between objects, instead binding is based on device name. With a device name you can get the shared pointer on it using new static method from can_bus_t object. Change-Id: I331e0ad8d03c88a15c697d12a9fce3699b0cd962 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r--src/can/can-bus.cpp30
-rw-r--r--src/can/can-bus.hpp5
-rw-r--r--src/can/can-message-definition.cpp8
-rw-r--r--src/can/can-message-definition.hpp7
-rw-r--r--src/can/can-message.cpp2
-rw-r--r--src/configuration-generated.cpp11
-rw-r--r--src/configuration.cpp2
-rw-r--r--src/configuration.hpp4
-rw-r--r--src/diagnostic/active-diagnostic-request.cpp12
-rw-r--r--src/diagnostic/active-diagnostic-request.hpp7
-rw-r--r--src/diagnostic/diagnostic-manager.cpp14
-rw-r--r--src/diagnostic/diagnostic-manager.hpp11
-rw-r--r--src/low-can-binding.cpp2
13 files changed, 73 insertions, 42 deletions
diff --git a/src/can/can-bus.cpp b/src/can/can-bus.cpp
index 69f08c5f..7c6ae429 100644
--- a/src/can/can-bus.cpp
+++ b/src/can/can-bus.cpp
@@ -50,6 +50,8 @@ can_bus_t::can_bus_t(int conf_file)
{
}
+std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_bus_t::can_devices_;
+
/**
* @brief Will make the decoding operation on a classic CAN message. It will not
* handle CAN commands nor diagnostic messages that have their own method to get
@@ -254,7 +256,7 @@ void can_bus_t::stop_threads()
int can_bus_t::init_can_dev()
{
std::vector<std::string> devices_name;
- int i;
+ int i = 0;
size_t t;
devices_name = read_conf();
@@ -262,20 +264,19 @@ int can_bus_t::init_can_dev()
if (! devices_name.empty())
{
t = devices_name.size();
- i=0;
for(const auto& device : devices_name)
{
- can_devices_.push_back(std::make_shared<can_bus_dev_t>(device, i));
- if (can_devices_[i]->open() == 0)
+ can_bus_t::can_devices_[device] = std::make_shared<can_bus_dev_t>(device, i);
+ if (can_bus_t::can_devices_[device]->open() == 0)
{
DEBUG(binder_interface, "Start reading thread");
NOTICE(binder_interface, "%s device opened and reading", device.c_str());
- can_devices_[i]->start_reading(*this);
+ can_bus_t::can_devices_[device]->start_reading(*this);
+ i++;
}
else
ERROR(binder_interface, "Can't open device %s", device.c_str());
- i++;
}
NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
@@ -421,7 +422,20 @@ void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
*
* @return map can_bus_dev_m_ map
*/
-const std::vector<std::shared_ptr<can_bus_dev_t>>& can_bus_t::get_can_devices() const
+const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& can_bus_t::get_can_devices() const
+{
+ return can_bus_t::can_devices_;
+}
+
+/**
+* @brief Return the shared pointer on the can_bus_dev_t initialized
+* with device_name "bus"
+*
+* @param[in] bus - CAN bus device name to retrieve.
+*
+* @return A shared pointer on an object can_bus_dev_t
+*/
+std::shared_ptr<can_bus_dev_t> can_bus_t::get_can_device(std::string bus)
{
- return can_devices_;
+ return can_bus_t::can_devices_[bus];
}
diff --git a/src/can/can-bus.hpp b/src/can/can-bus.hpp
index 97b683d7..a51dfb1b 100644
--- a/src/can/can-bus.hpp
+++ b/src/can/can-bus.hpp
@@ -68,7 +68,7 @@ private:
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::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
+ static std::map<std::string, 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);
@@ -91,5 +91,6 @@ public:
openxc_VehicleMessage next_vehicle_message();
void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
- const std::vector<std::shared_ptr<can_bus_dev_t>>& get_can_devices() const;
+ const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& get_can_devices() const;
+ static std::shared_ptr<can_bus_dev_t> get_can_device(std::string bus);
};
diff --git a/src/can/can-message-definition.cpp b/src/can/can-message-definition.cpp
index 82724573..7aca19e9 100644
--- a/src/can/can-message-definition.cpp
+++ b/src/can/can-message-definition.cpp
@@ -17,11 +17,15 @@
#include "can-message-definition.hpp"
-can_message_definition_t::can_message_definition_t(uint8_t bus)
+can_message_definition_t::can_message_definition_t(const std::string bus)
: bus_{bus}, last_value_(CAN_MESSAGE_SIZE)
{}
-can_message_definition_t::can_message_definition_t(uint8_t bus, uint32_t id, can_message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed)
+can_message_definition_t::can_message_definition_t(const std::string bus, uint32_t id, frequency_clock_t frequency_clock, bool force_send_changed)
+: bus_{bus}, id_{id}, frequency_clock_{frequency_clock}, force_send_changed_{force_send_changed}, last_value_(CAN_MESSAGE_SIZE)
+{}
+
+can_message_definition_t::can_message_definition_t(const std::string bus, uint32_t id, can_message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed)
: bus_{bus}, id_{id}, format_{format}, frequency_clock_{frequency_clock}, force_send_changed_{force_send_changed}, last_value_(CAN_MESSAGE_SIZE)
{}
diff --git a/src/can/can-message-definition.hpp b/src/can/can-message-definition.hpp
index 1c7199e6..921b58ad 100644
--- a/src/can/can-message-definition.hpp
+++ b/src/can/can-message-definition.hpp
@@ -35,7 +35,7 @@
class can_message_definition_t
{
private:
- uint8_t bus_; /*!< bus_ - Address of CAN bus device. */
+ std::string bus_; /*!< bus_ - Address of CAN bus device. */
uint32_t id_; /*!< id_ - The ID of the message.*/
can_message_format_t format_; /*!< format_ - the format of the message's ID.*/
frequency_clock_t frequency_clock_; /*!< clock_ - an optional frequency clock to control the output of this
@@ -48,8 +48,9 @@ private:
* needs to compare an incoming CAN message with the previous frame.*/
public:
- can_message_definition_t(uint8_t bus);
- can_message_definition_t(uint8_t bus, uint32_t id, can_message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed);
+ can_message_definition_t(const std::string bus);
+ can_message_definition_t(const std::string bus, uint32_t id, frequency_clock_t frequency_clock, bool force_send_changed);
+ can_message_definition_t(const std::string bus, uint32_t id, can_message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed);
uint32_t get_id() const;
};
diff --git a/src/can/can-message.cpp b/src/can/can-message.cpp
index cc4adb49..6b40d912 100644
--- a/src/can/can-message.cpp
+++ b/src/can/can-message.cpp
@@ -170,7 +170,7 @@ can_message_t can_message_t::convert_from_canfd_frame(const struct canfd_frame&
format = can_message_format_t::EXTENDED;
else
format = can_message_format_t::STANDARD;
-
+
switch(format)
{
case can_message_format_t::STANDARD:
diff --git a/src/configuration-generated.cpp b/src/configuration-generated.cpp
index be3bc368..fd2e3305 100644
--- a/src/configuration-generated.cpp
+++ b/src/configuration-generated.cpp
@@ -41,8 +41,8 @@ std::vector<can_message_set_t> CAN_MESSAGE_SET =
std::vector<std::vector<can_message_definition_t>> CAN_MESSAGES_DEFINITION = {
{
- can_message_definition_t(0, 0x620, can_message_format_t::STANDARD, frequency_clock_t(), false),
- can_message_definition_t(0, 0x442, can_message_format_t::STANDARD, frequency_clock_t(), false),
+ can_message_definition_t("vcan0", 0x620, can_message_format_t::STANDARD, frequency_clock_t(), false),
+ can_message_definition_t("vcan0", 0x442, can_message_format_t::STANDARD, frequency_clock_t(), false),
},
};
@@ -59,4 +59,9 @@ std::vector<std::vector<can_signal_t>> SIGNALS = {
configuration_t::configuration_t()
: can_message_set_{CAN_MESSAGE_SET}, can_message_definition_{CAN_MESSAGES_DEFINITION}, can_signals_{SIGNALS}, obd2_signals_{OBD2_PIDS}
-{} \ No newline at end of file
+{}
+
+const std::string configuration_t::get_diagnostic_bus() const
+{
+ return "vcan0";
+} \ No newline at end of file
diff --git a/src/configuration.cpp b/src/configuration.cpp
index e6114cb9..ec12d8f1 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -42,7 +42,7 @@ can_bus_t& configuration_t::get_can_bus_manager()
return can_bus_manager_;
}
-const std::vector<std::shared_ptr<can_bus_dev_t>>& configuration_t::get_can_bus_devices()
+const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& configuration_t::get_can_bus_devices()
{
return can_bus_manager_.get_can_devices();
}
diff --git a/src/configuration.hpp b/src/configuration.hpp
index 214b3e78..224672ec 100644
--- a/src/configuration.hpp
+++ b/src/configuration.hpp
@@ -61,7 +61,9 @@ class configuration_t
can_bus_t& get_can_bus_manager();
- const std::vector<std::shared_ptr<can_bus_dev_t>>& get_can_bus_devices();
+ const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& get_can_bus_devices();
+
+ const std::string get_diagnostic_bus() const;
diagnostic_manager_t& get_diagnostic_manager() ;
diff --git a/src/diagnostic/active-diagnostic-request.cpp b/src/diagnostic/active-diagnostic-request.cpp
index 9d0f6aae..fcdbc97a 100644
--- a/src/diagnostic/active-diagnostic-request.cpp
+++ b/src/diagnostic/active-diagnostic-request.cpp
@@ -16,9 +16,12 @@
*/
#include <fnmatch.h>
+#include <map>
#include "active-diagnostic-request.hpp"
+#include "../configuration.hpp"
+
std::string active_diagnostic_request_t::prefix_ = "diagnostic_messages";
bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
@@ -52,9 +55,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},
@@ -66,9 +68,9 @@ 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 bus_;
+ return can_bus_t::get_can_device(bus_);
}
DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
diff --git a/src/diagnostic/active-diagnostic-request.hpp b/src/diagnostic/active-diagnostic-request.hpp
index f18282f6..b40ae08a 100644
--- a/src/diagnostic/active-diagnostic-request.hpp
+++ b/src/diagnostic/active-diagnostic-request.hpp
@@ -56,7 +56,7 @@ typedef void (*DiagnosticResponseCallback)(const active_diagnostic_request_t* re
*/
class active_diagnostic_request_t {
private:
- std::shared_ptr<can_bus_dev_t> bus_; /*!< bus_ - The CAN bus this request should be made on, or is currently in flight-on*/
+ std::string 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.*/
@@ -82,15 +82,16 @@ private:
public:
bool operator==(const active_diagnostic_request_t& b);
active_diagnostic_request_t& operator=(const active_diagnostic_request_t& adr);
+
active_diagnostic_request_t();
active_diagnostic_request_t(active_diagnostic_request_t&&) = default;
- active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
+ 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);
uint32_t get_id() const;
- std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
+ const std::shared_ptr<can_bus_dev_t> get_can_bus_dev() const;
DiagnosticRequestHandle* get_handle();
const std::string get_name() const;
static std::string& get_prefix();
diff --git a/src/diagnostic/diagnostic-manager.cpp b/src/diagnostic/diagnostic-manager.cpp
index 21a599d2..1477b2b9 100644
--- a/src/diagnostic/diagnostic-manager.cpp
+++ b/src/diagnostic/diagnostic-manager.cpp
@@ -33,10 +33,10 @@ diagnostic_manager_t::diagnostic_manager_t()
: initialized_{false}
{}
-bool diagnostic_manager_t::initialize(std::shared_ptr<can_bus_dev_t> cbd)
+bool diagnostic_manager_t::initialize()
{
// Mandatory to set the bus before intiliaze shims.
- bus_ = cbd;
+ bus_ = configuration_t::instance().get_diagnostic_bus();
init_diagnostic_shims();
reset();
@@ -153,7 +153,7 @@ active_diagnostic_request_t* diagnostic_manager_t::find_recurring_request(const
std::shared_ptr<can_bus_dev_t> diagnostic_manager_t::get_can_bus_dev()
{
- return bus_;
+ return can_bus_t::get_can_device(bus_);
}
bool diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::string name,
@@ -178,7 +178,7 @@ bool diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::st
find_and_erase(entry, non_recurring_requests_);
DEBUG(binder_interface, "Added one-time diagnostic request on bus %s: %s",
- bus_->get_device_name(), request_string);
+ bus_, request_string);
non_recurring_requests_.push_back(entry);
}
@@ -227,7 +227,7 @@ bool diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, con
sizeof(request_string));
DEBUG(binder_interface, "add_recurring_request: Added recurring diagnostic request (freq: %f) on bus %s: %s",
- frequencyHz, bus_->get_device_name().c_str(), request_string);
+ frequencyHz, bus_.c_str(), request_string);
uint64_t usec;
sd_event_now(afb_daemon_get_event_loop(binder_interface->daemon), CLOCK_MONOTONIC, &usec);
@@ -343,7 +343,7 @@ int diagnostic_manager_t::send_request(sd_event_source *s, uint64_t usec, void *
// if(adr != nullptr && adr->get_can_bus_dev() == dm.get_can_bus_dev() && adr->should_send() &&
// dm.clear_to_send(adr))
- if(adr != nullptr && adr->get_can_bus_dev() == dm.bus_)
+ if(adr != nullptr && adr->get_can_bus_dev() == dm.get_can_bus_dev())
{
adr->get_frequency_clock().tick();
start_diagnostic_request(&dm.shims_, adr->get_handle());
@@ -376,7 +376,7 @@ DiagnosticShims& diagnostic_manager_t::get_shims()
bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
{
- std::shared_ptr<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 = can_bus_t::get_can_device(configuration_t::instance().get_diagnostic_manager().bus_);
return can_bus_dev->shims_send(arbitration_id, data, size);
}
diff --git a/src/diagnostic/diagnostic-manager.hpp b/src/diagnostic/diagnostic-manager.hpp
index 71662ba1..ff055d54 100644
--- a/src/diagnostic/diagnostic-manager.hpp
+++ b/src/diagnostic/diagnostic-manager.hpp
@@ -18,6 +18,7 @@
#pragma once
#include <systemd/sd-event.h>
+#include <map>
#include <vector>
#include "uds/uds.h"
@@ -49,10 +50,10 @@ protected:
static void shims_timer();
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.*/
- 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.*/
+ DiagnosticShims shims_; /*!< shims_ - A map of shim functions for each CAN bus that plug the diagnostics
+ * library (uds-c) into the VI's CAN peripheral.*/
+ std::string 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. Default to the first bus CAN at initialization.*/
std::vector<active_diagnostic_request_t*> recurring_requests_; /*!< recurringRequests - A list of active recurring diagnostic requests.*/
std::vector<active_diagnostic_request_t*> non_recurring_requests_; /*!< nonrecurringRequests - A list of active one-time diagnostic requests. When a
* response is received for a non-recurring request or it times out, it is removed*/
@@ -63,7 +64,7 @@ private:
public:
diagnostic_manager_t();
- bool initialize(std::shared_ptr<can_bus_dev_t> cbd);
+ bool initialize();
std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
DiagnosticShims& get_shims();
diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp
index f60790fc..074e95cc 100644
--- a/src/low-can-binding.cpp
+++ b/src/low-can-binding.cpp
@@ -246,7 +246,7 @@ extern "C"
/// Initialize Diagnostic manager that will handle obd2 requests.
/// We pass by default the first CAN bus device to its Initialization.
/// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
- if(configuration_t::instance().get_diagnostic_manager().initialize(can_bus_manager.get_can_devices().front()))
+ if(configuration_t::instance().get_diagnostic_manager().initialize())
return 0;
ERROR(binder_interface, "There was something wrong with CAN device Initialization. Check your config file maybe");