Merge "Merge branch 'master' into notification-service" into notification-service
[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 onNotificationPosted(NSProvider * provider, NSMessage * notification)
9 {
10     (void) provider;
11 //    printf("Notification from : %s:%d\n", ((OCDevAddr *)provider->mUserData)->addr,
12 //            ((OCDevAddr *)provider->mUserData)->port);
13     printf("id : %ld\n", notification->messageId);
14     printf("title : %s\n", notification->title);
15     printf("content : %s\n", notification->contentText);
16     printf("source : %s\n", notification->sourceName);
17 //    NSDropNSObject(notification);
18     NSConsumerReadCheck(notification);
19 }
20
21 void onDiscoverNotification(NSProvider * provider)
22 {
23     printf("notification resource discovered\n");
24     printf("subscribe result %d\n", NSSubscribe(provider));
25     printf("startSubscribing\n");
26 }
27
28 void onNotificationSync(NSProvider * provider, NSSyncInfo * sync)
29 {
30     (void) provider;
31 //    printf("Sync from : %s:%d\n", ((OCDevAddr *)provider->mUserData)->addr,
32 //            ((OCDevAddr *)provider->mUserData)->port);
33     printf("Sync ID : %ld\n", sync->messageId);
34     printf("Sync STATE : %d\n", sync->state);
35 }
36
37 int main(void)
38 {
39
40     printf("start Iotivity\n");
41     if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
42     {
43         printf("OCInit fail\n");
44         return 0;
45     }
46
47
48
49     printf("start notification consumer service\n");
50     NSResult ret = NSStartConsumer(
51             onDiscoverNotification,
52             onNotificationPosted,
53             onNotificationSync);
54     if(ret != NS_OK)
55     {
56         printf("error discoverNoti %d\n", ret);
57     }
58
59     while (true)
60     {
61         usleep(2000);
62         if(OCProcess() != OC_STACK_OK)
63         {
64             OCStop();
65             break;
66         }
67     }
68
69     return 0;
70 }