replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / tizen / SimpleClientServer / occlient.cpp
index d690cd4..6736a69 100644 (file)
@@ -20,6 +20,7 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include <stdio.h>
+#include <glib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
@@ -51,10 +52,15 @@ static int g_unicastDiscovery = 0;
 static int g_testCase = 0;
 static int g_connectivity = 0;
 
+static GMainLoop *g_mainloop = NULL;
+pthread_t g_thread;
+
 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;
@@ -106,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";
@@ -157,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)
     {
@@ -232,7 +242,7 @@ OCStackApplicationResult deleteReqCB(void *ctx,
     if(clientResponse)
     {
         cout << "\nStackResult: " << getResult(clientResponse->result);
-        //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+        //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
     }
     else
     {
@@ -257,7 +267,7 @@ OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
 
     cout << "\nStackResult: " << getResult(clientResponse->result);
     cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
-    //OC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
+    //OIC_LOG_PAYLOAD(INFO, TAG, clientResponse->payload);
 
     if (clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
     {
@@ -285,41 +295,42 @@ OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
 
     if (clientResponse)
     {
-        cout << "\nStackResult: " << getResult(clientResponse->result);
-        cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
-        cout << "\nCallback Context for OBSERVE notification recvd successfully ";
-        //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
-        gNumObserveNotifies++;
-        if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
+        if (clientResponse->sequenceNumber <= MAX_SEQUENCE_NUMBER)
         {
-            if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
+            if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
             {
-                if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
-                {
-                    cout << "\nObserve cancel error";
-                }
-                return OC_STACK_DELETE_TRANSACTION;
+                cout << "This also serves as a registration confirmation" << endl;
             }
-            else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
+
+            cout << "\nStackResult: " << getResult(clientResponse->result);
+            cout << "\nSEQUENCE NUMBER: " << clientResponse->sequenceNumber;
+            cout << "\nCallback Context for OBSERVE notification recvd successfully ";
+            //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
+            gNumObserveNotifies++;
+
+            if (gNumObserveNotifies == 15) //large number to test observing in DELETE case.
             {
-                if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
+                if (g_testCase == TEST_OBS_REQ_NON || g_testCase == TEST_OBS_REQ_CON)
+                {
+                    if (OCCancel(gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
+                    {
+                        cout << "Observe cancel error" << endl;
+                    }
+                    return OC_STACK_DELETE_TRANSACTION;
+                }
+                else if (g_testCase == TEST_OBS_REQ_NON_CANCEL_IMM)
                 {
-                    cout << "\nObserve cancel error";
+                    if (OCCancel(gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
+                    {
+                        cout << "\nObserve cancel error";
+                    }
                 }
             }
         }
-        if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
-        {
-            cout << "\nThis also serves as a registration confirmation";
-        }
-        else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
-        {
-            cout << "\nThis also serves as a deregistration confirmation";
-            return OC_STACK_DELETE_TRANSACTION;
-        }
-        else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
+        else
         {
-            cout << "\nThis also tells you that registration/deregistration failed";
+            OIC_LOG(INFO, TAG, "No observe option header is returned in the response.");
+            OIC_LOG(INFO, TAG, "For a registration request, it means the registration failed");
             return OC_STACK_DELETE_TRANSACTION;
         }
     }
@@ -343,7 +354,7 @@ OCStackApplicationResult presenceCB(void* ctx, OCDoHandle /*handle*/,
         cout << "\nStackResult: " << getResult(clientResponse->result);
         cout << "\nNONCE NUMBER: " << clientResponse->sequenceNumber;
         cout << "\nCallback Context for Presence notification recvd successfully ";
-        //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+        //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
         gNumPresenceNotifies++;
         if (gNumPresenceNotifies == 20)
         {
@@ -385,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;
         }
-        //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+        //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:
@@ -471,8 +527,8 @@ OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx,
 
     if (clientResponse)
     {
-        //OC_LOG truncates the response as it is too long.
-        //OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+        //OIC_LOG truncates the response as it is too long.
+        //OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
     }
     else
     {
@@ -492,7 +548,7 @@ OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
 
     if (clientResponse)
     {
-        //OC_LOG truncates the response as it is too long.
+        //OIC_LOG truncates the response as it is too long.
         cout << "\nDiscovery response: ";
         cout << clientResponse->payload;
     }
@@ -715,7 +771,7 @@ int InitPlatformDiscovery(OCQualityOfService qos)
 
     OCStackResult ret;
     OCCallbackData cbData;
-    char szQueryUri[64] = { 0 };
+    char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
 
     snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, g_discoveryAddr);
 
@@ -723,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";
@@ -740,7 +796,7 @@ int InitDeviceDiscovery(OCQualityOfService qos)
 
     OCStackResult ret;
     OCCallbackData cbData;
-    char szQueryUri[100] = { 0 };
+    char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
 
     snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, g_discoveryAddr);
 
@@ -748,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";
@@ -763,7 +819,7 @@ int InitDiscovery(OCQualityOfService qos)
 {
     OCStackResult ret;
     OCCallbackData cbData;
-    char szQueryUri[100] = { 0 };
+    char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
 
     snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, g_discoveryAddr);
 
@@ -771,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";
@@ -781,10 +837,57 @@ int InitDiscovery(OCQualityOfService qos)
     return ret;
 }
 
+void *GMainLoopThread(void *param)
+{
+
+    if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
+    {
+        InitDeviceDiscovery(OC_LOW_QOS);
+    }
+    else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ)
+    {
+        InitPlatformDiscovery(OC_LOW_QOS);
+    }
+    else
+    {
+        InitDiscovery(OC_LOW_QOS);
+    }
+
+    while (!gQuitFlag)
+    {
+        if (OCProcess() != OC_STACK_OK)
+        {
+            cout << "\nOCStack process error";
+            return NULL;
+        }
+#ifndef ROUTING_GATEWAY
+        sleep(1);
+#endif
+    }
+
+    if (g_mainloop)
+    {
+        g_main_loop_quit(g_mainloop);
+    }
+    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;
 
+    g_mainloop = g_main_loop_new(NULL, FALSE);
+    if(!g_mainloop)
+    {
+        printf("g_main_loop_new failed\n");
+        return 0;
+    }
+
     while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
     {
         switch(opt)
@@ -814,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;
@@ -829,7 +935,7 @@ int main(int argc, char* argv[])
     {
         if (OCProcess() != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "OCStack process error");
+            OIC_LOG(ERROR, TAG, "OCStack process error");
             return 0;
         }
         usleep(SLEEP_DURATION);
@@ -845,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...";
@@ -872,35 +983,20 @@ int main(int argc, char* argv[])
         }
     }
 
-    if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ)
-    {
-        InitDeviceDiscovery(OC_LOW_QOS);
-    }
-    else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ)
-    {
-        InitPlatformDiscovery(OC_LOW_QOS);
-    }
-    else
-    {
-        InitDiscovery(OC_LOW_QOS);
-    }
 
     // Break from loop with Ctrl+C
-    OC_LOG(INFO, TAG, "Entering occlient main loop...");
+    OIC_LOG(INFO, TAG, "Entering occlient main loop...");
     signal(SIGINT, handleSigInt);
-    while (!gQuitFlag)
-    {
 
-        if (OCProcess() != OC_STACK_OK)
-        {
-            cout << "\nOCStack process error\n";
-            return 0;
-        }
-#ifndef ROUTING_GATEWAY
-        sleep(1);
-#endif
+    int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL);
+    if (result < 0)
+    {
+        printf("pthread_create failed in initialize\n");
+        return 0;
     }
 
+    g_main_loop_run(g_mainloop);
+
     cout << "\nExiting occlient main loop...\n";
 
     if (OCStop() != OC_STACK_OK)