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 "cloud_connector.h"
33 #include "oic_malloc.h"
35 #define CLOUD_CONTEXT_VALUE 0x99
36 #define CLOUD_PRESENCE_SUBSCRIBE_QUERY "" // refer to IoTivity Cloud Module Sample
38 #define CLOUD_HOST_ADDRESS "" // refer to IoTivity Cloud Module Sample
39 #define CLOUD_IOTIVITYNS_SESSION "" // refer to IoTivity Cloud Module Sample
42 void onDiscoverNotification(NSProvider * provider)
44 printf("notification resource discovered\n");
45 printf("subscribe result %d\n", NSSubscribe(provider));
46 printf("startSubscribing\n");
49 void onProviderChanged(NSProvider * provider, NSResponse response)
51 printf("Provider changed: %d\n", response);
52 printf("subscribed provider Id : %s\n", provider->providerId);
54 if (response == NS_TOPIC)
56 printf ("Provider Topic Updated\n");
57 if (provider->topicLL)
59 NSTopicLL * iter = provider->topicLL;
62 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
67 printf("3. Get Topics\n");
68 printf("4. Select Topics\n");
81 printf("3. Get Topics\n");
82 NSConsumerGetInterestTopics(provider);
85 printf("4. Select Topics\n");
86 if (provider->topicLL)
88 NSTopicLL * iter = provider->topicLL;
92 iter->state = (i++)%2;
93 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
97 NSConsumerSelectInterestTopics(provider);
103 void onNotificationPosted(NSMessage * notification)
105 printf("id : %lld\n", (long long int)notification->messageId);
106 printf("title : %s\n", notification->title);
107 printf("content : %s\n", notification->contentText);
108 printf("source : %s\n", notification->sourceName);
109 if (notification->topic)
111 printf("topic : %s\n", notification->topic);
113 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
116 void onNotificationSync(NSSyncInfo * sync)
118 printf("Sync ID : %lld\n", (long long int)sync->messageId);
119 printf("Sync STATE : %d\n", sync->state);
124 OCStackApplicationResult handleLoginoutCB(void *ctx,
126 OCClientResponse *clientResponse)
129 if (ctx != (void *)CLOUD_CONTEXT_VALUE)
131 NS_LOG(DEBUG, "Invalid Login/out callback received");
134 NS_LOG(DEBUG, "Login/out response received");
136 if (clientResponse->payload != NULL &&
137 clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
139 NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
141 OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
147 NS_LOG(DEBUG, "Get payload values");
149 OCDevAddr * addr = NULL;
150 addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
151 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
153 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
155 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
156 NSConsumerPushEvent(task);
159 return OC_STACK_KEEP_TRANSACTION;
163 void* OCProcessThread(void * ptr)
170 if(OCProcess() != OC_STACK_OK)
183 pthread_t OCThread = NULL;
185 printf("start Iotivity\n");
186 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
188 printf("OCInit fail\n");
192 NSConsumerConfig cfg;
193 cfg.discoverCb = onDiscoverNotification;
194 cfg.changedCb = onProviderChanged;
195 cfg.messageCb = onNotificationPosted;
196 cfg.syncInfoCb = onNotificationSync;
199 NS_LOG(DEBUG, "process OCCloudLogin...");
200 OCCloudLogin(CLOUD_HOST_ADDRESS, CLOUD_IOTIVITYNS_SESSION, handleLoginoutCB);
201 NS_LOG(DEBUG, "OCCloudLogin return");
204 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
206 printf("start notification consumer service\n");
212 printf("1. Start Consumer\n");
213 printf("2. Stop Consumer\n");
226 printf("1. Start Consumer\n");
227 NSStartConsumer(cfg);
230 printf("2. Stop Consumer");