278c3e268405e95fa372c40c5249dd1a295200a6
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / con-client.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 // OCClient.cpp : Defines the entry point for the console application.
22 //
23 #include <string>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include <map>
27 #include <vector>
28 #include "OCPlatform.h"
29 #include "OCApi.h"
30 #include "ThingsManager.h"
31
32 using namespace OC;
33 using namespace OIC;
34
35 int g_Steps = 0;
36 int isWaiting = 0; //0: none to wait, 1: wait for the response of "getConfigurationValue"
37
38 static ThingsManager* g_thingsmanager;
39
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
43
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
47
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
51
52 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
53 std::vector< OCResourceHandle > resourceHandleVector;
54
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;
61
62 typedef std::string ConfigurationName;
63 typedef std::string ConfigurationValue;
64
65 void onReboot(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
66 {
67     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
68
69     std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
70
71     isWaiting = 0;
72 }
73
74 void onFactoryReset(const HeaderOptions& headerOptions, const OCRepresentation& rep,
75         const int eCode)
76 {
77     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
78
79     std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
80     isWaiting = 0;
81 }
82
83 void onUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
84 {
85     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
86
87     std::cout << "\t\tvalue:" << rep.getValue< std::string >("value") << std::endl;
88
89     isWaiting = 0;
90 }
91
92 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
93 {
94     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
95
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;
100
101     std::vector< OCRepresentation > children = rep.getChildren();
102
103     for (auto oit = children.begin(); oit != children.end(); ++oit)
104     {
105         std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
106
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;
111     }
112
113     isWaiting = 0;
114 }
115
116 // Callback to found collection resource
117 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
118 {
119
120     std::string resourceURI;
121     std::string hostAddress;
122     try
123     {
124         // Do some operations with resource object.
125         for (unsigned int i = 0; i < resources.size(); ++i)
126         {
127             std::shared_ptr< OCResource > resource = resources.at(i);
128
129             if (resource)
130             {
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;
137                 else
138                 {
139                     isWaiting = 0;
140                     return;
141                 }
142             }
143             else
144             {
145                 // Resource is invalid
146                 std::cout << "Resource is invalid" << std::endl;
147             }
148         }
149
150     }
151     catch (std::exception& e)
152     {
153         //log(e.what());
154     }
155
156     if (g_configurationCollection != NULL && g_diagnosticsCollection != NULL
157             && g_setCollection != NULL)
158         isWaiting = 0;
159 }
160
161 // Callback to found resources
162 void onFoundCandidateCollection(std::vector< std::shared_ptr< OCResource > > resources)
163 {
164
165     std::string resourceURI;
166     std::string hostAddress;
167
168     static bool flagForCon = false, flagForDiag = false, flagForSet = false;
169
170     try
171     {
172         // Do some operations with resource object.
173         for (unsigned int i = 0; i < resources.size(); ++i)
174         {
175             std::shared_ptr< OCResource > resource = resources.at(i);
176
177             if (resource)
178             {
179                 // Check if the resource is new one. If so, store it.
180
181                 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
182                         resourceTable.find(resource->host() + resource->uri());
183
184                 if (iter == resourceTable.end()) // new one
185                 {
186                     resourceTable[resource->host() + resource->uri()] = resource;
187
188                     OCResourceHandle foundResourceHandle;
189                     OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
190                             resource);
191                     std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
192                             << std::endl;
193                     if (result == OC_STACK_OK)
194                     {
195                         if (resource->uri() == "/oic/con")
196                         {
197                             flagForCon = true;
198                             OCPlatform::bindResource(configurationCollectionHandle,
199                                     foundResourceHandle);
200                             if (g_configurationResource == NULL)
201                                 g_configurationResource = resource;
202                         }
203                         else if (resource->uri() == "/oic/diag")
204                         {
205                             flagForDiag = true;
206                             OCPlatform::bindResource(diagnosticsCollectionHandle,
207                                     foundResourceHandle);
208                             if (g_diagnosticsResource == NULL)
209                                 g_diagnosticsResource = resource;
210                         }
211                         else if (resource->uri() == "/factorySet")
212                         {
213                             flagForSet = true;
214                             OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
215                             if (g_setResource == NULL)
216                                 g_setResource = resource;
217                         }
218
219                         resourceHandleVector.push_back(foundResourceHandle);
220                     }
221                     else
222                     {
223                         cout << "\tresource Error!" << endl;
224                     }
225
226                 }
227
228             }
229             else
230             {
231                 // Resource is invalid
232                 std::cout << "Resource is invalid" << std::endl;
233             }
234         }
235
236     }
237     catch (std::exception& e)
238     {
239         //log(e.what());
240     }
241
242     if (flagForCon && flagForDiag && flagForSet)
243         isWaiting = 0;
244 }
245
246 int main(int argc, char* argv[])
247 {
248
249     //**************************************************************
250     // STEP 0
251     PlatformConfig cfg
252     { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
253
254     OCPlatform::Configure(cfg);
255     g_thingsmanager = new ThingsManager();
256
257     //**************************************************************
258
259     while (true)
260     {
261
262         if (isWaiting > 0)
263             continue;
264
265         isWaiting = 0;
266
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;
276
277         cin >> g_Steps;
278         //
279         if (g_Steps == 0)
280             break;
281         else if (g_Steps == 1)
282         {
283             std::vector< std::string > types;
284             { // For Registering a collection resource for configuration resources
285
286                 string resourceURI = "/core/a/configuration/resourceset";
287                 string resourceTypeName = "core.configuration.resourceset";
288                 string resourceInterface = BATCH_INTERFACE;
289
290                 if (configurationCollectionHandle != NULL)
291                 {
292                     std::cout << "already exists" << std::endl;
293                     continue;
294                 }
295
296                 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
297                         resourceTypeName, resourceInterface, NULL,
298                         //&entityHandler, // entityHandler
299                         OC_DISCOVERABLE);
300
301                 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
302                 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
303                         DEFAULT_INTERFACE);
304
305                 // instead of registration
306                 types.push_back("oic.con");
307                 std::cout << "Finding Configuration Resource... " << std::endl;
308             }
309
310             { // For Registering a collection resource for diagnostics resources
311
312                 string resourceURI = "/core/a/diagnostics/resourceset";
313                 string resourceTypeName = "core.diagnostics.resourceset";
314                 string resourceInterface = BATCH_INTERFACE;
315
316                 if (diagnosticsCollectionHandle != NULL)
317                 {
318                     std::cout << "already exists" << std::endl;
319                     continue;
320                 }
321
322                 OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
323                         resourceTypeName, resourceInterface, NULL,
324                         //&entityHandler, // entityHandler
325                         OC_DISCOVERABLE);
326
327                 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
328                 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
329
330                 // instead of registration
331                 types.push_back("oic.diag");
332                 std::cout << "Finding Diagnostics Resource... " << std::endl;
333
334             }
335
336             { // For Registering a collection resource for set resources
337
338                 string resourceURI = "/core/a/factoryset/resourceset";
339                 string resourceTypeName = "core.factoryset.resourceset";
340                 string resourceInterface = BATCH_INTERFACE;
341
342                 if (setCollectionHandle != NULL)
343                 {
344                     std::cout << "already exists" << std::endl;
345                     continue;
346                 }
347
348                 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
349                         resourceInterface, NULL,
350                         //&entityHandler, // entityHandler
351                         OC_DISCOVERABLE);
352
353                 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
354                 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
355
356                 // instead of registration
357                 types.push_back("factorySet");
358                 std::cout << "Finding Set Resource... " << std::endl;
359             }
360
361             g_thingsmanager->findCandidateResources(types, &onFoundCandidateCollection, 5);
362
363             isWaiting = 1;
364         }
365         else if (g_Steps == 2) // make a group with found things
366         {
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");
371
372             g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
373
374             std::cout << "Finding Collection resource... " << std::endl;
375             isWaiting = 1;
376
377         }
378         else if (g_Steps == 3)
379         {
380             // get a value
381
382             ConfigurationName name = "configuration";
383
384             std::cout << "For example, get configuration collection's value" << std::endl;
385
386             std::vector< ConfigurationName > configurations;
387
388             configurations.push_back(name);
389
390             if (g_thingsmanager->getConfigurations(g_configurationResource, configurations, &onGet)
391                     != OC_STACK_ERROR)
392                 isWaiting = 1;
393         }
394         else if (g_Steps == 4)
395         {
396             ConfigurationName name = "region";
397             ConfigurationValue value = "U.S.A (new region)";
398
399             std::cout << "For example, change region resource's value" << std::endl;
400             std::cout << g_configurationCollection->uri() << std::endl;
401
402             std::map< ConfigurationName, ConfigurationValue > configurations;
403
404             configurations.insert(std::make_pair(name, value));
405
406             if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
407                     &onUpdate) != OC_STACK_ERROR)
408                 isWaiting = 1;
409         }
410         else if (g_Steps == 5)
411         {
412             // get a value
413
414             ConfigurationName name = "region";
415
416             std::cout << "For example, get region resource's value" << std::endl;
417
418             std::vector< ConfigurationName > configurations;
419
420             configurations.push_back(name);
421
422             if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations,
423                     &onGet) != OC_STACK_ERROR)
424                 isWaiting = 1;
425         }
426         else if (g_Steps == 6)
427         {
428             // factory reset
429             if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
430                     != OC_STACK_ERROR)
431                 isWaiting = 1;
432         }
433         else if (g_Steps == 7)
434         {
435             // reboot
436             if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
437                 isWaiting = 1;
438         }
439         else if (g_Steps == 10)
440         {
441             std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;
442
443         }
444
445     }
446
447     return 0;
448 }
449