Modify Consumer Logic
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationconsumer.c
1 #include <stdio.h>
2
3 #include <unistd.h>
4 #include "NSCommon.h"
5 #include "NSConsumerInterface.h"
6 #include "ocstack.h"
7
8 void onDiscoverNotification(NSProvider * provider)
9 {
10     printf("notification resource discovered\n");
11     printf("subscribe result %d\n", NSSubscribe(provider));
12     printf("startSubscribing\n");
13 }
14
15 void onSubscriptionAccepted(NSProvider * provider)
16 {
17     printf("Subscription accepted\n");
18     printf("subscribed provider Id : %s\n", provider->providerId);
19 }
20
21 void onNotificationPosted(NSMessage * notification)
22 {
23     printf("id : %llu\n", notification->messageId);
24     printf("title : %s\n", notification->title);
25     printf("content : %s\n", notification->contentText);
26     printf("source : %s\n", notification->sourceName);
27     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
28 }
29
30 void onNotificationSync(NSSyncInfo * sync)
31 {
32     printf("Sync ID : %llu\n", sync->messageId);
33     printf("Sync STATE : %d\n", sync->state);
34 }
35
36 int main(void)
37 {
38
39     printf("start Iotivity\n");
40     if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
41     {
42         printf("OCInit fail\n");
43         return 0;
44     }
45
46     NSConsumerConfig cfg;
47     cfg.discoverCb = onDiscoverNotification;
48     cfg.acceptedCb = onSubscriptionAccepted;
49     cfg.messageCb = onNotificationPosted;
50     cfg.syncInfoCb = onNotificationSync;
51
52
53     printf("start notification consumer service\n");
54     NSResult ret = NSStartConsumer(cfg);
55     if(ret != NS_OK)
56     {
57         printf("error discoverNoti %d\n", ret);
58     }
59
60     while (true)
61     {
62         usleep(2000);
63         if(OCProcess() != OC_STACK_OK)
64         {
65             OCStop();
66             break;
67         }
68     }
69
70     return 0;
71 }