Creating JSON policy files. Fix minor bugs
authorAndrey Zabolotnyi <a.zabolotnyi@samsung.com>
Mon, 8 May 2017 09:41:52 +0000 (12:41 +0300)
committerAndrey Zabolotnyi <a.zabolotnyi@samsung.com>
Mon, 8 May 2017 13:26:45 +0000 (16:26 +0300)
network-manager/CMakeLists.txt
network-manager/agent/main.cpp
network-manager/nmlib/IoT/src/iotivity.cpp
network-manager/nmlib/dpm/inc/dpm.h
network-manager/nmlib/dpm/src/dpm.cpp
network-manager/nmlib/include/iotivity.h
network-manager/nmlib/json_policy/inc/json_policy.h
network-manager/nmlib/json_policy/src/json_policy.cpp

index d7c6de6..1d79c4c 100644 (file)
@@ -43,12 +43,12 @@ IF("${FLAVOR}" STREQUAL "UBUNTU")
                $ENV{IOTIVITY_HOME}/resource/csdk/logger/include
                $ENV{IOTIVITY_HOME}/resource/csdk/security/include
                $ENV{IOTIVITY_HOME}/resource/csdk/connectivity/api
-               $ENV{IOTIVITY_HOME}/resource/csdk/resource-directory/include
+               $ENV{IOTIVITY_HOME}/resource/csdk/resource-directory/include/
                $ENV{IOTIVITY_HOME}/extlibs/cjson
-                $ENV{IOTIVITY_HOME}/service/easy-setup/mediator/richsdk/inc
-                $ENV{IOTIVITY_HOME}/service/easy-setup/enrollee/linux/wifi
-                $ENV{IOTIVITY_HOME}/service/easy-setup/enrollee/inc
-                $ENV{IOTIVITY_HOME}/service/easy-setup/inc
+#              $ENV{IOTIVITY_HOME}/service/easy-setup/mediator/richsdk/inc
+#              $ENV{IOTIVITY_HOME}/service/easy-setup/enrollee/linux/wifi
+#              $ENV{IOTIVITY_HOME}/service/easy-setup/enrollee/inc
+#              $ENV{IOTIVITY_HOME}/service/easy-setup/inc
        )
        #SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -isystem $ENV{TIZEN_ROOT_3_0}/usr/include")
 
@@ -79,7 +79,7 @@ else()
                SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
        ENDFOREACH(flag)
 
-        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -DTB_LOG -D__TIZEN__")
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -DTB_LOG -D__TIZEN__")
 
        # Remove unwanted flag
        string(REPLACE "-D__WITH_TLS__=1" "" EXTRA_CFLAGS ${EXTRA_CFLAGS})
@@ -91,6 +91,7 @@ endif()
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -D__LOGGING__ ")
 
 include_directories(common/inc)
+include_directories(nmlib/include)
 
 message(STATUS "EXTRA_CFLAGS: " ${EXTRA_CFLAGS})
 
index 4d121cb..cc9139c 100644 (file)
@@ -3,12 +3,11 @@
 #include <string.h>
 #include <cstdlib>
 #include "dpm.h"
-#include "../nmlib/json_policy/inc/json_policy.h"
 
 char s[1024];
 
 DPM *dpm;
