1 /* ****************************************************************
3 * Copyright 2014 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 ******************************************************************/
20 #include "caipadapter.h"
27 #include "caipinterface.h"
28 #include "caqueueingthread.h"
29 #include "caadapterutils.h"
31 #include "caadapternetdtls.h"
34 #include "uarraylist.h"
35 #include "caremotehandler.h"
37 #include "oic_malloc.h"
38 #include "oic_string.h"
41 * Logging tag for module name.
43 #define TAG "OIC_CA_IP_ADAP"
47 * Holds inter thread ip data information.
51 CAEndpoint_t *remoteEndpoint;
58 * Queue handle for Send Data.
60 static CAQueueingThread_t *g_sendQueueHandle = NULL;
64 * Network Packet Received Callback to CA.
66 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
69 * Network Changed Callback to CA.
71 static CANetworkChangeCallback g_networkChangeCallback = NULL;
74 * error Callback to CA adapter.
76 static CAErrorHandleCallback g_errorCallback = NULL;
78 static void CAIPPacketReceivedCB(const CASecureEndpoint_t *endpoint,
79 const void *data, uint32_t dataLength);
81 static void CAIPPacketSendCB(CAEndpoint_t *endpoint,
82 const void *data, uint32_t dataLength);
87 static CAResult_t CAIPInitializeQueueHandles();
89 static void CAIPDeinitializeQueueHandles();
91 static void CAIPSendDataThread(void *threadData);
93 static CAIPData_t *CACreateIPData(const CAEndpoint_t *remoteEndpoint,
94 const void *data, uint32_t dataLength,
97 void CAFreeIPData(CAIPData_t *ipData);
99 static void CADataDestroyer(void *data, uint32_t size);
101 CAResult_t CAIPInitializeQueueHandles()
103 // Check if the message queue is already initialized
104 if (g_sendQueueHandle)
106 OIC_LOG(DEBUG, TAG, "send queue handle is already initialized!");
110 // Create send message queue
111 g_sendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
112 if (!g_sendQueueHandle)
114 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
115 return CA_MEMORY_ALLOC_FAILED;
118 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle,
119 (const ca_thread_pool_t)caglobals.ip.threadpool,
120 CAIPSendDataThread, CADataDestroyer))
122 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
123 OICFree(g_sendQueueHandle);
124 g_sendQueueHandle = NULL;
125 return CA_STATUS_FAILED;
131 void CAIPDeinitializeQueueHandles()
133 CAQueueingThreadDestroy(g_sendQueueHandle);
134 OICFree(g_sendQueueHandle);
135 g_sendQueueHandle = NULL;
138 #endif // SINGLE_THREAD
140 void CAIPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status)
147 static void CAIPPacketSendCB(CAEndpoint_t *endpoint, const void *data, uint32_t dataLength)
149 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
150 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
152 CAIPSendData(endpoint, data, dataLength, false);
157 void CAIPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
160 VERIFY_NON_NULL_VOID(sep, TAG, "sep is NULL");
161 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
163 OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", sep->endpoint.addr, sep->endpoint.port);
165 if (g_networkPacketCallback)
167 g_networkPacketCallback(sep, data, dataLength);
171 void CAIPErrorHandler (const CAEndpoint_t *endpoint, const void *data,
172 uint32_t dataLength, CAResult_t result)
174 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
175 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
177 void *buf = (void*)OICMalloc(sizeof(char) * dataLength);
180 OIC_LOG(ERROR, TAG, "Memory Allocation failed!");
183 memcpy(buf, data, dataLength);
186 g_errorCallback(endpoint, buf, dataLength, result);
194 static void CAInitializeIPGlobals()
196 caglobals.ip.u6.fd = -1;
197 caglobals.ip.u6s.fd = -1;
198 caglobals.ip.u4.fd = -1;
199 caglobals.ip.u4s.fd = -1;
200 caglobals.ip.m6.fd = -1;
201 caglobals.ip.m6s.fd = -1;
202 caglobals.ip.m4.fd = -1;
203 caglobals.ip.m4s.fd = -1;
204 caglobals.ip.u6.port = 0;
205 caglobals.ip.u6s.port = 0;
206 caglobals.ip.u4.port = 0;
207 caglobals.ip.u4s.port = 0;
208 caglobals.ip.m6.port = CA_COAP;
209 caglobals.ip.m6s.port = CA_SECURE_COAP;
210 caglobals.ip.m4.port = CA_COAP;
211 caglobals.ip.m4s.port = CA_SECURE_COAP;
213 CATransportFlags_t flags = 0;
214 if (caglobals.client)
216 flags |= caglobals.clientFlags;
218 if (caglobals.server)
220 flags |= caglobals.serverFlags;
222 caglobals.ip.ipv6enabled = flags & CA_IPV6;
223 caglobals.ip.ipv4enabled = flags & CA_IPV4;
224 caglobals.ip.dualstack = caglobals.ip.ipv6enabled && caglobals.ip.ipv4enabled;
227 CAResult_t CAInitializeIP(CARegisterConnectivityCallback registerCallback,
228 CANetworkPacketReceivedCallback networkPacketCallback,
229 CANetworkChangeCallback netCallback,
230 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
232 OIC_LOG(DEBUG, TAG, "IN");
233 VERIFY_NON_NULL(registerCallback, TAG, "registerCallback");
234 VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback");
235 VERIFY_NON_NULL(netCallback, TAG, "netCallback");
236 #ifndef SINGLE_THREAD
237 VERIFY_NON_NULL(handle, TAG, "thread pool handle");
240 g_networkChangeCallback = netCallback;
241 g_networkPacketCallback = networkPacketCallback;
242 g_errorCallback = errorCallback;
244 CAInitializeIPGlobals();
245 caglobals.ip.threadpool = handle;
247 CAIPSetPacketReceiveCallback(CAIPPacketReceivedCB);
249 CAAdapterNetDtlsInit();
251 CADTLSSetAdapterCallbacks(CAIPPacketReceivedCB, CAIPPacketSendCB, 0);
254 static const CAConnectivityHandler_t ipHandler =
256 .startAdapter = CAStartIP,
257 .stopAdapter = CAStopIP,
258 .startListenServer = CAStartIPListeningServer,
259 .stopListenServer = CAStopIPListeningServer,
260 .startDiscoveryServer = CAStartIPDiscoveryServer,
261 .sendData = CASendIPUnicastData,
262 .sendDataToAll = CASendIPMulticastData,
263 .GetnetInfo = CAGetIPInterfaceInformation,
264 .readData = CAReadIPData,
265 .terminate = CATerminateIP,
266 .cType = CA_ADAPTER_IP
268 registerCallback(ipHandler);
270 OIC_LOG(INFO, TAG, "OUT IntializeIP is Success");
274 CAResult_t CAStartIP()
276 CAIPStartNetworkMonitor();
278 uint16_t unicastPort = 55555;
279 // Address is hardcoded as we are using Single Interface
280 CAResult_t ret = CAIPStartServer();
281 if (CA_STATUS_OK != ret)
283 OIC_LOG_V(DEBUG, TAG, "CAIPStartServer failed[%d]", ret);
287 if (CA_STATUS_OK != CAIPInitializeQueueHandles())
289 OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle");
291 return CA_STATUS_FAILED;
294 // Start send queue thread
295 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
297 OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread");
298 return CA_STATUS_FAILED;
301 CAResult_t ret = CAIPStartServer((const ca_thread_pool_t)caglobals.ip.threadpool);
302 if (CA_STATUS_OK != ret)
304 OIC_LOG_V(ERROR, TAG, "Failed to start server![%d]", ret);
313 CAResult_t CAStartIPListeningServer()
315 CAResult_t ret = CAIPStartListenServer();
316 if (CA_STATUS_OK != ret)
318 OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", ret);
325 CAResult_t CAStopIPListeningServer()
327 CAResult_t ret = CAIPStopListenServer();
328 if (CA_STATUS_OK != ret)
330 OIC_LOG_V(ERROR, TAG, "Failed to stop listening server![%d]", ret);
336 CAResult_t CAStartIPDiscoveryServer()
338 return CAStartIPListeningServer();
341 static int32_t CAQueueIPData(bool isMulticast, const CAEndpoint_t *endpoint,
342 const void *data, uint32_t dataLength)
344 VERIFY_NON_NULL_RET(endpoint, TAG, "remoteEndpoint", -1);
345 VERIFY_NON_NULL_RET(data, TAG, "data", -1);
349 OIC_LOG(ERROR, TAG, "Invalid Data Length");
355 CAIPSendData(endpoint, data, dataLength, isMulticast);
360 VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
361 // Create IPData to add to queue
362 CAIPData_t *ipData = CACreateIPData(endpoint, data, dataLength, isMulticast);
365 OIC_LOG(ERROR, TAG, "Failed to create ipData!");
368 // Add message to send queue
369 CAQueueingThreadAddData(g_sendQueueHandle, ipData, sizeof(CAIPData_t));
371 #endif // SINGLE_THREAD
376 int32_t CASendIPUnicastData(const CAEndpoint_t *endpoint,
377 const void *data, uint32_t dataLength)
379 return CAQueueIPData(false, endpoint, data, dataLength);
382 int32_t CASendIPMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength)
384 return CAQueueIPData(true, endpoint, data, dataLength);
387 CAResult_t CAReadIPData()
393 CAResult_t CAStopIP()
396 CAAdapterNetDtlsDeInit();
399 #ifndef SINGLE_THREAD
400 if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
402 CAQueueingThreadStop(g_sendQueueHandle);
406 CAIPStopNetworkMonitor();
408 //Re-initializing the Globals to start them again
409 CAInitializeIPGlobals();
417 CADTLSSetAdapterCallbacks(NULL, NULL, 0);
420 CAIPSetPacketReceiveCallback(NULL);
422 #ifndef SINGLE_THREAD
423 CAIPDeinitializeQueueHandles();
427 #ifndef SINGLE_THREAD
429 void CAIPSendDataThread(void *threadData)
431 CAIPData_t *ipData = (CAIPData_t *) threadData;
434 OIC_LOG(DEBUG, TAG, "Invalid ip data!");
438 if (ipData->isMulticast)
440 //Processing for sending multicast
441 OIC_LOG(DEBUG, TAG, "Send Multicast Data is called");
442 CAIPSendData(ipData->remoteEndpoint, ipData->data, ipData->dataLen, true);
446 //Processing for sending unicast
448 if (ipData->remoteEndpoint && ipData->remoteEndpoint->flags & CA_SECURE)
450 OIC_LOG(DEBUG, TAG, "CAAdapterNetDtlsEncrypt called!");
451 CAResult_t result = CAAdapterNetDtlsEncrypt(ipData->remoteEndpoint,
452 ipData->data, ipData->dataLen);
453 if (CA_STATUS_OK != result)
455 OIC_LOG(ERROR, TAG, "CAAdapterNetDtlsEncrypt failed!");
457 OIC_LOG_V(DEBUG, TAG,
458 "CAAdapterNetDtlsEncrypt returned with result[%d]", result);
462 OIC_LOG(DEBUG, TAG, "Send Unicast Data is called");
463 CAIPSendData(ipData->remoteEndpoint, ipData->data, ipData->dataLen, false);
466 CAIPSendData(ipData->remoteEndpoint, ipData->data, ipData->dataLen, false);
473 #ifndef SINGLE_THREAD
474 CAIPData_t *CACreateIPData(const CAEndpoint_t *remoteEndpoint, const void *data,
475 uint32_t dataLength, bool isMulticast)
477 VERIFY_NON_NULL_RET(remoteEndpoint, TAG, "remoteEndpoint is NULL", NULL);
478 VERIFY_NON_NULL_RET(data, TAG, "IPData is NULL", NULL);
480 CAIPData_t *ipData = (CAIPData_t *) OICMalloc(sizeof(*ipData));
483 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
487 ipData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
488 ipData->data = (void *) OICMalloc(dataLength);
491 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
492 CAFreeIPData(ipData);
496 memcpy(ipData->data, data, dataLength);
497 ipData->dataLen = dataLength;
499 ipData->isMulticast = isMulticast;
504 void CAFreeIPData(CAIPData_t *ipData)
506 VERIFY_NON_NULL_VOID(ipData, TAG, "ipData is NULL");
508 CAFreeEndpoint(ipData->remoteEndpoint);
509 OICFree(ipData->data);
513 void CADataDestroyer(void *data, uint32_t size)
515 if (size < sizeof(CAIPData_t))
517 OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
519 CAIPData_t *etdata = (CAIPData_t *) data;
521 CAFreeIPData(etdata);
524 #endif // SINGLE_THREAD