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 "caipinterface.h"
26 #include <utility/server_drv.h>
27 #include <utility/wifi_drv.h>
28 #include <IPAddress.h>
32 #include "caadapterinterface.h"
33 #include "caipadapter.h"
34 #include "caadapterutils.h"
36 /// This is the max buffer size between Arduino and WiFi Shield
37 #define ARDUINO_IP_BUFFERSIZE (90)
42 void CAIPSetUnicastSocket(int socketID)
47 void CAIPSetUnicastPort(uint16_t port)
52 void CAIPSendData(CAEndpoint_t *endpoint,
53 const void *data, uint32_t dataLength, bool isMulticast)
55 OIC_LOG(DEBUG, TAG, "IN");
57 VERIFY_NON_NULL_VOID(data, TAG, "data");
58 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint");
60 OIC_LOG_V(DEBUG, TAG, "remoteip: %s", endpoint->addr);
61 OIC_LOG_V(DEBUG, TAG, "port: %d", endpoint->port);
64 uint16_t parsedPort = 0;
65 CAResult_t res = CAParseIPv4AddressInternal(endpoint->addr, ip, sizeof(ip),
67 if (res != CA_STATUS_OK)
69 OIC_LOG_V(ERROR, TAG, "Remote adrs parse fail %d", res);
73 IPAddress remoteIp(ip);
74 Udp.beginPacket(remoteIp, endpoint->port);
76 uint32_t bytesWritten = 0;
77 while (bytesWritten < dataLength)
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));
88 OIC_LOG_V(ERROR, TAG, "Failed after %u", bytesWritten);
91 bytesWritten += writeCount;
94 if (Udp.endPacket() == 0)
96 OIC_LOG(ERROR, TAG, "Failed to send");
99 OIC_LOG(DEBUG, TAG, "OUT");