c86466978cc77e56cfb2f1c32d79bbe63fb817ab
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationprovider.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 <stdbool.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <ctype.h>
26
27 #include "NSProviderInterface.h"
28 #include "NSCommon.h"
29 #include "logger.h"
30 #include "octypes.h"
31 #include "pthread.h"
32 #include "oic_string.h"
33 #include "oic_malloc.h"
34 #include "ocstack.h"
35
36 #define TAG "NSProviderExample"
37
38 #ifdef WITH_CLOUD
39 #include "cloud_connector.h"
40
41 // Input the following values to publish resource to cloud
42 char REMOTE_SERVER_ADDRESS[50] = {'\0',};
43 char AUTH_PROVIDER[50] = {'\0',};
44 char AUTH_CODE[50] = {'\0',};
45 char UID[50] = {'\0',};
46 char ACCESS_TOKEN[50] = {'\0',};
47 #endif
48
49 char mainConsumer[37] = {'\0',};
50
51 extern char *strdup(const char *s);
52
53 bool isExit = false;
54
55 int id;
56
57 void* OCProcessThread(void * ptr)
58 {
59     (void) ptr;
60     while (!isExit)
61     {
62         if (OCProcess() != OC_STACK_OK)
63         {
64             printf("OCStack process error");
65             return NULL;
66         }
67     }
68
69     return NULL;
70 }
71
72 void subscribeRequestCallback(NSConsumer *consumer)
73 {
74     printf("consumer requested to subscribe");
75
76     printf("NS_APP Consumer Device ID: %s\n", consumer->consumerId);
77
78     if(mainConsumer[0] == '\0')
79     {
80         OICStrcpy(mainConsumer, 37, consumer->consumerId);
81     }
82
83     NSAcceptSubscription(consumer->consumerId, true);
84 }
85
86 void syncCallback(NSSyncInfo *sync)
87 {
88     printf("sync requested");
89
90     printf("NS_APP Sync State: %d\n", sync->state);
91 }
92
93 FILE* server_fopen(const char *path, const char *mode)
94 {
95     (void)path;
96     return fopen("oic_ns_provider_db.dat", mode);
97 }
98
99 void printTopics(NSTopicLL * topics)
100 {
101     if(!topics)
102     {
103         printf("topics is null\n");
104         return;
105     }
106
107     NSTopicLL * iter = topics;
108
109     while(iter)
110     {
111         printf("tName = %s, tState = %d\n", iter->topicName, (int)iter->state);
112         iter = iter->next;
113     }
114 }
115
116 void removeTopics(NSTopicLL * topics)
117 {
118     if(!topics)
119     {
120         printf("topics is null\n");
121         return;
122     }
123
124     NSTopicLL * iter = topics;
125
126     while(iter)
127     {
128         NSTopicLL * del = iter;
129         if(del->topicName)
130         {
131             OICFree(del->topicName);
132         }
133         iter = iter->next;
134
135         OICFree(del);
136     }
137 }
138
139 void input(char * buffer)
140 {
141     char ch;
142     int i = 0;
143
144     while( (ch = getchar()) != '\n' && i < 100)
145         buffer[i++] = ch;
146
147     buffer[i] = '\0';
148 }
149
150 int main()
151 {
152     int num;
153     pthread_t processThread;
154
155     printf("NSStartProvider()\n\n");
156
157     // open oic_db
158     static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
159     OCRegisterPersistentStorageHandler(&ps);
160
161     if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
162     {
163         printf("OCStack init error");
164         return 0;
165     }
166
167     pthread_create(&processThread, NULL, OCProcessThread, unlink);
168
169     while (!isExit)
170     {
171         char dummy;
172
173         printf("==============================================\n");
174         printf("1.  NSStartProvider(Accepter: Provider) \n");
175         printf("2.  NSStartProvider(Accepter: Consumer) \n");
176         printf("3.  NSSendNotification() \n");
177         printf("4.  NSRead() \n");
178         printf("5.  NSProviderAddTopic(); \n");
179         printf("6.  NSProviderDeleteTopic(); \n");
180         printf("7.  NSProviderSelectTopic(); \n");
181         printf("8.  NSProviderUnselectTopic(); \n");
182         printf("9.  NSProviderGetConsumerTopics(); \n");
183         printf("10. NSProviderGetTopics(); \n");
184         printf("11. NSStopProvider() \n");
185 #ifdef WITH_CLOUD
186         printf("21. NSProviderEnableRemoteService (after login) \n");
187         printf("22. NSProviderDisableRemoteService (after login) \n");
188         printf("31. Cloud Signup \n");
189         printf("32. Cloud Login \n");
190         printf("33. Cloud Logout \n");
191 #endif
192         printf("0. Exit() \n");
193         printf("==============================================\n");
194
195         printf("input : ");
196
197         if(scanf("%d", &num) > 0 && isdigit(num) == 0)
198         {
199             if(scanf("%c", &dummy) > 0)
200             {
201                 fflush(stdin);
202                 printf("\n");
203             }
204         }
205         else
206         {
207             printf("invalid input \n");
208             num = 0;
209         }
210
211         switch (num)
212         {
213             case 1:
214             {
215                 printf("NSStartProvider(Accepter: Provider)");
216                 NSProviderConfig config;
217                 config.subControllability = true;
218                 config.subRequestCallback = subscribeRequestCallback;
219                 config.syncInfoCallback = syncCallback;
220                 config.userInfo = OICStrdup("OCF_NOTIFICATION");
221                 NSStartProvider(config);
222             }
223                 break;
224
225             case 2:
226             {
227                 printf("NSStartProvider(Accepter: Consumer)");
228                 NSProviderConfig config;
229                 config.subControllability = false;
230                 config.subRequestCallback = subscribeRequestCallback;
231                 config.syncInfoCallback = syncCallback;
232                 config.userInfo = OICStrdup("OCF_NOTIFICATION");
233                 NSStartProvider(config);
234             }
235                 break;
236
237             case 3:
238             {
239                 printf("NSSendNotification()");
240                 char title[100] = {'\0',};
241                 char body[100] = {'\0',};
242                 char topic[100] = {'\0',};
243
244                 printf("id : %d\n", ++id);
245                 printf("title : ");
246                 input(title);
247
248                 printf("body : ");
249                 input(body);
250
251                 printf("topic : ");
252                 input(topic);
253
254                 printf("app - mTitle : %s \n", title);
255                 printf("app - mContentText : %s \n", body);
256                 printf("app - topic : %s \n", topic);
257
258                 NSMessage * msg = NSCreateMessage();
259                 if(msg)
260                 {
261
262                     msg->title = OICStrdup(title);
263                     msg->contentText = OICStrdup(body);
264                     msg->sourceName = OICStrdup("OCF");
265
266                     if(topic[0] != '\0')
267                     {
268                         msg->topic = OICStrdup(topic);
269                     }
270
271                     NSSendMessage(msg);
272                 }
273             }
274                 break;
275
276             case 4:
277                 printf("NSRead\n");
278                 break;
279
280             case 5:
281                 printf("NSProviderAddTopic\n");
282                 NSProviderRegisterTopic("OCF_TOPIC1");
283                 NSProviderRegisterTopic("OCF_TOPIC2");
284                 NSProviderRegisterTopic("OCF_TOPIC3");
285                 NSProviderRegisterTopic("OCF_TOPIC4");
286                 break;
287
288             case 6:
289                 printf("NSProviderDeleteTopic\n");
290                 NSProviderUnregisterTopic("OCF_TOPIC2");
291                 break;
292
293             case 7:
294                 printf("NSProviderSelectTopic\n");
295                 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC1");
296                 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC2");
297                 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC3");
298                 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC4");
299                 break;
300
301             case 8:
302                 printf("NSProviderUnSelectTopic\n");
303                 NSProviderUnsetConsumerTopic(mainConsumer, "OCF_TOPIC1");
304                 break;
305
306             case 9:
307                 printf("NSProviderGetConsumerTopics\n");
308                 {
309                     NSTopicLL * topics = NSProviderGetConsumerTopics(mainConsumer);
310                     printTopics(topics);
311                     removeTopics(topics);
312                 }
313                 break;
314
315             case 10:
316                 printf("NSProviderGetTopics\n");
317                 {
318                     NSTopicLL * topics = NSProviderGetTopics();
319                     printTopics(topics);
320                     removeTopics(topics);
321                 }
322                 break;
323
324             case 11:
325                 NSStopProvider();
326                 break;
327 #ifdef WITH_CLOUD
328             case 21:
329                 printf("Enable Remote Service\n");
330                 if(!IsCloudLoggedin())
331                 {
332                     printf("Login required\n");
333                     break;
334                 }
335                 NSProviderEnableRemoteService(REMOTE_SERVER_ADDRESS);
336                 break;
337
338             case 22:
339                 printf("Disable Remote Service\n");
340                 if(!IsCloudLoggedin())
341                 {
342                     printf("Login required\n");
343                     break;
344                 }
345                 NSProviderDisableRemoteService(REMOTE_SERVER_ADDRESS);
346                 break;
347
348             case 31:
349                 printf("Remote Server Address: ");
350                 input(REMOTE_SERVER_ADDRESS);
351
352                 printf("Auth Provider(eg. github): ");
353                 input(AUTH_PROVIDER);
354
355                 printf("Auth Code: ");
356                 input(AUTH_CODE);
357
358                 OCCloudSignup(REMOTE_SERVER_ADDRESS, OCGetServerInstanceIDString(),
359                     AUTH_PROVIDER, AUTH_CODE, CloudSignupCallback);
360                 printf("OCCloudSignup requested");
361                 break;
362             case 32:
363                 printf("Remote Server Address: ");
364                 input(REMOTE_SERVER_ADDRESS);
365
366                 printf("UID: ");
367                 input(UID);
368
369                 printf("ACCESS_TOKEN: ");
370                 input(ACCESS_TOKEN);
371
372                 OCCloudLogin(REMOTE_SERVER_ADDRESS, UID, OCGetServerInstanceIDString(),
373                     ACCESS_TOKEN, CloudLoginoutCallback);
374                 printf("OCCloudLogin requested\n");
375                 break;
376             case 33:
377                 OCCloudLogout(REMOTE_SERVER_ADDRESS, CloudLoginoutCallback);
378                 printf("OCCloudLogin requested\n");
379                 break;
380 #endif
381             case 0:
382                 NSStopProvider();
383                 isExit = true;
384                 break;
385             default:
386                 break;
387         }
388
389         printf("\n");
390     }
391
392     return 0;
393 }