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 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 gEthernetNetworkChangeCallback
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 CAEthernetInitializeQueueHandles();
132 static void CAEthernetDeinitializeQueueHandles();
133 static void CAEthernetNotifyNetworkChange(const char *address, const int16_t port,
134 const CANetworkStatus_t status);
135 static void CAEthernetConnectionStateCB(const char *ipAddress,
136 const CANetworkStatus_t status);
137 static void CAEthernetPacketReceivedCB(const char *ipAddress, const uint32_t port,
138 const void *data, const uint32_t dataLength, const CABool_t isSecured);
140 static uint32_t CAEthernetPacketSendCB(const char *ipAddress, const uint32_t port,
141 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, void *data,
146 uint32_t dataLength);
147 void CAFreeEthernetData(CAEthernetData *ethernetData);
150 CAResult_t CAEthernetInitializeQueueHandles()
152 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
154 // Check if the message queue is already initialized
155 if (gSendQueueHandle)
157 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Already queue is initialized!");
161 // Create send message queue
162 gSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
163 if (!gSendQueueHandle)
165 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
166 return CA_MEMORY_ALLOC_FAILED;
169 if (CA_STATUS_OK != CAQueueingThreadInitialize(gSendQueueHandle, gThreadPool,
170 CAEthernetSendDataThread))
172 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Initialize send queue thread");
173 OICFree(gSendQueueHandle);
174 gSendQueueHandle = NULL;
175 return CA_STATUS_FAILED;
178 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
182 void CAEthernetDeinitializeQueueHandles()
184 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
186 CAQueueingThreadDestroy(gSendQueueHandle);
187 OICFree(gSendQueueHandle);
188 gSendQueueHandle = NULL;
190 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
193 void CAEthernetNotifyNetworkChange(const char *address, const int16_t port,
194 const CANetworkStatus_t status)
196 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
198 CALocalConnectivity_t *localEndpoint = CAAdapterCreateLocalEndpoint(CA_ETHERNET, address);
201 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Out of memory");
204 localEndpoint->addressInfo.IP.port = port;
206 if (NULL != gNetworkChangeCallback)
208 gNetworkChangeCallback(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 int16_t port = CA_PORT;
228 int32_t serverFd = -1;
230 // Start Unicast server if requested earlier
231 if (gStartUnicastServerRequested)
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 gUnicastServerport = 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 gSecureUnicastServerport = 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 (gStartMulticastServerRequested)
265 int16_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 gIsMulticastServerStarted = 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 uint32_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, CA_FALSE, CA_TRUE);
316 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Successfully sent %d of encripted data!", sentLength);
322 void CAEthernetPacketReceivedCB(const char *ipAddress, const uint32_t port,
323 const void *data, const uint32_t dataLength, const CABool_t 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 (gNetworkPacketCallback)
349 gNetworkPacketCallback(endPoint, buf, dataLength);
352 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
355 CAResult_t CAInitializeEthernet(CARegisterConnectivityCallback registerCallback,
356 CANetworkPacketReceivedCallback networkPacketCallback,
357 CANetworkChangeCallback netCallback, u_thread_pool_t handle)
359 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
360 VERIFY_NON_NULL(registerCallback, ETHERNET_ADAPTER_TAG, "registerCallback");
361 VERIFY_NON_NULL(networkPacketCallback, ETHERNET_ADAPTER_TAG, "networkPacketCallback");
362 VERIFY_NON_NULL(netCallback, ETHERNET_ADAPTER_TAG, "netCallback");
363 VERIFY_NON_NULL(handle, ETHERNET_ADAPTER_TAG, "thread pool handle");
365 gThreadPool = handle;
366 gNetworkChangeCallback = netCallback;
367 gNetworkPacketCallback = networkPacketCallback;
369 CAResult_t ret = CAEthernetInitializeNetworkMonitor(gThreadPool);
370 if (CA_STATUS_OK != ret)
372 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to initialize n/w monitor![%d]", ret);
375 CAEthernetSetConnectionStateChangeCallback(CAEthernetConnectionStateCB);
377 ret = CAEthernetInitializeServer(gThreadPool);
378 if (CA_STATUS_OK != ret)
380 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to initialize server![%d]", ret);
381 CATerminateEthernet();
385 CAEthernetSetPacketReceiveCallback(CAEthernetPacketReceivedCB);
387 CAAdapterNetDtlsInit();
388 CADTLSSetAdapterCallbacks(CAEthernetPacketReceivedCB, CAEthernetPacketSendCB, DTLS_ETHERNET);
391 CAConnectivityHandler_t ethernetHandler;
392 ethernetHandler.startAdapter = CAStartEthernet;
393 ethernetHandler.startListenServer = CAStartEthernetListeningServer;
394 ethernetHandler.startDiscoverServer = CAStartEthernetDiscoveryServer;
395 ethernetHandler.sendData = CASendEthernetUnicastData;
396 ethernetHandler.sendDataToAll = CASendEthernetMulticastData;
397 ethernetHandler.GetnetInfo = CAGetEthernetInterfaceInformation;
398 ethernetHandler.readData = CAReadEthernetData;
399 ethernetHandler.stopAdapter = CAStopEthernet;
400 ethernetHandler.terminate = CATerminateEthernet;
401 registerCallback(ethernetHandler, CA_ETHERNET);
403 if (CA_STATUS_OK != CAEthernetInitializeQueueHandles())
405 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Initialize Queue Handle");
406 CATerminateEthernet();
407 return CA_STATUS_FAILED;
410 OIC_LOG(INFO, ETHERNET_ADAPTER_TAG, "IntializeEthernet is Success");
414 CAResult_t CAStartEthernet()
416 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
418 // Start monitoring Ethernet network
419 CAResult_t ret = CAEthernetStartNetworkMonitor();
420 if (CA_STATUS_OK != ret)
422 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Start n/w monitor");
426 // Start send queue thread
427 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
429 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Start Send Data Thread");
431 return CA_STATUS_FAILED;
434 gStartUnicastServerRequested = true;
435 bool retVal = CAEthernetIsConnected();
438 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Ethernet is not Connected");
444 ret = CAEthernetGetInterfaceInfo(&ifcName, &ifcAdrs);
445 if(CA_STATUS_OK != ret)
447 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Failed to get ethernet interface info [%d]", ret);
450 int16_t unicastPort = CA_PORT;
451 int32_t serverFd = 0;
452 // Address is hardcoded as we are using Single Interface
453 ret = CAEthernetStartUnicastServer(ifcAdrs, &unicastPort, false, false, &serverFd);
454 if (CA_STATUS_OK == ret)
456 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Unicast server started on %d port", unicastPort);
457 CAEthernetSetUnicastSocket(serverFd);
458 gUnicastServerport = unicastPort;
462 // Address is hardcoded as we are using Single Interface
463 unicastPort = CA_SECURE_PORT;
464 ret = CAEthernetStartUnicastServer(ifcAdrs, &unicastPort, false, true, &serverFd);
465 if (CA_STATUS_OK == ret)
467 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Secure Unicast server started on %d port", unicastPort);
468 CAEthernetSetSecureUnicastSocket(serverFd);
469 gSecureUnicastServerport = unicastPort;
474 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
478 CAResult_t CAStartEthernetListeningServer()
480 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
482 CAResult_t ret = CA_STATUS_OK;
483 int16_t multicastPort = CA_MCAST_PORT;
485 if (gIsMulticastServerStarted == true)
487 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
488 "Failed to Start Multicast Server, Already Started!");
489 return CA_SERVER_STARTED_ALREADY;
492 gStartMulticastServerRequested = true;
493 bool retVal = CAEthernetIsConnected();
496 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
497 "Failed to Start Multicast Server, Ethernet not Connected");
503 ret = CAEthernetGetInterfaceInfo(&ifcName, &ifcAdrs);
504 if(CA_STATUS_OK != ret)
506 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Failed to get ethernet interface info [%d]", ret);
509 int32_t multicastFd = 0;
510 ret = CAEthernetStartMulticastServer(ifcAdrs, CA_MULTICAST_IP, multicastPort, &multicastFd);
511 if (CA_STATUS_OK == ret)
513 OIC_LOG(INFO, ETHERNET_ADAPTER_TAG, "Multicast Server is Started Successfully");
514 gIsMulticastServerStarted = true;
519 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
523 CAResult_t CAStartEthernetDiscoveryServer()
525 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
526 return CAStartEthernetListeningServer();
529 uint32_t CASendEthernetUnicastData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
532 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
534 uint32_t dataSize = 0;
535 VERIFY_NON_NULL_RET(remoteEndpoint, ETHERNET_ADAPTER_TAG, "remoteEndpoint", dataSize);
536 VERIFY_NON_NULL_RET(data, ETHERNET_ADAPTER_TAG, "data", dataSize);
537 VERIFY_NON_NULL_RET(gSendQueueHandle, ETHERNET_ADAPTER_TAG, "sendQueueHandle", dataSize);
541 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Invalid Data Length");
545 // Create EthernetData to add to queue
546 CAEthernetData *ethernetData = CACreateEthernetData(remoteEndpoint, data, dataLength);
549 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create ethernetData!");
550 return CA_MEMORY_ALLOC_FAILED;
553 // Add message to send queue
554 CAQueueingThreadAddData(gSendQueueHandle, ethernetData, sizeof(CAEthernetData));
556 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
560 uint32_t CASendEthernetMulticastData(void *data, uint32_t dataLength)
562 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
564 uint32_t dataSize = 0;
565 VERIFY_NON_NULL_RET(data, ETHERNET_ADAPTER_TAG, "data", dataSize);
566 VERIFY_NON_NULL_RET(gSendQueueHandle, ETHERNET_ADAPTER_TAG, "sendQueueHandle", dataSize);
570 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Invalid Data Length");
574 // Create ethernetData to add to queue
575 CAEthernetData *ethernetData = CACreateEthernetData(NULL, data, dataLength);
578 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create ethernetData!");
579 return CA_MEMORY_ALLOC_FAILED;
582 // Add message to send queue
583 CAQueueingThreadAddData(gSendQueueHandle, ethernetData, sizeof(CAEthernetData));
585 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
589 CAResult_t CAGetEthernetInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
591 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
592 VERIFY_NON_NULL(info, ETHERNET_ADAPTER_TAG, "info");
593 VERIFY_NON_NULL(size, ETHERNET_ADAPTER_TAG, "size");
595 CALocalConnectivity_t *netInfo = NULL;
596 bool retVal = CAEthernetIsConnected();
599 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
600 "Failed to get interface address, Ethernet 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, ETHERNET_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 = CAEthernetGetInterfaceInfo(&ifcName, &ipAddress);
618 if (CA_STATUS_OK != ret || NULL == ipAddress)
620 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to get interface info [%d]", ret);
628 // Create local endpoint using util function
629 CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_ETHERNET, ipAddress);
630 if (NULL == endpoint)
632 OIC_LOG_V(ERROR, ETHERNET_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(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
665 CAResult_t CAReadEthernetData()
667 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
671 CAResult_t CAEthernetStopServers()
673 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
675 // Stop unicast server and set client socket accordingly
676 if (CA_STATUS_OK == CAEthernetStopUnicastServer())
678 CAEthernetSetUnicastSocket(-1);
679 gUnicastServerport = -1;
683 // Stop secure unicast server and set client socket accordingly
684 if (CA_STATUS_OK == CAEthernetStopSecureUnicastServer())
686 CAEthernetSetSecureUnicastSocket(-1);
687 gSecureUnicastServerport = -1;
691 //Stop multicast server and set the state accordingly
692 if (CA_STATUS_OK == CAEthernetStopMulticastServer())
694 gIsMulticastServerStarted = false;
697 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
701 CAResult_t CAStopEthernet()
703 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
705 gStartUnicastServerRequested = false;
706 gStartMulticastServerRequested = false;
708 // Stop ethernet network monitor
709 CAEthernetStopNetworkMonitor();
711 // Stop send queue thread
712 if (gSendQueueHandle && CA_FALSE == gSendQueueHandle->isStop)
714 CAQueueingThreadStop(gSendQueueHandle);
717 // Stop Unicast, Secured unicast and Multicast servers running
718 CAEthernetStopServers();
720 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
724 void CATerminateEthernet()
726 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
729 CADTLSSetAdapterCallbacks(NULL, NULL, DTLS_ETHERNET);
730 CAAdapterNetDtlsDeInit();
733 // Stop ethernet adapter
736 // Terminate network monitor
737 CAEthernetSetConnectionStateChangeCallback(NULL);
738 CAEthernetTerminateNetworkMonitor();
740 // Terminate message queue handler
741 CAEthernetDeinitializeQueueHandles();
742 CAEthernetSetPacketReceiveCallback(NULL);
744 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
748 void CAEthernetSendDataThread(void *threadData)
750 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
752 CAEthernetData *ethernetData = (CAEthernetData *) threadData;
755 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Invalid ethernet data!");
759 if (NULL != ethernetData->remoteEndpoint)
761 char *address = ethernetData->remoteEndpoint->addressInfo.IP.ipAddress;
762 uint32_t port = ethernetData->remoteEndpoint->addressInfo.IP.port;
765 if (!ethernetData->remoteEndpoint->isSecured)
767 CAEthernetSendData(address, port, ethernetData->data, ethernetData->dataLen, false,
768 ethernetData->remoteEndpoint->isSecured);
772 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt called!");
773 uint8_t cacheFalg = 0;
774 CAResult_t result = CAAdapterNetDtlsEncrypt(address, port, ethernetData->data,
775 ethernetData->dataLen, &cacheFalg, DTLS_ETHERNET);
777 if (CA_STATUS_OK != result)
779 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt failed!");
781 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt returned with cache[%d]",
785 CAEthernetSendData(address, port, ethernetData->data, ethernetData->dataLen, false,
786 ethernetData->remoteEndpoint->isSecured);
791 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Send Multicast Data is called");
792 CAEthernetSendData(CA_MULTICAST_IP, CA_MCAST_PORT, ethernetData->data,
793 ethernetData->dataLen, true, false);
797 CAFreeEthernetData(ethernetData);
799 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
802 CAEthernetData *CACreateEthernetData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
805 CAEthernetData *ethernetData = (CAEthernetData *) OICMalloc(sizeof(CAEthernetData));
808 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
812 ethernetData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
813 ethernetData->data = (void *)OICMalloc(dataLength);
814 if (NULL == ethernetData->data)
816 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
817 CAFreeEthernetData(ethernetData);
821 memcpy(ethernetData->data, data, dataLength);
822 ethernetData->dataLen = dataLength;
827 void CAFreeEthernetData(CAEthernetData *ethernetData)
834 CAAdapterFreeRemoteEndpoint(ethernetData->remoteEndpoint);
835 OICFree(ethernetData->data);
836 OICFree(ethernetData);