[IOT-2494] SVR DB Editor : Add pstat edit features
authorSangjoon Je <sangjoon.je@samsung.com>
Tue, 8 Aug 2017 23:46:41 +0000 (08:46 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Wed, 16 Aug 2017 11:51:26 +0000 (11:51 +0000)
Add pstat add remove modify
Cleanup svrdbeditoracl.c

New Feature: https://jira.iotivity.org/browse/IOT-2494
Signed-off-by: Sangjoon Je <sangjoon.je@samsung.com>
Change-Id: I851e8555f5a1d14332399f0c9aedc53bfa302bb3
Reviewed-on: https://gerrit.iotivity.org/gerrit/21799
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Jongmin Choi <jminl.choi@samsung.com>
Reviewed-by: Youngman Jung <yman.jung@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
(cherry picked from commit 10fb135447cb2cb192f686b33cb407699de2fbab)
Reviewed-on: https://gerrit.iotivity.org/gerrit/21897
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
resource/csdk/security/tool/svrdbeditor_src/svrdbeditor.c
resource/csdk/security/tool/svrdbeditor_src/svrdbeditoracl.c
resource/csdk/security/tool/svrdbeditor_src/svrdbeditoracl.h
resource/csdk/security/tool/svrdbeditor_src/svrdbeditorpstat.c
resource/csdk/security/tool/svrdbeditor_src/svrdbeditorpstat.h

index 48d66df..5f2dd25 100644 (file)
@@ -46,10 +46,10 @@ typedef enum OperationType
 } OperationType_t;
 
 static int MainOperation(const char *svrpath);
-static void PrintMainMenu();
+static void PrintMainMenu(void);
 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
                           bool modify);
-static void PrintHelp();
+static void PrintHelp(void);
 static FILE *SVRDBFopen(const char *path, const char *mode);
 
 int main(int argc, char *argv[])
@@ -182,9 +182,31 @@ static int MainOperation(const char *svrpath)
                 //T.B.D
                 break;
             case SVR_EDIT_PSTAT:
-                PRINT_INFO("NOT SUPPORTED YET");
-                //PrintEditMenu("Pstat Resource", false, false, true);
-                //T.B.D
+                for (;;)
+                {
+                    PrintEditMenu("Pstat Resource", true, true, true, true);
+                    editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
+                    if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
+                    {
+                        if (!g_allowedEditMenu[editMenu])
+                        {
+                            PRINT_ERR("Disabled menu");
+                            continue;
+                        }
+                    }
+                    else if (BACK == editMenu)
+                    {
+                        PRINT_INFO("Back to the previous menu.");
+                        break;
+                    }
+                    else
+                    {
+                        PRINT_ERR("Invalid menu");
+                        continue;
+                    }
+                    HandlePstatOperation(editMenu);
+                    RefreshPstat();
+                }
                 break;
             case EXIT:
                 run = false;
@@ -209,13 +231,12 @@ static FILE *SVRDBFopen(const char *path, const char *mode)
     return fopen(g_svrDbPath, mode);
 }
 
-static void PrintHelp()
+static void PrintHelp(void)
 {
     PRINT_ERR("<This program requires one input>");
     PRINT_INFO("./svrdbeditor <svr_db_file_path>");
 }
 
