summaryrefslogtreecommitdiffstats
path: root/src/low-can-binding.cpp
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-03-12 19:48:21 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2017-03-16 17:10:41 +0100
commit7617950b0ab386949b43c6f221b02826e18548d6 (patch)
treebd695ba29cbf87550288734690556af9df6615f7 /src/low-can-binding.cpp
parent6c40a7192cd6ddf89e625b53dd489b7a91a423e1 (diff)
Implemente way to send diagnostic request when subscribed.
When subscribed, the signal is added to recurring request list of diagnostic manager and an event is added to the systemd event loop with timer set using frequency parameter from the requested signal. Change-Id: I4d604c498047d7744c090b7f03fce0f2b427fd01 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src/low-can-binding.cpp')
-rw-r--r--src/low-can-binding.cpp59
1 files changed, 56 insertions, 3 deletions
diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp
index 52e893d..7baf147 100644
--- a/src/low-can-binding.cpp
+++ b/src/low-can-binding.cpp
@@ -72,6 +72,44 @@ static int create_event_handle(const std::string& sig_name, std::map<std::string
return 1;
}
+static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const std::string& sig, DiagnosticRequest* diag_req, int frequency)
+{
+ int ret;
+ sd_event_source *source;
+
+ std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = get_subscribed_signals();
+ if (s.find(sig) != s.end() && !afb_event_is_valid(s[sig]))
+ {
+ if(!subscribe)
+ {
+ NOTICE(binder_interface, "Event isn't valid, it can't be unsubscribed.");
+ ret = -1;
+ }
+ else
+ {
+ /* Event it isn't valid anymore, recreate it */
+ sd_event_add_time(afb_daemon_get_event_loop(binder_interface->daemon), &source, CLOCK_MONOTONIC, frequency, 0,
+ configuration_t::instance().get_diagnostic_manager().send_request, diag_req);
+ ret = create_event_handle(sig, s);
+ }
+ }
+ else
+ {
+ /* Event doesn't exist , so let's create it */
+ struct afb_event empty_event = {nullptr, nullptr};
+ subscribed_signals[sig] = empty_event;
+ ret = create_event_handle(sig, s);
+ }
+
+ /* Check whether or not the event handler has been correctly created and
+ * make the subscription/unsubscription operation is so.
+ */
+ if (ret <= 0)
+ return ret;
+ return make_subscription_unsubscription(request, sig, s, subscribe);
+}
+
static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const std::string& sig)
{
int ret;
@@ -125,10 +163,25 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe,
const std::string& can_prefix = configuration_t::instance().get_can_signals().front().get_prefix();
const std::string& obd2_prefix = configuration_t::instance().get_obd2_signals().front().get_prefix();
- for(auto& sig : signals)
+ for(const std::string& sig : signals)
{
- //if (sig.find_first_of())
- int ret = subscribe_unsubscribe_signal(request, subscribe, sig);
+ int ret;
+ if (sig.find_first_of(can_prefix.c_str(), 0, can_prefix.size()))
+ ret = subscribe_unsubscribe_signal(request, subscribe, sig);
+ else if (sig.find_first_of(obd2_prefix.c_str(), 0, obd2_prefix.size()))
+ {
+ std::vector<obd2_signal_t*> found;
+ configuration_t::instance().find_obd2_signals(build_DynamicField(sig), found);
+ int frequency = found.front()->get_frequency();
+ DiagnosticRequest* diag_req = new DiagnosticRequest(found.front()->build_diagnostic_request());
+ configuration_t::instance().get_diagnostic_manager().add_recurring_request(
+ diag_req, sig.c_str(), false, obd2_signal_t::decode_obd2_response, nullptr, (float)frequency);
+ //TODO: Adding callback requesting ignition status: diag_req, sig.c_str(), false, obd2_signal_t::decode_obd2_response, obd2_signal_t::check_ignition_status, frequency);
+ ret = subscribe_unsubscribe_signal(request, subscribe, sig, diag_req,frequency);
+ }
+ else
+ ret = -1;
+
if(ret <= 0)
return ret;
rets++;