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 "caethernetadapter.h"
27 #include "caethernetinterface.h"
28 #include "caqueueingthread.h"
29 #include "caadapterutils.h"
31 #include "caadapternetdtls.h"
35 #include "oic_malloc.h"
38 * @def ETHERNET_ADAPTER_TAG
39 * @brief Logging tag for module name
41 #define ETHERNET_ADAPTER_TAG "ETHERNET_ADAP"
45 * @brief Port to listen for incoming data
51 * @brief Secured (unicast) port number as defined in COAP Specification, RFC-7252.
53 #define CA_SECURE_PORT 5684
57 * @brief Multicast port number as defined in COAP Specification, RFC-7252.
59 #define CA_MCAST_PORT 5683
62 * @def CA_MULTICAST_IP
63 * @brief Multicast IP Address as defined in COAP Specification, RFC-7252.
65 #define CA_MULTICAST_IP "224.0.1.187"
69 CARemoteEndpoint_t *remoteEndpoint;
75 * @var g_networkPacketCallback
76 * @brief Network Packet Received Callback to CA
78 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
81 * @var g_networkChangeCallback
82 * @brief Network Changed Callback to CA
84 static CANetworkChangeCallback g_networkChangeCallback = NULL;
87 * @var g_isMulticastServerStarted
88 * @brief Flag to check if multicast server is started
90 static bool g_isMulticastServerStarted = false;
93 * @var gIsStartServerCalled
94 * @brief Flag to check if server start requested by CA.
96 static bool g_startUnicastServerRequested = false;
99 * @var g_unicastServerport
100 * @brief port number on which unicast server is running.
102 static uint16_t g_unicastServerport = 0;
106 * @var g_secureUnicastServerport
107 * @brief port number on which secure unicast server is running.
109 static uint16_t g_secureUnicastServerport = 0;
113 * @var g_startMulticastServerRequested
114 * @brief Flag to check if server start requested by CA.
116 static bool g_startMulticastServerRequested = false;
119 * @var g_sendQueueHandle
120 * @brief Queue handle for Send Data
122 static CAQueueingThread_t *g_sendQueueHandle = NULL;
126 * @brief ThreadPool for storing ca_thread_pool_t handle passed from CA
128 static ca_thread_pool_t g_threadPool = NULL;
130 static CAResult_t CAEthernetInitializeQueueHandles();
131 static void CAEthernetDeinitializeQueueHandles();
132 static void CAEthernetNotifyNetworkChange(const char *address, const uint16_t port,
133 const CANetworkStatus_t status);
134 static void CAEthernetConnectionStateCB(const char *ipAddress,
135 const CANetworkStatus_t status);
136 static void CAEthernetPacketReceivedCB(const char *ipAddress, const uint16_t port,
137 const void *data, const uint32_t dataLength, const bool isSecured);
139 static uint32_t CAEthernetPacketSendCB(const char *ipAddress, const uint16_t port,
140 const void *data, const uint32_t dataLength);
143 static CAResult_t CAEthernetStopServers();
144 static void CAEthernetSendDataThread(void *threadData);
145 static CAEthernetData *CACreateEthernetData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
146 uint32_t dataLength);
147 void CAFreeEthernetData(CAEthernetData *EthernetData);
149 static void CADataDestroyer(void *data, uint32_t size);
151 CAResult_t CAEthernetInitializeQueueHandles()
153 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
155 // Check if the message queue is already initialized
156 if (g_sendQueueHandle)
158 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "send queue handle is already initialized!");
162 // Create send message queue
163 g_sendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
164 if (!g_sendQueueHandle)
166 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
167 return CA_MEMORY_ALLOC_FAILED;
170 if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle, g_threadPool,
171 CAEthernetSendDataThread, CADataDestroyer))
173 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Initialize send queue thread");
174 OICFree(g_sendQueueHandle);
175 g_sendQueueHandle = NULL;
176 return CA_STATUS_FAILED;
179 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
183 void CAEthernetDeinitializeQueueHandles()
185 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
187 CAQueueingThreadDestroy(g_sendQueueHandle);
188 OICFree(g_sendQueueHandle);
189 g_sendQueueHandle = NULL;
191 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
194 void CAEthernetNotifyNetworkChange(const char *address, const uint16_t port,
195 const CANetworkStatus_t status)
197 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
199 CALocalConnectivity_t *localEndpoint = CAAdapterCreateLocalEndpoint(CA_ETHERNET, address);
202 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Out of memory");
206 if (NULL != g_networkChangeCallback)
208 g_networkChangeCallback(localEndpoint, status);
211 CAAdapterFreeLocalEndpoint(localEndpoint);
214 void CAEthernetConnectionStateCB(const char *ipAddress,
215 const CANetworkStatus_t status)
217 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
219 CAResult_t ret = CA_STATUS_FAILED;
222 * If Ethernet is connected, then get the latest IP from the Ethernet Interface
223 * and start unicast and multicast servers if requested earlier
225 if (CA_INTERFACE_UP == status)
227 uint16_t port = CA_PORT;
228 int32_t serverFd = -1;
230 // Start Unicast server if requested earlier
231 if (g_startUnicastServerRequested)
233 ret = CAEthernetStartUnicastServer("0.0.0.0", &port, false, false, &serverFd);
234 if (CA_STATUS_OK == ret)
236 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Unicast server started on %d port", port);
237 CAEthernetSetUnicastSocket(serverFd);
238 g_unicastServerport = port;
242 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to start Unicast server [%d]", ret);
246 port = CA_SECURE_PORT;
247 ret = CAEthernetStartUnicastServer("0.0.0.0", &port, false, true, &serverFd);
248 if (CA_STATUS_OK == ret)
250 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Secure Unicast server started on %d", port);
251 CAEthernetSetSecureUnicastSocket(serverFd);
252 g_secureUnicastServerport = port;
256 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to start secure Unicast server [%d]",
262 // Start Multicast server if requested earlier
263 if (g_startMulticastServerRequested)
265 uint16_t multicastPort = CA_MCAST_PORT;
266 int32_t multicastFd = 0;
267 ret = CAEthernetStartMulticastServer("0.0.0.0", CA_MULTICAST_IP, multicastPort,
269 if (CA_STATUS_OK == ret)
271 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Multicast server started on %d port",
273 g_isMulticastServerStarted = true;
277 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to start Multicast server [%d]", ret);
281 char *address = NULL;
282 char *ifcName = NULL;
283 ret = CAEthernetGetInterfaceInfo(&ifcName, &address);
284 if (CA_STATUS_OK != ret || NULL == address)
286 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to get interface info [%d]", ret);
292 // Notify network change to CA
293 CAEthernetNotifyNetworkChange(address, port, status);
299 CAEthernetNotifyNetworkChange("", 0, status);
301 // Stop Unicast, Secured unicast and Multicast servers
302 CAEthernetStopServers();
305 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
309 uint32_t CAEthernetPacketSendCB(const char *ipAddress, const uint16_t port,
310 const void *data, const uint32_t dataLength)
312 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
314 uint32_t sentLength = CAEthernetSendData(ipAddress, port, data, dataLength, false, true);
316 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Successfully sent %d of encrypted data!", sentLength);
322 void CAEthernetPacketReceivedCB(const char *ipAddress, const uint16_t port,
323 const void *data, const uint32_t dataLength, const bool isSecured)
325 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
326 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Address: %s, port:%d, data:%s", ipAddress, port, data);
328 // CA is freeing this memory
329 CARemoteEndpoint_t *endPoint = CAAdapterCreateRemoteEndpoint(CA_ETHERNET, ipAddress, NULL);
330 if (NULL == endPoint)
332 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Out of memory");
335 endPoint->addressInfo.IP.port = port;
336 endPoint->isSecured = isSecured;
338 void *buf = OICMalloc(dataLength + 1);
341 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Out of memory");
342 CAAdapterFreeRemoteEndpoint(endPoint);
345 memcpy(buf, data, dataLength);
346 memset(buf + dataLength, 0, 1);
347 if (g_networkPacketCallback)
349 g_networkPacketCallback(endPoint, buf, dataLength);
354 CAAdapterFreeRemoteEndpoint(endPoint);
357 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
360 CAResult_t CAInitializeEthernet(CARegisterConnectivityCallback registerCallback,
361 CANetworkPacketReceivedCallback networkPacketCallback,
362 CANetworkChangeCallback netCallback, ca_thread_pool_t handle)
364 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
365 VERIFY_NON_NULL(registerCallback, ETHERNET_ADAPTER_TAG, "registerCallback");
366 VERIFY_NON_NULL(networkPacketCallback, ETHERNET_ADAPTER_TAG, "networkPacketCallback");
367 VERIFY_NON_NULL(netCallback, ETHERNET_ADAPTER_TAG, "netCallback");
368 VERIFY_NON_NULL(handle, ETHERNET_ADAPTER_TAG, "thread pool handle");
370 g_threadPool = handle;
371 g_networkChangeCallback = netCallback;
372 g_networkPacketCallback = networkPacketCallback;
374 CAResult_t ret = CAEthernetInitializeNetworkMonitor(g_threadPool);
375 if (CA_STATUS_OK != ret)
377 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to initialize n/w monitor![%d]", ret);
380 CAEthernetSetConnectionStateChangeCallback(CAEthernetConnectionStateCB);
382 ret = CAEthernetInitializeServer(g_threadPool);
383 if (CA_STATUS_OK != ret)
385 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to initialize server![%d]", ret);
386 CATerminateEthernet();
390 CAEthernetSetPacketReceiveCallback(CAEthernetPacketReceivedCB);
392 CAAdapterNetDtlsInit();
394 CADTLSSetAdapterCallbacks(CAEthernetPacketReceivedCB, CAEthernetPacketSendCB, DTLS_ETHERNET);
397 CAConnectivityHandler_t ethernetHandler;
398 ethernetHandler.startAdapter = CAStartEthernet;
399 ethernetHandler.startListenServer = CAStartEthernetListeningServer;
400 ethernetHandler.startDiscoveryServer = CAStartEthernetDiscoveryServer;
401 ethernetHandler.sendData = CASendEthernetUnicastData;
402 ethernetHandler.sendDataToAll = CASendEthernetMulticastData;
403 ethernetHandler.GetnetInfo = CAGetEthernetInterfaceInformation;
404 ethernetHandler.readData = CAReadEthernetData;
405 ethernetHandler.stopAdapter = CAStopEthernet;
406 ethernetHandler.terminate = CATerminateEthernet;
407 registerCallback(ethernetHandler, CA_ETHERNET);
409 if (CA_STATUS_OK != CAEthernetInitializeQueueHandles())
411 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Initialize Queue Handle");
412 CATerminateEthernet();
413 return CA_STATUS_FAILED;
416 OIC_LOG(INFO, ETHERNET_ADAPTER_TAG, "IntializeEthernet is Success");
420 CAResult_t CAStartEthernet()
422 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
424 // Start monitoring Ethernet network
425 CAResult_t ret = CAEthernetStartNetworkMonitor();
426 if (CA_STATUS_OK != ret)
428 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Start n/w monitor");
432 // Start send queue thread
433 if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
435 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Start Send Data Thread");
437 return CA_STATUS_FAILED;
440 g_startUnicastServerRequested = true;
441 bool retVal = CAEthernetIsConnected();
444 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Ethernet is not Connected");
448 char *ifcName = NULL;
449 char *ifcAdrs = NULL;
450 ret = CAEthernetGetInterfaceInfo(&ifcName, &ifcAdrs);
451 if (CA_STATUS_OK != ret)
453 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Failed to get ethernet interface info [%d]", ret);
456 uint16_t unicastPort = CA_PORT;
457 int32_t serverFd = 0;
458 // Address is hardcoded as we are using Single Interface
459 ret = CAEthernetStartUnicastServer(ifcAdrs, &unicastPort, false, false, &serverFd);
460 if (CA_STATUS_OK == ret)
462 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Unicast server started on %d port", unicastPort);
463 CAEthernetSetUnicastSocket(serverFd);
464 g_unicastServerport = unicastPort;
468 // Address is hardcoded as we are using Single Interface
469 unicastPort = CA_SECURE_PORT;
470 ret = CAEthernetStartUnicastServer(ifcAdrs, &unicastPort, false, true, &serverFd);
472 if (CA_STATUS_OK == ret)
474 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Secure Unicast server started on %d port", unicastPort);
475 CAEthernetSetSecureUnicastSocket(serverFd);
476 g_secureUnicastServerport = unicastPort;
483 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
487 CAResult_t CAStartEthernetListeningServer()
489 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
491 CAResult_t ret = CA_STATUS_OK;
492 uint16_t multicastPort = CA_MCAST_PORT;
494 if (g_isMulticastServerStarted == true)
496 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
497 "Failed to Start Multicast Server, Already Started!");
498 return CA_SERVER_STARTED_ALREADY;
501 g_startMulticastServerRequested = true;
502 bool retVal = CAEthernetIsConnected();
505 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
506 "Failed to Start Multicast Server, Ethernet not Connected");
510 char *ifcName = NULL;
511 char *ifcAdrs = NULL;
512 ret = CAEthernetGetInterfaceInfo(&ifcName, &ifcAdrs);
513 if (CA_STATUS_OK != ret)
515 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Failed to get ethernet interface info [%d]", ret);
518 int32_t multicastFd = 0;
519 ret = CAEthernetStartMulticastServer(ifcAdrs, CA_MULTICAST_IP, multicastPort, &multicastFd);
520 if (CA_STATUS_OK == ret)
522 OIC_LOG(INFO, ETHERNET_ADAPTER_TAG, "Multicast Server is Started Successfully");
523 g_isMulticastServerStarted = true;
529 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
533 CAResult_t CAStartEthernetDiscoveryServer()
535 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
536 return CAStartEthernetListeningServer();
539 int32_t CASendEthernetUnicastData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
542 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
544 VERIFY_NON_NULL_RET(remoteEndpoint, ETHERNET_ADAPTER_TAG, "remoteEndpoint", -1);
545 VERIFY_NON_NULL_RET(data, ETHERNET_ADAPTER_TAG, "data", -1);
546 VERIFY_NON_NULL_RET(g_sendQueueHandle, ETHERNET_ADAPTER_TAG, "sendQueueHandle", -1);
550 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Invalid Data Length");
554 // Create EthernetData to add to queue
555 CAEthernetData *ethernetData = CACreateEthernetData(remoteEndpoint, data, dataLength);
558 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create ethernetData!");
563 // Add message to send queue
564 CAQueueingThreadAddData(g_sendQueueHandle, ethernetData, sizeof(CAEthernetData));
566 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
571 int32_t CASendEthernetMulticastData(const void *data, uint32_t dataLength)
573 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
575 VERIFY_NON_NULL_RET(data, ETHERNET_ADAPTER_TAG, "data", -1);
576 VERIFY_NON_NULL_RET(g_sendQueueHandle, ETHERNET_ADAPTER_TAG, "sendQueueHandle", -1);
580 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Invalid Data Length");
584 // Create EthernetData to add to queue
585 CAEthernetData *EthernetData = CACreateEthernetData(NULL, data, dataLength);
588 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create ethernetData!");
593 // Add message to send queue
594 CAQueueingThreadAddData(g_sendQueueHandle, EthernetData, sizeof(CAEthernetData));
596 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
601 CAResult_t CAGetEthernetInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
603 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
605 VERIFY_NON_NULL(info, ETHERNET_ADAPTER_TAG, "info");
606 VERIFY_NON_NULL(size, ETHERNET_ADAPTER_TAG, "size");
608 if (info == NULL || size == NULL)
610 return CA_STATUS_INVALID_PARAM;
613 CALocalConnectivity_t *netInfo = NULL;
614 bool retVal = CAEthernetIsConnected();
617 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
618 "Failed to get interface address, Ethernet not Connected",
619 CA_ADAPTER_NOT_ENABLED);
620 return CA_ADAPTER_NOT_ENABLED;
623 int32_t netInfoSize = 1;
625 if (g_secureUnicastServerport)
630 netInfo = (CALocalConnectivity_t *) OICCalloc(netInfoSize, sizeof(CALocalConnectivity_t));
631 VERIFY_NON_NULL_RET(netInfo, ETHERNET_ADAPTER_TAG, "malloc failed", CA_MEMORY_ALLOC_FAILED);
633 char *ipAddress = NULL;
634 char *ifcName = NULL;
635 CAResult_t ret = CAEthernetGetInterfaceInfo(&ifcName, &ipAddress);
636 if (CA_STATUS_OK != ret || NULL == ipAddress)
638 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to get interface info [%d]", ret);
646 // Create local endpoint using util function
647 CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_ETHERNET, ipAddress);
648 if (NULL == endpoint)
650 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create Local Endpoint!",
651 CA_MEMORY_ALLOC_FAILED);
655 return CA_MEMORY_ALLOC_FAILED;
658 // copy unicast server information
659 endpoint->isSecured = false;
660 endpoint->addressInfo.IP.port = g_unicastServerport;
661 memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
664 // copy secure unicast server information
665 if (g_secureUnicastServerport)
667 endpoint->isSecured = true;
668 endpoint->addressInfo.IP.port = g_secureUnicastServerport;
669 memcpy(&netInfo[1], endpoint, sizeof(CALocalConnectivity_t));
677 CAAdapterFreeLocalEndpoint(endpoint);
679 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
683 CAResult_t CAReadEthernetData()
685 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
686 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
690 CAResult_t CAEthernetStopServers()
692 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
694 // Stop unicast server and set client socket accordingly
695 if (CA_STATUS_OK == CAEthernetStopUnicastServer())
697 CAEthernetSetUnicastSocket(-1);
698 g_unicastServerport = -1;
702 // Stop secure unicast server and set client socket accordingly
703 if (CA_STATUS_OK == CAEthernetStopSecureUnicastServer())
705 CAEthernetSetSecureUnicastSocket(-1);
706 g_secureUnicastServerport = -1;
710 //Stop multicast server and set the state accordingly
711 if (CA_STATUS_OK == CAEthernetStopMulticastServer())
713 g_isMulticastServerStarted = false;
716 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
720 CAResult_t CAStopEthernet()
722 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
724 g_startUnicastServerRequested = false;
725 g_startMulticastServerRequested = false;
727 // Stop ethernet network monitor
728 CAEthernetStopNetworkMonitor();
730 // Stop send queue thread
731 if (g_sendQueueHandle && false == g_sendQueueHandle->isStop)
733 CAQueueingThreadStop(g_sendQueueHandle);
736 // Stop Unicast, Secured unicast and Multicast servers running
737 CAEthernetStopServers();
739 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
743 void CATerminateEthernet()
745 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
748 CADTLSSetAdapterCallbacks(NULL, NULL, DTLS_ETHERNET);
749 CAAdapterNetDtlsDeInit();
752 // Stop ethernet adapter
755 // Terminate Ethernet server
756 CAEthernetTerminateServer();
758 // Terminate network monitor
759 CAEthernetSetConnectionStateChangeCallback(NULL);
760 CAEthernetTerminateNetworkMonitor();
762 // Terminate message queue handler
763 CAEthernetDeinitializeQueueHandles();
764 CAEthernetSetPacketReceiveCallback(NULL);
766 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
770 void CAEthernetSendDataThread(void *threadData)
772 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
774 CAEthernetData *ethernetData = (CAEthernetData *) threadData;
777 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Invalid ethernet data!");
781 if (NULL != ethernetData->remoteEndpoint)
783 char *address = ethernetData->remoteEndpoint->addressInfo.IP.ipAddress;
784 uint16_t port = ethernetData->remoteEndpoint->addressInfo.IP.port;
787 if (!ethernetData->remoteEndpoint->isSecured)
789 CAEthernetSendData(address, port, ethernetData->data, ethernetData->dataLen, false,
790 ethernetData->remoteEndpoint->isSecured);
794 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt called!");
795 uint8_t cacheFlag = 0;
796 CAResult_t result = CAAdapterNetDtlsEncrypt(address, port, ethernetData->data,
797 ethernetData->dataLen, &cacheFlag, DTLS_ETHERNET);
799 if (CA_STATUS_OK != result)
801 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt failed!");
803 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
804 "CAAdapterNetDtlsEncrypt returned with cache[%d]",cacheFlag);
807 CAEthernetSendData(address, port, ethernetData->data, ethernetData->dataLen, false,
808 ethernetData->remoteEndpoint->isSecured);
813 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Send Multicast Data is called");
814 CAEthernetSendData(CA_MULTICAST_IP, CA_MCAST_PORT, ethernetData->data,
815 ethernetData->dataLen, true, false);
818 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
821 CAEthernetData *CACreateEthernetData(const CARemoteEndpoint_t *remoteEndpoint, const void *data,
824 CAEthernetData *ethernetData = (CAEthernetData *) OICMalloc(sizeof(CAEthernetData));
827 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
831 ethernetData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
832 ethernetData->data = (void *)OICMalloc(dataLength);
833 if (NULL == ethernetData->data)
835 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
836 CAFreeEthernetData(ethernetData);
840 memcpy(ethernetData->data, data, dataLength);
841 ethernetData->dataLen = dataLength;
846 void CAFreeEthernetData(CAEthernetData *ethernetData)
853 CAAdapterFreeRemoteEndpoint(ethernetData->remoteEndpoint);
854 OICFree(ethernetData->data);
855 OICFree(ethernetData);
858 void CADataDestroyer(void *data, uint32_t size)
860 CAEthernetData *etdata = (CAEthernetData *) data;
862 CAFreeEthernetData(etdata);