Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / arduino / caipclient_wifi.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 <WiFi.h>
24 #include <WiFiUdp.h>
25 #include <SPI.h>
26 #include <utility/server_drv.h>
27 #include <utility/wifi_drv.h>
28 #include <IPAddress.h>
29
30 #include "logger.h"
31 #include "cacommon.h"
32 #include "caadapterinterface.h"
33 #include "caipadapter.h"
34 #include "caadapterutils.h"
35
36 /// This is the max buffer size between Arduino and WiFi Shield
37 #define ARDUINO_IP_BUFFERSIZE (90)
38 #define TAG "IPC"
39
40 static WiFiUDP Udp;
41
42 void CAIPSetUnicastSocket(int socketID)
43 {
44
45 }
46
47 void CAIPSetUnicastPort(uint16_t port)
48 {
49
50 }
51
52 void CAIPSendData(CAEndpoint_t *endpoint,
53                   const void *data, uint32_t dataLength, bool isMulticast)
54 {
55     OIC_LOG(DEBUG, TAG, "IN");
56
57     VERIFY_NON_NULL_VOID(data, TAG, "data");
58     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
59
60     OIC_LOG_V(DEBUG, TAG, "remoteip: %s", endpoint->addr);
61     OIC_LOG_V(DEBUG, TAG, "port: %d", endpoint->port);
62
63     uint8_t ip[4] = {0};
64     uint16_t parsedPort = 0;
65     CAResult_t res = CAParseIPv4AddressInternal(endpoint->addr, ip, sizeof(ip),
66                                                 &parsedPort);
67     if (res != CA_STATUS_OK)
68     {
69         OIC_LOG_V(ERROR, TAG, "Remote adrs parse fail %d", res);
70         return;
71     }
72
73     IPAddress remoteIp(ip);
74     Udp.beginPacket(remoteIp, endpoint->port);
75
76     uint32_t bytesWritten = 0;
77     while (bytesWritten < dataLength)
78     {
79         // get remaining bytes
80         size_t writeCount = dataLength - bytesWritten;
81         // write upto max ARDUINO_WIFI_BUFFERSIZE bytes
82         writeCount = Udp.write((uint8_t *)data + bytesWritten,
83                                 (writeCount > ARDUINO_IP_BUFFERSIZE ?
84                                  ARDUINO_IP_BUFFERSIZE:writeCount));
85         if(writeCount == 0)
86         {
87             // write failed
88             OIC_LOG_V(ERROR, TAG, "Failed after %u", bytesWritten);
89             break;
90         }
91         bytesWritten += writeCount;
92     }
93
94     if (Udp.endPacket() == 0)
95     {
96         OIC_LOG(ERROR, TAG, "Failed to send");
97         return;
98     }
99     OIC_LOG(DEBUG, TAG, "OUT");
100     return;
101 }
102