-char *id;
+char *id = (char*)"agent";
 
 void print_menu(const char *state_s, const char *menu_s)
 {
@@ -653,9 +652,10 @@ const char* pname[] = {
        "browser",
 
        "pack_install",
-       "pack_reinstall",
        "pack_uninstall",
+       "pack_reinstall",
        "pack_move",
+       "pack_all",
 
        "add_ptobl", // add_privilege_to_blacklist
        "remove_pfrombl", // remove_privilege_from_blacklist
@@ -779,6 +779,8 @@ void process_policy(FILE *f)
                else if (!strcmp(s, pname[c++]))
                    err = dpm->wifi_remove_ssid_from_blocklist(param);
 
+               //if (err != DPM_OK) printf("Error: %i\n", (int)err);
+
        }
        catch (const char *e)
        {
@@ -792,13 +794,12 @@ int main(int argc, char* argv[])
        printf("#### Reference Agent App ####\n");
        printf("#############################\n");
 
-       dpm = new DPM;
+       if (argc > 1) id = argv[1];
+       printf("AgentID: %s\n", id);
+       dpm = new DPM(id);
 
        if (argc == 3)
        {
-               id = argv[1];
-               printf("ID: %s\n", id);
-
                char *fname = argv[2];
                FILE *f = fopen(fname, "r");
                if (f)
@@ -831,45 +832,45 @@ int main(int argc, char* argv[])
                }
                else
                        printf("[%s] error: can't open file %s\n", id, fname);
-
-               return 0;
        }
-
-       try
+       else
        {
-               while (1)
+               try
                {
-                       printf("\n==============\n");
-                       printf("Select module:\n");
-                       printf(" 1 - Application\n");
-                       printf(" 2 - Bluetooth\n");
-                       printf(" 3 - Restriction\n");
-                       printf(" 4 - Sequrity\n");
-                       printf(" 5 - WiFi\n");
-                       printf(" q - Exit\n");
-
-                       while(1)
+                       while (1)
                        {
-                               res = scanf("%s", s);
-                               if (!strcmp(s, "q")) throw 0;
-                               else if (!strcmp(s, "1")) menu_app();
-                               else if (!strcmp(s, "2")) menu_bt();
-                               else if (!strcmp(s, "3")) menu_restrict();
-                               else if (!strcmp(s, "4")) menu_sequrity();
-                               else if (!strcmp(s, "5")) menu_wifi();
-                               else
+                               printf("\n==============\n");
+                               printf("Select module:\n");
+                               printf(" 1 - Application\n");
+                               printf(" 2 - Bluetooth\n");
+                               printf(" 3 - Restriction\n");
+                               printf(" 4 - Sequrity\n");
+                               printf(" 5 - WiFi\n");
+                               printf(" q - Exit\n");
+
+                               while(1)
                                {
-                                       printf("Wrong input\n");
-                                       continue;
-                               }
+                                       res = scanf("%s", s);
+                                       if (!strcmp(s, "q")) throw 0;
+                                       else if (!strcmp(s, "1")) menu_app();
+                                       else if (!strcmp(s, "2")) menu_bt();
+                                       else if (!strcmp(s, "3")) menu_restrict();
+                                       else if (!strcmp(s, "4")) menu_sequrity();
+                                       else if (!strcmp(s, "5")) menu_wifi();
+                                       else
+                                       {
+                                               printf("Wrong input\n");
+                                               continue;
+                                       }
 
-                               break;
+                                       break;
+                               }
                        }
                }
-       }
-       catch(...)
-       {
+               catch(...)
+               {
 
+               }
        }
 
        delete dpm;
index 03d08ac..9709750 100644 (file)
@@ -446,4 +446,31 @@ bool IoTivity::isSignedIn()
     return signedIn;
 }
 
+using namespace std;
+
+string IoTivity::getDeviceID()
+{
+       OCUUIdentity deviceId;
+       OCStackResult res = OC::OCPlatform::getDeviceId(&deviceId);
+       if (res != OC_STACK_OK)
+       {
+               cout << "OCPlatform::getDeviceId error: " << res << endl;
+               return string("");
+       }
+
+       char s[128];
+       unsigned char *id = deviceId.id;
+
+       snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+                       id[0],                  id[1],                  id[2],                  id[3],
+                       id[4],                  id[5],                  id[6],                  id[7],
+                       id[8],                  id[9],                  id[10],                 id[11],
+                       id[12],                 id[13],                 id[14],                 id[15]);
+
+       cout << "IoTivity::getDeviceID(): " << s << endl;
+
+       return string(s);//"b519ea4c-30cc-11e7-93ae-92361f002671";}
+
+}
+
 }
index c03c40b..951e18c 100644 (file)
@@ -1,13 +1,11 @@
-/*
- * dpm.h
- *
- *  Created on: Apr 12, 2017
- *      Author: surc
- */
-
 #ifndef DPMLIB_H_
 #define DPMLIB_H_
 
