diff options
author | Scott Murray <scott.murray@konsulko.com> | 2021-01-06 18:30:03 -0500 |
---|---|---|
committer | Scott Murray <scott.murray@konsulko.com> | 2021-01-11 17:57:06 -0500 |
commit | 9e23caa4c56259044604c38f107f7c637001b846 (patch) | |
tree | 12c843eb45e2783f2009f81c4d61707efd56fdd5 /low-can-binding/binding/low-can-cb.cpp | |
parent | 64092acda9a8581034c61732754eddee39f747ca (diff) |
Restore /etc/dev-mapping.conf supportneedlefish_13.93.0needlefish/13.93.0marlin_12.93.0marlin_12.92.0marlin_12.91.0marlin_12.90.1marlin_12.90.0marlin/12.93.0marlin/12.92.0marlin/12.91.0marlin/12.90.1marlin/12.90.0lamprey_11.92.0lamprey_11.91.0lamprey/11.92.0lamprey/11.91.0koi_10.93.0koi_10.92.0koi/10.93.0koi/10.92.013.93.012.93.012.92.012.91.012.90.112.90.011.92.011.91.010.93.010.92.0
Add back the ini-config and config-parser code that existed
previously, and use it in binding init to over-ride the device
mapping from the controller JSON if /etc/dev-mapping.conf exists.
This restores the documented behavior, and is needed for the
existing AGL demo platform support and soon CI.
Additionally:
- Add code to validate the active_message_set, diagnostic_bus, and
bus device mapping configuration values.
- The above required moving plugin loading before the configuration
callback in the controller configuration, but this change seems
rational in that everything required by the generated plugin code
is already initialized before then, and it makes validating the
configuration possible without adding an extra callback.
- Add logging of the used CAN bus to device mappings at info level
to ease debugging any future issues.
- Tweak the log level of the missing configuration file message to
info from error, since it is a legitimate mode of operation if
relying on the default bus values in the controller JSON.
Bug-AGL: SPEC-3755
Signed-off-by: Scott Murray <scott.murray@konsulko.com>
Change-Id: I440f5e0fc85be41f7c4c1f47d824a403525a18f9
Diffstat (limited to 'low-can-binding/binding/low-can-cb.cpp')
-rw-r--r-- | low-can-binding/binding/low-can-cb.cpp | 81 |
1 files changed, 67 insertions, 14 deletions
diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index d994dd24..32adac86 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2015, 2018 "IoT.bzh" + * Copyright (C) 2021 Konsulko Group * Author "Romain Forlot" <romain.forlot@iot.bzh> * Author "Loic Collignon" <loic.collignon@iot.bzh> * @@ -23,6 +24,7 @@ #include <queue> #include <mutex> #include <vector> +#include <set> #include <thread> #include <algorithm> #include <wrap-json.h> @@ -37,7 +39,7 @@ #include "../utils/signals.hpp" #include "../diagnostic/diagnostic-message.hpp" #include "../utils/openxc-utils.hpp" -#include "../utils/signals.hpp" +#include "../utils/config-parser.hpp" #ifdef USE_FEATURE_J1939 #include "../can/message/j1939-message.hpp" @@ -56,7 +58,7 @@ int config_low_can(afb_api_t apiHandle, CtlSectionT *section, json_object *json_ CtlConfigT *ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle); int active_message_set = 0; json_object *dev_mapping = nullptr; - const char *diagnotic_bus = nullptr; + const char *diagnostic_bus = nullptr; if(! ctrlConfig || ! ctrlConfig->external) return -1; @@ -64,35 +66,86 @@ int config_low_can(afb_api_t apiHandle, CtlSectionT *section, json_object *json_ application_t *application = (application_t*) ctrlConfig->external; if(wrap_json_unpack(json_obj, "{si, s?s}", - "active_message_set", &active_message_set, - "diagnostic_bus", &diagnotic_bus)) + "active_message_set", &active_message_set, + "diagnostic_bus", &diagnostic_bus)) { + AFB_ERROR("active_message_set and/or diagnostic_bus missing in controller JSON"); + return -1; + } + + if(active_message_set < 0 || + active_message_set > (application->get_messages_definition().size() - 1)) { + AFB_ERROR("Invalid active message set %d", active_message_set); return -1; + } + application->set_active_message_set((uint8_t) active_message_set); + + utils::config_parser_t conf_file("/etc/dev-mapping.conf"); + if(conf_file.check_conf()) + { + // If a mapping file in /etc exists, use it + AFB_INFO("Using /etc/dev-mapping.conf"); + application->get_can_bus_manager().set_can_devices(conf_file.get_devices_name()); + } else { + // Use whatever configuration is in the controller JSON + if(wrap_json_unpack(json_obj, "{so}", + "dev-mapping", &dev_mapping)) { + AFB_ERROR("No device mapping in controller JSON"); + return -1; + } + + application->get_can_bus_manager().set_can_devices(dev_mapping); + } + + // Find all required buses + std::set<std::string> buses; + std::set<std::string> j1939_buses; + vect_ptr_msg_def_t msg_defs = application->get_messages_definition(); + for(std::shared_ptr<message_definition_t> &msg_def : msg_defs) { + if(!msg_def->is_j1939()) + buses.insert(msg_def->get_bus_name()); + else + j1939_buses.insert(msg_def->get_bus_name()); + } +#ifdef USE_FEATURE_J1939 + // NOTE: With C++17 just: buses.merge(j1939_buses) + for(auto it = begin(j1939_buses); it != end(j1939_buses); ++it) + buses.insert(*it); +#endif - application->set_active_message_set((uint8_t)active_message_set); + // Check that required buses have device mappings + for(auto it = begin(buses); it != end(buses); ++it) { + std::string dev = application->get_can_bus_manager().get_can_device_name(*it); + if(dev == "") { + AFB_ERROR("No CAN device defined for bus \"%s\"", it->c_str()); + return -1; + } + AFB_INFO("Using CAN device %s for bus \"%s\"", dev.c_str(), it->c_str()); + } - if(wrap_json_unpack(json_obj, "{so}", - "dev-mapping", &dev_mapping)) + // Check that diagnostic bus is one of the configured buses + if(diagnostic_bus && buses.count(diagnostic_bus) == 0) { + AFB_ERROR("No CAN device mapping defined for diagnostic bus \"%s\"", diagnostic_bus); return -1; - application->get_can_bus_manager().set_can_devices(dev_mapping); + } /// Initialize Diagnostic manager that will handle obd2 requests. /// We pass by default the first CAN bus device to its Initialization. - if(! diagnotic_bus || application_t::instance().get_diagnostic_manager().initialize(diagnotic_bus)) + if(! diagnostic_bus || application_t::instance().get_diagnostic_manager().initialize(diagnostic_bus)) AFB_WARNING("Diagnostic Manager: not initialized. No diagnostic messages will be processed."); return 0; } CtlSectionT ctlSections_[] = { - [0]={.key="config" , .uid="config", .info=nullptr, - .loadCB=config_low_can, - .handle=nullptr, - .actions=nullptr}, - [1]={.key="plugins" , .uid="plugins", .info=nullptr, + [0]={.key="plugins" , .uid="plugins", .info=nullptr, .loadCB=PluginConfig, .handle=nullptr, .actions=nullptr}, + [1]={.key="config" , .uid="config", .info=nullptr, + .loadCB=config_low_can, + .handle=nullptr, + .actions=nullptr}, [2]={.key=nullptr , .uid=nullptr, .info=nullptr, .loadCB=nullptr, .handle=nullptr, |