Apply schema API to bluetooth-policy table 36/240636/1
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 10 Aug 2020 07:34:44 +0000 (16:34 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 10 Aug 2020 07:55:17 +0000 (16:55 +0900)
[example]
- bluetooth.select(Bluetooth::State);
- bluetooth.update(Bluetooth::State = 1, Bluetooth::Pairing = 0);

Change-Id: Ia27ef58e44e311dea3652f8bd63f22cd77db5b92
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/schema/bluetooth.hpp [deleted file]
tables/policy/bluetooth/policy.cpp
tables/policy/bluetooth/policy.hpp
tables/policy/bluetooth/schema.hpp [new file with mode: 0644]
tables/policy/bluetooth/table.cpp
tables/policy/bluetooth/tests/bluetooth.cpp

diff --git a/src/vist/schema/bluetooth.hpp b/src/vist/schema/bluetooth.hpp
deleted file mode 100644 (file)
index 18ce416..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- *  Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-
-#pragma once
-
-#include <vist/query-builder.hpp>
-
-namespace vist {
-namespace schema {
-
-struct Bluetooth {
-       int state;
-       int desktopConnectivity;
-       int pairing;
-       int tethering;
-
-       DECLARE_COLUMN(State, "state", &Bluetooth::state);
-       DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::desktopConnectivity);
-       DECLARE_COLUMN(Pairing, "pairing", &Bluetooth::pairing);
-       DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering);
-};
-
-DECLARE_TABLE(BluetoothTable, "bluetooth", Bluetooth::State,
-                         Bluetooth::DesktopConnectivity,
-                         Bluetooth::Pairing,
-                         Bluetooth::Tethering);
-
-} // namesapce schema
-} // namesapce vist
index 49ba0b4190191a88b41220a922724cf3d081f024..b90a05e653352761c6257311a1c110f3b8e1cead 100644 (file)
@@ -45,7 +45,13 @@ auto bt_allowed(int value)
 
 } // anonymous namespace
 
-void BluetoothState::onChanged(const PolicyValue& value)
+
+BluetoothProvider::State::State() :
+       PolicyModel(GetPolicyName(schema::Bluetooth::State), PolicyValue(1))
+{
+}
+
+void BluetoothProvider::State::onChanged(const PolicyValue& value)
 {
        auto enable = bt_allowed(value);
        auto ret = ::bluetooth_dpm_set_allow_mode(enable);
@@ -55,7 +61,12 @@ void BluetoothState::onChanged(const PolicyValue& value)
        INFO(VIST_PLUGIN) << "Bluetooth state is changed to " << enable;
 }
 
-void DesktopConnectivity::onChanged(const PolicyValue& value)
+BluetoothProvider::DesktopConnectivity::DesktopConnectivity() :
+       PolicyModel(GetPolicyName(schema::Bluetooth::DesktopConnectivity), PolicyValue(1))
+{
+}
+
+void BluetoothProvider::DesktopConnectivity::onChanged(const PolicyValue& value)
 {
        auto enable = allowed(value);
        auto ret = ::bluetooth_dpm_set_desktop_connectivity_state(enable);
@@ -65,7 +76,12 @@ void DesktopConnectivity::onChanged(const PolicyValue& value)
        INFO(VIST_PLUGIN) << "Bluetooth desktop connectivity state is changed to " << enable;
 }
 
-void Pairing::onChanged(const PolicyValue& value)
+BluetoothProvider::Pairing::Pairing() :
+       PolicyModel(GetPolicyName(schema::Bluetooth::Pairing), PolicyValue(1))
+{
+}
+
+void BluetoothProvider::Pairing::onChanged(const PolicyValue& value)
 {
        auto enable = allowed(value);
        auto ret = ::bluetooth_dpm_set_pairing_state(enable);
@@ -75,19 +91,24 @@ void Pairing::onChanged(const PolicyValue& value)
        INFO(VIST_PLUGIN) << "Bluetooth pairing state is changed to " << enable;
 }
 
-void Tethering::onChanged(const PolicyValue& value)
+BluetoothProvider::Tethering::Tethering() :
+       PolicyModel(GetPolicyName(schema::Bluetooth::Tethering), PolicyValue(1))
+{
+}
+
+void BluetoothProvider::Tethering::onChanged(const PolicyValue& value)
 {
        auto enable = value;
        INFO(VIST_PLUGIN) << "Bluetooth tethering state is changed to " << enable;
 }
 
-Bluetooth::Bluetooth(const std::string& name) : PolicyProvider(name)
+BluetoothProvider::BluetoothProvider(const std::string& name) : PolicyProvider(name)
 {
        if (::bt_initialize() != BT_ERROR_NONE)
                THROW(ErrCode::RuntimeError) << "Failed to init bluetooth provider.";
 }
 
-Bluetooth::~Bluetooth()
+BluetoothProvider::~BluetoothProvider()
 {
        ::bt_deinitialize();
 }
