replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationconsumer.c
index 83393e6..8c14cd3 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <stdio.h>
 #include <unistd.h>
+#include <string.h>
 #include "pthread.h"
 
 #include "ocstack.h"
 
 #ifdef WITH_CLOUD
 #include "NSConstants.h"
-#include "NSConsumerCommon.h"
-#include "cloud_connector.h"
 #include "oic_malloc.h"
+#include "cloud_connector.h"
 
 #define CLOUD_CONTEXT_VALUE 0x99
-#define CLOUD_PRESENCE_SUBSCRIBE_QUERY ""          // refer to IoTivity Cloud Module Sample
 
-#define CLOUD_HOST_ADDRESS ""                      // refer to IoTivity Cloud Module Sample
-#define CLOUD_IOTIVITYNS_SESSION ""                // refer to IoTivity Cloud Module Sample
+char CLOUD_ADDRESS[100];
+char CLOUD_TOPIC[100];
+char CLOUD_AUTH_PROVIDER[100];
+char CLOUD_AUTH_CODE[100];
+char CLOUD_UID[100];
+char CLOUD_ACCESS_TOKEN[100];
 #endif
 
-void onDiscoverNotification(NSProvider * provider)
+
+NSProvider * g_provider = NULL;
+NSTopicLL * g_topicLL = NULL;
+
+FILE* server_fopen(const char *path, const char *mode)
+{
+    (void)path;
+    return fopen("oic_ns_provider_db.dat", mode);
+}
+
+void printProviderTopicList(NSTopicLL * topics)
 {
-    printf("notification resource discovered\n");
-    printf("subscribe result %d\n", NSSubscribe(provider));
-    printf("startSubscribing\n");
+    printf("printProviderTopicList\n");
+    if (topics)
+    {
+        NSTopicLL * iter = topics;
+        while (iter)
+        {
+            printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
+            iter = iter->next;
+        }
+    }
 }
 
-void onSubscriptionAccepted(NSProvider * provider)
+void onProviderChanged(NSProvider * provider, NSProviderState response)
 {
-    printf("Subscription accepted\n");
+    printf("Provider changed: %d\n", response);
     printf("subscribed provider Id : %s\n", provider->providerId);
+
+    if (response == NS_DISCOVERED)
+    {
+        printf("notification resource discovered\n");
+        printf("subscribe result %d\n", NSSubscribe(provider->providerId));
+        printf("startSubscribing\n");
+    }
+
+    else if (response == NS_TOPIC)
+    {
+        printf ("Provider Topic Updated\n");
+        g_topicLL = NSConsumerGetTopicList(provider->providerId);
+        printProviderTopicList(g_topicLL);
+        g_provider = provider;
+    }
 }
 
 void onNotificationPosted(NSMessage * notification)
@@ -58,6 +93,10 @@ void onNotificationPosted(NSMessage * notification)
     printf("title : %s\n", notification->title);
     printf("content : %s\n", notification->contentText);
     printf("source : %s\n", notification->sourceName);
+    if (notification->topic && strlen(notification->topic) > 0)
+    {
+        printf("topic : %s\n", notification->topic);
+    }
     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
 }
 
@@ -67,47 +106,6 @@ void onNotificationSync(NSSyncInfo * sync)
     printf("Sync STATE : %d\n", sync->state);
 }
 
