Create an auth verb to raise privilege of session
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-hat.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-hat.hpp"
20 #include "low-can-subscription.hpp"
21
22 #include <map>
23 #include <queue>
24 #include <mutex>
25 #include <vector>
26 #include <json-c/json.h>
27
28 #include "application.hpp"
29 #include "../can/can-bus.hpp"
30
31 extern "C"
32 {
33         static int initv2();
34
35         static constexpr struct afb_auth loa_afb_auth(const unsigned loa)
36         {
37                 struct afb_auth a = {};
38                 a.type = afb_auth_LOA;
39                 a.loa = loa;
40                 return a;
41         }
42
43         static constexpr struct afb_auth perm_afb_auth(const char* permission)
44         {
45                 struct afb_auth a = {};
46                 a.type = afb_auth_Permission;
47                 a.text = permission;
48                 return a;
49         }
50
51         static const struct afb_auth afb_auth_loa_1 = { loa_afb_auth(1) };
52         static const struct afb_auth afb_auth_perm = { perm_afb_auth("urn:AGL:permission::platform:can:write") };
53
54         static const struct afb_verb_v2 verbs[]=
55         {
56                 { .verb= "auth", .callback= auth, .auth= &afb_auth_perm, .info="Authentification against service to get the required level of confidence", .session= AFB_SESSION_NONE},
57                 { .verb= "subscribe", .callback= subscribe, .auth= NULL, .info="Let subscribe to signals", .session= AFB_SESSION_NONE},
58                 { .verb= "unsubscribe", .callback= unsubscribe, .auth= NULL, .info="Let unsubscribe signals", .session= AFB_SESSION_NONE},
59                 { .verb= "swrite", .callback= swrite, .auth= &afb_auth_loa_1, .info="Write a single CAN message on a CAN bus", .session= AFB_SESSION_LOA_1},
60                 { .verb= NULL, .callback= NULL, .auth= NULL, .info=NULL, .session= 0}
61         };
62
63         const struct afb_binding_v2 afbBindingV2 {
64                 .api = "low-can",
65                 .specification = NULL,
66                 .info = "API to Low level CAN service, read and decode the bus",
67                 .verbs = verbs,
68                 .preinit = NULL,
69                 .init = initv2,
70                 .onevent = NULL,
71                 .noconcurrency = 0
72         };
73
74         /// @brief Initialize the binding.
75         ///
76         /// @param[in] service Structure which represent the Application Framework Binder.
77         ///
78         /// @return Exit code, zero if success.
79         static int initv2()
80         {
81                 can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
82
83                 can_bus_manager.set_can_devices();
84                 can_bus_manager.start_threads();
85
86                 /// Initialize Diagnostic manager that will handle obd2 requests.
87                 /// We pass by default the first CAN bus device to its Initialization.
88                 /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
89                 if(application_t::instance().get_diagnostic_manager().initialize())
90                         return 0;
91
92                 AFB_ERROR("There was something wrong with CAN device Initialization.");
93                 return 1;
94         }
95 };