Added logic for remove NSProvider.
[platform/upstream/iotivity.git] / service / notification / examples / linux / notificationconsumer.c
1 #include <stdio.h>
2 #include <unistd.h>
3
4 #include "ocstack.h"
5 #include "NSCommon.h"
6 #include "NSConsumerInterface.h"
7
8 #ifdef WITH_CLOUD
9 #include "NSConstants.h"
10 #include "NSConsumerCommon.h"
11 #include "cloud_connector.h"
12 #include "oic_malloc.h"
13
14 #define CLOUD_CONTEXT_VALUE 0x99
15 #define CLOUD_PRESENCE_SUBSCRIBE_QUERY ""          // refer to IoTivity Cloud Module Sample
16
17 #define CLOUD_HOST_ADDRESS ""                      // refer to IoTivity Cloud Module Sample
18 #define CLOUD_IOTIVITYNS_SESSION ""                // refer to IoTivity Cloud Module Sample
19 #endif
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 onSubscriptionAccepted(NSProvider * provider)
29 {
30     printf("Subscription accepted\n");
31     printf("subscribed provider Id : %s\n", provider->providerId);
32 }
33
34 void onNotificationPosted(NSMessage * notification)
35 {
36     printf("id : %lld\n", (long long int)notification->messageId);
37     printf("title : %s\n", notification->title);
38     printf("content : %s\n", notification->contentText);
39     printf("source : %s\n", notification->sourceName);
40     NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
41 }
42
43 void onNotificationSync(NSSyncInfo * sync)
44 {
45     printf("Sync ID : %lld\n", (long long int)sync->messageId);
46     printf("Sync STATE : %d\n", sync->state);
47 }
48
49 #ifdef WITH_CLOUD
50 OCStackApplicationResult handleLoginoutCB(void *ctx,
51         OCDoHandle handle,
52         OCClientResponse *clientResponse)
53 {
54     (void)handle;
55     if (ctx != (void *)CLOUD_CONTEXT_VALUE)
56     {
57         NS_LOG(DEBUG, "Invalid Login/out callback received");
58     }
59
60     NS_LOG(DEBUG, "Login/out response received");
61
62     if (clientResponse->payload != NULL &&
63         clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
64     {
65         NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
66
67         OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
68
69         while (val)
70         {
71             val = val->next;
72         }
73         NS_LOG(DEBUG, "Get payload values");
74
75         OCDevAddr * addr = NULL;
76         addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
77         memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
78
79         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
80
81         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
82         NSConsumerPushEvent(task);
83     }
84
85     return OC_STACK_KEEP_TRANSACTION;
86 }
87 #endif
88
89 int main(void)
90 {
91
92     printf("start Iotivity\n");
93     if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
94     {
95         printf("OCInit fail\n");
96         return 0;
97     }
98
99     NSConsumerConfig cfg;
100     cfg.discoverCb = onDiscoverNotification;
101     cfg.acceptedCb = onSubscriptionAccepted;
102     cfg.messageCb = onNotificationPosted;
103     cfg.syncInfoCb = onNotificationSync;
104
105
106     printf("start notification consumer service\n");
107     NSResult ret = NSStartConsumer(cfg);
108     if(ret != NS_OK)
109     {
110         printf("error discoverNoti %d\n", ret);
111     }
112
113 #ifdef WITH_CLOUD
114     NS_LOG(DEBUG, "process OCCloudLogin...");
115     OCCloudLogin(CLOUD_HOST_ADDRESS, CLOUD_IOTIVITYNS_SESSION, handleLoginoutCB);
116     NS_LOG(DEBUG, "OCCloudLogin return");
117 #endif
118
119     while (true)
120     {
121         usleep(2000);
122         if(OCProcess() != OC_STACK_OK)
123         {
124             OCStop();
125             break;
126         }
127     }
128
129     return 0;
130 }