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 "caethernetinterface_singlethread.h"
26 #include <EthernetUdp.h>
27 #include <IPAddress.h>
31 #include "caadapterinterface.h"
32 #include "caethernetadapter_singlethread.h"
33 #include "caethernetadapterutils.h"
34 #include "caadapterutils.h"
35 #include "oic_malloc.h"
37 /// This is the max buffer size between Arduino and WiFi Shield
38 #define ARDUINO_ETHERNET_SPI_RECV_BUFFERSIZE (64)
42 static int32_t gSockID = 0;
48 static int16_t gUnicastPort = 0;
50 void CAEthernetSetUnicastSocket(const int32_t socketID)
52 OIC_LOG(DEBUG, MOD_NAME, "IN");
59 OIC_LOG(DEBUG, MOD_NAME, "OUT");
63 void CAEthernetSetUnicastPort(const int16_t port)
65 OIC_LOG(DEBUG, MOD_NAME, "IN");
72 OIC_LOG(DEBUG, MOD_NAME, "OUT");
76 uint32_t CAEthernetSendData(const char *remoteAddress, const int16_t port,
77 const char *buf, const uint32_t bufLen, bool isMulticast)
79 if (!isMulticast && 0 == gUnicastPort)
81 OIC_LOG(ERROR, MOD_NAME, "Failed");
88 if (CAArduinoInitMulticastUdpSocket(remoteAddress, &port, &gUnicastPort, &socketID) != CA_STATUS_OK)
90 OIC_LOG(ERROR, MOD_NAME, "multicast");
93 OIC_LOG_V(DEBUG, MOD_NAME, "MPORT:%d", port);
94 OIC_LOG_V(DEBUG, MOD_NAME, "LPORT:%d", gUnicastPort);
95 OIC_LOG_V(DEBUG, MOD_NAME, "SOCKET ID:%d", socketID);
101 if (CAArduinoInitUdpSocket((int16_t *)&port, &socketID) != CA_STATUS_OK)
103 OIC_LOG(ERROR, MOD_NAME, "unicast");
114 VERIFY_NON_NULL(buf, MOD_NAME, "buf");
115 VERIFY_NON_NULL(remoteAddress, MOD_NAME, "address");
117 uint8_t ipAddr[4] = { 0 };
118 uint16_t parsedPort = 0;
119 if (!CAParseIPv4AddressLocal((unsigned char *) remoteAddress, ipAddr, &parsedPort))
121 OIC_LOG(ERROR, MOD_NAME, "failed");
125 ret = sendto(socketID, (const uint8_t *)buf, (uint16_t)bufLen, (uint8_t *)ipAddr, port);
127 if (gSockID != socketID)
132 OIC_LOG(DEBUG, MOD_NAME, "OUT");