aaa9e54a6511710f786a851bcc8017e1122cd095
[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         const auto& ss_i = subscribed_signals.find(sig.genericName);
38         if (ss_i != subscribed_signals.end())
39         {
40                 if(!afb_event_is_valid(ss_i->second))
41                 {
42                         if(!subscribe)
43                         {
44                                 NOTICE(interface, "Event isn't valid, it can't be unsubscribed.");
45                                 ret = 1;
46                         }
47                         else
48                         {
49                                 ss_i->second = afb_daemon_make_event(interface->daemon, ss_i->first.c_str());
50                                 if (!afb_event_is_valid(ss_i->second)) 
51                                 {
52                                         ERROR(interface, "Can't create an event, something goes wrong.");
53                                         ret = 0;
54                                 }
55                         }
56                 }
57         }
58         else
59         {
60                 subscribed_signals[sig.genericName] = afb_daemon_make_event(interface->daemon, sig.genericName);
61                 if (!afb_event_is_valid(ss_i->second)) 
62                 {
63                         ERROR(interface, "Can't create an event, something goes wrong.");
64                         ret = 0;
65                 }
66         }
67
68         if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, subscribed_signals[sig.genericName])) < 0)
69         {
70                 ERROR(interface, "Operation goes wrong for signal: %s", sig.genericName);
71                 ret = 0;
72         }
73         else
74                 ret = 1;
75         
76         return ret;
77 }
78
79 static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<CanSignal>& signals)
80 {
81         int ret;
82
83         // TODO: lock the subscribed_signals when insert/remove
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 }
91
92 static int subscribe_unsubscribe_all(struct afb_req request, bool subscribe)
93 {
94         int e = 0;
95
96         //for (const auto& sig : SIGNALS)
97         //      e += !subscribe_unsubscribe_signals(request, subscribe, sig);
98         e += !subscribe_unsubscribe_signals(request, subscribe, SIGNALS[MESSAGE_SET_ID]);
99         
100         return e == 0;
101 }
102
103 static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, const char *name)
104 {
105         std::vector<CanSignal> sig;
106         int ret = 0;
107
108         if (!::strcmp(name, "*"))
109                 ret = subscribe_unsubscribe_all(request, subscribe);
110         else
111         {
112                 //if(obd2_handler_c.is_obd2_signal(name))
113                 if(false)
114                 {
115                 // TODO
116                 }
117                 else
118                 {
119                         openxc_DynamicField search_key = build_DynamicField(name);
120                         sig = find_can_signals(search_key);
121                         if (sig.empty())
122                                 ret = 0;
123                 }
124                 ret = subscribe_unsubscribe_signals(request, subscribe, sig);
125         }
126         return ret;
127 }
128
129 static void subscribe_unsubscribe(struct afb_req request, bool subscribe)
130 {
131         int ok, i, n;
132         struct json_object *args, *a, *x;
133
134         /* makes the subscription/unsubscription */
135         args = afb_req_json(request);
136         if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
137                 ok = subscribe_unsubscribe_all(request, subscribe);
138         } else if (json_object_get_type(a) != json_type_array) {
139                 ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
140         } else {
141                 n = json_object_array_length(a);
142                 ok = 0;
143                 for (i = 0 ; i < n ; i++) {
144                         x = json_object_array_get_idx(a, i);
145                         if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
146                                 ok++;
147                 }
148                 ok = (ok == n);
149         }
150
151         /* send the report */
152         if (ok)
153                 afb_req_success(request, NULL, NULL);
154         else
155                 afb_req_fail(request, "error", NULL);
156 }
157
158 static const struct afb_verb_desc_v1 verbs[]=
159 {
160         { .name= "subscribe",   .session= AFB_SESSION_NONE, .callback= subscribe,       .info= "subscribe to notification of CAN bus messages." },
161         { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe,     .info= "unsubscribe a previous subscription." }
162 };
163
164 static const struct afb_binding binding_desc {
165         AFB_BINDING_VERSION_1,
166         {
167                 "CAN bus service",
168                 "can",
169                 verbs
170         }
171 };
172
173 extern "C"
174 {
175         static void subscribe(struct afb_req request)
176         {
177                 subscribe_unsubscribe(request, true);
178         }
179
180         static void unsubscribe(struct afb_req request)
181         {
182                 subscribe_unsubscribe(request, false);
183         }
184
185         const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
186         {
187                 interface = itf;
188
189                 return &binding_desc;
190         }
191
192         /**
193         * @brief Initialize the binding.
194         * 
195         * @param[in] service Structure which represent the Application Framework Binder.
196         * 
197         * @return Exit code, zero if success.
198         */
199         int afbBindingV1ServiceInit(struct afb_service service)
200         {
201                 int fd_conf;
202                 fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL);
203
204                 /* Open CAN socket */
205                 can_bus_t can_bus_handler(interface, fd_conf);
206                 if(can_bus_handler.init_can_dev() == 0)
207                 {
208                         can_bus_handler.start_threads();
209                         return 0;
210                 }
211
212                 return 1;
213         }
214 };