[ARDUINO] Modified sample to let user select response
authorAbhishek Sharma <ce.abhishek@samsung.com>
Fri, 5 Jun 2015 08:57:22 +0000 (14:27 +0530)
committerErich Keane <erich.keane@intel.com>
Wed, 10 Jun 2015 15:45:50 +0000 (15:45 +0000)
Modified sample to let user select response to a request

Change-Id: I3ce60015ce2dff382429bb546618950b6080dabd
Signed-off-by: Abhishek Sharma <ce.abhishek@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1204
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/connectivity/samples/arduino/casample.cpp

index 8f28fc2..5d8a65a 100644 (file)
@@ -850,26 +850,70 @@ void SendResponse(CARemoteEndpoint_t *endpoint, const CAInfo_t* info)
     char buf[MAX_BUF_LEN] = {0};
 
     Serial.println("============");
-    CAInfo_t responseData = {CA_MSG_RESET};
-    if(info && info->type == CA_MSG_CONFIRM)
-    {
-        responseData.type = CA_MSG_ACKNOWLEDGE;
-    }
-    else
+    Serial.println("Select Message Type");
+    Serial.println("CON: 0");
+    Serial.println("NON: 1");
+    Serial.println("ACK: 2");
+    Serial.println("RESET: 3");
+
+    size_t len = 0;
+    int messageType = 0;
+    while(1)
     {
-        responseData.type = CA_MSG_NONCONFIRM;
+        GetData(buf, sizeof(buf), &len);
+        if(len >= 1)
+        {
+            messageType = buf[0] - '0';
+            if (messageType >= 0 && messageType <= 3)
+            {
+                break;
+            }
+        }
+        Serial.println("Invalid type");
+    }
+
+    int respCode = 0;
+    if(messageType != 3)
+    {
+        Serial.println("============");
+        Serial.println("Enter Resp Code:");
+        Serial.println("For Ex: Empty  : 0");
+        Serial.println("Success: 200");
+        Serial.println("Created: 201");
+        Serial.println("Deleted: 202");
+        Serial.println("BadReq : 400");
+        Serial.println("BadOpt : 402");
+        Serial.println("NotFnd : 404");
+        Serial.println("Internal Srv Err:500");
+        Serial.println("Timeout: 504");
+        while(1)
+        {
+            GetData(buf, sizeof(buf), &len);
+            if(len >= 1)
+            {
+                respCode = atoi(buf);
+                if (respCode >= 0 && respCode <= 504)
+                {
+                    break;
+                }
+            }
+            Serial.println("Invalid response");
+        }
     }
 
+    CAInfo_t responseData = {CA_MSG_RESET};
+    responseData.type = static_cast<CAMessageType_t>(messageType);
     responseData.messageId = (info != NULL) ? info->messageId : 0;
-    responseData.token = (info != NULL) ? (CAToken_t)info->token : NULL;
-    responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
-    responseData.payload = (CAPayload_t)"response payload";
-
+    if(messageType != 3)
+    {
+        responseData.token = (info != NULL) ? info->token : NULL;
+        responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
+        responseData.payload = static_cast<CAPayload_t>("response payload");
+    }
     CAResponseInfo_t responseInfo = {CA_BAD_REQ, {CA_MSG_RESET}};
-    responseInfo.result = (CAResponseResult_t)203;
+    responseInfo.result = static_cast<CAResponseResult_t>(respCode);
     responseInfo.info = responseData;
-
-    // send request (connectivityType from remoteEndpoint of request Info)
+    // send request (transportType from remoteEndpoint of request Info)
     CAResult_t res = CASendResponse(endpoint, &responseInfo);
     if(res != CA_STATUS_OK)
     {