+#include "../../json_policy/inc/json_policy.h"
+#include "iotivity.h"
+
+using namespace std;
+
 enum dpm_error
 {
        DPM_OK,                                         /**< The operation was successful */
@@ -25,13 +23,17 @@ enum dpm_error
 
 class DPM
 {
-       void *handle;
+       void    *handle;
+       string  sender;
+       NetworkManager::IoTivity *iot;
 
 public:
-       DPM();
+       DPM(const char *sender_id);
        ~DPM();
 
        const char* get_error_string(dpm_error err);
+       void            UpdatePolicy(const char *name, Policy::Action action, const char *param = "");
+       void            SendJSON(string json);
 
        dpm_error set_camera_state(bool allow);
        dpm_error get_camera_state(bool &allow);
@@ -108,7 +110,7 @@ public:
 private:
        dpm_error convert_err(int err);
        dpm_error get_state(int (*f)(void*, int*), bool &allow);
-       dpm_error set_state(int (*f)(void*, int), bool allow);
+       dpm_error set_state(int (*f)(void*, int), bool allow, int policy_num);
 };
 
 
index be40931..e48beba 100644 (file)
 
 typedef device_policy_manager_h dpmh;
 
-DPM::DPM() : handle(nullptr)
+DPM::DPM(const char *sender_id) :
+               handle(nullptr),
+               sender(sender_id)
 {
        handle = (dpmh)dpm_manager_create();
+       printf("DPM::DPM()\n");
+       iot = NetworkManager::IoTivity::getInstance();
 }
 
 DPM::~DPM()
@@ -78,6 +82,107 @@ const char* DPM::get_error_string(dpm_error err)
        return "Unknown";
 }
 
+void DPM::UpdatePolicy(const char *name, Policy::Action action, const char *param)
+{
+       try
+       {
+               string json;
+               PolicyUpdate p(sender, string(name), action, string(param), json);
+               SendJSON(json);
+       }
+       catch (PolicyException &e) {
+               printf("Exception: %s\n", e.what());
+       }
+}
+
+void DPM::SendJSON(string json)
+{
+       // TODO: sending json policy
+       printf("$$==============\n");
+       printf("JSON:\n");
+       printf("%s", json.c_str());
+       printf("================\n");
+}
+
+const char* policy[] = {
+       "Camera", // 0
+       "Microphone",
+       "Location",
+       "USB mass storage",
+       "Clipboard",
+       "Debugging", // 5
+       "WiFi",
+       "WiFi hotspot",
+       "Bluetooth tethering",
+       "USB tethering",
+       "Bluetooth mode change", // 10
+       "Bluetooth desktop conn",
+       "Bluetooth pairing",
+       "SMS or text messaging",
+       "POP or IMAP email",
+       "WEB browser", // 15
+
+       "package", // params: install reinstall uninstall move all
+
+       "privileges blacklist", // add_privilege_to_blacklist
+
+       "bluetooth MAC blacklist", // bluetooth_add_device_to_blacklist
+       "bluetooth MAC restriction", // bluetooth_set_device_restriction
+       "bluetooth UUID blacklist", // bluetooth_add_uuid_to_blacklist
+       "bluetooth UUID restriction", // bluetooth_set_device_restriction
+
+       "lockout screen", // lockout_screen();
+       "internal storage encryption", // set_internal_storage_encryption(bool encrypt);
+       "external storage encryption", // set_external_storage_encryption(bool encrypt);
+       "wipe external data", // wipe_external_data();
+       "wipe internal data", // wipe_internal_data();
+
+       "WiFi profile change", // wifi_set_profile_change_restriction(bool enable);
+       "WiFi network access", // wifi_set_network_access_restriction(bool enable);
+       "WiFi SSID blacklist", // wifi_add_ssid_to_blocklist(const char* ssid);
+
+};
+
+enum policy_name : int {
+       Camera = 0,
+       Microphone,
+       Location,
+       USB_mass_storage,
+       Clipboard,
+       Debugging, // 5
+       WiFi,
+       WiFi_hotspot,
+       Bluetooth_tethering,
+       USB_tethering,
+       Bluetooth_mode_change, // 10
+       Bluetooth_desktop_conn,
+       Bluetooth_pairing,
+       SMS_or_text_messaging,
+       POP_or_IMAP_email,
+       WEB_browser, // 15
+
+       package, // params: install reinstall uninstall move all
+
+       privileges_blacklist, // add_privilege_to_blacklist
+
+       bluetooth_MAC_blacklist, // bluetooth_add_device_to_blacklist
+       bluetooth_MAC_restriction, // bluetooth_set_device_restriction
+       bluetooth_UUID_blacklist, // bluetooth_add_uuid_to_blacklist
+       bluetooth_UUID_restriction, // bluetooth_set_device_restriction
+
+       lockout_screen_, // lockout_screen();
+       internal_storage_encryption_, // set_internal_storage_encryption(bool encrypt);
+       external_storage_encryption_, // set_external_storage_encryption(bool encrypt);
+       wipe_external_data_, // wipe_external_data();
+       wipe_internal_data_, // wipe_internal_data();
+
+       WiFi_profile_change_, // wifi_set_profile_change_restriction(bool enable);
+       WiFi_network_access_, // wifi_set_network_access_restriction(bool enable);
+       WiFi_SSID_blacklist_, // wifi_add_ssid_to_blocklist(const char* ssid);
+
+};
+
+
 dpm_error DPM::get_state(int (*f)(void*, int*), bool &allow)
 {
        if (!handle) return DPM_NOT_INITIALIZED;
@@ -87,16 +192,23 @@ dpm_error DPM::get_state(int (*f)(void*, int*), bool &allow)
        return convert_err(res);
 }
 
