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 "caipadapterutils_eth.h"
26 #include <EthernetUdp.h>
27 #include <IPAddress.h>
31 #include "caadapterinterface.h"
32 #include "caadapterutils.h"
36 CAResult_t CAArduinoGetAvailableSocket(int *sockID)
38 VERIFY_NON_NULL(sockID, TAG, "sockID");
40 //Is any socket available to work with ?
42 for (int i = 1; i < MAX_SOCK_NUM; i++)
44 state = W5100.readSnSR(i);
45 if (state == SnSR::CLOSED || state == SnSR::FIN_WAIT)
54 OIC_LOG(ERROR, TAG, "sockID 0");
55 return CA_SOCKET_OPERATION_FAILED;
61 CAResult_t CAArduinoInitUdpSocket(uint16_t *port, int *socketID)
63 OIC_LOG(DEBUG, TAG, "IN");
64 VERIFY_NON_NULL(port, TAG, "port");
65 VERIFY_NON_NULL(socketID, TAG, "socketID");
67 CAResult_t ret = CAArduinoGetAvailableSocket(socketID);
68 if (ret != CA_STATUS_OK)
70 OIC_LOG(ERROR, TAG, "get sock fail");
74 //Create a datagram socket on which to recv/send.
75 if (!socket(*socketID, SnMR::UDP, *port, 0))
77 OIC_LOG(ERROR, TAG, "sock fail");
78 return CA_STATUS_FAILED;
81 OIC_LOG_V(DEBUG, TAG, "socketId:%d", *socketID);
82 OIC_LOG(DEBUG, TAG, "OUT");
86 CAResult_t CAArduinoInitMulticastUdpSocket(const char *mcastAddress,
88 uint16_t lport, int *socketID)
90 OIC_LOG(DEBUG, TAG, "IN");
91 VERIFY_NON_NULL(mcastAddress, TAG, "address");
92 VERIFY_NON_NULL(socketID, TAG, "socket");
94 uint8_t mcastMacAddr[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00};
95 uint8_t ipAddr[4] = { 0 };
96 uint16_t parsedPort = 0;
97 if (CAParseIPv4AddressInternal(mcastAddress, ipAddr, sizeof(ipAddr),
98 &parsedPort) != CA_STATUS_OK)
100 OIC_LOG(ERROR, TAG, "parse fail");
101 return CA_STATUS_FAILED;
105 CAResult_t ret = CAArduinoGetAvailableSocket(socketID);
106 if (ret != CA_STATUS_OK)
108 OIC_LOG(ERROR, TAG, "sock fail");
112 //Calculate Multicast MAC address
113 mcastMacAddr[3] = ipAddr[1] & 0x7F;
114 mcastMacAddr[4] = ipAddr[2];
115 mcastMacAddr[5] = ipAddr[3];
116 W5100.writeSnDIPR(*socketID, (uint8_t *)ipAddr);
117 W5100.writeSnDHAR(*socketID, mcastMacAddr);
118 W5100.writeSnDPORT(*socketID, mport);
120 //Create a datagram socket on which to recv/send.
121 if (!socket(*socketID, SnMR::UDP, lport, SnMR::MULTI))
123 OIC_LOG(ERROR, TAG, "sock fail");
124 return CA_SOCKET_OPERATION_FAILED;
127 OIC_LOG_V(DEBUG, TAG, "socketId:%d", *socketID);
128 OIC_LOG(DEBUG, TAG, "OUT");