Change way to convert simplemessage into a json
[apps/agl-service-can-low-level.git] / src / low-can-binding.cpp
index c5fadbe..1812d81 100644 (file)
  * limitations under the License.
  */
 
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <net/if.h>
-#include <linux/can.h>
-#include <linux/can/raw.h>
-#include <fcntl.h>
-#include <systemd/sd-event.h>
-#include <errno.h>
-#include <vector>
-#include <map>
-#include <queue>
-#include <string>
-#include <functional>
-#include <memory>
-#include <thread>
-#include <fstream>
-
-#include <json-c/json.h>
-#include <openxc.pb.h>
-
-#include <afb/afb-binding.h>
-#include <afb/afb-service-itf.h>
-
-#include "obd2.hpp"
-#include "can-utils.hpp"
-#include "can-signals.hpp"
-
-/*
- *     Interface between the daemon and the binding
- */
-static const struct afb_binding_interface *interface;
-static obd2_handler_t obd2_handler();
+#include "low-can-binding.hpp"
 
 /********************************************************************************
 *
@@ -63,11 +30,12 @@ static obd2_handler_t obd2_handler();
 *
 *********************************************************************************/
 
-static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, std::vector<CanSignal>::const_iterator& sig_i)
+static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const CanSignal& sig)
 {
        int ret;
 
-       const auto& ss_i = subscribed_signals.find(sig_i);
+       // TODO: lock the subscribed_signals when insert/remove
+       const auto& ss_i = subscribed_signals.find(sig.genericName);
        if (ss_i != subscribed_signals.end())
        {
                if(!afb_event_is_valid(ss_i->second))
@@ -79,7 +47,7 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
                        }
                        else
                        {
-                               ss_i->second = afb_daemon_make_event(interface->daemon, ss_i->first.genericName);
+                               ss_i->second = afb_daemon_make_event(interface->daemon, ss_i->first.c_str());
                                if (!afb_event_is_valid(ss_i->second)) 
                                {
                                        ERROR(interface, "Can't create an event, something goes wrong.");
@@ -90,7 +58,7 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
        }
        else
        {
-               subscribed_signals[sig_i] = afb_daemon_make_event(interface->daemon, sig_i->genericName);
+               subscribed_signals[sig.genericName] = afb_daemon_make_event(interface->daemon, sig.genericName);
                if (!afb_event_is_valid(ss_i->second)) 
                {
                        ERROR(interface, "Can't create an event, something goes wrong.");
@@ -98,9 +66,9 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
                }
        }
 
-       if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, subscribed_signals[sig_i])) < 0)
+       if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, subscribed_signals[sig.genericName])) < 0)
        {
-               ERROR(interface, "Operation goes wrong for signal: %s", sig_i->genericName);
+               ERROR(interface, "Operation goes wrong for signal: %s", sig.genericName);
                ret = 0;
        }
        else
@@ -111,25 +79,24 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
 
 static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<CanSignal>& signals)
 {
-       int ret;
+       int ret = 0;
 
-       // TODO: lock the subscribed_signals when insert/remove
        for(const auto& signal_i : signals)
        {
                ret = subscribe_unsubscribe_signal(request, subscribe, signal_i);
                if(ret == 0)
                        return ret;
        }
+       return ret;
 }
 
 static int subscribe_unsubscribe_all(struct afb_req request, bool subscribe)
 {
-       int n, e;
+       int e = 0;
 
-       n = obd2_handler.OBD2_PIDS.size();
-       e = 0;
-       for (const auto& pid : obd2_handler.OBD2_PIDS)
-               e += !subscribe_unsubscribe_signals(request, subscribe, pid);
+       //for (const auto& sig : SIGNALS)
+       //      e += !subscribe_unsubscribe_signals(request, subscribe, sig);
+       e += !subscribe_unsubscribe_signals(request, subscribe, getSignals());
        
        return e == 0;
 }
@@ -150,7 +117,8 @@ static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, co
                }
                else
                {
-                       sig = find_can_signals(name);
+                       openxc_DynamicField search_key = build_DynamicField(name);
+                       sig = find_can_signals(interface, search_key);
                        if (sig.empty())
                                ret = 0;
                }
@@ -188,57 +156,60 @@ static void subscribe_unsubscribe(struct afb_req request, bool subscribe)
                afb_req_fail(request, "error", NULL);
 }
 
-static void subscribe(struct afb_req request)
-{
-       subscribe_unsubscribe(request, true);
-}
-
-static void unsubscribe(struct afb_req request)
-{
-       subscribe_unsubscribe(request, false);
-}
-
 static const struct afb_verb_desc_v1 verbs[]=
 {
-       { .name= "subscribe",    .session= AFB_SESSION_NONE, .callback= subscribe,      .info= "subscribe to notification of CAN bus messages." },
-       { .name= "unsubscribe",  .session= AFB_SESSION_NONE, .callback= unsubscribe,    .info= "unsubscribe a previous subscription." }
+       { .name= "subscribe",   .session= AFB_SESSION_NONE, .callback= subscribe,       .info= "subscribe to notification of CAN bus messages." },
+       { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe,     .info= "unsubscribe a previous subscription." }
 };
 
-static const struct afb_binding binding_desc {
-       .type = AFB_BINDING_VERSION_1,
-       .v1 = {
-               .info = "CAN bus service",
-               .prefix = "can",
-               .verbs = verbs
+static const struct afb_binding binding_desc {
+       AFB_BINDING_VERSION_1,
+       {
+               "CAN bus service",
+               "can",
+               verbs
        }
 };
 
-const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
+extern "C"
 {
-       interface = itf;
-
-       return &binding_desc;
-}
+       static void subscribe(struct afb_req request)
+       {
+               subscribe_unsubscribe(request, true);
+       }
 
-/**
- * @brief Initialize the binding.
- * 
- * @param[in] service Structure which represent the Application Framework Binder.
- * 
- * @return Exit code, zero if success.
- */
-int afbBindingV1ServiceInit(struct afb_service service)
-{
-       std::ifstream fd_conf;
-       fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL);
+       static void unsubscribe(struct afb_req request)
+       {
+               subscribe_unsubscribe(request, false);
+       }
 
-       /* Open CAN socket */
-       can_bus_t can_bus_handler(interface, fd_conf);
-       if(can_bus_handler.init_can_dev() == 0)
+       const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
        {
-               can_bus_handler.start_threads();
-               return 0;
+               interface = itf;
+
+               return &binding_desc;
        }
 
-       return 1;
-}
+       /**
+       * @brief Initialize the binding.
+       * 
+       * @param[in] service Structure which represent the Application Framework Binder.
+       * 
+       * @return Exit code, zero if success.
+       */
+       int afbBindingV1ServiceInit(struct afb_service service)
+       {
+               int fd_conf;
+               fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL);
+
+               /* Open CAN socket */
+               can_bus_t can_bus_handler(interface, fd_conf);
+               if(can_bus_handler.init_can_dev() == 0)
+               {
+                       can_bus_handler.start_threads();
+                       return 0;
+               }
+
+               return 1;
+       }
+};
\ No newline at end of file