-dpm_error DPM::set_state(int (*f)(void*, int), bool allow)
+dpm_error DPM::set_state(int (*f)(void*, int), bool allow, int policy_num)
 {
        if (!handle) return DPM_NOT_INITIALIZED;
+       if (policy_num < 0 || policy_num >= int(sizeof(policy)/sizeof(policy[0])))
+               return DPM_INVALID_PARAMETER;
+
        int res = f(handle, allow ? 1 : 0);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[policy_num],
+                               allow ? Policy::ALLOW : Policy::RESTRICT);
+
        return convert_err(res);
 }
 
 dpm_error DPM::set_camera_state(bool allow)
 {
-       return set_state(dpm_restriction_set_camera_state, allow);
+       return set_state(dpm_restriction_set_camera_state, allow, Camera);
 }
 
 dpm_error DPM::get_camera_state(bool &allow)
@@ -106,7 +218,7 @@ dpm_error DPM::get_camera_state(bool &allow)
 
 dpm_error DPM::set_microphone_state(bool allow)
 {
-       return set_state(dpm_restriction_set_microphone_state, allow);
+       return set_state(dpm_restriction_set_microphone_state, allow, Microphone);
 }
 
 dpm_error DPM::get_microphone_state(bool &allow)
@@ -116,7 +228,7 @@ dpm_error DPM::get_microphone_state(bool &allow)
 
 dpm_error DPM::set_location_state(bool allow)
 {
-       return set_state(dpm_restriction_set_location_state, allow);
+       return set_state(dpm_restriction_set_location_state, allow, Location);
 }
 
 dpm_error DPM::get_location_state(bool &allow)
@@ -126,7 +238,7 @@ dpm_error DPM::get_location_state(bool &allow)
 
 dpm_error DPM::set_external_storage_state(bool allow)
 {
-       return set_state(dpm_restriction_set_external_storage_state, allow);
+       return set_state(dpm_restriction_set_external_storage_state, allow, external_storage_encryption_);
 }
 
 dpm_error DPM::get_external_storage_state(bool &allow)
@@ -136,7 +248,7 @@ dpm_error DPM::get_external_storage_state(bool &allow)
 
 dpm_error DPM::set_clipboard_state(bool allow)
 {
-       return set_state(dpm_restriction_set_clipboard_state, allow);
+       return set_state(dpm_restriction_set_clipboard_state, allow, Clipboard);
 }
 
 dpm_error DPM::get_clipboard_state(bool &allow)
