DPM API mapping
authorAndrey Zabolotnyi <a.zabolotnyi@samsung.com>
Fri, 23 Jun 2017 13:45:27 +0000 (16:45 +0300)
committerAndrey Zabolotnyi <a.zabolotnyi@samsung.com>
Fri, 23 Jun 2017 14:05:18 +0000 (17:05 +0300)
device_core/nmdaemon/CMakeLists.txt
device_core/nmdaemon/dpm/dpm_api_mapper.cpp [new file with mode: 0644]
device_core/nmdaemon/dpm/dpm_api_mapper.h [new file with mode: 0644]
device_core/nmdaemon/dpm/iot_tvext_enforce.cpp
device_core/utest/CMakeLists.txt
device_core/utest/test_dpm_api_mapper.cpp [new file with mode: 0644]

index 4eb117d..0ab649d 100644 (file)
@@ -33,6 +33,7 @@ target_link_libraries (${PROJECT_NAME}
        ${AGENT_LIB_PROJECT_NAME}
        jsoncpp
         boost_system boost_thread boost_serialization
+       dpm
 )
 
 # systemd dependency handling
diff --git a/device_core/nmdaemon/dpm/dpm_api_mapper.cpp b/device_core/nmdaemon/dpm/dpm_api_mapper.cpp
new file mode 100644 (file)
index 0000000..6464375
--- /dev/null
@@ -0,0 +1,556 @@
+#include <functional>
+#include <map>
+
+#include <dpm/device-policy-manager.h>
+#include <dpm/bluetooth.h>
+#include <dpm/wifi.h>
+#include <dpm/restriction.h>
+#include <dpm/application.h>
+#include <dpm/security.h>
+
+#include "dpm_api_mapper.h"
+
+using namespace dpm_api;
+using namespace std;
+
+typedef device_policy_manager_h dpmh;
+
+typedef function<int(dpmh)>  ApiNone;
+typedef function<int(dpmh, int)>  ApiInt;
+typedef function<int(dpmh, const char*)>  ApiStr;
+typedef function<int(dpmh, int, const char*)>  ApiIntStr;
+
+const map<const string, ApiNone> m = {
+               {"lockout-screen",      dpm_security_lockout_screen},
+};
+
+const map<const string, ApiInt> mi = {
+               {"camera",                      dpm_restriction_set_camera_state},
+               {"microphone",          dpm_restriction_set_microphone_state},
+               {"location",            dpm_restriction_set_location_state},
+               {"usb-storage",         dpm_restriction_set_external_storage_state},
+               {"clipboard",           dpm_restriction_set_clipboard_state},
+               {"usb-debug",           dpm_restriction_set_usb_debugging_state},
+               {"wifi",                        dpm_restriction_set_wifi_state},
+               {"wifi-hotspot",        dpm_restriction_set_wifi_hotspot_state},
+               {"bt-tethering",        dpm_restriction_set_bluetooth_tethering_state},
+               {"usb-tethering",       dpm_restriction_set_usb_tethering_state},
+               {"bt-mode-change",      dpm_restriction_set_bluetooth_mode_change_state},
+               {"bt-desktop-conn", dpm_restriction_set_bluetooth_desktop_connectivity_state},
+               {"bt-pairing",          dpm_restriction_set_bluetooth_pairing_state},
+               {"email",                       dpm_restriction_set_popimap_email_state},
+               {"browser",             dpm_restriction_set_browser_state},
+               {"bt-mac",              dpm_bluetooth_set_device_restriction},
+               {"bt-uuid",             dpm_bluetooth_set_uuid_restriction},
+               {"int-stor-enc",        dpm_security_set_internal_storage_encryption},
+               {"ext-stor-enc",        dpm_security_set_external_storage_encryption},
+               {"wifi-profile",        dpm_wifi_set_profile_change_restriction},
+               {"wifi-network",        dpm_wifi_set_network_access_restriction},
+};
+
+const map<const string, ApiStr> ms = {
+               {"bt-mac-add",          dpm_bluetooth_add_device_to_blacklist},
+               {"bt-mac-del",          dpm_bluetooth_remove_device_from_blacklist},
+               {"bt-uuid-add",         dpm_bluetooth_add_uuid_to_blacklist},
+               {"bt-uuid-del",         dpm_bluetooth_remove_uuid_from_blacklist},
+               {"wifi-ssid-add",       dpm_wifi_add_ssid_to_blocklist},
+               {"wifi-ssid-del",       dpm_wifi_remove_ssid_from_blocklist},
+
+};
+
+const map<const string, ApiIntStr> mis;
+
+
+//             {"wipe external data", dpm_security_wipe_data},
+//             {"wipe internal data", dpm_security_wipe_data},
+//             {"messaging",           dpm_restriction_set_messaging_state},
+//             {"privil-add",          dpm_application_add_privilege_to_blacklist},
+//             {"privil-del",          dpm_application_remove_privilege_from_blacklist},
+//             {"package-set", dpm_application_set_mode_restriction},
+//             {"package-unset", dpm_application_unset_mode_restriction},
+
+Mapper::Mapper() : handle(nullptr)
+{
+       handle = (dpmh)dpm_manager_create();
+}
+
+Mapper::~Mapper()
+{
+       if (handle) dpm_manager_destroy((dpmh)handle);
+}
+
+error_code convert_err(int err)
+{
+       switch (err)
+       {
+       case DPM_ERROR_NONE:                            return SUCCESS;
+       case DPM_ERROR_INVALID_PARAMETER:       return INVALID_PARAMETER;
+       case DPM_ERROR_CONNECTION_REFUSED:      return CONNECTION_REFUSED;
+       case DPM_ERROR_TIMED_OUT:                       return TIMED_OUT;
+       case DPM_ERROR_PERMISSION_DENIED:       return PERMISSION_DENIED;
+       case DPM_ERROR_NOT_PERMITTED:           return NOT_PERMITTED;
+       case DPM_ERROR_FILE_EXISTS:                     return FILE_EXISTS;
+       case DPM_ERROR_OUT_OF_MEMORY:           return OUT_OF_MEMORY;
+       case DPM_ERROR_NO_DATA:                         return NO_DATA;
+       default:
+               return UNKNOWN;
+       }
+       return UNKNOWN;
+}
+
+const char* Mapper::get_error_string(error_code err)
+{
+       struct errs
+       {
+               error_code      code;
+               const char      *name;
+       };
+
+       static const errs errs_arr[] =
+       {
+               {SUCCESS,                               "The operation was successful"},
+               {INVALID_PARAMETER,     "Invalid parameter"},
+               {CONNECTION_REFUSED,    "Connection refused"},
+               {TIMED_OUT,             "Time out"},
+               {PERMISSION_DENIED,     "Access privilege is not sufficient"},
+               {NOT_PERMITTED,         "Operation not permitted"},
+               {FILE_EXISTS,           "File exists"},
+               {OUT_OF_MEMORY,         "Out of memory"},
+               {NO_DATA,               "No Data"},
+               {NOT_INITIALIZED,       "Library not initialized"},
+               {NAME_NOT_FOUND,        "Policy's name not found"},
+               {UNKNOWN,                       "Unknown error"},
+       };
+
+       for(unsigned int i = 0; i < sizeof(errs_arr) / sizeof(errs_arr[0]); ++i)
+       {
+               if (errs_arr[i].code == err) return errs_arr[i].name;
+       }
+
+       return "Unknown";
+}
+
+error_code Mapper::apply(const std::string &name, const int param, const vector<string> &items)
+{
+       int res = 0;
+       if (!handle) return NOT_INITIALIZED;
+
+       auto iti = mi.find(name);
+       if (iti != mi.end())
+               return convert_err(iti->second((dpmh)handle, param));
+
+       auto itn = m.find(name);
+       if (itn != m.end())
+               return convert_err(itn->second((dpmh)handle));
+
+       auto its = ms.find(name);
+       if (its != ms.end())
+       {
+               for (auto & item : items)
+               {
+                       res = its->second((dpmh)handle, item.c_str());
+                       if (res != DPM_ERROR_NONE) break;
+               }
+               return convert_err(res);
+       }
+
+       auto itis = mis.find(name);
+       if (itis != mis.end())
+       {
+               for (auto & item : items)
+               {
+                       res = itis->second((dpmh)handle, param, item.c_str());
+                       if (res != DPM_ERROR_NONE) break;
+               }
+               return convert_err(res);
+       }
+
+       return NAME_NOT_FOUND;
+}
+
+
+//dpm_error dpm_api_mapper::get_state(int (*f)(void*, int*), bool &allow)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int is_allow = 0;
+//     int res = f(handle, &is_allow);
+//     allow = (is_allow != 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::set_state(int (*f)(void*, int), bool allow)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = f(handle, allow ? 1 : 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::set_camera_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_camera_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_camera_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_camera_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_microphone_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_microphone_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_microphone_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_microphone_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_location_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_location_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_location_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_location_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_external_storage_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_external_storage_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_external_storage_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_external_storage_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_clipboard_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_clipboard_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_clipboard_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_clipboard_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_usb_debugging_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_usb_debugging_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_usb_debugging_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_usb_debugging_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_wifi_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_wifi_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_wifi_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_wifi_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_wifi_hotspot_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_wifi_hotspot_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_wifi_hotspot_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_wifi_hotspot_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_bluetooth_tethering_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_bluetooth_tethering_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_bluetooth_tethering_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_bluetooth_tethering_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_usb_tethering_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_usb_tethering_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_usb_tethering_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_usb_tethering_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_bluetooth_mode_change_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_bluetooth_mode_change_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_bluetooth_mode_change_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_bluetooth_mode_change_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_bluetooth_desktop_connectivity_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_bluetooth_desktop_connectivity_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_bluetooth_desktop_connectivity_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_bluetooth_desktop_connectivity_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_bluetooth_pairing_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_bluetooth_pairing_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_bluetooth_pairing_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_bluetooth_pairing_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_messaging_state(bool allow)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_restriction_set_messaging_state((dpmh)handle, "sim_id", allow ? 1 : 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::get_messaging_state(bool &allow)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int is_allow = 0;
+//     int res = dpm_restriction_get_messaging_state((dpmh)handle, "sim_id", &is_allow);
+//     allow = (is_allow != 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::set_popimap_email_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_popimap_email_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_popimap_email_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_popimap_email_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::set_browser_state(bool allow)
+//{
+//     return set_state(dpm_restriction_set_browser_state, allow);
+//}
+//
+//dpm_error dpm_api_mapper::get_browser_state(bool &allow)
+//{
+//     return get_state(dpm_restriction_get_browser_state, allow);
+//}
+//
+//// application
+//
+//dpm_error dpm_api_mapper::set_package_restriction(package_mode mode, bool allow)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//
+//     int m = 0;
+//     if (mode == PACKAGE_INSTALL) m = DPM_PACKAGE_RESTRICTION_MODE_INSTALL;
+//     else if (mode == PACKAGE_UNINSTALL) m = DPM_PACKAGE_RESTRICTION_MODE_UNINSTALL;
+//     else if (mode == PACKAGE_REINSTALL) m = DPM_PACKAGE_RESTRICTION_MODE_REINSTALL;
+//     else if (mode == PACKAGE_MOVE) m = DPM_PACKAGE_RESTRICTION_MODE_MOVE;
+//     else
+//             return DPM_INVALID_PARAMETER;
+//
+//     int res = allow ?
+//                     dpm_application_unset_mode_restriction((dpmh)handle, m) :
+//                     dpm_application_set_mode_restriction((dpmh)handle, m);
+//
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::get_package_restriction(package_mode mode, bool &allow)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int is_allow = 0;
+//     int res = dpm_application_get_mode_restriction((dpmh)handle, &is_allow);
+//     allow = (is_allow != 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::add_privilege_to_blacklist(const char* privilege)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_application_add_privilege_to_blacklist((dpmh)handle, 0, privilege);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::remove_privilege_from_blacklist(const char* privilege)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_application_remove_privilege_from_blacklist((dpmh)handle, 0, privilege);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::check_privilege_is_blacklisted(const char* privilege, bool &blacklisted)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int is_blacklisted;
+//     int res = dpm_application_check_privilege_is_blacklisted((dpmh)handle, 0, privilege, &is_blacklisted);
+//     blacklisted = (is_blacklisted == 1);
+//     return convert_err(res);
+//}
+//
+//// bluetooth
+//
+//dpm_error dpm_api_mapper::bluetooth_add_device_to_blacklist(const char* address)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_bluetooth_add_device_to_blacklist((dpmh)handle, address);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::bluetooth_remove_device_from_blacklist(const char* address)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_bluetooth_remove_device_from_blacklist((dpmh)handle, address);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::bluetooth_set_device_restriction(bool enable)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_bluetooth_set_device_restriction((dpmh)handle, enable ? 1 : 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::bluetooth_is_device_restricted(bool &is_enabled)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int x = 0;
+//     int res = dpm_bluetooth_is_device_restricted((dpmh)handle, &x);
+//     is_enabled = (x != 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::bluetooth_add_uuid_to_blacklist(const char* uuid)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_bluetooth_add_uuid_to_blacklist((dpmh)handle, uuid);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::bluetooth_remove_uuid_from_blacklist(const char* uuid)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_bluetooth_remove_uuid_from_blacklist((dpmh)handle, uuid);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::bluetooth_set_uuid_restriction(bool enable)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_bluetooth_set_uuid_restriction((dpmh)handle, enable);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::bluetooth_is_uuid_restricted(bool &is_enabled)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int x = 0;
+//     int res = dpm_bluetooth_is_uuid_restricted((dpmh)handle, &x);
+//     is_enabled = (x != 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::lockout_screen()
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_security_lockout_screen((dpmh)handle);
+//     return convert_err(res);
+//}
+//dpm_error dpm_api_mapper::set_internal_storage_encryption(bool encrypt)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_security_set_internal_storage_encryption((dpmh)handle, encrypt ? 1 : 0);
+//     return convert_err(res);
+//}
+//dpm_error dpm_api_mapper::is_internal_storage_encrypted(bool &is_encrypted)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int x = 0;
+//     int res = dpm_security_is_internal_storage_encrypted((dpmh)handle, &x);
+//     is_encrypted = (x != 0);
+//     return convert_err(res);
+//}
+//dpm_error dpm_api_mapper::set_external_storage_encryption(bool encrypt)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_security_set_external_storage_encryption((dpmh)handle, encrypt ? 1 : 0);
+//     return convert_err(res);
+//}
+//dpm_error dpm_api_mapper::is_external_storage_encrypted(bool &is_encrypted)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int x = 0;
+//     int res = dpm_security_is_external_storage_encrypted((dpmh)handle, &x);
+//     is_encrypted = (x != 0);
+//     return convert_err(res);
+//}
+//dpm_error dpm_api_mapper::wipe_external_data()
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_security_wipe_data((dpmh)handle, DPM_SECURITY_WIPE_EXTERNAL_STORAGE);
+//     return convert_err(res);
+//}
+//dpm_error dpm_api_mapper::wipe_internal_data()
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_security_wipe_data((dpmh)handle, DPM_SECURITY_WIPE_INTERNAL_STORAGE);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::wifi_set_profile_change_restriction(bool enable)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_wifi_set_profile_change_restriction((dpmh)handle, enable ? 1 : 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::wifi_is_profile_change_restricted(bool &is_enabled)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_wifi_is_profile_change_restricted((dpmh)handle, (int*)&is_enabled);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::wifi_set_network_access_restriction(bool enable)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_wifi_set_network_access_restriction((dpmh)handle, enable ? 1 : 0);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::wifi_is_network_access_restricted(bool &is_enabled)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_wifi_is_network_access_restricted((dpmh)handle, (int*)&is_enabled);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::wifi_add_ssid_to_blocklist(const char* ssid)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_wifi_add_ssid_to_blocklist((dpmh)handle, ssid);
+//     return convert_err(res);
+//}
+//
+//dpm_error dpm_api_mapper::wifi_remove_ssid_from_blocklist(const char* ssid)
+//{
+//     if (!handle) return DPM_NOT_INITIALIZED;
+//     int res = dpm_wifi_remove_ssid_from_blocklist((dpmh)handle, ssid);
+//     return convert_err(res);
+//}
diff --git a/device_core/nmdaemon/dpm/dpm_api_mapper.h b/device_core/nmdaemon/dpm/dpm_api_mapper.h
new file mode 100644 (file)
index 0000000..07ec3ab
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef DPM_API_MAPPER_CPP_
+#define DPM_API_MAPPER_CPP_
+
+#include <string>
+#include <vector>
+
+namespace dpm_api
+{
+enum error_code
+{
+       SUCCESS,                                /**< The operation was successful */
+       INVALID_PARAMETER,      /**< Invalid parameter */
+       CONNECTION_REFUSED,     /**< Connection refused */
+       TIMED_OUT,              /**< Time out */
+       PERMISSION_DENIED,      /**< Access privilege is not sufficient */
+       NOT_PERMITTED,          /**< Operation not permitted */
+       FILE_EXISTS,            /**< File exists */
+       OUT_OF_MEMORY,          /**< Out of memory */
+       NO_DATA,                /**< No Data */
+       NOT_INITIALIZED,        /**< Library not initialized */
+       NAME_NOT_FOUND,                 /**< Policy's name not found */
+       UNKNOWN,                        /**< Unknown error */
+};
+
+class Mapper
+{
+       void *handle;
+
+public:
+
+       Mapper();
+       ~Mapper();
+
+       const char* get_error_string(error_code err);
+
+       error_code apply(const std::string &name, const int param, const std::vector<std::string> &items);
+};
+}
+
+#endif
index 7219a81..c709b7a 100644 (file)
@@ -11,8 +11,8 @@
 
 
 #include "iot_tvext_enforce.h"
-
 #include "logging.h"
+#include "dpm_api_mapper.h"
 
 #define TAG "nmdaemon"
 
@@ -48,45 +48,59 @@ bool TvExtPolicyEnforce::ParseGroup(Json::Value& groupList)
 {
     LOG_D(TAG, "...Start tvext policies parsing and applying...");
 
+    dpm_api::Mapper mapper;
+
     for (auto & node : groupList)
     {
         std::string name = node.get("name", "").asString();
         int         state = node.get("state", 0).asInt();
+        Json::Value items = node.get("items", "");
 
         if (name.empty())
         {
             continue; //TODO or return error
         }
         
-        if ("usb" == name)
-        {
-            LOG_D(TAG, "Enforce policy [%s] to state %d", name.c_str(), state);
-        }
-        else if ("screen-capture" == name)
-        {
-            LOG_D(TAG, "Enforce policy [%s] to state %d", name.c_str(), state);
-        }
-        else if ("bluetooth" == name)
-        {
-            LOG_D(TAG, "Enforce policy [%s] to state %d", name.c_str(), state);
-        }
-        else if ("iptables" == name)
-        {
-            Json::Value items = node.get("items", "");
-            if (items.empty())
-            {
-                LOG_E(TAG, "No items node for iptables policy");
-                continue; // TODO or return error
-            }
-            for (auto & item : items)
-            {
-                LOG_D(TAG, "Enforce policy [%s] wit item %s", name.c_str(), item.asString().c_str() );
-            }
-        }
-        else 
-        {
-            LOG_D(TAG, "Unknown policy found: %s", name.c_str());
-        }
+        dpm_api::error_code err;
+
+        std::vector<std::string> v;
+        for (auto & item : items) v.push_back(item.asString());
+
+        LOG_D(TAG, "Enforce policy [%s] to state %d", name.c_str(), state);
+        err = mapper.apply(name, state, v);
+        if (err != dpm_api::SUCCESS)
+               LOG_E(TAG, "Enforce policy [%s] error: %s", name.c_str(), mapper.get_error_string(err));
+
+//        if ("usb" == name)
+//        {
+//            LOG_D(TAG, "Enforce policy [%s] to state %d", name.c_str(), state);
+//            dpm_restriction_set_external_storage_state(dpmh, state);
+//        }
+//        else if ("screen-capture" == name)
+//        {
+//            LOG_D(TAG, "Enforce policy [%s] to state %d", name.c_str(), state);
+//        }
+//        else if ("bluetooth" == name)
+//        {
+//            LOG_D(TAG, "Enforce policy [%s] to state %d", name.c_str(), state);
+//            dpm_restriction_set_bluetooth_tethering_state(dpmh, state);
+//        }
+//        else if ("iptables" == name)
+//        {
+//            if (items.empty())
+//            {
+//                LOG_E(TAG, "No items node for iptables policy");
+//                continue; // TODO or return error
+//            }
+//            for (auto & item : items)
+//            {
+//                LOG_D(TAG, "Enforce policy [%s] wit item %s", name.c_str(), item.asString().c_str() );
+//            }
+//        }
+//        else
+//        {
+//            LOG_D(TAG, "Unknown policy found: %s", name.c_str());
+//        }
 
     }
     LOG_D(TAG, "...Finish tvext policies parsing and applying...");
index 09789d5..be0a6d4 100644 (file)
@@ -18,6 +18,7 @@ FILE(GLOB SRCS *.cpp
        ../iotivity_lib/IoT/*.cpp
        ../nmdaemon/agentpolicyservice.cpp
        ../nmdaemon/device_list.cpp
+       ../nmdaemon/dpm/dpm_api_mapper.cpp
        )
 
 add_executable (${PROJECT_NAME} ${SRCS})
@@ -28,6 +29,7 @@ set (TEST_LINK_LIBRARIES ${GTEST_LIB}
        ${AGENT_LIB_PROJECT_NAME}
        jsoncpp
         boost_system boost_thread boost_serialization
+       dpm
 )
 
 if ("${FLAVOR}" STREQUAL "UBUNTU")
diff --git a/device_core/utest/test_dpm_api_mapper.cpp b/device_core/utest/test_dpm_api_mapper.cpp
new file mode 100644 (file)
index 0000000..61bcbfd
--- /dev/null
@@ -0,0 +1,24 @@
+#include <cstdio>
+#include <iostream>
+#include <gtest/gtest.h>
+#include "dpm/dpm_api_mapper.h"
+
+using namespace dpm_api;
+using namespace std;
+
+TEST(Test_DPM_API, test)
+{
+       Mapper mapper;
+
+       const vector<string> v = {
+                       "item 1",
+                       "item 2",
+                       "item 3",
+       };
+
+       ASSERT_EQ(mapper.apply("camera", 1, v), dpm_api::NOT_PERMITTED);
+       ASSERT_EQ(mapper.apply("microphone", 1, v), dpm_api::NOT_PERMITTED);
+       ASSERT_EQ(mapper.apply("clipboard", 1, v), dpm_api::NOT_PERMITTED);
+       ASSERT_EQ(mapper.apply("bt-mac-add", 1, v), dpm_api::UNKNOWN);
+}
+