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"
38 #include "oic_string.h"
42 // Length of the IP address decimal notation string
43 #define IPNAMESIZE (16)
45 // Start offsets based on end of received data buffer
46 #define IP_RECBUF_IPADDR_OFFSET (6)
47 #define IP_RECBUF_PORT_OFFSET (2)
49 #define IP_RECBUF_IPADDR_SIZE (IP_RECBUF_IPADDR_OFFSET - IP_RECBUF_PORT_OFFSET)
50 #define IP_RECBUF_PORT_SIZE (IP_RECBUF_PORT_OFFSET - 0)
51 #define IP_RECBUF_FOOTER_SIZE (IP_RECBUF_IPADDR_SIZE + IP_RECBUF_PORT_SIZE)
53 static void CAArduinoCheckData();
54 static void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port,
55 const void *data, const uint32_t dataLength);
57 static CAIPPacketReceivedCallback gPacketReceivedCallback = NULL;
58 static int32_t gUnicastSocket = 0;
59 static bool gServerRunning = false;
65 static uint16_t g_unicastPort = 0;
67 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool)
70 * This API is to keep design in sync with other platforms.
71 * The required implementation is done in Start() api's.
76 void CAIPTerminateServer(void)
79 * This API is to keep design in sync with other platforms.
80 * The required implementation is done in Stop() api's.
84 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured)
89 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
92 OIC_LOG(DEBUG, TAG, "IN");
93 VERIFY_NON_NULL(port, TAG, "port");
98 OIC_LOG(DEBUG, TAG, "Error");
99 return CA_STATUS_FAILED;
102 if (WiFi.status() != WL_CONNECTED)
104 OIC_LOG(ERROR, TAG, "ERROR:No WIFI");
105 return CA_STATUS_FAILED;
108 OIC_LOG_V(DEBUG, TAG, "port: %u", *port);
110 Udp.begin((uint16_t ) *port);
111 gServerRunning = true;
112 g_unicastPort = *port;
113 OIC_LOG(DEBUG, TAG, "OUT");
117 CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
118 uint16_t multicastPort)
120 // wifi shield does not support multicast
121 OIC_LOG(DEBUG, TAG, "IN");
122 OIC_LOG(DEBUG, TAG, "OUT");
123 return CA_NOT_SUPPORTED;
126 CAResult_t CAIPStartServer()
128 uint16_t unicastPort = 55555;
130 CAResult_t ret = CAIPStartUnicastServer("0.0.0.0", &unicastPort, false);
131 if (CA_STATUS_OK != ret)
133 OIC_LOG_V(DEBUG, TAG, "Start unicast serv failed[%d]", ret);
135 ret = CAIPStartMulticastServer("0.0.0.0", "224.0.1.187", 5683);
136 if (CA_STATUS_OK != ret)
138 OIC_LOG_V(ERROR, TAG, "Start multicast failed[%d]", ret);
143 CAResult_t CAIPStopUnicastServer()
145 OIC_LOG(DEBUG, TAG, "IN");
148 gServerRunning = false;
149 OIC_LOG(DEBUG, TAG, "OUT");
153 CAResult_t CAIPStopMulticastServer()
155 return CAIPStopUnicastServer();
158 void CAIPStopServer()
160 OIC_LOG(DEBUG, TAG, "IN");
161 CAResult_t result = CAIPStopUnicastServer();
162 if (CA_STATUS_OK != result)
164 OIC_LOG_V(ERROR, TAG, "stop ucast srv fail:%d", result);
167 CAIPSetUnicastSocket(-1);
168 CAIPSetUnicastPort(0);
170 result = CAIPStopMulticastServer();
171 if (CA_STATUS_OK != result)
173 OIC_LOG_V(ERROR, TAG, "stop mcast srv fail:%d", result);
175 OIC_LOG(DEBUG, TAG, "OUT");
178 void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port,
179 const void *data, const uint32_t dataLength)
181 OIC_LOG(DEBUG, TAG, "IN");
182 if (gPacketReceivedCallback)
184 CASecureEndpoint_t sep =
185 {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = CA_IPV4, .port = port}};
187 OICStrcpy(sep.endpoint.addr, sizeof(sep.endpoint.addr), ipAddress);
188 gPacketReceivedCallback(&sep, data, dataLength);
189 OIC_LOG(DEBUG, TAG, "Notified network packet");
191 OIC_LOG(DEBUG, TAG, "OUT");
194 void CAArduinoCheckData()
196 OIC_LOG(DEBUG, TAG, "IN");
197 char addr[IPNAMESIZE] = {0};
198 uint16_t senderPort = 0;
199 int16_t packetSize = Udp.parsePacket();
200 OIC_LOG_V(DEBUG, TAG, "Rcv packet of size:%d ", packetSize);
203 packetSize = packetSize > COAP_MAX_PDU_SIZE ? COAP_MAX_PDU_SIZE:packetSize;
204 char *data = (char *)OICMalloc(packetSize + 1);
209 IPAddress remoteIp = Udp.remoteIP();
210 senderPort = Udp.remotePort();
211 sprintf(addr, "%d.%d.%d.%d", remoteIp[0], remoteIp[1], remoteIp[2], remoteIp[3]);
212 OIC_LOG_V(DEBUG, TAG, "remoteip: %s, port: %d", addr, senderPort);
213 // read the packet into packetBufffer
214 int32_t dataLen = Udp.read(data, COAP_MAX_PDU_SIZE);
219 CAPacketReceivedCallback(addr, senderPort, data, dataLen);
222 OIC_LOG(DEBUG, TAG, "OUT");
225 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
227 OIC_LOG(DEBUG, TAG, "IN");
228 gPacketReceivedCallback = callback;
229 OIC_LOG(DEBUG, TAG, "OUT");
232 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
237 void CAIPSetErrorHandleCallback(CAIPErrorHandleCallback ipErrorCallback)
239 OIC_LOG(DEBUG, TAG, "IN");
240 OIC_LOG(DEBUG, TAG, "OUT");
245 CAArduinoCheckData();
248 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
250 OIC_LOG(DEBUG, TAG, "IN");
252 VERIFY_NON_NULL(info, TAG, "info is NULL");
253 VERIFY_NON_NULL(size, TAG, "size is NULL");
255 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
258 OIC_LOG(ERROR, TAG, "get interface info failed");
259 return CA_STATUS_FAILED;
262 uint32_t len = u_arraylist_length(iflist);
264 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
267 OIC_LOG(ERROR, TAG, "Malloc Failed");
268 u_arraylist_destroy(iflist);
269 return CA_MEMORY_ALLOC_FAILED;
272 for (uint32_t i = 0, j = 0; i < len; i++)
274 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
276 OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
277 eps[j].flags = CA_IPV4;
278 eps[j].adapter = CA_ADAPTER_IP;
279 eps[j].interface = 0;
287 u_arraylist_destroy(iflist);
289 OIC_LOG(DEBUG, TAG, "OUT");