Check Device Operational State
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocstack.c
index 3051ad9..8f77cf7 100644 (file)
@@ -151,9 +151,6 @@ static const char COAP_TCP_SCHEME[] = "coap+tcp:";
 static const char COAPS_TCP_SCHEME[] = "coaps+tcp:";
 static const char CORESPEC[] = "core";
 
-CAAdapterStateChangedCB g_adapterHandler = NULL;
-CAConnectionStateChangedCB g_connectionHandler = NULL;
-
 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
 static OCOtmEventHandler_t g_otmEventHandler = {NULL, NULL};
 #endif
@@ -722,6 +719,9 @@ OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode)
         case CA_FORBIDDEN_REQ:
             ret = OC_STACK_FORBIDDEN_REQ;
             break;
+        case CA_TOO_MANY_REQUESTS:
+            ret = OC_STACK_TOO_MANY_REQUESTS;
+            break;
         case CA_INTERNAL_SERVER_ERROR:
             ret = OC_STACK_INTERNAL_SERVER_ERROR;
             break;
@@ -807,6 +807,9 @@ CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method)
         case OC_STACK_FORBIDDEN_REQ:
             ret = CA_FORBIDDEN_REQ;
             break;
+        case OC_STACK_TOO_MANY_REQUESTS:
+            ret = CA_TOO_MANY_REQUESTS;
+            break;
         case OC_STACK_INTERNAL_SERVER_ERROR:
             ret = CA_INTERNAL_SERVER_ERROR;
             break;
@@ -1236,6 +1239,7 @@ OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint,
                     OIC_LOG(ERROR, TAG,
                                   "Could not allocate memory for cbNode->presence->timeOut");
                     OICFree(cbNode->presence);
+                    cbNode->presence = NULL;
                     result = OC_STACK_NO_MEMORY;
                     goto exit;
                 }
@@ -3126,6 +3130,8 @@ OCStackResult OCDoRequest(OCDoHandle *handle,
         goto exit;
     }
 
+    cbData = NULL;        // Client CB list entry now owns it
+    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
@@ -3160,6 +3166,10 @@ exit:
     if (result != OC_STACK_OK)
     {
         OIC_LOG(ERROR, TAG, "OCDoResource error");
+        if (NULL != cbData && NULL != cbData->cd)
+        {
+            cbData->cd(cbData->context);
+        }
         FindAndDeleteClientCB(clientCB);
         CADestroyToken(token);
         if (handle)
@@ -3325,10 +3335,11 @@ OCStackResult OCProcessPresence()
     // to most purposes.  Uncomment as needed.
     //OIC_LOG(INFO, TAG, "Entering RequestPresence");
     ClientCB* cbNode = NULL;
+    ClientCB* tempcbNode = NULL;
     OCClientResponse clientResponse;
     OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
 
-    LL_FOREACH(cbList, cbNode)
+    LL_FOREACH_SAFE(cbList, cbNode, tempcbNode)
     {
         if (OC_REST_PRESENCE != cbNode->method || !cbNode->presence)
         {
@@ -3374,6 +3385,7 @@ OCStackResult OCProcessPresence()
             {
                 FindAndDeleteClientCB(cbNode);
             }
+            continue;
         }
 
         if (now < cbNode->presence->timeOut[cbNode->presence->TTLlevel])
@@ -3994,6 +4006,37 @@ OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
     return result;
 }
 
+OCStackResult OCResetResourceTypes(OCResourceHandle handle,
+                                   const char *newResourceType)
+{
+    OCStackResult result = OC_STACK_ERROR;
+    OCResource *resource = NULL;
+
+    resource = findResource((OCResource *) handle);
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
+        return OC_STACK_ERROR;
+    }
+
+    // Clear all bound resource types
+    deleteResourceType(resource->rsrcType);
+    resource->rsrcType = NULL;
+
+    // Bind new resource type to resource
+    result = BindResourceTypeToResource(resource, newResourceType);
+
+#ifdef WITH_PRESENCE
+    if(presenceResource.handle)
+    {
+        ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
+        SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
+    }
+#endif
+
+    return result;
+}
+
 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
         const char *resourceInterfaceName)
 {
@@ -4021,6 +4064,37 @@ OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
     return result;
 }
 
+OCStackResult OCResetResourceInterfaces(OCResourceHandle handle,
+                                        const char *newResourceInterface)
+{
+    OCStackResult result = OC_STACK_ERROR;
+    OCResource *resource = NULL;
+
+    resource = findResource((OCResource *) handle);
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
+        return OC_STACK_ERROR;
+    }
+
+    // Clear all bound interface
+    deleteResourceInterface(resource->rsrcInterface);
+    resource->rsrcInterface = NULL;
+
+    // Bind new interface to resource
+    result = BindResourceInterfaceToResource(resource, newResourceInterface);
+
+#ifdef WITH_PRESENCE
+    if (presenceResource.handle)
+    {
+        ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
+        SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
+    }
+#endif
+
+    return result;
+}
+
 OCStackResult OCGetNumberOfResources(uint8_t *numResources)
 {
     OCResource *pointer = headResource;
@@ -4634,6 +4708,9 @@ OCStackResult deleteResource(OCResource *resource)
                 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
             }
 #endif
+            // Delete resource's all observers
+            DeleteObserverUsingResource(resource);
+
             // Only resource in list.
             if (temp == headResource && temp == tailResource)
             {
@@ -4702,6 +4779,8 @@ void deleteResourceElements(OCResource *resource)
         OCDeleteResourceAttributes(resource->rsrcAttributes);
         resource->rsrcAttributes = NULL;
     }
+
+    resource->entityHandler = NULL;
     resource = NULL;
 }
 
@@ -5418,19 +5497,14 @@ OCStackResult OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t numOptions, uin
 void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
 {
     OIC_LOG(DEBUG, TAG, "OCDefaultAdapterStateChangedHandler");
-    if (g_adapterHandler)
-    {
-        g_adapterHandler(adapter, enabled);
-    }
+    
+    OC_UNUSED(adapter);
+    OC_UNUSED(enabled);
 }
 
 void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected)
 {
     OIC_LOG(DEBUG, TAG, "OCDefaultConnectionStateChangedHandler");
-    if (g_connectionHandler)
-    {
-       g_connectionHandler(info, isConnected);
-    }
 
     /*
      * If the client observes one or more resources over a reliable connection,
@@ -5496,6 +5570,17 @@ OCStackResult OCGetDeviceOwnedState(bool *isOwned)
     return ret;
 }
 
+OCStackResult OCGetDeviceOperationalState(bool* isOp)
+{
+    if(NULL != isOp)
+    {
+        *isOp = GetPstatIsop();
+        return OC_STACK_OK;
+    }
+
+    return OC_STACK_ERROR;
+}
+
 void OCClearCallBackList()
 {
     DeleteClientCBList();