@@ -146,7 +258,7 @@ dpm_error DPM::get_clipboard_state(bool &allow)
 
 dpm_error DPM::set_usb_debugging_state(bool allow)
 {
-       return set_state(dpm_restriction_set_usb_debugging_state, allow);
+       return set_state(dpm_restriction_set_usb_debugging_state, allow, Debugging);
 }
 
 dpm_error DPM::get_usb_debugging_state(bool &allow)
@@ -156,7 +268,7 @@ dpm_error DPM::get_usb_debugging_state(bool &allow)
 
 dpm_error DPM::set_wifi_state(bool allow)
 {
-       return set_state(dpm_restriction_set_wifi_state, allow);
+       return set_state(dpm_restriction_set_wifi_state, allow, WiFi);
 }
 
 dpm_error DPM::get_wifi_state(bool &allow)
@@ -166,7 +278,7 @@ dpm_error DPM::get_wifi_state(bool &allow)
 
 dpm_error DPM::set_wifi_hotspot_state(bool allow)
 {
-       return set_state(dpm_restriction_set_wifi_hotspot_state, allow);
+       return set_state(dpm_restriction_set_wifi_hotspot_state, allow, WiFi_hotspot);
 }
 
 dpm_error DPM::get_wifi_hotspot_state(bool &allow)
@@ -176,7 +288,7 @@ dpm_error DPM::get_wifi_hotspot_state(bool &allow)
 
 dpm_error DPM::set_bluetooth_tethering_state(bool allow)
 {
-       return set_state(dpm_restriction_set_bluetooth_tethering_state, allow);
+       return set_state(dpm_restriction_set_bluetooth_tethering_state, allow, Bluetooth_tethering);
 }
 
 dpm_error DPM::get_bluetooth_tethering_state(bool &allow)
@@ -186,7 +298,7 @@ dpm_error DPM::get_bluetooth_tethering_state(bool &allow)
 
 dpm_error DPM::set_usb_tethering_state(bool allow)
 {
-       return set_state(dpm_restriction_set_usb_tethering_state, allow);
+       return set_state(dpm_restriction_set_usb_tethering_state, allow, USB_tethering);
 }
 
 dpm_error DPM::get_usb_tethering_state(bool &allow)
@@ -196,7 +308,7 @@ dpm_error DPM::get_usb_tethering_state(bool &allow)
 
 dpm_error DPM::set_bluetooth_mode_change_state(bool allow)
 {
-       return set_state(dpm_restriction_set_bluetooth_mode_change_state, allow);
+       return set_state(dpm_restriction_set_bluetooth_mode_change_state, allow, Bluetooth_mode_change);
 }
 
 dpm_error DPM::get_bluetooth_mode_change_state(bool &allow)
@@ -206,7 +318,7 @@ dpm_error DPM::get_bluetooth_mode_change_state(bool &allow)
 
 dpm_error DPM::set_bluetooth_desktop_connectivity_state(bool allow)
 {
-       return set_state(dpm_restriction_set_bluetooth_desktop_connectivity_state, allow);
+       return set_state(dpm_restriction_set_bluetooth_desktop_connectivity_state, allow, Bluetooth_desktop_conn);
 }
 
 dpm_error DPM::get_bluetooth_desktop_connectivity_state(bool &allow)
@@ -216,7 +328,7 @@ dpm_error DPM::get_bluetooth_desktop_connectivity_state(bool &allow)
 
 dpm_error DPM::set_bluetooth_pairing_state(bool allow)
 {
-       return set_state(dpm_restriction_set_bluetooth_pairing_state, allow);
+       return set_state(dpm_restriction_set_bluetooth_pairing_state, allow, Bluetooth_pairing);
 }
 
 dpm_error DPM::get_bluetooth_pairing_state(bool &allow)
@@ -228,6 +340,9 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[SMS_or_text_messaging],
+                               allow ? Policy::ALLOW : Policy::RESTRICT);
        return convert_err(res);
 }
 
