Merge "Merge branch 'master' into resource-manipulation" into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / occlientcoll.cpp
index 99bd9cd..0f9c40a 100644 (file)
@@ -30,7 +30,7 @@
 const char *getResult(OCStackResult result);
 std::string getIPAddrTBServer(OCClientResponse * clientResponse);
 std::string getPortTBServer(OCClientResponse * clientResponse);
-std::string getQueryStrForGetPut(unsigned  const char * responsePayload);
+std::string getQueryStrForGetPut(const char * responsePayload);
 
 #define TAG PCF("occlient")
 #define DEFAULT_CONTEXT_VALUE 0x99
@@ -57,29 +57,29 @@ unsigned static int TEST = TEST_INVALID;
 
 typedef struct
 {
-    unsigned char text[30];
+    char text[30];
     CLIENT_TEST test;
 } testToTextMap;
 
 testToTextMap queryInterface[] = {
         {"invalid", TEST_INVALID},
-        {"?if=oc.mi.def", TEST_GET_DEFAULT},
-        {"?if=oc.mi.b", TEST_GET_BATCH},
-        {"?if=oc.mi.ll", TEST_GET_LINK_LIST},
-        {"?if=oc.mi.def", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
-        {"?if=oc.mi.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
-        {"?if=oc.mi.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
-        {"?if=oc.mi.def", TEST_PUT_DEFAULT},
-        {"?if=oc.mi.b", TEST_PUT_BATCH},
-        {"?if=oc.mi.ll", TEST_PUT_LINK_LIST},
+        {"?if=oic.if.baseline", TEST_GET_DEFAULT},
+        {"?if=oic.if.b", TEST_GET_BATCH},
+        {"?if=oic.if.ll", TEST_GET_LINK_LIST},
+        {"?if=oic.if.baseline", TEST_UNKNOWN_RESOURCE_GET_DEFAULT},
+        {"?if=oic.if.b", TEST_UNKNOWN_RESOURCE_GET_BATCH},
+        {"?if=oic.if.ll", TEST_UNKNOWN_RESOURCE_GET_LINK_LIST},
+        {"?if=oic.if.baseline", TEST_PUT_DEFAULT},
+        {"?if=oic.if.b", TEST_PUT_BATCH},
+        {"?if=oic.if.ll", TEST_PUT_LINK_LIST},
 };
 
 static std::string putPayload = "{\"state\":\"off\",\"power\":\"0\"}";
 
-//The following variable determines the interface (wifi, ethernet etc.)
-//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";
+//The following variable determines the interface protocol (IP, etc)
+//to be used for sending unicast messages. Default set to IP.
+static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP;
+static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
 
 // The handle for the observe registration
 OCDoHandle gObserveDoHandle;
@@ -107,24 +107,24 @@ int InitDiscovery();
 void PrintUsage()
 {
     OC_LOG(INFO, TAG, "Usage : occlientcoll -t <Test Case> -c <CA connectivity Type>");
-    OC_LOG(INFO, TAG, "-c <0|1> : Send messages over Ethernet or WIFI");
-    OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an"\
+    OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)");
+    OC_LOG(INFO, TAG, "Test Case 1 : Discover Resources && Initiate GET Request on an "\
             "available resource using default interface.");
-    OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 2 : Discover Resources && Initiate GET Request on an "\
                  "available resource using batch interface.");
-    OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 3 : Discover Resources && Initiate GET Request on an "\
                  "available resource using link list interface.");
-    OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 4 : Discover Resources && Initiate GET & PUT Request on an "\
                  "available resource using default interface.");
-    OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 5 : Discover Resources && Initiate GET & PUT Request on an "\
                  "available resource using batch interface.");
-    OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 6 : Discover Resources && Initiate GET & PUT Request on an "\
                  "available resource using link list interface.");
-    OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 7 : Discover Resources && Initiate GET Request on an "\
                  "unavailable resource using default interface.");
-    OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 8 : Discover Resources && Initiate GET Request on an "\
                  "unavailable resource using batch interface.");
-    OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an"\
+    OC_LOG(INFO, TAG, "Test Case 9 : Discover Resources && Initiate GET Request on an "\
                  "unavailable resource using link list interface.");
 }
 
@@ -183,9 +183,6 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse
 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
         OCClientResponse * clientResponse)
 {
-    uint8_t remoteIpAddr[4];
-    uint16_t remotePortNu;
-
     OC_LOG(INFO, TAG,
             "Entering discoveryReqCB (Application Layer CB)");
     OC_LOG_V(INFO, TAG, "StackResult: %s",
@@ -196,14 +193,9 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
         OC_LOG_V(INFO, TAG, "Callback Context recvd successfully");
     }
 
-    OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
-            remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
-    OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
-
     OC_LOG_V(INFO, TAG,
             "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
-            clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
-            remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
+            clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
 
     if(TEST == TEST_UNKNOWN_RESOURCE_GET_DEFAULT || TEST == TEST_UNKNOWN_RESOURCE_GET_BATCH ||\
             TEST == TEST_UNKNOWN_RESOURCE_GET_LINK_LIST)
@@ -222,15 +214,14 @@ int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
 {
     OCStackResult ret;
     OCCallbackData cbData;
-    OCDoHandle handle;
     std::ostringstream getQuery;
-    getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
-            getPortTBServer(clientResponse) << "/SomeUnknownResource";
+    getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
+            clientResponse->devAddr.port << "/SomeUnknownResource";
     cbData.cb = getReqCB;
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
 
-    ret = OCDoResource(&handle, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
+    ret = OCDoResource(NULL, OC_REST_GET, getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
             &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
     {
@@ -246,8 +237,8 @@ int InitObserveRequest(OCClientResponse * clientResponse)
     OCCallbackData cbData;
     OCDoHandle handle;
     std::ostringstream obsReg;
-    obsReg << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
-            getPortTBServer(clientResponse) <<
+    obsReg << "coap://" << clientResponse->devAddr.addr << ":" <<
+            clientResponse->devAddr.addr <<
             getQueryStrForGetPut(clientResponse->resJSONPayload);
     cbData.cb = getReqCB;
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
@@ -272,18 +263,17 @@ int InitPutRequest(OCClientResponse * clientResponse)
 {
     OCStackResult ret;
     OCCallbackData cbData;
-    OCDoHandle handle;
     //* Make a PUT query*/
     std::ostringstream getQuery;
-    getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
-            getPortTBServer(clientResponse) <<
+    getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
+            clientResponse->devAddr.port <<
             "/a/room" << queryInterface[TEST].text;
     cbData.cb = putReqCB;
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
     OC_LOG_V(INFO, TAG, "PUT payload from client = %s ", putPayload.c_str());
 
-    ret = OCDoResource(&handle, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
+    ret = OCDoResource(NULL, OC_REST_PUT, getQuery.str().c_str(), 0, putPayload.c_str(),
                         OC_CONNTYPE, OC_LOW_QOS, &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
     {
@@ -297,19 +287,11 @@ int InitGetRequest(OCClientResponse * clientResponse)
 {
     OCStackResult ret;
     OCCallbackData cbData;
-    OCDoHandle handle;
-
-    uint8_t remoteIpAddr[4];
-    uint16_t remotePortNu;
-
-    OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
-            remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
-    OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
 
     //* Make a GET query*/
     std::ostringstream getQuery;
-    getQuery << "coap://" << getIPAddrTBServer(clientResponse) << ":" <<
-            getPortTBServer(clientResponse) <<
+    getQuery << "coap://" << clientResponse->devAddr.addr << ":" <<
+            clientResponse->devAddr.port <<
             "/a/room" << queryInterface[TEST].text;
 
     std::cout << "Get Query: " << getQuery.str() << std::endl;
@@ -317,7 +299,7 @@ int InitGetRequest(OCClientResponse * clientResponse)
     cbData.cb = getReqCB;
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
-    ret = OCDoResource(&handle, OC_REST_GET,
+    ret = OCDoResource(NULL, OC_REST_GET,
             getQuery.str().c_str(), 0, 0, OC_CONNTYPE, OC_LOW_QOS,
             &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
@@ -331,7 +313,6 @@ int InitDiscovery()
 {
     OCStackResult ret;
     OCCallbackData cbData;
-    OCDoHandle handle;
     /* Start a discovery query*/
     char szQueryUri[64] = { 0 };
 
@@ -340,7 +321,7 @@ int InitDiscovery()
     cbData.cb = discoveryReqCB;
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
-    ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
+    ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT,
                         OC_LOW_QOS,
             &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
@@ -362,7 +343,7 @@ int main(int argc, char* argv[])
                 TEST = atoi(optarg);
                 break;
             case 'c':
-                OC_CONNTYPE = OCConnectivityType(atoi(optarg));
+                OC_CONNTYPE = CT_ADAPTER_IP;
                 break;
             default:
                 PrintUsage();
@@ -407,52 +388,11 @@ int main(int argc, char* argv[])
     return 0;
 }
 
-std::string getIPAddrTBServer(OCClientResponse * clientResponse)
-{
-    if (!clientResponse)
-    {
-        return "";
-    }
-    if (!clientResponse->addr)
-    {
-        return "";
-    }
-    uint8_t a, b, c, d = 0;
-    if (0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d))
-    {
-        return "";
-    }
-
-    char ipaddr[16] = {'\0'};
-    // ostringstream not working correctly here, hence snprintf
-    snprintf(ipaddr,  sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
-    return std::string (ipaddr);
-}
-
-std::string getPortTBServer(OCClientResponse * clientResponse)
-{
-    if (!clientResponse)
-    {
-        return "";
-    }
-    if (!clientResponse->addr)
-    {
-        return "";
-    }
-    uint16_t p = 0;
-    if (0 != OCDevAddrToPort(clientResponse->addr, &p))
-    {
-        return "";
-    }
-    std::ostringstream ss;
-    ss << p;
-    return ss.str();
-}
-
-std::string getQueryStrForGetPut(unsigned  const char * responsePayload)
+std::string getQueryStrForGetPut(const char * responsePayload)
 {
 
-    std::string jsonPayload(reinterpret_cast<char*>(const_cast<unsigned char*>(responsePayload)));
+    std::string jsonPayload(responsePayload);
 
     return "/a/room";
 }
+