diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-07-17 10:56:46 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-07-21 12:02:11 +0200 |
commit | 1a90edea7d3d45da772c590d642c0093839cc91f (patch) | |
tree | cfb40e18158e097dea9d41a1628f2718a0afb999 | |
parent | 2a4abf5bd6440e9cd5b98b70453d74ae3d1b8cfa (diff) |
Split big function to several smaller
Change-Id: I070fdafd51e789eb37b963a100b57cae9dff3e25
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | low-can-binding/binding/low-can-cb.cpp | 118 | ||||
-rw-r--r-- | low-can-binding/binding/low-can-hat.cpp | 2 | ||||
-rw-r--r-- | low-can-binding/binding/low-can-hat.hpp | 2 |
3 files changed, 75 insertions, 47 deletions
diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index 5910f6a..cac9fc4 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -368,17 +368,80 @@ void unsubscribe(struct afb_req request) do_subscribe_unsubscribe(request, false); } -void swrite(struct afb_req request) +static int send_frame(const std::string& bus_name, const struct can_frame& cf) +{ + std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices(); + + if( cd.count(bus_name) == 0) + {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());} + + return cd[bus_name]->tx_send(cf, bus_name); +} + +static int write_raw_frame(const std::string& bus_name, uint32_t can_id, uint8_t can_dlc, struct json_object* can_data) +{ + int rc = 0; + struct can_frame cf; + + ::memset(&cf, 0, sizeof(cf)); + + cf.can_id = can_id; + cf.can_dlc = can_dlc; + + struct json_object *x; + int n = json_object_array_length(can_data); + if(n <= 8) + { + for (int i = 0 ; i < n ; i++) + { + x = json_object_array_get_idx(can_data, i); + cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0; + } + } + + const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name); + if( ! found_device.empty()) + { + rc = send_frame(found_device, cf); + } + + return rc; +} +static int write_signal(const std::string& name, uint64_t value) { int rc = 0; struct can_frame cf; struct utils::signals_found sf; + + ::memset(&cf, 0, sizeof(cf)); + + openxc_DynamicField search_key = build_DynamicField(name); + sf = utils::signals_manager_t::instance().find_signals(search_key); + + if (sf.can_signals.empty()) + { + AFB_WARNING("No signal(s) found for %s. Message not sent.", name.c_str()); + rc = -1; + } + else + { + for(const auto& sig: sf.can_signals) + { + cf = encoder_t::build_frame(sig, value); + const std::string bus_name = sig->get_message()->get_bus_name(); + rc = send_frame(bus_name, cf); + } + } + + return rc; +} + +void write(struct afb_req request) +{ + int rc = 0; struct json_object* args = nullptr, *json_name = nullptr, *json_value = nullptr; - std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices(); - - ::memset(&cf, 0, sizeof(cf)); args = afb_req_json(request); @@ -395,28 +458,10 @@ void swrite(struct afb_req request) (json_object_object_get_ex(json_value, "can_dlc", &json_can_dlc) && (json_object_is_type(json_can_dlc, json_type_double) || json_object_is_type(json_can_dlc, json_type_int))) && (json_object_object_get_ex(json_value, "can_data", &json_can_data) && json_object_is_type(json_can_data, json_type_array) )) { - cf.can_id = json_object_get_int(json_can_id); - cf.can_dlc = (uint8_t)json_object_get_int(json_can_dlc); - - struct json_object *x; - int n = json_object_array_length(json_can_data); - if(n <= 8) - { - for (int i = 0 ; i < n ; i++) - { - x = json_object_array_get_idx(json_can_data, i); - cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0; - } - } - - const std::string bus_name = json_object_get_string(json_name); - const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name); - if( ! found_device.empty()) - { - if( cd.count(bus_name) == 0) - {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());} - rc = cd[bus_name]->tx_send(cf, found_device); - } + write_raw_frame(json_object_get_string(json_name), + json_object_get_int(json_can_id), + (uint8_t)json_object_get_int(json_can_dlc), + json_can_data); } else { @@ -429,25 +474,8 @@ void swrite(struct afb_req request) (json_object_object_get_ex(args, "signal_name", &json_name) && json_object_is_type(json_name, json_type_string)) && (json_object_object_get_ex(args, "signal_value", &json_value) && (json_object_is_type(json_value, json_type_double) || json_object_is_type(json_value, json_type_int)))) { - openxc_DynamicField search_key = build_DynamicField(json_object_get_string(json_name)); - sf = utils::signals_manager_t::instance().find_signals(search_key); - - if (sf.can_signals.empty()) - { - AFB_WARNING("No signal(s) found for id %d. Message not sent.", cf.can_id); - rc = -1; - } - else - { - for(const auto& sig: sf.can_signals) - { - cf = encoder_t::build_frame(sig, (uint64_t)json_object_get_double(json_value)); - const std::string bus_name = sig->get_message()->get_bus_name(); - if( cd.count(bus_name) == 0) - {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());} - rc = cd[bus_name]->tx_send(cf, sig); - } - } + write_signal(json_object_get_string(json_name), + (uint64_t)json_object_get_double(json_value)); } else { diff --git a/low-can-binding/binding/low-can-hat.cpp b/low-can-binding/binding/low-can-hat.cpp index 0fb7e8c..ae16f5d 100644 --- a/low-can-binding/binding/low-can-hat.cpp +++ b/low-can-binding/binding/low-can-hat.cpp @@ -56,7 +56,7 @@ extern "C" { .verb= "auth", .callback= auth, .auth= &afb_auth_perm, .info="Authentification against service to get the required level of confidence", .session= AFB_SESSION_NONE}, { .verb= "subscribe", .callback= subscribe, .auth= NULL, .info="Let subscribe to signals", .session= AFB_SESSION_NONE}, { .verb= "unsubscribe", .callback= unsubscribe, .auth= NULL, .info="Let unsubscribe signals", .session= AFB_SESSION_NONE}, - { .verb= "swrite", .callback= swrite, .auth= &afb_auth_loa_1, .info="Write a single CAN message on a CAN bus", .session= AFB_SESSION_LOA_1}, + { .verb= "write", .callback= write, .auth= &afb_auth_loa_1, .info="Write a single CAN message on a CAN bus", .session= AFB_SESSION_LOA_1}, { .verb= NULL, .callback= NULL, .auth= NULL, .info=NULL, .session= 0} }; diff --git a/low-can-binding/binding/low-can-hat.hpp b/low-can-binding/binding/low-can-hat.hpp index b0f29b5..eafb9c6 100644 --- a/low-can-binding/binding/low-can-hat.hpp +++ b/low-can-binding/binding/low-can-hat.hpp @@ -39,4 +39,4 @@ int read_message(sd_event_source *s, int fd, uint32_t revents, void *userdata); void auth(struct afb_req request); void subscribe(struct afb_req request); void unsubscribe(struct afb_req request); -void swrite(struct afb_req request); +void write(struct afb_req request); |