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