Clean up the C samples
authorSakthivel Samidurai <sakthivel.samidurai@intel.com>
Mon, 2 Feb 2015 19:59:38 +0000 (16:59 -0300)
committerSudarshan Prasad <sudarshan.prasad@intel.com>
Thu, 5 Feb 2015 02:14:21 +0000 (02:14 +0000)
Update the URI in the samples for Unicast discovery and device discovery.

Change-Id: Ice2ec98bb33033743c17f3259e6a86380c0afc0e
Signed-off-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/285
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
Reviewed-by: Joseph Morrow <joseph.l.morrow@intel.com>
Reviewed-by: Yuliya Kamatkova <yuliya.kamatkova@intel.com>
Reviewed-by: Sudarshan Prasad <sudarshan.prasad@intel.com>
resource/csdk/stack/samples/linux/SimpleClientServer/common.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/occlientbasicops.h
resource/csdk/stack/samples/linux/SimpleClientServer/occlientcoll.cpp
resource/csdk/stack/samples/linux/SimpleClientServer/occlientslow.cpp

index 3f2b586..af0c749 100644 (file)
@@ -72,3 +72,16 @@ const char *getResult(OCStackResult result) {
         return "UNKNOWN";
     }
 }
+
+void StripNewLineChar(char* str) {
+    int i = 0;
+    if (str)
+    {
+        while( str[i])
+        {
+            if (str[i] == '\n')
+                str[i] = '\0';
+            i++;
+        }
+    }
+}
index d74aa68..cb22f4f 100644 (file)
 
 static int UNICAST_DISCOVERY = 0;
 static int TEST_CASE = 0;
-static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
-static const char * TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core/d";
-
+static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:5298/oc/core";
+static const char * UNICAST_DEVICE_DISCOVERY_QUERY = "coap://%s:5298/oc/core/d";
 static const char * MULTICAST_DEVICE_DISCOVERY_QUERY = "/oc/core/d";
-
 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
 //The following variable determines the interface (wifi, ethernet etc.)
 //to be used for sending unicast messages. Default set to WIFI.
@@ -44,6 +42,10 @@ static std::string putPayload = "{\"state\":\"on\",\"power\":5}";
 static std::string coapServerIP = "255.255.255.255";
 static std::string coapServerPort = "5683";
 static std::string coapServerResource = "/a/light";
+static const int IPV4_ADDR_SIZE = 16;
+//Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
+char ipv4addr[IPV4_ADDR_SIZE];
+void StripNewLineChar(char* str);
 
 // The handle for the observe registration
 OCDoHandle gObserveDoHandle;
@@ -567,10 +569,8 @@ int InitDeviceDiscovery()
 
     if(UNICAST_DISCOVERY)
     {
-        strncpy(szQueryUri, TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY,
-                        (strlen(TEST_APP_UNICAST_DEVICE_DISCOVERY_QUERY) + 1));
+        snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DEVICE_DISCOVERY_QUERY, ipv4addr);
     }
-
     else
     {
         strncpy(szQueryUri, MULTICAST_DEVICE_DISCOVERY_QUERY,
@@ -606,7 +606,7 @@ int InitDiscovery()
 
     if (UNICAST_DISCOVERY)
     {
-        strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
+        snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
     }
     else
     {
@@ -634,10 +634,6 @@ int InitDiscovery()
 }
 
 int main(int argc, char* argv[]) {
-    uint8_t addr[20] = {0};
-    uint8_t* paddr = NULL;
-    uint16_t port = USE_RANDOM_PORT;
-    uint8_t ifname[] = "eth0";
     int opt;
 
     while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
@@ -666,21 +662,25 @@ int main(int argc, char* argv[]) {
         return -1;
     }
 
-
-    /*Get Ip address on defined interface and initialize coap on it with random port number
-     * this port number will be used as a source port in all coap communications*/
-    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
-                sizeof(addr)) == ERR_SUCCESS)
-    {
-        OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
-        paddr = addr;
-    }
-
     /* Initialize OCStack*/
-    if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
+    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
         OC_LOG(ERROR, TAG, "OCStack init error");
         return 0;
     }
