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"
38 const int SUCCESS_RESPONSE = 0;
40 static ThingsManager* g_thingsmanager;
42 OCResourceHandle configurationCollectionHandle;
43 std::shared_ptr< OCResource > g_configurationCollection; // for a group of multiple resources
44 std::shared_ptr< OCResource > g_configurationResource; // For a single resource
46 OCResourceHandle diagnosticsCollectionHandle;
47 std::shared_ptr< OCResource > g_diagnosticsCollection; // for a group of multiple resources
48 std::shared_ptr< OCResource > g_diagnosticsResource; // For a single resource
50 OCResourceHandle setCollectionHandle;
51 std::shared_ptr< OCResource > g_setCollection; // for a group of multiple resources
52 std::shared_ptr< OCResource > g_setResource; // For a single resource
54 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
55 std::vector< OCResourceHandle > resourceHandleVector;
57 typedef std::function<
58 void(const HeaderOptions& headerOptions,
59 const OCRepresentation& rep, const int eCode) > ConfigurationCallback;
60 typedef std::function<
61 void(const HeaderOptions& headerOptions,
62 const OCRepresentation& rep, const int eCode) > DiagnosticsCallback;
64 typedef std::string ConfigurationName;
65 typedef std::string ConfigurationValue;
67 void timeCheck(int timeSec)
73 void onReboot(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
77 if (eCode != SUCCESS_RESPONSE)
82 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
83 std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
86 void onFactoryReset(const HeaderOptions& headerOptions, const OCRepresentation& rep,
91 if (eCode != SUCCESS_RESPONSE)
96 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
97 std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
100 void onUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
104 if (eCode != SUCCESS_RESPONSE)
109 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
111 if (rep.hasAttribute("loc"))
112 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
113 if (rep.hasAttribute("st"))
114 std::cout << "\t\tSystemTime:" << rep.getValue< std::string >("st") << std::endl;
115 if (rep.hasAttribute("c"))
116 std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
117 if (rep.hasAttribute("r"))
118 std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
121 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
125 if (eCode != SUCCESS_RESPONSE)
130 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
132 if (rep.hasAttribute("loc"))
133 std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
134 if (rep.hasAttribute("st"))
135 std::cout << "\t\tSystemTime:" << rep.getValue< std::string >("st") << std::endl;
136 if (rep.hasAttribute("c"))
137 std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
138 if (rep.hasAttribute("r"))
139 std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
142 // Callback to found collection resource
143 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
145 std::string resourceURI;
146 std::string hostAddress;
149 // Do some operations with resource object.
150 for (unsigned int i = 0; i < resources.size(); ++i)
152 std::shared_ptr< OCResource > resource = resources.at(i);
156 if (resource->uri() == "/core/a/configuration/resourceset")
157 g_configurationCollection = resource;
158 else if (resource->uri() == "/core/a/diagnostics/resourceset")
159 g_diagnosticsCollection = resource;
160 else if (resource->uri() == "/core/a/factoryset/resourceset")
161 g_setCollection = resource;
170 // Resource is invalid
171 std::cout << "Resource is invalid" << std::endl;
176 catch (std::exception& e)
184 // Callback to found resources
185 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
188 std::string resourceURI;
189 std::string hostAddress;
193 // Do some operations with resource object.
194 for (unsigned int i = 0; i < resources.size(); ++i)
196 std::shared_ptr< OCResource > resource = resources.at(i);
200 // Check if the resource is new one. If so, store it.
202 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
203 resourceTable.find(resource->host() + resource->uri());
205 if (iter == resourceTable.end()) // new one
207 resourceTable[resource->host() + resource->uri()] = resource;
209 OCResourceHandle foundResourceHandle;
210 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
212 std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
214 if (result == OC_STACK_OK)
216 if (resource->uri() == "/oic/con")
218 OCPlatform::bindResource(configurationCollectionHandle,
219 foundResourceHandle);
220 if (g_configurationResource == NULL)
221 g_configurationResource = resource;
223 else if (resource->uri() == "/oic/diag")
225 OCPlatform::bindResource(diagnosticsCollectionHandle,
226 foundResourceHandle);
227 if (g_diagnosticsResource == NULL)
228 g_diagnosticsResource = resource;
230 else if (resource->uri() == "/factorySet")
232 OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
233 if (g_setResource == NULL)
234 g_setResource = resource;
237 resourceHandleVector.push_back(foundResourceHandle);
241 cout << "\tresource Error!" << endl;
249 // Resource is invalid
250 std::cout << "Resource is invalid" << std::endl;
255 catch (std::exception& e)
263 int main(int argc, char* argv[])
265 std::string str_steps;
267 //**************************************************************
270 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
272 OCPlatform::Configure(cfg);
273 g_thingsmanager = new ThingsManager();
275 //**************************************************************
285 cout << endl << endl << "(0) Quit" << std::endl;
286 cout << "(1) Find all resources(URI: /oic/con, /oic/diag, /factoryset)" << std::endl;
287 cout << "(2) Find all groups" << std::endl;
288 cout << "(3) Get a Configuration resource" << std::endl;
289 cout << "(4) Update a region attribute value" << std::endl;
290 cout << "(5) FactoryReset (for the group)" << std::endl;
291 cout << "(6) Reboot (for the group)" << std::endl;
292 cout << "(10) Show Configuration Units" << std::endl;
296 std::getline (std::cin, str_steps);
301 g_Steps = std::stoi(str_steps);
303 catch(std::invalid_argument&)
305 std::cout << "Please put a digit, not string" << std::endl;
311 else if (g_Steps == 1)
313 std::vector< std::string > types;
315 // For Registering a collection resource for configuration resources
316 if (configurationCollectionHandle == NULL)
318 string resourceURI = "/core/a/configuration/resourceset";
319 string resourceTypeName = "core.configuration.resourceset";
320 string resourceInterface = BATCH_INTERFACE;
322 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
323 resourceTypeName, resourceInterface, NULL,
324 //&entityHandler, // entityHandler
327 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
328 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
332 // For Registering a collection resource for diagnostics resources
333 if (diagnosticsCollectionHandle == NULL)
335 string resourceURI = "/core/a/diagnostics/resourceset";
336 string resourceTypeName = "core.diagnostics.resourceset";
337 string resourceInterface = BATCH_INTERFACE;
339 OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
340 resourceTypeName, resourceInterface, NULL,
341 //&entityHandler, // entityHandler
344 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
345 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
348 // For Registering a collection resource for set resources
349 if (setCollectionHandle == NULL)
351 string resourceURI = "/core/a/factoryset/resourceset";
352 string resourceTypeName = "core.factoryset.resourceset";
353 string resourceInterface = BATCH_INTERFACE;
355 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
356 resourceInterface, NULL,
357 //&entityHandler, // entityHandler
360 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
361 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
364 types.push_back("oic.con");
365 types.push_back("oic.diag");
366 types.push_back("factorySet");
368 std::cout << "Finding Configuration Resource... " << std::endl;
369 std::cout << "Finding Diagnostics Resource... " << std::endl;
370 std::cout << "Finding Set Resource... " << std::endl;
372 g_thingsmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
376 thread t(&timeCheck, 5);
377 t.join(); // After 5 seconds, isWaiting value will be 0.
379 else if (g_Steps == 2) // make a group with found things
381 std::vector< std::string > types;
382 types.push_back("core.configuration.resourceset");
383 types.push_back("core.diagnostics.resourceset");
384 types.push_back("core.factoryset.resourceset");
386 g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
388 std::cout << "Finding Collection resource... " << std::endl;
392 thread t(&timeCheck, 5);
393 t.join(); // After 5 seconds, isWaiting value will be 0.
395 else if (g_Steps == 3)
399 ConfigurationName name = "all";
401 std::cout << "For example, get configuration resources's value" << std::endl;
403 std::vector< ConfigurationName > configurations;
405 configurations.push_back(name);
407 if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations, &onGet)
411 else if (g_Steps == 4)
413 ConfigurationName name = "r";
414 ConfigurationValue value = "U.S.A (new region)";
416 if(g_configurationCollection == NULL)
418 std::cout<<"Note that you first create a group to use this command." << std::endl;
422 std::cout << "For example, change region resource's value" << std::endl;
423 std::cout << g_configurationCollection->uri() << std::endl;
425 std::map< ConfigurationName, ConfigurationValue > configurations;
427 configurations.insert(std::make_pair(name, value));
429 if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
430 &onUpdate) != OC_STACK_ERROR)
433 else if (g_Steps == 5)
436 if(g_diagnosticsCollection == NULL)
438 std::cout<<"Note that you first create a group to use this command." << std::endl;
442 if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
446 else if (g_Steps == 6)
449 if(g_diagnosticsCollection == NULL)
451 std::cout<<"Note that you first create a group to use this command." << std::endl;
455 if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
458 else if (g_Steps == 10)
460 std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;