[IOT-1352] SampleApp(CloudClient) goes into infinite loop while testing some features
authorAndrii Shtompel <a.shtompel@samsung.com>
Thu, 29 Sep 2016 15:36:21 +0000 (18:36 +0300)
committerRandeep Singh <randeep.s@samsung.com>
Fri, 7 Oct 2016 06:48:50 +0000 (06:48 +0000)
Change-Id: Ib173abc1968051e2a8ef3dc70e1bbff14bc4d732
Signed-off-by: Andrii Shtompel <a.shtompel@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/12569
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/provisioning/sample/cloud/cloudCommon.c
resource/csdk/security/provisioning/sample/cloud/cloudWrapper.c

index 218922e..ec30045 100644 (file)
@@ -189,10 +189,8 @@ static void printMenu(OCMode mode)
     printf("** %d - ACL Cancel invitation Request\n", ACL_GROUP_CANCEL_INVITE);
 
     printf("** EXIT\n");
-    printf("** %d - Exit cloud %s\n\n", EXIT, title);
+    printf("** %d - Exit cloud %s\n", EXIT, title);
     printf("************************************************************\n");
-
-    printf(">> Enter Menu number:\n");
 }
 
 void unlockMenu(void *data)
@@ -390,7 +388,7 @@ static void userRequests(void *data)
         printMenu(mode);
 
         int request = 0;
-        scanf("%d", &request);
+        readInteger(&request, "Menu number", "see above");
 
         switch (request)
         {
index 8fb0923..5031a52 100644 (file)
 //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)
@@ -44,7 +53,7 @@ static bool readOptional(const char* description)
     while(1)
     {
         scanf("%c", &choice);
-        getchar();
+        skipSpecialCharacters();
 
         switch (choice)
         {
@@ -62,7 +71,7 @@ void readString(char* item, int length, const char* description, const char* exa
     char template[8] = { 0 };
     snprintf(template, sizeof(template), "%%%ds", length - 1);
     scanf(template, item);
-    getchar();
+    skipSpecialCharacters();
 }
 
 /**
@@ -85,7 +94,7 @@ void readInteger(int* item, const char* description, const char* example)
 {
     printf("Enter %s (f.e. %s):\n", description, example);
     scanf("%d", item);
-    getchar();
+    skipSpecialCharacters();
 }
 
 /**
@@ -256,10 +265,11 @@ bool readFile(const char *name, OCByteString *out)
     }
 
     //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;
     }