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