} // 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);
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);
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);
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();
}
#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
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
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);
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)
};
}
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));
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();
*/
#include "../policy.hpp"
+#include "../schema.hpp"
#include <vist/client/query.hpp>
#include <vist/exception.hpp>
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();
}
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);
+}