[IOT-1413,IOT-1415] Fixed request handling bugs for /oic/ping resource
authorhyuna0213.jo <hyuna0213.jo@samsung.com>
Tue, 11 Oct 2016 06:41:47 +0000 (15:41 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Thu, 13 Oct 2016 05:34:45 +0000 (05:34 +0000)
- /oic/ping resource is responding with 5.00(internal server error)
when it receives a GET/POST/PUT request with baseline query
- /oic/ping resource is successfully registering and deregistering
observe requests, even though it is shown as non observable resource
in discovery response.

Change-Id: I86db52f17b0b42750906fdfa8b377c78a85ef822
Signed-off-by: hyuna0213.jo <hyuna0213.jo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13089
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
resource/csdk/stack/include/internal/ocresourcehandler.h
resource/csdk/stack/include/internal/ocstackinternal.h
resource/csdk/stack/include/internal/oickeepalive.h
resource/csdk/stack/include/octypes.h
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/src/ocstack.c
resource/csdk/stack/src/oickeepalive.c

index fbc1b9b..60bca1b 100644 (file)
@@ -71,16 +71,21 @@ typedef enum
     /** "/oic/gateway" .*/
     OC_GATEWAY_URI,
 #endif
-    #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
     /** "/oic/ad" .*/
     OC_PRESENCE,
-    #endif
+#endif
 
 #ifdef MQ_BROKER
     /** "/oic/ps" .*/
     OC_MQ_BROKER_URI,
 #endif
 
+#ifdef TCP_ADAPTER
+    /** "/oic/ping" .*/
+    OC_KEEPALIVE_RESOURCE_URI,
+#endif
+
     /** Max items in the list */
     OC_MAX_VIRTUAL_RESOURCES    //<s Max items in the list
 
index c093284..405d518 100644 (file)
@@ -230,6 +230,14 @@ OCStackResult BindResourceInterfaceToResource(OCResource* resource,
 OCStackResult BindResourceTypeToResource(OCResource* resource,
                                             const char *resourceTypeName);
 
+/**
+ * Convert OCStackResult to CAResponseResult_t.
+ *
+ * @param ocCode OCStackResult code.
+ * @param method OCMethod method the return code replies to.
+ * @return ::CA_CONTENT on OK, some other value upon failure.
+ */
+CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
 
 /**
  * Converts a CAResult_t type to a OCStackResult type.
index 95f3cd0..38203c6 100644 (file)
@@ -74,12 +74,12 @@ void ProcessKeepAlive();
 /**
  * This API will be called from RI layer whenever there is a request for KeepAlive.
  * Virtual Resource.
- * @param[in]   endPoint        RemoteEndpoint which sent the packet.
- * @param[in]   requestInfo     Received coap packet.
+ * @param[in]   request     Received request information.
+ * @param[in]   resource    Resource handle used for sending the response.
  * @return  ::OC_STACK_OK or Appropriate error code.
  */
-OCStackResult HandleKeepAliveRequest(const CAEndpoint_t* endPoint,
-                                     const CARequestInfo_t* requestInfo);
+OCStackResult HandleKeepAliveRequest(OCServerRequest *request,
+                                     const OCResource *resource);
 
 /**
  * API to handle the connected device for KeepAlive.
index 91cb461..4c88207 100644 (file)
@@ -71,6 +71,9 @@ extern "C" {
 /** MQ Broker URI.*/
 #define OC_RSRVD_WELL_KNOWN_MQ_URI            "/oic/ps"
 
+/** KeepAlive URI.*/
+#define OC_RSRVD_KEEPALIVE_URI                "/oic/ping"
+
 
 /** Presence */
 
index 35fd4be..00ee331 100755 (executable)
@@ -240,6 +240,14 @@ static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
         return OC_MQ_BROKER_URI;
     }
 #endif //MQ_BROKER
+
+#ifdef TCP_ADAPTER
+    else if (strcmp(uriInRequest, OC_RSRVD_KEEPALIVE_URI) == 0)
+    {
+        return OC_KEEPALIVE_RESOURCE_URI;
+    }
+#endif
+
     return OC_UNKNOWN_URI;
 }
 
@@ -825,7 +833,14 @@ static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource
 
     }
 #endif
