replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / otmcontextlist.c
1 /* *****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * *****************************************************************/
20
21 #include "logger.h"
22 #include "oic_malloc.h"
23 #include "oic_string.h"
24 #include "octypes.h"
25 #include "ownershiptransfermanager.h"
26 #include "utlist.h"
27 #include "otmcontextlist.h"
28
29 #define TAG "OIC_OTM_CTX"
30
31 /**
32  * List for saving the OTMContext to be used while ownership transfer.
33  */
34 static OTMContextItem_t* g_otmCtxList = NULL;
35
36 void RemoveOTMContext(const char* addr, uint16_t port)
37 {
38     OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
39     size_t ctxCnt = 0;
40
41     if (NULL != addr && 0 != port)
42     {
43         OTMContextItem_t* item = NULL;
44         OTMContextItem_t* temp = NULL;
45
46         LL_FOREACH_SAFE(g_otmCtxList, item, temp)
47         {
48             ctxCnt++;
49             if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
50                 port == item->endpoint.port)
51             {
52                 OIC_LOG_V(DEBUG, TAG, "Remove [%s:%d]'s context from OTMContext list", addr, port);
53                 item->otmCtx = NULL;
54                 LL_DELETE(g_otmCtxList, item);
55                 OICFree(item);
56                 ctxCnt--;
57                 break;
58             }
59         }
60
61         if (0 == ctxCnt)
62         {
63             g_otmCtxList = NULL;
64         }
65     }
66
67     OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
68 }
69
70 void DeleteOTMContextList()
71 {
72     OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
73
74     OTMContextItem_t* item = NULL;
75     OTMContextItem_t* temp = NULL;
76     int index = 0;
77
78     LL_FOREACH_SAFE(g_otmCtxList, item, temp)
79     {
80         LL_DELETE(g_otmCtxList, item);
81         if (item)
82         {
83             OIC_LOG_V(DEBUG, TAG, "Found OTM Context Item #%d in List", index++);
84             if (item->otmCtx)
85             {
86                 OIC_LOG(DEBUG, TAG, "Found OTM Context is exist.");
87                 if (item->otmCtx->selectedDeviceInfo)
88                 {
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);
92                 }
93                 if (item->otmCtx->ctxResultArray)
94                 {
95                     OIC_LOG(DEBUG, TAG, "Found Result Array is exist in OTM Context.");
96                     OICFree(item->otmCtx->ctxResultArray);
97                 }
98                 OICFree(item->otmCtx);
99             }
100             OICFree(item);
101         }
102     }
103     g_otmCtxList = NULL;
104     OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
105 }
106
107 OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
108 {
109     OTMContextItem_t* item = NULL;
110     OTMContextItem_t* temp = NULL;
111     OTMContextItem_t* newItem = NULL;
112
113     OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
114
115     if (NULL == ctx || NULL == addr || 0 == strlen(addr) || 0 == port)
116     {
117         return OC_STACK_INVALID_PARAM;
118     }
119
120     LL_FOREACH_SAFE(g_otmCtxList, item, temp)
121     {
122             if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
123                 port == item->endpoint.port)
124             {
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;
128                 return OC_STACK_OK;
129             }
130     }
131
132     newItem = (OTMContextItem_t*)OICCalloc(1, sizeof(OTMContextItem_t));
133     if (NULL == newItem)
134     {
135         OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
136         return OC_STACK_NO_MEMORY;
137     }
138
139     OIC_LOG_V(DEBUG, TAG, "Add [%s:%d]'s context to OTMContext list", addr, port);
140
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);
145
146     OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
147
148     return OC_STACK_OK;
149 }
150
151 OTMContext_t* GetOTMContext(const char* addr, uint16_t port)
152 {
153     OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
154
155     if (NULL != addr && 0 != port)
156     {
157         OTMContextItem_t* item = NULL;
158         OTMContextItem_t* temp = NULL;
159
160         LL_FOREACH_SAFE(g_otmCtxList, item, temp)
161         {
162             if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
163                port == item->endpoint.port)
164             {
165                 OIC_LOG_V(DEBUG, TAG, "Found the OTMContext for [%s:%d]", addr, port);
166                 return item->otmCtx;
167             }
168         }
169     }
170
171     OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
172
173     return NULL;
174 }
175