replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationprovider.c
index 7deb1d3..11c1c36 100644 (file)
 #include "cloud_connector.h"
 
 // Input the following values to publish resource to cloud
-char REMOTE_SERVER_ADDRESS[50];
-char AUTH_PROVIDER[50];
-char AUTH_CODE[50];
-char UID[50];
-char ACCESS_TOKEN[50];
+char REMOTE_SERVER_ADDRESS[50] = {'\0',};
+char AUTH_PROVIDER[50] = {'\0',};
+char AUTH_CODE[50] = {'\0',};
+char UID[50] = {'\0',};
+char ACCESS_TOKEN[50] = {'\0',};
+#endif
+
+#ifdef WITH_MQ
+char CLOUD_ADDRESS[100] = "10.113.64.52:5686";
+char CLOUD_TOPIC[100] = "/oic/ps/notification";
 #endif
 
 char mainConsumer[37] = {'\0',};
@@ -135,6 +140,17 @@ void removeTopics(NSTopicLL * topics)
     }
 }
 
+void input(char * buffer)
+{
+    char ch;
+    int i = 0;
+
+    while( (ch = getchar()) != '\n' && i < 100)
+        buffer[i++] = ch;
+
+    buffer[i] = '\0';
+}
+
 int main()
 {
     int num;
@@ -169,7 +185,8 @@ int main()
         printf("8.  NSProviderUnselectTopic(); \n");
         printf("9.  NSProviderGetConsumerTopics(); \n");
         printf("10. NSProviderGetTopics(); \n");
-        printf("11. NSStopProvider() \n");
+        printf("11. NSProviderSubscribeMQService() \n");
+        printf("0. NSStopProvider() \n");
 #ifdef WITH_CLOUD
         printf("21. NSProviderEnableRemoteService (after login) \n");
         printf("22. NSProviderDisableRemoteService (after login) \n");
@@ -182,10 +199,19 @@ int main()
 
         printf("input : ");
 
-        scanf("%d", &num);
-        fflush(stdin);
-        scanf("%c", &dummy);
-        fflush(stdin);
+        if(scanf("%d", &num) > 0)
+        {
+            if(scanf("%c", &dummy) > 0)
+            {
+                fflush(stdin);
+                printf("\n");
+            }
+        }
+        else
+        {
+            printf("invalid input \n");
+            num = 0;
+        }
 
         switch (num)
         {
@@ -193,7 +219,7 @@ int main()
             {
                 printf("NSStartProvider(Accepter: Provider)");
                 NSProviderConfig config;
-                config.policy = true;
+                config.subControllability = true;
                 config.subRequestCallback = subscribeRequestCallback;
                 config.syncInfoCallback = syncCallback;
                 config.userInfo = OICStrdup("OCF_NOTIFICATION");
@@ -205,7 +231,7 @@ int main()
             {
                 printf("NSStartProvider(Accepter: Consumer)");
                 NSProviderConfig config;
-                config.policy = false;
+                config.subControllability = false;
                 config.subRequestCallback = subscribeRequestCallback;
                 config.syncInfoCallback = syncCallback;
                 config.userInfo = OICStrdup("OCF_NOTIFICATION");
@@ -216,36 +242,40 @@ int main()
             case 3:
             {
                 printf("NSSendNotification()");
-                char title[100];
-                char body[100];
-                char topic[100];
+                char title[100] = {'\0',};
+                char body[100] = {'\0',};
+                char topic[100] = {'\0',};
 
                 printf("id : %d\n", ++id);
                 printf("title : ");
-                fgets(title, sizeof(title), stdin);
+                input(title);
 
                 printf("body : ");
-                fgets(body, sizeof(body), stdin);
+                input(body);
 
                 printf("topic : ");
-                fgets(topic, sizeof(topic), stdin);
+
+                input(topic);
 
                 printf("app - mTitle : %s \n", title);
                 printf("app - mContentText : %s \n", body);
                 printf("app - topic : %s \n", topic);
 
                 NSMessage * msg = NSCreateMessage();
+                if(msg)
+                {
 
-                msg->title = OICStrdup(title);
-                msg->contentText = OICStrdup(body);
-                msg->sourceName = OICStrdup("OCF");
+                    msg->title = OICStrdup(title);
+                    msg->contentText = OICStrdup(body);
+                    msg->sourceName = OICStrdup("OCF");
 
-                if(topic[0] != '\0')
-                {
-                    msg->topic = OICStrdup(topic);
-                }
+                    if(topic[0] != '\0')
+                    {
+                        msg->topic = OICStrdup(topic);
+                    }
 
-                NSSendMessage(msg);
+                    NSSendMessage(msg);
+                }
             }
                 break;
 
@@ -289,7 +319,7 @@ int main()
                 break;
 
             case 10:
-                printf("NSProviderGetConsumerTopics\n");
+                printf("NSProviderGetTopics\n");
                 {
                     NSTopicLL * topics = NSProviderGetTopics();
                     printTopics(topics);
@@ -297,25 +327,36 @@ int main()
                 }
                 break;
 
+#if WITH_MQ
             case 11:
-                NSStopProvider();
+                printf("11. Subscribe MQ Service\n");
+
+                printf("Remote Server Address: ");
+                //input(CLOUD_ADDRESS);
+
+                printf("Topic[notificationtest]: ");
+                //input(CLOUD_TOPIC);
+
+                NSProviderSubscribeMQService(CLOUD_ADDRESS, CLOUD_TOPIC);
                 break;
+#endif
+
 #ifdef WITH_CLOUD
             case 21:
-                printf("Enable Remote Service");
+                printf("Enable Remote Service\n");
                 if(!IsCloudLoggedin())
                 {
-                    printf("Login required");
+                    printf("Login required\n");
                     break;
                 }
-                NSProviderEnableRemoteService(REMOTE_SERVER_ADDRESS);                
+                NSProviderEnableRemoteService(REMOTE_SERVER_ADDRESS);
                 break;
 
             case 22:
-                printf("Disable Remote Service");
+                printf("Disable Remote Service\n");
                 if(!IsCloudLoggedin())
                 {
-                    printf("Login required");
+                    printf("Login required\n");
                     break;
                 }
                 NSProviderDisableRemoteService(REMOTE_SERVER_ADDRESS);
@@ -323,13 +364,13 @@ int main()
 
             case 31:
                 printf("Remote Server Address: ");
-                gets(REMOTE_SERVER_ADDRESS);
+                input(REMOTE_SERVER_ADDRESS);
 
                 printf("Auth Provider(eg. github): ");
-                gets(AUTH_PROVIDER);
+                input(AUTH_PROVIDER);
 
                 printf("Auth Code: ");
-                gets(AUTH_CODE);
+                input(AUTH_CODE);
 
                 OCCloudSignup(REMOTE_SERVER_ADDRESS, OCGetServerInstanceIDString(),
                     AUTH_PROVIDER, AUTH_CODE, CloudSignupCallback);
@@ -337,21 +378,21 @@ int main()
                 break;
             case 32:
                 printf("Remote Server Address: ");
-                gets(REMOTE_SERVER_ADDRESS);
+                input(REMOTE_SERVER_ADDRESS);
 
                 printf("UID: ");
-                gets(UID);
+                input(UID);
 
                 printf("ACCESS_TOKEN: ");
-                gets(ACCESS_TOKEN);
+                input(ACCESS_TOKEN);
 
                 OCCloudLogin(REMOTE_SERVER_ADDRESS, UID, OCGetServerInstanceIDString(),
                     ACCESS_TOKEN, CloudLoginoutCallback);
-                printf("OCCloudLogin requested");
+                printf("OCCloudLogin requested\n");
                 break;
             case 33:
                 OCCloudLogout(REMOTE_SERVER_ADDRESS, CloudLoginoutCallback);
-                printf("OCCloudLogin requested");
+                printf("OCCloudLogin requested\n");
                 break;
 #endif
             case 0: