Merge "Merge branch 'master' into resource-manipulation" into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / occlientcoll.cpp
index a8875d4..0f9c40a 100644 (file)
@@ -76,9 +76,9 @@ testToTextMap queryInterface[] = {
 
 static std::string putPayload = "{\"state\":\"off\",\"power\":\"0\"}";
 
-//The following variable determines the interface protocol (IPv4, IPv6, etc)
-//to be used for sending unicast messages. Default set to IPv4.
-static OCConnectivityType OC_CONNTYPE = OC_IPV4;
+//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
@@ -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)
@@ -223,8 +215,8 @@ int InitGetRequestToUnavailableResource(OCClientResponse * clientResponse)
     OCStackResult ret;
     OCCallbackData cbData;
     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;
@@ -245,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;
@@ -273,8 +265,8 @@ int InitPutRequest(OCClientResponse * clientResponse)
     OCCallbackData cbData;
     //* 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;
@@ -296,17 +288,10 @@ int InitGetRequest(OCClientResponse * clientResponse)
     OCStackResult ret;
     OCCallbackData cbData;
 
-    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;
@@ -336,7 +321,7 @@ int InitDiscovery()
     cbData.cb = discoveryReqCB;
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
-    ret = OCDoResource(NULL, 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)
@@ -358,9 +343,7 @@ int main(int argc, char* argv[])
                 TEST = atoi(optarg);
                 break;
             case 'c':
-                // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
-                // OC_CONNTYPE = OCConnectivityType(atoi(optarg));
-                OC_CONNTYPE = OC_IPV4;
+                OC_CONNTYPE = CT_ADAPTER_IP;
                 break;
             default:
                 PrintUsage();
@@ -405,48 +388,6 @@ 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(const char * responsePayload)
 {