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 "cawifiadapter.h"
27 #include "cawifiinterface.h"
28 #include "caqueueingthread.h"
29 #include "caadapterutils.h"
31 #include "caadapternetdtls.h"
35 #include "oic_malloc.h"
38 * @def WIFI_ADAPTER_TAG
39 * @brief Logging tag for module name
41 #define WIFI_ADAPTER_TAG "WIFI_ADAP"
45 * @brief Port to listen for incoming data. port 5683 is as per COAP RFC.
51 * @brief Secure port to listen for incoming data
53 #define CA_SECURE_PORT 5684
57 * @brief Multicast Port Number
59 #define CA_MCAST_PORT 5298
62 * @def CA_MULTICAST_IP
63 * @brief Multicast IP Address
65 #define CA_MULTICAST_IP "224.0.1.187"
70 CARemoteEndpoint_t *remoteEndpoint;
76 * @var gNetworkPacketCallback
77 * @brief Network Packet Received Callback to CA
79 static CANetworkPacketReceivedCallback gNetworkPacketCallback = NULL;
82 * @var gNetworkChangeCb
83 * @brief Network Changed Callback to CA
85 static CANetworkChangeCallback gNetworkChangeCallback = NULL;
88 * @var gIsMulticastServerStarted
89 * @brief Flag to check if multicast server is started
91 static bool gIsMulticastServerStarted = false;
94 * @var gIsStartServerCalled
95 * @brief Flag to check if server start requested by CA.
97 static bool gStartUnicastServerRequested = false;
100 * @var gUnicastServerport
101 * @brief port number on which unicast server is running.
103 static int16_t gUnicastServerport = 0;
107 * @var gSecureUnicastServerport
108 * @brief port number on which secure unicast server is running.
110 static int16_t gSecureUnicastServerport = 0;
114 * @var gIsStartServerCalled
115 * @brief Flag to check if server start requested by CA.
117 static bool gStartMulticastServerRequested = false;
120 * @var gSendQueueHandle
121 * @brief Queue handle for Send Data
123 static CAQueueingThread_t *gSendQueueHandle = NULL;
127 * @brief ThreadPool for storing u_thread_pool_t handle passed from CA
129 static u_thread_pool_t gThreadPool = NULL;
131 static CAResult_t CAWiFiInitializeQueueHandles();
132 static void CAWiFiDeinitializeQueueHandles();
133 static void CAWiFiNotifyNetworkChange(const char *address, const int16_t port,
134 const CANetworkStatus_t status);
135 static void CAWiFiConnectionStateCB(const char *ipAddress,
136 const CANetworkStatus_t status);
137 static void CAWiFiPacketReceivedCB(const char *ipAddress, const uint32_t port,
138 const void *data, const uint32_t dataLength,
139 const CABool_t isSecured);
141 static uint32_t CAWiFiPacketSendCB(const char *ipAddress, const uint32_t port,
142 const void *data, const uint32_t dataLength);
144 static CAResult_t CAWiFiStopServers();
145 static void CAWiFiSendDataThread(void *threadData);
146 static CAWiFiData *CACreateWiFiData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
147 uint32_t dataLength);
148 void CAFreeWiFiData(CAWiFiData *wifiData);
151 CAResult_t CAWiFiInitializeQueueHandles()
153 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
155 // Check if the message queue is already initialized
156 if (gSendQueueHandle)
158 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "Already queue is initialized!");
162 // Create send message queue
163 gSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
164 if (!gSendQueueHandle)
166 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Memory allocation failed!");
167 return CA_MEMORY_ALLOC_FAILED;
170 if (CA_STATUS_OK != CAQueueingThreadInitialize(gSendQueueHandle, gThreadPool,
171 CAWiFiSendDataThread))
173 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Failed to Initialize send queue thread");
174 OICFree(gSendQueueHandle);
175 gSendQueueHandle = NULL;
176 return CA_STATUS_FAILED;
179 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
183 void CAWiFiDeinitializeQueueHandles()
185 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
187 CAQueueingThreadDestroy(gSendQueueHandle);
188 OICFree(gSendQueueHandle);
189 gSendQueueHandle = NULL;
191 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
194 void CAWiFiNotifyNetworkChange(const char *address, const int16_t port,
195 const CANetworkStatus_t status)
197 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
199 CALocalConnectivity_t *localEndpoint = CAAdapterCreateLocalEndpoint(CA_WIFI, address);
202 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Out of memory");
206 localEndpoint->addressInfo.IP.port = port;
207 if (NULL != gNetworkChangeCallback)
209 gNetworkChangeCallback(localEndpoint, status);
212 CAAdapterFreeLocalEndpoint(localEndpoint);
215 void CAWiFiConnectionStateCB(const char *ipAddress,
216 const CANetworkStatus_t status)
218 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
220 CAResult_t ret = CA_STATUS_FAILED;
223 * If Wifi is connected, then get the latest IP from the WIFI Interface
224 * and start unicast and multicast servers if requested earlier
226 if (CA_INTERFACE_UP == status)
228 int16_t port = CA_PORT;
229 int32_t serverFd = -1;
231 // Start Unicast server if requested earlier
232 if (gStartUnicastServerRequested)
234 ret = CAWiFiStartUnicastServer("0.0.0.0", &port, false, false, &serverFd);
235 if (CA_STATUS_OK == ret)
237 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Unicast server started on %d port", port);
238 CAWiFiSetUnicastSocket(serverFd);
239 gUnicastServerport = port;
243 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to start Unicast server [%d]", ret);
247 port = CA_SECURE_PORT;
248 ret = CAWiFiStartUnicastServer("0.0.0.0", &port, false, true, &serverFd);
249 if (CA_STATUS_OK == ret)
251 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Secure Unicast server started on %d", port);
252 CAWiFiSetSecureUnicastSocket(serverFd);
253 gSecureUnicastServerport = port;
257 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to start secure Unicast server [%d]",
263 // Start Multicast server if requested earlier
264 if (gStartMulticastServerRequested)
266 int16_t multicastPort = CA_MCAST_PORT;
267 int32_t multicastFd = 0;
268 ret = CAWiFiStartMulticastServer("0.0.0.0", CA_MULTICAST_IP, multicastPort,
270 if (CA_STATUS_OK == ret)
272 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Multicast server started on %d port",
274 gIsMulticastServerStarted = true;
278 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to start Multicast server [%d]", ret);
282 char *address = NULL;
283 char *ifcName = NULL;
284 ret = CAWiFiGetInterfaceInfo(&ifcName, &address);
285 if (CA_STATUS_OK != ret || NULL == address)
287 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to get interface info [%d]", ret);
293 // Notify network change to CA
294 CAWiFiNotifyNetworkChange(address, port, status);
300 CAWiFiNotifyNetworkChange("", 0, status);
302 // Stop Unicast, Secured unicast and Multicast servers
306 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
310 uint32_t CAWiFiPacketSendCB(const char *ipAddress, const uint32_t port,
311 const void *data, const uint32_t dataLength)
313 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
315 uint32_t sentLength = CAWiFiSendData(ipAddress, port, data, dataLength, CA_FALSE, CA_TRUE);
317 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Successfully sent %d of encripted data!", sentLength);
323 void CAWiFiPacketReceivedCB(const char *ipAddress, const uint32_t port,
324 const void *data, const uint32_t dataLength, const CABool_t isSecured)
326 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
327 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Address: %s, port:%d, data:%s", ipAddress, port, data);
329 // CA is freeing this memory
330 CARemoteEndpoint_t *endPoint = CAAdapterCreateRemoteEndpoint(CA_WIFI, ipAddress, NULL);
331 if (NULL == endPoint)
333 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Out of memory");
336 endPoint->addressInfo.IP.port = port;
337 endPoint->isSecured = isSecured;
339 void *buf = OICMalloc(dataLength + 1);
342 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Out of memory");
343 CAAdapterFreeRemoteEndpoint(endPoint);
346 memcpy(buf, data, dataLength);
347 memset(buf + dataLength, 0, 1);
348 if (gNetworkPacketCallback)
350 gNetworkPacketCallback(endPoint, buf, dataLength);
352 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
355 CAResult_t CAInitializeWifi(CARegisterConnectivityCallback registerCallback,
356 CANetworkPacketReceivedCallback networkPacketCallback,
357 CANetworkChangeCallback netCallback, u_thread_pool_t handle)
359 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
360 VERIFY_NON_NULL(registerCallback, WIFI_ADAPTER_TAG, "registerCallback");
361 VERIFY_NON_NULL(networkPacketCallback, WIFI_ADAPTER_TAG, "networkPacketCallback");
362 VERIFY_NON_NULL(netCallback, WIFI_ADAPTER_TAG, "netCallback");
363 VERIFY_NON_NULL(handle, WIFI_ADAPTER_TAG, "thread pool handle");
365 gThreadPool = handle;
366 gNetworkChangeCallback = netCallback;
367 gNetworkPacketCallback = networkPacketCallback;
369 CAResult_t ret = CAWiFiInitializeNetworkMonitor(gThreadPool);
370 if (CA_STATUS_OK != ret)
372 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to initialize n/w monitor![%d]", ret);
375 CAWiFiSetConnectionStateChangeCallback(CAWiFiConnectionStateCB);
377 ret = CAWiFiInitializeServer(gThreadPool);
378 if (CA_STATUS_OK != ret)
380 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to initialize server![%d]", ret);
385 CAWiFiSetPacketReceiveCallback(CAWiFiPacketReceivedCB);
387 CAAdapterNetDtlsInit();
388 CADTLSSetAdapterCallbacks(CAWiFiPacketReceivedCB, CAWiFiPacketSendCB, DTLS_WIFI);
391 CAConnectivityHandler_t wifiHandler;
392 wifiHandler.startAdapter = CAStartWIFI;
393 wifiHandler.startListenServer = CAStartWIFIListeningServer;
394 wifiHandler.startDiscoverServer = CAStartWIFIDiscoveryServer;
395 wifiHandler.sendData = CASendWIFIUnicastData;
396 wifiHandler.sendDataToAll = CASendWIFIMulticastData;
397 wifiHandler.GetnetInfo = CAGetWIFIInterfaceInformation;
398 wifiHandler.readData = CAReadWIFIData;
399 wifiHandler.stopAdapter = CAStopWIFI;
400 wifiHandler.terminate = CATerminateWIfI;
401 registerCallback(wifiHandler, CA_WIFI);
403 if (CA_STATUS_OK != CAWiFiInitializeQueueHandles())
405 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Failed to Initialize Queue Handle");
407 return CA_STATUS_FAILED;
410 OIC_LOG(INFO, WIFI_ADAPTER_TAG, "IntializeWifi is Success");
414 CAResult_t CAStartWIFI()
416 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
418 // Start monitoring wifi network
419 CAResult_t ret = CAWiFiStartNetworkMonitor();
420 if (CA_STATUS_OK != ret)
422 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Failed to Start n/w monitor");
425 // Start send queue thread
426 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
428 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Failed to Start Send Data Thread");
429 return CA_STATUS_FAILED;
432 gStartUnicastServerRequested = true;
433 bool retVal = CAWiFiIsConnected();
436 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "WIFI is not Connected");
442 ret = CAWiFiGetInterfaceInfo(&ifcName, &ifcAdrs);
443 if(CA_STATUS_OK != ret)
445 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Failed to get wifi interface info [%d]", ret);
449 int16_t unicastPort = CA_PORT;
450 int32_t serverFd = 0;
451 // Address is hardcoded as we are using Single Interface
452 ret = CAWiFiStartUnicastServer(ifcAdrs, &unicastPort, false, false, &serverFd);
453 if (CA_STATUS_OK == ret)
455 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Unicast server started on %d port", unicastPort);
456 CAWiFiSetUnicastSocket(serverFd);
457 gUnicastServerport = unicastPort;
461 // Address is hardcoded as we are using Single Interface
462 unicastPort = CA_SECURE_PORT;
463 ret = CAWiFiStartUnicastServer(ifcAdrs, &unicastPort, false, true, &serverFd);
464 if (CA_STATUS_OK == ret)
466 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Secure Unicast server started on %d port", unicastPort);
467 CAWiFiSetSecureUnicastSocket(serverFd);
468 gSecureUnicastServerport = unicastPort;
473 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
477 CAResult_t CAStartWIFIListeningServer()
479 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
481 CAResult_t ret = CA_STATUS_OK;
482 int16_t multicastPort = CA_MCAST_PORT;
484 if (gIsMulticastServerStarted == true)
486 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG,
487 "Failed to Start Multicast Server, Already Started!");
488 return CA_SERVER_STARTED_ALREADY;
491 gStartMulticastServerRequested = true;
492 bool retVal = CAWiFiIsConnected();
495 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG,
496 "Failed to Start Multicast Server, WIFI not Connected");
502 ret = CAWiFiGetInterfaceInfo(&ifcName, &ifcAdrs);
503 if(CA_STATUS_OK != ret)
505 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Failed to get wifi interface info [%d]", ret);
509 int32_t multicastFd = 0;
510 ret = CAWiFiStartMulticastServer(ifcAdrs, CA_MULTICAST_IP, multicastPort, &multicastFd);
511 if (CA_STATUS_OK == ret)
513 OIC_LOG(INFO, WIFI_ADAPTER_TAG, "Multicast Server is Started Successfully");
514 gIsMulticastServerStarted = true;
519 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
523 CAResult_t CAStartWIFIDiscoveryServer()
525 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
526 return CAStartWIFIListeningServer();
529 uint32_t CASendWIFIUnicastData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
532 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
534 uint32_t dataSize = 0;
535 VERIFY_NON_NULL_RET(remoteEndpoint, WIFI_ADAPTER_TAG, "remoteEndpoint", dataSize);
536 VERIFY_NON_NULL_RET(data, WIFI_ADAPTER_TAG, "data", dataSize);
537 VERIFY_NON_NULL_RET(gSendQueueHandle, WIFI_ADAPTER_TAG, "sendQueueHandle", dataSize);
541 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Invalid Data Length");
545 // Create WifiData to add to queue
546 CAWiFiData *wifiData = CACreateWiFiData(remoteEndpoint, data, dataLength);
549 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to create wifidata!");
550 return CA_MEMORY_ALLOC_FAILED;
553 // Add message to send queue
554 CAQueueingThreadAddData(gSendQueueHandle, wifiData, sizeof(CAWiFiData));
556 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
560 uint32_t CASendWIFIMulticastData(void *data, uint32_t dataLength)
562 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
564 uint32_t dataSize = 0;
565 VERIFY_NON_NULL_RET(data, WIFI_ADAPTER_TAG, "data", dataSize);
566 VERIFY_NON_NULL_RET(gSendQueueHandle, WIFI_ADAPTER_TAG, "sendQueueHandle", dataSize);
570 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Invalid Data Length");
574 // Create WifiData to add to queue
575 CAWiFiData *wifiData = CACreateWiFiData(NULL, data, dataLength);
578 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to create wifidata!");
579 return CA_MEMORY_ALLOC_FAILED;
582 // Add message to send queue
583 CAQueueingThreadAddData(gSendQueueHandle, wifiData, sizeof(CAWiFiData));
585 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
589 CAResult_t CAGetWIFIInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
591 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
592 VERIFY_NON_NULL(info, WIFI_ADAPTER_TAG, "info");
593 VERIFY_NON_NULL(size, WIFI_ADAPTER_TAG, "size");
595 CALocalConnectivity_t *netInfo = NULL;
596 bool retVal = CAWiFiIsConnected();
599 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG,
600 "Failed to get interface address, WIFI not Connected", CA_ADAPTER_NOT_ENABLED);
601 return CA_ADAPTER_NOT_ENABLED;
604 int32_t netInfoSize = 1;
606 if (gSecureUnicastServerport)
611 netInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * netInfoSize);
612 VERIFY_NON_NULL_RET(netInfo, WIFI_ADAPTER_TAG, "malloc failed", CA_MEMORY_ALLOC_FAILED);
613 memset(netInfo, 0, sizeof(CALocalConnectivity_t) * netInfoSize);
615 char *ipAddress = NULL;
616 char *ifcName = NULL;
617 CAResult_t ret = CAWiFiGetInterfaceInfo(&ifcName, &ipAddress);
618 if (CA_STATUS_OK != ret || NULL == ipAddress)
620 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to get interface info [%d]", ret);
628 // Create local endpoint using util function
629 CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_WIFI, ipAddress);
630 if (NULL == endpoint)
632 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to create Local Endpoint!",
633 CA_MEMORY_ALLOC_FAILED);
637 return CA_MEMORY_ALLOC_FAILED;
640 // copy unciast server information
641 endpoint->isSecured = CA_FALSE;
642 endpoint->addressInfo.IP.port = gUnicastServerport;
643 memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
646 // copy sevure unicast server information
647 if (gSecureUnicastServerport)
649 endpoint->isSecured = CA_TRUE;
650 endpoint->addressInfo.IP.port = gSecureUnicastServerport;
651 memcpy(&netInfo[1], endpoint, sizeof(CALocalConnectivity_t));
659 CAAdapterFreeLocalEndpoint(endpoint);
661 OIC_LOG(INFO, WIFI_ADAPTER_TAG, "GetWIFIInterfaceInformation success");
662 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
666 CAResult_t CAReadWIFIData()
668 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
669 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
673 CAResult_t CAWiFiStopServers()
675 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
677 // Stop unicast server and set client socket accordingly
678 if (CA_STATUS_OK == CAWiFiStopUnicastServer())
680 CAWiFiSetUnicastSocket(-1);
681 gUnicastServerport = -1;
684 // Stop secure unicast server and set client socket accordingly
685 if (CA_STATUS_OK == CAWiFiStopSecureUnicastServer())
687 CAWiFiSetSecureUnicastSocket(-1);
688 gSecureUnicastServerport = -1;
692 //Stop multicast server and set the state accordingly
693 if (CA_STATUS_OK != CAWiFiStopMulticastServer())
695 gIsMulticastServerStarted = false;
698 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
702 CAResult_t CAStopWIFI()
704 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
706 gStartUnicastServerRequested = false;
707 gStartMulticastServerRequested = false;
709 // Stop wifi network monitor
710 CAWiFiStopNetworkMonitor();
712 // Stop send queue thread
713 if (gSendQueueHandle && CA_FALSE == gSendQueueHandle->isStop)
715 CAQueueingThreadStop(gSendQueueHandle);
718 // Stop Unicast, Secured unicast and Multicast servers running
721 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
725 void CATerminateWIfI()
727 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
730 CADTLSSetAdapterCallbacks(NULL, NULL, DTLS_WIFI);
731 CAAdapterNetDtlsDeInit();
737 // Terminate network monitor
738 CAWiFiSetConnectionStateChangeCallback(NULL);
739 CAWiFiTerminateNetworkMonitor();
741 // Terminate message queue handler
742 CAWiFiDeinitializeQueueHandles();
744 CAWiFiSetPacketReceiveCallback(NULL);
746 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
750 void CAWiFiSendDataThread(void *threadData)
752 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
754 CAWiFiData *wifiData = (CAWiFiData *) threadData;
757 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "Invalid wifidata!");
761 if (NULL != wifiData->remoteEndpoint)
763 char *address = wifiData->remoteEndpoint->addressInfo.IP.ipAddress;
764 uint32_t port = wifiData->remoteEndpoint->addressInfo.IP.port;
767 if (!wifiData->remoteEndpoint->isSecured)
769 CAWiFiSendData(address, port, wifiData->data, wifiData->dataLen, false,
770 wifiData->remoteEndpoint->isSecured);
774 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt called!");
775 uint8_t cacheFalg = 0;
776 CAResult_t result = CAAdapterNetDtlsEncrypt(address, port, wifiData->data,
777 wifiData->dataLen, &cacheFalg, DTLS_WIFI);
779 if (CA_STATUS_OK != result)
781 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt failed!");
783 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt returned with cache[%d]",
787 CAWiFiSendData(address, port, wifiData->data, wifiData->dataLen, false,
788 wifiData->remoteEndpoint->isSecured);
793 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "Send Multicast Data is called");
794 CAWiFiSendData(CA_MULTICAST_IP, CA_MCAST_PORT, wifiData->data,
795 wifiData->dataLen, true, false);
799 CAFreeWiFiData(wifiData);
801 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
805 CAWiFiData *CACreateWiFiData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
808 CAWiFiData *wifiData = (CAWiFiData *) OICMalloc(sizeof(CAWiFiData));
811 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Memory allocation failed!");
815 wifiData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
816 wifiData->data = (void *)OICMalloc(dataLength);
817 if (NULL == wifiData->data)
819 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Memory allocation failed!");
820 CAFreeWiFiData(wifiData);
823 memcpy(wifiData->data, data, dataLength);
824 wifiData->dataLen = dataLength;
829 void CAFreeWiFiData(CAWiFiData *wifiData)
834 CAAdapterFreeRemoteEndpoint(wifiData->remoteEndpoint);
835 OICFree(wifiData->data);