replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / sample / cloud / cloudWrapper.c
index 60b9d86..7702c3d 100644 (file)
@@ -1,8 +1,28 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * *****************************************************************/
 #include "logger.h"
 #include "occloudprovisioning.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
 #include "srmutility.h"
+#include "aclresource.h"
 #include "utlist.h"
 
 #include "utils.h"
 //in case of optional parameters absence should be sent NULL
 #define OPTIONAL(str) (str[0] ? str : NULL)
 
+/**
+ * Skip special characters from stdin
+ * */
+static void skipSpecialCharacters()
+{
+    for( ; 0x20<=getchar(); );  // for removing overflow garbages
+                                // '0x20<=code' is character region
+}
+
 static bool readOptional(const char* description)
 {
     if (NULL == description)
@@ -38,12 +67,16 @@ static bool readOptional(const char* description)
     }
 
     printf("Do you want to Enter %s (y/n):\n", description);
-    char choice = 0;
+    char temp, choice = 0;
 
     while(1)
     {
-        scanf("%c", &choice);
-        getchar();
+        for(int ret = 0; 1 != ret; )
+        {
+            ret = scanf("%c", &temp);
+            skipSpecialCharacters();
+        }
+        choice = temp;
 
         switch (choice)
         {
@@ -57,11 +90,24 @@ static bool readOptional(const char* description)
 
 void readString(char* item, int length, const char* description, const char* example)
 {
+    char *temp = (char*)OICCalloc(length, sizeof(char));
+    if (NULL == temp)
+    {
+        OIC_LOG(INFO, TAG, "temp is NULL");
+        return;
+    }
+
     printf("Enter %s (f.e. %s):\n", description, example);
     char template[8] = { 0 };
     snprintf(template, sizeof(template), "%%%ds", length - 1);
-    scanf(template, item);
-    getchar();
+
+    for(int ret = 0; 1 != ret; )
+    {
+        ret = scanf(template, temp);
+        skipSpecialCharacters();
+    }
+    strncpy(item, temp, length);
+    OICFree(temp);
 }
 
 /**
@@ -82,9 +128,16 @@ static void readOptionalString(char* item, int length, const char* description,
 
 void readInteger(int* item, const char* description, const char* example)
 {
+    int temp;
+
     printf("Enter %s (f.e. %s):\n", description, example);
-    scanf("%d", item);
-    getchar();
+
+    for(int ret = 0; 1 != ret; )
+    {
+        ret = scanf("%d", &temp);
+        skipSpecialCharacters();
+    }
+    *item = temp;
 }
 
 /**
@@ -160,19 +213,63 @@ static void readOptionalStringArray(stringArray_t *list, int length, const char*
     }
 }
 
-/**
- * Copies whole binary file to crl variable
- *
- * @param[in] list           array of strings structure
- * @param[out] crl           byte array to fill
- * @return                   negative error code
- * */
-static int ReadFile(const char *name, OCByteString *crl)
+void printStringArray(stringArray_t *list)
+{
+    if (NULL == list)
+    {
+        OIC_LOG(INFO, TAG, "Received NULL list");
+        return;
+    }
+
+    OIC_LOG_V(INFO, TAG, "List contains %zu items", list->length);
+
+    for (size_t i = 0; i < list->length; i++)
+    {
+        OIC_LOG_V(INFO, TAG, "item[%zu] = %s", i, list->array[i]);
+    }
+}
+
+void printInviteResponse(inviteResponse_t *in)
+{
+    if (NULL == in)
+    {
+        OIC_LOG(INFO, TAG, "Received NULL invitation response");
+        return;
+    }
+
+    OIC_LOG(INFO, TAG, "Received next invite gid list:");
+    printStringArray(&in->invite.gidlist);
+
+    OIC_LOG(INFO, TAG, "Received next invite mid list:");
+    printStringArray(&in->invite.midlist);
+
+    OIC_LOG(INFO, TAG, "Received next invited gid list:");
+    printStringArray(&in->invited.gidlist);
+
+    OIC_LOG(INFO, TAG, "Received next invited mid list:");
+    printStringArray(&in->invited.midlist);
+}
+
+void clearInviteResponse(inviteResponse_t *in)
+{
+    if (NULL == in)
+    {
+        return;
+    }
+
+    clearStringArray(&in->invite.gidlist);
+    clearStringArray(&in->invite.midlist);
+
+    clearStringArray(&in->invited.gidlist);
+    clearStringArray(&in->invited.midlist);
+}
+
+bool readFile(const char *name, OCByteString *out)
 {
     FILE *file = NULL;
     int length = 0;
     uint8_t *buffer = NULL;
-    int result = 1;
+    bool result = false;
 
     //Open file
     file = fopen(name, "rb");
@@ -183,8 +280,7 @@ static int ReadFile(const char *name, OCByteString *crl)
     }
 
     //Get file length
-    result = fseek(file, 0, SEEK_END);
-    if (result)
+    if (fseek(file, 0, SEEK_END))
     {
         OIC_LOG(ERROR, TAG, "Failed to SEEK_END");
         goto exit;
@@ -197,8 +293,7 @@ static int ReadFile(const char *name, OCByteString *crl)
         goto exit;
     }
 
-    result = fseek(file, 0, SEEK_SET);
-    if (result)
+    if (fseek(file, 0, SEEK_SET))
     {
         OIC_LOG(ERROR, TAG, "Failed to SEEK_SET");
         goto exit;
@@ -213,20 +308,21 @@ static int ReadFile(const char *name, OCByteString *crl)
     }
 
     //Read file contents into buffer
-    size_t realLen = fread(buffer, length, 1, file);
-    if (realLen != (size_t)length)
+    size_t count = 1;
+    size_t realCount = fread(buffer, length, count, file);
+    if (realCount != count)
     {
-        OIC_LOG_V(ERROR, TAG, "Length mismatch: read %zu instead of %d bytes", realLen, length);
+        OIC_LOG_V(ERROR, TAG, "Read %d bytes %zu times instead of %zu", length, realCount, count);
         goto exit;
     }
 
-    crl->bytes = buffer;
-    crl->len   = length;
+    out->bytes = buffer;
+    out->len   = length;
 
-    result = 0;
+    result = true;
 exit:
     fclose(file);
-    return 0;
+    return result;
 }
 
 /**
@@ -294,7 +390,7 @@ OCStackResult OCWrapperPostCRL(const OCDevAddr *endPoint, OCCloudResponseCB call
         readString(filename, sizeof(filename),
                    "filename from which binary Crl in DER format will be read", "crl");
 
-        if (ReadFile(filename, &crlData))
+        if (!readFile(filename, &crlData))
         {
             printf("Can't read crl from file %s\n", filename);
             goto exit;
@@ -377,7 +473,6 @@ OCStackResult OCWrapperAclIndividualUpdateAce(const OCDevAddr *endPoint, OCCloud
         int stype = 0;
         int permission = 0;
 
-        readString(aceid, sizeof(aceid), "ace id", ACE_ID_EXAMPLE);
         do
         {
             readString(subjectuuid, sizeof(subjectuuid), "subjectuuid", SUBJECT_ID_EXAMPLE);
@@ -426,6 +521,72 @@ exit:
     return result;
 }
 
+OCStackResult OCWrapperAclIndividualUpdate(const OCDevAddr *endPoint, OCCloudResponseCB callback)
+{
+    OCStackResult result = OC_STACK_NO_MEMORY;
+
+    char aclid[MAX_ID_LENGTH] = { 0 };
+    readString(aclid, sizeof(aclid), "acl id", ACL_ID_EXAMPLE);
+
+    cloudAce_t *ace = OICCalloc(1, sizeof(cloudAce_t));
+    if (!ace)
+    {
+        OIC_LOG(ERROR, TAG, "Can't allocate memory for ace");
+        goto exit;
+    }
+
+    char aceid[MAX_ID_LENGTH] = { 0 };
+    char subjectuuid[MAX_ID_LENGTH] = { 0 };
+    int stype = 0;
+    int permission = 0;
+
+    readString(aceid, sizeof(aceid), "ace id", ACE_ID_EXAMPLE);
+    do
+    {
+        readString(subjectuuid, sizeof(subjectuuid), "subjectuuid", SUBJECT_ID_EXAMPLE);
+    } while (OC_STACK_OK != ConvertStrToUuid(subjectuuid, &ace->subjectuuid));
+
+    readInteger(&stype, "subject type", "0 – Device, 1 – User, 2 - Group");
+    readInteger(&permission, "permission", "6");
+
+    ace->stype = stype;
+    ace->permission = permission;
+
+    int reslist_count = 0;
+    readInteger(&reslist_count, "resources list count", "1");
+
+    for (int i = 0; i < reslist_count; i++)
+    {
+        OicSecRsrc_t *res = OICCalloc(1, sizeof(OicSecRsrc_t));
+        if (!res)
+        {
+            OIC_LOG(ERROR, TAG, "Can't allocate memory for res");
+            goto exit;
+        }
+        LL_APPEND(ace->resources, res);
+
+        char href[32] = { 0 };
+        readString(href, sizeof(href), "href", RESOURCE_URI_EXAMPLE);
+
+        stringArray_t rt = {0, 0};
+        readStringArray(&rt, MAX_ID_LENGTH, "resource type", RESOURCE_TYPE_EXAMPLE);
+
+        stringArray_t _if = {0, 0};
+        readStringArray(&_if, MAX_ID_LENGTH, "interface", INTERFACE_EXAMPLE);
+
+        res->href = OICStrdup(href);
+        res->types = rt.array;
+        res->typeLen = rt.length;
+        res->interfaces = _if.array;
+        res->interfaceLen = _if.length;
+    }
+
+
+    result = OCCloudAclIndividualUpdate(NULL, aclid,aceid, ace, endPoint, callback);
+exit:
+    return result;
+}
+
 OCStackResult OCWrapperAclIndividualDelete(const OCDevAddr *endPoint, OCCloudResponseCB callback)
 {
     char aclid[MAX_ID_LENGTH] = { 0 };
@@ -435,6 +596,17 @@ OCStackResult OCWrapperAclIndividualDelete(const OCDevAddr *endPoint, OCCloudRes
     return OCCloudAclIndividualDelete(NULL, aclid, endPoint, callback);
 }
 
+OCStackResult OCWrapperAclIndividualDeleteAce(const OCDevAddr *endPoint, OCCloudResponseCB callback)
+{
+    char aclid[MAX_ID_LENGTH] = { 0 };
+    char aceid[MAX_ID_LENGTH] = { 0 };
+
+    readString(aclid, sizeof(aclid), "acl id", ACL_ID_EXAMPLE);
+    readString(aceid, sizeof(aceid), "ace id", ACE_ID_EXAMPLE);
+
+    return OCCloudAclIndividualDeleteAce(NULL, aclid, aceid, endPoint, callback);
+}
+
 OCStackResult OCWrapperAclCreateGroup(const OCDevAddr *endPoint, OCCloudResponseCB callback)
 {
     char gtype[16] = { 0 };