Merge branch 'master' into simulator
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / sample / provisioningclient.c
index fab89e0..ee16b58 100644 (file)
@@ -28,8 +28,8 @@
 #include "ocprovisioningmanager.h"
 #include "secureresourceprovider.h"
 #include "oxmjustworks.h"
-// TODO: PIN based OxM implementation is required.
-//#include "oxmrandompin.h"
+#include "oxmrandompin.h"
+#include "pinoxmcommon.h"
 #include "oic_string.h"
 
 #define MAX_URI_LENGTH (64)
@@ -67,7 +67,7 @@ static void deleteACL(OicSecAcl_t *acl)
     if (acl)
     {
         /* Clean Resources */
-        for (int i = 0; i < (acl)->resourcesLen; i++)
+        for (size_t i = 0; i < (acl)->resourcesLen; i++)
         {
             OICFree((acl)->resources[i]);
         }
@@ -155,7 +155,7 @@ static int CalculateAclPermission(const char *temp_pms, uint16_t *pms)
  */
 static int InputACL(OicSecAcl_t *acl)
 {
-    int unused __attribute__((unused));
+    int ret;
     char temp_id [UUID_LENGTH + 4] = {0,};
     char temp_rsc[MAX_URI_LENGTH + 1] = {0,};
     char temp_pms[MAX_PERMISSION_LENGTH + 1] = {0,};
@@ -166,29 +166,58 @@ static int InputACL(OicSecAcl_t *acl)
     printf("-URN identifying the subject\n");
     printf("ex) 1111-1111-1111-1111 (16 Numbers except to '-')\n");
     printf("Subject : ");
-    unused = scanf("%19s", temp_id);
+    char *ptr = NULL;
+    ret = scanf("%19ms", &ptr);
+    if(1==ret)
+    {
+        OICStrcpy(temp_id, sizeof(temp_id), ptr);
+        OICFree(ptr);
+    }
+    else
+    {
+         printf("Error while input\n");
+         return -1;
+    }
     int j = 0;
     for (int i = 0; temp_id[i] != '\0'; i++)
     {
         if (DASH != temp_id[i])
+        {
+            if(j>UUID_LENGTH)
+            {
+                printf("Invalid input\n");
+                return -1;
+            }
             acl->subject.id[j++] = temp_id[i];
+        }
     }
 
     //Set Resource.
-    printf("Num. of Resource : ");
-    unused = scanf("%zu", &acl->resourcesLen);
+    printf("Num. of Resource : \n");
+    ret = scanf("%zu", &acl->resourcesLen);
     printf("-URI of resource\n");
     printf("ex)/oic/sh/temp/0 (Max_URI_Length: 64 Byte )\n");
-    acl->resources = (char **)OICMalloc(acl->resourcesLen * sizeof(char *));
+    acl->resources = (char **)OICCalloc(acl->resourcesLen, sizeof(char *));
     if (NULL == acl->resources)
     {
         OC_LOG(ERROR, TAG, "Error while memory allocation");
         return -1;
     }
-    for (int i = 0; i < acl->resourcesLen; i++)
+    for (size_t i = 0; i < acl->resourcesLen; i++)
     {
-        printf("[%d]Resource : ", i + 1);
-        unused = scanf("%64s", temp_rsc);
+        printf("[%zu]Resource : ", i + 1);
+        char *ptr_tempRsc = NULL;
+        ret = scanf("%64ms", &ptr_tempRsc);
+        if (1==ret)
+        {
+            OICStrcpy(temp_rsc, sizeof(temp_rsc), ptr_tempRsc);
+            OICFree(ptr_tempRsc);
+        }
+        else
+        {
+            printf("Error while input\n");
+            return -1;
+        }
         acl->resources[i] = OICStrdup(temp_rsc);
 
         if (NULL == acl->resources[i])
@@ -196,8 +225,6 @@ static int InputACL(OicSecAcl_t *acl)
             OC_LOG(ERROR, TAG, "Error while memory allocation");
             return -1;
         }
-        strncpy(acl->resources[i], temp_rsc, strlen(temp_rsc));
-        acl->resources[i][strlen(temp_rsc)] = '\0';
     }
     // Set Permission
     do
@@ -205,12 +232,24 @@ static int InputACL(OicSecAcl_t *acl)
         printf("-Set the permission(C,R,U,D,N)\n");
         printf("ex) CRUDN, CRU_N,..(5 Charaters)\n");
         printf("Permission : ");
-        unused = scanf("%5s", temp_pms);
+        char *ptr_temp_pms = NULL;
+        ret = scanf("%5ms", &ptr_temp_pms);
+        if(1 == ret)
+        {
+            OICStrcpy(temp_pms, sizeof(temp_pms), ptr_temp_pms);
+            OICFree(ptr_temp_pms);
+
+        }
+        else
+        {
+            printf("Error while input\n");
+            return -1;
+        }
     }
     while (0 != CalculateAclPermission(temp_pms, &(acl->permission)) );
     // Set Rowner
     printf("Num. of Rowner : ");
