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"
36 int isWaiting = 0; //0: none to wait, 1: wait for the response of "getConfigurationValue"
37 pthread_mutex_t mutex_lock;
39 const int SUCCESS_RESPONSE = 0;
41 static ThingsManager* g_thingsmanager;
43 OCResourceHandle configurationCollectionHandle;
44 std::shared_ptr< OCResource > g_configurationCollection; // for a group of multiple resources
45 std::shared_ptr< OCResource > g_configurationResource; // For a single resource
47 OCResourceHandle diagnosticsCollectionHandle;
48 std::shared_ptr< OCResource > g_diagnosticsCollection; // for a group of multiple resources
49 std::shared_ptr< OCResource > g_diagnosticsResource; // For a single resource
51 OCResourceHandle setCollectionHandle;
52 std::shared_ptr< OCResource > g_setCollection; // for a group of multiple resources
53 std::shared_ptr< OCResource > g_setResource; // For a single resource
55 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
56 std::vector< OCResourceHandle > resourceHandleVector;
58 typedef std::function<
59 void(const HeaderOptions& headerOptions,
60 const OCRepresentation& rep, const int eCode) > ConfigurationCallback;
61 typedef std::function<
62 void(const HeaderOptions& headerOptions,
63 const OCRepresentation& rep, const int eCode) > DiagnosticsCallback;
65 typedef std::string ConfigurationName;
66 typedef std::string ConfigurationValue;
68 void timeCheck(int timeSec)
71 pthread_mutex_lock(&mutex_lock);
73 pthread_mutex_unlock(&mutex_lock);
76 void onReboot(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
78 pthread_mutex_lock(&mutex_lock);
80 pthread_mutex_unlock(&mutex_lock);
82 if (eCode != SUCCESS_RESPONSE)
87 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
88 std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
91 void onFactoryReset(const HeaderOptions& headerOptions, const OCRepresentation& rep,
94 pthread_mutex_lock(&mutex_lock);
96 pthread_mutex_unlock(&mutex_lock);
98 if (eCode != SUCCESS_RESPONSE)
103 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
104 std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
107 void onUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
109 pthread_mutex_lock(&mutex_lock);
111 pthread_mutex_unlock(&mutex_lock);
113 if (eCode != SUCCESS_RESPONSE)
118 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
120 if (rep.hasAttribute("loc"))
121 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
122 if (rep.hasAttribute("st"))
123 std::cout << "\t\tSystemTime:" << rep.getValue< std::string >("st") << std::endl;
124 if (rep.hasAttribute("c"))
125 std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
126 if (rep.hasAttribute("r"))
127 std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
130 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
132 pthread_mutex_lock(&mutex_lock);
134 pthread_mutex_unlock(&mutex_lock);
136 if (eCode != SUCCESS_RESPONSE)
141 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
143 if (rep.hasAttribute("loc"))
144 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
145 if (rep.hasAttribute("st"))
146 std::cout << "\t\tSystemTime:" << rep.getValue< std::string >("st") << 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/diagnostics/resourceset")
170 g_diagnosticsCollection = resource;
171 else if (resource->uri() == "/core/a/factoryset/resourceset")
172 g_setCollection = resource;
181 // Resource is invalid
182 std::cout << "Resource is invalid" << std::endl;
187 catch (std::exception& e)
189 std::cout << "Exception: " << e.what() << std::endl;
195 // Callback to found resources
196 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
198 std::string resourceURI;
199 std::string hostAddress;
203 // Do some operations with resource object.
204 for (unsigned int i = 0; i < resources.size(); ++i)
206 std::shared_ptr< OCResource > resource = resources.at(i);
210 // Check if the resource is new one. If so, store it.
212 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
213 resourceTable.find(resource->host() + resource->uri());
215 if (iter == resourceTable.end()) // new one
217 resourceTable[resource->host() + resource->uri()] = resource;
219 OCResourceHandle foundResourceHandle;
220 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
222 std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
224 if (result == OC_STACK_OK)
226 if (resource->uri() == "/oic/con")
228 OCPlatform::bindResource(configurationCollectionHandle,
229 foundResourceHandle);
230 if (g_configurationResource == NULL)
231 g_configurationResource = resource;
233 else if (resource->uri() == "/oic/diag")
235 OCPlatform::bindResource(diagnosticsCollectionHandle,
236 foundResourceHandle);
237 if (g_diagnosticsResource == NULL)
238 g_diagnosticsResource = resource;
240 else if (resource->uri() == "/factorySet")
242 OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
243 if (g_setResource == NULL)
244 g_setResource = resource;
247 resourceHandleVector.push_back(foundResourceHandle);
251 cout << "\tresource Error!" << endl;
259 // Resource is invalid
260 std::cout << "Resource is invalid" << std::endl;
265 catch (std::exception& e)
267 std::cout << "Exception: " << e.what() << std::endl;
273 int main(int argc, char* argv[])
275 std::string str_steps;
276 pthread_mutex_init(&mutex_lock, NULL);
278 //**************************************************************
281 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
283 OCPlatform::Configure(cfg);
284 g_thingsmanager = new ThingsManager();
286 //**************************************************************
290 pthread_mutex_lock(&mutex_lock);
293 pthread_mutex_unlock(&mutex_lock);
298 pthread_mutex_unlock(&mutex_lock);
300 cout << endl << endl << "(0) Quit" << std::endl;
301 cout << "(1) Find all resources(URI: /oic/con, /oic/diag, /factoryset)" << std::endl;
302 cout << "(2) Find all groups" << std::endl;
303 cout << "(3) Get a Configuration resource" << std::endl;
304 cout << "(4) Update a region attribute value" << std::endl;
305 cout << "(5) FactoryReset (for the group)" << std::endl;
306 cout << "(6) Reboot (for the group)" << std::endl;
307 cout << "(10) Show Configuration Units" << std::endl;
311 std::getline (std::cin, str_steps);
319 g_Steps = std::stoi(str_steps);
322 catch(std::invalid_argument&)
324 std::cout << "Please put a digit, not string" << std::endl;
332 else if (g_Steps == 1)
334 std::vector< std::string > types;
336 // For Registering a collection resource for configuration resources
337 if (configurationCollectionHandle == NULL)
339 string resourceURI = "/core/a/configuration/resourceset";
340 string resourceTypeName = "core.configuration.resourceset";
341 string resourceInterface = BATCH_INTERFACE;
343 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
344 resourceTypeName, resourceInterface, NULL,
345 //&entityHandler, // entityHandler
348 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
349 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
353 // For Registering a collection resource for diagnostics resources
354 if (diagnosticsCollectionHandle == NULL)
356 string resourceURI = "/core/a/diagnostics/resourceset";
357 string resourceTypeName = "core.diagnostics.resourceset";
358 string resourceInterface = BATCH_INTERFACE;
360 OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
361 resourceTypeName, resourceInterface, NULL,
362 //&entityHandler, // entityHandler
365 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
366 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
369 // For Registering a collection resource for set resources
370 if (setCollectionHandle == NULL)
372 string resourceURI = "/core/a/factoryset/resourceset";
373 string resourceTypeName = "core.factoryset.resourceset";
374 string resourceInterface = BATCH_INTERFACE;
376 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
377 resourceInterface, NULL,
378 //&entityHandler, // entityHandler
381 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
382 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
385 types.push_back("oic.con");
386 types.push_back("oic.diag");
387 types.push_back("factorySet");
389 std::cout << "Finding Configuration Resource... " << std::endl;
390 std::cout << "Finding Diagnostics Resource... " << std::endl;
391 std::cout << "Finding Set Resource... " << std::endl;
393 g_thingsmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
397 thread t(&timeCheck, 5);
398 t.join(); // After 5 seconds, isWaiting value will be 0.
400 else if (g_Steps == 2) // make a group with found things
402 std::vector< std::string > types;
403 types.push_back("core.configuration.resourceset");
404 types.push_back("core.diagnostics.resourceset");
405 types.push_back("core.factoryset.resourceset");
407 g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
409 std::cout << "Finding Collection resource... " << std::endl;
413 thread t(&timeCheck, 5);
414 t.join(); // After 5 seconds, isWaiting value will be 0.
416 else if (g_Steps == 3)
420 ConfigurationName name = "all";
422 std::cout << "For example, get configuration resources's value" << std::endl;
424 std::vector< ConfigurationName > configurations;
426 configurations.push_back(name);
428 if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations, &onGet)
431 pthread_mutex_lock(&mutex_lock);
433 pthread_mutex_unlock(&mutex_lock);
436 else if (g_Steps == 4)
438 ConfigurationName name = "r";
439 ConfigurationValue value = "U.S.A (new region)";
441 if(g_configurationCollection == NULL)
443 std::cout<<"Note that you first create a group to use this command." << std::endl;
447 std::cout << "For example, change region resource's value" << std::endl;
448 std::cout << g_configurationCollection->uri() << std::endl;
450 std::map< ConfigurationName, ConfigurationValue > configurations;
452 configurations.insert(std::make_pair(name, value));
454 if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
455 &onUpdate) != OC_STACK_ERROR)
457 pthread_mutex_lock(&mutex_lock);
459 pthread_mutex_unlock(&mutex_lock);
462 else if (g_Steps == 5)
465 if(g_diagnosticsCollection == NULL)
467 std::cout<<"Note that you first create a group to use this command." << std::endl;
471 if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
474 pthread_mutex_lock(&mutex_lock);
476 pthread_mutex_unlock(&mutex_lock);
479 else if (g_Steps == 6)
482 if(g_diagnosticsCollection == NULL)
484 std::cout<<"Note that you first create a group to use this command." << std::endl;
488 if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
490 pthread_mutex_lock(&mutex_lock);
492 pthread_mutex_unlock(&mutex_lock);
495 else if (g_Steps == 10)
497 std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;