index 3c927df1c975915f08c5f1e08fd8a362acc9150c..a83f47c9a478e7fd34434b8eaf9ddfc91e600be9 100644 (file)
 #include <vist/sdk/policy-provider.hpp>
 #include <vist/sdk/policy-value.hpp>
 
+#include "schema.hpp"
+
 namespace vist {
 namespace policy {
 
-class BluetoothState : public PolicyModel {
+class BluetoothProvider final : public PolicyProvider {
 public:
-       BluetoothState() : PolicyModel("bluetooth", PolicyValue(1)) {}
-       void onChanged(const PolicyValue& value) override;
-};
+       BluetoothProvider(const std::string& name);
+       ~BluetoothProvider();
 
-class DesktopConnectivity : public PolicyModel {
-public:
-       DesktopConnectivity() : PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {}
-       void onChanged(const PolicyValue& value) override;
-};
+       struct State : public PolicyModel {
+               State();
+               void onChanged(const PolicyValue& value) override;
+       };
 
-class Pairing: public PolicyModel {
-public:
-       Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {}
-       void onChanged(const PolicyValue& value) override;
-};
+       struct DesktopConnectivity : public PolicyModel {
+               DesktopConnectivity();
+               void onChanged(const PolicyValue& value) override;
+       };
 
-class Tethering: public PolicyModel {
-public:
-       Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {}
-       void onChanged(const PolicyValue&);
-};
+       struct Pairing: public PolicyModel {
+               Pairing();
+               void onChanged(const PolicyValue& value) override;
+       };
+
+       struct Tethering: public PolicyModel {
+               Tethering();
+               void onChanged(const PolicyValue&) override;
+       };
 
-class Bluetooth final : public PolicyProvider {
-public:
-       Bluetooth(const std::string& name);
-       ~Bluetooth();
 };
 
+template <typename Column>
+std::string GetPolicyName(const Column& column) {
+       return schema::bluetooth.name + "-" + column.name;
+}
+
 } // namespace policy
 } // namespace vist
diff --git a/tables/policy/bluetooth/schema.hpp b/tables/policy/bluetooth/schema.hpp
new file mode 100644 (file)
index 0000000..d422c20
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ *  Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#pragma once
+
+#include <vist/query-builder.hpp>
+
+namespace vist {
+namespace schema {
+
+struct Bluetooth {
+       int state;
+       int desktopConnectivity;
+       int pairing;
+       int tethering;
+
+       DECLARE_COLUMN(State, "state", &Bluetooth::state);
+       DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::desktopConnectivity);
+       DECLARE_COLUMN(Pairing, "pairing", &Bluetooth::pairing);
+       DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering);
+};
+
+DECLARE_TABLE(bluetooth, "bluetooth", Bluetooth::State,
+                                                                         Bluetooth::DesktopConnectivity,
+                                                                         Bluetooth::Pairing,
+                                                                         Bluetooth::Tethering);
+
+} // namesapce schema
+} // namesapce vist
index 6bedb72c101b52a8d224680a09acf7725989a91b..060b3eedeb8cb4f8e623fdfd6320ffb5c712bcb3 100644 (file)
@@ -34,29 +34,29 @@ namespace table {
 
 namespace {
 
-std::map<std::string, std::string> ALIAS = {
-       { "state", "bluetooth" },
-       { "desktopConnectivity", "bluetooth-desktop-connectivity" },
-       { "pairing", "bluetooth-pairing" },
-       { "tethering", "bluetooth-tethering"}
-};
-
 void setPolicy(const std::string& name, int value)
 {
        vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value));
 }
 
