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 NSProvider * g_provider = NULL;
43 void onDiscoverNotification(NSProvider * provider)
45 printf("notification resource discovered\n");
46 printf("subscribe result %d\n", NSSubscribe(provider));
47 printf("startSubscribing\n");
50 void printProviderTopicList(NSProvider *provider)
52 printf("printProviderTopicList\n");
53 if (provider->topicLL)
55 NSTopicLL * iter = provider->topicLL;
58 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
64 void onProviderChanged(NSProvider * provider, NSResponse response)
66 printf("Provider changed: %d\n", response);
67 printf("subscribed provider Id : %s\n", provider->providerId);
69 if (response == NS_TOPIC)
71 printf ("Provider Topic Updated\n");
73 printProviderTopicList(provider);
74 g_provider = provider;
78 void onNotificationPosted(NSMessage * notification)
80 printf("id : %lld\n", (long long int)notification->messageId);
81 printf("title : %s\n", notification->title);
82 printf("content : %s\n", notification->contentText);
83 printf("source : %s\n", notification->sourceName);
84 if (notification->topic && strlen(notification->topic) > 0)
86 printf("topic : %s\n", notification->topic);
88 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
91 void onNotificationSync(NSSyncInfo * sync)
93 printf("Sync ID : %lld\n", (long long int)sync->messageId);
94 printf("Sync STATE : %d\n", sync->state);
98 OCStackApplicationResult handleLoginoutCB(void *ctx,
100 OCClientResponse *clientResponse)
103 if (ctx != (void *)CLOUD_CONTEXT_VALUE)
105 NS_LOG(DEBUG, "Invalid Login/out callback received");
108 NS_LOG(DEBUG, "Login/out response received");
110 if (clientResponse->payload != NULL &&
111 clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
113 NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
115 OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
121 NS_LOG(DEBUG, "Get payload values");
123 OCDevAddr * addr = NULL;
124 addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
125 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
127 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
129 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
130 NSConsumerPushEvent(task);
133 return OC_STACK_KEEP_TRANSACTION;
137 void* OCProcessThread(void * ptr)
144 if(OCProcess() != OC_STACK_OK)
157 pthread_t OCThread = NULL;
159 printf("start Iotivity\n");
160 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
162 printf("OCInit fail\n");
166 NSConsumerConfig cfg;
167 cfg.discoverCb = onDiscoverNotification;
168 cfg.changedCb = onProviderChanged;
169 cfg.messageCb = onNotificationPosted;
170 cfg.syncInfoCb = onNotificationSync;
173 NS_LOG(DEBUG, "process OCCloudLogin...");
174 NS_LOG(DEBUG, "OCCloudLogin return");
177 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
179 printf("start notification consumer service\n");
185 printf("1. Start Consumer\n");
186 printf("2. Stop Consumer\n");
187 printf("3. Get Topics\n");
188 printf("4. Select Topics\n");
201 printf("1. Start Consumer\n");
202 NSStartConsumer(cfg);
205 printf("2. Stop Consumer");
209 printf("3. Get Topics\n");
212 NSConsumerGetInterestTopics(g_provider);
216 printf("4. Select Topics\n");
218 if (g_provider && g_provider->topicLL)
220 NSTopicLL * iter = g_provider->topicLL;
224 iter->state = (i++)%2;
225 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
228 NSConsumerSelectInterestTopics(g_provider);