Update consumer logic for stopped provider.
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationconsumer.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include "pthread.h"
25
26 #include "ocstack.h"
27 #include "NSCommon.h"
28 #include "NSConsumerInterface.h"
29
30 #ifdef WITH_CLOUD
31 #include "NSConstants.h"
32 #include "oic_malloc.h"
33 #include "cloud_connector.h"
34
35 #define CLOUD_CONTEXT_VALUE 0x99
36
37 char CLOUD_ADDRESS[50];
38 char CLOUD_AUTH_PROVIDER[50];
39 char CLOUD_AUTH_CODE[50];
40 char CLOUD_UID[50];
41 char CLOUD_ACCESS_TOKEN[50];
42 #endif
43
44
45 NSProvider * g_provider = NULL;
46
47 FILE* server_fopen(const char *path, const char *mode)
48 {
49     (void)path;
50     return fopen("oic_ns_provider_db.dat", mode);
51 }
52
53 void onDiscoverNotification(NSProvider * provider)
54 {
55     printf("notification resource discovered\n");
56     printf("subscribe result %d\n", NSSubscribe(provider));
57     printf("startSubscribing\n");
58 }
59
60 void printProviderTopicList(NSProvider *provider)
61 {
62     printf("printProviderTopicList\n");
63     if (provider->topicLL)
64     {
65         NSTopicLL * iter = provider->topicLL;
66         while (iter)
67         {
68             printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
69             iter = iter->next;
70         }
71     }
72 }
73
74 void onProviderChanged(NSProvider * provider, NSProviderState response)
75 {
76     printf("Provider changed: %d\n", response);
77     printf("subscribed provider Id : %s\n", provider->providerId);
78
79     if (response == NS_TOPIC)
80     {
81         printf ("Provider Topic Updated\n");
82
83         printProviderTopicList(provider);
84         g_provider = provider;
85     }
86 }
87
88 void onNotificationPosted(NSMessage * notification)
89 {
90     printf("id : %lld\n", (long long int)notification->messageId);
91     printf("title : %s\n", notification->title);
92     printf("content : %s\n", notification->contentText);
93     printf("source : %s\n", notification->sourceName);
94     if (notification->topic && strlen(notification->topic) > 0)
95     {
96         printf("topic : %s\n", notification->topic);
97     }
98     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
99 }
100
101 void onNotificationSync(NSSyncInfo * sync)
102 {
103     printf("Sync ID : %lld\n", (long long int)sync->messageId);
104     printf("Sync STATE : %d\n", sync->state);
105 }
106
107 void* OCProcessThread(void * ptr)
108 {
109     (void) ptr;
110
111     while (true)
112     {
113         usleep(2000);
114         if(OCProcess() != OC_STACK_OK)
115         {
116             OCStop();
117             break;
118         }
119     }
120
121     return NULL;
122 }
123
124 int main(void)
125 {
126     bool isExit = false;
127     pthread_t OCThread = NULL;
128
129     printf("start Iotivity\n");
130
131     // open oic_db
132     static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
133     OCRegisterPersistentStorageHandler(&ps);
134
135     if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
136     {
137         printf("OCInit fail\n");
138         return 0;
139     }
140
141     NSConsumerConfig cfg;
142     cfg.changedCb = onProviderChanged;
143     cfg.messageCb = onNotificationPosted;
144     cfg.syncInfoCb = onNotificationSync;
145
146     pthread_create(&OCThread, NULL, OCProcessThread, NULL);
147
148     printf("start notification consumer service\n");
149     while (!isExit)
150     {
151         int num = 0;
152         char dummy = '\0';
153
154         printf("1. Start Consumer\n");
155         printf("2. Stop Consumer\n");
156         printf("3. Get Topics\n");
157         printf("4. Select Topics\n");
158         printf("5. Exit\n");
159 #ifdef WITH_CLOUD
160         printf("21. Enable Remote Service (after login)\n");
161         printf("31. Cloud Signup\n");
162         printf("32. Cloud Login\n");
163         printf("33. Cloud Logout\n");
164 #endif
165
166         printf("Input: ");
167
168         scanf("%d", &num);
169         fflush(stdin);
170         scanf("%c", &dummy);
171         fflush(stdin);
172
173         switch (num)
174         {
175             case 1:
176                 printf("1. Start Consumer\n");
177                 NSStartConsumer(cfg);
178                 break;
179             case 2:
180                 printf("2. Stop Consumer");
181                 NSStopConsumer();
182                 break;
183             case 3:
184                 printf("3. Get Topics\n");
185                 if(g_provider)
186                 {
187                     NSConsumerGetInterestTopics(g_provider);
188                 }
189                 break;
190             case 4:
191                 printf("4. Select Topics\n");
192
193                 if (g_provider && g_provider->topicLL)
194                 {
195                     NSTopicLL * iter = g_provider->topicLL;
196                     int i = 0;
197                     while (iter)
198                     {
199                         iter->state = (i++)%2;
200                         printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
201                         iter = iter->next;
202                     }
203                     NSConsumerSelectInterestTopics(g_provider);
204                 }
205                 break;
206             case 5:
207                 printf("5. Exit");
208                 isExit = true;
209                 break;
210 #ifdef WITH_CLOUD
211             case 21:
212                 printf("Enable Remote Service");
213                 if(!IsCloudLoggedin())
214                 {
215                     printf("Cloud Login required");
216                     break;
217                 }
218                 NSConsumerEnableRemoteService(CLOUD_ADDRESS);
219                 break;
220             case 31:
221                 printf("Remote Server Address: ");
222                 gets(CLOUD_ADDRESS);
223
224                 printf("Auth Provider(eg. github): ");
225                 gets(CLOUD_AUTH_PROVIDER);
226
227                 printf("Auth Code: ");
228                 gets(CLOUD_AUTH_CODE);
229
230                 OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
231                     CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
232                 printf("OCCloudSignup requested");
233                 break;
234             case 32:
235                 printf("Remote Server Address: ");
236                 gets(CLOUD_ADDRESS);
237
238                 printf("UID: ");
239                 gets(CLOUD_UID);
240
241                 printf("ACCESS_TOKEN: ");
242                 gets(CLOUD_ACCESS_TOKEN);
243
244                 OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
245                     CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
246                 printf("OCCloudLogin requested");
247                 break;
248             case 33:
249                 OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
250                 printf("OCCloudLogin requested");
251                 break;
252 #endif
253             default:
254                 break;
255         }
256     }
257     return 0;
258 }