replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / caconnectionmanager.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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "caadapterutils.h"
26 #include "cainterface.h"
27 #include "camessagehandler.h"
28 #include "caremotehandler.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31 #include "octhread.h"
32 #include "logger.h"
33 #include "caadapterutils.h"
34
35 #include "caconnectionmanager.h"
36 #include "capolicymanager.h"
37
38 #define TAG "OIC_CM"
39
40 static oc_mutex g_threadCMConfigureMutex = NULL;
41
42 // context for connection manager
43 static CAConnectionManagerContext_t g_context = {.sendThreadFunc = NULL,
44                                                  .receivedThreadFunc = NULL,
45                                                  .dataList = NULL};
46
47 void CAStartConnectionManagerService(CMConfigureInfo_t info)
48 {
49     OIC_LOG(DEBUG, TAG, "CAStartConnectionManagerService");
50
51     oc_mutex_lock(g_threadCMConfigureMutex);
52     CMSetConfigure(info);
53     oc_mutex_unlock(g_threadCMConfigureMutex);
54 }
55
56 CAData_t* CAGetConnectionManagerMessageData(CAData_t *data)
57 {
58     OIC_LOG(DEBUG, TAG, "CAGetConnectionManagerMessageData");
59
60     VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
61
62     // TODO
63     // decide specific reqeust/response message
64
65     return data;
66 }
67
68 CAResult_t CAInitializeConnectionManager(CASendThreadFunc sendThreadFunc,
69                                          CAReceiveThreadFunc receivedThreadFunc)
70 {
71     OIC_LOG(DEBUG, TAG, "CAInitializeConnectionManager");
72
73     if (!g_context.sendThreadFunc)
74     {
75         g_context.sendThreadFunc = sendThreadFunc;
76     }
77
78     if (!g_context.receivedThreadFunc)
79     {
80         g_context.receivedThreadFunc = receivedThreadFunc;
81     }
82
83     if (!g_context.dataList)
84     {
85         g_context.dataList = u_arraylist_create();
86     }
87
88     CAResult_t res = CAInitConnectionManagerMutexVariables();
89     if (CA_STATUS_OK != res)
90     {
91         u_arraylist_free(&g_context.dataList);
92         g_context.dataList = NULL;
93         OIC_LOG(ERROR, TAG, "init has failed");
94     }
95     return res;
96 }
97
98 void CATerminateConnectionManager()
99 {
100     OIC_LOG(DEBUG, TAG, "CATerminateConnectionManager");
101
102     if (g_context.dataList)
103     {
104         // TODO
105         // Remove all of management data();
106         u_arraylist_free(&g_context.dataList);
107     }
108     CATerminateConnectionManagerMutexVariables();
109 }
110
111 CAResult_t CAInitConnectionManagerMutexVariables()
112 {
113     if (!g_context.dataListMutex)
114     {
115         g_context.dataListMutex = oc_mutex_new();
116         if (!g_context.dataListMutex)
117         {
118             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
119             return CA_STATUS_FAILED;
120         }
121     }
122
123     if (!g_context.dataSenderMutex)
124     {
125         g_context.dataSenderMutex = oc_mutex_new();
126         if (!g_context.dataSenderMutex)
127         {
128             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
129             CATerminateConnectionManagerMutexVariables();
130             return CA_STATUS_FAILED;
131         }
132     }
133
134     if (NULL == g_threadCMConfigureMutex)
135     {
136         g_threadCMConfigureMutex = oc_mutex_new();
137         if (NULL == g_threadCMConfigureMutex)
138         {
139             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
140             return CA_STATUS_FAILED;
141         }
142     }
143
144     return CA_STATUS_OK;
145 }
146
147 void CATerminateConnectionManagerMutexVariables()
148 {
149     if (g_context.dataListMutex)
150     {
151         oc_mutex_free(g_context.dataListMutex);
152         g_context.dataListMutex = NULL;
153     }
154
155     if (g_context.dataSenderMutex)
156     {
157         oc_mutex_free(g_context.dataSenderMutex);
158         g_context.dataSenderMutex = NULL;
159     }
160
161     if (g_threadCMConfigureMutex)
162     {
163         oc_mutex_free(g_threadCMConfigureMutex);
164         g_threadCMConfigureMutex = NULL;
165     }
166 }