2 * Copyright (C) 2015, 2018 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loic Collignon" <loic.collignon@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "low-can-hat.hpp"
20 #include "low-can-apidef.h"
27 #include <wrap-json.h>
28 #include <systemd/sd-event.h>
29 #include <ctl-config.h>
30 #include "openxc.pb.h"
31 #include "application.hpp"
32 #include "../can/can-encoder.hpp"
33 #include "../can/can-bus.hpp"
34 #include "../can/signals.hpp"
35 #include "../can/message/message.hpp"
36 #include "../utils/signals.hpp"
37 #include "../diagnostic/diagnostic-message.hpp"
38 #include "../utils/openxc-utils.hpp"
39 #include "../utils/signals.hpp"
41 #ifdef USE_FEATURE_J1939
42 #include "../can/message/j1939-message.hpp"
43 #include <linux/can/j1939.h>
46 ///*****************************************************************************
48 /// Controller Definitions and Callbacks
50 ///****************************************************************************/
52 int config_low_can(afb_api_t apiHandle, CtlSectionT *section, json_object *json_obj)
54 AFB_DEBUG("Config %s", json_object_to_json_string(json_obj));
55 CtlConfigT *ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
56 int active_message_set = 0;
57 json_object *dev_mapping = nullptr;
58 const char *diagnotic_bus = nullptr;
60 if(! ctrlConfig || ! ctrlConfig->external)
63 application_t *application = (application_t*) ctrlConfig->external;
65 if(wrap_json_unpack(json_obj, "{si, ss}",
66 "active_message_set", &active_message_set,
67 "diagnostic_bus", &diagnotic_bus))
70 application->set_active_message_set((uint8_t)active_message_set);
72 if(wrap_json_unpack(json_obj, "{so}",
73 "dev-mapping", &dev_mapping))
76 application->get_can_bus_manager().set_can_devices(dev_mapping);
78 /// Initialize Diagnostic manager that will handle obd2 requests.
79 /// We pass by default the first CAN bus device to its Initialization.
80 if(! application_t::instance().get_diagnostic_manager().initialize(diagnotic_bus))
82 AFB_ERROR("Diagnostic Manager: error at initialization");
89 CtlSectionT ctlSections_[] = {
90 [0]={.key="config" , .uid="config", .info=nullptr, .prefix=nullptr,
91 .loadCB=config_low_can,
94 [1]={.key="plugins" , .uid="plugins", .info=nullptr, .prefix=nullptr,
98 [2]={.key=nullptr , .uid=nullptr, .info=nullptr, .prefix=nullptr,
104 ///*****************************************************************************
106 /// Subscription and unsubscription
108 ///****************************************************************************/
110 /// @brief This will determine if an event handle needs to be created and checks if
111 /// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription
112 /// against the application framework using that event handle.
113 static int subscribe_unsubscribe_signal(afb_req_t request,
115 std::shared_ptr<low_can_subscription_t>& can_subscription,
119 int sub_index = can_subscription->get_index();
120 bool subscription_exists = s.count(sub_index);
125 /* There is no valid request to subscribe so this must be an
126 * internal permanent diagnostic request. Skip the subscription
127 * part and don't register it into the current "low-can"
135 // Event doesn't exist , so let's create it
136 if ((ret = can_subscription->subscribe(request)) < 0)
139 if(! subscription_exists)
140 s[sub_index] = can_subscription;
145 // Unsubscrition part
146 if(! subscription_exists)
148 AFB_NOTICE("There isn't any valid subscriptions for that request.");
151 else if (subscription_exists &&
152 ! afb_event_is_valid(s[sub_index]->get_event()) )
154 AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
158 if( (ret = s[sub_index]->unsubscribe(request)) < 0)
160 s.find(sub_index)->second->set_index(-1);
165 static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscription)
167 struct sd_event_source* event_source = nullptr;
168 return ( sd_event_add_io(afb_daemon_get_event_loop(),
170 can_subscription->get_socket()->socket(),
173 can_subscription.get()));
176 static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request,
178 list_ptr_diag_msg_t diagnostic_messages,
179 struct event_filter_t& event_filter,
181 bool perm_rec_diag_req)
184 application_t& app = application_t::instance();
185 diagnostic_manager_t& diag_m = app.get_diagnostic_manager();
187 for(const auto& sig : diagnostic_messages)
189 DiagnosticRequest* diag_req = new DiagnosticRequest(sig->build_diagnostic_request());
190 event_filter.frequency = event_filter.frequency == 0 ? sig->get_frequency() : event_filter.frequency;
191 std::shared_ptr<low_can_subscription_t> can_subscription;
193 auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
195 return (! sub.second->get_diagnostic_message().empty());
197 can_subscription = it != s.end() ?
199 std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
200 // If the requested diagnostic message is not supported by the car then unsubcribe it
201 // no matter what we want, worst case will be a failed unsubscription but at least we won't
202 // poll a PID for nothing.
203 if(sig->get_supported() && subscribe)
205 if (!app.is_engine_on())
206 AFB_WARNING("signal: Engine is off, %s won't received responses until it's on", sig->get_name().c_str());
208 diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), event_filter.frequency, perm_rec_diag_req);
209 if(can_subscription->create_rx_filter(sig) < 0)
211 AFB_DEBUG("Signal: %s subscribed", sig->get_name().c_str());
212 if(it == s.end() && add_to_event_loop(can_subscription) < 0)
214 diag_m.cleanup_request(
215 diag_m.find_recurring_request(*diag_req), true);
216 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
222 if(sig->get_supported())
224 AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());
228 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
232 int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription, s);
241 static int subscribe_unsubscribe_signals(afb_req_t request,
243 list_ptr_signal_t signals,
244 struct event_filter_t& event_filter,
248 for(const auto& sig: signals)
250 auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
252 return sub.second->is_signal_subscription_corresponding(sig, event_filter) ;
254 std::shared_ptr<low_can_subscription_t> can_subscription;
256 can_subscription = it->second;
259 can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
260 if(can_subscription->create_rx_filter(sig) < 0)
262 if(add_to_event_loop(can_subscription) < 0)
266 if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
270 AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un");
276 /// @brief subscribe to all signals in the vector signals
278 /// @param[in] afb_req request : contains original request use to subscribe or unsubscribe
279 /// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription
280 /// @param[in] signals - struct containing vectors with signal_t and diagnostic_messages to subscribe
281 /// @param[in] event_filter - stuct containing filter on the signal
283 /// @return Number of correctly subscribed signal
285 static int subscribe_unsubscribe_signals(afb_req_t request,
287 const struct utils::signals_found& signals,
288 struct event_filter_t& event_filter)
291 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
293 std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
294 map_subscription& s = sm.get_subscribed_signals();
296 rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s, false);
297 rets += subscribe_unsubscribe_signals(request, subscribe, signals.signals, event_filter, s);
302 static event_filter_t generate_filter(json_object* args)
304 event_filter_t event_filter;
305 struct json_object *filter, *obj;
307 // computes the filter
308 if (json_object_object_get_ex(args, "filter", &filter))
310 if (json_object_object_get_ex(filter, "frequency", &obj)
311 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
312 event_filter.frequency = (float)json_object_get_double(obj);
313 if (json_object_object_get_ex(filter, "min", &obj)
314 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
315 event_filter.min = (float)json_object_get_double(obj);
316 if (json_object_object_get_ex(filter, "max", &obj)
317 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
318 event_filter.max = (float)json_object_get_double(obj);
319 if (json_object_object_get_ex(filter, "rx_id", &obj)
320 && (json_object_is_type(obj, json_type_int)))
321 event_filter.rx_id = (canid_t) json_object_get_int(obj);
322 if (json_object_object_get_ex(filter, "tx_id", &obj)
323 && (json_object_is_type(obj, json_type_int)))
324 event_filter.tx_id = (canid_t) json_object_get_int(obj);
330 static int one_subscribe_unsubscribe_events(afb_req_t request, bool subscribe, const std::string& tag, json_object* args)
333 struct utils::signals_found sf;
335 // subscribe or unsubscribe
336 openxc_DynamicField search_key = build_DynamicField(tag);
337 sf = utils::signals_manager_t::instance().find_signals(search_key);
340 #ifdef USE_FEATURE_ISOTP
341 if(sf.signals.size() > 1)
343 sf.signals.remove_if([](std::shared_ptr<signal_t> x){
344 bool isotp = x->get_message()->is_isotp();
346 AFB_NOTICE("ISO TP messages need to be subscribed one by one (rx, tx)");
353 if (sf.signals.empty() && sf.diagnostic_messages.empty())
355 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
360 event_filter_t event_filter = generate_filter(args);
361 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
366 static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const uint32_t& id, json_object *args)
369 std::shared_ptr<message_definition_t> message_definition = application_t::instance().get_message_definition(id);
370 struct utils::signals_found sf;
372 if(message_definition)
373 sf.signals = list_ptr_signal_t(message_definition->get_signals().begin(), message_definition->get_signals().end());
375 if(sf.signals.empty())
377 AFB_NOTICE("No signal(s) found for %d.", id);
382 event_filter_t event_filter = generate_filter(args);
383 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
390 static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_object *args)
393 json_object *x = nullptr, *event = nullptr, *id = nullptr;
396 // 2 cases : ID(PGN) and event
398 json_object_object_get_ex(args,"event",&event);
399 json_bool test_id = json_object_object_get_ex(args,"id",&id);
401 test_id = json_object_object_get_ex(args,"pgn",&id);
403 if( args == NULL || (id && ((std::string)json_object_get_string(id)).compare("*") == 0))
405 rc = one_subscribe_unsubscribe_events(request, subscribe, "*", args);
411 if (json_object_get_type(event) != json_type_array) // event is set before and check if it's an array
413 rc = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(event), args);
415 else // event is set and it's not an array
417 for (int i = 0 ; i < json_object_array_length(event); i++)
419 x = json_object_array_get_idx(event, i);
420 rc2 = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(x), args);
422 rc = rc2 >= 0 ? rc + rc2 : rc2;
429 if (json_object_get_type(id) != json_type_array) // id is set before and check if it's an array
431 rc = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(id), args);
433 else // event is set and it's not an array
435 for (int i = 0 ; i < json_object_array_length(id); i++)
437 x = json_object_array_get_idx(id, i);
438 rc2 = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(x), args);
440 rc = rc2 >= 0 ? rc + rc2 : rc2;
448 static void do_subscribe_unsubscribe(afb_req_t request, bool subscribe)
451 struct json_object *args, *x;
453 args = afb_req_json(request);
454 if (json_object_get_type(args) == json_type_array)
456 for(int i = 0; i < json_object_array_length(args); i++)
458 x = json_object_array_get_idx(args, i);
459 rc += process_one_subscribe_args(request, subscribe, x);
464 rc += process_one_subscribe_args(request, subscribe, args);
468 afb_req_success(request, NULL, NULL);
470 afb_req_fail(request, "error", NULL);
473 void auth(afb_req_t request)
475 afb_req_session_set_LOA(request, 1);
476 afb_req_success(request, NULL, NULL);
479 void subscribe(afb_req_t request)
481 do_subscribe_unsubscribe(request, true);
484 void unsubscribe(afb_req_t request)
486 do_subscribe_unsubscribe(request, false);
490 static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, socket_type type)
497 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
499 if( cd.count(bus_name) == 0)
501 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
505 if(type == socket_type::BCM)
507 return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
509 else if(type == socket_type::J1939)
511 return low_can_subscription_t::j1939_send(*cd[bus_name], cfd, bus_name);
518 static int send_message(message_t *message, const std::string& bus_name, uint32_t flags, event_filter_t &event_filter, std::shared_ptr<signal_t> signal)
523 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
525 if( cd.count(bus_name) == 0)
526 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
528 cd[bus_name]->set_signal(signal);
531 if(flags&CAN_PROTOCOL)
532 return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name);
533 #ifdef USE_FEATURE_ISOTP
534 else if(flags&ISOTP_PROTOCOL)
535 return low_can_subscription_t::isotp_send(*cd[bus_name], message, bus_name);
537 #ifdef USE_FEATURE_J1939
538 else if(flags&J1939_PROTOCOL)
539 return low_can_subscription_t::j1939_send(*cd[bus_name], message, bus_name);
546 static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message,
547 struct json_object *can_data, uint32_t flags, event_filter_t &event_filter)
550 struct utils::signals_found sf;
552 utils::signals_manager_t::instance().lookup_signals_by_id(message->get_id(), application_t::instance().get_all_signals(), sf.signals);
554 if( !sf.signals.empty() )
556 AFB_DEBUG("ID WRITE RAW : %d", sf.signals.front()->get_message()->get_id());
557 if(flags & CAN_PROTOCOL)
559 if(sf.signals.front()->get_message()->is_fd())
561 AFB_DEBUG("CANFD_MAX_DLEN");
562 message->set_flags(CAN_PROTOCOL_WITH_FD_FRAME);
563 message->set_maxdlen(CANFD_MAX_DLEN);
567 AFB_DEBUG("CAN_MAX_DLEN");
568 message->set_maxdlen(CAN_MAX_DLEN);
571 if(sf.signals.front()->get_message()->is_isotp())
573 flags = ISOTP_PROTOCOL;
574 message->set_maxdlen(MAX_ISOTP_FRAMES * message->get_maxdlen());
578 #ifdef USE_FEATURE_J1939
579 if(flags&J1939_PROTOCOL)
580 message->set_maxdlen(J1939_MAX_DLEN);
583 if(message->get_length() > 0 && message->get_length() <= message->get_maxdlen())
585 std::vector<uint8_t> data;
586 for (int i = 0 ; i < message->get_length() ; i++)
588 struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
589 data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
590 (uint8_t)json_object_get_int(one_can_data) : 0);
592 message->set_data(data);
596 if(flags&CAN_PROTOCOL)
597 afb_req_fail(request, "Invalid", "Frame BCM");
598 else if(flags&J1939_PROTOCOL)
599 afb_req_fail(request, "Invalid", "Frame J1939");
600 else if(flags&ISOTP_PROTOCOL)
601 afb_req_fail(request, "Invalid", "Frame ISOTP");
603 afb_req_fail(request, "Invalid", "Frame");
608 if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), flags, event_filter, sf.signals.front()))
609 afb_req_success(request, nullptr, "Message correctly sent");
611 afb_req_fail(request, "Error", "sending the message. See the log for more details.");
615 afb_req_fail(request, "Error", "no find id in signals. See the log for more details.");
620 static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value, event_filter_t &event_filter)
625 struct json_object *can_data = nullptr;
626 std::vector<uint8_t> data;
628 AFB_DEBUG("JSON content %s", json_object_get_string(json_value));
630 if(!wrap_json_unpack(json_value, "{si, si, so !}",
633 "can_data", &can_data))
635 message = new can_message_t(0, id, length, false, 0, data, 0);
636 write_raw_frame(request, bus_name, message, can_data, CAN_PROTOCOL, event_filter);
638 #ifdef USE_FEATURE_J1939
639 else if(!wrap_json_unpack(json_value, "{si, si, so !}",
644 message = new j1939_message_t(length, data, 0, J1939_NO_NAME, (pgn_t)id, J1939_NO_ADDR);
645 write_raw_frame(request, bus_name, message, can_data, J1939_PROTOCOL, event_filter);
650 afb_req_fail(request, "Invalid", "Frame object malformed");
656 static void write_signal(afb_req_t request, const std::string& name, json_object *json_value, event_filter_t &event_filter)
658 struct canfd_frame cfd;
659 struct utils::signals_found sf;
660 signal_encoder encoder = nullptr;
663 ::memset(&cfd, 0, sizeof(cfd));
665 openxc_DynamicField search_key = build_DynamicField(name);
666 sf = utils::signals_manager_t::instance().find_signals(search_key);
667 openxc_DynamicField dynafield_value = build_DynamicField(json_value);
669 if (sf.signals.empty())
671 afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
675 std::shared_ptr<signal_t> sig = sf.signals.front();
676 if(! sig->get_writable())
678 afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
682 uint64_t value = (encoder = sig->get_encoder()) ?
683 encoder(*sig, dynafield_value, &send) :
684 encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
686 uint32_t flags = INVALID_FLAG;
688 if(sig->get_message()->is_j1939())
689 flags = J1939_PROTOCOL;
690 else if(sig->get_message()->is_isotp())
691 flags = ISOTP_PROTOCOL;
693 flags = CAN_PROTOCOL;
695 // cfd = encoder_t::build_frame(sig, value);
696 message_t *message = encoder_t::build_message(sig, value, false, false);
698 if(! send_message(message, sig->get_message()->get_bus_device_name(), flags, event_filter, sig) && send)
699 afb_req_success(request, nullptr, "Message correctly sent");
701 afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
703 if(sig->get_message()->is_j1939())
704 #ifdef USE_FEATURE_J1939
705 delete (j1939_message_t*) message;
707 afb_req_fail(request, "Warning", "J1939 not implemented in your kernel.");
710 delete (can_message_t*) message;
713 void write(afb_req_t request)
715 struct json_object* args = nullptr, *json_value = nullptr, *name = nullptr;
716 args = afb_req_json(request);
720 event_filter_t event_filter = generate_filter(args);
722 if(json_object_object_get_ex(args,"bus_name",&name))
724 if(json_object_object_get_ex(args,"frame",&json_value))
725 write_frame(request, (std::string)json_object_get_string(name), json_value, event_filter);
727 afb_req_fail(request, "Error", "Request argument malformed");
729 else if(json_object_object_get_ex(args,"signal_name",&name))
731 if(json_object_object_get_ex(args,"signal_value",&json_value))
732 write_signal(request, (std::string)json_object_get_string(name), json_value, event_filter);
734 afb_req_fail(request, "Error", "Request argument malformed");
738 afb_req_fail(request, "Error", "Request argument malformed");
743 afb_req_fail(request, "Error", "Request argument null");
747 static struct json_object *get_signals_value(const std::string& name)
749 struct utils::signals_found sf;
750 struct json_object *ans = nullptr;
752 openxc_DynamicField search_key = build_DynamicField(name);
753 sf = utils::signals_manager_t::instance().find_signals(search_key);
755 if (sf.signals.empty())
757 AFB_WARNING("No signal(s) found for %s.", name.c_str());
760 ans = json_object_new_array();
761 for(const auto& sig: sf.signals)
763 struct json_object *jobj = json_object_new_object();
764 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
765 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
766 json_object_array_add(ans, jobj);
771 void get(afb_req_t request)
774 struct json_object* args = nullptr,
775 *json_name = nullptr;
776 json_object *ans = nullptr;
778 args = afb_req_json(request);
780 // Process about Raw CAN message on CAN bus directly
781 if (args != nullptr &&
782 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
784 ans = get_signals_value(json_object_get_string(json_name));
790 AFB_ERROR("Request argument malformed. Please use the following syntax:");
795 afb_req_success(request, ans, NULL);
797 afb_req_fail(request, "error", NULL);
801 static struct json_object *list_can_message(const std::string& name)
803 struct utils::signals_found sf;
804 struct json_object *ans = nullptr;
806 openxc_DynamicField search_key = build_DynamicField(name);
807 sf = utils::signals_manager_t::instance().find_signals(search_key);
809 if (sf.signals.empty() && sf.diagnostic_messages.empty())
811 AFB_WARNING("No signal(s) found for %s.", name.c_str());
814 ans = json_object_new_array();
815 for(const auto& sig: sf.signals)
817 json_object_array_add(ans,
818 json_object_new_string(sig->get_name().c_str()));
820 for(const auto& sig: sf.diagnostic_messages)
822 json_object_array_add(ans,
823 json_object_new_string(sig->get_name().c_str()));
829 void list(afb_req_t request)
832 json_object *ans = nullptr;
833 struct json_object* args = nullptr,
834 *json_name = nullptr;
835 args = afb_req_json(request);
837 if ((args != nullptr) &&
838 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
839 name = json_object_get_string(json_name);
843 ans = list_can_message(name);
848 afb_req_success(request, ans, NULL);
850 afb_req_fail(request, "error", NULL);
853 /// @brief Initialize the binding.
855 /// @param[in] service Structure which represent the Application Framework Binder.
857 /// @return Exit code, zero if success.
858 int init_binding(afb_api_t api)
861 application_t& application = application_t::instance();
862 can_bus_t& can_bus_manager = application.get_can_bus_manager();
864 if(application.get_message_set().empty())
866 AFB_ERROR("No message_set defined");
870 can_bus_manager.start_threads();
871 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
873 // Add a recurring dignostic message request to get engine speed at all times.
874 openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
875 struct utils::signals_found sf = sm.find_signals(search_key);
877 if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
879 afb_req_t request = nullptr;
881 struct event_filter_t event_filter;
882 event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
884 map_subscription& s = sm.get_subscribed_signals();
886 subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
890 #ifdef USE_FEATURE_J1939
891 std::string j1939_bus;
892 vect_ptr_msg_def_t current_messages_definition = application.get_messages_definition();
893 for(std::shared_ptr<message_definition_t> message_definition: current_messages_definition)
895 if(message_definition->is_j1939())
897 if (j1939_bus == message_definition->get_bus_device_name() )
899 j1939_bus = message_definition->get_bus_device_name();
901 std::shared_ptr<low_can_subscription_t> low_can_j1939 = std::make_shared<low_can_subscription_t>();
902 application.set_subscription_address_claiming(low_can_j1939);
904 ret = low_can_subscription_t::open_socket(*low_can_j1939,
906 J1939_ADDR_CLAIM_PROTOCOL);
910 AFB_ERROR("Error open socket address claiming for j1939 protocol");
913 add_to_event_loop(low_can_j1939);
920 AFB_ERROR("There was something wrong with CAN device Initialization.");
925 int load_config(afb_api_t api)
928 CtlConfigT *ctlConfig;
929 const char *dirList = getenv("CONTROL_CONFIG_PATH");
930 std::string bindingDirPath = GetBindingDirPath(api);
931 std::string filepath = bindingDirPath + "/etc";
934 dirList=CONTROL_CONFIG_PATH;
936 filepath.append(":");
937 filepath.append(dirList);
938 const char *configPath = CtlConfigSearch(api, filepath.c_str(), "control");
942 AFB_ERROR_V3("CtlPreInit: No control-* config found invalid JSON %s ", filepath.c_str());
946 // create one API per file
947 ctlConfig = CtlLoadMetaData(api, configPath);
950 AFB_ERROR_V3("CtrlPreInit No valid control config file in:\n-- %s", configPath);
954 // Save the config in the api userdata field
955 afb_api_set_userdata(api, ctlConfig);
957 setExternalData(ctlConfig, (void*) &application_t::instance());
958 ret= CtlLoadSections(api, ctlConfig, ctlSections_);