+std::string getPolicy(const std::string& name)
+{
+       int value = vist::policy::API::Get(name);
+       return std::to_string(value);
+}
+
 } // anonymous namespace
 
 void BluetoothTable::init()
 {
        Builder::table<BluetoothTable>("bluetooth");
 
-       auto provider = std::make_shared<policy::Bluetooth>("bluetooth");
-       provider->add(std::make_shared<policy::BluetoothState>());
-       provider->add(std::make_shared<policy::DesktopConnectivity>());
-       provider->add(std::make_shared<policy::Pairing>());
-       provider->add(std::make_shared<policy::Tethering>());
+       using namespace vist::policy;
+       auto provider = std::make_shared<BluetoothProvider>("bluetooth");
+       provider->add(std::make_shared<BluetoothProvider::State>());
+       provider->add(std::make_shared<BluetoothProvider::DesktopConnectivity>());
+       provider->add(std::make_shared<BluetoothProvider::Pairing>());
+       provider->add(std::make_shared<BluetoothProvider::Tethering>());
 
        policy::API::Admin::AddProvider(provider);
 
@@ -65,11 +65,13 @@ void BluetoothTable::init()
 
 TableColumns BluetoothTable::columns() const
 {
+       // Define Virtual table's schema (column)
+       using namespace schema;
        return {
-               Builder::column<int>("state"),
-               Builder::column<int>("desktopConnectivity"),
-               Builder::column<int>("pairing"),
-               Builder::column<int>("tethering")
+               Builder::column<int>(Bluetooth::State.name),
+               Builder::column<int>(Bluetooth::DesktopConnectivity.name),
+               Builder::column<int>(Bluetooth::Pairing.name),
+               Builder::column<int>(Bluetooth::Tethering.name)
        };
 }
 
@@ -79,11 +81,16 @@ QueryData BluetoothTable::select(QueryContext&)
 
        INFO(VIST) << "Select query about bluetooth table.";
 
+       using namespace schema;
+       using namespace policy;
+       // Policy name format: bluetooth-xxx
+       // Virtual table column name formant: xxx
        Row row;
-       for (const auto& [schemaName, policyName] : ALIAS) {
-               int value = vist::policy::API::Get(policyName);
-               row[schemaName] = std::to_string(value);
-       }
+       row[Bluetooth::State.name] = getPolicy(GetPolicyName(Bluetooth::State));
+       row[Bluetooth::DesktopConnectivity.name] =
+               getPolicy(GetPolicyName(Bluetooth::DesktopConnectivity));
+       row[Bluetooth::Pairing.name] = getPolicy(GetPolicyName(Bluetooth::Pairing));
+       row[Bluetooth::Tethering.name] = getPolicy(GetPolicyName(Bluetooth::Tethering));
 
        QueryData results;
        results.emplace_back(std::move(row));
@@ -99,11 +106,12 @@ QueryData BluetoothTable::update(QueryContext&, const PluginRequest& request)
 
        INFO(VIST) << "Update query about bluetooth table.";
 
-       /// TODO(Sangwan): Sync vtab schema with policy definition
-       setPolicy("bluetooth", Parser::column<int>(request, 0));
-       setPolicy("bluetooth-desktop-connectivity", Parser::column<int>(request, 1));
-       setPolicy("bluetooth-pairing", Parser::column<int>(request, 2));
-       setPolicy("bluetooth-tethering", Parser::column<int>(request, 3));
+       using namespace schema;
+       using namespace policy;
+       setPolicy(GetPolicyName(Bluetooth::State), Parser::column<int>(request, 0));
+       setPolicy(GetPolicyName(Bluetooth::DesktopConnectivity), Parser::column<int>(request, 1));
+       setPolicy(GetPolicyName(Bluetooth::Pairing), Parser::column<int>(request, 2));
+       setPolicy(GetPolicyName(Bluetooth::Tethering), Parser::column<int>(request, 3));
 
        return success();
 
index d682805c770ab36f4e5dcf1835b2007fe2003fb4..920f2f66e2e3845610435d254c7fddc39c1a294d 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include "../policy.hpp"
+#include "../schema.hpp"
 
 #include <vist/client/query.hpp>
 #include <vist/exception.hpp>
@@ -45,10 +46,10 @@ void change_policy_state()
 TEST(BluetoothTests, change_policy_state)
 {
        try {
-               change_policy_state<BluetoothState>();
-               change_policy_state<DesktopConnectivity>();
-               change_policy_state<Pairing>();
-               change_policy_state<Tethering>();
+               change_policy_state<BluetoothProvider::State>();
+               change_policy_state<BluetoothProvider::DesktopConnectivity>();
+               change_policy_state<BluetoothProvider::Pairing>();
+               change_policy_state<BluetoothProvider::Tethering>();
        } catch(const vist::Exception<ErrCode>& e) {
                EXPECT_TRUE(false) << e.what();
        }
@@ -81,3 +82,27 @@ TEST(BluetoothTests, set_policies)
 
        Query::Execute("DELETE FROM policy_admin WHERE name = 'vist-bluetooth-policy-test'");
 }
+
+TEST(BluetoothTests, schema)
+{
+       using namespace vist::schema;
+       std::string select1 = bluetooth.selectAll();
+       EXPECT_EQ(select1, "SELECT * FROM bluetooth");
+
+       std::string select2 = bluetooth.select(Bluetooth::State);
+       EXPECT_EQ(select2, "SELECT state FROM bluetooth");
+
+       std::string update1 = bluetooth.update(Bluetooth::State = 1);
+       EXPECT_EQ(update1, "UPDATE bluetooth SET state = 1");
+
+       std::string update2 = bluetooth.update(Bluetooth::State = 1, Bluetooth::Pairing = 0);
+       EXPECT_EQ(update2, "UPDATE bluetooth SET state = 1, pairing = 0");
+}
+
+TEST(BluetoothTests, type_safe_query)
+{
+       std::string query = schema::bluetooth.selectAll();
+       auto rows = Query::Execute(query);
+
+       EXPECT_TRUE(rows.size() == 1);
+}