@@ -242,7 +357,7 @@ dpm_error DPM::get_messaging_state(bool &allow)
 
 dpm_error DPM::set_popimap_email_state(bool allow)
 {
-       return set_state(dpm_restriction_set_popimap_email_state, allow);
+       return set_state(dpm_restriction_set_popimap_email_state, allow, POP_or_IMAP_email);
 }
 
 dpm_error DPM::get_popimap_email_state(bool &allow)
@@ -252,7 +367,7 @@ dpm_error DPM::get_popimap_email_state(bool &allow)
 
 dpm_error DPM::set_browser_state(bool allow)
 {
-       return set_state(dpm_restriction_set_browser_state, allow);
+       return set_state(dpm_restriction_set_browser_state, allow, WEB_browser);
 }
 
 dpm_error DPM::get_browser_state(bool &allow)
@@ -279,6 +394,18 @@ dpm_error DPM::set_package_restriction(package_mode mode, bool allow)
                        dpm_application_unset_mode_restriction((dpmh)handle, m) :
                        dpm_application_set_mode_restriction((dpmh)handle, m);
 
+       //if (res == DPM_ERROR_NONE)
+       {
+               const char *param = "none";
+               if (mode == PACKAGE_INSTALL) param = "install";
+               else if (mode == PACKAGE_UNINSTALL) param = "uninstall";
+               else if (mode == PACKAGE_REINSTALL) param = "reinstall";
+               else if (mode == PACKAGE_MOVE) param = "move";
+               else if (mode == PACKAGE_ALL) param = "all";
+
+               UpdatePolicy(policy[package], allow ? Policy::ALLOW : Policy::RESTRICT, param);
+       }
+
        return convert_err(res);
 }
 
@@ -295,6 +422,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[privileges_blacklist], Policy::ADD, privilege);
        return convert_err(res);
 }
 
@@ -302,6 +431,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[privileges_blacklist], Policy::REMOVE, privilege);
        return convert_err(res);
 }
 
@@ -320,6 +451,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[bluetooth_MAC_blacklist], Policy::ADD, address);
        return convert_err(res);
 }
 
@@ -327,6 +460,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[bluetooth_MAC_blacklist], Policy::REMOVE, address);
        return convert_err(res);
 }
 
@@ -334,6 +469,9 @@ dpm_error DPM::bluetooth_set_device_restriction(bool enable)
 {
        if (!handle) return DPM_NOT_INITIALIZED;
        int res = dpm_bluetooth_set_device_restriction((dpmh)handle, enable ? 1 : 0);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[bluetooth_MAC_restriction],
+                               enable ? Policy::ALLOW : Policy::RESTRICT);
        return convert_err(res);
 }
 
@@ -350,6 +488,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[bluetooth_UUID_blacklist], Policy::ADD, uuid);
        return convert_err(res);
 }
 
@@ -357,6 +497,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[bluetooth_UUID_blacklist], Policy::REMOVE, uuid);
        return convert_err(res);
 }
 
@@ -364,6 +506,9 @@ dpm_error DPM::bluetooth_set_uuid_restriction(bool enable)
 {
        if (!handle) return DPM_NOT_INITIALIZED;
        int res = dpm_bluetooth_set_uuid_restriction((dpmh)handle, enable);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[bluetooth_UUID_restriction],
+                               enable ? Policy::ALLOW : Policy::RESTRICT);
        return convert_err(res);
 }
 
@@ -380,12 +525,17 @@ dpm_error DPM::lockout_screen()
 {
        if (!handle) return DPM_NOT_INITIALIZED;
        int res = dpm_security_lockout_screen((dpmh)handle);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[lockout_screen_], Policy::ALLOW);
        return convert_err(res);
 }
 dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[internal_storage_encryption_],
+                               encrypt ? Policy::ALLOW : Policy::RESTRICT);
        return convert_err(res);
 }
 dpm_error DPM::is_internal_storage_encrypted(bool &is_encrypted)
@@ -400,6 +550,9 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[external_storage_encryption_],
+                               encrypt ? Policy::ALLOW : Policy::RESTRICT);
        return convert_err(res);
 }
 dpm_error DPM::is_external_storage_encrypted(bool &is_encrypted)
