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.h"
26 #include <EthernetUdp.h>
27 #include <IPAddress.h>
31 #include "caadapterinterface.h"
32 #include "caipadapter.h"
33 #include "caipadapterutils_eth.h"
34 #include "caadapterutils.h"
35 #include "oic_malloc.h"
36 #include "oic_string.h"
40 static int g_sockID = 0;
46 static uint16_t g_unicastPort = 0;
48 #define IPv4_MULTICAST "224.0.1.187"
50 void CAIPSetUnicastSocket(int socketID)
52 OIC_LOG(DEBUG, TAG, "IN");
59 OIC_LOG(ERROR, TAG, "sock err");
62 OIC_LOG(DEBUG, TAG, "OUT");
66 void CAIPSetUnicastPort(uint16_t port)
68 OIC_LOG(DEBUG, TAG, "IN");
70 OIC_LOG(DEBUG, TAG, "OUT");
74 void CAIPSendData(CAEndpoint_t *endpoint, const void *buf,
75 uint32_t bufLen, bool isMulticast)
77 if (!isMulticast && 0 == g_unicastPort)
79 OIC_LOG(ERROR, TAG, "port 0");
83 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
86 uint16_t port = endpoint->port;
90 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
91 if (CAArduinoInitMulticastUdpSocket(endpoint->addr, port,
92 g_unicastPort, &socketID) != CA_STATUS_OK)
94 OIC_LOG(ERROR, TAG, "init mcast err");
97 OIC_LOG_V(DEBUG, TAG, "MPORT:%u", port);
98 OIC_LOG_V(DEBUG, TAG, "LPORT:%u", g_unicastPort);
99 OIC_LOG_V(DEBUG, TAG, "SOCKET ID:%d", socketID);
105 if (CAArduinoInitUdpSocket(&port, &socketID) != CA_STATUS_OK)
107 OIC_LOG(ERROR, TAG, "init ucast err");
118 uint8_t ipAddr[4] = { 0 };
119 uint16_t parsedPort = 0;
120 if (CAParseIPv4AddressInternal(endpoint->addr, ipAddr, sizeof(ipAddr),
121 &parsedPort) != CA_STATUS_OK)
123 OIC_LOG(ERROR, TAG, "parse fail");
127 if (bufLen > 65535) // Max value for uint16_t
129 // This will never happen as max buffer size we are dealing with is COAP_MAX_PDU_SIZE
130 OIC_LOG(ERROR, TAG, "Size exceeded");
134 ret = sendto(socketID, (const uint8_t *)buf, (uint16_t)bufLen, ipAddr, port);
137 OIC_LOG_V(ERROR, TAG, "SendData failed: %d", ret);
139 if (g_sockID != socketID)
144 OIC_LOG(DEBUG, TAG, "OUT");