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 static ThingsManager* g_thingsmanager;
40 OCResourceHandle configurationCollectionHandle;
41 std::shared_ptr< OCResource > g_configurationCollection; // for a group of multiple resources
42 std::shared_ptr< OCResource > g_configurationResource; // For a single resource
44 OCResourceHandle diagnosticsCollectionHandle;
45 std::shared_ptr< OCResource > g_diagnosticsCollection; // for a group of multiple resources
46 std::shared_ptr< OCResource > g_diagnosticsResource; // For a single resource
48 OCResourceHandle setCollectionHandle;
49 std::shared_ptr< OCResource > g_setCollection; // for a group of multiple resources
50 std::shared_ptr< OCResource > g_setResource; // For a single resource
52 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
53 std::vector< OCResourceHandle > resourceHandleVector;
55 typedef std::function<
56 void(const HeaderOptions& headerOptions,
57 const OCRepresentation& rep, const int eCode) > ConfigurationCallback;
58 typedef std::function<
59 void(const HeaderOptions& headerOptions,
60 const OCRepresentation& rep, const int eCode) > DiagnosticsCallback;
62 typedef std::string ConfigurationName;
63 typedef std::string ConfigurationValue;
65 void onReboot(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
67 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
69 std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
74 void onFactoryReset(const HeaderOptions& headerOptions, const OCRepresentation& rep,
77 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
79 std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
83 void onUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
85 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
87 std::cout << "\t\tvalue:" << rep.getValue< std::string >("value") << std::endl;
92 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
94 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
96 if (rep.hasAttribute("value"))
97 std::cout << "\t\tvalue:" << rep.getValue< std::string >("value") << std::endl;
98 else if (rep.hasAttribute("link"))
99 std::cout << "\t\tlink:" << rep.getValue< std::string >("link") << std::endl;
101 std::vector< OCRepresentation > children = rep.getChildren();
103 for (auto oit = children.begin(); oit != children.end(); ++oit)
105 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
107 if (oit->hasAttribute("value"))
108 std::cout << "\t\tvalue:" << oit->getValue< std::string >("value") << std::endl;
109 else if (oit->hasAttribute("link"))
110 std::cout << "\t\tlink:" << oit->getValue< std::string >("link") << std::endl;
116 // Callback to found collection resource
117 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
120 std::string resourceURI;
121 std::string hostAddress;
124 // Do some operations with resource object.
125 for (unsigned int i = 0; i < resources.size(); ++i)
127 std::shared_ptr< OCResource > resource = resources.at(i);
131 if (resource->uri() == "/core/a/configuration/resourceset")
132 g_configurationCollection = resource;
133 else if (resource->uri() == "/core/a/diagnostics/resourceset")
134 g_diagnosticsCollection = resource;
135 else if (resource->uri() == "/core/a/factoryset/resourceset")
136 g_setCollection = resource;
145 // Resource is invalid
146 std::cout << "Resource is invalid" << std::endl;
151 catch (std::exception& e)
156 if (g_configurationCollection != NULL && g_diagnosticsCollection != NULL
157 && g_setCollection != NULL)
161 // Callback to found resources
162 void onFoundCandidateCollection(std::vector< std::shared_ptr< OCResource > > resources)
165 std::string resourceURI;
166 std::string hostAddress;
168 static bool flagForCon = false, flagForDiag = false, flagForSet = false;
172 // Do some operations with resource object.
173 for (unsigned int i = 0; i < resources.size(); ++i)
175 std::shared_ptr< OCResource > resource = resources.at(i);
179 // Check if the resource is new one. If so, store it.
181 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
182 resourceTable.find(resource->host() + resource->uri());
184 if (iter == resourceTable.end()) // new one
186 resourceTable[resource->host() + resource->uri()] = resource;
188 OCResourceHandle foundResourceHandle;
189 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
191 std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
193 if (result == OC_STACK_OK)
195 if (resource->uri() == "/oic/con")
198 OCPlatform::bindResource(configurationCollectionHandle,
199 foundResourceHandle);
200 if (g_configurationResource == NULL)
201 g_configurationResource = resource;
203 else if (resource->uri() == "/oic/diag")
206 OCPlatform::bindResource(diagnosticsCollectionHandle,
207 foundResourceHandle);
208 if (g_diagnosticsResource == NULL)
209 g_diagnosticsResource = resource;
211 else if (resource->uri() == "/factorySet")
214 OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
215 if (g_setResource == NULL)
216 g_setResource = resource;
219 resourceHandleVector.push_back(foundResourceHandle);
223 cout << "\tresource Error!" << endl;
231 // Resource is invalid
232 std::cout << "Resource is invalid" << std::endl;
237 catch (std::exception& e)
242 if (flagForCon && flagForDiag && flagForSet)
246 int main(int argc, char* argv[])
249 //**************************************************************
252 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
254 OCPlatform::Configure(cfg);
255 g_thingsmanager = new ThingsManager();
257 //**************************************************************
267 cout << endl << endl << "(0) Quit" << std::endl;
268 cout << "(1) Find all resources(URI: /oic/con, /oic/diag, /factoryset)" << std::endl;
269 cout << "(2) Find all groups" << std::endl;
270 cout << "(3) Get a new value (of \"Configuration\" Collection)" << std::endl;
271 cout << "(4) Update a value (of \"Region\" Resource)" << std::endl;
272 cout << "(5) Get a value (for \"Region\" Resource)" << std::endl;
273 cout << "(6) FactoryReset (for the group)" << std::endl;
274 cout << "(7) Reboot (for the group)" << std::endl;
275 cout << "(10) Show Configuration Units" << std::endl;
281 else if (g_Steps == 1)
283 std::vector< std::string > types;
284 { // For Registering a collection resource for configuration resources
286 string resourceURI = "/core/a/configuration/resourceset";
287 string resourceTypeName = "core.configuration.resourceset";
288 string resourceInterface = BATCH_INTERFACE;
290 if (configurationCollectionHandle != NULL)
292 std::cout << "already exists" << std::endl;
296 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
297 resourceTypeName, resourceInterface, NULL,
298 //&entityHandler, // entityHandler
301 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
302 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
305 // instead of registration
306 types.push_back("oic.con");
307 std::cout << "Finding Configuration Resource... " << std::endl;
310 { // For Registering a collection resource for diagnostics resources
312 string resourceURI = "/core/a/diagnostics/resourceset";
313 string resourceTypeName = "core.diagnostics.resourceset";
314 string resourceInterface = BATCH_INTERFACE;
316 if (diagnosticsCollectionHandle != NULL)
318 std::cout << "already exists" << std::endl;
322 OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
323 resourceTypeName, resourceInterface, NULL,
324 //&entityHandler, // entityHandler
327 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
328 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
330 // instead of registration
331 types.push_back("oic.diag");
332 std::cout << "Finding Diagnostics Resource... " << std::endl;
336 { // For Registering a collection resource for set resources
338 string resourceURI = "/core/a/factoryset/resourceset";
339 string resourceTypeName = "core.factoryset.resourceset";
340 string resourceInterface = BATCH_INTERFACE;
342 if (setCollectionHandle != NULL)
344 std::cout << "already exists" << std::endl;
348 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
349 resourceInterface, NULL,
350 //&entityHandler, // entityHandler
353 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
354 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
356 // instead of registration
357 types.push_back("factorySet");
358 std::cout << "Finding Set Resource... " << std::endl;
361 g_thingsmanager->findCandidateResources(types, &onFoundCandidateCollection, 5);
365 else if (g_Steps == 2) // make a group with found things
367 std::vector< std::string > types;
368 types.push_back("core.configuration.resourceset");
369 types.push_back("core.diagnostics.resourceset");
370 types.push_back("core.factoryset.resourceset");
372 g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
374 std::cout << "Finding Collection resource... " << std::endl;
378 else if (g_Steps == 3)
382 ConfigurationName name = "configuration";
384 std::cout << "For example, get configuration collection's value" << std::endl;
386 std::vector< ConfigurationName > configurations;
388 configurations.push_back(name);
390 if (g_thingsmanager->getConfigurations(g_configurationResource, configurations, &onGet)
394 else if (g_Steps == 4)
396 ConfigurationName name = "region";
397 ConfigurationValue value = "U.S.A (new region)";
399 std::cout << "For example, change region resource's value" << std::endl;
400 std::cout << g_configurationCollection->uri() << std::endl;
402 std::map< ConfigurationName, ConfigurationValue > configurations;
404 configurations.insert(std::make_pair(name, value));
406 if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
407 &onUpdate) != OC_STACK_ERROR)
410 else if (g_Steps == 5)
414 ConfigurationName name = "region";
416 std::cout << "For example, get region resource's value" << std::endl;
418 std::vector< ConfigurationName > configurations;
420 configurations.push_back(name);
422 if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations,
423 &onGet) != OC_STACK_ERROR)
426 else if (g_Steps == 6)
429 if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
433 else if (g_Steps == 7)
436 if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
439 else if (g_Steps == 10)
441 std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;