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 "ThingsManager.h"
31 #include "ThingsConfiguration.h"
32 #include "ThingsMaintenance.h"
33 #include "GroupManager.h"
39 int isWaiting = 0; //0: none to wait, 1: wait for the response of "getConfigurationValue"
40 pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
42 const int SUCCESS_RESPONSE = 0;
44 //static ThingsManager* g_thingsmanager;
45 static GroupManager* g_groupmanager;
46 static ThingsConfiguration* g_thingsConf;
47 static ThingsMaintenance* g_thingsMnt;
49 OCResourceHandle configurationCollectionHandle;
50 std::shared_ptr< OCResource > g_configurationCollection; // for a group of multiple resources
51 std::shared_ptr< OCResource > g_configurationResource; // For a single resource
53 OCResourceHandle maintenanceCollectionHandle;
54 std::shared_ptr< OCResource > g_maintenanceCollection; // for a group of multiple resources
55 std::shared_ptr< OCResource > g_maintenanceResource; // For a single resource
57 OCResourceHandle setCollectionHandle;
58 std::shared_ptr< OCResource > g_setCollection; // for a group of multiple resources
59 std::shared_ptr< OCResource > g_setResource; // For a single resource
61 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
62 std::vector< OCResourceHandle > resourceHandleVector;
64 typedef std::string ConfigurationName;
65 typedef std::string ConfigurationValue;
67 void timeCheck(int timeSec)
70 pthread_mutex_lock(&mutex_lock);
72 pthread_mutex_unlock(&mutex_lock);
75 void onReboot(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
77 pthread_mutex_lock(&mutex_lock);
79 pthread_mutex_unlock(&mutex_lock);
81 if (eCode != SUCCESS_RESPONSE)
86 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
87 std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
90 void onFactoryReset(const HeaderOptions& headerOptions, const OCRepresentation& rep,
93 pthread_mutex_lock(&mutex_lock);
95 pthread_mutex_unlock(&mutex_lock);
97 if (eCode != SUCCESS_RESPONSE)
102 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
103 std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
106 void onUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
108 pthread_mutex_lock(&mutex_lock);
110 pthread_mutex_unlock(&mutex_lock);
112 if (eCode != SUCCESS_RESPONSE)
117 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
119 if (rep.hasAttribute("n"))
120 std::cout << "\t\tDeviceName:" << rep.getValue< std::string >("n") << std::endl;
121 if (rep.hasAttribute("loc"))
122 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
123 if (rep.hasAttribute("locn"))
124 std::cout << "\t\tLocationName:" << rep.getValue< std::string >("locn") << std::endl;
125 if (rep.hasAttribute("c"))
126 std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
127 if (rep.hasAttribute("r"))
128 std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
131 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
133 pthread_mutex_lock(&mutex_lock);
135 pthread_mutex_unlock(&mutex_lock);
137 if (eCode != SUCCESS_RESPONSE)
142 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
144 if (rep.hasAttribute("n"))
145 std::cout << "\t\tDeviceName:" << rep.getValue< std::string >("n") << std::endl;
146 if (rep.hasAttribute("loc"))
147 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
148 if (rep.hasAttribute("locn"))
149 std::cout << "\t\tLocationName:" << rep.getValue< std::string >("locn") << std::endl;
150 if (rep.hasAttribute("c"))
151 std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
152 if (rep.hasAttribute("r"))
153 std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
156 // Callback to found collection resource
157 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
159 std::string resourceURI;
160 std::string hostAddress;
163 // Do some operations with resource object.
164 for (unsigned int i = 0; i < resources.size(); ++i)
166 std::shared_ptr< OCResource > resource = resources.at(i);
170 if (resource->uri() == "/core/a/configuration/resourceset")
171 g_configurationCollection = resource;
172 else if (resource->uri() == "/core/a/maintenance/resourceset")
173 g_maintenanceCollection = resource;
174 else if (resource->uri() == "/core/a/factoryset/resourceset")
175 g_setCollection = resource;
178 pthread_mutex_lock(&mutex_lock);
180 pthread_mutex_unlock(&mutex_lock);
186 // Resource is invalid
187 std::cout << "Resource is invalid" << std::endl;
192 catch (std::exception& e)
194 std::cout << "Exception: " << e.what() << std::endl;
200 // Callback to found resources
201 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
203 std::string resourceURI;
204 std::string hostAddress;
208 // Do some operations with resource object.
209 for (unsigned int i = 0; i < resources.size(); ++i)
211 std::shared_ptr< OCResource > resource = resources.at(i);
215 // Check if the resource is new one. If so, store it.
217 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
218 resourceTable.find(resource->host() + resource->uri());
220 if (iter == resourceTable.end()) // new one
222 resourceTable[resource->host() + resource->uri()] = resource;
224 OCResourceHandle foundResourceHandle;
225 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
227 std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
229 if (result == OC_STACK_OK)
231 if (resource->uri() == "/oic/con")
233 OCPlatform::bindResource(configurationCollectionHandle,
234 foundResourceHandle);
235 if (g_configurationResource == NULL)
236 g_configurationResource = resource;
238 else if (resource->uri() == "/oic/mnt")
240 OCPlatform::bindResource(maintenanceCollectionHandle,
241 foundResourceHandle);
242 if (g_maintenanceResource == NULL)
243 g_maintenanceResource = resource;
245 else if (resource->uri() == "/factorySet")
247 OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
248 if (g_setResource == NULL)
249 g_setResource = resource;
252 resourceHandleVector.push_back(foundResourceHandle);
256 cout << "\tresource Error!" << endl;
264 // Resource is invalid
265 std::cout << "Resource is invalid" << std::endl;
270 catch (std::exception& e)
272 std::cout << "Exception: " << e.what() << std::endl;
275 pthread_mutex_lock(&mutex_lock);
277 pthread_mutex_unlock(&mutex_lock);
280 int main(int argc, char* argv[])
282 std::string str_steps;
286 //**************************************************************
289 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
291 OCPlatform::Configure(cfg);
292 //g_thingsmanager = new ThingsManager();
293 g_thingsConf = new ThingsConfiguration();
294 g_thingsMnt = new ThingsMaintenance();
295 g_groupmanager = new GroupManager();
297 //**************************************************************
301 pthread_mutex_lock(&mutex_lock);
304 pthread_mutex_unlock(&mutex_lock);
309 pthread_mutex_unlock(&mutex_lock);
311 cout << endl << endl << "(0) Quit" << std::endl;
312 cout << "(1) Find all resources(URI: /oic/con, /oic/mnt, /factoryset)" << std::endl;
313 cout << "(2) Find all groups" << std::endl;
314 cout << "(3) Get a Configuration resource" << std::endl;
315 cout << "(4) Update a region attribute value" << std::endl;
316 cout << "(5) FactoryReset (for the group)" << std::endl;
317 cout << "(6) Reboot (for the group)" << std::endl;
318 cout << "(10) Show Configuration Units" << std::endl;
322 std::getline (std::cin, str_steps);
330 g_Steps = std::stoi(str_steps);
333 catch(std::invalid_argument&)
335 std::cout << "Please put a digit, not string" << std::endl;
343 else if (g_Steps == 1)
345 std::vector< std::string > types;
347 // For Registering a collection resource for configuration resources
348 if (configurationCollectionHandle == NULL)
350 string resourceURI = "/core/a/configuration/resourceset";
351 string resourceTypeName = "core.configuration.resourceset";
352 string resourceInterface = BATCH_INTERFACE;
354 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
355 resourceTypeName, resourceInterface, NULL,
356 //&entityHandler, // entityHandler
359 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
360 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
364 // For Registering a collection resource for maintenance resources
365 if (maintenanceCollectionHandle == NULL)
367 string resourceURI = "/core/a/maintenance/resourceset";
368 string resourceTypeName = "core.maintenance.resourceset";
369 string resourceInterface = BATCH_INTERFACE;
371 OCPlatform::registerResource(maintenanceCollectionHandle, resourceURI,
372 resourceTypeName, resourceInterface, NULL,
373 //&entityHandler, // entityHandler
376 OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, GROUP_INTERFACE);
377 OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, DEFAULT_INTERFACE);
380 // For Registering a collection resource for set resources
381 if (setCollectionHandle == NULL)
383 string resourceURI = "/core/a/factoryset/resourceset";
384 string resourceTypeName = "core.factoryset.resourceset";
385 string resourceInterface = BATCH_INTERFACE;
387 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
388 resourceInterface, NULL,
389 //&entityHandler, // entityHandler
392 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
393 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
396 types.push_back("oic.wk.con");
397 types.push_back("oic.wk.mnt");
398 types.push_back("factorySet");
400 std::cout << "Finding Configuration Resource... " << std::endl;
401 std::cout << "Finding Maintenance Resource... " << std::endl;
402 std::cout << "Finding Set Resource... " << std::endl;
404 g_groupmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
406 pthread_mutex_lock(&mutex_lock);
408 pthread_mutex_unlock(&mutex_lock);
410 thread t(&timeCheck, 5);
411 t.join(); // After 5 seconds, isWaiting value will be 0.
413 else if (g_Steps == 2) // make a group with found things
415 std::vector< std::string > types;
416 types.push_back("core.configuration.resourceset");
417 types.push_back("core.maintenance.resourceset");
418 types.push_back("core.factoryset.resourceset");
420 g_groupmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
422 std::cout << "Finding Collection resource... " << std::endl;
424 pthread_mutex_lock(&mutex_lock);
426 pthread_mutex_unlock(&mutex_lock);
428 thread t(&timeCheck, 5);
429 t.join(); // After 5 seconds, isWaiting value will be 0.
431 else if (g_Steps == 3)
435 ConfigurationName name = "all";
437 std::cout << "For example, get configuration resources's value" << std::endl;
439 std::vector< ConfigurationName > configurations;
441 configurations.push_back(name);
443 if (g_thingsConf->getConfigurations(g_configurationCollection, configurations, &onGet)
446 pthread_mutex_lock(&mutex_lock);
448 pthread_mutex_unlock(&mutex_lock);
451 else if (g_Steps == 4)
453 ConfigurationName name = "n";
454 ConfigurationValue value = "OIC Device";
456 if(g_configurationCollection == NULL)
458 std::cout<<"Note that you first create a group to use this command." << std::endl;
462 std::cout << "For example, change region resource's value" << std::endl;
463 std::cout << g_configurationCollection->uri() << std::endl;
465 std::map< ConfigurationName, ConfigurationValue > configurations;
467 configurations.insert(std::make_pair(name, value));
469 if (g_thingsConf->updateConfigurations(g_configurationCollection, configurations,
470 &onUpdate) != OC_STACK_ERROR)
472 pthread_mutex_lock(&mutex_lock);
474 pthread_mutex_unlock(&mutex_lock);
477 else if (g_Steps == 5)
480 if(g_maintenanceCollection == NULL)
482 std::cout<<"Note that you first create a group to use this command." << std::endl;
486 if (g_thingsMnt->factoryReset(g_maintenanceCollection, &onFactoryReset)
489 pthread_mutex_lock(&mutex_lock);
491 pthread_mutex_unlock(&mutex_lock);
494 else if (g_Steps == 6)
497 if(g_maintenanceCollection == NULL)
499 std::cout<<"Note that you first create a group to use this command." << std::endl;
503 if (g_thingsMnt->reboot(g_maintenanceCollection, &onReboot) != OC_STACK_ERROR)
505 pthread_mutex_lock(&mutex_lock);
507 pthread_mutex_unlock(&mutex_lock);
510 else if (g_Steps == 10)
512 std::cout << g_thingsConf->getListOfSupportedConfigurationUnits() << std::endl;
516 }catch (OCException e)
518 std::cout << "Exception in main: " << e.what();