#include <bluetooth-api.h>
#include <bluetooth_internal.h>
-#include <policyd/pil/policy-context.h>
-#include <policyd/pil/policy-model.h>
+#include <policyd/pil/global-policy.h>
#include <policyd/pil/policy-storage.h>
#include <policyd/pil/policy-event.h>
+#include <memory>
+
#include "../dlog.h"
#define BT_FAILED(ret) \
} // namespace
-class ModeChange : public GlobalPolicy<DataSetInt> {
+class ModeChange : public GlobalPolicy {
public:
ModeChange() : GlobalPolicy("bluetooth")
{
PolicyEventNotifier::create("bluetooth");
}
- bool apply(const DataType& value)
+ bool apply(const DataSetInt& value, uid_t)
{
int ret = bluetooth_dpm_set_allow_mode(STATE_CHANGE_IS_ALLOWED(value));
if (!BT_FAILED(ret)) {
}
};
-class DesktopConnectivity : public GlobalPolicy<DataSetInt> {
+class DesktopConnectivity : public GlobalPolicy {
public:
DesktopConnectivity() : GlobalPolicy("bluetooth-desktop-connectivity")
{
PolicyEventNotifier::create("bluetooth_desktop_connectivity");
}
- bool apply(const DataType & value)
+ bool apply(const DataSetInt & value, uid_t)
{
int ret = bluetooth_dpm_set_desktop_connectivity_state(POLICY_IS_ALLOWED(value));
if (!BT_FAILED(ret)) {
}
};
-class Pairing: public GlobalPolicy<DataSetInt> {
+class Pairing: public GlobalPolicy {
public:
Pairing() : GlobalPolicy("bluetooth-pairing")
{
PolicyEventNotifier::create("bluetooth_pairing");
}
- bool apply(const DataType& value)
+ bool apply(const DataSetInt& value, uid_t)
{
int ret = bluetooth_dpm_set_pairing_state(POLICY_IS_ALLOWED(value));
if (!BT_FAILED(ret)) {
}
};
-class Tethering: public GlobalPolicy<DataSetInt> {
+class Tethering: public GlobalPolicy {
public:
Tethering() : GlobalPolicy("bluetooth-tethering")
{
PolicyEventNotifier::create("bluetooth_tethering");
}
- bool apply(const DataType& value)
+ bool apply(const DataSetInt& value, uid_t)
{
int enable = value;
PolicyEventNotifier::emit("bluetooth_tethering",
Bluetooth();
~Bluetooth();
- int setModeChangeState(bool enable);
- bool getModeChangeState();
- int setDesktopConnectivityState(bool enable);
- bool getDesktopConnectivityState();
- int setTetheringState(bool enable);
- bool getTetheringState();
- int setPairingState(bool enable);
- bool getPairingState();
-
private:
static void onStateChanged(int result, bt_adapter_state_e state, void *user_data);
-
-private:
- ModeChange modeChange;
- DesktopConnectivity connectivity;
- Pairing pairing;
- Tethering tethering;
};
Bluetooth::Bluetooth()
{
if (::bt_initialize() != BT_ERROR_NONE) {
- ERROR(PLUGINS, "Bluetooth framework was not initilaized");
+ ERROR(PLUGINS,"Bluetooth framework was not initilaized");
return;
}
if (::bt_adapter_set_state_changed_cb(onStateChanged, this) != BT_ERROR_NONE) {
- ERROR(PLUGINS, "Failed to register Bluetooth callback");
+ ERROR(PLUGINS,"Failed to register Bluetooth callback");
return;
}
}
// pimpl->uuidRestriction.enforce();
}
}
-
-int Bluetooth::setModeChangeState(bool enable)
-{
- try {
- modeChange.set(enable);
- } catch (runtime::Exception& e) {
- ERROR(PLUGINS, "Exception: " << e.what());
- return -1;
- }
-
- return 0;
-}
-
-bool Bluetooth::getModeChangeState()
-{
- return modeChange.get();
-}
-
-int Bluetooth::setDesktopConnectivityState(bool enable)
-{
- try {
- connectivity.set(enable);
- } catch (runtime::Exception& e) {
- ERROR(PLUGINS, "Exception: " << e.what());
- return -1;
- }
-
- return 0;
-}
-
-bool Bluetooth::getDesktopConnectivityState()
-{
- return connectivity.get();
-}
-
-int Bluetooth::setPairingState(bool enable)
-{
- try {
- pairing.set(enable);
- } catch (runtime::Exception& e) {
- ERROR(PLUGINS, "Exception: " << e.what());
- return -1;
- }
-
- return 0;
-}
-
-bool Bluetooth::getPairingState()
-{
- return pairing.get();
-}
-
-int Bluetooth::setTetheringState(bool enable)
-{
- try {
- tethering.set(enable);
- } catch (runtime::Exception& e) {
- ERROR(PLUGINS, "Exception " << e.what());
- return -1;
- }
-
- return 0;
-}
-
-bool Bluetooth::getTetheringState()
-{
- return tethering.get();
-}
-
-
-extern "C" {
-
-#define PRIVILEGE "http://tizen.org/privilege/dpm.bluetooth"
-
-AbstractPolicyProvider *PolicyFactory(PolicyControlContext& context)
-{
- INFO(PLUGINS, "Bluetooth plugin loaded");
- Bluetooth *policy = new Bluetooth();
-
- context.expose(policy, PRIVILEGE, (int)(Bluetooth::setModeChangeState)(bool));
- context.expose(policy, PRIVILEGE, (int)(Bluetooth::setDesktopConnectivityState)(bool));
- context.expose(policy, PRIVILEGE, (int)(Bluetooth::setTetheringState)(bool));
- context.expose(policy, PRIVILEGE, (int)(Bluetooth::setPairingState)(bool));
-
- context.expose(policy, "", (bool)(Bluetooth::getModeChangeState)());
- context.expose(policy, "", (bool)(Bluetooth::getDesktopConnectivityState)());
- context.expose(policy, "", (bool)(Bluetooth::getTetheringState)());
- context.expose(policy, "", (bool)(Bluetooth::getPairingState)());
-
- return policy;
-}
-
-} // extern "C"
logger.h
packman.h
policy-event.h
- policy-context.h
policy-admin.h
policy-client.h
policy-model.h
status.h
observer.h)
-SET(PIL_DEPENDENCY klay
- glib-2.0
- gio-2.0
- sqlite3
- bundle
- aul
- appsvc
- pkgmgr
- pkgmgr-info
- syspopup-caller
- deviced
- libtzplatform-config
- security-privilege-manager
- capi-system-info
- capi-base-common
- capi-system-system-settings
- cynara-client
- notification
- cynara-session)
+SET(DEPENDENCY klay
+ glib-2.0
+ gio-2.0
+ sqlite3
+ bundle
+ aul
+ appsvc
+ pkgmgr
+ pkgmgr-info
+ vconf
+ syspopup-caller
+ deviced
+ libtzplatform-config
+ security-privilege-manager
+ capi-system-info
+ capi-base-common
+ capi-system-system-settings
+ notification
+ cynara-client
+ cynara-session)
-PKG_CHECK_MODULES(PIL_DEPS REQUIRED ${PIL_DEPENDENCY})
+PKG_CHECK_MODULES(PIL_DEPS REQUIRED ${DEPENDENCY})
INCLUDE_DIRECTORIES(SYSTEM "${PIL_DEPS_INCLUDE_DIRS}")
--- /dev/null
+/*
+ * Copyright (c) 2019 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 <string>
+#include <unordered_map>
+
+#include <klay/rmi/service.h>
+
+#include "policy-model.h"
+
+class DomainPolicy : public AbstractPolicy {
+public:
+ DomainPolicy(const std::string& name) : AbstractPolicy(name) {}
+ virtual ~DomainPolicy() = default;
+
+ DomainPolicy(const DomainPolicy&) = delete;
+ DomainPolicy& operator=(const DomainPolicy&) = delete;
+
+ DomainPolicy(DomainPolicy&&) = default;
+ DomainPolicy& operator=(DomainPolicy&&) = default;
+
+ inline void set(const DataSetInt& val) override
+ {
+ PolicyAdmin admin(rmi::Service::getPeerPid(), rmi::Service::getPeerUid());
+ PolicyStorage::update(id, admin.getName(), admin.getUid(), val);
+
+ std::lock_guard<std::mutex> mtxGuard(mtx);
+ this->enforce(admin.getUid());
+ }
+
+ inline DataSetInt get() override
+ {
+ uid_t domain = rmi::Service::getPeerUid();
+
+ std::lock_guard<std::mutex> mtxGuard(mtx);
+ if (!current.count(domain))
+ enforce(domain);
+
+ return current[domain].value;
+ }
+
+private:
+ inline void enforce(uid_t domain) override
+ {
+ auto value = initial;
+ if (!ready[domain])
+ current[domain] = initial;
+
+ PolicyStorage::strictize(id, value, domain);
+ if (current[domain] != value) {
+ apply(value, domain);
+ current[domain] = value;
+ }
+
+ ready[domain] = true;
+ }
+
+private:
+ std::unordered_map<uid_t, DataSetInt> current;
+ std::unordered_map<uid_t, bool> ready;
+};
--- /dev/null
+/*
+ * Copyright (c) 2019 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 <string>
+#include <unordered_map>
+
+#include <klay/rmi/service.h>
+
+#include "policy-model.h"
+
+class GlobalPolicy : public AbstractPolicy {
+public:
+ GlobalPolicy(const std::string& name) : AbstractPolicy(name) {}
+ virtual ~GlobalPolicy() = default;
+
+ GlobalPolicy(const GlobalPolicy&) = delete;
+ GlobalPolicy& operator=(const GlobalPolicy&) = delete;
+
+ GlobalPolicy(GlobalPolicy&&) = default;
+ GlobalPolicy& operator=(GlobalPolicy&&) = default;
+
+ inline void set(const DataSetInt& val) override
+ {
+ PolicyAdmin admin(rmi::Service::getPeerPid(), rmi::Service::getPeerUid());
+ PolicyStorage::update(id, admin.getName(), admin.getUid(), val);
+
+ std::lock_guard<std::mutex> mtxGuard(mtx);
+ enforce(-1);
+ }
+
+ inline DataSetInt get() override
+ {
+ std::lock_guard<std::mutex> mtxGuard(mtx);
+ if (!ready) {
+ enforce(-1);
+ ready = true;
+ }
+ return current.value;
+ }
+
+private:
+ inline void enforce(uid_t) override
+ {
+ auto value = initial;
+ PolicyStorage::strictize(id, value);
+ if (current != value) {
+ apply(value);
+ current = value;
+ }
+ }
+
+private:
+ DataSetInt current;
+ bool ready = false;
+};
/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 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.
* limitations under the License
*/
-#ifndef __DPM_POLICY_MODEL_H__
-#define __DPM_POLICY_MODEL_H__
+#pragma once
+
#include <string>
#include <vector>
-#include <unordered_map>
-#include <iostream>
#include <mutex>
+#include <memory>
#include <klay/exception.h>
#include "policy-admin.h"
#include "observer.h"
-class AbstractPolicyProvider {
-public:
+class AbstractPolicy;
+
+struct AbstractPolicyProvider {
AbstractPolicyProvider() {}
virtual ~AbstractPolicyProvider() {}
+
+ std::vector<std::shared_ptr<AbstractPolicy>> policies;
};
-template<typename Type>
-class DomainPolicy : public Observer {
+class AbstractPolicy : public Observer {
public:
- typedef Type DataType;
-
- DomainPolicy(const std::string& name);
- virtual ~DomainPolicy();
-
- void set(const Type& value);
- DataType get();
-
- void onEvent(uid_t domain);
-
- virtual bool apply(const Type& value, uid_t domain)
+ explicit AbstractPolicy(const std::string& name) : name(name)
{
- return true;
- }
+ this->id = PolicyStorage::define(name, initial);
+ if (id < 0)
+ throw runtime::Exception("Failed to define policy");
-private:
- void enforce(uid_t domid);
-
-private:
- int id;
- Type initial;
- std::unordered_map<uid_t, Type> current;
- std::unordered_map<uid_t, bool> ready;
- std::mutex mtx;
-};
-
-template<typename Type>
-DomainPolicy<Type>::DomainPolicy(const std::string& name) :
- id(-1), current()
-{
- id = PolicyStorage::define(name, initial);
- if (id < 0) {
- throw runtime::Exception("Failed to define policy");
+ PolicyStorage::subscribeEvent(this);
}
- PolicyStorage::subscribeEvent(this);
-}
-
-template<typename Type>
-DomainPolicy<Type>::~DomainPolicy()
-{
-}
-
-template<typename Type>
-void DomainPolicy<Type>::enforce(uid_t domid)
-{
- Type value = initial;
- if (!ready[domid])
- current[domid] = initial;
-
- PolicyStorage::strictize(id, value, domid);
- if (current[domid] != value) {
- apply(value, domid);
- current[domid] = value;
- }
+ ~AbstractPolicy() = default;
- ready[domid] = true;
-}
+ AbstractPolicy(const AbstractPolicy&) = delete;
+ AbstractPolicy& operator=(const AbstractPolicy&) = delete;
-template<typename Type>
-void DomainPolicy<Type>::set(const DataType& val)
-{
- PolicyAdmin admin(rmi::Service::getPeerPid(), rmi::Service::getPeerUid());
- PolicyStorage::update(id, admin.getName(), admin.getUid(), val);
+ AbstractPolicy(AbstractPolicy&&) = default;
+ AbstractPolicy& operator=(AbstractPolicy&&) = default;
- std::lock_guard<std::mutex> mtxGuard(mtx);
- enforce(admin.getUid());
-}
+ virtual void set(const DataSetInt& value) = 0;
+ virtual DataSetInt get() = 0;
-template<typename Type>
-Type DomainPolicy<Type>::get()
-{
- uid_t domain = rmi::Service::getPeerUid();
+ virtual bool apply(const DataSetInt& value, uid_t domain = 0) = 0;
- std::lock_guard<std::mutex> mtxGuard(mtx);
- if (!current.count(domain)) {
+ inline void onEvent(uid_t domain)
+ {
+ std::lock_guard<std::mutex> mtxGuard(mtx);
enforce(domain);
}
- return current[domain].value;
-}
+ std::string name;
-template<typename Type>
-void DomainPolicy<Type>::onEvent(uid_t domain)
-{
- std::lock_guard<std::mutex> mtxGuard(mtx);
- enforce(domain);
-}
-
-template<typename Type>
-class GlobalPolicy : public Observer {
-public:
- typedef Type DataType;
-
- GlobalPolicy(const std::string& name);
- virtual ~GlobalPolicy();
-
- void set(const Type& value);
- Type get();
-
- void onEvent(uid_t domain);
-
- virtual bool apply(const Type& value)
- {
- return true;
- }
-
-private:
- void enforce();
+protected:
+ int id = -1;
+ std::mutex mtx;
+ DataSetInt initial;
private:
- int id;
- Type initial;
- Type current;
- bool ready;
- std::mutex mtx;
+ virtual void enforce(uid_t domain = 0) = 0;
};
-
-template<typename Type>
-GlobalPolicy<Type>::GlobalPolicy(const std::string& name) :
- id(-1), current(), ready(false)
-{
- id = PolicyStorage::define(name, initial);
- if (id < 0) {
- throw runtime::Exception("Failed to define policy");
- }
-
- current = initial;
- PolicyStorage::subscribeEvent(this);
-}
-
-template<typename Type>
-GlobalPolicy<Type>::~GlobalPolicy()
-{
-}
-
-template<typename Type>
-void GlobalPolicy<Type>::enforce()
-{
- Type value = initial;
- PolicyStorage::strictize(id, value);
- if (current != value) {
- apply(value);
- current = value;
- }
-}
-
-template<typename Type>
-void GlobalPolicy<Type>::set(const Type& val)
-{
- PolicyAdmin admin(rmi::Service::getPeerPid(), rmi::Service::getPeerUid());
- PolicyStorage::update(id, admin.getName(), admin.getUid(), val);
-
- std::lock_guard<std::mutex> mtxGuard(mtx);
- enforce();
-}
-
-template<typename Type>
-Type GlobalPolicy<Type>::get()
-{
- std::lock_guard<std::mutex> mtxGuard(mtx);
- if (!ready) {
- enforce();
- ready = true;
- }
- return current.value;
-}
-
-template<typename Type>
-void GlobalPolicy<Type>::onEvent(uid_t domain)
-{
- std::lock_guard<std::mutex> mtxGuard(mtx);
- enforce();
-}
-#endif //__DPM_POLICY_MODEL_H__
SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fPIE")
SET_TARGET_PROPERTIES(${TARGET} PROPERTIES LINK_FLAGS "-pie")
-INSTALL(TARGETS ${TARGET} DESTINATION ${CMAKE_INSTALL_BINDIR})
+INSTALL(TARGETS ${TARGET} DESTINATION bin)
{
}
-AbstractPolicyProvider* PolicyLoader::instantiate(const std::string& name, PolicyControlContext& context)
+std::shared_ptr<AbstractPolicyProvider> PolicyLoader::instantiate(const std::string& name)
{
PluginMap::iterator iter = pluginMap.find(name);
if (iter == pluginMap.end()) {
pluginMap[name] = std::move(plguin);
- return (*factory)(context);
+ auto provider = (*factory)();
+ return std::shared_ptr<AbstractPolicyProvider>(provider);
}
return nullptr;
/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019 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.
* limitations under the License
*/
-#ifndef __DPM_PLUGIN_H__
-#define __DPM_PLUGIN_H__
+#pragma once
+
#include <string>
#include <exception>
#include <unordered_map>
+#include <memory>
-#include "pil/policy-context.h"
#include "pil/policy-model.h"
class Plugin {
class PolicyLoader {
public:
- typedef AbstractPolicyProvider* (*PolicyFactory)(PolicyControlContext& context);
+ typedef AbstractPolicyProvider* (*PolicyFactory)();
PolicyLoader(const std::string& base);
- AbstractPolicyProvider* instantiate(const std::string& name, rmi::Service& context);
+ std::shared_ptr<AbstractPolicyProvider> instantiate(const std::string& name);
private:
typedef std::unordered_map<std::string, Plugin> PluginMap;
std::string basename;
PluginMap pluginMap;
};
-
-#endif /*!__DPM_PLUGIN_H__*/
continue;
}
- auto instance = policyLoader->instantiate(iter->getName(), *this);
+ auto instance = policyLoader->instantiate(iter->getName());
if (instance == nullptr) {
ERROR(DPM, "Failed to instantiate.: " << iter->getName());
++failed;
continue;
}
- policyList.push_back(instance);
+ providerList.emplace_back(std::move(instance));
++passed;
++iter;
}
void run(int activation, int timeout);
+ // Temporary debug method
+ inline std::vector<std::shared_ptr<AbstractPolicyProvider>>& getProviderList()
+ {
+ return this->providerList;
+ }
+
private:
void initPolicyStorage();
typedef std::unordered_map<int, int> ClientRegistry;
ClientRegistry clientRegistry;
- std::vector<AbstractPolicyProvider *> policyList;
+ std::vector<std::shared_ptr<AbstractPolicyProvider>> providerList;
std::unique_ptr<PolicyLoader> policyLoader;
std::thread policyApplyThread;
-# Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2019 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.
#include <klay/testbench.h>
+#include <iostream>
+
TESTCASE(LOAD_PLUGINS)
{
DevicePolicyManager manager;
auto result = manager.loadPolicyPlugins();
+
+ // Temporary debug method
+ auto providers = manager.getProviderList();
+ for (const auto& provider : providers) {
+ for (const auto& policy : provider->policies) {
+ std::cout << "Policy Lists:" << std::endl;
+ std::cout << "\t" << policy->name << std::endl;
+ }
+ }
+
TEST_EXPECT(true, result.first > 0);
TEST_EXPECT(true, result.second == 0);
}