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 ******************************************************************/
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"
33 #include "caadapterutils.h"
35 #include "caconnectionmanager.h"
36 #include "capolicymanager.h"
40 static oc_mutex g_threadCMConfigureMutex = NULL;
42 // context for connection manager
43 static CAConnectionManagerContext_t g_context = {.sendThreadFunc = NULL,
44 .receivedThreadFunc = NULL,
47 void CAStartConnectionManagerService(CMConfigureInfo_t info)
49 OIC_LOG(DEBUG, TAG, "CAStartConnectionManagerService");
51 oc_mutex_lock(g_threadCMConfigureMutex);
53 oc_mutex_unlock(g_threadCMConfigureMutex);
56 CAData_t* CAGetConnectionManagerMessageData(CAData_t *data)
58 OIC_LOG(DEBUG, TAG, "CAGetConnectionManagerMessageData");
60 VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
63 // decide specific reqeust/response message
68 CAResult_t CAInitializeConnectionManager(CASendThreadFunc sendThreadFunc,
69 CAReceiveThreadFunc receivedThreadFunc)
71 OIC_LOG(DEBUG, TAG, "CAInitializeConnectionManager");
73 if (!g_context.sendThreadFunc)
75 g_context.sendThreadFunc = sendThreadFunc;
78 if (!g_context.receivedThreadFunc)
80 g_context.receivedThreadFunc = receivedThreadFunc;
83 if (!g_context.dataList)
85 g_context.dataList = u_arraylist_create();
88 CAResult_t res = CAInitConnectionManagerMutexVariables();
89 if (CA_STATUS_OK != res)
91 u_arraylist_free(&g_context.dataList);
92 g_context.dataList = NULL;
93 OIC_LOG(ERROR, TAG, "init has failed");
98 void CATerminateConnectionManager()
100 OIC_LOG(DEBUG, TAG, "CATerminateConnectionManager");
102 if (g_context.dataList)
105 // Remove all of management data();
106 u_arraylist_free(&g_context.dataList);
108 CATerminateConnectionManagerMutexVariables();
111 CAResult_t CAInitConnectionManagerMutexVariables()
113 if (!g_context.dataListMutex)
115 g_context.dataListMutex = oc_mutex_new();
116 if (!g_context.dataListMutex)
118 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
119 return CA_STATUS_FAILED;
123 if (!g_context.dataSenderMutex)
125 g_context.dataSenderMutex = oc_mutex_new();
126 if (!g_context.dataSenderMutex)
128 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
129 CATerminateConnectionManagerMutexVariables();
130 return CA_STATUS_FAILED;
134 if (NULL == g_threadCMConfigureMutex)
136 g_threadCMConfigureMutex = oc_mutex_new();
137 if (NULL == g_threadCMConfigureMutex)
139 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
140 return CA_STATUS_FAILED;
147 void CATerminateConnectionManagerMutexVariables()
149 if (g_context.dataListMutex)
151 oc_mutex_free(g_context.dataListMutex);
152 g_context.dataListMutex = NULL;
155 if (g_context.dataSenderMutex)
157 oc_mutex_free(g_context.dataSenderMutex);
158 g_context.dataSenderMutex = NULL;
161 if (g_threadCMConfigureMutex)
163 oc_mutex_free(g_threadCMConfigureMutex);
164 g_threadCMConfigureMutex = NULL;