6 #include "NSConsumerInterface.h"
9 #include "NSConstants.h"
10 #include "NSConsumerCommon.h"
11 #include "cloud_connector.h"
12 #include "oic_malloc.h"
14 #define CLOUD_CONTEXT_VALUE 0x99
15 #define CLOUD_PRESENCE_SUBSCRIBE_QUERY "" // refer to IoTivity Cloud Module Sample
17 #define CLOUD_HOST_ADDRESS "" // refer to IoTivity Cloud Module Sample
18 #define CLOUD_IOTIVITYNS_SESSION "" // refer to IoTivity Cloud Module Sample
21 void onDiscoverNotification(NSProvider * provider)
23 printf("notification resource discovered\n");
24 printf("subscribe result %d\n", NSSubscribe(provider));
25 printf("startSubscribing\n");
28 void onSubscriptionAccepted(NSProvider * provider)
30 printf("Subscription accepted\n");
31 printf("subscribed provider Id : %s\n", provider->providerId);
34 void onNotificationPosted(NSMessage * notification)
36 printf("id : %lld\n", 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);
43 void onNotificationSync(NSSyncInfo * sync)
45 printf("Sync ID : %lld\n", sync->messageId);
46 printf("Sync STATE : %d\n", sync->state);
50 OCStackApplicationResult handleLoginoutCB(void *ctx,
52 OCClientResponse *clientResponse)
55 if (ctx != (void *)CLOUD_CONTEXT_VALUE)
57 NS_LOG(DEBUG, "Invalid Login/out callback received");
60 NS_LOG(DEBUG, "Login/out response received");
62 if (clientResponse->payload != NULL &&
63 clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
65 NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
67 OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
73 NS_LOG(DEBUG, "Get payload values");
75 OCDevAddr * addr = NULL;
76 addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
77 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
79 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
81 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
82 NSConsumerPushEvent(task);
85 return OC_STACK_KEEP_TRANSACTION;
92 printf("start Iotivity\n");
93 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
95 printf("OCInit fail\n");
100 cfg.discoverCb = onDiscoverNotification;
101 cfg.acceptedCb = onSubscriptionAccepted;
102 cfg.messageCb = onNotificationPosted;
103 cfg.syncInfoCb = onNotificationSync;
106 printf("start notification consumer service\n");
107 NSResult ret = NSStartConsumer(cfg);
110 printf("error discoverNoti %d\n", ret);
114 NS_LOG(DEBUG, "process OCCloudLogin...");
115 OCCloudLogin(CLOUD_HOST_ADDRESS, CLOUD_IOTIVITYNS_SESSION, handleLoginoutCB);
116 NS_LOG(DEBUG, "OCCloudLogin return");
122 if(OCProcess() != OC_STACK_OK)