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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
28 #include "NSConsumerInterface.h"
31 #include "NSConstants.h"
32 #include "oic_malloc.h"
33 #include "cloud_connector.h"
35 #define CLOUD_CONTEXT_VALUE 0x99
37 char CLOUD_ADDRESS[100];
38 char CLOUD_AUTH_PROVIDER[100];
39 char CLOUD_AUTH_CODE[100];
41 char CLOUD_ACCESS_TOKEN[100];
45 NSProvider * g_provider = NULL;
46 NSTopicLL * g_topicLL = NULL;
48 FILE* server_fopen(const char *path, const char *mode)
51 return fopen("oic_ns_provider_db.dat", mode);
54 void printProviderTopicList(NSTopicLL * topics)
56 printf("printProviderTopicList\n");
59 NSTopicLL * iter = topics;
62 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
68 void onProviderChanged(NSProvider * provider, NSProviderState response)
70 printf("Provider changed: %d\n", response);
71 printf("subscribed provider Id : %s\n", provider->providerId);
73 if (response == NS_DISCOVERED)
75 printf("notification resource discovered\n");
76 printf("subscribe result %d\n", NSSubscribe(provider->providerId));
77 printf("startSubscribing\n");
80 else if (response == NS_TOPIC)
82 printf ("Provider Topic Updated\n");
83 g_topicLL = NSConsumerGetTopicList(provider->providerId);
84 printProviderTopicList(g_topicLL);
85 g_provider = provider;
89 void onNotificationPosted(NSMessage * notification)
91 printf("id : %lld\n", (long long int)notification->messageId);
92 printf("title : %s\n", notification->title);
93 printf("content : %s\n", notification->contentText);
94 printf("source : %s\n", notification->sourceName);
95 if (notification->topic && strlen(notification->topic) > 0)
97 printf("topic : %s\n", notification->topic);
99 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
102 void onNotificationSync(NSSyncInfo * sync)
104 printf("Sync ID : %lld\n", (long long int)sync->messageId);
105 printf("Sync STATE : %d\n", sync->state);
108 void* OCProcessThread(void * ptr)
115 if(OCProcess() != OC_STACK_OK)
125 void input(char * buffer)
130 while( (ch = getchar()) != '\n' && i < 100)
139 pthread_t OCThread = NULL;
141 printf("start Iotivity\n");
144 static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
145 OCRegisterPersistentStorageHandler(&ps);
147 if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
149 printf("OCInit fail\n");
153 NSConsumerConfig cfg;
154 cfg.changedCb = onProviderChanged;
155 cfg.messageCb = onNotificationPosted;
156 cfg.syncInfoCb = onNotificationSync;
158 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
160 printf("start notification consumer service\n");
166 printf("1. Start Consumer\n");
167 printf("2. Stop Consumer\n");
168 printf("3. Get Topics\n");
169 printf("4. Select Topics\n");
170 printf("5. Cancel select Topics\n");
173 printf("21. Enable Remote Service (after login)\n");
174 printf("31. Cloud Signup\n");
175 printf("32. Cloud Login\n");
176 printf("33. Cloud Logout\n");
181 if(scanf("%d", &num) == EOF)
183 printf("Fail to input num\n");
187 if(scanf("%c", &dummy) == EOF)
189 printf("Fail to input dummy\n");
196 printf("1. Start Consumer\n");
197 NSStartConsumer(cfg);
200 printf("2. Stop Consumer");
204 printf("3. Get Topics\n");
207 g_topicLL = NSConsumerGetTopicList(g_provider->providerId);
208 printProviderTopicList(g_topicLL);
212 printf("4. Select Topics\n");
214 if (g_provider && g_topicLL)
216 NSTopicLL * iter = g_topicLL;
220 iter->state = (i++)%2;
223 NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
226 printProviderTopicList(g_topicLL);
230 printf("Update fail\n");
235 printf("5. Cancel select Topics\n");
236 NSTopicLL * iter = g_topicLL;
239 iter->state = NS_TOPIC_UNSUBSCRIBED;
243 NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
246 printf("Cancel select topic fail\n");
255 printf("Enable Remote Service");
256 if(!IsCloudLoggedin())
258 printf("Cloud Login required");
261 NSConsumerEnableRemoteService(CLOUD_ADDRESS);
264 printf("Remote Server Address: ");
265 input(CLOUD_ADDRESS);
267 printf("Auth Provider(eg. github): ");
268 input(CLOUD_AUTH_PROVIDER);
270 printf("Auth Code: ");
271 input(CLOUD_AUTH_CODE);
273 OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
274 CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
275 printf("OCCloudSignup requested");
278 printf("Remote Server Address: ");
279 input(CLOUD_ADDRESS);
284 printf("ACCESS_TOKEN: ");
285 input(CLOUD_ACCESS_TOKEN);
287 OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
288 CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
289 printf("OCCloudLogin requested");
292 OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
293 printf("OCCloudLogin requested");