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 #ifndef __STDC_FORMAT_MACROS
27 #define __STDC_FORMAT_MACROS
31 #include "cainterface.h"
32 #include "catcpadapter.h"
33 #include "catcpinterface.h"
34 #include "caqueueingthread.h"
35 #include "caadapterutils.h"
37 #include "uarraylist.h"
38 #include "caremotehandler.h"
40 #include "oic_malloc.h"
41 #include "oic_string.h"
44 * Logging tag for module name.
46 #define TAG "OIC_CA_TCP_ADAP"
49 * Holds internal thread TCP data information.
53 CAEndpoint_t *remoteEndpoint;
59 #define CA_TCP_LISTEN_BACKLOG 3
61 #define CA_TCP_SELECT_TIMEOUT 10
64 * Queue handle for Send Data.
66 static CAQueueingThread_t *g_sendQueueHandle = NULL;
69 * Network Packet Received Callback to CA.
71 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
74 * Adapter Changed Callback to CA.
76 static CAAdapterChangeCallback g_networkChangeCallback = NULL;
79 * Connection Changed Callback to CA.
81 static CAConnectionChangeCallback g_connectionChangeCallback = NULL;
84 * error Callback to CA adapter.
86 static CAErrorHandleCallback g_errorCallback = NULL;
88 static void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep,
89 const void *data, uint32_t dataLength);
92 * KeepAlive Connected or Disconnected Callback to CA adapter.
94 static CAKeepAliveConnectionCallback g_connKeepAliveCallback = NULL;
96 static CAResult_t CATCPInitializeQueueHandles();
98 static void CATCPDeinitializeQueueHandles();
100 static void CATCPSendDataThread(void *threadData);
102 static CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint,
103 const void *data, size_t dataLength,
105 void CAFreeTCPData(CATCPData *ipData);
107 static void CADataDestroyer(void *data, uint32_t size);
109 CAResult_t CATCPInitializeQueueHandles()
111 // Check if the message queue is already initialized
112 if (g_sendQueueHandle)
114 OIC_LOG(DEBUG, TAG, "send queue handle is already initialized!");
118 // Create send message queue
119 g_sendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
120 if (!g_sendQueueHandle)
122 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
123 return CA_MEMORY_ALLOC_FAILED;
126 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle,
127 (const ca_thread_pool_t)caglobals.tcp.threadpool,
128 CATCPSendDataThread, CADataDestroyer))
130 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
131 OICFree(g_sendQueueHandle);
132 g_sendQueueHandle = NULL;
133 return CA_STATUS_FAILED;
139 void CATCPDeinitializeQueueHandles()
141 CAQueueingThreadDestroy(g_sendQueueHandle);
142 OICFree(g_sendQueueHandle);
143 g_sendQueueHandle = NULL;
146 void CATCPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status)
152 void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
155 VERIFY_NON_NULL_VOID(sep, TAG, "sep is NULL");
156 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
158 OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", sep->endpoint.addr, sep->endpoint.port);
160 if (g_networkPacketCallback)
162 g_networkPacketCallback(sep, data, dataLength);
166 void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data,
167 uint32_t dataLength, CAResult_t result)
169 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
170 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
174 g_errorCallback(endpoint, data, dataLength, result);
178 static void CATCPConnectionHandler(const CAEndpoint_t *endpoint, bool isConnected)
180 // Pass the changed connection status to RI Layer for keepalive.
181 if (g_connKeepAliveCallback)
183 g_connKeepAliveCallback(endpoint, isConnected);
186 // Pass the changed connection status to CAUtil.
187 if (g_connectionChangeCallback)
189 g_connectionChangeCallback(endpoint, isConnected);
193 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler)
195 g_connKeepAliveCallback = ConnHandler;
198 static void CAInitializeTCPGlobals()
200 caglobals.tcp.ipv4.fd = -1;
201 caglobals.tcp.ipv4.port = 0;
202 caglobals.tcp.ipv6.fd = -1;
203 caglobals.tcp.ipv6.port = 0;
204 caglobals.tcp.selectTimeout = CA_TCP_SELECT_TIMEOUT;
205 caglobals.tcp.listenBacklog = CA_TCP_LISTEN_BACKLOG;
206 caglobals.tcp.svrlist = NULL;
208 CATransportFlags_t flags = 0;
209 if (caglobals.client)
211 flags |= caglobals.clientFlags;
213 if (caglobals.server)
215 flags |= caglobals.serverFlags;
218 caglobals.tcp.ipv4tcpenabled = flags & CA_IPV4;
219 caglobals.tcp.ipv6tcpenabled = flags & CA_IPV6;
222 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
223 CANetworkPacketReceivedCallback networkPacketCallback,
224 CAAdapterChangeCallback netCallback,
225 CAConnectionChangeCallback connCallback,
226 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
228 OIC_LOG(DEBUG, TAG, "IN");
229 VERIFY_NON_NULL(registerCallback, TAG, "registerCallback");
230 VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback");
231 VERIFY_NON_NULL(netCallback, TAG, "netCallback");
232 VERIFY_NON_NULL(handle, TAG, "thread pool handle");
234 g_networkChangeCallback = netCallback;
235 g_connectionChangeCallback = connCallback;
236 g_networkPacketCallback = networkPacketCallback;
237 g_errorCallback = errorCallback;
239 CAInitializeTCPGlobals();
240 caglobals.tcp.threadpool = handle;
242 CATCPSetConnectionChangedCallback(CATCPConnectionHandler);
243 CATCPSetPacketReceiveCallback(CATCPPacketReceivedCB);
244 CATCPSetErrorHandler(CATCPErrorHandler);
246 CAConnectivityHandler_t tcpHandler = {
247 .startAdapter = CAStartTCP,
248 .startListenServer = CAStartTCPListeningServer,
249 .stopListenServer = CAStopTCPListeningServer,
250 .startDiscoveryServer = CAStartTCPDiscoveryServer,
251 .sendData = CASendTCPUnicastData,
252 .sendDataToAll = CASendTCPMulticastData,
253 .GetnetInfo = CAGetTCPInterfaceInformation,
254 .readData = CAReadTCPData,
255 .stopAdapter = CAStopTCP,
256 .terminate = CATerminateTCP,
257 .cType = CA_ADAPTER_TCP};
259 registerCallback(tcpHandler);
261 OIC_LOG(INFO, TAG, "OUT IntializeTCP is Success");
265 CAResult_t CAStartTCP()
267 if (CA_STATUS_OK != CATCPInitializeQueueHandles())
269 OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle");
271 return CA_STATUS_FAILED;
274 // Start send queue thread
275 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
277 OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread");
278 return CA_STATUS_FAILED;
284 CAResult_t CAStartTCPListeningServer()
286 if (!caglobals.server)
288 caglobals.server = true; // only needed to run CA tests
291 CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
292 if (CA_STATUS_OK != ret)
294 OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", ret);
301 CAResult_t CAStopTCPListeningServer()
306 CAResult_t CAStartTCPDiscoveryServer()
308 if (!caglobals.client)
310 caglobals.client = true; // only needed to run CA tests
313 CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
314 if (CA_STATUS_OK != ret)
316 OIC_LOG_V(ERROR, TAG, "Failed to start discovery server![%d]", ret);
323 static size_t CAQueueTCPData(bool isMulticast, const CAEndpoint_t *endpoint,
324 const void *data, size_t dataLength)
326 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", -1);
327 VERIFY_NON_NULL_RET(data, TAG, "data", -1);
331 OIC_LOG(ERROR, TAG, "Invalid Data Length");
335 VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
337 // Create TCPData to add to queue
338 CATCPData *tcpData = CACreateTCPData(endpoint, data, dataLength, isMulticast);
341 OIC_LOG(ERROR, TAG, "Failed to create ipData!");
344 // Add message to send queue
345 CAQueueingThreadAddData(g_sendQueueHandle, tcpData, sizeof(CATCPData));
350 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
351 const void *data, uint32_t dataLength)
353 return CAQueueTCPData(false, endpoint, data, dataLength);
356 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
357 const void *data, uint32_t dataLength)
359 return CAQueueTCPData(true, endpoint, data, dataLength);
362 CAResult_t CAReadTCPData()
367 CAResult_t CAStopTCP()
369 if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
371 CAQueueingThreadStop(g_sendQueueHandle);
376 //Re-initializing the Globals to start them again
377 CAInitializeTCPGlobals();
382 void CATerminateTCP()
384 CATCPSetPacketReceiveCallback(NULL);
386 CATCPDeinitializeQueueHandles();
389 void CATCPSendDataThread(void *threadData)
391 CATCPData *tcpData = (CATCPData *) threadData;
394 OIC_LOG(DEBUG, TAG, "Invalid TCP data!");
398 if (tcpData->isMulticast)
400 //Processing for sending multicast
401 OIC_LOG(DEBUG, TAG, "Send Multicast Data is called, not supported");
406 //Processing for sending unicast
407 CATCPSendData(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen, false);
411 CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint, const void *data,
412 size_t dataLength, bool isMulticast)
414 VERIFY_NON_NULL_RET(remoteEndpoint, TAG, "remoteEndpoint is NULL", NULL);
415 VERIFY_NON_NULL_RET(data, TAG, "data is NULL", NULL);
417 CATCPData *tcpData = (CATCPData *) OICCalloc(1, sizeof(*tcpData));
420 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
424 tcpData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
425 tcpData->data = (void *) OICMalloc(dataLength);
428 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
429 CAFreeTCPData(tcpData);
433 memcpy(tcpData->data, data, dataLength);
434 tcpData->dataLen = dataLength;
436 tcpData->isMulticast = isMulticast;
441 void CAFreeTCPData(CATCPData *tcpData)
443 VERIFY_NON_NULL_VOID(tcpData, TAG, "tcpData is NULL");
445 CAFreeEndpoint(tcpData->remoteEndpoint);
446 OICFree(tcpData->data);
450 void CADataDestroyer(void *data, uint32_t size)
452 if (size < sizeof(CATCPData))
454 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %" PRIu32, data, size);
456 CATCPData *TCPData = (CATCPData *) data;
458 CAFreeTCPData(TCPData);