1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 #include "NSConsumerInterface.h"
30 #include "NSConstants.h"
31 #include "NSConsumerCommon.h"
32 #include "oic_malloc.h"
34 #define CLOUD_CONTEXT_VALUE 0x99
35 #define CLOUD_PRESENCE_SUBSCRIBE_QUERY "" // refer to IoTivity Cloud Module Sample
37 #define CLOUD_HOST_ADDRESS "" // refer to IoTivity Cloud Module Sample
38 #define CLOUD_IOTIVITYNS_SESSION "" // refer to IoTivity Cloud Module Sample
41 void onDiscoverNotification(NSProvider * provider)
43 printf("notification resource discovered\n");
44 printf("subscribe result %d\n", NSSubscribe(provider));
45 printf("startSubscribing\n");
48 void onProviderChanged(NSProvider * provider, NSResponse response)
50 printf("Provider changed: %d\n", response);
51 printf("subscribed provider Id : %s\n", provider->providerId);
53 if (response == NS_TOPIC)
55 printf ("Provider Topic Updated\n");
56 if (provider->topicLL)
58 NSTopicLL * iter = provider->topicLL;
61 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
66 printf("3. Get Topics\n");
67 printf("4. Select Topics\n");
80 printf("3. Get Topics\n");
81 NSConsumerGetInterestTopics(provider);
84 printf("4. Select Topics\n");
85 if (provider->topicLL)
87 NSTopicLL * iter = provider->topicLL;
91 iter->state = (i++)%2;
92 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
96 NSConsumerSelectInterestTopics(provider);
102 void onNotificationPosted(NSMessage * notification)
104 printf("id : %lld\n", (long long int)notification->messageId);
105 printf("title : %s\n", notification->title);
106 printf("content : %s\n", notification->contentText);
107 printf("source : %s\n", notification->sourceName);
108 if (notification->topic && strlen(notification->topic) > 0)
110 printf("topic : %s\n", notification->topic);
112 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
115 void onNotificationSync(NSSyncInfo * sync)
117 printf("Sync ID : %lld\n", (long long int)sync->messageId);
118 printf("Sync STATE : %d\n", sync->state);
123 OCStackApplicationResult handleLoginoutCB(void *ctx,
125 OCClientResponse *clientResponse)
128 if (ctx != (void *)CLOUD_CONTEXT_VALUE)
130 NS_LOG(DEBUG, "Invalid Login/out callback received");
133 NS_LOG(DEBUG, "Login/out response received");
135 if (clientResponse->payload != NULL &&
136 clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
138 NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
140 OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
146 NS_LOG(DEBUG, "Get payload values");
148 OCDevAddr * addr = NULL;
149 addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
150 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
152 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
154 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
155 NSConsumerPushEvent(task);
158 return OC_STACK_KEEP_TRANSACTION;
162 void* OCProcessThread(void * ptr)
169 if(OCProcess() != OC_STACK_OK)
182 pthread_t OCThread = NULL;
184 printf("start Iotivity\n");
185 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
187 printf("OCInit fail\n");
191 NSConsumerConfig cfg;
192 cfg.discoverCb = onDiscoverNotification;
193 cfg.changedCb = onProviderChanged;
194 cfg.messageCb = onNotificationPosted;
195 cfg.syncInfoCb = onNotificationSync;
198 NS_LOG(DEBUG, "process OCCloudLogin...");
199 NS_LOG(DEBUG, "OCCloudLogin return");
202 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
204 printf("start notification consumer service\n");
210 printf("1. Start Consumer\n");
211 printf("2. Stop Consumer\n");
224 printf("1. Start Consumer\n");
225 NSStartConsumer(cfg);
228 printf("2. Stop Consumer");