Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / arduino / caipadapterutils_eth.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20 #include "caipadapterutils_eth.h"
21
22 #include <Arduino.h>
23 #include <Ethernet.h>
24 #include <socket.h>
25 #include <w5100.h>
26 #include <EthernetUdp.h>
27 #include <IPAddress.h>
28
29 #include "logger.h"
30 #include "cacommon.h"
31 #include "caadapterinterface.h"
32 #include "caadapterutils.h"
33
34 #define TAG "IPU"
35
36 CAResult_t CAArduinoGetAvailableSocket(int *sockID)
37 {
38     VERIFY_NON_NULL(sockID, TAG, "sockID");
39     uint8_t state;
40     //Is any socket available to work with ?
41     *sockID = 0;
42     for (int i = 1; i < MAX_SOCK_NUM; i++)
43     {
44         state = W5100.readSnSR(i);
45         if (state == SnSR::CLOSED || state == SnSR::FIN_WAIT)
46         {
47             *sockID = i;
48             break;
49         }
50     }
51
52     if (*sockID == 0)
53     {
54         OIC_LOG(ERROR, TAG, "sockID 0");
55         return CA_SOCKET_OPERATION_FAILED;
56     }
57
58     return CA_STATUS_OK;
59 }
60
61 CAResult_t CAArduinoInitUdpSocket(uint16_t *port, int *socketID)
62 {
63     OIC_LOG(DEBUG, TAG, "IN");
64     VERIFY_NON_NULL(port, TAG, "port");
65     VERIFY_NON_NULL(socketID, TAG, "socketID");
66
67     CAResult_t ret = CAArduinoGetAvailableSocket(socketID);
68     if (ret != CA_STATUS_OK)
69     {
70         OIC_LOG(ERROR, TAG, "get sock fail");
71         return ret;
72     }
73
74     //Create a datagram socket on which to recv/send.
75     if (!socket(*socketID, SnMR::UDP, *port, 0))
76     {
77         OIC_LOG(ERROR, TAG, "sock fail");
78         return CA_STATUS_FAILED;
79     }
80
81     OIC_LOG_V(DEBUG, TAG, "socketId:%d", *socketID);
82     OIC_LOG(DEBUG, TAG, "OUT");
83     return CA_STATUS_OK;
84 }
85
86 CAResult_t CAArduinoInitMulticastUdpSocket(const char *mcastAddress,
87                                            uint16_t mport,
88                                            uint16_t lport, int *socketID)
89 {
90     OIC_LOG(DEBUG, TAG, "IN");
91     VERIFY_NON_NULL(mcastAddress, TAG, "address");
92     VERIFY_NON_NULL(socketID, TAG, "socket");
93
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)
99     {
100         OIC_LOG(ERROR, TAG, "parse fail");
101         return CA_STATUS_FAILED;
102     }
103
104     *socketID = 0;
105     CAResult_t ret = CAArduinoGetAvailableSocket(socketID);
106     if (ret != CA_STATUS_OK)
107     {
108         OIC_LOG(ERROR, TAG, "sock fail");
109         return ret;
110     }
111
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);
119
120     //Create a datagram socket on which to recv/send.
121     if (!socket(*socketID, SnMR::UDP, lport, SnMR::MULTI))
122     {
123         OIC_LOG(ERROR, TAG, "sock fail");
124         return CA_SOCKET_OPERATION_FAILED;
125     }
126
127     OIC_LOG_V(DEBUG, TAG, "socketId:%d", *socketID);
128     OIC_LOG(DEBUG, TAG, "OUT");
129     return CA_STATUS_OK;
130 }
131