@@ -414,12 +567,16 @@ dpm_error DPM::wipe_external_data()
 {
        if (!handle) return DPM_NOT_INITIALIZED;
        int res = dpm_security_wipe_data((dpmh)handle, DPM_SECURITY_WIPE_EXTERNAL_STORAGE);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[wipe_external_data_], Policy::ALLOW);
        return convert_err(res);
 }
 dpm_error DPM::wipe_internal_data()
 {
        if (!handle) return DPM_NOT_INITIALIZED;
        int res = dpm_security_wipe_data((dpmh)handle, DPM_SECURITY_WIPE_INTERNAL_STORAGE);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[wipe_internal_data_], Policy::ALLOW);
        return convert_err(res);
 }
 
@@ -427,6 +584,9 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[WiFi_profile_change_],
+                               enable ? Policy::ALLOW : Policy::RESTRICT);
        return convert_err(res);
 }
 
@@ -441,6 +601,9 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[WiFi_network_access_],
+                               enable ? Policy::ALLOW : Policy::RESTRICT);
        return convert_err(res);
 }
 
@@ -455,6 +618,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[WiFi_SSID_blacklist_], Policy::ADD, ssid);
        return convert_err(res);
 }
 
@@ -462,6 +627,8 @@ dpm_error DPM::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);
+       //if (res == DPM_ERROR_NONE)
+               UpdatePolicy(policy[WiFi_SSID_blacklist_], Policy::REMOVE, ssid);
        return convert_err(res);
 }
 
index 33024ba..b7ccb80 100644 (file)
@@ -33,7 +33,6 @@ typedef std::function<void(const std::string&, bool)> PresenceHook;
 class IoTivity
 {
 public:
-
     /**
      * Indicates that user is signed in
      * @return true if user if signed in
@@ -97,6 +96,9 @@ public:
      * @brief Get unowned devices list from local network
      */
     IoTDevicesMap getUnOwnedDevices();
+
+    static std::string getDeviceID();
+
 private:
     static const std::string DEFAULT_PROVIDER;
     static const int DEFAULT_TIMEOUT;
index b4b4217..d2e7cda 100644 (file)
@@ -1,15 +1,19 @@
+#ifndef JSON_POLICY_H_
+#define JSON_POLICY_H_
+
 #include <string>
 #include <vector>
+#include "../../include/iotivity.h"
 
 using namespace std;
 
-class PolicyExeption : public exception
+class PolicyException : public exception
 {
        const string    desc; // description of the error
 public:
-       PolicyExeption(const char *s) : desc(s) {}
+       PolicyException(const char *s) : desc(s) {}
        virtual const char* what() const throw() {return desc.c_str();}
-       virtual ~PolicyExeption() throw() {}
+       virtual ~PolicyException() throw() {}
 };
 
 class Policy
@@ -107,6 +111,7 @@ protected:
        PolicyUpdate() : action(RESTRICT) {}
 public:
        string                  sender;         // The entity who sends the request
+       string                  device_id;  // UUID of the device on which the sender is running
        Action                  action;         // what to do
        vector<string>  params;         // additional parameters (optionally)
 
@@ -161,6 +166,7 @@ public:
                                 vector<string> &params_) :
                                         Policy(name),
                                         sender(sender_),
+                                        device_id(NetworkManager::IoTivity::getDeviceID()),
                                         action(action_),
                                         params(params_) {}
 
@@ -220,3 +226,5 @@ public:
         */
        PolicyList(vector<PolicyEnforcement> list_, string &json);
 };
+
+#endif // JSON_POLICY_H_
index 59fac2c..53249a7 100644 (file)
@@ -16,9 +16,9 @@ void find_tag(string json, size_t &pos, const string tag)
 {
        size_t x;
        x = json.find(tag, pos);
-       if (x == string::npos) throw PolicyExeption(tag_not_found);
+       if (x == string::npos) throw PolicyException(tag_not_found);
        x = json.find(":", x);
-       if (x == string::npos) throw PolicyExeption(wrong_json_format);
+       if (x == string::npos) throw PolicyException(wrong_json_format);
        pos = x;
 }
 
