Merge Things Manager codes CA and Master branch.
[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 const int SUCCESS_RESPONSE = 0;
39
40 static ThingsManager* g_thingsmanager;
41
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
45
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
49
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
53
54 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
55 std::vector< OCResourceHandle > resourceHandleVector;
56
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;
63
64 typedef std::string ConfigurationName;
65 typedef std::string ConfigurationValue;
66
67 void timeCheck(int timeSec)
68 {
69     sleep(timeSec);
70     isWaiting = 0;
71 }
72
73 void onReboot(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
74 {
75     isWaiting = 0;
76
77     if (eCode != SUCCESS_RESPONSE)
78     {
79         return ;
80     }
81
82     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
83     std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
84 }
85
86 void onFactoryReset(const HeaderOptions& headerOptions, const OCRepresentation& rep,
87         const int eCode)
88 {
89     isWaiting = 0;
90
91     if (eCode != SUCCESS_RESPONSE)
92     {
93        return ;
94     }
95
96     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
97     std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
98 }
99
100 void onUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
101 {
102     isWaiting = 0;
103
104     if (eCode != SUCCESS_RESPONSE)
105     {
106         return ;
107     }
108
109     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
110
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;
119 }
120
121 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
122 {
123     isWaiting = 0;
124
125     if (eCode != SUCCESS_RESPONSE)
126     {
127         return ;
128     }
129
130     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
131
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;
140 }
141
142 // Callback to found collection resource
143 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
144 {
145     std::string resourceURI;
146     std::string hostAddress;
147     try
148     {
149         // Do some operations with resource object.
150         for (unsigned int i = 0; i < resources.size(); ++i)
151         {
152             std::shared_ptr< OCResource > resource = resources.at(i);
153
154             if (resource)
155             {
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;
162                 else
163                 {
164                     isWaiting = 0;
165                     return;
166                 }
167             }
168             else
169             {
170                 // Resource is invalid
171                 std::cout << "Resource is invalid" << std::endl;
172             }
173         }
174
175     }
176     catch (std::exception& e)
177     {
178         //log(e.what());
179     }
180
181     isWaiting = 0;
182 }
183
184 // Callback to found resources
185 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
186 {
187
188     std::string resourceURI;
189     std::string hostAddress;
190
191     try
192     {
193         // Do some operations with resource object.
194         for (unsigned int i = 0; i < resources.size(); ++i)
195         {
196             std::shared_ptr< OCResource > resource = resources.at(i);
197
198             if (resource)
199             {
200                 // Check if the resource is new one. If so, store it.
201
202                 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
203                         resourceTable.find(resource->host() + resource->uri());
204
205                 if (iter == resourceTable.end()) // new one
206                 {
207                     resourceTable[resource->host() + resource->uri()] = resource;
208
209                     OCResourceHandle foundResourceHandle;
210                     OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
211                             resource);
212                     std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
213                             << std::endl;
214                     if (result == OC_STACK_OK)
215                     {
216                         if (resource->uri() == "/oic/con")
217                         {
218                             OCPlatform::bindResource(configurationCollectionHandle,
219                                     foundResourceHandle);
220                             if (g_configurationResource == NULL)
221                                 g_configurationResource = resource;
222                         }
223                         else if (resource->uri() == "/oic/diag")
224                         {
225                             OCPlatform::bindResource(diagnosticsCollectionHandle,
226                                     foundResourceHandle);
227                             if (g_diagnosticsResource == NULL)
228                                 g_diagnosticsResource = resource;
229                         }
230                         else if (resource->uri() == "/factorySet")
231                         {
232                             OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
233                             if (g_setResource == NULL)
234                                 g_setResource = resource;
235                         }
236
237                         resourceHandleVector.push_back(foundResourceHandle);
238                     }
239                     else
240                     {
241                         cout << "\tresource Error!" << endl;
242                     }
243
244                 }
245
246             }
247             else
248             {
249                 // Resource is invalid
250                 std::cout << "Resource is invalid" << std::endl;
251             }
252         }
253
254     }
255     catch (std::exception& e)
256     {
257         //log(e.what());
258     }
259
260     isWaiting = 0;
261 }
262
263 int main(int argc, char* argv[])
264 {
265     std::string str_steps;
266
267     //**************************************************************
268     // STEP 0
269     PlatformConfig cfg
270     { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
271
272     OCPlatform::Configure(cfg);
273     g_thingsmanager = new ThingsManager();
274
275     //**************************************************************
276
277     while (true)
278     {
279
280         if (isWaiting > 0)
281             continue;
282
283         isWaiting = 0;
284
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;
293
294         try
295         {
296             std::getline (std::cin, str_steps);
297
298             if(str_steps == "")
299                 continue;
300             else
301                 g_Steps = std::stoi(str_steps);
302         }
303         catch(std::invalid_argument&)
304         {
305             std::cout << "Please put a digit, not string" << std::endl;
306             continue;
307         }
308
309         if (g_Steps == 0)
310             break;
311         else if (g_Steps == 1)
312         {
313             std::vector< std::string > types;
314
315             // For Registering a collection resource for configuration resources
316             if (configurationCollectionHandle == NULL)
317             {
318                 string resourceURI = "/core/a/configuration/resourceset";
319                 string resourceTypeName = "core.configuration.resourceset";
320                 string resourceInterface = BATCH_INTERFACE;
321
322                 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
323                     resourceTypeName, resourceInterface, NULL,
324                     //&entityHandler, // entityHandler
325                     OC_DISCOVERABLE);
326
327                 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
328                 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
329                     DEFAULT_INTERFACE);
330             }
331
332             // For Registering a collection resource for diagnostics resources
333             if (diagnosticsCollectionHandle == NULL)
334             {
335                 string resourceURI = "/core/a/diagnostics/resourceset";
336                 string resourceTypeName = "core.diagnostics.resourceset";
337                 string resourceInterface = BATCH_INTERFACE;
338
339                 OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
340                     resourceTypeName, resourceInterface, NULL,
341                     //&entityHandler, // entityHandler
342                     OC_DISCOVERABLE);
343
344                 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
345                 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
346             }
347
348             // For Registering a collection resource for set resources
349             if (setCollectionHandle == NULL)
350             {
351                 string resourceURI = "/core/a/factoryset/resourceset";
352                 string resourceTypeName = "core.factoryset.resourceset";
353                 string resourceInterface = BATCH_INTERFACE;
354
355                 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
356                     resourceInterface, NULL,
357                     //&entityHandler, // entityHandler
358                     OC_DISCOVERABLE);
359
360                 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
361                 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
362             }
363
364             types.push_back("oic.con");
365             types.push_back("oic.diag");
366             types.push_back("factorySet");
367
368             std::cout << "Finding Configuration Resource... " << std::endl;
369             std::cout << "Finding Diagnostics Resource... " << std::endl;
370             std::cout << "Finding Set Resource... " << std::endl;
371
372             g_thingsmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
373
374             isWaiting = 1;
375
376             thread t(&timeCheck, 5);
377             t.join();       // After 5 seconds, isWaiting value will be 0.
378         }
379         else if (g_Steps == 2) // make a group with found things
380         {
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");
385
386             g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
387
388             std::cout << "Finding Collection resource... " << std::endl;
389
390             isWaiting = 1;
391
392             thread t(&timeCheck, 5);
393             t.join();       // After 5 seconds, isWaiting value will be 0.
394         }
395         else if (g_Steps == 3)
396         {
397             // get a value
398
399             ConfigurationName name = "all";
400
401             std::cout << "For example, get configuration resources's value" << std::endl;
402
403             std::vector< ConfigurationName > configurations;
404
405             configurations.push_back(name);
406
407             if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations, &onGet)
408                     != OC_STACK_ERROR)
409                 isWaiting = 1;
410         }
411         else if (g_Steps == 4)
412         {
413             ConfigurationName name = "r";
414             ConfigurationValue value = "U.S.A (new region)";
415
416             if(g_configurationCollection == NULL)
417             {
418                 std::cout<<"Note that you first create a group to use this command." << std::endl;
419                 continue;
420             }
421
422             std::cout << "For example, change region resource's value" << std::endl;
423             std::cout << g_configurationCollection->uri() << std::endl;
424
425             std::map< ConfigurationName, ConfigurationValue > configurations;
426
427             configurations.insert(std::make_pair(name, value));
428
429             if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
430                     &onUpdate) != OC_STACK_ERROR)
431                 isWaiting = 1;
432         }
433         else if (g_Steps == 5)
434         {
435             // factory reset
436             if(g_diagnosticsCollection == NULL)
437             {
438                 std::cout<<"Note that you first create a group to use this command." << std::endl;
439                 continue;
440             }
441
442             if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
443                     != OC_STACK_ERROR)
444                 isWaiting = 1;
445         }
446         else if (g_Steps == 6)
447         {
448             // reboot
449             if(g_diagnosticsCollection == NULL)
450             {
451                 std::cout<<"Note that you first create a group to use this command." << std::endl;
452                 continue;
453             }
454
455             if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
456                 isWaiting = 1;
457         }
458         else if (g_Steps == 10)
459         {
460             std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;
461
462         }
463
464     }
465
466     return 0;
467 }
468