1 /* ****************************************************************
3 * Copyright 2015 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 ******************************************************************/
26 #define __STDC_FORMAT_MACROS
29 #include "cainterface.h"
30 #include "catcpadapter.h"
31 #include "catcpinterface.h"
32 #include "caqueueingthread.h"
33 #include "caadapterutils.h"
35 #include "uarraylist.h"
36 #include "caremotehandler.h"
38 #include "oic_malloc.h"
39 #include "oic_string.h"
42 * Logging tag for module name.
44 #define TAG "OIC_CA_TCP_ADAP"
47 * Holds internal thread TCP data information.
51 CAEndpoint_t *remoteEndpoint;
57 #define CA_TCP_LISTEN_BACKLOG 3
59 #define CA_TCP_SELECT_TIMEOUT 10
62 * Queue handle for Send Data.
64 static CAQueueingThread_t *g_sendQueueHandle = NULL;
67 * Network Packet Received Callback to CA.
69 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
72 * Network Changed Callback to CA.
74 static CANetworkChangeCallback g_networkChangeCallback = NULL;
77 * error Callback to CA adapter.
79 static CAErrorHandleCallback g_errorCallback = NULL;
81 static void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep,
82 const void *data, uint32_t dataLength);
85 * KeepAlive Connected Callback to CA adapter.
87 static CAKeepAliveConnectedCallback g_connCallback = NULL;
90 * KeepAlive Disconnected Callback to CA adapter.
92 static CAKeepAliveDisconnectedCallback g_disconnCallback = NULL;
94 static CAResult_t CATCPInitializeQueueHandles();
96 static void CATCPDeinitializeQueueHandles();
98 static void CATCPSendDataThread(void *threadData);
100 static CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint,
101 const void *data, size_t dataLength,
103 void CAFreeTCPData(CATCPData *ipData);
105 static void CADataDestroyer(void *data, uint32_t size);
107 CAResult_t CATCPInitializeQueueHandles()
109 // Check if the message queue is already initialized
110 if (g_sendQueueHandle)
112 OIC_LOG(DEBUG, TAG, "send queue handle is already initialized!");
116 // Create send message queue
117 g_sendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
118 if (!g_sendQueueHandle)
120 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
121 return CA_MEMORY_ALLOC_FAILED;
124 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle,
125 (const ca_thread_pool_t)caglobals.tcp.threadpool,
126 CATCPSendDataThread, CADataDestroyer))
128 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
129 OICFree(g_sendQueueHandle);
130 g_sendQueueHandle = NULL;
131 return CA_STATUS_FAILED;
137 void CATCPDeinitializeQueueHandles()
139 CAQueueingThreadDestroy(g_sendQueueHandle);
140 OICFree(g_sendQueueHandle);
141 g_sendQueueHandle = NULL;
144 void CATCPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status)
150 void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
153 VERIFY_NON_NULL_VOID(sep, TAG, "sep is NULL");
154 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
156 OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", sep->endpoint.addr, sep->endpoint.port);
158 if (g_networkPacketCallback)
160 g_networkPacketCallback(sep, data, dataLength);
164 void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data,
165 uint32_t dataLength, CAResult_t result)
167 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
168 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
172 g_errorCallback(endpoint, data, dataLength, result);
176 static void CATCPKeepAliveHandler(const char *addr, uint16_t port, bool isConnected)
178 CAEndpoint_t endpoint = { .adapter = CA_ADAPTER_TCP,
180 OICStrcpy(endpoint.addr, sizeof(endpoint.addr), addr);
184 g_connCallback(&endpoint);
188 g_disconnCallback(&endpoint);
192 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectedCallback ConnHandler,
193 CAKeepAliveDisconnectedCallback DisconnHandler)
195 g_connCallback = ConnHandler;
196 g_disconnCallback = DisconnHandler;
198 CATCPSetKeepAliveCallback(CATCPKeepAliveHandler);
201 static void CAInitializeTCPGlobals()
203 caglobals.tcp.selectTimeout = CA_TCP_SELECT_TIMEOUT;
204 caglobals.tcp.listenBacklog = CA_TCP_LISTEN_BACKLOG;
205 caglobals.tcp.svrlist = NULL;
207 CATransportFlags_t flags = 0;
208 if (caglobals.client)
210 flags |= caglobals.clientFlags;
212 if (caglobals.server)
214 flags |= caglobals.serverFlags;
217 caglobals.tcp.ipv4tcpenabled = flags & CA_IPV4;
220 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
221 CANetworkPacketReceivedCallback networkPacketCallback,
222 CANetworkChangeCallback netCallback,
223 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
225 OIC_LOG(DEBUG, TAG, "IN");
226 VERIFY_NON_NULL(registerCallback, TAG, "registerCallback");
227 VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback");
228 VERIFY_NON_NULL(netCallback, TAG, "netCallback");
229 VERIFY_NON_NULL(handle, TAG, "thread pool handle");
231 g_networkChangeCallback = netCallback;
232 g_networkPacketCallback = networkPacketCallback;
233 g_errorCallback = errorCallback;
235 CAInitializeTCPGlobals();
236 caglobals.tcp.threadpool = handle;
238 CATCPSetPacketReceiveCallback(CATCPPacketReceivedCB);
239 CATCPSetErrorHandler(CATCPErrorHandler);
241 CAConnectivityHandler_t TCPHandler = {
242 .startAdapter = CAStartTCP,
243 .startListenServer = CAStartTCPListeningServer,
244 .stopListenServer = CAStopTCPListeningServer,
245 .startDiscoveryServer = CAStartTCPDiscoveryServer,
246 .sendData = CASendTCPUnicastData,
247 .sendDataToAll = CASendTCPMulticastData,
248 .GetnetInfo = CAGetTCPInterfaceInformation,
249 .readData = CAReadTCPData,
250 .stopAdapter = CAStopTCP,
251 .terminate = CATerminateTCP,
252 .cType = CA_ADAPTER_TCP};
254 registerCallback(TCPHandler);
256 OIC_LOG(INFO, TAG, "OUT IntializeTCP is Success");
260 CAResult_t CAStartTCP()
262 if (CA_STATUS_OK != CATCPInitializeQueueHandles())
264 OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle");
266 return CA_STATUS_FAILED;
269 // Start send queue thread
270 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
272 OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread");
273 return CA_STATUS_FAILED;
276 CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
277 if (CA_STATUS_OK != ret)
279 OIC_LOG_V(ERROR, TAG, "Failed to start server![%d]", ret);
286 CAResult_t CAStartTCPListeningServer()
291 CAResult_t CAStopTCPListeningServer()
296 CAResult_t CAStartTCPDiscoveryServer()
301 static size_t CAQueueTCPData(bool isMulticast, const CAEndpoint_t *endpoint,
302 const void *data, size_t dataLength)
304 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", -1);
305 VERIFY_NON_NULL_RET(data, TAG, "data", -1);
309 OIC_LOG(ERROR, TAG, "Invalid Data Length");
313 VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
315 // Create TCPData to add to queue
316 CATCPData *tcpData = CACreateTCPData(endpoint, data, dataLength, isMulticast);
319 OIC_LOG(ERROR, TAG, "Failed to create ipData!");
322 // Add message to send queue
323 CAQueueingThreadAddData(g_sendQueueHandle, tcpData, sizeof(CATCPData));
328 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
329 const void *data, uint32_t dataLength)
331 return CAQueueTCPData(false, endpoint, data, dataLength);
334 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
335 const void *data, uint32_t dataLength)
337 return CAQueueTCPData(true, endpoint, data, dataLength);
340 CAResult_t CAReadTCPData()
345 CAResult_t CAStopTCP()
347 if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
349 CAQueueingThreadStop(g_sendQueueHandle);
354 //Re-initializing the Globals to start them again
355 CAInitializeTCPGlobals();
360 void CATerminateTCP()
362 CATCPSetPacketReceiveCallback(NULL);
364 CATCPDeinitializeQueueHandles();
367 void CATCPSendDataThread(void *threadData)
369 CATCPData *tcpData = (CATCPData *) threadData;
372 OIC_LOG(DEBUG, TAG, "Invalid TCP data!");
376 if (tcpData->isMulticast)
378 //Processing for sending multicast
379 OIC_LOG(DEBUG, TAG, "Send Multicast Data is called, not supported");
384 //Processing for sending unicast
385 CATCPSendData(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen, false);
389 CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint, const void *data,
390 size_t dataLength, bool isMulticast)
392 VERIFY_NON_NULL_RET(remoteEndpoint, TAG, "remoteEndpoint is NULL", NULL);
393 VERIFY_NON_NULL_RET(data, TAG, "data is NULL", NULL);
395 CATCPData *tcpData = (CATCPData *) OICCalloc(1, sizeof(*tcpData));
398 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
402 tcpData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
403 tcpData->data = (void *) OICMalloc(dataLength);
406 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
407 CAFreeTCPData(tcpData);
411 memcpy(tcpData->data, data, dataLength);
412 tcpData->dataLen = dataLength;
414 tcpData->isMulticast = isMulticast;
419 void CAFreeTCPData(CATCPData *tcpData)
421 VERIFY_NON_NULL_VOID(tcpData, TAG, "tcpData is NULL");
423 CAFreeEndpoint(tcpData->remoteEndpoint);
424 OICFree(tcpData->data);
428 void CADataDestroyer(void *data, uint32_t size)
430 if (size < sizeof(CATCPData))
432 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %" PRIu32, data, size);
434 CATCPData *TCPData = (CATCPData *) data;
436 CAFreeTCPData(TCPData);