2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loic Collignon" <loic.collignon@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "low-can-binding.hpp"
21 /********************************************************************************
23 * Subscription and unsubscription
25 *********************************************************************************/
27 std::vector<std::string> get_name(struct afb_req request)
30 std::vector<std::string> ret;
31 struct json_object *args, *a, *x;
33 args = afb_req_json(request);
34 if (args == NULL || !json_object_object_get_ex(args, "event", &a))
36 else if (json_object_get_type(a) != json_type_array)
37 ret.push_back(json_object_get_string(a));
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));
50 static const struct afb_verb_desc_v1 verbs[]=
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." }
56 static const struct afb_binding binding_desc {
57 AFB_BINDING_VERSION_1,
67 static void subscribe(struct afb_req request)
70 std::vector<std::string> names;
72 names = get_name(request);
74 ret = subscribe_signals(request, "*");
76 ret = subscribe_signals(request, names);
79 static void unsubscribe(struct afb_req request)
81 subscribe_unsubscribe(request, false);
84 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
92 * @brief Initialize the binding.
94 * @param[in] service Structure which represent the Application Framework Binder.
96 * @return Exit code, zero if success.
98 int afbBindingV1ServiceInit(struct afb_service service)
101 fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL);
103 /* Open CAN socket */
104 can_bus_t can_bus_handler(interface, fd_conf);
105 if(can_bus_handler.init_can_dev() == 0)
107 can_bus_handler.start_threads();