-    unused = scanf("%zu", &acl->ownersLen);
+    ret = scanf("%zu", &acl->ownersLen);
     printf("-URN identifying the rowner\n");
     printf("ex) 1111-1111-1111-1111 (16 Numbers except to '-')\n");
     acl->owners = (OicUuid_t *)OICCalloc(acl->ownersLen, sizeof(OicUuid_t));
@@ -219,10 +258,21 @@ static int InputACL(OicSecAcl_t *acl)
         OC_LOG(ERROR, TAG, "Error while memory allocation");
         return -1;
     }
-    for (int i = 0; i < acl->ownersLen; i++)
+    for (size_t i = 0; i < acl->ownersLen; i++)
     {
-        printf("[%d]Rowner : ", i + 1);
-        unused = scanf("%19s", temp_id);
+        printf("[%zu]Rowner : ", i + 1);
+        char *ptr_temp_id = NULL;
+        ret = scanf("%19ms", &ptr_temp_id);
+        if (1 == ret)
+        {
+            OICStrcpy(temp_id, sizeof(temp_id), ptr_temp_id);
+            OICFree(ptr_temp_id);
+        }
+        else
+        {
+            printf("Error while input\n");
+            return -1;
+        }
         j = 0;
         for (int k = 0; temp_id[k] != '\0'; k++)
         {
@@ -237,17 +287,46 @@ static int InputACL(OicSecAcl_t *acl)
 
 
 //FILE *client_fopen(const char *path, const char *mode)
-FILE *client_fopen(const char *path, const char *mode)
+FILE *client_fopen(const char* UNUSED_PARAM , const char *mode)
 {
+    (void)UNUSED_PARAM;
     return fopen(CRED_FILE, mode);
 }
 
+void PrintfResult(const char* procName, void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
+{
+    printf("-----------------------------------------------------------\n");
+    if(!hasError)
+    {
+        printf("%s was successfully done.\n", procName);
+    }
+    else
+    {
+        for(int i = 0; i < nOfRes; i++)
+        {
+            printf("UUID : ");
+            for(int j = 0; j < UUID_LENGTH; i++)
+            {
+                printf("%c", arr[i].deviceId.id[j]);
+            }
+            printf("\t");
+            printf("Result=%d\n", arr[i].res);
+        }
+    }
+
+    if(ctx)
+    {
+        printf("Context is %s\n", (char*)ctx);
+    }
+    printf("-----------------------------------------------------------\n");
+}
+
 void ProvisionCredCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
 {
     if(!hasError)
     {
-        OC_LOG(INFO, TAG, "Provision Credential IS DONE~~!!");
         gOwnershipState = 1;
+        PrintfResult("Provision Credential", ctx, nOfRes, arr, hasError);
     }
 }
 
@@ -255,8 +334,8 @@ void ProvisionAclCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasErr
 {
     if(!hasError)
     {
-        OC_LOG(INFO, TAG, "Provision Acl IS DONE~~!!");
         gOwnershipState = 1;
+        PrintfResult("Provision ACL", ctx, nOfRes, arr, hasError);
     }
 }
 
@@ -264,9 +343,8 @@ void ProvisionPairwiseCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool h
 {
     if(!hasError)
     {
-        OC_LOG_V(INFO, TAG, "Context is %s", (char*)ctx);
-        OC_LOG(INFO, TAG, "Provision Pairwise IS DONE~~!!");
         gOwnershipState = 1;
+        PrintfResult("Provision Pairwise Credential", ctx, nOfRes, arr, hasError);
     }
 }
 
@@ -274,9 +352,8 @@ void OwnershipTransferCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool h
 {
     if(!hasError)
     {
-        OC_LOG_V(INFO, TAG, "Context is %s", (char*)ctx);
-        OC_LOG(INFO, TAG, "OWNERSHIP TRANSFER IS DONE~~!!");
         gOwnershipState = 1;
+        PrintfResult("Ownership transfer", ctx, nOfRes, arr, hasError);
     }
 }
 
@@ -284,28 +361,37 @@ void InputPinCB(char* pinBuf, size_t bufSize)
 {
     if(pinBuf)
     {
-        scanf("%s", pinBuf);
-        pinBuf[bufSize - 1] = '\0';
+        printf("INPUT PIN : ");
+        char *ptr = NULL;
+        int ret = scanf("%ms", &ptr);
+        if(1 == ret)
+        {
+            OICStrcpy(pinBuf, bufSize, ptr);
+            OICFree(ptr);
+        }
+        else
+        {
+             printf("Error in input\n");
+             return;
+        }
     }
 }
 
 /**
  * Provisioning client sample using ProvisioningAPI
  */
-int main(int argc, char **argv)
+int main()
 {
     OCStackResult res = OC_STACK_OK;
-    int unused __attribute__((unused));
-
-    gAcl1 = (OicSecAcl_t *)OICMalloc(sizeof(OicSecAcl_t));
-    if (NULL == gAcl1)
-    {
-        OC_LOG(ERROR, TAG, "Error while memory allocation");
-        goto error;
-    }
+    int unused;
+    (void)unused;
 
     // Initialize Persistent Storage for SVR database
-    OCPersistentStorage ps = {};
+    OCPersistentStorage ps = { .open = NULL,
+                               .read = NULL,
+                               .write = NULL,
+                               .close = NULL,
+                               .unlink = NULL };
     ps.open = client_fopen;
     ps.read = fread;
     ps.write = fwrite;
@@ -338,20 +424,27 @@ int main(int argc, char **argv)
     }
 
     //Register callback function to each OxM
-    OTMCallbackData_t justWorksCBData = {};
+    OTMCallbackData_t justWorksCBData = {.loadSecretCB=NULL,
+                                         .createSecureSessionCB=NULL,
+                                         .createSelectOxmPayloadCB=NULL,
+                                         .createOwnerTransferPayloadCB=NULL};
     justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
     justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
     justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
     justWorksCBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
     OTMSetOwnershipTransferCallbackData(OIC_JUST_WORKS, &justWorksCBData);
 
-    // TODO: PIN based OxM implementation is required.
-    //OTMCallbackData_t pinBasedCBData = {};
-    //pinBasedCBData.loadSecretCB = InputPinCodeCallback;
-    //pinBasedCBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
-    //pinBasedCBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
-    //pinBasedCBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
-    //OTMSetOwnershipTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &pinBasedCBData);
+    OTMCallbackData_t pinBasedCBData = {.loadSecretCB=NULL,
+                                         .createSecureSessionCB=NULL,
+                                         .createSelectOxmPayloadCB=NULL,
+                                         .createOwnerTransferPayloadCB=NULL};
+    pinBasedCBData.loadSecretCB = InputPinCodeCallback;
+    pinBasedCBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
+    pinBasedCBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
+    pinBasedCBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
+    OTMSetOwnershipTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &pinBasedCBData);
+
+    SetInputPinCB(&InputPinCB);
 
     char* myContext = "OTM Context";
     //Perform ownership transfer
@@ -414,21 +507,19 @@ int main(int argc, char **argv)
     unused = scanf("%d", &Device2);
 
 
-    gAcl1 = (OicSecAcl_t *)OICMalloc(sizeof(OicSecAcl_t));
+    gAcl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
     if (NULL == gAcl1)
     {
         OC_LOG(ERROR, TAG, "Error while memory allocation");
         goto error;
     }
-    memset(gAcl1, 0x00, sizeof(OicSecAcl_t));
 
-    gAcl2 = (OicSecAcl_t *)OICMalloc(sizeof(OicSecAcl_t));
+    gAcl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
     if (NULL == gAcl2)
     {
         OC_LOG(ERROR, TAG, "Error while memory allocation");
         goto error;
     }
-    memset(gAcl2, 0x00, sizeof(OicSecAcl_t));
 
     printf("Input ACL for Device1\n");
     if ( 0 == InputACL(gAcl1))