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 "NSProviderInterface.h"
32 #include "oic_string.h"
33 #include "oic_malloc.h"
36 #define TAG "NSProviderExample"
39 #include "cloud_connector.h"
41 // Input the following values to publish resource to cloud
42 char REMOTE_SERVER_ADDRESS[50] = {'\0',};
43 char AUTH_PROVIDER[50] = {'\0',};
44 char AUTH_CODE[50] = {'\0',};
45 char UID[50] = {'\0',};
46 char ACCESS_TOKEN[50] = {'\0',};
49 char mainConsumer[37] = {'\0',};
51 extern char *strdup(const char *s);
57 void* OCProcessThread(void * ptr)
62 if (OCProcess() != OC_STACK_OK)
64 printf("OCStack process error");
72 void subscribeRequestCallback(NSConsumer *consumer)
74 printf("consumer requested to subscribe");
76 printf("NS_APP Consumer Device ID: %s\n", consumer->consumerId);
78 if(mainConsumer[0] == '\0')
80 OICStrcpy(mainConsumer, 37, consumer->consumerId);
83 NSAcceptSubscription(consumer->consumerId, true);
86 void syncCallback(NSSyncInfo *sync)
88 printf("sync requested");
90 printf("NS_APP Sync State: %d\n", sync->state);
93 FILE* server_fopen(const char *path, const char *mode)
96 return fopen("oic_ns_provider_db.dat", mode);
99 void printTopics(NSTopicLL * topics)
103 printf("topics is null\n");
107 NSTopicLL * iter = topics;
111 printf("tName = %s, tState = %d\n", iter->topicName, (int)iter->state);
116 void removeTopics(NSTopicLL * topics)
120 printf("topics is null\n");
124 NSTopicLL * iter = topics;
128 NSTopicLL * del = iter;
131 OICFree(del->topicName);
139 void input(char * buffer)
144 while( (ch = getchar()) != '\n' && i < 100)
153 pthread_t processThread;
155 printf("NSStartProvider()\n\n");
158 static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
159 OCRegisterPersistentStorageHandler(&ps);
161 if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
163 printf("OCStack init error");
167 pthread_create(&processThread, NULL, OCProcessThread, unlink);
173 printf("==============================================\n");
174 printf("1. NSStartProvider(Accepter: Provider) \n");
175 printf("2. NSStartProvider(Accepter: Consumer) \n");
176 printf("3. NSSendNotification() \n");
177 printf("4. NSRead() \n");
178 printf("5. NSProviderAddTopic(); \n");
179 printf("6. NSProviderDeleteTopic(); \n");
180 printf("7. NSProviderSelectTopic(); \n");
181 printf("8. NSProviderUnselectTopic(); \n");
182 printf("9. NSProviderGetConsumerTopics(); \n");
183 printf("10. NSProviderGetTopics(); \n");
184 printf("11. NSStopProvider() \n");
186 printf("21. NSProviderEnableRemoteService (after login) \n");
187 printf("22. NSProviderDisableRemoteService (after login) \n");
188 printf("31. Cloud Signup \n");
189 printf("32. Cloud Login \n");
190 printf("33. Cloud Logout \n");
192 printf("0. Exit() \n");
193 printf("==============================================\n");
197 if(scanf("%d", &num) > 0 && isdigit(num) == 0)
199 if(scanf("%c", &dummy) > 0)
207 printf("invalid input \n");
215 printf("NSStartProvider(Accepter: Provider)");
216 NSProviderConfig config;
217 config.subControllability = true;
218 config.subRequestCallback = subscribeRequestCallback;
219 config.syncInfoCallback = syncCallback;
220 config.userInfo = OICStrdup("OCF_NOTIFICATION");
221 NSStartProvider(config);
227 printf("NSStartProvider(Accepter: Consumer)");
228 NSProviderConfig config;
229 config.subControllability = false;
230 config.subRequestCallback = subscribeRequestCallback;
231 config.syncInfoCallback = syncCallback;
232 config.userInfo = OICStrdup("OCF_NOTIFICATION");
233 NSStartProvider(config);
239 printf("NSSendNotification()");
240 char title[100] = {'\0',};
241 char body[100] = {'\0',};
242 char topic[100] = {'\0',};
244 printf("id : %d\n", ++id);
254 printf("app - mTitle : %s \n", title);
255 printf("app - mContentText : %s \n", body);
256 printf("app - topic : %s \n", topic);
258 NSMessage * msg = NSCreateMessage();
262 msg->title = OICStrdup(title);
263 msg->contentText = OICStrdup(body);
264 msg->sourceName = OICStrdup("OCF");
268 msg->topic = OICStrdup(topic);
281 printf("NSProviderAddTopic\n");
282 NSProviderRegisterTopic("OCF_TOPIC1");
283 NSProviderRegisterTopic("OCF_TOPIC2");
284 NSProviderRegisterTopic("OCF_TOPIC3");
285 NSProviderRegisterTopic("OCF_TOPIC4");
289 printf("NSProviderDeleteTopic\n");
290 NSProviderUnregisterTopic("OCF_TOPIC2");
294 printf("NSProviderSelectTopic\n");
295 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC1");
296 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC2");
297 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC3");
298 NSProviderSetConsumerTopic(mainConsumer, "OCF_TOPIC4");
302 printf("NSProviderUnSelectTopic\n");
303 NSProviderUnsetConsumerTopic(mainConsumer, "OCF_TOPIC1");
307 printf("NSProviderGetConsumerTopics\n");
309 NSTopicLL * topics = NSProviderGetConsumerTopics(mainConsumer);
311 removeTopics(topics);
316 printf("NSProviderGetTopics\n");
318 NSTopicLL * topics = NSProviderGetTopics();
320 removeTopics(topics);
329 printf("Enable Remote Service\n");
330 if(!IsCloudLoggedin())
332 printf("Login required\n");
335 NSProviderEnableRemoteService(REMOTE_SERVER_ADDRESS);
339 printf("Disable Remote Service\n");
340 if(!IsCloudLoggedin())
342 printf("Login required\n");
345 NSProviderDisableRemoteService(REMOTE_SERVER_ADDRESS);
349 printf("Remote Server Address: ");
350 input(REMOTE_SERVER_ADDRESS);
352 printf("Auth Provider(eg. github): ");
353 input(AUTH_PROVIDER);
355 printf("Auth Code: ");
358 OCCloudSignup(REMOTE_SERVER_ADDRESS, OCGetServerInstanceIDString(),
359 AUTH_PROVIDER, AUTH_CODE, CloudSignupCallback);
360 printf("OCCloudSignup requested");
363 printf("Remote Server Address: ");
364 input(REMOTE_SERVER_ADDRESS);
369 printf("ACCESS_TOKEN: ");
372 OCCloudLogin(REMOTE_SERVER_ADDRESS, UID, OCGetServerInstanceIDString(),
373 ACCESS_TOKEN, CloudLoginoutCallback);
374 printf("OCCloudLogin requested\n");
377 OCCloudLogout(REMOTE_SERVER_ADDRESS, CloudLoginoutCallback);
378 printf("OCCloudLogin requested\n");