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