Update snapshot(2018-02-07)
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocstack.c
index f588187..f5575b1 100644 (file)
@@ -272,11 +272,11 @@ static void deleteResourceType(OCResourceType *resourceType);
 static void deleteResourceInterface(OCResourceInterface *resourceInterface);
 
 /**
- * Delete all child resources.
+ * Unbind all child resources.
  *
- * @param resourceChild Specified binded resource is deleted from parent.
+ * @param resourceChild Specified binded resource head is deleted from parent.
  */
-static void deleteResourceChild(OCChildResource *resourceChild);
+static void unbindChildResources(OCChildResource *resourceChild);
 
 /**
  * Delete all of the dynamically allocated elements that were created for the resource.
@@ -1578,13 +1578,20 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
                     return;
                 }
 
-                for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
+                if (response.numRcvdVendorSpecificHeaderOptions > 0)
                 {
-                    if(&(responseInfo->info.options[i]))
+                    response.rcvdVendorSpecificHeaderOptions =
+                        (OCHeaderOption *) OICCalloc(response.numRcvdVendorSpecificHeaderOptions, sizeof(OCHeaderOption));
+                    if (NULL == response.rcvdVendorSpecificHeaderOptions)
                     {
-                        memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
-                                &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
+                        OIC_LOG(ERROR, TAG, "Failed to allocate memory for vendor header options");
+                        OCPayloadDestroy(response.payload);
+                        OICFree((void *)response.resourceUri);
+                        return;
                     }
+
+                    memcpy(response.rcvdVendorSpecificHeaderOptions, responseInfo->info.options + start,
+                        response.numRcvdVendorSpecificHeaderOptions*sizeof(OCHeaderOption));
                 }
             }
 
@@ -1678,6 +1685,7 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
 
             OICFree((void *)response.resourceUri);
             OCPayloadDestroy(response.payload);
+            OICFree(response.rcvdVendorSpecificHeaderOptions);
         }
         return;
     }
@@ -2195,10 +2203,20 @@ void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
         OICFree(serverRequest.requestToken);
         return;
     }
+
     serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
     if (serverRequest.numRcvdVendorSpecificHeaderOptions && requestInfo->info.options)
     {
-        memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
+        serverRequest.rcvdVendorSpecificHeaderOptions = (OCHeaderOption*) OICCalloc(tempNum, sizeof(OCHeaderOption));
+        if (NULL == serverRequest.rcvdVendorSpecificHeaderOptions)
+        {
+            OIC_LOG(ERROR, TAG, "Failed to allocated memory to vnd header options!");
+            OICFree(serverRequest.payload);
+            OICFree(serverRequest.requestToken);
+            return;
+        }
+
+        memcpy (serverRequest.rcvdVendorSpecificHeaderOptions, requestInfo->info.options,
             sizeof(CAHeaderOption_t)*tempNum);
     }
 
@@ -2237,6 +2255,12 @@ void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
     {
         OIC_LOG_V(ERROR, TAG, "HandleStackRequests failed. error: %d", requestResult);
 
+        // Delete observer node if it is OBSERVE failure from app
+        if (serverRequest.observationOption == OC_OBSERVE_REGISTER)
+        {
+            DeleteObserverUsingToken(requestInfo->info.token, requestInfo->info.tokenLength);
+        }
+
         CAResponseResult_t stackResponse =
             OCToCAStackResult(requestResult, serverRequest.method);
 
@@ -2250,6 +2274,7 @@ void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
     // The token is copied in there, and is thus still owned by this function.
     OICFree(serverRequest.payload);
     OICFree(serverRequest.requestToken);
+    OICFree(serverRequest.rcvdVendorSpecificHeaderOptions);
     OIC_LOG(INFO, TAG, "Exit OCHandleRequests");
 }
 
@@ -2502,7 +2527,7 @@ OCStackResult OCInit2(OCMode mode, OCTransportFlags serverFlags, OCTransportFlag
     }
 #endif
 
-    InitializeObseverList();
+    InitializeObserverList();
 
 exit:
     if(result != OC_STACK_OK)
@@ -2560,7 +2585,7 @@ OCStackResult OCStop()
 
     TerminateScheduleResourceList();
     // Remove all observers
-    TerminateObserverList();
+    DeleteObserverList();
     // Free memory dynamically allocated for resources
     deleteAllResources();
     // Remove all the client callbacks
@@ -2572,6 +2597,9 @@ OCStackResult OCStop()
     // TODO after BeachHead delivery: consolidate into single SRMDeInit()
     SRMDeInitPolicyEngine();
 
+    // Destroy Observer List Mutex
+    TerminateObserverList();
+
     stackState = OC_STACK_UNINITIALIZED;
     return OC_STACK_OK;
 }
@@ -3098,6 +3126,7 @@ OCStackResult OCDoRequest(OCDoHandle *handle,
         goto exit;
     }
 
+    token = NULL;         // Client CB list entry now owns it
     devAddr = NULL;       // Client CB list entry now owns it
     resourceUri = NULL;   // Client CB list entry now owns it
     resourceType = NULL;  // Client CB list entry now owns it
@@ -4666,7 +4695,7 @@ void deleteResourceElements(OCResource *resource)
     }
     if (resource->rsrcChildResourcesHead)
     {
-        deleteResourceChild(resource->rsrcChildResourcesHead);
+        unbindChildResources(resource->rsrcChildResourcesHead);
         resource->rsrcChildResourcesHead = NULL;
     }
     if (resource->rsrcAttributes)
@@ -4708,18 +4737,13 @@ void deleteResourceInterface(OCResourceInterface *resourceInterface)
     }
 }
 
-void deleteResourceChild(OCChildResource *resourceChild)
+void unbindChildResources(OCChildResource *head)
 {
     OCChildResource *next = NULL;
-    for (OCChildResource *pointer = resourceChild; pointer; pointer = next)
+    for (OCChildResource *current = head; current; current = next)
     {
-        next = pointer->next ? pointer->next : NULL;
-        if (pointer->rsrcResource)
-        {
-            deleteResourceElements(pointer->rsrcResource);
-            pointer->rsrcResource = NULL;
-        }
-        OICFree(pointer);
+        next = current->next;
+        OICFree(current);
     }
 }
 
@@ -5313,7 +5337,7 @@ OCStackResult OCGetResourceIns(OCResourceHandle handle, int64_t *ins)
 #endif
 
 OCStackResult OCSetHeaderOption(OCHeaderOption* ocHdrOpt, size_t* numOptions, uint16_t optionID,
-                                void* optionData, size_t optionDataLength)
+                                const void* optionData, size_t optionDataLength)
 {
     if (!ocHdrOpt)
     {