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 "OTM_CTX_LIST"
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 OTMContext_t* retCtx = NULL;
40 OIC_LOG(DEBUG, TAG, "IN RemoveOTMContext");
42 if (NULL != addr && 0 != port)
44 OTMContextItem_t* item = NULL;
45 OTMContextItem_t* temp = NULL;
47 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);
53 retCtx = item->otmCtx;
55 LL_DELETE(g_otmCtxList, item);
62 OIC_LOG(DEBUG, TAG, "OUT RemoveOTMContext");
65 OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
67 OTMContextItem_t* item = NULL;
68 OTMContextItem_t* temp = NULL;
69 OTMContextItem_t* newItem = NULL;
71 OIC_LOG(DEBUG, TAG, "IN AddOTMContext");
73 if (NULL == ctx || NULL == addr || 0 == strlen(addr) || 0 == port)
75 return OC_STACK_INVALID_PARAM;
78 LL_FOREACH_SAFE(g_otmCtxList, item, temp)
80 if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
81 port == item->endpoint.port)
83 //if OTM Context already exists, just return OC_STACK_OK.
84 OIC_LOG(DEBUG, TAG, "Same OTMContext already exists.");
89 newItem = (OTMContextItem_t*)OICCalloc(1, sizeof(OTMContextItem_t));
92 OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
93 return OC_STACK_NO_MEMORY;
96 OIC_LOG_V(DEBUG, TAG, "Add [%s:%d]'s context to OTMContext list", addr, port);
98 newItem->otmCtx = ctx;
99 OICStrcpy(newItem->endpoint.addr, sizeof(newItem->endpoint.addr), addr);
100 newItem->endpoint.port = port;
101 LL_APPEND(g_otmCtxList, newItem);
103 OIC_LOG(DEBUG, TAG, "OUT AddOTMContext");
108 const OTMContext_t* GetOTMContext(const char* addr, uint16_t port)
110 OIC_LOG(DEBUG, TAG, "IN GetOTMContext");
112 if (NULL != addr && 0 != port)
114 OTMContextItem_t* item = NULL;
115 OTMContextItem_t* temp = NULL;
117 LL_FOREACH_SAFE(g_otmCtxList, item, temp)
119 if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
120 port == item->endpoint.port)
122 OIC_LOG_V(DEBUG, TAG, "Found the OTMContext for [%s:%d]", addr, port);
128 OIC_LOG(DEBUG, TAG, "OUT GetOTMContext");