-
 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
                           bool modify)
 {
@@ -228,7 +249,6 @@ static void PrintEditMenu(const char *resourceName, bool print, bool add, bool r
     {
         g_allowedEditMenu[i] = false;
     }
-
     if (print)
     {
         g_allowedEditMenu[SVR_PRINT] = true;
@@ -248,8 +268,6 @@ static void PrintEditMenu(const char *resourceName, bool print, bool add, bool r
     {
         PRINT_NORMAL("\t%2d. Add entity\n", SVR_ADD);
     }
-
-
     if (remove)
     {
         g_allowedEditMenu[SVR_REMOVE] = true;
@@ -272,7 +290,7 @@ static void PrintEditMenu(const char *resourceName, bool print, bool add, bool r
     PRINT_DATA("\t%2d. Back to the main menu\n", BACK);
 }
 
-static void PrintMainMenu()
+static void PrintMainMenu(void)
 {
     PRINT_PROG("\n\nYou can perform the "
                CYAN_BEGIN "cyan color opertions : " COLOR_END_NL);
@@ -281,6 +299,6 @@ static void PrintMainMenu()
     PRINT_DATA("\t%2d. Edit Credential Resource.\n", SVR_EDIT_CRED);
     PRINT_DATA("\t%2d. Edit ACL Resource.\n", SVR_EDIT_ACL);
     PRINT_PROG("\t%2d. Edit Doxm Resource. (T.B.D)\n", SVR_EDIT_DOXM);
-    PRINT_PROG("\t%2d. Edit Pstat Resource. (T.B.D)\n", SVR_EDIT_PSTAT);
+    PRINT_DATA("\t%2d. Edit Pstat Resource.\n", SVR_EDIT_PSTAT);
     PRINT_DATA("\t%2d. Exit.\n", EXIT);
 }
index b34e95e..74d1fb0 100644 (file)
@@ -23,7 +23,6 @@
 #include "utlist.h"
 #include "octypes.h"
 #include "oic_malloc.h"
-#include "logger.h"
 
 #include "psinterface.h"
 
@@ -52,22 +51,7 @@ typedef enum AceModifyType
     ACE_EDIT_PERMISSION = 5
 } AceModifyType_t;
 
-
-static void UpdateACL();
-static void FreeACE(OicSecAce_t *ace);
-static size_t PrintResourceList(const OicSecRsrc_t *rsrcList);
-static void PrintValidity(const OicSecValidity_t *validities);
-static void PrintPermission(uint16_t permission);
-static void PrintAceid(uint16_t aceid);
-static int PrintAce(const OicSecAce_t *ace);
-static int InputSubjectType(OicSecAce_t *ace);
-static int InputResources(OicSecRsrc_t *resources);
-static uint16_t InputAccessPermission();
-static uint16_t generateAceid();
-static int ModifyAce(OicSecAce_t *ace);
-static int ModifyAcl();
-
-void DeInitACL()
+void DeInitACL(void)
 {
     if (g_acl)
     {
@@ -76,7 +60,7 @@ void DeInitACL()
     }
 }
 
-void RefreshACL()
+void RefreshACL(void)
 {
     OicSecAcl_t *tmpACL = NULL;
     uint8_t *secPayload = NULL;
@@ -105,7 +89,7 @@ void RefreshACL()
     g_acl = tmpACL;
 }
 
-static void UpdateACL()
+static void UpdateACL(void)
 {
     OCStackResult aclResult = OC_STACK_ERROR;
     uint8_t *aclPayload = NULL;
@@ -117,7 +101,6 @@ static void UpdateACL()
         PRINT_ERR("AclToCBORPayload error : %d" , aclResult);
         return;
     }
-
     aclResult = UpdateSecureResourceInPS(OIC_JSON_ACL_NAME, aclPayload, aclPayloadSize);
     if (OC_STACK_OK != aclResult)
     {
@@ -226,6 +209,27 @@ static void PrintAceid(uint16_t aceid)
     PRINT_DATA("%d\n", aceid);
 }
 
+static size_t PrintResourceList(const OicSecRsrc_t *rsrcList)
+{
+    const OicSecRsrc_t *rsrc = NULL;
+    const OicSecRsrc_t *tempRsrc = NULL;
+    size_t rsrcCnt = 0;
+
+    LL_FOREACH_SAFE(rsrcList, rsrc, tempRsrc)
+    {
+        PRINT_DATA("Resource #%zu:\n", rsrcCnt + 1);
+        PRINT_DATA("%10s : %s\n", OIC_JSON_HREF_NAME, rsrc->href);
+        PRINT_DATA("%10s : %s\n", OIC_JSON_REL_NAME, rsrc->rel);
+        PRINT_DATA("%10s : ", OIC_JSON_RT_NAME);
+        PrintStringArray((const char **)rsrc->types, rsrc->typeLen);
+        PRINT_DATA("%10s : ", OIC_JSON_IF_NAME);
+        PrintStringArray((const char **)rsrc->interfaces, rsrc->interfaceLen);
+        rsrcCnt++;
+    }
+    return rsrcCnt;
+}
+
+
 static int PrintAce(const OicSecAce_t *ace)
 {
     //aceid
@@ -286,7 +290,7 @@ static int PrintAce(const OicSecAce_t *ace)
     return 0;
 }
 
-size_t PrintAcl()
+size_t PrintAcl(void)
 {
     OicSecAce_t *ace = NULL;
     OicSecAce_t *tempAce = NULL;
@@ -524,7 +528,7 @@ static int InputResources(OicSecRsrc_t *resources)
     return 0;
 }
 
-static uint16_t InputAccessPermission()
+static uint16_t InputAccessPermission(void)
 {
     uint16_t pmsn = PERMISSION_FULL_CONTROL;  // default full permission
     uint16_t pmsn_msk = PERMISSION_CREATE;  // default permission mask
@@ -747,7 +751,7 @@ static int ModifyAce(OicSecAce_t *ace)
     return ret;
 }
 
-static int ModifyAcl()
+static int ModifyAcl(void)
 {
     int ret = 0;
     int modifyMenu = 0;
@@ -764,7 +768,7 @@ static int ModifyAcl()
     switch (modifyMenu)
     {
         case ACL_MODIFY_ACE:
-            numOfAce = PrintAcl(g_acl);
+            numOfAce = PrintAcl();
             if (0 == numOfAce)
             {
                 PRINT_ERR("empty ace");
@@ -815,26 +819,6 @@ static int ModifyAcl()
     return ret;
 }
 
-static size_t PrintResourceList(const OicSecRsrc_t *rsrcList)
-{
-    const OicSecRsrc_t *rsrc = NULL;
-    const OicSecRsrc_t *tempRsrc = NULL;
-    size_t rsrcCnt = 0;
-
-    LL_FOREACH_SAFE(rsrcList, rsrc, tempRsrc)
-    {
-        PRINT_DATA("Resource #%zu:\n", rsrcCnt + 1);
-        PRINT_DATA("%10s : %s\n", OIC_JSON_HREF_NAME, rsrc->href);
-        PRINT_DATA("%10s : %s\n", OIC_JSON_REL_NAME, rsrc->rel);
-        PRINT_DATA("%10s : ", OIC_JSON_RT_NAME);
-        PrintStringArray((const char **)rsrc->types, rsrc->typeLen);
-        PRINT_DATA("%10s : ", OIC_JSON_IF_NAME);
-        PrintStringArray((const char **)rsrc->interfaces, rsrc->interfaceLen);
-        rsrcCnt++;
-    }
-    return rsrcCnt;
-}
-
 void HandleAclOperation(const SubOperationType_t cmd)
 {
     size_t aclIdx = 0;
@@ -856,7 +840,7 @@ void HandleAclOperation(const SubOperationType_t cmd)
     switch (cmd)
     {
         case SVR_PRINT:
-            PrintAcl(g_acl);
+            PrintAcl();
             break;
         case SVR_ADD:
             {
@@ -896,7 +880,7 @@ void HandleAclOperation(const SubOperationType_t cmd)
                 size_t curIdx = 0;
                 size_t numOfAce = 0;
 
-                numOfAce = PrintAcl(g_acl);
+                numOfAce = PrintAcl();
                 if (0 == numOfAce)
                 {
                     PRINT_ERR("empty ace");
index af1d5d0..b7f6e96 100644 (file)
@@ -23,9 +23,9 @@
 
 #include "svrdbeditorcommon.h"
 
-void DeInitACL();
-void RefreshACL();
-size_t PrintAcl();
+void DeInitACL(void);
+void RefreshACL(void);
+size_t PrintAcl(void);
 void HandleAclOperation(const SubOperationType_t cmd);
 
 #endif /* SVRDBEDITOR_ACL_H_ */
\ No newline at end of file
index c00d459..e1c137c 100644 (file)
 #include "octypes.h"
 #include "oic_malloc.h"
 
-#include "srmresourcestrings.h"
 #include "experimental/securevirtualresourcetypes.h"
+#include "srmresourcestrings.h"
 #include "psinterface.h"
 
 #include "pstatresource.h"
 #include "svrdbeditorpstat.h"
 
+#define DPM_CNT (8)
+#define DPOM_CNT (3)
+#define DOSTYPE_CNT (5)
+
 static OicSecPstat_t *g_pstat = NULL;
 
-static void PrintDpm(const OicSecDpm_t dpm);
-static void PrintDpom(const OicSecDpom_t dpom);
+typedef enum PstatModifyType
+{
+    PSTAT_EDIT_DOS = 1,
+    PSTAT_EDIT_ISOP,
+    PSTAT_EDIT_CM,
+    PSTAT_EDIT_TM,
+    PSTAT_EDIT_OM,
+    PSTAT_EDIT_SM,
+    PSTAT_EDIT_ROWNERID = 7
+} PstatModifyType_t;
 
-void DeInitPstat()
+void DeInitPstat(void)
 {
     DeletePstatBinData(g_pstat);
     g_pstat = NULL;
 }
 
-void RefreshPstat()
+void RefreshPstat(void)
 {
+    OCStackResult ocResult = OC_STACK_ERROR;
     OicSecPstat_t *tmpPstat = NULL;
     uint8_t *secPayload = NULL;
     size_t payloadSize = 0;
-    OCStackResult ocResult = OC_STACK_ERROR;
 
     ocResult = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &secPayload, &payloadSize);
     if (OC_STACK_OK != ocResult)
@@ -52,6 +64,7 @@ void RefreshPstat()
         PRINT_WARN("GetSecureVirtualDatabaseFromPS : %d", ocResult);
         return;
     }
+
     ocResult = CBORPayloadToPstat(secPayload, payloadSize, &tmpPstat);
     if (OC_STACK_OK != ocResult)
     {
@@ -67,6 +80,55 @@ void RefreshPstat()
     }
     g_pstat = tmpPstat;
 }
+
+static void UpdatePstat(void)
+{
+    OCStackResult pstatResult = OC_STACK_ERROR;
+    uint8_t *pstatPayload = NULL;
+    size_t pstatPayloadSize = 0;
+
+    pstatResult = PstatToCBORPayload(g_pstat, &pstatPayload, &pstatPayloadSize);
+    if (OC_STACK_OK != pstatResult)
+    {
+        PRINT_ERR("PstatToCBORPayload error : %d", pstatResult);
+        return;
+    }
+    pstatResult = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, pstatPayload, pstatPayloadSize);
+    if (OC_STACK_OK != pstatResult)
+    {
+        PRINT_ERR("UpdateSecureResourceInPS error : %d", pstatResult);
+        OICFree(pstatPayload);
+        return;
+    }
+    OICFree(pstatPayload);
+}
+
+static void PrintDostype(const OicSecDostype_t dos)
+{
+    PRINT_DATA("Device Onboarding State : %d (", dos.state);
+    if (DOS_RESET == dos.state)
+    {
+        PRINT_DATA(" RESET ");
+    }
+    else if (DOS_RFOTM == dos.state)
+    {
+        PRINT_DATA(" RFOTM ");
+    }
+    else if (DOS_RFPRO == dos.state)
+    {
+        PRINT_DATA(" RFPRO ");
+    }
+    else if (DOS_RFNOP == dos.state)
+    {
+        PRINT_DATA(" RFNOP ");
+    }
+    else if (DOS_SRESET == dos.state)
+    {
+        PRINT_DATA(" SRESET ");
+    }
+    PRINT_DATA("), Pending state : %s\n", (dos.pending ? "True" : "False"));
+}
+
 static void PrintDpm(const OicSecDpm_t dpm)
 {
     PRINT_DATA("%d (", dpm);
@@ -99,6 +161,15 @@ static void PrintDpm(const OicSecDpm_t dpm)
     {
         PRINT_DATA(" PROVISION_ACLS ");
     }
+    if (dpm & VERIFY_SOFTWARE_VERSION)
+    {
+        PRINT_DATA(" VERIFY_SOFTWARE_VERSION ");
+    }
+    if (dpm & UPDATE_SOFTWARE)
+    {
+        PRINT_DATA(" UPDATE_SOFTWARE ");
+    }
+
     PRINT_DATA(") \n");
 }
 
@@ -121,39 +192,381 @@ static void PrintDpom(const OicSecDpom_t dpom)
     PRINT_DATA(") \n");
 }
 
-
-void PrintPstat()
+void PrintPstat(void)
 {
-    OicSecPstat_t *pstat = g_pstat;
     PRINT_INFO("\n\n********************* [%-20s] *********************",
                "PSTAT Resource");
-    PRINT_PROG("%15s : ", OIC_JSON_ISOP_NAME);
-    (pstat->isOp ? PrintString("True") : PrintString("False"));
+    if (g_pstat)
+    {
+        //dos
+        PRINT_PROG("%15s : ", OIC_JSON_DOS_NAME);
+        PrintDostype(g_pstat->dos);
+
+        //isop
+        PRINT_PROG("%15s : ", OIC_JSON_ISOP_NAME);
+        (g_pstat->isOp ? PrintString("True") : PrintString("False"));
+
+        //cm
+        PRINT_PROG("%15s : ", OIC_JSON_CM_NAME);
+        PrintDpm(g_pstat->cm);
+
+        //tm
+        PRINT_PROG("%15s : ", OIC_JSON_TM_NAME);
+        PrintDpm(g_pstat->tm);
+
+        //om
+        PRINT_PROG("%15s : ", OIC_JSON_OM_NAME);
+        PrintDpom(g_pstat->om);
+
+        //sm
+        PRINT_PROG("%15s : \n", OIC_JSON_SM_NAME);
+        for (size_t i = 0; i < g_pstat->smLen; i++)
+        {
+            PRINT_DATA("supported mode #%zu : ", i + 1);
+            PrintDpom(g_pstat->sm[i]);
+        }
+
+        PRINT_PROG("%15s : ", OIC_JSON_ROWNERID_NAME);
+        PrintUuid(&(g_pstat->rownerID));
+    }
+    else
+    {
+        PRINT_PROG("pstat is empty.\n");
+    }
+    PRINT_INFO("********************* [%-20s] *********************",
+               "PSTAT Resource");
+}
+
+static int InputDostype(OicSecDostype_t *dos)
+{
+    int state = 0;
+    int pending = 0;
+    if (NULL == dos)
+    {
+        PRINT_ERR("Invalid parameter");
+        return -1;
+    }
+    state = InputNumber("Input the Device Onboarding State"
+                        "( 0.RESET 1.RFOTM 2.RFPRO 3.RFNOP 4.SRESET ) : ");
+    if (state < 0 || DOSTYPE_CNT <= state)
+    {
+        PRINT_ERR("Invalid number");
+        return -1;
+    }
+    pending = InputNumber("Input the Pending State ( 0.False, 1.True ) : ");
+    if (pending < 0 || 1 < pending)
+    {
+        PRINT_ERR("Invalid number");
+        return -1;
+    }
+    dos->state = state;
+    dos->pending = (1 == pending ? true : false);
+    return 0;
+}
+
+static unsigned int InputDpm(void)
+{
+    unsigned int dpm = 0;
+    unsigned int dpm_msk = 1;  //default mask
+    const char *Operation_Mode[DPM_CNT] =
+    {
+        "RESET", "TAKE_OWNER", "BOOTSTRAP_SERVICE", "SECURITY_MANAGEMENT_SERVICES",
+        "PROVISION_CREDENTIALS", "PROVISION_ACLS", "VERIFY_SOFTWARE_VERSION", "UPDATE_SOFTWARE"
+    };
+
+    for (int i = 0; i < DPM_CNT; i++)
+    {
+        char ans = 0;
+        for (;;)
+        {
+            PRINT_NORMAL("\tEnter %s Mode (y/n): ", Operation_Mode[i]);
+            for (int ret = 0; 1 != ret; )
+            {
+                ret = scanf("%c", &ans);
+                for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
+                // '0x20<=code' is character region
+            }
+            if ('y' == ans || 'Y' == ans || 'n' == ans || 'N' == ans)
+            {
+                ans &= ~0x20;  // for masking lower case, 'y/n'
+                break;
+            }
+            PRINT_NORMAL("\tEntered Wrong Answer. Please Enter 'y/n' Again\n");
+        }
+        if ('Y' == ans)
+        {
+            dpm |= dpm_msk;
+        }
+        dpm_msk <<= 1;
+    }
+    return dpm;
+}
+
+static unsigned int InputDpom()
+{
+    unsigned int dpom = 0;
+    unsigned int dpom_msk = 1;  //default mask
+    const char *Operation_Mode[DPOM_CNT] =
+    {
+        "MULTIPLE_SERVICE_SERVER_DRIVEN", "SINGLE_SERVICE_SERVER_DRIVEN",
+        "SINGLE_SERVICE_CLIENT_DRIVEN"
+    };
+
+    PRINT_PROG("SINGLE_SERVICE_CLIENT_DRIVEN is the only mode IoTivity supports currently\n");
+
+    for (int i = 0; i < DPOM_CNT; i++)
+    {
+        char ans = 0;
+        for (;;)
+        {
+            PRINT_NORMAL("\tEnter %s Mode (y/n): ", Operation_Mode[i]);
+            for (int ret = 0; 1 != ret; )
+            {
+                ret = scanf("%c", &ans);
+                for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
+                // '0x20<=code' is character region
+            }
+            if ('y' == ans || 'Y' == ans || 'n' == ans || 'N' == ans)
+            {
+                ans &= ~0x20;  // for masking lower case, 'y/n'
+                break;
+            }
+            PRINT_NORMAL("\tEntered Wrong Answer. Please Enter 'y/n' Again\n");
+        }
+        if ('Y' == ans)
+        {
+            dpom |= dpom_msk;
+        }
+        dpom_msk <<= 1;
+    }
+    return dpom;
+}
 
-    PRINT_PROG("%15s : ", OIC_JSON_SM_NAME);
+static OicSecPstat_t *InputPstatData(void)
+{
+    OicSecPstat_t *pstat = NULL;
+    int isop = 0;
+
+    pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
+    if (NULL == pstat)
+    {
+        PRINT_ERR("Failed to allocate memory");
+        return NULL;
+    }
+
+    PRINT_PROG("\n\nPlease input the each entity of new pstat.\n");
+
+    if (0 != InputDostype(&pstat->dos))
+    {
+        PRINT_ERR("Failed InputDostype");
+        OICFree(pstat);
+        return NULL;
+    }
+
+    isop = InputNumber("Input the isop value ( 0.False, 1.True ) : ");
+    if (isop < 0 || 1 < isop)
+    {
+        PRINT_ERR("Invalid input");
+        OICFree(pstat);
+        return NULL;
+    }
+    else
+    {
+        pstat->isOp = (1 == isop ? true : false);
+    }
+
+    PRINT_PROG("Input Current Mode\n");
+    pstat->cm = InputDpm();
+    PRINT_PROG("Input Target Mode\n");
+    pstat->tm = InputDpm();
+    PRINT_PROG("Input Operational Mode\n");
+
+    pstat->om = InputDpom();
+    pstat->smLen = 1; // supported only one dpom mode in current iotivity
+    pstat->sm = (OicSecDpom_t *)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
+    if (NULL == pstat->sm)
+    {
+        PRINT_ERR("InputPstatData : Failed to allocate memory");
+        OICFree(pstat);
+        return NULL;
+    }
     for (size_t i = 0; i < pstat->smLen; i++)
     {
-        PrintDpom(pstat->sm[i]);
+        PRINT_PROG("Input dpom #%zu", i);
+        pstat->sm[i] = InputDpom();
     }
 
-    PRINT_PROG("%15s : ", OIC_JSON_OM_NAME);
-    PrintDpom(pstat->om);
+    PRINT_PROG(
+        "\tInput the ROWNER UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
+    if (0 != InputUuid(&(pstat->rownerID)))
+    {
+        PRINT_ERR("InputUuid error");
+        OICFree(pstat->sm);
+        OICFree(pstat);
+        return NULL;
+    }
+    return pstat;
+}
 
-    PRINT_PROG("%15s : ", OIC_JSON_CM_NAME);
-    PrintDpm(pstat->cm);
+static int ModifyPstat(void)
+{
+    int ret = 0;
+    int modifyMenu = 0;
+    if (NULL == g_pstat)
+    {
+        PRINT_ERR("pstat is NULL");
+        return -1;
+    }
 
-    PRINT_PROG("%15s : ", OIC_JSON_TM_NAME);
-    PrintDpm(pstat->tm);
+    PRINT_PROG("\n\nPlease input the attribute you want to modify\n");
+    PRINT_DATA("\t%2d. Edit dos\n", PSTAT_EDIT_DOS);
+    PRINT_DATA("\t%2d. Edit isop\n", PSTAT_EDIT_ISOP);
+    PRINT_DATA("\t%2d. Edit cm\n", PSTAT_EDIT_CM);
+    PRINT_DATA("\t%2d. Edit tm\n", PSTAT_EDIT_TM);
+    PRINT_DATA("\t%2d. Edit om\n", PSTAT_EDIT_OM);
+    PRINT_DATA("\t%2d. Edit sm\n", PSTAT_EDIT_SM);
+    PRINT_DATA("\t%2d. Edit rownerID\n", PSTAT_EDIT_ROWNERID);
+    PRINT_DATA("\t%2d. Back to the previous\n", BACK);
+    modifyMenu = InputNumber("Select the menu : ");
+    switch (modifyMenu)
+    {
+        case PSTAT_EDIT_DOS:
+            if (0 != InputDostype(&(g_pstat->dos)))
+            {
+                PRINT_ERR("Failed InputDostype");
+            }
+            break;
+        case PSTAT_EDIT_ISOP:
+            {
+                int isop = InputNumber("Input the isop value ( 0.False, 1.True ) : ");
+                if (isop < 0 || 1 < isop)
+                {
+                    PRINT_ERR("Invalid input");
+                    return -1;
+                }
+                else
+                {
+                    g_pstat->isOp = (1 == isop ? true : false);
+                }
+                break;
+            }
+        case PSTAT_EDIT_CM :
+            PRINT_PROG("Input Current Mode\n");
+            g_pstat->cm = InputDpm();
+            break;
+        case PSTAT_EDIT_TM:
+            PRINT_PROG("Input Target Mode\n");
+            g_pstat->tm = InputDpm();
+            break;
+        case PSTAT_EDIT_OM:
+            PRINT_PROG("Input Operational Mode\n");
+            g_pstat->om = InputDpom();
+            break;
+        case PSTAT_EDIT_SM:
+            {
+                OicSecDpom_t *sm = NULL;
+                size_t smLen = 1;
 
-    PRINT_PROG("%15s : ", OIC_JSON_ROWNERID_NAME);
-    PrintUuid(&pstat->rownerID);
-    PRINT_INFO("********************* [%-20s] *********************",
-               "PSTAT Resource");
+                sm = (OicSecDpom_t *)OICCalloc(smLen, sizeof(OicSecDpom_t));
+                if (NULL == sm)
+                {
+                    PRINT_ERR("Failed to allocate memory");
+                    return -1;
+                }
+                for (size_t i = 0; i < smLen; i++)
+                {
+                    PRINT_PROG("Input dpom #%zu", i);
+                    sm[i] = InputDpom();
+                }
+                if (g_pstat->sm)
+                {
+                    OICFree(g_pstat->sm);
+                }
+                g_pstat->smLen = smLen;
+                g_pstat->sm = sm;
+                break;
+            }
+        case PSTAT_EDIT_ROWNERID:
+            PRINT_PROG(
+                "\tInput the ROWNER UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
+            ret = InputUuid(&(g_pstat->rownerID));
+            if (0 != ret)
+            {
+                PRINT_ERR("InputUuid error");
+                return ret;
+            }
+            break;
+        case BACK:
+            PRINT_INFO("Back to the previous menu.");
+            break;
+        default:
+            PRINT_ERR("Wrong type Number");
+            ret = -1;
+            break;
+    }
+    return ret;
 }
 
 void HandlePstatOperation(const SubOperationType_t cmd)
 {
-    (void)cmd;
-    //T.B.D
-}
+    OCStackResult pstatResult = OC_STACK_ERROR;
 
+    if (SVR_EDIT_IDX_SIZE <= cmd)
+    {
+        PRINT_ERR("Invalid menu for pstat");
+        return;
+    }
+    switch (cmd)
+    {
+        case SVR_PRINT:
+            PrintPstat();
+            break;
+        case SVR_ADD:
+            if (g_pstat)
+            {
+                PRINT_ERR("Already exist");
+                return;
+            }
+            else
+            {
+                g_pstat = InputPstatData();
+                if (NULL == g_pstat)
+                {
+                    PRINT_ERR("Failed to InputPstatData");
+                    return;
+                }
+                UpdatePstat();
+            }
+            break;
+        case SVR_REMOVE:
+            if (g_pstat)
+            {
+                DeInitPstat();
+                pstatResult = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, NULL, 0);
+                if (OC_STACK_OK != pstatResult)
+                {
+                    PRINT_ERR("UpdateSecureResourceInPS error : %d", pstatResult);
+                    return;
+                }
+            }
+            else
+            {
+                PRINT_ERR("pstat is NULL");
+                return;
+            }
+            break;
+        case SVR_MODIFY:
+            if (0 != ModifyPstat())
+            {
+                PRINT_ERR("Failed Modify");
+                return;
+            }
+            PRINT_INFO("\n\nPstat Modified");
+            PrintPstat();
+            UpdatePstat();
+            break;
+        default:
+            PRINT_ERR("Invalid menu for pstat");
+            break;
+    }
+}
index 9c3baa4..083730f 100644 (file)
@@ -23,9 +23,9 @@
 
 #include "svrdbeditorcommon.h"
 
-void DeInitPstat();
-void RefreshPstat();
-void PrintPstat();
+void DeInitPstat(void);
+void RefreshPstat(void);
+void PrintPstat(void);
 void HandlePstatOperation(const SubOperationType_t cmd);
 
 #endif /* SVRDBEDITOR_PSTAT_H_ */