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 "oic_malloc.h"
32 #include "cloud_connector.h"
34 #define CLOUD_CONTEXT_VALUE 0x99
36 char CLOUD_ADDRESS[50];
37 char CLOUD_AUTH_PROVIDER[50];
38 char CLOUD_AUTH_CODE[50];
40 char CLOUD_ACCESS_TOKEN[50];
44 NSProvider * g_provider = NULL;
46 FILE* server_fopen(const char *path, const char *mode)
49 return fopen("oic_ns_provider_db.dat", mode);
52 void onDiscoverNotification(NSProvider * provider)
54 printf("notification resource discovered\n");
55 printf("subscribe result %d\n", NSSubscribe(provider));
56 printf("startSubscribing\n");
59 void printProviderTopicList(NSProvider *provider)
61 printf("printProviderTopicList\n");
62 if (provider->topicLL)
64 NSTopicLL * iter = provider->topicLL;
67 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
73 void onProviderChanged(NSProvider * provider, NSResponse response)
75 printf("Provider changed: %d\n", response);
76 printf("subscribed provider Id : %s\n", provider->providerId);
78 if (response == NS_TOPIC)
80 printf ("Provider Topic Updated\n");
82 printProviderTopicList(provider);
83 g_provider = provider;
87 void onNotificationPosted(NSMessage * notification)
89 printf("id : %lld\n", (long long int)notification->messageId);
90 printf("title : %s\n", notification->title);
91 printf("content : %s\n", notification->contentText);
92 printf("source : %s\n", notification->sourceName);
93 if (notification->topic && strlen(notification->topic) > 0)
95 printf("topic : %s\n", notification->topic);
97 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
100 void onNotificationSync(NSSyncInfo * sync)
102 printf("Sync ID : %lld\n", (long long int)sync->messageId);
103 printf("Sync STATE : %d\n", sync->state);
106 void* OCProcessThread(void * ptr)
113 if(OCProcess() != OC_STACK_OK)
126 pthread_t OCThread = NULL;
128 printf("start Iotivity\n");
131 static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
132 OCRegisterPersistentStorageHandler(&ps);
134 if (OCInit1(OC_CLIENT_SERVER, 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;
146 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
148 printf("start notification consumer service\n");
154 printf("1. Start Consumer\n");
155 printf("2. Stop Consumer\n");
156 printf("3. Get Topics\n");
157 printf("4. Select Topics\n");
160 printf("21. Enable Remote Service (after login)\n");
161 printf("31. Cloud Signup\n");
162 printf("32. Cloud Login\n");
163 printf("33. Cloud Logout\n");
176 printf("1. Start Consumer\n");
177 NSStartConsumer(cfg);
180 printf("2. Stop Consumer");
184 printf("3. Get Topics\n");
187 NSConsumerGetInterestTopics(g_provider);
191 printf("4. Select Topics\n");
193 if (g_provider && g_provider->topicLL)
195 NSTopicLL * iter = g_provider->topicLL;
199 iter->state = (i++)%2;
200 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
203 NSConsumerSelectInterestTopics(g_provider);
212 printf("Enable Remote Service");
213 if(!IsCloudLoggedin())
215 printf("Cloud Login required");
218 NSConsumerEnableRemoteService(CLOUD_ADDRESS);
221 printf("Remote Server Address: ");
224 printf("Auth Provider(eg. github): ");
225 gets(CLOUD_AUTH_PROVIDER);
227 printf("Auth Code: ");
228 gets(CLOUD_AUTH_CODE);
230 OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
231 CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
232 printf("OCCloudSignup requested");
235 printf("Remote Server Address: ");
241 printf("ACCESS_TOKEN: ");
242 gets(CLOUD_ACCESS_TOKEN);
244 OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
245 CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
246 printf("OCCloudLogin requested");
249 OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
250 printf("OCCloudLogin requested");