+    if (UNICAST_DISCOVERY)
+    {
+        printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
+        if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
+        {
+           //Strip newline char from ipv4addr
+            StripNewLineChar(ipv4addr);
+        }
+        else
+        {
+            OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
+            return OC_STACK_INVALID_PARAM;
+        }
+     }
 
     if(UNICAST_DISCOVERY  == 0  && TEST_CASE == TEST_DISCOVER_DEV_REQ)
     {
index e3a60ec..06059bf 100644 (file)
 #include "logger.h"
 #include "occlientbasicops.h"
 
+static int IPV4_ADDR_SIZE = 16;
 static int UNICAST_DISCOVERY = 0;
 static int TEST_CASE = 0;
 
-static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
+static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:5298/oc/core";
 
 static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
 
@@ -277,7 +278,19 @@ int InitDiscovery()
     char szQueryUri[64] = { 0 };
     if (UNICAST_DISCOVERY)
     {
-        strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
+        char ipv4addr[IPV4_ADDR_SIZE];
+        printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
+        if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
+        {
+            //Strip newline char from ipv4addr
+            StripNewLineChar(ipv4addr);
+            snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
+        }
+        else
+        {
+            OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
+            return OC_STACK_INVALID_PARAM;
+        }
     }
     else
     {
@@ -288,7 +301,7 @@ int InitDiscovery()
     cbData.cd = NULL;
     if (UNICAST_DISCOVERY)
     {
-        ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_CONNTYPE),
+        ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
                 OC_LOW_QOS, &cbData, NULL, 0);
     }
     else
@@ -305,10 +318,6 @@ int InitDiscovery()
 
 int main(int argc, char* argv[])
 {
-    uint8_t addr[20] = {0};
-    uint8_t* paddr = NULL;
-    uint16_t port = USE_RANDOM_PORT;
-    uint8_t ifname[] = "eth0";
     int opt;
 
     while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
@@ -337,18 +346,8 @@ int main(int argc, char* argv[])
         return -1;
     }
 
-
-    /*Get Ip address on defined interface and initialize coap on it with random port number
-     * this port number will be used as a source port in all coap communications*/
-    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
-                sizeof(addr)) == ERR_SUCCESS)
-    {
-        OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
-        paddr = addr;
-    }
-
     /* Initialize OCStack*/
-    if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
+    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
     {
         OC_LOG(ERROR, TAG, "OCStack init error");
         return 0;
index 6752bed..4d694eb 100644 (file)
@@ -98,5 +98,5 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse
 
 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
         OCClientResponse * clientResponse);
-
+void StripNewLineChar(char* str);
 #endif
index 7b27e76..c69caf1 100644 (file)
@@ -332,10 +332,6 @@ int InitDiscovery()
 }
 
 int main(int argc, char* argv[]) {
-    uint8_t addr[20] = {0};
-    uint8_t* paddr = NULL;
-    uint16_t port = USE_RANDOM_PORT;
-    uint8_t ifname[] = "eth0";
     int opt;
 
     while ((opt = getopt(argc, argv, "t:c:")) != -1)
@@ -358,17 +354,9 @@ int main(int argc, char* argv[]) {
         return -1;
     }
 
-    /*Get Ip address on defined interface and initialize coap on it with random port number
-     * this port number will be used as a source port in all coap communications*/
-    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
-                               sizeof(addr)) == ERR_SUCCESS)
-    {
-        OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
-        paddr = addr;
-    }
 
     /* Initialize OCStack*/
-    if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
+    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
         OC_LOG(ERROR, TAG, "OCStack init error");
         return 0;
     }
index 9f12159..8831174 100644 (file)
@@ -31,7 +31,7 @@
 
 static int UNICAST_DISCOVERY = 0;
 static int TEST_CASE = 0;
-static const char * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
+static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:5298/oc/core";
 static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
 static std::string coapServerIP = "255.255.255.255";
 static std::string coapServerPort = "5683";
@@ -41,6 +41,8 @@ static std::string coapServerResource = "/a/led";
 //to be used for sending unicast messages. Default set to WIFI.
 static OCConnectivityType OC_CONNTYPE = OC_WIFI;
 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
+static int IPV4_ADDR_SIZE = 16;
+void StripNewLineChar(char* str);
 
 int gQuitFlag = 0;
 
@@ -187,7 +189,19 @@ int InitDiscovery()
     char szQueryUri[64] = { 0 };
     if (UNICAST_DISCOVERY)
     {
-        strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
+        char ipv4addr[IPV4_ADDR_SIZE];
+        printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
+        if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
+        {
+            //Strip newline char from ipv4addr
+            StripNewLineChar(ipv4addr);
+            snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
+        }
+        else
+        {
+            OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
+            return OC_STACK_INVALID_PARAM;
+        }
     }
     else
     {
@@ -215,10 +229,6 @@ int InitDiscovery()
 
 int main(int argc, char* argv[])
 {
-    uint8_t addr[20] = {0};
-    uint8_t* paddr = NULL;
-    uint16_t port = USE_RANDOM_PORT;
-    uint8_t ifname[] = "eth0";
     int opt;
 
     while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
@@ -247,18 +257,8 @@ int main(int argc, char* argv[])
         return -1;
     }
 
-
-    /*Get Ip address on defined interface and initialize coap on it with random port number
-     * this port number will be used as a source port in all coap communications*/
-    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
-                sizeof(addr)) == ERR_SUCCESS)
-    {
-        OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
-        paddr = addr;
-    }
-
     /* Initialize OCStack*/
-    if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
+    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
     {
         OC_LOG(ERROR, TAG, "OCStack init error");
         return 0;