-
+#ifdef TCP_ADAPTER
+    else if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
+    {
+        // Received request for a keepalive
+        OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
+        discoveryResult = HandleKeepAliveRequest(request, resource);
+    }
+#endif
     /**
      * Step 2: Send the discovery response
      *
@@ -858,31 +873,39 @@ static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource
         SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
     }
     else
-    #endif
-#ifdef ROUTING_GATEWAY
+#endif
+#if ROUTING_GATEWAY
     // Gateway uses the RMHandleGatewayRequest to respond to the request.
     if (OC_GATEWAY_URI != virtualUriInRequest)
 #endif
     {
-        if(discoveryResult == OC_STACK_OK)
-        {
-            SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
-        }
-        else if(((request->devAddr.flags &  OC_MULTICAST) == false) &&
-            (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
-            (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
-        {
-            OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
-                discoveryResult, virtualUriInRequest);
-            SendNonPersistantDiscoveryResponse(request, resource, NULL,
-                (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
-        }
-        else
+#if TCP_ADAPTER
+        // KeepAlive uses the HandleKeepAliveRequest to respond to the request.
+        if (OC_KEEPALIVE_RESOURCE_URI != virtualUriInRequest)
+#endif
         {
-            // Ignoring the discovery request as per RFC 7252, Section #8.2
-            OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send. ");
-            // the request should be removed. since it never remove and causes a big memory waste.
-            FindAndDeleteServerRequest(request);
+            if(discoveryResult == OC_STACK_OK)
+            {
+                SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
+            }
+            else if(((request->devAddr.flags &  OC_MULTICAST) == false) &&
+                (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
+                (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
+            {
+                OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
+                    discoveryResult, virtualUriInRequest);
+                SendNonPersistantDiscoveryResponse(request, resource, NULL,
+                    (discoveryResult == OC_STACK_NO_RESOURCE) ?
+                            OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
+            }
+            else
+            {
+                // Ignoring the discovery request as per RFC 7252, Section #8.2
+                OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
+                // the request should be removed.
+                // since it never remove and causes a big memory waste.
+                FindAndDeleteServerRequest(request);
+            }
         }
     }
 
index eeaf02b..c8c0da5 100644 (file)
@@ -322,15 +322,6 @@ static OCStackResult CAResultToOCStackResult(CAResult_t caResult);
 static OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode);
 
 /**
- * Convert OCStackResult to CAResponseResult_t.
- *
- * @param caCode OCStackResult code.
- * @param method OCMethod method the return code replies to.
- * @return ::CA_CONTENT on OK, some other value upon failure.
- */
-static CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
-
-/**
  * Convert OCTransportFlags_t to CATransportModifiers_t.
  *
  * @param ocConType OCTransportFlags_t input.
@@ -1325,7 +1316,7 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
                         type = PAYLOAD_TYPE_REPRESENTATION ;
                     }
 #ifdef TCP_ADAPTER
-                    else if (strcmp(cbNode->requestUri, KEEPALIVE_RESOURCE_URI) == 0)
+                    else if (strcmp(cbNode->requestUri, OC_KEEPALIVE_RESOURCE_URI) == 0)
                     {
                         type = PAYLOAD_TYPE_REPRESENTATION;
                     }
@@ -1780,15 +1771,6 @@ void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
 {
     OIC_LOG(DEBUG, TAG, "Enter OCHandleRequests");
 
-#ifdef TCP_ADAPTER
-    if (requestInfo->info.resourceUri &&
-            strcmp(requestInfo->info.resourceUri, KEEPALIVE_RESOURCE_URI) == 0)
-    {
-        HandleKeepAliveRequest(endPoint, requestInfo);
-        return;
-    }
-#endif
-
     OCStackResult requestResult = OC_STACK_ERROR;
 
     if(myStackMode == OC_CLIENT)
index aabb391..0e684d6 100644 (file)
@@ -152,21 +152,21 @@ static OCStackResult DeleteKeepAliveResource();
 
 /**
  * API to handle the GET request received for a KeepAlive resource.
- * @param[in]   endPoint        RemoteEndpoint which sent the packet.
- * @param[in]   requestInfo     Received coap packet.
+ * @param[in]   request     Request Received.
+ * @param[in]   resource    Resource handle used for sending the response.
  * @return  ::OC_STACK_OK or Appropriate error code.
  */
