Cleaning, improve comments
[apps/agl-service-can-low-level.git] / src / low-can-binding-new.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 *               Subscription and unsubscription
24 *
25 *********************************************************************************/
26
27 std::vector<std::string> get_name(struct afb_req request)
28 {
29         int i, n;
30         std::vector<std::string> ret;
31         struct json_object *args, *a, *x;
32
33         args = afb_req_json(request);
34         if (args == NULL || !json_object_object_get_ex(args, "event", &a))
35                 ret.clear();
36         else if (json_object_get_type(a) != json_type_array)
37                 ret.push_back(json_object_get_string(a));
38         else
39         {
40                 n = json_object_array_length(a);
41                 for (i = 0 ; i < n ; i++) {
42                         x = json_object_array_get_idx(a, i);
43                         ret.push_back(json_object_get_string(x));
44                 }
45         }
46
47         return ret;
48 }
49
50 static const struct afb_verb_desc_v1 verbs[]=
51 {
52         { .name= "subscribe",   .session= AFB_SESSION_NONE, .callback= subscribe,       .info= "subscribe to notification of CAN bus messages." },
53         { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe,     .info= "unsubscribe a previous subscription." }
54 };
55
56 static const struct afb_binding binding_desc {
57         AFB_BINDING_VERSION_1,
58         {
59                 "CAN bus service",
60                 "can",
61                 verbs
62         }
63 };
64
65 extern "C"
66 {
67         static void subscribe(struct afb_req request)
68         {
69                 int ret;
70                 std::vector<std::string> names;
71                 
72                 names = get_name(request);
73                 if(names.empty())
74                         ret = subscribe_signals(request, "*");
75                 else
76                         ret = subscribe_signals(request, names);
77         }
78
79         static void unsubscribe(struct afb_req request)
80         {
81                 subscribe_unsubscribe(request, false);
82         }
83
84         const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
85         {
86                 interface = itf;
87
88                 return &binding_desc;
89         }
90
91         /**
92         * @brief Initialize the binding.
93         * 
94         * @param[in] service Structure which represent the Application Framework Binder.
95         * 
96         * @return Exit code, zero if success.
97         */
98         int afbBindingV1ServiceInit(struct afb_service service)
99         {
100                 int fd_conf;
101                 fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL);
102
103                 /* Open CAN socket */
104                 can_bus_t can_bus_handler(interface, fd_conf);
105                 if(can_bus_handler.init_can_dev() == 0)
106                 {
107                         can_bus_handler.start_threads();
108                         return 0;
109                 }
110
111                 return 1;
112         }
113 };