Return correct error code when server stack fails.
authorMandeep Shetty <mandeep.shetty@intel.com>
Mon, 13 Apr 2015 23:58:45 +0000 (16:58 -0700)
committerErich Keane <erich.keane@intel.com>
Tue, 14 Apr 2015 16:44:03 +0000 (16:44 +0000)
Previously we supressed all stack level errors to OC_STACK_ERROR even
though there are more specific errors available. Also, we always sent
out CA_BAD_REQ causing the client to think it was always its fault.

Change-Id: I225558365c7aa7ba7ad09156f5f857f8effc1490
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/710
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/stack/src/ocstack.c

index 233c46a..15c48ac 100644 (file)
@@ -327,6 +327,14 @@ static OCStackResult OCBuildIPv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t
 static OCStackResult CAToOCStackResult(CAResponseResult_t caCode);
 
 /**
+ * Convert OCStackResult to CAResponseResult_t.
+ *
+ * @param caCode OCStackResult code.
+ * @return ::CA_SUCCESS on success, some other value upon failure.
+ */
+static CAResponseResult_t OCToCAStackResult(OCStackResult ocCode);
+
+/**
  * Convert OCConnectivityType to CAConnectivityType_t.
  *
  * @param ocConType OCConnectivityType input.
@@ -593,6 +601,39 @@ OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
     return ret;
 }
 
+CAResponseResult_t OCToCAStackResult(OCStackResult ocCode)
+{
+    CAResponseResult_t ret = CA_INTERNAL_SERVER_ERROR;
+
+    switch(ocCode)
+    {
+        case OC_STACK_OK:
+            ret = CA_SUCCESS;
+            break;
+        case OC_STACK_RESOURCE_CREATED:
+            ret = CA_CREATED;
+            break;
+        case OC_STACK_RESOURCE_DELETED:
+            ret = CA_DELETED;
+            break;
+        case OC_STACK_INVALID_QUERY:
+            ret = CA_BAD_REQ;
+            break;
+        case OC_STACK_INVALID_OPTION:
+            ret = CA_BAD_OPT;
+            break;
+        case OC_STACK_NO_RESOURCE:
+            ret = CA_NOT_FOUND;
+            break;
+        case OC_STACK_COMM_ERROR:
+            ret = CA_RETRANSMIT_TIMEOUT;
+            break;
+        default:
+            break;
+    }
+    return ret;
+}
+
 OCStackResult OCToCAConnectivityType(OCConnectivityType ocConType, CAConnectivityType_t* caConType)
 {
     OCStackResult ret = OC_STACK_OK;
@@ -1397,8 +1438,11 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
     }
     else if(requestResult != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, PCF("HandleStackRequests failed"));
-        SendResponse(endPoint, requestInfo->info.messageId, CA_BAD_REQ,
+        OC_LOG_V(ERROR, TAG, PCF("HandleStackRequests failed. error: %d"), requestResult);
+
+        CAResponseResult_t stackResponse = OCToCAStackResult(requestResult);
+
+        SendResponse(endPoint, requestInfo->info.messageId, stackResponse,
                 requestInfo->info.type, requestInfo->info.numOptions,
                 requestInfo->info.options, requestInfo->info.token,
                 requestInfo->info.tokenLength);
@@ -1466,10 +1510,6 @@ OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
         {
             result = ProcessRequest(resHandling, resource, request);
         }
-        else
-        {
-            result = OC_STACK_ERROR;
-        }
     }
     else
     {