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 ******************************************************************/
21 #include "caipinterface.h"
27 #include <utility/server_drv.h>
28 #include <utility/wifi_drv.h>
29 #include <IPAddress.h>
33 #include "cainterface.h"
34 #include "caadapterinterface.h"
35 #include "caipadapter.h"
36 #include "caadapterutils.h"
37 #include "oic_malloc.h"
41 // Length of the IP address decimal notation string
42 #define IPNAMESIZE (16)
44 // Start offsets based on end of received data buffer
45 #define IP_RECBUF_IPADDR_OFFSET (6)
46 #define IP_RECBUF_PORT_OFFSET (2)
48 #define IP_RECBUF_IPADDR_SIZE (IP_RECBUF_IPADDR_OFFSET - IP_RECBUF_PORT_OFFSET)
49 #define IP_RECBUF_PORT_SIZE (IP_RECBUF_PORT_OFFSET - 0)
50 #define IP_RECBUF_FOOTER_SIZE (IP_RECBUF_IPADDR_SIZE + IP_RECBUF_PORT_SIZE)
52 static void CAArduinoCheckData();
53 static void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port,
54 const void *data, const uint32_t dataLength);
56 static CAIPPacketReceivedCallback gPacketReceivedCallback = NULL;
57 static int32_t gUnicastSocket = 0;
58 static bool gServerRunning = false;
64 static uint16_t g_unicastPort = 0;
66 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool)
69 * This API is to keep design in sync with other platforms.
70 * The required implementation is done in Start() api's.
75 void CAIPTerminateServer(void)
78 * This API is to keep design in sync with other platforms.
79 * The required implementation is done in Stop() api's.
83 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured)
88 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
91 OIC_LOG(DEBUG, TAG, "IN");
92 VERIFY_NON_NULL(port, TAG, "port");
97 OIC_LOG(DEBUG, TAG, "Error");
98 return CA_STATUS_FAILED;
101 if (WiFi.status() != WL_CONNECTED)
103 OIC_LOG(ERROR, TAG, "ERROR:No WIFI");
104 return CA_STATUS_FAILED;
107 OIC_LOG_V(DEBUG, TAG, "port: %d", *port);
109 Udp.begin((uint16_t ) *port);
110 gServerRunning = true;
111 g_unicastPort = *port;
112 OIC_LOG(DEBUG, TAG, "OUT");
116 CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
117 uint16_t multicastPort)
119 // wifi shield does not support multicast
120 OIC_LOG(DEBUG, TAG, "IN");
121 OIC_LOG(DEBUG, TAG, "OUT");
122 return CA_NOT_SUPPORTED;
125 CAResult_t CAIPStopUnicastServer()
127 OIC_LOG(DEBUG, TAG, "IN");
130 gServerRunning = false;
131 OIC_LOG(DEBUG, TAG, "OUT");
135 CAResult_t CAIPStopMulticastServer()
137 return CAIPStopUnicastServer();
140 CAResult_t CAIPStopServer(const char *interfaceAddress)
142 /* For arduino, Server will be running in only one interface */
143 return CAIPStopAllServers();
146 CAResult_t CAIPStopAllServers()
148 OIC_LOG(DEBUG, TAG, "IN");
149 CAResult_t result = CAIPStopUnicastServer();
150 if (CA_STATUS_OK != result)
152 OIC_LOG_V(ERROR, TAG, "stop ucast srv fail:%d", result);
155 CAIPSetUnicastSocket(-1);
156 CAIPSetUnicastPort(0);
158 result = CAIPStopMulticastServer();
159 if (CA_STATUS_OK != result)
161 OIC_LOG_V(ERROR, TAG, "stop mcast srv fail:%d", result);
163 OIC_LOG(DEBUG, TAG, "OUT");
167 void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port,
168 const void *data, const uint32_t dataLength)
170 OIC_LOG(DEBUG, TAG, "IN");
171 if (gPacketReceivedCallback)
174 strncpy(ep.addr, ipAddress, MAX_ADDR_STR_SIZE_CA);
177 ep.adapter = CA_ADAPTER_IP;
178 gPacketReceivedCallback(&ep, data, dataLength);
179 OIC_LOG(DEBUG, TAG, "Notified network packet");
181 OIC_LOG(DEBUG, TAG, "OUT");
184 void CAArduinoCheckData()
186 OIC_LOG(DEBUG, TAG, "IN");
187 char addr[IPNAMESIZE] = {0};
188 uint16_t senderPort = 0;
189 int16_t packetSize = Udp.parsePacket();
190 OIC_LOG_V(DEBUG, TAG, "Rcv packet of size:%d ", packetSize);
193 packetSize = packetSize > COAP_MAX_PDU_SIZE ? COAP_MAX_PDU_SIZE:packetSize;
194 char *data = (char *)OICMalloc(packetSize + 1);
199 IPAddress remoteIp = Udp.remoteIP();
200 senderPort = Udp.remotePort();
201 sprintf(addr, "%d.%d.%d.%d", remoteIp[0], remoteIp[1], remoteIp[2], remoteIp[3]);
202 OIC_LOG_V(DEBUG, TAG, "remoteip: %s, port: %d", addr, senderPort);
203 // read the packet into packetBufffer
204 int32_t dataLen = Udp.read(data, COAP_MAX_PDU_SIZE);
209 CAPacketReceivedCallback(addr, senderPort, data, dataLen);
212 OIC_LOG(DEBUG, TAG, "OUT");
215 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
217 OIC_LOG(DEBUG, TAG, "IN");
218 gPacketReceivedCallback = callback;
219 OIC_LOG(DEBUG, TAG, "OUT");
222 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
227 void CAIPSetErrorHandleCallback(CAIPErrorHandleCallback ipErrorCallback)
229 OIC_LOG(DEBUG, TAG, "IN");
230 OIC_LOG(DEBUG, TAG, "OUT");
235 CAArduinoCheckData();