Make template from similar writing stream operations.
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / low-can-binding.cpp
index 0244beb..e6af92c 100644 (file)
@@ -84,6 +84,9 @@ static int create_event_handle(const std::string& sig_name, std::map<std::string
        return 1;
 }
 
+/// @brief Will determine if it is needed or not to create the event handle and checks it to be sure that
+/// we got a valid afb_event to get subscribe or unsubscribe. Then launch the subscription or unsubscription
+/// against the application framework using that event handle.
 static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const std::string& sig)
 {
        int ret;
@@ -96,14 +99,14 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
        {
                if (!afb_event_is_valid(s[sig]) && !subscribe)
                {
-                       NOTICE(binder_interface, "%s: Event isn't valid, it can't be unsubscribed.", __FUNCTION__);
+                       NOTICE(binder_interface, "%s: Event isn't valid, no need to unsubscribed.", __FUNCTION__);
                        ret = -1;
                }
-               /*else
+               else
                {
                        // Event it isn't valid annymore, recreate it
                        ret = create_event_handle(sig, s);
-               }*/
+               }
        }
        else
        {
@@ -113,21 +116,19 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
                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.
-        */
+       // 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);
 }
 
 ///
-/// @fn static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<can_signal_t>& signals)
 /// @brief subscribe to all signals in the vector signals
 ///
 /// @param[in] afb_req request : contain original request use to subscribe or unsubscribe
 /// @param[in] subscribe boolean value used to chose between a subscription operation or an unsubscription
-/// @param[in] can_signal_t  vector with can_signal_t to subscribe
+/// @param[in] signals -  struct containing vectors with can_signal_t and diagnostic_messages to subscribe
 ///
 /// @return Number of correctly subscribed signal
 ///
@@ -141,22 +142,23 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe,
 
        for(const auto& sig : signals.diagnostic_messages)
        {
+               diagnostic_manager_t& diag_m = conf.get_diagnostic_manager();
                DiagnosticRequest* diag_req = conf.get_request_from_diagnostic_message(sig->get_name());
 
-               // If the requested diagnostic message isn't supported by the car then unssubcribe.
+               // If the requested diagnostic message isn't supported by the car then unsubcribe it
                // no matter what we want, worse case will be a fail unsubscription but at least we don't
                // poll a PID for nothing.
                if(sig->get_supported() && subscribe)
                {
                                float frequency = sig->get_frequency();
-                               subscribe = conf.get_diagnostic_manager().add_recurring_request(
+                               subscribe = diag_m.add_recurring_request(
                                        diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), (float)frequency);
                                        //TODO: Adding callback requesting ignition status:     diag_req, sig.c_str(), false, diagnostic_message_t::decode_obd2_response, diagnostic_message_t::check_ignition_status, frequency);
                }
                else
                {
-                       conf.get_diagnostic_manager().cleanup_request(
-                               conf.get_diagnostic_manager().find_recurring_request(diag_req), true);
+                       diag_m.cleanup_request(
+                               diag_m.find_recurring_request(diag_req), true);
                        WARNING(binder_interface, "%s: signal: %s isn't supported. Canceling operation.", __FUNCTION__, sig->get_name().c_str());
                        return -1;
                }
@@ -170,70 +172,79 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe,
 
        for(const auto& sig: signals.can_signals)
        {
-               ret = subscribe_unsubscribe_signal(request, subscribe, sig->get_name());
-               if(ret <= 0)
-                       return ret;
-               rets++;
-               DEBUG(binder_interface, "%s: signal: %s subscribed", __FUNCTION__, sig->get_name().c_str());
+               if(conf.get_can_bus_manager().create_rx_filter(*sig) <= 0 && 
+               subscribe_unsubscribe_signal(request, subscribe, sig->get_name()) <= 0)
+               {
+                       return -1;
+                       rets++;
+                       DEBUG(binder_interface, "%s: signal: %s subscribed", __FUNCTION__, sig->get_name().c_str());
+               }
        }
        return rets;
 }
 
-static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, const char *name)
-{
-       struct utils::signals_found signals;
-       int ret = 0;
-
-       openxc_DynamicField search_key = build_DynamicField(std::string(name));
-       signals = utils::signals_manager_t::instance().find_signals(search_key);
-       if (signals.can_signals.empty() && signals.diagnostic_messages.empty())
-               ret = 0;
-
-       ret = subscribe_unsubscribe_signals(request, subscribe, signals);
-       NOTICE(binder_interface, "%s: Subscribed/unsubscribe correctly to %d/%d signal(s).", __FUNCTION__, ret, (int)(signals.can_signals.size() + signals.diagnostic_messages.size())  );
-
-       return ret;
-}
-
-static void subscribe_unsubscribe(struct afb_req request, bool subscribe)
+static const std::vector<std::string> parse_args_from_request(struct afb_req request, bool subscribe)
 {
-       int ok, i, n;
+       int i, n;
+       std::vector<std::string> ret;
        struct json_object *args, *a, *x;
 
-       /* makes the subscription/unsubscription */
+       /* retrieve signals to subscribe */
        args = afb_req_json(request);
-       if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
-               ok = subscribe_unsubscribe_name(request, subscribe, "*");
-       } else if (json_object_get_type(a) != json_type_array) {
-               ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
-       } else {
+       if (args == NULL || !json_object_object_get_ex(args, "event", &a))
+       {
+               ret.push_back("*");
+       }
+       else if (json_object_get_type(a) != json_type_array)
+       {
+               ret.push_back(json_object_get_string(a));
+       }
+       else
+       {
                n = json_object_array_length(a);
-               ok = 0;
-               for (i = 0 ; i < n ; i++) {
+               for (i = 0 ; i < n ; i++)
+               {
                        x = json_object_array_get_idx(a, i);
-                       if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
-                               ok++;
+                       ret.push_back(json_object_get_string(x));
                }
-               ok = (ok == n);
        }
 
-       /* send the report */
-       if (ok)
-               afb_req_success(request, NULL, NULL);
-       else
-               afb_req_fail(request, "error", NULL);
+       return ret;
 }
 
 extern "C"
 {
        static void subscribe(struct afb_req request)
        {
-               subscribe_unsubscribe(request, true);
+               std::vector<std::string> args;
+               struct utils::signals_found sf;
+               int ok = 0, total = 0;
+               bool subscribe = true;
+
+               args = parse_args_from_request(request, subscribe);
+
+               for(const auto& sig: args)
+               {
+                       openxc_DynamicField search_key = build_DynamicField(sig);
+                       sf = utils::signals_manager_t::instance().find_signals(search_key);
+                       total = (int)sf.can_signals.size() + (int)sf.diagnostic_messages.size();
+
+                       if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
+                               NOTICE(binder_interface, "%s: No signal(s) found for %s.", __FUNCTION__, sig.c_str());
+                       else
+                               ok = subscribe_unsubscribe_signals(request, subscribe, sf);
+               }
+
+               NOTICE(binder_interface, "%s: Subscribed/unsubscribe correctly to %d/%d signal(s).", __FUNCTION__, ok, total);
+               if (ok)
+                       afb_req_success(request, NULL, NULL);
+               else
+                       afb_req_fail(request, "error", NULL);
        }
 
        static void unsubscribe(struct afb_req request)
        {
-               subscribe_unsubscribe(request, false);
+               parse_args_from_request(request, false);
        }
 
        static const struct afb_verb_desc_v1 verbs[]=