1 //******************************************************************
3 // Copyright 2014 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // OCClient.cpp : Defines the entry point for the console application.
28 #include "OCPlatform.h"
30 #include "ThingsConfiguration.h"
31 #include "ThingsMaintenance.h"
32 #include "GroupManager.h"
38 int isWaiting = 0; //0: none to wait, 1: wait for the response of "getConfigurationValue"
39 pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
41 const int SUCCESS_RESPONSE = 0;
43 static GroupManager* g_groupmanager;
44 static ThingsConfiguration* g_thingsConf;
45 static ThingsMaintenance* g_thingsMnt;
47 OCResourceHandle configurationCollectionHandle;
48 std::shared_ptr< OCResource > g_configurationCollection; // for a group of multiple resources
49 std::shared_ptr< OCResource > g_configurationResource; // For a single resource
51 OCResourceHandle maintenanceCollectionHandle;
52 std::shared_ptr< OCResource > g_maintenanceCollection; // for a group of multiple resources
53 std::shared_ptr< OCResource > g_maintenanceResource; // For a single resource
55 OCResourceHandle setCollectionHandle;
56 std::shared_ptr< OCResource > g_setCollection; // for a group of multiple resources
57 std::shared_ptr< OCResource > g_setResource; // For a single resource
59 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
60 std::vector< OCResourceHandle > resourceHandleVector;
62 typedef std::string ConfigurationName;
63 typedef std::string ConfigurationValue;
65 void timeCheck(int timeSec)
68 pthread_mutex_lock(&mutex_lock);
70 pthread_mutex_unlock(&mutex_lock);
73 void onReboot(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
75 pthread_mutex_lock(&mutex_lock);
77 pthread_mutex_unlock(&mutex_lock);
79 if (eCode != SUCCESS_RESPONSE)
84 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
85 std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
88 void onFactoryReset(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
90 pthread_mutex_lock(&mutex_lock);
92 pthread_mutex_unlock(&mutex_lock);
94 if (eCode != SUCCESS_RESPONSE)
99 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
100 std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
103 void onUpdate(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
105 pthread_mutex_lock(&mutex_lock);
107 pthread_mutex_unlock(&mutex_lock);
109 if (eCode != SUCCESS_RESPONSE)
114 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
116 if (rep.hasAttribute("n"))
117 std::cout << "\t\tDeviceName:" << rep.getValue< std::string >("n") << std::endl;
118 if (rep.hasAttribute("loc"))
119 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
120 if (rep.hasAttribute("locn"))
121 std::cout << "\t\tLocationName:" << rep.getValue< std::string >("locn") << std::endl;
122 if (rep.hasAttribute("c"))
123 std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
124 if (rep.hasAttribute("r"))
125 std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
128 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
130 pthread_mutex_lock(&mutex_lock);
132 pthread_mutex_unlock(&mutex_lock);
134 if (eCode != SUCCESS_RESPONSE)
139 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
141 if (rep.hasAttribute("n"))
142 std::cout << "\t\tDeviceName:" << rep.getValue< std::string >("n") << std::endl;
143 if (rep.hasAttribute("loc"))
144 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
145 if (rep.hasAttribute("locn"))
146 std::cout << "\t\tLocationName:" << rep.getValue< std::string >("locn") << std::endl;
147 if (rep.hasAttribute("c"))
148 std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
149 if (rep.hasAttribute("r"))
150 std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
153 // Callback to found collection resource
154 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
156 std::string resourceURI;
157 std::string hostAddress;
160 // Do some operations with resource object.
161 for (unsigned int i = 0; i < resources.size(); ++i)
163 std::shared_ptr< OCResource > resource = resources.at(i);
167 if (resource->uri() == "/core/a/configuration/resourceset")
168 g_configurationCollection = resource;
169 else if (resource->uri() == "/core/a/maintenance/resourceset")
170 g_maintenanceCollection = resource;
171 else if (resource->uri() == "/core/a/factoryset/resourceset")
172 g_setCollection = resource;
175 pthread_mutex_lock(&mutex_lock);
177 pthread_mutex_unlock(&mutex_lock);
183 // Resource is invalid
184 std::cout << "Resource is invalid" << std::endl;
189 catch (std::exception& e)
191 std::cout << "Exception: " << e.what() << std::endl;
194 pthread_mutex_lock(&mutex_lock);
196 pthread_mutex_unlock(&mutex_lock);
199 // Callback to found resources
200 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
202 std::string resourceURI;
203 std::string hostAddress;
207 // Do some operations with resource object.
208 for (unsigned int i = 0; i < resources.size(); ++i)
210 std::shared_ptr< OCResource > resource = resources.at(i);
214 // Check if the resource is new one. If so, store it.
216 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
217 resourceTable.find(resource->host() + resource->uri());
219 if (iter == resourceTable.end()) // new one
221 resourceTable[resource->host() + resource->uri()] = resource;
223 OCResourceHandle foundResourceHandle = NULL;
224 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
226 std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
228 if (result == OC_STACK_OK)
230 if (resource->uri() == "/oic/con")
232 OCPlatform::bindResource(configurationCollectionHandle,
233 foundResourceHandle);
234 if (g_configurationResource == NULL)
235 g_configurationResource = resource;
237 else if (resource->uri() == "/oic/mnt")
239 OCPlatform::bindResource(maintenanceCollectionHandle,
240 foundResourceHandle);
241 if (g_maintenanceResource == NULL)
242 g_maintenanceResource = resource;
244 else if (resource->uri() == "/factoryset")
246 OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
247 if (g_setResource == NULL)
248 g_setResource = resource;
251 resourceHandleVector.push_back(foundResourceHandle);
255 cout << "\tresource Error!" << endl;
263 // Resource is invalid
264 std::cout << "Resource is invalid" << std::endl;
269 catch (std::exception& e)
271 std::cout << "Exception: " << e.what() << std::endl;
274 pthread_mutex_lock(&mutex_lock);
276 pthread_mutex_unlock(&mutex_lock);
281 std::string str_steps;
285 //**************************************************************
288 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
290 OCPlatform::Configure(cfg);
291 //g_thingsmanager = new ThingsManager();
292 g_thingsConf = new ThingsConfiguration();
293 g_thingsMnt = new ThingsMaintenance();
294 g_groupmanager = new GroupManager();
296 //**************************************************************
300 pthread_mutex_lock(&mutex_lock);
303 pthread_mutex_unlock(&mutex_lock);
308 pthread_mutex_unlock(&mutex_lock);
310 cout << endl << endl << "(0) Quit" << std::endl;
311 cout << "(1) Find all resources(URI: /oic/con, /oic/mnt, /factoryset)" << std::endl;
312 cout << "(2) Find all groups" << std::endl;
313 cout << "(3) Get a Configuration resource" << std::endl;
314 cout << "(4) Update a device name attribute value" << std::endl;
315 cout << "(5) FactoryReset (for the group)" << std::endl;
316 cout << "(6) Reboot (for the group)" << std::endl;
317 cout << "(10) Show Configuration Units" << std::endl;
321 std::getline (std::cin, str_steps);
329 g_Steps = std::stoi(str_steps);
332 catch(std::invalid_argument&)
334 std::cout << "Please put a digit, not string" << std::endl;
337 catch(std::out_of_range&)
339 std::cout << "Please put a number within the supported range" << std::endl;
347 else if (g_Steps == 1)
349 std::vector< std::string > types;
351 // For Registering a collection resource for configuration resources
352 if (configurationCollectionHandle == NULL)
354 string resourceURI = "/core/a/configuration/resourceset";
355 string resourceTypeName = "core.configuration.resourceset";
356 string resourceInterface = BATCH_INTERFACE;
358 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
359 resourceTypeName, resourceInterface, NULL,
360 //&entityHandler, // entityHandler
363 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
364 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
368 // For Registering a collection resource for maintenance resources
369 if (maintenanceCollectionHandle == NULL)
371 string resourceURI = "/core/a/maintenance/resourceset";
372 string resourceTypeName = "core.maintenance.resourceset";
373 string resourceInterface = BATCH_INTERFACE;
375 OCPlatform::registerResource(maintenanceCollectionHandle, resourceURI,
376 resourceTypeName, resourceInterface, NULL,
377 //&entityHandler, // entityHandler
380 OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, GROUP_INTERFACE);
381 OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, DEFAULT_INTERFACE);
384 // For Registering a collection resource for set resources
385 if (setCollectionHandle == NULL)
387 string resourceURI = "/core/a/factoryset/resourceset";
388 string resourceTypeName = "core.factoryset.resourceset";
389 string resourceInterface = BATCH_INTERFACE;
391 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
392 resourceInterface, NULL,
393 //&entityHandler, // entityHandler
396 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
397 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
400 types.push_back("oic.wk.con");
401 types.push_back("oic.wk.mnt");
402 types.push_back("factoryset");
404 std::cout << "Finding Configuration Resource... " << std::endl;
405 std::cout << "Finding Maintenance Resource... " << std::endl;
406 std::cout << "Finding Set Resource... " << std::endl;
408 g_groupmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
410 pthread_mutex_lock(&mutex_lock);
412 pthread_mutex_unlock(&mutex_lock);
414 thread t(&timeCheck, 5);
415 t.join(); // After 5 seconds, isWaiting value will be 0.
417 else if (g_Steps == 2) // make a group with found things
419 std::vector< std::string > types;
420 types.push_back("core.configuration.resourceset");
421 types.push_back("core.maintenance.resourceset");
422 types.push_back("core.factoryset.resourceset");
424 g_groupmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
426 std::cout << "Finding Collection resource... " << std::endl;
428 pthread_mutex_lock(&mutex_lock);
430 pthread_mutex_unlock(&mutex_lock);
432 thread t(&timeCheck, 5);
433 t.join(); // After 5 seconds, isWaiting value will be 0.
435 else if (g_Steps == 3)
439 ConfigurationName name = "all";
441 std::cout << "For example, get configuration resources's value" << std::endl;
443 std::vector< ConfigurationName > configurations;
445 configurations.push_back(name);
447 if (g_thingsConf->getConfigurations(g_configurationCollection, configurations, &onGet)
450 pthread_mutex_lock(&mutex_lock);
452 pthread_mutex_unlock(&mutex_lock);
455 else if (g_Steps == 4)
457 ConfigurationName name = "n";
458 ConfigurationValue value = "OIC Device";
460 if(g_configurationCollection == NULL)
462 std::cout<<"Note that you first create a group to use this command." << std::endl;
466 std::cout << "For example, change a device name" << std::endl;
467 std::cout << g_configurationCollection->uri() << std::endl;
469 std::map< ConfigurationName, ConfigurationValue > configurations;
471 configurations.insert(std::make_pair(name, value));
473 if (g_thingsConf->updateConfigurations(g_configurationCollection, configurations,
474 &onUpdate) != OC_STACK_ERROR)
476 pthread_mutex_lock(&mutex_lock);
478 pthread_mutex_unlock(&mutex_lock);
481 else if (g_Steps == 5)
484 if(g_maintenanceCollection == NULL)
486 std::cout<<"Note that you first create a group to use this command." << std::endl;
490 if (g_thingsMnt->factoryReset(g_maintenanceCollection, &onFactoryReset)
493 pthread_mutex_lock(&mutex_lock);
495 pthread_mutex_unlock(&mutex_lock);
498 else if (g_Steps == 6)
501 if(g_maintenanceCollection == NULL)
503 std::cout<<"Note that you first create a group to use this command." << std::endl;
507 if (g_thingsMnt->reboot(g_maintenanceCollection, &onReboot) != OC_STACK_ERROR)
509 pthread_mutex_lock(&mutex_lock);
511 pthread_mutex_unlock(&mutex_lock);
514 else if (g_Steps == 10)
516 std::cout << g_thingsConf->getListOfSupportedConfigurationUnits() << std::endl;
520 }catch (OCException e)
522 std::cout << "Exception in main: " << e.what();