[Win32] Rename 'interface' to satisfy Windows.h
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / arduino / caipserver_wifi.cpp
index 5c31cf9..5351219 100644 (file)
 #include "caipadapter.h"
 #include "caadapterutils.h"
 #include "oic_malloc.h"
+#include "oic_string.h"
 
 #define TAG "IPS"
 
 // Length of the IP address decimal notation string
 #define IPNAMESIZE (16)
 
+/** Multicast IP address.*/
+#define IPv4_MULTICAST      "224.0.1.187"
+
+/** Multicast Port.*/
+#define IPv4_MULTICAST_PORT 5683
+
 // Start offsets based on end of received data buffer
 #define IP_RECBUF_IPADDR_OFFSET  (6)
 #define IP_RECBUF_PORT_OFFSET    (2)
@@ -104,11 +111,12 @@ CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
         return CA_STATUS_FAILED;
     }
 
-    OIC_LOG_V(DEBUG, TAG, "port: %d", *port);
+    OIC_LOG_V(DEBUG, TAG, "port: %u", *port);
 
     Udp.begin((uint16_t ) *port);
     gServerRunning = true;
     g_unicastPort = *port;
+    caglobals.ip.u4.port =  *port;
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -122,12 +130,31 @@ CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multic
     return CA_NOT_SUPPORTED;
 }
 
+CAResult_t CAIPStartServer()
+{
+    uint16_t unicastPort = 55555;
+
+    CAResult_t ret = CAIPStartUnicastServer("0.0.0.0", &unicastPort, false);
+    if (CA_STATUS_OK != ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "Start unicast server failed[%d]", ret);
+        return ret;
+    }
+    ret = CAIPStartMulticastServer("0.0.0.0", IPv4_MULTICAST, IPv4_MULTICAST_PORT);
+    if (CA_STATUS_OK != ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "Start multicast failed[%d]", ret);
+    }
+    return ret;
+}
+
 CAResult_t CAIPStopUnicastServer()
 {
     OIC_LOG(DEBUG, TAG, "IN");
     Udp.stop();
 
     gServerRunning = false;
+    caglobals.ip.u4.port =  0;
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -137,20 +164,29 @@ CAResult_t CAIPStopMulticastServer()
     return CAIPStopUnicastServer();
 }
 
-CAResult_t CAIPStopServer(const char *interfaceAddress)
+CAResult_t CAIPStartListenServer()
 {
-    /* For arduino, Server will be running in only one interface */
-    return CAIPStopAllServers();
+    CAResult_t ret = CAIPStartMulticastServer("0.0.0.0", IPv4_MULTICAST, IPv4_MULTICAST_PORT);
+    if (CA_STATUS_OK != ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "Start multicast failed[%d]", ret);
+    }
+    return ret;
+}
+
+CAResult_t CAIPStopListenServer()
+{
+    return CAIPStopMulticastServer();
 }
 
-CAResult_t CAIPStopAllServers()
+void CAIPStopServer()
 {
     OIC_LOG(DEBUG, TAG, "IN");
     CAResult_t result = CAIPStopUnicastServer();
     if (CA_STATUS_OK != result)
     {
         OIC_LOG_V(ERROR, TAG, "stop ucast srv fail:%d", result);
-        return result;
+        return;
     }
     CAIPSetUnicastSocket(-1);
     CAIPSetUnicastPort(0);
@@ -161,7 +197,6 @@ CAResult_t CAIPStopAllServers()
         OIC_LOG_V(ERROR, TAG, "stop mcast srv fail:%d", result);
     }
     OIC_LOG(DEBUG, TAG, "OUT");
-    return result;
 }
 
 void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port,
@@ -170,12 +205,11 @@ void CAPacketReceivedCallback(const char *ipAddress, const uint16_t port,
     OIC_LOG(DEBUG, TAG, "IN");
     if (gPacketReceivedCallback)
     {
-        CAEndpoint_t ep;
-        strncpy(ep.addr, ipAddress, MAX_ADDR_STR_SIZE_CA);
-        ep.port = port;
-        ep.flags = CA_IPV4;
-        ep.adapter = CA_ADAPTER_IP;
-        gPacketReceivedCallback(&ep, data, dataLength);
+        CASecureEndpoint_t sep =
+        {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = CA_IPV4, .port = port}};
+
+        OICStrcpy(sep.endpoint.addr, sizeof(sep.endpoint.addr), ipAddress);
+        gPacketReceivedCallback(&sep, data, dataLength);
         OIC_LOG(DEBUG, TAG, "Notified network packet");
     }
     OIC_LOG(DEBUG, TAG, "OUT");
@@ -234,3 +268,53 @@ void CAIPPullData()
 {
     CAArduinoCheckData();
 }
+
+CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+
+    VERIFY_NON_NULL(info, TAG, "info is NULL");
+    VERIFY_NON_NULL(size, TAG, "size is NULL");
+
+    u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
+    if (!iflist)
+    {
+        OIC_LOG(ERROR, TAG, "get interface info failed");
+        return CA_STATUS_FAILED;
+    }
+
+    uint32_t len = u_arraylist_length(iflist);
+
+    CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
+    if (!eps)
+    {
+        OIC_LOG(ERROR, TAG, "Malloc Failed");
+        u_arraylist_destroy(iflist);
+        return CA_MEMORY_ALLOC_FAILED;
+    }
+
+    for (uint32_t i = 0, j = 0; i < len; i++)
+    {
+        CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+        if(!ifitem)
+        {
+            continue;
+        }
+        unsigned char *addr=  (unsigned char *) &(ifitem->ipv4addr);
+        snprintf(eps[j].addr, MAX_ADDR_STR_SIZE_CA, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
+
+        eps[j].flags = CA_IPV4;
+        eps[j].adapter = CA_ADAPTER_IP;
+        eps[j].ifindex = 0;
+        eps[j].port = caglobals.ip.u4.port;
+        j++;
+    }
+
+    *info = eps;
+    *size = len;
+
+    u_arraylist_destroy(iflist);
+
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
+}