replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / otmcontextlist.c
index 6cfc96f..99cba81 100644 (file)
@@ -26,7 +26,7 @@
 #include "utlist.h"
 #include "otmcontextlist.h"
 
-#define TAG "OTM_CTX_LIST"
+#define TAG "OIC_OTM_CTX"
 
 /**
  * List for saving the OTMContext to be used while ownership transfer.
@@ -35,9 +35,8 @@ static OTMContextItem_t* g_otmCtxList = NULL;
 
 void RemoveOTMContext(const char* addr, uint16_t port)
 {
-    OTMContext_t* retCtx = NULL;
-
-    OIC_LOG(DEBUG, TAG, "IN RemoveOTMContext");
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+    size_t ctxCnt = 0;
 
     if (NULL != addr && 0 != port)
     {
@@ -46,20 +45,63 @@ void RemoveOTMContext(const char* addr, uint16_t port)
 
         LL_FOREACH_SAFE(g_otmCtxList, item, temp)
         {
+            ctxCnt++;
             if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
                 port == item->endpoint.port)
             {
                 OIC_LOG_V(DEBUG, TAG, "Remove [%s:%d]'s context from OTMContext list", addr, port);
-                retCtx = item->otmCtx;
                 item->otmCtx = NULL;
                 LL_DELETE(g_otmCtxList, item);
                 OICFree(item);
+                ctxCnt--;
                 break;
             }
         }
+
+        if (0 == ctxCnt)
+        {
+            g_otmCtxList = NULL;
+        }
     }
 
-    OIC_LOG(DEBUG, TAG, "OUT RemoveOTMContext");
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+}
+
+void DeleteOTMContextList()
+{
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+
+    OTMContextItem_t* item = NULL;
+    OTMContextItem_t* temp = NULL;
+    int index = 0;
+
+    LL_FOREACH_SAFE(g_otmCtxList, item, temp)
+    {
+        LL_DELETE(g_otmCtxList, item);
+        if (item)
+        {
+            OIC_LOG_V(DEBUG, TAG, "Found OTM Context Item #%d in List", index++);
+            if (item->otmCtx)
+            {
+                OIC_LOG(DEBUG, TAG, "Found OTM Context is exist.");
+                if (item->otmCtx->selectedDeviceInfo)
+                {
+                    OIC_LOG_V(DEBUG, TAG, "Remove [%s:%d]'s context from OTMContext list",
+                              item->otmCtx->selectedDeviceInfo->endpoint.addr,
+                              item->otmCtx->selectedDeviceInfo->securePort);
+                }
+                if (item->otmCtx->ctxResultArray)
+                {
+                    OIC_LOG(DEBUG, TAG, "Found Result Array is exist in OTM Context.");
+                    OICFree(item->otmCtx->ctxResultArray);
+                }
+                OICFree(item->otmCtx);
+            }
+            OICFree(item);
+        }
+    }
+    g_otmCtxList = NULL;
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
 }
 
 OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
@@ -68,7 +110,7 @@ OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
     OTMContextItem_t* temp = NULL;
     OTMContextItem_t* newItem = NULL;
 
-    OIC_LOG(DEBUG, TAG, "IN AddOTMContext");
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
 
     if (NULL == ctx || NULL == addr || 0 == strlen(addr) || 0 == port)
     {
@@ -82,6 +124,7 @@ OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
             {
                 //if OTM Context already exists, just return OC_STACK_OK.
                 OIC_LOG(DEBUG, TAG, "Same OTMContext already exists.");
+                item->otmCtx->ctxResultCallback = ctx->ctxResultCallback;
                 return OC_STACK_OK;
             }
     }
@@ -100,14 +143,14 @@ OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
     newItem->endpoint.port = port;
     LL_APPEND(g_otmCtxList, newItem);
 
-    OIC_LOG(DEBUG, TAG, "OUT AddOTMContext");
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
 
     return OC_STACK_OK;
 }
 
-const OTMContext_t* GetOTMContext(const char* addr, uint16_t port)
+OTMContext_t* GetOTMContext(const char* addr, uint16_t port)
 {
-    OIC_LOG(DEBUG, TAG, "IN GetOTMContext");
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
 
     if (NULL != addr && 0 != port)
     {
@@ -125,7 +168,7 @@ const OTMContext_t* GetOTMContext(const char* addr, uint16_t port)
         }
     }
 
-    OIC_LOG(DEBUG, TAG, "OUT GetOTMContext");
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
 
     return NULL;
 }