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);
55 void onNotificationPosted(NSMessage * notification)
57 printf("id : %lld\n", (long long int)notification->messageId);
58 printf("title : %s\n", notification->title);
59 printf("content : %s\n", notification->contentText);
60 printf("source : %s\n", notification->sourceName);
61 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
64 void onNotificationSync(NSSyncInfo * sync)
66 printf("Sync ID : %lld\n", (long long int)sync->messageId);
67 printf("Sync STATE : %d\n", sync->state);
72 OCStackApplicationResult handleLoginoutCB(void *ctx,
74 OCClientResponse *clientResponse)
77 if (ctx != (void *)CLOUD_CONTEXT_VALUE)
79 NS_LOG(DEBUG, "Invalid Login/out callback received");
82 NS_LOG(DEBUG, "Login/out response received");
84 if (clientResponse->payload != NULL &&
85 clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
87 NS_LOG(DEBUG, "PAYLOAD_TYPE_REPRESENTATION received");
89 OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
95 NS_LOG(DEBUG, "Get payload values");
97 OCDevAddr * addr = NULL;
98 addr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
99 memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
101 NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED_TCP, addr);
103 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, OC_STACK_KEEP_TRANSACTION, NSOICFree(addr));
104 NSConsumerPushEvent(task);
107 return OC_STACK_KEEP_TRANSACTION;
111 void* OCProcessThread(void * ptr)
118 if(OCProcess() != OC_STACK_OK)
131 pthread_t OCThread = NULL;
133 printf("start Iotivity\n");
134 if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
136 printf("OCInit fail\n");
140 NSConsumerConfig cfg;
141 cfg.discoverCb = onDiscoverNotification;
142 cfg.changedCb = onProviderChanged;
143 cfg.messageCb = onNotificationPosted;
144 cfg.syncInfoCb = onNotificationSync;
147 NS_LOG(DEBUG, "process OCCloudLogin...");
148 OCCloudLogin(CLOUD_HOST_ADDRESS, CLOUD_IOTIVITYNS_SESSION, handleLoginoutCB);
149 NS_LOG(DEBUG, "OCCloudLogin return");
152 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
154 printf("start notification consumer service\n");
160 printf("1. Start Consumer\n");
161 printf("2. Stop Consumer\n");
174 printf("1. Start Consumer\n");
175 NSStartConsumer(cfg);
178 printf("2. Stop Consumer");