[IOT-1558] Add notification consumer for windows.
[iotivity.git] / service / notification / examples / windows / 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 <string.h>
23 #include "octhread.h"
24
25 #include "ocstack.h"
26 #include "NSCommon.h"
27 #include "NSConsumerInterface.h"
28
29 #ifdef SYSTEM_WINDOWS
30 #include <unistd.h>
31 #else
32 #include "win_sleep.h"
33 #endif
34
35 #ifdef WITH_CLOUD
36 #include "NSConstants.h"
37 #include "oic_malloc.h"
38 #include "cloud_connector.h"
39
40 #define CLOUD_CONTEXT_VALUE 0x99
41
42 char CLOUD_ADDRESS[100];
43 char CLOUD_AUTH_PROVIDER[100];
44 char CLOUD_AUTH_CODE[100];
45 char CLOUD_UID[100];
46 char CLOUD_ACCESS_TOKEN[100];
47 #endif
48
49
50 NSProvider * g_provider = NULL;
51 NSTopicLL * g_topicLL = NULL;
52
53 FILE* server_fopen(const char *path, const char *mode)
54 {
55     (void)path;
56     return fopen("oic_ns_provider_db.dat", mode);
57 }
58
59 void printProviderTopicList(NSTopicLL * topics)
60 {
61     printf("printProviderTopicList\n");
62     if (topics)
63     {
64         NSTopicLL * iter = topics;
65         while (iter)
66         {
67             printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
68             iter = iter->next;
69         }
70     }
71 }
72
73 void onProviderChanged(NSProvider * provider, NSProviderState response)
74 {
75     printf("Provider changed: %d\n", response);
76     printf("subscribed provider Id : %s\n", provider->providerId);
77
78     if (response == NS_DISCOVERED)
79     {
80         printf("notification resource discovered\n");
81         printf("subscribe result %d\n", NSSubscribe(provider->providerId));
82         printf("startSubscribing\n");
83     }
84
85     else if (response == NS_TOPIC)
86     {
87         printf ("Provider Topic Updated\n");
88         g_topicLL = NSConsumerGetTopicList(provider->providerId);
89         printProviderTopicList(g_topicLL);
90         g_provider = provider;
91     }
92 }
93
94 void onNotificationPosted(NSMessage * notification)
95 {
96     printf("id : %lld\n", (long long int)notification->messageId);
97     printf("title : %s\n", notification->title);
98     printf("content : %s\n", notification->contentText);
99     printf("source : %s\n", notification->sourceName);
100     if (notification->topic && strlen(notification->topic) > 0)
101     {
102         printf("topic : %s\n", notification->topic);
103     }
104     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
105 }
106
107 void onNotificationSync(NSSyncInfo * sync)
108 {
109     printf("Sync ID : %lld\n", (long long int)sync->messageId);
110     printf("Sync STATE : %d\n", sync->state);
111 }
112
113 void* OCProcessThread(void * ptr)
114 {
115     (void) ptr;
116
117     while (true)
118     {
119         if(OCProcess() != OC_STACK_OK)
120         {
121             OCStop();
122             break;
123         }
124     }
125
126     return NULL;
127 }
128
129 void input(char * buffer)
130 {
131     char ch;
132     int i = 0;
133
134     while( (ch = getchar()) != '\n' && i < 100)
135         buffer[i++] = ch;
136
137     buffer[i] = '\0';
138 }
139
140 int main(void)
141 {
142     bool isExit = false;
143     oc_thread OCThread = NULL;
144
145     printf("start Iotivity\n");
146
147     // open oic_db
148     static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
149     OCRegisterPersistentStorageHandler(&ps);
150
151     if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
152     {
153         printf("OCInit fail\n");
154         return 0;
155     }
156
157     NSConsumerConfig cfg;
158     cfg.changedCb = onProviderChanged;
159     cfg.messageCb = onNotificationPosted;
160     cfg.syncInfoCb = onNotificationSync;
161
162     OCThreadResult_t ret = oc_thread_new(&OCThread, OCProcessThread, NULL);
163     printf("create thread result : %d\n", ret);
164
165     printf("start notification consumer service\n");
166     while (!isExit)
167     {
168         int num = 0;
169         char dummy = '\0';
170
171         printf("1. Start Consumer\n");
172         printf("2. Stop Consumer\n");
173         printf("3. Get Topics\n");
174         printf("4. Select Topics\n");
175         printf("5. Cancel select Topics\n");
176         printf("0. Exit\n");
177 #ifdef WITH_CLOUD
178         printf("21. Enable Remote Service (after login)\n");
179         printf("31. Cloud Signup\n");
180         printf("32. Cloud Login\n");
181         printf("33. Cloud Logout\n");
182 #endif
183
184         printf("Input: ");
185
186         if(scanf("%d", &num) == EOF)
187         {
188             printf("Fail to input num\n");
189         }
190         fflush(stdin);
191
192         if(scanf("%c", &dummy) == EOF)
193         {
194             printf("Fail to input dummy\n");
195         }
196         fflush(stdin);
197
198         switch (num)
199         {
200             case 1:
201                 printf("1. Start Consumer\n");
202                 NSStartConsumer(cfg);
203                 break;
204             case 2:
205                 printf("2. Stop Consumer");
206                 NSStopConsumer();
207                 break;
208             case 3:
209                 printf("3. Get Topics\n");
210                 if(g_provider)
211                 {
212                     g_topicLL = NSConsumerGetTopicList(g_provider->providerId);
213                     printProviderTopicList(g_topicLL);
214                 }
215                 break;
216             case 4:
217                 printf("4. Select Topics\n");
218
219                 if (g_provider && g_topicLL)
220                 {
221                     NSTopicLL * iter = g_topicLL;
222                     int i = 0;
223                     while (iter)
224                     {
225                         iter->state = (i++)%2;
226                         iter = iter->next;
227                     }
228                     NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
229                     if (ret == NS_OK)
230                     {
231                         printProviderTopicList(g_topicLL);
232                     }
233                     else
234                     {
235                         printf("Update fail\n");
236                     }
237                 }
238                 break;
239             case 5:
240                 printf("5. Cancel select Topics\n");
241                 NSTopicLL * iter = g_topicLL;
242                 while (iter)
243                 {
244                     iter->state = NS_TOPIC_UNSUBSCRIBED;
245                     iter = iter->next;
246                 }
247
248                 NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
249                 if (ret != NS_OK)
250                 {
251                     printf("Cancel select topic fail\n");
252                 }
253                 break;
254             case 0:
255                 printf("0. Exit");
256                 isExit = true;
257                 break;
258 #ifdef WITH_CLOUD
259             case 21:
260                 printf("Enable Remote Service");
261                 if(!IsCloudLoggedin())
262                 {
263                     printf("Cloud Login required");
264                     break;
265                 }
266                 NSConsumerEnableRemoteService(CLOUD_ADDRESS);
267                 break;
268             case 31:
269                 printf("Remote Server Address: ");
270                 input(CLOUD_ADDRESS);
271
272                 printf("Auth Provider(eg. github): ");
273                 input(CLOUD_AUTH_PROVIDER);
274
275                 printf("Auth Code: ");
276                 input(CLOUD_AUTH_CODE);
277
278                 OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
279                     CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
280                 printf("OCCloudSignup requested");
281                 break;
282             case 32:
283                 printf("Remote Server Address: ");
284                 input(CLOUD_ADDRESS);
285
286                 printf("UID: ");
287                 input(CLOUD_UID);
288
289                 printf("ACCESS_TOKEN: ");
290                 input(CLOUD_ACCESS_TOKEN);
291
292                 OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
293                     CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
294                 printf("OCCloudLogin requested");
295                 break;
296             case 33:
297                 OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
298                 printf("OCCloudLogin requested");
299                 break;
300 #endif
301             default:
302                 break;
303         }
304     }
305     return 0;
306 }
307