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 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 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);
144 static CAResult_t CAEthernetStopServers();
145 static void CAEthernetSendDataThread(void *threadData);
146 static CAEthernetData *CACreateEthernetData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
147 uint32_t dataLength);
148 void CAFreeEthernetData(CAEthernetData *EthernetData);
151 CAResult_t CAEthernetInitializeQueueHandles()
153 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
155 // Check if the message queue is already initialized
156 if (gSendQueueHandle)
158 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Already queue is initialized!");
162 // Create send message queue
163 gSendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
164 if (!gSendQueueHandle)
166 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
167 return CA_MEMORY_ALLOC_FAILED;
170 if (CA_STATUS_OK != CAQueueingThreadInitialize(gSendQueueHandle, gThreadPool,
171 CAEthernetSendDataThread))
173 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Initialize send queue thread");
174 OICFree(gSendQueueHandle);
175 gSendQueueHandle = 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(gSendQueueHandle);
188 OICFree(gSendQueueHandle);
189 gSendQueueHandle = NULL;
191 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
194 void CAEthernetNotifyNetworkChange(const char *address, const int16_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_V(ERROR, ETHERNET_ADAPTER_TAG, "Out of memory");
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();
389 CADTLSSetAdapterCallbacks(CAEthernetPacketReceivedCB, CAEthernetPacketSendCB, DTLS_ETHERNET);
392 CAConnectivityHandler_t ethernetHandler;
393 ethernetHandler.startAdapter = CAStartEthernet;
394 ethernetHandler.startListenServer = CAStartEthernetListeningServer;
395 ethernetHandler.startDiscoverServer = CAStartEthernetDiscoveryServer;
396 ethernetHandler.sendData = CASendEthernetUnicastData;
397 ethernetHandler.sendDataToAll = CASendEthernetMulticastData;
398 ethernetHandler.GetnetInfo = CAGetEthernetInterfaceInformation;
399 ethernetHandler.readData = CAReadEthernetData;
400 ethernetHandler.stopAdapter = CAStopEthernet;
401 ethernetHandler.terminate = CATerminateEthernet;
402 registerCallback(ethernetHandler, CA_ETHERNET);
404 if (CA_STATUS_OK != CAEthernetInitializeQueueHandles())
406 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Initialize Queue Handle");
407 CATerminateEthernet();
408 return CA_STATUS_FAILED;
411 OIC_LOG(INFO, ETHERNET_ADAPTER_TAG, "IntializeEthernet is Success");
415 CAResult_t CAStartEthernet()
417 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
419 // Start monitoring Ethernet network
420 CAResult_t ret = CAEthernetStartNetworkMonitor();
421 if (CA_STATUS_OK != ret)
423 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Start n/w monitor");
427 // Start send queue thread
428 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
430 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Start Send Data Thread");
432 return CA_STATUS_FAILED;
435 // Start send queue thread
436 if (CA_STATUS_OK != CAQueueingThreadStart(gSendQueueHandle))
438 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Failed to Start Send Data Thread");
439 return CA_STATUS_FAILED;
442 gStartUnicastServerRequested = true;
443 bool retVal = CAEthernetIsConnected();
446 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Ethernet is not Connected");
452 ret = CAEthernetGetInterfaceInfo(&ifcName, &ifcAdrs);
453 if (CA_STATUS_OK != ret)
455 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Failed to get ethernet interface info [%d]", ret);
458 int16_t unicastPort = CA_PORT;
459 int32_t serverFd = 0;
460 // Address is hardcoded as we are using Single Interface
461 ret = CAEthernetStartUnicastServer(ifcAdrs, &unicastPort, false, false, &serverFd);
462 if (CA_STATUS_OK == ret)
464 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Unicast server started on %d port", unicastPort);
465 CAEthernetSetUnicastSocket(serverFd);
466 gUnicastServerport = unicastPort;
470 // Address is hardcoded as we are using Single Interface
471 unicastPort = CA_SECURE_PORT;
472 ret = CAEthernetStartUnicastServer(ifcAdrs, &unicastPort, false, true, &serverFd);
474 if (CA_STATUS_OK == ret)
476 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Secure Unicast server started on %d port", unicastPort);
477 CAEthernetSetSecureUnicastSocket(serverFd);
478 gSecureUnicastServerport = unicastPort;
485 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
489 CAResult_t CAStartEthernetListeningServer()
491 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
493 CAResult_t ret = CA_STATUS_OK;
494 int16_t multicastPort = CA_MCAST_PORT;
496 if (gIsMulticastServerStarted == true)
498 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
499 "Failed to Start Multicast Server, Already Started!");
500 return CA_SERVER_STARTED_ALREADY;
503 gStartMulticastServerRequested = true;
504 bool retVal = CAEthernetIsConnected();
507 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
508 "Failed to Start Multicast Server, Ethernet not Connected");
514 ret = CAEthernetGetInterfaceInfo(&ifcName, &ifcAdrs);
515 if (CA_STATUS_OK != ret)
517 OIC_LOG_V(DEBUG, ETHERNET_ADAPTER_TAG, "Failed to get ethernet interface info [%d]", ret);
520 int32_t multicastFd = 0;
521 ret = CAEthernetStartMulticastServer(ifcAdrs, CA_MULTICAST_IP, multicastPort, &multicastFd);
522 if (CA_STATUS_OK == ret)
524 OIC_LOG(INFO, ETHERNET_ADAPTER_TAG, "Multicast Server is Started Successfully");
525 gIsMulticastServerStarted = true;
530 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
534 CAResult_t CAStartEthernetDiscoveryServer()
536 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
537 return CAStartEthernetListeningServer();
540 uint32_t CASendEthernetUnicastData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
543 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
545 uint32_t dataSize = 0;
546 VERIFY_NON_NULL_RET(remoteEndpoint, ETHERNET_ADAPTER_TAG, "remoteEndpoint", dataSize);
547 VERIFY_NON_NULL_RET(data, ETHERNET_ADAPTER_TAG, "data", dataSize);
548 VERIFY_NON_NULL_RET(gSendQueueHandle, ETHERNET_ADAPTER_TAG, "sendQueueHandle", dataSize);
552 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Invalid Data Length");
556 // Create EthernetData to add to queue
557 CAEthernetData *ethernetData = CACreateEthernetData(remoteEndpoint, data, dataLength);
560 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create ethernetData!");
561 return CA_MEMORY_ALLOC_FAILED;
564 // Add message to send queue
565 CAQueueingThreadAddData(gSendQueueHandle, ethernetData, sizeof(CAEthernetData));
567 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
571 uint32_t CASendEthernetMulticastData(void *data, uint32_t dataLength)
573 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
575 uint32_t dataSize = 0;
576 VERIFY_NON_NULL_RET(data, ETHERNET_ADAPTER_TAG, "data", dataSize);
577 VERIFY_NON_NULL_RET(gSendQueueHandle, ETHERNET_ADAPTER_TAG, "sendQueueHandle", dataSize);
581 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Invalid Data Length");
585 // Create EthernetData to add to queue
586 CAEthernetData *EthernetData = CACreateEthernetData(NULL, data, dataLength);
589 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create ethernetData!");
590 return CA_MEMORY_ALLOC_FAILED;
593 // Add message to send queue
594 CAQueueingThreadAddData(gSendQueueHandle, EthernetData, sizeof(CAEthernetData));
596 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
600 CAResult_t CAGetEthernetInterfaceInformation(CALocalConnectivity_t **info, uint32_t *size)
602 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
603 VERIFY_NON_NULL(info, ETHERNET_ADAPTER_TAG, "info");
604 VERIFY_NON_NULL(size, ETHERNET_ADAPTER_TAG, "size");
606 CALocalConnectivity_t *netInfo = NULL;
607 bool retVal = CAEthernetIsConnected();
610 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG,
611 "Failed to get interface address, Ethernet not Connected", CA_ADAPTER_NOT_ENABLED);
612 return CA_ADAPTER_NOT_ENABLED;
615 int32_t netInfoSize = 1;
617 if (gSecureUnicastServerport)
622 netInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * netInfoSize);
623 VERIFY_NON_NULL_RET(netInfo, ETHERNET_ADAPTER_TAG, "malloc failed", CA_MEMORY_ALLOC_FAILED);
624 memset(netInfo, 0, sizeof(CALocalConnectivity_t) * netInfoSize);
626 char *ipAddress = NULL;
627 char *ifcName = NULL;
628 CAResult_t ret = CAEthernetGetInterfaceInfo(&ifcName, &ipAddress);
629 if (CA_STATUS_OK != ret || NULL == ipAddress)
631 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to get interface info [%d]", ret);
639 // Create local endpoint using util function
640 CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_ETHERNET, ipAddress);
641 if (NULL == endpoint)
643 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "Failed to create Local Endpoint!",
644 CA_MEMORY_ALLOC_FAILED);
648 return CA_MEMORY_ALLOC_FAILED;
651 // copy unciast server information
652 endpoint->isSecured = CA_FALSE;
653 endpoint->addressInfo.IP.port = gUnicastServerport;
654 memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
657 // copy sevure unicast server information
658 if (gSecureUnicastServerport)
660 endpoint->isSecured = CA_TRUE;
661 endpoint->addressInfo.IP.port = gSecureUnicastServerport;
662 memcpy(&netInfo[1], endpoint, sizeof(CALocalConnectivity_t));
670 CAAdapterFreeLocalEndpoint(endpoint);
672 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
676 CAResult_t CAReadEthernetData()
678 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
682 CAResult_t CAEthernetStopServers()
684 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
686 // Stop unicast server and set client socket accordingly
687 if (CA_STATUS_OK == CAEthernetStopUnicastServer())
689 CAEthernetSetUnicastSocket(-1);
690 gUnicastServerport = -1;
694 // Stop secure unicast server and set client socket accordingly
695 if (CA_STATUS_OK == CAEthernetStopSecureUnicastServer())
697 CAEthernetSetSecureUnicastSocket(-1);
698 gSecureUnicastServerport = -1;
702 //Stop multicast server and set the state accordingly
703 if (CA_STATUS_OK == CAEthernetStopMulticastServer())
705 gIsMulticastServerStarted = false;
708 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
712 CAResult_t CAStopEthernet()
714 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
716 gStartUnicastServerRequested = false;
717 gStartMulticastServerRequested = false;
719 // Stop ethernet network monitor
720 CAEthernetStopNetworkMonitor();
722 // Stop send queue thread
723 if (gSendQueueHandle && CA_FALSE == gSendQueueHandle->isStop)
725 CAQueueingThreadStop(gSendQueueHandle);
728 // Stop Unicast, Secured unicast and Multicast servers running
729 CAEthernetStopServers();
731 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
735 void CATerminateEthernet()
737 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
740 CADTLSSetAdapterCallbacks(NULL, NULL, DTLS_ETHERNET);
741 CAAdapterNetDtlsDeInit();
744 // Stop ethernet adapter
747 // Terminate Ethernet server
748 CAEthernetTerminateServer();
750 // Terminate network monitor
751 CAEthernetSetConnectionStateChangeCallback(NULL);
752 CAEthernetTerminateNetworkMonitor();
754 // Terminate message queue handler
755 CAEthernetDeinitializeQueueHandles();
756 CAEthernetSetPacketReceiveCallback(NULL);
758 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
762 void CAEthernetSendDataThread(void *threadData)
764 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "IN");
766 CAEthernetData *ethernetData = (CAEthernetData *) threadData;
769 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Invalid ethernet data!");
773 if (NULL != ethernetData->remoteEndpoint)
775 char *address = ethernetData->remoteEndpoint->addressInfo.IP.ipAddress;
776 uint32_t port = ethernetData->remoteEndpoint->addressInfo.IP.port;
779 if (!ethernetData->remoteEndpoint->isSecured)
781 CAEthernetSendData(address, port, ethernetData->data, ethernetData->dataLen, false,
782 ethernetData->remoteEndpoint->isSecured);
786 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt called!");
787 uint8_t cacheFalg = 0;
788 CAResult_t result = CAAdapterNetDtlsEncrypt(address, port, ethernetData->data,
789 ethernetData->dataLen, &cacheFalg, DTLS_ETHERNET);
791 if (CA_STATUS_OK != result)
793 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt failed!");
795 OIC_LOG_V(ERROR, ETHERNET_ADAPTER_TAG, "CAAdapterNetDtlsEncrypt returned with cache[%d]",
799 CAEthernetSendData(address, port, ethernetData->data, ethernetData->dataLen, false,
800 ethernetData->remoteEndpoint->isSecured);
805 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "Send Multicast Data is called");
806 CAEthernetSendData(CA_MULTICAST_IP, CA_MCAST_PORT, ethernetData->data,
807 ethernetData->dataLen, true, false);
811 CAFreeEthernetData(ethernetData);
813 OIC_LOG(DEBUG, ETHERNET_ADAPTER_TAG, "OUT");
816 CAEthernetData *CACreateEthernetData(const CARemoteEndpoint_t *remoteEndpoint, void *data,
819 CAEthernetData *ethernetData = (CAEthernetData *) OICMalloc(sizeof(CAEthernetData));
822 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
826 ethernetData->remoteEndpoint = CAAdapterCopyRemoteEndpoint(remoteEndpoint);
827 ethernetData->data = (void *)OICMalloc(dataLength);
828 if (NULL == ethernetData->data)
830 OIC_LOG(ERROR, ETHERNET_ADAPTER_TAG, "Memory allocation failed!");
831 CAFreeEthernetData(ethernetData);
835 memcpy(ethernetData->data, data, dataLength);
836 ethernetData->dataLen = dataLength;
841 void CAFreeEthernetData(CAEthernetData *ethernetData)
848 CAAdapterFreeRemoteEndpoint(ethernetData->remoteEndpoint);
849 OICFree(ethernetData->data);
850 OICFree(ethernetData);