-static OCStackResult HandleKeepAliveGETRequest(const CAEndpoint_t* endPoint,
-                                        const CARequestInfo_t* requestInfo);
+static OCStackResult HandleKeepAliveGETRequest(OCServerRequest *request,
+                                               const OCResource *resource);
 
 /**
  * API to handle the PUT request received for a KeepAlive resource.
- * @param[in]   endPoint        RemoteEndpoint which sent the packet.
- * @param[in]   requestInfo     Received coap packet.
+ * @param[in]   request     Request Received.
+ * @param[in]   resource    Resource handle used for sending the response.
  * @return  ::OC_STACK_OK or Appropriate error code.
  */
-static OCStackResult HandleKeepAlivePUTRequest(const CAEndpoint_t* endPoint,
-                                        const CARequestInfo_t* requestInfo);
+static OCStackResult HandleKeepAlivePUTRequest(OCServerRequest *request,
+                                               const OCResource *resource);
 
 /**
  * API to handle the Response payload.
@@ -314,66 +314,92 @@ OCStackResult DeleteKeepAliveResource()
     return result;
 }
 
-OCStackResult HandleKeepAliveRequest(const CAEndpoint_t* endPoint,
-                                     const CARequestInfo_t* requestInfo)
+OCStackResult HandleKeepAliveRequest(OCServerRequest *request,
+                                     const OCResource *resource)
 {
-    VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM);
-    VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(request, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(resource, FATAL, OC_STACK_INVALID_PARAM);
 
     OIC_LOG(DEBUG, TAG, "HandleKeepAliveRequest IN");
 
-    OCStackResult result = OC_STACK_OK;
-    if (CA_PUT == requestInfo->method)
+    OCStackResult result = OC_STACK_ERROR;
+    if (OC_REST_GET == request->method)
     {
-        result = HandleKeepAlivePUTRequest(endPoint, requestInfo);
+        switch ((OCObserveAction)request->observationOption)
+        {
+            case OC_OBSERVE_NO_OPTION:
+                OIC_LOG(DEBUG, TAG, "Received GET request");
+                result = HandleKeepAliveGETRequest(request, resource);
+                break;
+            default:
+                OIC_LOG(DEBUG, TAG, "Not Supported by KeepAlive");
+                result = OC_STACK_UNAUTHORIZED_REQ;
+        }
     }
-    else if (CA_GET == requestInfo->method)
+    else if (OC_REST_PUT == request->method)
     {
-        result = HandleKeepAliveGETRequest(endPoint, requestInfo);
+        OIC_LOG(DEBUG, TAG, "Received PUT request");
+        result = HandleKeepAlivePUTRequest(request, resource);
     }
+    else
+    {
+        OIC_LOG(DEBUG, TAG, "Not Supported by KeepAlive");
+        result = OC_STACK_UNAUTHORIZED_REQ;
+    }
+
+    // convert OCStackResult to CAResponseResult_t.
+    CAResponseResult_t caResult = OCToCAStackResult(result, request->method);
+    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
+    CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
+
+    // Send response message.
+    SendDirectStackResponse(&endpoint, request->coapID, caResult,
+                           qualityOfServiceToMessageType(request->qos),
+                           request->numRcvdVendorSpecificHeaderOptions,
+                           request->rcvdVendorSpecificHeaderOptions,
+                           request->requestToken, request->tokenLength,
+                           request->resourceUrl, CA_RESPONSE_DATA);
 
     OIC_LOG(DEBUG, TAG, "HandleKeepAliveRequest OUT");
-    return result;
+    return OC_STACK_OK;
 }
 
-OCStackResult HandleKeepAliveGETRequest(const CAEndpoint_t* endPoint,
-                                        const CARequestInfo_t* requestInfo)
+OCStackResult HandleKeepAliveGETRequest(OCServerRequest *request,
+                                        const OCResource *resource)
 {
-    VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM);
-    VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(request, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(resource, FATAL, OC_STACK_INVALID_PARAM);
 
-    OIC_LOG_V(DEBUG, TAG, "Find Ping resource [%s]", requestInfo->info.resourceUri);
+    OIC_LOG_V(DEBUG, TAG, "Find Ping resource [%s]", request->resourceUrl);
 
     CAResponseResult_t result = CA_CONTENT;
-    OCResource *resourcePtr = FindResourceByUri(requestInfo->info.resourceUri);
+    OCResource *resourcePtr = FindResourceByUri(request->resourceUrl);
     if (!resourcePtr)
     {
         // Resource URL not specified
-        OIC_LOG_V(DEBUG, TAG, "There is no Ping resource [%s]", requestInfo->info.resourceUri);
-        result = CA_NOT_FOUND;
+        OIC_LOG_V(DEBUG, TAG, "There is no Ping resource [%s]", request->resourceUrl);
+        return OC_STACK_NO_RESOURCE;
     }
 
-    SendDirectStackResponse(endPoint, requestInfo->info.messageId, result, requestInfo->info.type,
-                            requestInfo->info.numOptions, requestInfo->info.options,
-                            requestInfo->info.token, requestInfo->info.tokenLength,
-                            requestInfo->info.resourceUri, CA_RESPONSE_DATA);
-
     return OC_STACK_OK;
 }
 
-OCStackResult HandleKeepAlivePUTRequest(const CAEndpoint_t* endPoint,
-                                        const CARequestInfo_t* requestInfo)
+OCStackResult HandleKeepAlivePUTRequest(OCServerRequest *request,
+                                        const OCResource *resource)
 {
-    VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM);
-    VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(request, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(resource, FATAL, OC_STACK_INVALID_PARAM);
 
     // Get entry from KeepAlive table.
+    CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
+    CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
+
     uint32_t index = 0;
-    KeepAliveEntry_t *entry = GetEntryFromEndpoint(endPoint, &index);
+    KeepAliveEntry_t *entry = GetEntryFromEndpoint(&endpoint, &index);
     if (!entry)
     {
         OIC_LOG(ERROR, TAG, "Received the first keepalive message from client");
-        entry = AddKeepAliveEntry(endPoint, OC_SERVER, NULL);
+        entry = AddKeepAliveEntry(&endpoint, OC_SERVER, NULL);
         if (!entry)
         {
             OIC_LOG(ERROR, TAG, "Failed to add new keepalive entry");
@@ -383,7 +409,7 @@ OCStackResult HandleKeepAlivePUTRequest(const CAEndpoint_t* endPoint,
 
     OCPayload *ocPayload = NULL;
     OCParsePayload(&ocPayload, PAYLOAD_TYPE_REPRESENTATION,
-                   requestInfo->info.payload, requestInfo->info.payloadSize);
+                   request->payload, request->payloadSize);
     OCRepPayload *repPayload = (OCRepPayload *)ocPayload;
 
     int64_t interval = 0;
@@ -394,12 +420,7 @@ OCStackResult HandleKeepAlivePUTRequest(const CAEndpoint_t* endPoint,
 
     OCPayloadDestroy(ocPayload);
 
-    // Send response message.
-    return SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_CHANGED,
-                                   requestInfo->info.type, requestInfo->info.numOptions,
-                                   requestInfo->info.options, requestInfo->info.token,
-                                   requestInfo->info.tokenLength, requestInfo->info.resourceUri,
-                                   CA_RESPONSE_DATA);
+    return OC_STACK_OK;
 }
 
 OCStackResult HandleKeepAliveResponse(const CAEndpoint_t *endPoint,
@@ -606,7 +627,7 @@ OCStackApplicationResult PingRequestCallback(void* ctx, OCDoHandle handle,
     if (NULL == clientResponse)
     {
         OIC_LOG(ERROR, TAG, "clientResponse is NULL");
-        return OC_STACK_KEEP_TRANSACTION;
+        return OC_STACK_DELETE_TRANSACTION;
     }
 
     CAEndpoint_t endpoint = { .adapter = CA_ADAPTER_TCP };
@@ -616,7 +637,7 @@ OCStackApplicationResult PingRequestCallback(void* ctx, OCDoHandle handle,
                             (OCRepPayload *)clientResponse->payload);
 
     OIC_LOG(DEBUG, TAG, "PingRequestCallback OUT");
-    return OC_STACK_KEEP_TRANSACTION;
+    return OC_STACK_DELETE_TRANSACTION;
 }
 
 KeepAliveEntry_t *GetEntryFromEndpoint(const CAEndpoint_t *endpoint, uint32_t *index)