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;
57 ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
61 if(! ctrlConfig->external)
64 application_t *application = (application_t*) ctrlConfig->external;
66 int active_message_set;
67 const char *diagnotic_bus = nullptr;
69 if(wrap_json_unpack(json_obj, "{si, ss}",
70 "active_message_set", &active_message_set,
71 "diagnostic_bus", &diagnotic_bus))
74 application->set_active_message_set((uint8_t)active_message_set);
76 /// Initialize Diagnostic manager that will handle obd2 requests.
77 /// We pass by default the first CAN bus device to its Initialization.
78 if(! application_t::instance().get_diagnostic_manager().initialize(diagnotic_bus))
80 AFB_ERROR("Diagnostic Manager: error at initialization");
87 CtlSectionT ctlSections_[] = {
88 [0]={.key="config" , .uid="config", .info=nullptr,
89 .loadCB=config_low_can,
92 [1]={.key="plugins" , .uid="plugins", .info=nullptr,
98 ///*****************************************************************************
100 /// Subscription and unsubscription
102 ///****************************************************************************/
104 /// @brief This will determine if an event handle needs to be created and checks if
105 /// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription
106 /// against the application framework using that event handle.
107 static int subscribe_unsubscribe_signal(afb_req_t request,
109 std::shared_ptr<low_can_subscription_t>& can_subscription,
113 int sub_index = can_subscription->get_index();
114 bool subscription_exists = s.count(sub_index);
119 /* There is no valid request to subscribe so this must be an
120 * internal permanent diagnostic request. Skip the subscription
121 * part and don't register it into the current "low-can"
129 // Event doesn't exist , so let's create it
130 if ((ret = can_subscription->subscribe(request)) < 0)
133 if(! subscription_exists)
134 s[sub_index] = can_subscription;
139 // Unsubscrition part
140 if(! subscription_exists)
142 AFB_NOTICE("There isn't any valid subscriptions for that request.");
145 else if (subscription_exists &&
146 ! afb_event_is_valid(s[sub_index]->get_event()) )
148 AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
152 if( (ret = s[sub_index]->unsubscribe(request)) < 0)
154 s.find(sub_index)->second->set_index(-1);
159 static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscription)
161 struct sd_event_source* event_source = nullptr;
162 return ( sd_event_add_io(afb_daemon_get_event_loop(),
164 can_subscription->get_socket()->socket(),
167 can_subscription.get()));
170 static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request,
172 list_ptr_diag_msg_t diagnostic_messages,
173 struct event_filter_t& event_filter,
175 bool perm_rec_diag_req)
178 application_t& app = application_t::instance();
179 diagnostic_manager_t& diag_m = app.get_diagnostic_manager();
181 for(const auto& sig : diagnostic_messages)
183 DiagnosticRequest* diag_req = new DiagnosticRequest(sig->build_diagnostic_request());
184 event_filter.frequency = event_filter.frequency == 0 ? sig->get_frequency() : event_filter.frequency;
185 std::shared_ptr<low_can_subscription_t> can_subscription;
187 auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
189 return (! sub.second->get_diagnostic_message().empty());
191 can_subscription = it != s.end() ?
193 std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
194 // If the requested diagnostic message is not supported by the car then unsubcribe it
195 // no matter what we want, worst case will be a failed unsubscription but at least we won't
196 // poll a PID for nothing.
197 if(sig->get_supported() && subscribe)
199 if (!app.is_engine_on())
200 AFB_WARNING("signal: Engine is off, %s won't received responses until it's on", sig->get_name().c_str());
202 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);
203 if(can_subscription->create_rx_filter(sig) < 0)
205 AFB_DEBUG("Signal: %s subscribed", sig->get_name().c_str());
206 if(it == s.end() && add_to_event_loop(can_subscription) < 0)
208 diag_m.cleanup_request(
209 diag_m.find_recurring_request(*diag_req), true);
210 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
216 if(sig->get_supported())
218 AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());
222 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
226 int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription, s);
235 static int subscribe_unsubscribe_signals(afb_req_t request,
237 list_ptr_signal_t signals,
238 struct event_filter_t& event_filter,
242 for(const auto& sig: signals)
244 auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
246 return sub.second->is_signal_subscription_corresponding(sig, event_filter) ;
248 std::shared_ptr<low_can_subscription_t> can_subscription;
250 can_subscription = it->second;
253 can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
254 if(can_subscription->create_rx_filter(sig) < 0)
256 if(add_to_event_loop(can_subscription) < 0)
260 if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
264 AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un");
270 /// @brief subscribe to all signals in the vector signals
272 /// @param[in] afb_req request : contains original request use to subscribe or unsubscribe
273 /// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription
274 /// @param[in] signals - struct containing vectors with signal_t and diagnostic_messages to subscribe
275 /// @param[in] event_filter - stuct containing filter on the signal
277 /// @return Number of correctly subscribed signal
279 static int subscribe_unsubscribe_signals(afb_req_t request,
281 const struct utils::signals_found& signals,
282 struct event_filter_t& event_filter)
285 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
287 std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
288 map_subscription& s = sm.get_subscribed_signals();
290 rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s, false);
291 rets += subscribe_unsubscribe_signals(request, subscribe, signals.signals, event_filter, s);
296 static event_filter_t generate_filter(json_object* args)
298 event_filter_t event_filter;
299 struct json_object *filter, *obj;
301 // computes the filter
302 if (json_object_object_get_ex(args, "filter", &filter))
304 if (json_object_object_get_ex(filter, "frequency", &obj)
305 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
306 event_filter.frequency = (float)json_object_get_double(obj);
307 if (json_object_object_get_ex(filter, "min", &obj)
308 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
309 event_filter.min = (float)json_object_get_double(obj);
310 if (json_object_object_get_ex(filter, "max", &obj)
311 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
312 event_filter.max = (float)json_object_get_double(obj);
313 if (json_object_object_get_ex(filter, "rx_id", &obj)
314 && (json_object_is_type(obj, json_type_int)))
315 event_filter.rx_id = (canid_t) json_object_get_int(obj);
316 if (json_object_object_get_ex(filter, "tx_id", &obj)
317 && (json_object_is_type(obj, json_type_int)))
318 event_filter.tx_id = (canid_t) json_object_get_int(obj);
324 static int one_subscribe_unsubscribe_events(afb_req_t request, bool subscribe, const std::string& tag, json_object* args)
327 struct utils::signals_found sf;
329 // subscribe or unsubscribe
330 openxc_DynamicField search_key = build_DynamicField(tag);
331 sf = utils::signals_manager_t::instance().find_signals(search_key);
334 #ifdef USE_FEATURE_ISOTP
335 if(sf.signals.size() > 1)
337 sf.signals.remove_if([](std::shared_ptr<signal_t> x){
338 bool isotp = x->get_message()->is_isotp();
340 AFB_NOTICE("ISO TP messages need to be subscribed one by one (rx, tx)");
347 if (sf.signals.empty() && sf.diagnostic_messages.empty())
349 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
354 event_filter_t event_filter = generate_filter(args);
355 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
360 static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const uint32_t& id, json_object *args)
363 std::shared_ptr<message_definition_t> message_definition = application_t::instance().get_message_definition(id);
364 struct utils::signals_found sf;
366 if(message_definition)
367 sf.signals = list_ptr_signal_t(message_definition->get_signals().begin(), message_definition->get_signals().end());
369 if(sf.signals.empty())
371 AFB_NOTICE("No signal(s) found for %d.", id);
376 event_filter_t event_filter = generate_filter(args);
377 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
384 static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_object *args)
387 json_object *x = nullptr, *event = nullptr, *id = nullptr;
390 // 2 cases : ID(PGN) and event
392 json_object_object_get_ex(args,"event",&event);
393 json_bool test_id = json_object_object_get_ex(args,"id",&id);
395 test_id = json_object_object_get_ex(args,"pgn",&id);
397 if( args == NULL || (id && ((std::string)json_object_get_string(id)).compare("*") == 0))
399 rc = one_subscribe_unsubscribe_events(request, subscribe, "*", args);
405 if (json_object_get_type(event) != json_type_array) // event is set before and check if it's an array
407 rc = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(event), args);
409 else // event is set and it's not an array
411 for (int i = 0 ; i < json_object_array_length(event); i++)
413 x = json_object_array_get_idx(event, i);
414 rc2 = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(x), args);
416 rc = rc2 >= 0 ? rc + rc2 : rc2;
423 if (json_object_get_type(id) != json_type_array) // id is set before and check if it's an array
425 rc = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(id), args);
427 else // event is set and it's not an array
429 for (int i = 0 ; i < json_object_array_length(id); i++)
431 x = json_object_array_get_idx(id, i);
432 rc2 = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(x), args);
434 rc = rc2 >= 0 ? rc + rc2 : rc2;
442 static void do_subscribe_unsubscribe(afb_req_t request, bool subscribe)
445 struct json_object *args, *x;
447 args = afb_req_json(request);
448 if (json_object_get_type(args) == json_type_array)
450 for(int i = 0; i < json_object_array_length(args); i++)
452 x = json_object_array_get_idx(args, i);
453 rc += process_one_subscribe_args(request, subscribe, x);
458 rc += process_one_subscribe_args(request, subscribe, args);
462 afb_req_success(request, NULL, NULL);
464 afb_req_fail(request, "error", NULL);
467 void auth(afb_req_t request)
469 afb_req_session_set_LOA(request, 1);
470 afb_req_success(request, NULL, NULL);
473 void subscribe(afb_req_t request)
475 do_subscribe_unsubscribe(request, true);
478 void unsubscribe(afb_req_t request)
480 do_subscribe_unsubscribe(request, false);
484 static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, socket_type type)
491 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
493 if( cd.count(bus_name) == 0)
495 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
499 if(type == socket_type::BCM)
501 return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
503 else if(type == socket_type::J1939)
505 return low_can_subscription_t::j1939_send(*cd[bus_name], cfd, bus_name);
512 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)
517 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
519 if( cd.count(bus_name) == 0)
520 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
522 cd[bus_name]->set_signal(signal);
525 if(flags&BCM_PROTOCOL)
526 return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name);
527 #ifdef USE_FEATURE_ISOTP
528 else if(flags&ISOTP_PROTOCOL)
529 return low_can_subscription_t::isotp_send(*cd[bus_name], message, bus_name);
531 #ifdef USE_FEATURE_J1939
532 else if(flags&J1939_PROTOCOL)
533 return low_can_subscription_t::j1939_send(*cd[bus_name], message, bus_name);
540 static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message,
541 struct json_object *can_data, uint32_t flags, event_filter_t &event_filter)
544 struct utils::signals_found sf;
546 utils::signals_manager_t::instance().lookup_signals_by_id(message->get_id(), application_t::instance().get_all_signals(), sf.signals);
548 if( !sf.signals.empty() )
550 AFB_DEBUG("ID WRITE RAW : %d", sf.signals.front()->get_message()->get_id());
551 if(flags & BCM_PROTOCOL)
553 if(sf.signals.front()->get_message()->is_fd())
555 AFB_DEBUG("CANFD_MAX_DLEN");
556 message->set_flags(CAN_FD_FRAME);
557 message->set_maxdlen(CANFD_MAX_DLEN);
561 AFB_DEBUG("CAN_MAX_DLEN");
562 message->set_maxdlen(CAN_MAX_DLEN);
565 if(sf.signals.front()->get_message()->is_isotp())
567 flags = ISOTP_PROTOCOL;
568 message->set_maxdlen(MAX_ISOTP_FRAMES * message->get_maxdlen());
572 #ifdef USE_FEATURE_J1939
573 if(flags&J1939_PROTOCOL)
574 message->set_maxdlen(J1939_MAX_DLEN);
577 if(message->get_length() > 0 && message->get_length() <= message->get_maxdlen())
579 std::vector<uint8_t> data;
580 for (int i = 0 ; i < message->get_length() ; i++)
582 struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
583 data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
584 (uint8_t)json_object_get_int(one_can_data) : 0);
586 message->set_data(data);
590 if(flags&BCM_PROTOCOL)
591 afb_req_fail(request, "Invalid", "Frame BCM");
592 else if(flags&J1939_PROTOCOL)
593 afb_req_fail(request, "Invalid", "Frame J1939");
594 else if(flags&ISOTP_PROTOCOL)
595 afb_req_fail(request, "Invalid", "Frame ISOTP");
597 afb_req_fail(request, "Invalid", "Frame");
602 if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), flags, event_filter, sf.signals.front()))
603 afb_req_success(request, nullptr, "Message correctly sent");
605 afb_req_fail(request, "Error", "sending the message. See the log for more details.");
609 afb_req_fail(request, "Error", "no find id in signals. See the log for more details.");
614 static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value, event_filter_t &event_filter)
619 struct json_object *can_data = nullptr;
620 std::vector<uint8_t> data;
622 AFB_DEBUG("JSON content %s", json_object_get_string(json_value));
624 if(!wrap_json_unpack(json_value, "{si, si, so !}",
627 "can_data", &can_data))
629 message = new can_message_t(0, id, length, false, 0, data, 0);
630 write_raw_frame(request, bus_name, message, can_data, BCM_PROTOCOL, event_filter);
632 #ifdef USE_FEATURE_J1939
633 else if(!wrap_json_unpack(json_value, "{si, si, so !}",
638 message = new j1939_message_t(length, data, 0, J1939_NO_NAME, (pgn_t)id, J1939_NO_ADDR);
639 write_raw_frame(request, bus_name, message, can_data, J1939_PROTOCOL, event_filter);
644 afb_req_fail(request, "Invalid", "Frame object malformed");
650 static void write_signal(afb_req_t request, const std::string& name, json_object *json_value, event_filter_t &event_filter)
652 struct canfd_frame cfd;
653 struct utils::signals_found sf;
654 signal_encoder encoder = nullptr;
657 ::memset(&cfd, 0, sizeof(cfd));
659 openxc_DynamicField search_key = build_DynamicField(name);
660 sf = utils::signals_manager_t::instance().find_signals(search_key);
661 openxc_DynamicField dynafield_value = build_DynamicField(json_value);
663 if (sf.signals.empty())
665 afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
669 std::shared_ptr<signal_t> sig = sf.signals.front();
670 if(! sig->get_writable())
672 afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
676 uint64_t value = (encoder = sig->get_encoder()) ?
677 encoder(*sig, dynafield_value, &send) :
678 encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
680 uint32_t flags = INVALID_FLAG;
682 if(sig->get_message()->is_j1939())
683 flags = J1939_PROTOCOL;
684 else if(sig->get_message()->is_isotp())
685 flags = ISOTP_PROTOCOL;
687 flags = BCM_PROTOCOL;
689 // cfd = encoder_t::build_frame(sig, value);
690 message_t *message = encoder_t::build_message(sig, value, false, false);
692 if(! send_message(message, sig->get_message()->get_bus_device_name(), flags, event_filter, sig) && send)
693 afb_req_success(request, nullptr, "Message correctly sent");
695 afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
697 if(sig->get_message()->is_j1939())
698 #ifdef USE_FEATURE_J1939
699 delete (j1939_message_t*) message;
701 afb_req_fail(request, "Warning", "J1939 not implemented in your kernel.");
704 delete (can_message_t*) message;
707 void write(afb_req_t request)
709 struct json_object* args = nullptr, *json_value = nullptr, *name = nullptr;
710 args = afb_req_json(request);
714 event_filter_t event_filter = generate_filter(args);
716 if(json_object_object_get_ex(args,"bus_name",&name))
718 if(json_object_object_get_ex(args,"frame",&json_value))
719 write_frame(request, (std::string)json_object_get_string(name), json_value, event_filter);
721 afb_req_fail(request, "Error", "Request argument malformed");
723 else if(json_object_object_get_ex(args,"signal_name",&name))
725 if(json_object_object_get_ex(args,"signal_value",&json_value))
726 write_signal(request, (std::string)json_object_get_string(name), json_value, event_filter);
728 afb_req_fail(request, "Error", "Request argument malformed");
732 afb_req_fail(request, "Error", "Request argument malformed");
737 afb_req_fail(request, "Error", "Request argument null");
741 static struct json_object *get_signals_value(const std::string& name)
743 struct utils::signals_found sf;
744 struct json_object *ans = nullptr;
746 openxc_DynamicField search_key = build_DynamicField(name);
747 sf = utils::signals_manager_t::instance().find_signals(search_key);
749 if (sf.signals.empty())
751 AFB_WARNING("No signal(s) found for %s.", name.c_str());
754 ans = json_object_new_array();
755 for(const auto& sig: sf.signals)
757 struct json_object *jobj = json_object_new_object();
758 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
759 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
760 json_object_array_add(ans, jobj);
765 void get(afb_req_t request)
768 struct json_object* args = nullptr,
769 *json_name = nullptr;
770 json_object *ans = nullptr;
772 args = afb_req_json(request);
774 // Process about Raw CAN message on CAN bus directly
775 if (args != nullptr &&
776 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
778 ans = get_signals_value(json_object_get_string(json_name));
784 AFB_ERROR("Request argument malformed. Please use the following syntax:");
789 afb_req_success(request, ans, NULL);
791 afb_req_fail(request, "error", NULL);
795 static struct json_object *list_can_message(const std::string& name)
797 struct utils::signals_found sf;
798 struct json_object *ans = nullptr;
800 openxc_DynamicField search_key = build_DynamicField(name);
801 sf = utils::signals_manager_t::instance().find_signals(search_key);
803 if (sf.signals.empty() && sf.diagnostic_messages.empty())
805 AFB_WARNING("No signal(s) found for %s.", name.c_str());
808 ans = json_object_new_array();
809 for(const auto& sig: sf.signals)
811 json_object_array_add(ans,
812 json_object_new_string(sig->get_name().c_str()));
814 for(const auto& sig: sf.diagnostic_messages)
816 json_object_array_add(ans,
817 json_object_new_string(sig->get_name().c_str()));
823 void list(afb_req_t request)
826 json_object *ans = nullptr;
827 struct json_object* args = nullptr,
828 *json_name = nullptr;
829 args = afb_req_json(request);
831 if ((args != nullptr) &&
832 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
833 name = json_object_get_string(json_name);
837 ans = list_can_message(name);
842 afb_req_success(request, ans, NULL);
844 afb_req_fail(request, "error", NULL);
848 /// @brief Initialize the binding.
850 /// @param[in] service Structure which represent the Application Framework Binder.
852 /// @return Exit code, zero if success.
853 int init_binding(afb_api_t api)
856 application_t& application = application_t::instance();
857 can_bus_t& can_bus_manager = application.get_can_bus_manager();
859 if(application.get_message_set().empty())
861 AFB_ERROR("No message_set defined");
866 can_bus_manager.set_can_devices();
867 can_bus_manager.start_threads();
868 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
870 // Add a recurring dignostic message request to get engine speed at all times.
871 openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
872 struct utils::signals_found sf = sm.find_signals(search_key);
874 if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
876 afb_req_t request = nullptr;
878 struct event_filter_t event_filter;
879 event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
881 map_subscription& s = sm.get_subscribed_signals();
883 subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
887 #ifdef USE_FEATURE_J1939
888 std::string j1939_bus;
889 vect_ptr_msg_def_t current_messages_definition = application.get_messages_definition();
890 for(std::shared_ptr<message_definition_t> message_definition: current_messages_definition)
892 if(message_definition->is_j1939())
894 if (j1939_bus == message_definition->get_bus_device_name() )
896 j1939_bus = message_definition->get_bus_device_name();
898 std::shared_ptr<low_can_subscription_t> low_can_j1939 = std::make_shared<low_can_subscription_t>();
899 application.set_subscription_address_claiming(low_can_j1939);
901 ret = low_can_subscription_t::open_socket(*low_can_j1939,
903 J1939_ADDR_CLAIM_PROTOCOL);
907 AFB_ERROR("Error open socket address claiming for j1939 protocol");
910 add_to_event_loop(low_can_j1939);
917 AFB_ERROR("There was something wrong with CAN device Initialization.");
922 int load_config(afb_api_t api)
925 CtlConfigT *ctlConfig;
926 const char *dirList = getenv("CONTROL_CONFIG_PATH");
927 std::string bindingDirPath = GetBindingDirPath(api);
928 std::string filepath = bindingDirPath + "/etc";
931 dirList=CONTROL_CONFIG_PATH;
933 filepath.append(":");
934 filepath.append(dirList);
935 const char *configPath = CtlConfigSearch(api, filepath.c_str(), "control");
939 AFB_ERROR_V3("CtlPreInit: No control-* config found invalid JSON %s ", filepath.c_str());
943 // create one API per file
944 ctlConfig = CtlLoadMetaData(api, configPath);
947 AFB_ERROR_V3("CtrlPreInit No valid control config file in:\n-- %s", configPath);
951 // Save the config in the api userdata field
952 afb_api_set_userdata(api, ctlConfig);
954 setExternalData(ctlConfig, (void*) &application_t::instance());
955 ret= CtlLoadSections(api, ctlConfig, ctlSections_);