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 "caipinterface_singlethread.h"
26 #include <EthernetUdp.h>
27 #include <IPAddress.h>
31 #include "caadapterinterface.h"
32 #include "caipadapter_singlethread.h"
33 #include "caipadapterutils_eth.h"
34 #include "caadapterutils.h"
35 #include "oic_malloc.h"
39 static int g_sockID = 0;
45 static uint16_t g_unicastPort = 0;
47 void CAIPSetUnicastSocket(int socketID)
49 OIC_LOG(DEBUG, TAG, "IN");
56 OIC_LOG(ERROR, TAG, "sock err");
59 OIC_LOG(DEBUG, TAG, "OUT");
63 void CAIPSetUnicastPort(uint16_t port)
65 OIC_LOG(DEBUG, TAG, "IN");
67 OIC_LOG(DEBUG, TAG, "OUT");
71 uint32_t CAIPSendData(const char *remoteAddress, uint16_t port,
72 const char *buf, uint32_t bufLen, bool isMulticast)
74 if (!isMulticast && 0 == g_unicastPort)
76 OIC_LOG(ERROR, TAG, "port 0");
80 VERIFY_NON_NULL(buf, TAG, "buf");
81 VERIFY_NON_NULL(remoteAddress, TAG, "address");
86 if (CAArduinoInitMulticastUdpSocket(remoteAddress, port, g_unicastPort, &socketID)
89 OIC_LOG(ERROR, TAG, "init mcast err");
92 OIC_LOG_V(DEBUG, TAG, "MPORT:%d", port);
93 OIC_LOG_V(DEBUG, TAG, "LPORT:%d", g_unicastPort);
94 OIC_LOG_V(DEBUG, TAG, "SOCKET ID:%d", socketID);
100 if (CAArduinoInitUdpSocket(&port, &socketID) != CA_STATUS_OK)
102 OIC_LOG(ERROR, TAG, "init ucast err");
113 uint8_t ipAddr[4] = { 0 };
114 uint16_t parsedPort = 0;
115 if (CAParseIPv4AddressInternal(remoteAddress, ipAddr, sizeof(ipAddr),
116 &parsedPort) != CA_STATUS_OK)
118 OIC_LOG(ERROR, TAG, "parse fail");
122 if (bufLen > 65535) // Max value for uint16_t
124 // This will never happen as max buffer size we are dealing with is COAP_MAX_PDU_SIZE
125 OIC_LOG(ERROR, TAG, "Size exceeded");
129 ret = sendto(socketID, (const uint8_t *)buf, (uint16_t)bufLen, ipAddr, port);
130 if (g_sockID != socketID)
135 OIC_LOG(DEBUG, TAG, "OUT");