6cfc96fa6459f5afb9edb5a7f58c1f3f7c765f41
[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 "OTM_CTX_LIST"
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     OTMContext_t* retCtx = NULL;
39
40     OIC_LOG(DEBUG, TAG, "IN RemoveOTMContext");
41
42     if (NULL != addr && 0 != port)
43     {
44         OTMContextItem_t* item = NULL;
45         OTMContextItem_t* temp = NULL;
46
47         LL_FOREACH_SAFE(g_otmCtxList, item, temp)
48         {
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                 retCtx = item->otmCtx;
54                 item->otmCtx = NULL;
55                 LL_DELETE(g_otmCtxList, item);
56                 OICFree(item);
57                 break;
58             }
59         }
60     }
61
62     OIC_LOG(DEBUG, TAG, "OUT RemoveOTMContext");
63 }
64
65 OCStackResult AddOTMContext(OTMContext_t* ctx, const char* addr, uint16_t port)
66 {
67     OTMContextItem_t* item = NULL;
68     OTMContextItem_t* temp = NULL;
69     OTMContextItem_t* newItem = NULL;
70
71     OIC_LOG(DEBUG, TAG, "IN AddOTMContext");
72
73     if (NULL == ctx || NULL == addr || 0 == strlen(addr) || 0 == port)
74     {
75         return OC_STACK_INVALID_PARAM;
76     }
77
78     LL_FOREACH_SAFE(g_otmCtxList, item, temp)
79     {
80             if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
81                 port == item->endpoint.port)
82             {
83                 //if OTM Context already exists, just return OC_STACK_OK.
84                 OIC_LOG(DEBUG, TAG, "Same OTMContext already exists.");
85                 return OC_STACK_OK;
86             }
87     }
88
89     newItem = (OTMContextItem_t*)OICCalloc(1, sizeof(OTMContextItem_t));
90     if (NULL == newItem)
91     {
92         OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
93         return OC_STACK_NO_MEMORY;
94     }
95
96     OIC_LOG_V(DEBUG, TAG, "Add [%s:%d]'s context to OTMContext list", addr, port);
97
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);
102
103     OIC_LOG(DEBUG, TAG, "OUT AddOTMContext");
104
105     return OC_STACK_OK;
106 }
107
108 const OTMContext_t* GetOTMContext(const char* addr, uint16_t port)
109 {
110     OIC_LOG(DEBUG, TAG, "IN GetOTMContext");
111
112     if (NULL != addr && 0 != port)
113     {
114         OTMContextItem_t* item = NULL;
115         OTMContextItem_t* temp = NULL;
116
117         LL_FOREACH_SAFE(g_otmCtxList, item, temp)
118         {
119             if (strncmp(addr, item->endpoint.addr, sizeof(item->endpoint.addr)) == 0 &&
120                port == item->endpoint.port)
121             {
122                 OIC_LOG_V(DEBUG, TAG, "Found the OTMContext for [%s:%d]", addr, port);
123                 return item->otmCtx;
124             }
125         }
126     }
127
128     OIC_LOG(DEBUG, TAG, "OUT GetOTMContext");
129
130     return NULL;
131 }
132