aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-02-15 16:44:59 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-02-15 16:45:40 +0000
commit54595b88520c3caea40fac61ed79f2dcb57b7062 (patch)
treee9bfd9082af968e1e16667cb49b7aaa99d40529b
parent914a47b69953ec321e35109841f2564286224631 (diff)
Fixed subscribe/unsubscribe operations.
Change-Id: I79c2f32801bf72e12b3e2da45e2eb2939cb5f85e Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--src/can-signals.cpp35
-rw-r--r--src/low-can-binding.cpp36
2 files changed, 52 insertions, 19 deletions
diff --git a/src/can-signals.cpp b/src/can-signals.cpp
index 7af957f9..8e3a1e69 100644
--- a/src/can-signals.cpp
+++ b/src/can-signals.cpp
@@ -17,27 +17,31 @@
#include <string>
#include <vector>
+#include <fnmatch.h>
#include "can-signals.h"
-bool match(const std::string &first, const std::string &second)
-{
-
-}
+/* Can signal event map making access to afb_event
+ * external to openxc existing structure.
+ */
+std::map <CanSignal, struct afb_event> subscribed_signals;
+std::map <CanSignal, struct afb_event>::iterator subscribed_signals_i;
-CanSignal* getSignals(openxc_DynamicField *key)
+/* Find one or many signals based on its name or id
+* passed through openxc_DynamicField.
+*/
+void find_signals(openxc_DynamicField *key, std:vector <CanSignal> *signals)
{
int n_signals, i;
- std::vector <CanSignal> ret_signals;
- n_signals = SIGNALS.size();
+ n_signals = getSignalCount();
switch(key->type):
{
case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
for(i=0; i<=n_signals; i++)
{
- if(match((std::string*)key->string_value, (std::string*)SIGNALS[i]->genericName))
- ret_signals.insert(&SIGNALS[i]);
+ if(fnmatch(key->string_value, SIGNALS[i]->genericName) == 0)
+ signals.insert(&SIGNALS[i]);
}
break;
case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
@@ -45,13 +49,22 @@ CanSignal* getSignals(openxc_DynamicField *key)
{
CanMessageDefinition *msg_def = SIGNALS[i]->message;
if(msg_def->id == key->numeric_value)
- ret_signals.insert(&SIGNALS[i])
+ signals.insert(&SIGNALS[i])
}
break;
default:
+ ERROR(interface, "find_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
return NULL;
break;
}
+}
- return &ret_signals;
+CanSignal* getSignals()
+{
+ return &SIGNALS;
+}
+
+int getSignalCount()
+{
+ return SIGNALS.size();
} \ No newline at end of file
diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp
index b6098907..af86786d 100644
--- a/src/low-can-binding.cpp
+++ b/src/low-can-binding.cpp
@@ -121,22 +121,42 @@ static int connect_to_event_loop(CanBus &CanBus_handler)
*
*********************************************************************************/
-static int subscribe_unsubscribe_sig(struct afb_req request, int subscribe, struct signal *sig)
-{
- if (!afb_event_is_valid(sig->event)) {
+static int subscribe_unsubscribe_signal(struct afb_req request, int subscribe, std::map<CanSignal, struct afb_event>::iterator *s_sig)
+ {
+ if (!afb_event_is_valid(s_sig->second)) {
if (!subscribe)
return 1;
- sig->event = afb_daemon_make_event(afbitf->daemon, sig->name);
- if (!afb_event_is_valid(sig->event)) {
+ sig->event = afb_daemon_make_event(afbitf->daemon, s_sig->first->genericName);
+ if (!afb_event_is_valid(s_sig->second)) {
return 0;
}
}
- if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, sig->event)) < 0) {
+ if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s_sig->second)) < 0) {
return 0;
}
return 1;
+ }
+
+static int subscribe_unsubscribe_signals(struct afb_req request, int subscribe, std:vector<CanSignal> *signals)
+{
+ std::vector<CanSignal>::iterator signal_i;
+ std::map <CanSignal, struct afb_event>::iterator s_signal_i;
+
+ for(signal_i=signals.begin(); signal_i != signals.end(); signal_i++)
+ {
+ s_signal_i = subscribed_signals.find(signal_i);
+ if(s_signal_i != subscribed_signals.end())
+ subscribe_unsubscribe_signal(request, subscribe, s_signal_i);
+ else
+ {
+ std::map <CanSignal, struct afb_event>::iterator it = subscribed_signals.begin();
+ it = subscribed_signals.insert(it, std::pair<CanSignal, struct afb_event>(signal_i, NULL);
+ subscribe_unsubscribe_signal(request, subscribe, it);
+ }
+ return 0;
+ }
}
static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
@@ -152,12 +172,12 @@ static int subscribe_unsubscribe_all(struct afb_req request, int subscribe)
static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name)
{
- struct signal *sig;
+ std::vector <CanSignal> *sig;
if (0 == strcmp(name, "*"))
return subscribe_unsubscribe_all(request, subscribe);
- sig = getSignal(name);
+ find_signals(name, sig);
if (sig == NULL) {
return 0;
}