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);
299 // Notify network change to CA
300 CAWiFiNotifyNetworkChange(address, port, status);
312 CAWiFiNotifyNetworkChange("", 0, status);
314 // Stop Unicast, Secured unicast and Multicast servers
318 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
322 uint32_t CAWiFiPacketSendCB(const char *ipAddress, const uint32_t port,
323 const void *data, const uint32_t dataLength)
325 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
327 uint32_t sentLength = CAWiFiSendData(ipAddress, port, data, dataLength, CA_FALSE, CA_TRUE);
329 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Successfully sent %d of encripted data!", sentLength);
335 void CAWiFiPacketReceivedCB(const char *ipAddress, const uint32_t port,
336 const void *data, const uint32_t dataLength, const CABool_t isSecured)
338 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
339 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Address: %s, port:%d, data:%s", ipAddress, port, data);
341 // CA is freeing this memory
342 CARemoteEndpoint_t *endPoint = CAAdapterCreateRemoteEndpoint(CA_WIFI, ipAddress, NULL);
343 if (NULL == endPoint)
345 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Out of memory");
348 endPoint->addressInfo.IP.port = port;
349 endPoint->isSecured = isSecured;
351 void *buf = OICMalloc(dataLength + 1);
354 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Out of memory");
355 CAAdapterFreeRemoteEndpoint(endPoint);
358 memcpy(buf, data, dataLength);
359 memset(buf + dataLength, 0, 1);
360 if (gNetworkPacketCallback)
362 gNetworkPacketCallback(endPoint, buf, dataLength);
364 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
367 CAResult_t CAInitializeWifi(CARegisterConnectivityCallback registerCallback,
368 CANetworkPacketReceivedCallback networkPacketCallback,
369 CANetworkChangeCallback netCallback, u_thread_pool_t handle)
371 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
372 VERIFY_NON_NULL(registerCallback, WIFI_ADAPTER_TAG, "registerCallback");
373 VERIFY_NON_NULL(networkPacketCallback, WIFI_ADAPTER_TAG, "networkPacketCallback");
374 VERIFY_NON_NULL(netCallback, WIFI_ADAPTER_TAG, "netCallback");
375 VERIFY_NON_NULL(handle, WIFI_ADAPTER_TAG, "thread pool handle");
377 gThreadPool = handle;
378 gNetworkChangeCallback = netCallback;
379 gNetworkPacketCallback = networkPacketCallback;
381 CAResult_t ret = CAWiFiInitializeNetworkMonitor(gThreadPool);
382 if (CA_STATUS_OK != ret)
384 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to initialize n/w monitor![%d]", ret);
387 CAWiFiSetConnectionStateChangeCallback(CAWiFiConnectionStateCB);
389 ret = CAWiFiInitializeServer(gThreadPool);
390 if (CA_STATUS_OK != ret)
392 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to initialize server![%d]", ret);
397 CAWiFiSetPacketReceiveCallback(CAWiFiPacketReceivedCB);
399 CAAdapterNetDtlsInit();
400 CADTLSSetAdapterCallbacks(CAWiFiPacketReceivedCB, CAWiFiPacketSendCB, DTLS_WIFI);
403 CAConnectivityHandler_t wifiHandler;
404 wifiHandler.startAdapter = CAStartWIFI;
405 wifiHandler.startListenServer = CAStartWIFIListeningServer;
406 wifiHandler.startDiscoverServer = CAStartWIFIDiscoveryServer;
407 wifiHandler.sendData = CASendWIFIUnicastData;
408 wifiHandler.sendDataToAll = CASendWIFIMulticastData;
409 wifiHandler.GetnetInfo = CAGetWIFIInterfaceInformation;
410 wifiHandler.readData = CAReadWIFIData;
411 wifiHandler.stopAdapter = CAStopWIFI;
412 wifiHandler.terminate = CATerminateWIfI;
413 registerCallback(wifiHandler, CA_WIFI);
415 if (CA_STATUS_OK != CAWiFiInitializeQueueHandles())
417 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Failed to Initialize Queue Handle");
419 return CA_STATUS_FAILED;
422 OIC_LOG(INFO, WIFI_ADAPTER_TAG, "IntializeWifi is Success");
426 CAResult_t CAStartWIFI()
428 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
430 // Start monitoring wifi network
431 CAResult_t ret = CAWiFiStartNetworkMonitor();
432 if (CA_STATUS_OK != ret)
434 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Failed to Start n/w monitor");
437 // Start send queue thread
438 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
440 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Failed to Start Send Data Thread");
441 return CA_STATUS_FAILED;
444 gStartUnicastServerRequested = true;
445 bool retVal = CAWiFiIsConnected();
448 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "WIFI is not Connected");
452 char *ifcName = NULL;
453 char *ifcAdrs = NULL;
454 ret = CAWiFiGetInterfaceInfo(&ifcName, &ifcAdrs);
455 if (CA_STATUS_OK != ret)
457 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Failed to get wifi interface info [%d]", ret);
461 int16_t unicastPort = CA_PORT;
462 int32_t serverFd = 0;
463 // Address is hardcoded as we are using Single Interface
464 ret = CAWiFiStartUnicastServer(ifcAdrs, &unicastPort, false, false, &serverFd);
465 if (CA_STATUS_OK == ret)
467 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Unicast server started on %d port", unicastPort);
468 CAWiFiSetUnicastSocket(serverFd);
469 gUnicastServerport = unicastPort;
473 // Address is hardcoded as we are using Single Interface
474 unicastPort = CA_SECURE_PORT;
475 ret = CAWiFiStartUnicastServer(ifcAdrs, &unicastPort, false, true, &serverFd);
476 if (CA_STATUS_OK == ret)
478 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Secure Unicast server started on %d port", unicastPort);
479 CAWiFiSetSecureUnicastSocket(serverFd);
480 gSecureUnicastServerport = unicastPort;
491 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
495 CAResult_t CAStartWIFIListeningServer()
497 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
499 CAResult_t ret = CA_STATUS_OK;
500 int16_t multicastPort = CA_MCAST_PORT;
502 if (gIsMulticastServerStarted == true)
504 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG,
505 "Failed to Start Multicast Server, Already Started!");
506 return CA_SERVER_STARTED_ALREADY;
509 gStartMulticastServerRequested = true;
510 bool retVal = CAWiFiIsConnected();
513 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG,
514 "Failed to Start Multicast Server, WIFI not Connected");
518 char *ifcName = NULL;
519 char *ifcAdrs = NULL;
520 ret = CAWiFiGetInterfaceInfo(&ifcName, &ifcAdrs);
521 if (CA_STATUS_OK != ret)
523 OIC_LOG_V(DEBUG, WIFI_ADAPTER_TAG, "Failed to get wifi interface info [%d]", ret);
527 int32_t multicastFd = 0;
528 ret = CAWiFiStartMulticastServer(ifcAdrs, CA_MULTICAST_IP, multicastPort, &multicastFd);
529 if (CA_STATUS_OK == ret)
531 OIC_LOG(INFO, WIFI_ADAPTER_TAG, "Multicast Server is Started Successfully");
532 gIsMulticastServerStarted = true;
543 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
547 CAResult_t CAStartWIFIDiscoveryServer()
549 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
550 return CAStartWIFIListeningServer();
553 uint32_t CASendWIFIUnicastData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
556 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
558 uint32_t dataSize = 0;
559 VERIFY_NON_NULL_RET(remoteEndpoint, WIFI_ADAPTER_TAG, "remoteEndpoint", dataSize);
560 VERIFY_NON_NULL_RET(data, WIFI_ADAPTER_TAG, "data", dataSize);
561 VERIFY_NON_NULL_RET(gSendQueueHandle, WIFI_ADAPTER_TAG, "sendQueueHandle", dataSize);
565 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Invalid Data Length");
569 // Create WifiData to add to queue
570 CAWiFiData *wifiData = CACreateWiFiData(remoteEndpoint, data, dataLength);
573 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to create wifidata!");
574 return CA_MEMORY_ALLOC_FAILED;
577 // Add message to send queue
578 CAQueueingThreadAddData(gSendQueueHandle, wifiData, sizeof(CAWiFiData));
580 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
584 uint32_t CASendWIFIMulticastData(void *data, uint32_t dataLength)
586 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
588 uint32_t dataSize = 0;
589 VERIFY_NON_NULL_RET(data, WIFI_ADAPTER_TAG, "data", dataSize);
590 VERIFY_NON_NULL_RET(gSendQueueHandle, WIFI_ADAPTER_TAG, "sendQueueHandle", dataSize);
594 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Invalid Data Length");
598 // Create WifiData to add to queue
599 CAWiFiData *wifiData = CACreateWiFiData(NULL, data, dataLength);
602 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to create wifidata!");
603 return CA_MEMORY_ALLOC_FAILED;
606 // Add message to send queue
607 CAQueueingThreadAddData(gSendQueueHandle, wifiData, sizeof(CAWiFiData));
609 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
613 CAResult_t CAGetWIFIInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
615 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
616 VERIFY_NON_NULL(info, WIFI_ADAPTER_TAG, "info");
617 VERIFY_NON_NULL(size, WIFI_ADAPTER_TAG, "size");
619 CALocalConnectivity_t *netInfo = NULL;
620 bool retVal = CAWiFiIsConnected();
623 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG,
624 "Failed to get interface address, WIFI not Connected", CA_ADAPTER_NOT_ENABLED);
625 return CA_ADAPTER_NOT_ENABLED;
628 int32_t netInfoSize = 1;
630 if (gSecureUnicastServerport)
635 netInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * netInfoSize);
636 VERIFY_NON_NULL_RET(netInfo, WIFI_ADAPTER_TAG, "malloc failed", CA_MEMORY_ALLOC_FAILED);
637 memset(netInfo, 0, sizeof(CALocalConnectivity_t) * netInfoSize);
639 char *ipAddress = NULL;
640 char *ifcName = NULL;
641 CAResult_t ret = CAWiFiGetInterfaceInfo(&ifcName, &ipAddress);
642 if (CA_STATUS_OK != ret || NULL == ipAddress)
644 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to get interface info [%d]", ret);
661 // Create local endpoint using util function
662 CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_WIFI, ipAddress);
663 if (NULL == endpoint)
665 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "Failed to create Local Endpoint!",
666 CA_MEMORY_ALLOC_FAILED);
679 return CA_MEMORY_ALLOC_FAILED;
682 // copy unciast server information
683 endpoint->isSecured = CA_FALSE;
684 endpoint->addressInfo.IP.port = gUnicastServerport;
685 memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
688 // copy sevure unicast server information
689 if (gSecureUnicastServerport)
691 endpoint->isSecured = CA_TRUE;
692 endpoint->addressInfo.IP.port = gSecureUnicastServerport;
693 memcpy(&netInfo[1], endpoint, sizeof(CALocalConnectivity_t));
707 CAAdapterFreeLocalEndpoint(endpoint);
709 OIC_LOG(INFO, WIFI_ADAPTER_TAG, "GetWIFIInterfaceInformation success");
710 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
714 CAResult_t CAReadWIFIData()
716 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
717 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
721 CAResult_t CAWiFiStopServers()
723 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
725 // Stop unicast server and set client socket accordingly
726 if (CA_STATUS_OK == CAWiFiStopUnicastServer())
728 CAWiFiSetUnicastSocket(-1);
729 gUnicastServerport = -1;
732 // Stop secure unicast server and set client socket accordingly
733 if (CA_STATUS_OK == CAWiFiStopSecureUnicastServer())
735 CAWiFiSetSecureUnicastSocket(-1);
736 gSecureUnicastServerport = -1;
740 //Stop multicast server and set the state accordingly
741 if (CA_STATUS_OK != CAWiFiStopMulticastServer())
743 gIsMulticastServerStarted = false;
746 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
750 CAResult_t CAStopWIFI()
752 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
754 gStartUnicastServerRequested = false;
755 gStartMulticastServerRequested = false;
757 // Stop wifi network monitor
758 CAWiFiStopNetworkMonitor();
760 // Stop send queue thread
761 if (gSendQueueHandle && CA_FALSE == gSendQueueHandle->isStop)
763 CAQueueingThreadStop(gSendQueueHandle);
766 // Stop Unicast, Secured unicast and Multicast servers running
769 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
773 void CATerminateWIfI()
775 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
778 CADTLSSetAdapterCallbacks(NULL, NULL, DTLS_WIFI);
779 CAAdapterNetDtlsDeInit();
785 // Terminate wifi server
786 CAWiFiTerminateServer();
788 // Terminate network monitor
789 CAWiFiSetConnectionStateChangeCallback(NULL);
790 CAWiFiTerminateNetworkMonitor();
792 // Terminate message queue handler
793 CAWiFiDeinitializeQueueHandles();
795 CAWiFiSetPacketReceiveCallback(NULL);
797 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
801 void CAWiFiSendDataThread(void *threadData)
803 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "IN");
805 CAWiFiData *wifiData = (CAWiFiData *) threadData;
808 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "Invalid wifidata!");
812 if (NULL != wifiData->remoteEndpoint)
814 char *address = wifiData->remoteEndpoint->addressInfo.IP.ipAddress;
815 uint32_t port = wifiData->remoteEndpoint->addressInfo.IP.port;
818 if (!wifiData->remoteEndpoint->isSecured)
820 CAWiFiSendData(address, port, wifiData->data, wifiData->dataLen, false,
821 wifiData->remoteEndpoint->isSecured);
825 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt called!");
826 uint8_t cacheFalg = 0;
827 CAResult_t result = CAAdapterNetDtlsEncrypt(address, port, wifiData->data,
828 wifiData->dataLen, &cacheFalg, DTLS_WIFI);
830 if (CA_STATUS_OK != result)
832 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt failed!");
834 OIC_LOG_V(ERROR, WIFI_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt returned with cache[%d]",
838 CAWiFiSendData(address, port, wifiData->data, wifiData->dataLen, false,
839 wifiData->remoteEndpoint->isSecured);
844 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "Send Multicast Data is called");
845 CAWiFiSendData(CA_MULTICAST_IP, CA_MCAST_PORT, wifiData->data,
846 wifiData->dataLen, true, false);
850 CAFreeWiFiData(wifiData);
852 OIC_LOG(DEBUG, WIFI_ADAPTER_TAG, "OUT");
856 CAWiFiData *CACreateWiFiData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
859 CAWiFiData *wifiData = (CAWiFiData *) OICMalloc(sizeof(CAWiFiData));
862 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Memory allocation failed!");
866 wifiData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
867 wifiData->data = (void *)OICMalloc(dataLength);
868 if (NULL == wifiData->data)
870 OIC_LOG(ERROR, WIFI_ADAPTER_TAG, "Memory allocation failed!");
871 CAFreeWiFiData(wifiData);
874 memcpy(wifiData->data, data, dataLength);
875 wifiData->dataLen = dataLength;
880 void CAFreeWiFiData(CAWiFiData *wifiData)
885 CAAdapterFreeRemoteEndpoint(wifiData->remoteEndpoint);
886 OICFree(wifiData->data);