879f5001fb318ecb254fa59934e67a1b571256e1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / arduino / caipclient_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 "caipinterface.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 "caipadapter.h"
33 #include "caipnwmonitor.h"
34 #include "caipadapterutils_eth.h"
35 #include "caadapterutils.h"
36 #include "oic_malloc.h"
37 #include "oic_string.h"
38
39 #define TAG "IPC"
40
41 #define UINT16_MAX 65535
42
43 static int g_sockID = 0;
44
45 /**
46  * @var g_unicastPort
47  * @brief Unicast Port
48  */
49 static uint16_t g_unicastPort = 0;
50
51 #define IPv4_MULTICAST     "224.0.1.187"
52
53 void CAIPSetUnicastSocket(int socketID)
54 {
55     OIC_LOG(DEBUG, TAG, "IN");
56     if (0 < socketID)
57     {
58         g_sockID = socketID;
59     }
60     else
61     {
62         OIC_LOG(ERROR, TAG, "sock err");
63     }
64
65     OIC_LOG(DEBUG, TAG, "OUT");
66     return;
67 }
68
69 void CAIPSetUnicastPort(uint16_t port)
70 {
71     OIC_LOG(DEBUG, TAG, "IN");
72     g_unicastPort = port;
73     OIC_LOG(DEBUG, TAG, "OUT");
74     return;
75 }
76
77 void CAIPSendData(CAEndpoint_t *endpoint, const void *buf,
78                   size_t bufLen, bool isMulticast)
79 {
80     if (!isMulticast && 0 == g_unicastPort)
81     {
82         OIC_LOG(ERROR, TAG, "port 0");
83         return;
84     }
85
86     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
87
88     int socketID = 0;
89     uint16_t port = endpoint->port;
90     if (isMulticast)
91     {
92         port = CA_COAP;
93         OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
94         if (CAArduinoInitMulticastUdpSocket(endpoint->addr, port,
95                                             g_unicastPort, &socketID) != CA_STATUS_OK)
96         {
97             OIC_LOG(ERROR, TAG, "init mcast err");
98             return;
99         }
100         OIC_LOG_V(DEBUG, TAG, "MPORT:%u", port);
101         OIC_LOG_V(DEBUG, TAG, "LPORT:%u", g_unicastPort);
102         OIC_LOG_V(DEBUG, TAG, "SOCKET ID:%d", socketID);
103     }
104     else
105     {
106         if (0 == g_sockID)
107         {
108             if (CAArduinoInitUdpSocket(&port, &socketID) != CA_STATUS_OK)
109             {
110                 OIC_LOG(ERROR, TAG, "init ucast err");
111                 return;
112             }
113         }
114         else
115         {
116             socketID = g_sockID;
117         }
118     }
119
120     uint32_t ret;
121     uint8_t ipAddr[4] = { 0 };
122     uint16_t parsedPort = 0;
123     if (CAParseIPv4AddressInternal(endpoint->addr, ipAddr, sizeof(ipAddr),
124                                    &parsedPort) != CA_STATUS_OK)
125     {
126         OIC_LOG(ERROR, TAG, "parse fail");
127         return;
128     }
129
130     if (bufLen > UINT16_MAX)
131     {
132         // This will never happen as max buffer size we are dealing with is COAP_MAX_PDU_SIZE
133         OIC_LOG(ERROR, TAG, "Size exceeded");
134         return;
135     }
136
137     ret = sendto(socketID, (const uint8_t *)buf, (uint16_t)bufLen, ipAddr, port);
138     if (ret <= 0)
139     {
140         OIC_LOG_V(ERROR, TAG, "SendData failed: %d", ret);
141     }
142     if (g_sockID != socketID)
143     {
144         close(socketID);
145     }
146
147     OIC_LOG(DEBUG, TAG, "OUT");
148 }
149