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 = PTHREAD_MUTEX_INITIALIZER;
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;
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;
197 // Callback to found resources
198 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
200 std::string resourceURI;
201 std::string hostAddress;
205 // Do some operations with resource object.
206 for (unsigned int i = 0; i < resources.size(); ++i)
208 std::shared_ptr< OCResource > resource = resources.at(i);
212 // Check if the resource is new one. If so, store it.
214 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
215 resourceTable.find(resource->host() + resource->uri());
217 if (iter == resourceTable.end()) // new one
219 resourceTable[resource->host() + resource->uri()] = resource;
221 OCResourceHandle foundResourceHandle;
222 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
224 std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
226 if (result == OC_STACK_OK)
228 if (resource->uri() == "/oic/con")
230 OCPlatform::bindResource(configurationCollectionHandle,
231 foundResourceHandle);
232 if (g_configurationResource == NULL)
233 g_configurationResource = resource;
235 else if (resource->uri() == "/oic/diag")
237 OCPlatform::bindResource(diagnosticsCollectionHandle,
238 foundResourceHandle);
239 if (g_diagnosticsResource == NULL)
240 g_diagnosticsResource = resource;
242 else if (resource->uri() == "/factorySet")
244 OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
245 if (g_setResource == NULL)
246 g_setResource = resource;
249 resourceHandleVector.push_back(foundResourceHandle);
253 cout << "\tresource Error!" << endl;
261 // Resource is invalid
262 std::cout << "Resource is invalid" << std::endl;
267 catch (std::exception& e)
269 std::cout << "Exception: " << e.what() << std::endl;
272 pthread_mutex_lock(&mutex_lock);
274 pthread_mutex_unlock(&mutex_lock);
277 int main(int argc, char* argv[])
279 std::string str_steps;
283 //**************************************************************
286 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
288 OCPlatform::Configure(cfg);
289 g_thingsmanager = new ThingsManager();
291 //**************************************************************
295 pthread_mutex_lock(&mutex_lock);
298 pthread_mutex_unlock(&mutex_lock);
303 pthread_mutex_unlock(&mutex_lock);
305 cout << endl << endl << "(0) Quit" << std::endl;
306 cout << "(1) Find all resources(URI: /oic/con, /oic/diag, /factoryset)" << std::endl;
307 cout << "(2) Find all groups" << std::endl;
308 cout << "(3) Get a Configuration resource" << std::endl;
309 cout << "(4) Update a region attribute value" << std::endl;
310 cout << "(5) FactoryReset (for the group)" << std::endl;
311 cout << "(6) Reboot (for the group)" << std::endl;
312 cout << "(10) Show Configuration Units" << std::endl;
316 std::getline (std::cin, str_steps);
324 g_Steps = std::stoi(str_steps);
327 catch(std::invalid_argument&)
329 std::cout << "Please put a digit, not string" << std::endl;
337 else if (g_Steps == 1)
339 std::vector< std::string > types;
341 // For Registering a collection resource for configuration resources
342 if (configurationCollectionHandle == NULL)
344 string resourceURI = "/core/a/configuration/resourceset";
345 string resourceTypeName = "core.configuration.resourceset";
346 string resourceInterface = BATCH_INTERFACE;
348 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
349 resourceTypeName, resourceInterface, NULL,
350 //&entityHandler, // entityHandler
353 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
354 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
358 // For Registering a collection resource for diagnostics resources
359 if (diagnosticsCollectionHandle == NULL)
361 string resourceURI = "/core/a/diagnostics/resourceset";
362 string resourceTypeName = "core.diagnostics.resourceset";
363 string resourceInterface = BATCH_INTERFACE;
365 OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
366 resourceTypeName, resourceInterface, NULL,
367 //&entityHandler, // entityHandler
370 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
371 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
374 // For Registering a collection resource for set resources
375 if (setCollectionHandle == NULL)
377 string resourceURI = "/core/a/factoryset/resourceset";
378 string resourceTypeName = "core.factoryset.resourceset";
379 string resourceInterface = BATCH_INTERFACE;
381 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
382 resourceInterface, NULL,
383 //&entityHandler, // entityHandler
386 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
387 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
390 types.push_back("oic.con");
391 types.push_back("oic.diag");
392 types.push_back("factorySet");
394 std::cout << "Finding Configuration Resource... " << std::endl;
395 std::cout << "Finding Diagnostics Resource... " << std::endl;
396 std::cout << "Finding Set Resource... " << std::endl;
398 g_thingsmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
400 pthread_mutex_lock(&mutex_lock);
402 pthread_mutex_unlock(&mutex_lock);
404 thread t(&timeCheck, 5);
405 t.join(); // After 5 seconds, isWaiting value will be 0.
407 else if (g_Steps == 2) // make a group with found things
409 std::vector< std::string > types;
410 types.push_back("core.configuration.resourceset");
411 types.push_back("core.diagnostics.resourceset");
412 types.push_back("core.factoryset.resourceset");
414 g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
416 std::cout << "Finding Collection resource... " << std::endl;
418 pthread_mutex_lock(&mutex_lock);
420 pthread_mutex_unlock(&mutex_lock);
422 thread t(&timeCheck, 5);
423 t.join(); // After 5 seconds, isWaiting value will be 0.
425 else if (g_Steps == 3)
429 ConfigurationName name = "all";
431 std::cout << "For example, get configuration resources's value" << std::endl;
433 std::vector< ConfigurationName > configurations;
435 configurations.push_back(name);
437 if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations, &onGet)
440 pthread_mutex_lock(&mutex_lock);
442 pthread_mutex_unlock(&mutex_lock);
445 else if (g_Steps == 4)
447 ConfigurationName name = "r";
448 ConfigurationValue value = "U.S.A (new region)";
450 if(g_configurationCollection == NULL)
452 std::cout<<"Note that you first create a group to use this command." << std::endl;
456 std::cout << "For example, change region resource's value" << std::endl;
457 std::cout << g_configurationCollection->uri() << std::endl;
459 std::map< ConfigurationName, ConfigurationValue > configurations;
461 configurations.insert(std::make_pair(name, value));
463 if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
464 &onUpdate) != OC_STACK_ERROR)
466 pthread_mutex_lock(&mutex_lock);
468 pthread_mutex_unlock(&mutex_lock);
471 else if (g_Steps == 5)
474 if(g_diagnosticsCollection == NULL)
476 std::cout<<"Note that you first create a group to use this command." << std::endl;
480 if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
483 pthread_mutex_lock(&mutex_lock);
485 pthread_mutex_unlock(&mutex_lock);
488 else if (g_Steps == 6)
491 if(g_diagnosticsCollection == NULL)
493 std::cout<<"Note that you first create a group to use this command." << std::endl;
497 if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
499 pthread_mutex_lock(&mutex_lock);
501 pthread_mutex_unlock(&mutex_lock);
504 else if (g_Steps == 10)
506 std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;
510 }catch (OCException e)
512 std::cout << "Exception in main: " << e.what();