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_TOPIC[100];
39 char CLOUD_AUTH_PROVIDER[100];
40 char CLOUD_AUTH_CODE[100];
42 char CLOUD_ACCESS_TOKEN[100];
46 NSProvider * g_provider = NULL;
47 NSTopicLL * g_topicLL = NULL;
49 FILE* server_fopen(const char *path, const char *mode)
52 return fopen("oic_ns_provider_db.dat", mode);
55 void printProviderTopicList(NSTopicLL * topics)
57 printf("printProviderTopicList\n");
60 NSTopicLL * iter = topics;
63 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
69 void onProviderChanged(NSProvider * provider, NSProviderState response)
71 printf("Provider changed: %d\n", response);
72 printf("subscribed provider Id : %s\n", provider->providerId);
74 if (response == NS_DISCOVERED)
76 printf("notification resource discovered\n");
77 printf("subscribe result %d\n", NSSubscribe(provider->providerId));
78 printf("startSubscribing\n");
81 else if (response == NS_TOPIC)
83 printf ("Provider Topic Updated\n");
84 g_topicLL = NSConsumerGetTopicList(provider->providerId);
85 printProviderTopicList(g_topicLL);
86 g_provider = provider;
90 void onNotificationPosted(NSMessage * notification)
92 printf("id : %lld\n", (long long int)notification->messageId);
93 printf("title : %s\n", notification->title);
94 printf("content : %s\n", notification->contentText);
95 printf("source : %s\n", notification->sourceName);
96 if (notification->topic && strlen(notification->topic) > 0)
98 printf("topic : %s\n", notification->topic);
100 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
103 void onNotificationSync(NSSyncInfo * sync)
105 printf("Sync ID : %lld\n", (long long int)sync->messageId);
106 printf("Sync STATE : %d\n", sync->state);
109 void* OCProcessThread(void * ptr)
116 if(OCProcess() != OC_STACK_OK)
126 void input(char * buffer)
131 while( (ch = getchar()) != '\n' && i < 100)
140 pthread_t OCThread = NULL;
142 printf("start Iotivity\n");
145 static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
146 OCRegisterPersistentStorageHandler(&ps);
148 if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
150 printf("OCInit fail\n");
154 NSConsumerConfig cfg;
155 cfg.changedCb = onProviderChanged;
156 cfg.messageCb = onNotificationPosted;
157 cfg.syncInfoCb = onNotificationSync;
159 pthread_create(&OCThread, NULL, OCProcessThread, NULL);
161 printf("start notification consumer service\n");
167 printf("1. Start Consumer\n");
168 printf("2. Stop Consumer\n");
169 printf("3. Get Topics\n");
170 printf("4. Select Topics\n");
171 printf("5. Cancel select Topics\n");
174 printf("11. Subscribe MQ Service\n");
177 printf("21. Enable Remote Service (after login)\n");
178 printf("31. Cloud Signup\n");
179 printf("32. Cloud Login\n");
180 printf("33. Cloud Logout\n");
185 if(scanf("%d", &num) == EOF)
187 printf("Fail to input num\n");
191 if(scanf("%c", &dummy) == EOF)
193 printf("Fail to input dummy\n");
200 printf("1. Start Consumer\n");
201 NSStartConsumer(cfg);
204 printf("2. Stop Consumer");
208 printf("3. Get Topics\n");
211 g_topicLL = NSConsumerGetTopicList(g_provider->providerId);
212 printProviderTopicList(g_topicLL);
216 printf("4. Select Topics\n");
218 if (g_provider && g_topicLL)
220 NSTopicLL * iter = g_topicLL;
224 iter->state = (i++)%2;
227 NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
230 printProviderTopicList(g_topicLL);
234 printf("Update fail\n");
239 printf("5. Cancel select Topics\n");
240 NSTopicLL * iter = g_topicLL;
243 iter->state = NS_TOPIC_UNSUBSCRIBED;
247 NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, g_topicLL);
250 printf("Cancel select topic fail\n");
259 printf("11. Subscribe MQ Service\n");
261 printf("Remote Server Address: ");
262 input(CLOUD_ADDRESS);
264 printf("Topic[notification]: ");
267 NSConsumerSubscribeMQService(CLOUD_ADDRESS, CLOUD_TOPIC);
272 printf("Enable Remote Service");
273 if(!IsCloudLoggedin())
275 printf("Cloud Login required");
278 NSConsumerEnableRemoteService(CLOUD_ADDRESS);
281 printf("Remote Server Address: ");
282 input(CLOUD_ADDRESS);
284 printf("Auth Provider(eg. github): ");
285 input(CLOUD_AUTH_PROVIDER);
287 printf("Auth Code: ");
288 input(CLOUD_AUTH_CODE);
290 OCCloudSignup(CLOUD_ADDRESS, OCGetServerInstanceIDString(),
291 CLOUD_AUTH_PROVIDER, CLOUD_AUTH_CODE, CloudSignupCallback);
292 printf("OCCloudSignup requested");
295 printf("Remote Server Address: ");
296 input(CLOUD_ADDRESS);
301 printf("ACCESS_TOKEN: ");
302 input(CLOUD_ACCESS_TOKEN);
304 OCCloudLogin(CLOUD_ADDRESS, CLOUD_UID, OCGetServerInstanceIDString(),
305 CLOUD_ACCESS_TOKEN, CloudLoginoutCallback);
306 printf("OCCloudLogin requested");
309 OCCloudLogout(CLOUD_ADDRESS, CloudLoginoutCallback);
310 printf("OCCloudLogin requested");