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>
tables/policy/bluetooth/policy.cpp
tables/policy/bluetooth/policy.hpp
tables/policy/bluetooth/schema.hpp [moved from src/vist/schema/bluetooth.hpp with 87% similarity]
tables/policy/bluetooth/table.cpp
tables/policy/bluetooth/tests/bluetooth.cpp

index 49ba0b4..b90a05e 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 3c927df..a83f47c 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
similarity index 87%
rename from src/vist/schema/bluetooth.hpp
rename to tables/policy/bluetooth/schema.hpp
index 18ce416..d422c20 100644 (file)
@@ -33,10 +33,10 @@ struct Bluetooth {
        DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering);
 };
 
-DECLARE_TABLE(BluetoothTable, "bluetooth", Bluetooth::State,
-                         Bluetooth::DesktopConnectivity,
-                         Bluetooth::Pairing,
-                         Bluetooth::Tethering);
+DECLARE_TABLE(bluetooth, "bluetooth", Bluetooth::State,
+                                                                         Bluetooth::DesktopConnectivity,
+                                                                         Bluetooth::Pairing,
+                                                                         Bluetooth::Tethering);
 
 } // namesapce schema
 } // namesapce vist
index 6bedb72..060b3ee 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 d682805..920f2f6 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);
+}