replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / tizen / SimpleClientServer / occlient.cpp
index fe1f95f..6736a69 100644 (file)
@@ -59,6 +59,8 @@ static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d";
 static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p";
 static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res";
 
+static const char *DEFAULT_DB_FILE_PATH = "/opt/usr/etc/oic_svr_db_client.dat";
+
 //The following variable determines the interface protocol (IPv4, IPv6, etc)
 //to be used for sending unicast messages. Default set to IP dual stack.
 static OCConnectivityType g_connType = CT_ADAPTER_IP;
@@ -110,11 +112,12 @@ OCPayload* putPayload()
 static void PrintUsage()
 {
     cout << "Hello";
-    cout << "\nUsage : occlient -u <0|1> -t <1..17> -c <0|1|2>";
+    cout << "\nUsage : occlient -u <0|1> -t <1..17> -c <0|1|2|3>";
     cout << "\n-u <0|1> : Perform multicast/unicast discovery of resources";
     cout << "\n-c 0 : Default IP selection";
     cout << "\n-c 1 : IP Connectivity Type";
     cout << "\n-c 2 : EDR Connectivity Type (IPv6 not currently supported)";
+    cout << "\n-c 3 : TCP Connectivity Type";
     cout << "\n-t 1  :  Discover Resources";
     cout << "\n-t 2  :  Discover Resources and Initiate Nonconfirmable Get Request";
     cout << "\n-t 3  :  Discover Resources and Initiate Nonconfirmable Get Request with query filter";
@@ -161,9 +164,12 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query,
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
 
-    ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
-                       (method == OC_REST_PUT) ? putPayload() : NULL,
-                       (g_connType), qos, &cbData, options, numOptions);
+    OCPayload* payload = (method == OC_REST_PUT) ? putPayload() : NULL;
+
+    ret = OCDoRequest(&handle, method, query.str().c_str(), remoteAddr,
+                      payload, (g_connType), qos, &cbData, options, numOptions);
+
+    OCPayloadDestroy(payload);
 
     if (ret != OC_STACK_OK)
     {
@@ -390,16 +396,61 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
         cout << "\nDiscovered on " << connectionType.c_str();
         cout << "\nDevice ======> Discovered ";
         cout << clientResponse->devAddr.addr;
-        if (CT_ADAPTER_IP == clientResponse->connType)
+        if (CT_ADAPTER_IP == (clientResponse->connType & CT_MASK_ADAPTER))
         {
             cout << ":" << clientResponse->devAddr.port;
         }
         //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
         cout << "\nConnectivity type: " << clientResponse->connType;
-        g_connType = clientResponse->connType;
         g_serverAddr = clientResponse->devAddr;
         parseClientResponse(clientResponse);
 
+        OCDiscoveryPayload *payload = (OCDiscoveryPayload*) clientResponse->payload;
+        if (!payload)
+        {
+            cout << "\nDiscovery payload is empty!";
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
+        int targetResourceFound = 0;
+        while(resource)
+        {
+            cout << "\nFound resource: " << resource->uri;
+            if (resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0)
+            {
+                cout << "\nLight resource found.";
+                targetResourceFound = 1;
+
+                if (g_testCase >= TEST_OBS_PRESENCE) break;
+
+                if (resource->secure && resource->port)
+                {
+                    cout << "\nLight resource with secure flag";
+                    g_serverAddr.flags = (OCTransportFlags) (clientResponse->devAddr.flags | OC_SECURE);
+                    g_serverAddr.port = resource->port;
+                }
+
+#ifdef WITH_TCP
+                if(g_connType == CT_ADAPTER_TCP && resource->tcpPort)
+                {
+                    cout << "\nRequest will be send with TCP";
+                    g_serverAddr.port = resource->tcpPort;
+                    g_serverAddr.adapter = OC_ADAPTER_TCP;
+                }
+#endif
+
+                break;
+            }
+            resource = resource->next;
+        }
+
+        if (!targetResourceFound)
+        {
+            OIC_LOG_V(INFO, TAG, "No %s in payload", coapServerResource.c_str());
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
         switch(g_testCase)
         {
             case TEST_GET_REQ_NON:
@@ -728,9 +779,9 @@ int InitPlatformDiscovery(OCQualityOfService qos)
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
 
-    ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
-                       (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
-                       &cbData, NULL, 0);
+    ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
+                      (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
+                      &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
     {
         cout << "\nOCStack device error";
@@ -753,9 +804,9 @@ int InitDeviceDiscovery(OCQualityOfService qos)
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
 
-    ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
-                       (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
-                       &cbData, NULL, 0);
+    ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
+                      (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
+                      &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
     {
         cout << "\nOCStack device error";
@@ -776,9 +827,9 @@ int InitDiscovery(OCQualityOfService qos)
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
 
-    ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
-                       (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
-                       &cbData, NULL, 0);
+    ret = OCDoRequest(NULL, OC_REST_DISCOVER, szQueryUri, NULL, 0, CT_DEFAULT,
+                      (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
+                      &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
     {
         cout << "\nOCStack resource error";
@@ -821,6 +872,11 @@ void *GMainLoopThread(void *param)
     return NULL;
 }
 
+static FILE *client_fopen(const char */*path*/, const char *mode)
+{
+    return fopen(DEFAULT_DB_FILE_PATH, mode);
+}
+
 int main(int argc, char* argv[])
 {
     int opt;
@@ -861,7 +917,10 @@ int main(int argc, char* argv[])
 
     cout << "\nEntering occlient main loop...\n";
 
-    if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
+    OCPersistentStorage ps{ client_fopen, fread, fwrite, fclose, unlink };
+    OCRegisterPersistentStorageHandler(&ps);
+
+    if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
     {
         cout << "\nOCStack init error";
         return 0;
@@ -892,6 +951,11 @@ int main(int argc, char* argv[])
         cout << "\nSelected EDR Adapter\n";
         g_connType = CT_ADAPTER_RFCOMM_BTEDR;
     }
+    else if(g_connectivity == CT_TCP)
+    {
+        cout << "\nSelected TCP Adapter\n";
+        g_connType = CT_ADAPTER_TCP;
+    }
     else
     {
         cout << "\nDefault Connectivity type selected...";