1 /* *****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
22 #include "oic_malloc.h"
23 #include "oic_string.h"
25 #include "ownershiptransfermanager.h"
27 #include "otmcontextlist.h"
29 #define TAG "OIC_OTM_CTX"
32 * List for saving the OTMContext to be used while ownership transfer.
34 static OTMContextItem_t* g_otmCtxList = NULL;
36 void RemoveOTMContext(const char* addr, uint16_t port)
38 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
41 if (NULL != addr && 0 != port)
43 OTMContextItem_t* item = NULL;
44 OTMContextItem_t* temp = NULL;
46 LL_FOREACH_SAFE(g_otmCtxList, item, temp)
49 if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
50 port == item->endpoint.port)
52 OIC_LOG_V(DEBUG, TAG, "Remove [%s:%d]'s context from OTMContext list", addr, port);
54 LL_DELETE(g_otmCtxList, item);
67 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
70 void DeleteOTMContextList()
72 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
74 OTMContextItem_t* item = NULL;
75 OTMContextItem_t* temp = NULL;
78 LL_FOREACH_SAFE(g_otmCtxList, item, temp)
80 LL_DELETE(g_otmCtxList, item);
83 OIC_LOG_V(DEBUG, TAG, "Found OTM Context Item #%d in List", index++);
86 OIC_LOG(DEBUG, TAG, "Found OTM Context is exist.");
87 if (item->otmCtx->selectedDeviceInfo)
89 OIC_LOG_V(DEBUG, TAG, "Remove [%s:%d]'s context from OTMContext list",
90 item->otmCtx->selectedDeviceInfo->endpoint.addr,
91 item->otmCtx->selectedDeviceInfo->securePort);
93 if (item->otmCtx->ctxResultArray)
95 OIC_LOG(DEBUG, TAG, "Found Result Array is exist in OTM Context.");
96 OICFree(item->otmCtx->ctxResultArray);
98 OICFree(item->otmCtx);
104 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
107 OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
109 OTMContextItem_t* item = NULL;
110 OTMContextItem_t* temp = NULL;
111 OTMContextItem_t* newItem = NULL;
113 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
115 if (NULL == ctx || NULL == addr || 0 == strlen(addr) || 0 == port)
117 return OC_STACK_INVALID_PARAM;
120 LL_FOREACH_SAFE(g_otmCtxList, item, temp)
122 if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
123 port == item->endpoint.port)
125 //if OTM Context already exists, just return OC_STACK_OK.
126 OIC_LOG(DEBUG, TAG, "Same OTMContext already exists.");
127 item->otmCtx->ctxResultCallback = ctx->ctxResultCallback;
132 newItem = (OTMContextItem_t*)OICCalloc(1, sizeof(OTMContextItem_t));
135 OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
136 return OC_STACK_NO_MEMORY;
139 OIC_LOG_V(DEBUG, TAG, "Add [%s:%d]'s context to OTMContext list", addr, port);
141 newItem->otmCtx = ctx;
142 OICStrcpy(newItem->endpoint.addr, sizeof(newItem->endpoint.addr), addr);
143 newItem->endpoint.port = port;
144 LL_APPEND(g_otmCtxList, newItem);
146 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
151 OTMContext_t* GetOTMContext(const char* addr, uint16_t port)
153 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
155 if (NULL != addr && 0 != port)
157 OTMContextItem_t* item = NULL;
158 OTMContextItem_t* temp = NULL;
160 LL_FOREACH_SAFE(g_otmCtxList, item, temp)
162 if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
163 port == item->endpoint.port)
165 OIC_LOG_V(DEBUG, TAG, "Found the OTMContext for [%s:%d]", addr, port);
171 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);