@@ -26,13 +26,13 @@ string get_val(string json, size_t &pos, const string tag)
 {
        size_t x, y;
        x = json.find(tag, pos);
-       if (x == string::npos) throw PolicyExeption(wrong_json_format);
+       if (x == string::npos) throw PolicyException(wrong_json_format);
        x = json.find(":", x);
-       if (x == string::npos) throw PolicyExeption(wrong_json_format);
+       if (x == string::npos) throw PolicyException(wrong_json_format);
        x = json.find("\"", x) + 1;
-       if (x == string::npos) throw PolicyExeption(wrong_json_format);
+       if (x == string::npos) throw PolicyException(wrong_json_format);
        y = json.find("\"", x);
-       if (y == string::npos) throw PolicyExeption(wrong_json_format);
+       if (y == string::npos) throw PolicyException(wrong_json_format);
        pos = y + 1;
        return json.substr(x, y - x);
 }
@@ -48,11 +48,11 @@ void get_array(string json, size_t &pos, const string tag, vector<string> &arr)
        while (1)
        {
                x = json.find_first_of("\"]", x);
-               if (x == string::npos) throw PolicyExeption(wrong_json_format);
+               if (x == string::npos) throw PolicyException(wrong_json_format);
                if (json.at(x) == ']') break;
                x++;
                x2 = json.find_first_of('\"', x);
-               if (x2 == string::npos) throw PolicyExeption(wrong_json_format);
+               if (x2 == string::npos) throw PolicyException(wrong_json_format);
                arr.push_back(json.substr(x, x2 - x));
                x = x2 + 1;
        }
@@ -72,7 +72,7 @@ Policy::Type Policy::GetType(string &json)
                if (tag == "policy state") return POLICY_STATE;
                if (tag == "policy enforcement report") return POLICY_REPORT;
        }
-       throw PolicyExeption("Policy::GetType(): bad JSON format");
+       throw PolicyException("Policy::GetType(): bad JSON format");
        return POLICY_UPDATE;
 }
 
@@ -170,6 +170,7 @@ void PolicyUpdate::parse_json(string &json, size_t &pos)
 {
        find_tag(json, pos, "update policy");
     sender = get_val(json, pos, "sender");
+    device_id = get_val(json, pos, "device");
     policy_name = get_val(json, pos, "policy");
        string action_s = get_val(json, pos, "action");
        if (action_str[0] == action_s) action = RESTRICT;
@@ -186,6 +187,7 @@ PolicyUpdate::PolicyUpdate(string sender_,
                                                   string &json) :
                                         Policy(name),
                                         sender(sender_),
+                                        device_id(NetworkManager::IoTivity::getDeviceID()),
                                         action(action_)
 {
        params = params_;
@@ -199,6 +201,7 @@ PolicyUpdate::PolicyUpdate(string sender_,
                                                   string &json) :
                                         Policy(name),
                                         sender(sender_),
+                                        device_id(NetworkManager::IoTivity::getDeviceID()),
                                         action(action_)
 {
        params.push_back(params_);
@@ -213,6 +216,7 @@ void PolicyUpdate::init(string &json)
                << "  \"update policy\" :" << endl
                << "  {" << endl
                << "    \"sender\" : " << "\"" << sender << "\"," << endl
+               << "    \"device\" : " << "\"" << device_id << "\"," << endl
                << "    \"policy\" : " << "\"" << policy_name << "\"," << endl
                << "    \"action\" : " << "\"" << action_str[action] << "\"," << endl
                << "    \"params\" : [" ;
@@ -301,7 +305,7 @@ PolicyList::PolicyList(string &json)
                        list.push_back(pe);
                }
        }
-       catch (PolicyExeption &e)
+       catch (PolicyException &e)
        {
                if (e.what() != string(tag_not_found)) throw e;
        }
@@ -409,7 +413,7 @@ void json_test()
        json.erase();
 
        }
-       catch (PolicyExeption &e)
+       catch (PolicyException &e)
        {
                cout << e.what();
        }