0507288823cd01ea1e60004a525821c50e7a4320
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ethernet_adapter / arduino / caethernetclient.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 "caethernetinterface_singlethread.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 "caethernetadapter_singlethread.h"
33 #include "caethernetadapterutils.h"
34 #include "caadapterutils.h"
35 #include "oic_malloc.h"
36
37 /// This is the max buffer size between Arduino and WiFi Shield
38 #define ARDUINO_ETHERNET_SPI_RECV_BUFFERSIZE (64)
39
40 #define MOD_NAME "EC"
41
42 static int32_t gSockID = 0;
43
44 /**
45  * @var gUnicastPort
46  * @brief Unicast Port
47  */
48 static int16_t gUnicastPort = 0;
49
50 void CAEthernetSetUnicastSocket(const int32_t socketID)
51 {
52     OIC_LOG(DEBUG, MOD_NAME, "IN");
53     if (0 < socketID)
54     {
55         gSockID = socketID;
56         return;
57     }
58
59     OIC_LOG(DEBUG, MOD_NAME, "OUT");
60     return;
61 }
62
63 void CAEthernetSetUnicastPort(const int16_t port)
64 {
65     OIC_LOG(DEBUG, MOD_NAME, "IN");
66     if (0 < port)
67     {
68         gUnicastPort = port;
69         return;
70     }
71
72     OIC_LOG(DEBUG, MOD_NAME, "OUT");
73     return;
74 }
75
76 uint32_t CAEthernetSendData(const char *remoteAddress, const int16_t port,
77                             const char *buf, const uint32_t bufLen, bool isMulticast)
78 {
79     if (!isMulticast && 0 == gUnicastPort)
80     {
81         OIC_LOG(ERROR, MOD_NAME, "Failed");
82         return 0;
83     }
84
85     int32_t socketID = 0;
86     if (isMulticast)
87     {
88         if (CAArduinoInitMulticastUdpSocket(remoteAddress, &port, &gUnicastPort, &socketID) != CA_STATUS_OK)
89         {
90             OIC_LOG(ERROR, MOD_NAME, "multicast");
91             return 0;
92         }
93         OIC_LOG_V(DEBUG, MOD_NAME, "MPORT:%d", port);
94         OIC_LOG_V(DEBUG, MOD_NAME, "LPORT:%d", gUnicastPort);
95         OIC_LOG_V(DEBUG, MOD_NAME, "SOCKET ID:%d", socketID);
96     }
97     else
98     {
99         if (0 == gSockID)
100         {
101             if (CAArduinoInitUdpSocket((int16_t *)&port, &socketID) != CA_STATUS_OK)
102             {
103                 OIC_LOG(ERROR, MOD_NAME, "unicast");
104                 return 0;
105             }
106         }
107         else
108         {
109             socketID = gSockID;
110         }
111     }
112
113     uint32_t ret;
114     VERIFY_NON_NULL(buf, MOD_NAME, "buf");
115     VERIFY_NON_NULL(remoteAddress, MOD_NAME, "address");
116
117     uint8_t ipAddr[4] = { 0 };
118     uint16_t parsedPort = 0;
119     if (!CAParseIPv4AddressLocal((unsigned char *) remoteAddress, ipAddr, &parsedPort))
120     {
121         OIC_LOG(ERROR, MOD_NAME, "failed");
122         return 0;
123     }
124
125     ret = sendto(socketID, (const uint8_t *)buf, (uint16_t)bufLen, (uint8_t *)ipAddr, port);
126     delay(10);
127     if (gSockID != socketID)
128     {
129         close(socketID);
130     }
131
132     OIC_LOG(DEBUG, MOD_NAME, "OUT");
133     return ret;
134 }