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[50];
38 char CLOUD_AUTH_PROVIDER[50];
39 char CLOUD_AUTH_CODE[50];
41 char CLOUD_ACCESS_TOKEN[50];
45 NSProvider * g_provider = NULL;
47 FILE* server_fopen(const char *path, const char *mode)
50 return fopen("oic_ns_provider_db.dat", mode);
53 void onDiscoverNotification(NSProvider * provider)
55 printf("notification resource discovered\n");
56 printf("subscribe result %d\n", NSSubscribe(provider));
57 printf("startSubscribing\n");
60 void printProviderTopicList(NSProvider *provider)
62 printf("printProviderTopicList\n");
63 if (provider->topicLL)
65 NSTopicLL * iter = provider->topicLL;
68 printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
74 void onProviderChanged(NSProvider * provider, NSResponse response)
76 printf("Provider changed: %d\n", response);
77 printf("subscribed provider Id : %s\n", provider->providerId);
79 if (response == NS_TOPIC)
81 printf ("Provider Topic Updated\n");
83 printProviderTopicList(provider);
84 g_provider = provider;
88 void onNotificationPosted(NSMessage * notification)
90 printf("id : %lld\n", (long long int)notification->messageId);
91 printf("title : %s\n", notification->title);
92 printf("content : %s\n", notification->contentText);
93 printf("source : %s\n", notification->sourceName);
94 if (notification->topic && strlen(notification->topic) > 0)
96 printf("topic : %s\n", notification->topic);
98 NSConsumerSendSyncInfo(notification->providerId, notification->messageId, NS_SYNC_READ);
101 void onNotificationSync(NSSyncInfo * sync)
103 printf("Sync ID : %lld\n", (long long int)sync->messageId);
104 printf("Sync STATE : %d\n", sync->state);
107 void* OCProcessThread(void * ptr)
114 if(OCProcess() != OC_STACK_OK)
127 pthread_t OCThread = NULL;
129 printf("start Iotivity\n");
132 static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
133 OCRegisterPersistentStorageHandler(&ps);
135 if (OCInit1(OC_CLIENT_SERVER, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)
137 printf("OCInit fail\n");
141 NSConsumerConfig cfg;
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");