1812d8185c9e988aa9ee6c31a936712af9df8d12
[apps/agl-service-can-low-level.git] / src / low-can-binding.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loic Collignon" <loic.collignon@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "low-can-binding.hpp"
20
21 /********************************************************************************
22 *
23 *               Event management
24 *
25 *********************************************************************************/
26
27 /********************************************************************************
28 *
29 *               Subscription and unsubscription
30 *
31 *********************************************************************************/
32
33 static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const CanSignal& sig)
34 {
35         int ret;
36
37         // TODO: lock the subscribed_signals when insert/remove
38         const auto& ss_i = subscribed_signals.find(sig.genericName);
39         if (ss_i != subscribed_signals.end())
40         {
41                 if(!afb_event_is_valid(ss_i->second))
42                 {
43                         if(!subscribe)
44                         {
45                                 NOTICE(interface, "Event isn't valid, it can't be unsubscribed.");
46                                 ret = 1;
47                         }
48                         else
49                         {
50                                 ss_i->second = afb_daemon_make_event(interface->daemon, ss_i->first.c_str());
51                                 if (!afb_event_is_valid(ss_i->second)) 
52                                 {
53                                         ERROR(interface, "Can't create an event, something goes wrong.");
54                                         ret = 0;
55                                 }
56                         }
57                 }
58         }
59         else
60         {
61                 subscribed_signals[sig.genericName] = afb_daemon_make_event(interface->daemon, sig.genericName);
62                 if (!afb_event_is_valid(ss_i->second)) 
63                 {
64                         ERROR(interface, "Can't create an event, something goes wrong.");
65                         ret = 0;
66                 }
67         }
68
69         if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, subscribed_signals[sig.genericName])) < 0)
70         {
71                 ERROR(interface, "Operation goes wrong for signal: %s", sig.genericName);
72                 ret = 0;
73         }
74         else
75                 ret = 1;
76         
77         return ret;
78 }
79
80 static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<CanSignal>& signals)
81 {
82         int ret = 0;
83
84         for(const auto& signal_i : signals)
85         {
86                 ret = subscribe_unsubscribe_signal(request, subscribe, signal_i);
87                 if(ret == 0)
88                         return ret;
89         }
90         return ret;
91 }
92
93 static int subscribe_unsubscribe_all(struct afb_req request, bool subscribe)
94 {
95         int e = 0;
96
97         //for (const auto& sig : SIGNALS)
98         //      e += !subscribe_unsubscribe_signals(request, subscribe, sig);
99         e += !subscribe_unsubscribe_signals(request, subscribe, getSignals());
100         
101         return e == 0;
102 }
103
104 static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, const char *name)
105 {
106         std::vector<CanSignal> sig;
107         int ret = 0;
108
109         if (!::strcmp(name, "*"))
110                 ret = subscribe_unsubscribe_all(request, subscribe);
111         else
112         {
113                 //if(obd2_handler_c.is_obd2_signal(name))
114                 if(false)
115                 {
116                 // TODO
117                 }
118                 else
119                 {
120                         openxc_DynamicField search_key = build_DynamicField(name);
121                         sig = find_can_signals(interface, search_key);
122                         if (sig.empty())
123                                 ret = 0;
124                 }
125                 ret = subscribe_unsubscribe_signals(request, subscribe, sig);
126         }
127         return ret;
128 }
129
130 static void subscribe_unsubscribe(struct afb_req request, bool subscribe)
131 {
132         int ok, i, n;
133         struct json_object *args, *a, *x;
134
135         /* makes the subscription/unsubscription */
136         args = afb_req_json(request);
137         if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
138                 ok = subscribe_unsubscribe_all(request, subscribe);
139         } else if (json_object_get_type(a) != json_type_array) {
140                 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
141         } else {
142                 n = json_object_array_length(a);
143                 ok = 0;
144                 for (i = 0 ; i < n ; i++) {
145                         x = json_object_array_get_idx(a, i);
146                         if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
147                                 ok++;
148                 }
149                 ok = (ok == n);
150         }
151
152         /* send the report */
153         if (ok)
154                 afb_req_success(request, NULL, NULL);
155         else
156                 afb_req_fail(request, "error", NULL);
157 }
158
159 static const struct afb_verb_desc_v1 verbs[]=
160 {
161         { .name= "subscribe",   .session= AFB_SESSION_NONE, .callback= subscribe,       .info= "subscribe to notification of CAN bus messages." },
162         { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe,     .info= "unsubscribe a previous subscription." }
163 };
164
165 static const struct afb_binding binding_desc {
166         AFB_BINDING_VERSION_1,
167         {
168                 "CAN bus service",
169                 "can",
170                 verbs
171         }
172 };
173
174 extern "C"
175 {
176         static void subscribe(struct afb_req request)
177         {
178                 subscribe_unsubscribe(request, true);
179         }
180
181         static void unsubscribe(struct afb_req request)
182         {
183                 subscribe_unsubscribe(request, false);
184         }
185
186         const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
187         {
188                 interface = itf;
189
190                 return &binding_desc;
191         }
192
193         /**
194         * @brief Initialize the binding.
195         * 
196         * @param[in] service Structure which represent the Application Framework Binder.
197         * 
198         * @return Exit code, zero if success.
199         */
200         int afbBindingV1ServiceInit(struct afb_service service)
201         {
202                 int fd_conf;
203                 fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL);
204
205                 /* Open CAN socket */
206                 can_bus_t can_bus_handler(interface, fd_conf);
207                 if(can_bus_handler.init_can_dev() == 0)
208                 {
209                         can_bus_handler.start_threads();
210                         return 0;
211                 }
212
213                 return 1;
214         }
215 };