From 0115cd7c02bec50d346248811140376f54e807ed Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Mon, 10 Aug 2020 16:34:44 +0900 Subject: [PATCH] Apply schema API to bluetooth-policy table [example] - bluetooth.select(Bluetooth::State); - bluetooth.update(Bluetooth::State = 1, Bluetooth::Pairing = 0); Change-Id: Ia27ef58e44e311dea3652f8bd63f22cd77db5b92 Signed-off-by: Sangwan Kwon --- tables/policy/bluetooth/policy.cpp | 33 +++++++++-- tables/policy/bluetooth/policy.hpp | 50 ++++++++-------- .../policy/bluetooth/schema.hpp | 8 +-- tables/policy/bluetooth/table.cpp | 58 +++++++++++-------- tables/policy/bluetooth/tests/bluetooth.cpp | 33 +++++++++-- 5 files changed, 120 insertions(+), 62 deletions(-) rename src/vist/schema/bluetooth.hpp => tables/policy/bluetooth/schema.hpp (87%) diff --git a/tables/policy/bluetooth/policy.cpp b/tables/policy/bluetooth/policy.cpp index 49ba0b4..b90a05e 100644 --- a/tables/policy/bluetooth/policy.cpp +++ b/tables/policy/bluetooth/policy.cpp @@ -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(); } diff --git a/tables/policy/bluetooth/policy.hpp b/tables/policy/bluetooth/policy.hpp index 3c927df..a83f47c 100644 --- a/tables/policy/bluetooth/policy.hpp +++ b/tables/policy/bluetooth/policy.hpp @@ -20,38 +20,42 @@ #include #include +#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 +std::string GetPolicyName(const Column& column) { + return schema::bluetooth.name + "-" + column.name; +} + } // namespace policy } // namespace vist diff --git a/src/vist/schema/bluetooth.hpp b/tables/policy/bluetooth/schema.hpp similarity index 87% rename from src/vist/schema/bluetooth.hpp rename to tables/policy/bluetooth/schema.hpp index 18ce416..d422c20 100644 --- a/src/vist/schema/bluetooth.hpp +++ b/tables/policy/bluetooth/schema.hpp @@ -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 diff --git a/tables/policy/bluetooth/table.cpp b/tables/policy/bluetooth/table.cpp index 6bedb72..060b3ee 100644 --- a/tables/policy/bluetooth/table.cpp +++ b/tables/policy/bluetooth/table.cpp @@ -34,29 +34,29 @@ namespace table { namespace { -std::map 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("bluetooth"); - auto provider = std::make_shared("bluetooth"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); + using namespace vist::policy; + auto provider = std::make_shared("bluetooth"); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); 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("state"), - Builder::column("desktopConnectivity"), - Builder::column("pairing"), - Builder::column("tethering") + Builder::column(Bluetooth::State.name), + Builder::column(Bluetooth::DesktopConnectivity.name), + Builder::column(Bluetooth::Pairing.name), + Builder::column(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(request, 0)); - setPolicy("bluetooth-desktop-connectivity", Parser::column(request, 1)); - setPolicy("bluetooth-pairing", Parser::column(request, 2)); - setPolicy("bluetooth-tethering", Parser::column(request, 3)); + using namespace schema; + using namespace policy; + setPolicy(GetPolicyName(Bluetooth::State), Parser::column(request, 0)); + setPolicy(GetPolicyName(Bluetooth::DesktopConnectivity), Parser::column(request, 1)); + setPolicy(GetPolicyName(Bluetooth::Pairing), Parser::column(request, 2)); + setPolicy(GetPolicyName(Bluetooth::Tethering), Parser::column(request, 3)); return success(); diff --git a/tables/policy/bluetooth/tests/bluetooth.cpp b/tables/policy/bluetooth/tests/bluetooth.cpp index d682805..920f2f6 100644 --- a/tables/policy/bluetooth/tests/bluetooth.cpp +++ b/tables/policy/bluetooth/tests/bluetooth.cpp @@ -15,6 +15,7 @@ */ #include "../policy.hpp" +#include "../schema.hpp" #include #include @@ -45,10 +46,10 @@ void change_policy_state() TEST(BluetoothTests, change_policy_state) { try { - change_policy_state(); - change_policy_state(); - change_policy_state(); - change_policy_state(); + change_policy_state(); + change_policy_state(); + change_policy_state(); + change_policy_state(); } catch(const vist::Exception& 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); +} -- 2.34.1