Static code review fixes.
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / binding / low-can-cb.cpp
index 4faae85..9e85f5c 100644 (file)
@@ -52,7 +52,7 @@ void on_no_clients(std::string message)
        }
 }
 
-static void push_n_notify(const can_message_t cm)
+static void push_n_notify(const can_message_t& cm)
 {
        can_bus_t& cbm = configuration_t::instance().get_can_bus_manager();
        std::lock_guard<std::mutex> can_message_lock(cbm.get_can_message_mutex());
@@ -181,7 +181,7 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
 ///
 static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const struct utils::signals_found& signals)
 {
-       int ret,  rets = 0;
+       int rets = 0;
 
        //TODO: Implement way to dynamically call the right function no matter
        // how much signals types we have.
@@ -189,6 +189,7 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe,
 
        for(const auto& sig : signals.diagnostic_messages)
        {
+               int ret = 0;
                diagnostic_manager_t& diag_m = conf.get_diagnostic_manager();
                DiagnosticRequest* diag_req = conf.get_request_from_diagnostic_message(sig->get_name());
 
@@ -231,19 +232,19 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe,
        return rets;
 }
 
-static int process_args(struct afb_req request, const std::vector<std::string>& args, bool subscribe)
+static int process_args(struct afb_req request, const std::map<std::string, struct event_filter_t>& args, bool subscribe)
 {
        struct utils::signals_found sf;
        int ok = 0, total = 0;
 
        for(const auto& sig: args)
        {
-               openxc_DynamicField search_key = build_DynamicField(sig);
+               openxc_DynamicField search_key = build_DynamicField(sig.first);
                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());
+                       NOTICE(binder_interface, "%s: No signal(s) found for %s.", __FUNCTION__, sig.first.c_str());
                else
                        ok = subscribe_unsubscribe_signals(request, subscribe, sf);
        }
@@ -251,29 +252,69 @@ static int process_args(struct afb_req request, const std::vector<std::string>&
        return ok;
 }
 
-static const std::vector<std::string> parse_args_from_request(struct afb_req request)
+static int parse_filter(json_object* event, struct event_filter_t* event_filter)
 {
-       int i, n;
-       std::vector<std::string> ret;
-       struct json_object *args, *a, *x;
+       struct json_object  *filter, *obj;
+       int ret = 0;
+
+       if (json_object_object_get_ex(event, "filter", &filter))
+       {
+               event_filter->frequency = -1.0;
+               event_filter->min = -1.0;
+               event_filter->max = -1.0;
+               if (json_object_object_get_ex(filter, "frequency", &obj)
+               && json_object_get_type(obj) == json_type_double)
+               {
+                       event_filter->frequency = json_object_get_double(obj);
+                       ret += 1;
+               }
+               if (json_object_object_get_ex(filter, "min", &obj)
+               && json_object_get_type(obj) == json_type_double)
+               {
+                       event_filter->min = json_object_get_double(obj);
+                       ret += 2;
+               }
+               if (json_object_object_get_ex(filter, "max", &obj)
+               && json_object_get_type(obj) == json_type_double)
+               {
+                       event_filter->max = json_object_get_double(obj);
+                       ret += 4;
+               }
+       }
+
+       return ret;
+}
 
+static const std::map<std::string, struct event_filter_t> parse_args_from_request(struct afb_req request)
+{
+       int i, n;
+       std::map<std::string, struct event_filter_t> ret;
+       struct json_object *args, *event, *x;
+       struct event_filter_t event_filter;
+       
        /* retrieve signals to subscribe */
        args = afb_req_json(request);
-       if (args == NULL || !json_object_object_get_ex(args, "event", &a))
+       if (args == NULL || !json_object_object_get_ex(args, "event", &event))
        {
-               ret.push_back("*");
+               event = json_object_new_string("*");
+               parse_filter(event, &event_filter);
+               ret["*"] = event_filter;
        }
-       else if (json_object_get_type(a) != json_type_array)
+       else if (json_object_get_type(event) != json_type_array)
        {
-               ret.push_back(json_object_get_string(a));
+               const std::string event_pattern = std::string(json_object_get_string(event));
+               parse_filter(event, &event_filter);
+               ret[event_pattern] = event_filter;
        }
        else
        {
-               n = json_object_array_length(a);
+               n = json_object_array_length(event);
                for (i = 0 ; i < n ; i++)
                {
-                       x = json_object_array_get_idx(a, i);
-                       ret.push_back(json_object_get_string(x));
+                       x = json_object_array_get_idx(event, i);
+                       const std::string event_pattern = std::string(json_object_get_string(x));
+                       parse_filter(x, &event_filter);
+                       ret[event_pattern] = event_filter;
                }
        }
 
@@ -284,7 +325,7 @@ void subscribe(struct afb_req request)
 {
        bool subscribe = true;
 
-       const std::vector<std::string> args = parse_args_from_request(request);
+       const std::map<std::string, struct event_filter_t> args = parse_args_from_request(request);
 
        if (process_args(request, args, subscribe) > 0)
                afb_req_success(request, NULL, NULL);
@@ -294,10 +335,9 @@ void subscribe(struct afb_req request)
 
 void unsubscribe(struct afb_req request)
 {
-       std::vector<std::string> args;
        bool subscribe = false;
        
-       args = parse_args_from_request(request);
+       const std::map<std::string, struct event_filter_t> args = parse_args_from_request(request);
 
        if (process_args(request, args, subscribe) > 0)
                afb_req_success(request, NULL, NULL);