Apply schema API to bluetooth-policy table
[platform/core/security/vist.git] / tables / policy / bluetooth / table.cpp
1 /*
2  *  Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #include "policy.hpp"
18 #include "table.hpp"
19
20 #include <vist/exception.hpp>
21 #include <vist/logger.hpp>
22 #include <vist/policy/api.hpp>
23 #include <vist/table/builder.hpp>
24 #include <vist/table/parser.hpp>
25 #include <vist/table/util.hpp>
26
27 extern "C" vist::table::DynamicTable* DynamicTableFactory()
28 {
29         return new vist::table::BluetoothTable;
30 }
31
32 namespace vist {
33 namespace table {
34
35 namespace {
36
37 void setPolicy(const std::string& name, int value)
38 {
39         vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value));
40 }
41
42 std::string getPolicy(const std::string& name)
43 {
44         int value = vist::policy::API::Get(name);
45         return std::to_string(value);
46 }
47
48 } // anonymous namespace
49
50 void BluetoothTable::init()
51 {
52         Builder::table<BluetoothTable>("bluetooth");
53
54         using namespace vist::policy;
55         auto provider = std::make_shared<BluetoothProvider>("bluetooth");
56         provider->add(std::make_shared<BluetoothProvider::State>());
57         provider->add(std::make_shared<BluetoothProvider::DesktopConnectivity>());
58         provider->add(std::make_shared<BluetoothProvider::Pairing>());
59         provider->add(std::make_shared<BluetoothProvider::Tethering>());
60
61         policy::API::Admin::AddProvider(provider);
62
63         INFO(VIST_PLUGIN) << "Bluetooth plugin loaded.";
64 }
65
66 TableColumns BluetoothTable::columns() const
67 {
68         // Define Virtual table's schema (column)
69         using namespace schema;
70         return {
71                 Builder::column<int>(Bluetooth::State.name),
72                 Builder::column<int>(Bluetooth::DesktopConnectivity.name),
73                 Builder::column<int>(Bluetooth::Pairing.name),
74                 Builder::column<int>(Bluetooth::Tethering.name)
75         };
76 }
77
78 QueryData BluetoothTable::select(QueryContext&)
79 {
80         TABLE_EXCEPTION_GUARD_START
81
82         INFO(VIST) << "Select query about bluetooth table.";
83
84         using namespace schema;
85         using namespace policy;
86         // Policy name format: bluetooth-xxx
87         // Virtual table column name formant: xxx
88         Row row;
89         row[Bluetooth::State.name] = getPolicy(GetPolicyName(Bluetooth::State));
90         row[Bluetooth::DesktopConnectivity.name] =
91                 getPolicy(GetPolicyName(Bluetooth::DesktopConnectivity));
92         row[Bluetooth::Pairing.name] = getPolicy(GetPolicyName(Bluetooth::Pairing));
93         row[Bluetooth::Tethering.name] = getPolicy(GetPolicyName(Bluetooth::Tethering));
94
95         QueryData results;
96         results.emplace_back(std::move(row));
97
98         return results;
99
100         TABLE_EXCEPTION_GUARD_END
101 }
102
103 QueryData BluetoothTable::update(QueryContext&, const PluginRequest& request)
104 {
105         TABLE_EXCEPTION_GUARD_START
106
107         INFO(VIST) << "Update query about bluetooth table.";
108
109         using namespace schema;
110         using namespace policy;
111         setPolicy(GetPolicyName(Bluetooth::State), Parser::column<int>(request, 0));
112         setPolicy(GetPolicyName(Bluetooth::DesktopConnectivity), Parser::column<int>(request, 1));
113         setPolicy(GetPolicyName(Bluetooth::Pairing), Parser::column<int>(request, 2));
114         setPolicy(GetPolicyName(Bluetooth::Tethering), Parser::column<int>(request, 3));
115
116         return success();
117
118         TABLE_EXCEPTION_GUARD_END
119 }
120
121 } // namespace table
122 } // namespace vist