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