-
-#ifdef WITH_CLOUD
-OCStackApplicationResult handleLoginoutCB(void *ctx,
-        OCDoHandle handle,
-        OCClientResponse *clientResponse)
-{
-    (void)handle;
-    if (ctx != (void *)CLOUD_CONTEXT_VALUE)
-    {
-        NS_LOG(DEBUG, "Invalid Login/out callback received");
-    }
-
-    NS_LOG(DEBUG, "Login/out response received");
-
-    if (clientResponse->payload != NULL &&
-        clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
-    {
-        NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
-
-        OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
-
-        while (val)
-        {
-            val = val->next;
-        }
-        NS_LOG(DEBUG, "Get payload values");
-
-        OCDevAddr * addr = NULL;
-        addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
-        memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
-
-        NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
-
-        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
-        NSConsumerPushEvent(task);
-    }
-
-    return OC_STACK_KEEP_TRANSACTION;
-}
-#endif
-
 void* OCProcessThread(void * ptr)
 {
     (void) ptr;
@@ -125,30 +123,39 @@ void* OCProcessThread(void * ptr)
     return NULL;
 }
 
+void input(char * buffer)
+{
+    char ch;
+    int i = 0;
+
+    while( (ch = getchar()) != '\n' && i < 100)
+        buffer[i++] = ch;
+
+    buffer[i] = '\0';
+}
+
 int main(void)
 {
     bool isExit = false;
     pthread_t OCThread = NULL;
 
     printf("start Iotivity\n");
-    if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
+
+    // open oic_db
+    static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
+    OCRegisterPersistentStorageHandler(&ps);
+
+    if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
     {
         printf("OCInit fail\n");
         return 0;
     }
 
     NSConsumerConfig cfg;
-    cfg.discoverCb = onDiscoverNotification;
-    cfg.acceptedCb = onSubscriptionAccepted;
+    cfg.changedCb = onProviderChanged;
     cfg.messageCb = onNotificationPosted;
     cfg.syncInfoCb = onNotificationSync;
 
-#ifdef WITH_CLOUD
-    NS_LOG(DEBUG, "process OCCloudLogin...");
-    OCCloudLogin(CLOUD_HOST_ADDRESS, CLOUD_IOTIVITYNS_SESSION, handleLoginoutCB);
-    NS_LOG(DEBUG, "OCCloudLogin return");
-#endif
-
     pthread_create(&OCThread, NULL, OCProcessThread, NULL);
 
     printf("start notification consumer service\n");
@@ -159,13 +166,32 @@ int main(void)
 
         printf("1. Start Consumer\n");
         printf("2. Stop Consumer\n");
-        printf("5. Exit\n");
+        printf("3. Get Topics\n");
+        printf("4. Select Topics\n");
+        printf("5. Cancel select Topics\n");
+        printf("0. Exit\n");
+#ifdef WITH_MQ
+        printf("11. Subscribe MQ Service\n");
+#endif
+#ifdef WITH_CLOUD
+        printf("21. Enable Remote Service (after login)\n");
+        printf("31. Cloud Signup\n");
+        printf("32. Cloud Login\n");
+        printf("33. Cloud Logout\n");
+#endif
 
         printf("Input: ");
 
-        scanf("%d", &num);
+        if(scanf("%d", &num) == EOF)
+        {
+            printf("Fail to input num\n");
+        }
         fflush(stdin);
-        scanf("%c", &dummy);
+
+        if(scanf("%c", &dummy) == EOF)
+        {
+            printf("Fail to input dummy\n");
+        }
         fflush(stdin);
 
         switch (num)
@@ -178,14 +204,116 @@ int main(void)
                 printf("2. Stop Consumer");
                 NSStopConsumer();
                 break;
+            case 3:
+                printf("3. Get Topics\n");
+                if(g_provider)
+                {
+                    g_topicLL = NSConsumerGetTopicList(g_provider->providerId);
+                    printProviderTopicList(g_topicLL);
+                }
+                break;
+            case 4:
+                printf("4. Select Topics\n");
+
+                if (g_provider && g_topicLL)
+                {
+                    NSTopicLL * iter = g_topicLL;
+                    int i = 0;
+                    while (iter)
+                    {
+                        iter->state = (i++)%2;
+                        iter = iter->next;
+                    }
+                    NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
+                    if (ret == NS_OK)
+                    {
+                        printProviderTopicList(g_topicLL);
+                    }
+                    else
+                    {
+                        printf("Update fail\n");
+                    }
+                }
+                break;
             case 5:
-                printf("5. Exit");
+                printf("5. Cancel select Topics\n");
+                NSTopicLL * iter = g_topicLL;
+                while (iter)
+                {
+                    iter->state = NS_TOPIC_UNSUBSCRIBED;
+                    iter = iter->next;
+                }
+
+                NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
+                if (ret != NS_OK)
+                {
+                    printf("Cancel select topic fail\n");
+                }
+                break;
+            case 0:
+                printf("0. Exit");
                 isExit = true;
                 break;
+#if WITH_MQ
+            case 11:
+                printf("11. Subscribe MQ Service\n");
+
+                printf("Remote Server Address: ");
+                input(CLOUD_ADDRESS);
+
+                printf("Topic[notification]: ");
+                input(CLOUD_TOPIC);
+
+                NSConsumerSubscribeMQService(CLOUD_ADDRESS, CLOUD_TOPIC);
+                break;
+#endif
+#ifdef WITH_CLOUD
+            case 21:
+                printf("Enable Remote Service");
+                if(!IsCloudLoggedin())
+                {
+                    printf("Cloud Login required");
+                    break;
+                }
+                NSConsumerEnableRemoteService(CLOUD_ADDRESS);
+                break;
+            case 31:
+                printf("Remote Server Address: ");
+                input(CLOUD_ADDRESS);
+
+                printf("Auth Provider(eg. github): ");
+                input(CLOUD_AUTH_PROVIDER);
+
+                printf("Auth Code: ");
+                input(CLOUD_AUTH_CODE);
+
+                OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
+                    CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
+                printf("OCCloudSignup requested");
+                break;
+            case 32:
+                printf("Remote Server Address: ");
+                input(CLOUD_ADDRESS);
+
+                printf("UID: ");
+                input(CLOUD_UID);
+
+                printf("ACCESS_TOKEN: ");
+                input(CLOUD_ACCESS_TOKEN);
+
+                OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
+                    CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
+                printf("OCCloudLogin requested");
+                break;
+            case 33:
+                OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
+                printf("OCCloudLogin requested");
+                break;
+#endif
             default:
                 break;
         }
     }
-
     return 0;
 }
+