Correct some defects detected by Static Code Analysis Tool in things manager
[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 "ThingsConfiguration.h"
31 #include "ThingsMaintenance.h"
32 #include "GroupManager.h"
33
34 using namespace OC;
35 using namespace OIC;
36
37 int g_Steps = 0;
38 int isWaiting = 0; //0: none to wait, 1: wait for the response of "getConfigurationValue"
39 pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
40
41 const int SUCCESS_RESPONSE = 0;
42
43 static GroupManager* g_groupmanager;
44 static ThingsConfiguration* g_thingsConf;
45 static ThingsMaintenance* g_thingsMnt;
46
47 OCResourceHandle configurationCollectionHandle;
48 std::shared_ptr< OCResource > g_configurationCollection; // for a group of multiple resources
49 std::shared_ptr< OCResource > g_configurationResource; // For a single resource
50
51 OCResourceHandle maintenanceCollectionHandle;
52 std::shared_ptr< OCResource > g_maintenanceCollection; // for a group of multiple resources
53 std::shared_ptr< OCResource > g_maintenanceResource; // For a single resource
54
55 OCResourceHandle setCollectionHandle;
56 std::shared_ptr< OCResource > g_setCollection; // for a group of multiple resources
57 std::shared_ptr< OCResource > g_setResource; // For a single resource
58
59 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
60 std::vector< OCResourceHandle > resourceHandleVector;
61
62 typedef std::string ConfigurationName;
63 typedef std::string ConfigurationValue;
64
65 void timeCheck(int timeSec)
66 {
67     sleep(timeSec);
68     pthread_mutex_lock(&mutex_lock);
69     isWaiting = 0;
70     pthread_mutex_unlock(&mutex_lock);
71 }
72
73 void onReboot(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
74 {
75     pthread_mutex_lock(&mutex_lock);
76     isWaiting = 0;
77     pthread_mutex_unlock(&mutex_lock);
78
79     if (eCode != SUCCESS_RESPONSE)
80     {
81         return ;
82     }
83
84     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
85     std::cout << "\t\tReboot:" << rep.getValue< std::string >("value") << std::endl;
86 }
87
88 void onFactoryReset(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
89 {
90     pthread_mutex_lock(&mutex_lock);
91     isWaiting = 0;
92     pthread_mutex_unlock(&mutex_lock);
93
94     if (eCode != SUCCESS_RESPONSE)
95     {
96        return ;
97     }
98
99     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
100     std::cout << "\t\tFactoryReset:" << rep.getValue< std::string >("value") << std::endl;
101 }
102
103 void onUpdate(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
104 {
105     pthread_mutex_lock(&mutex_lock);
106     isWaiting = 0;
107     pthread_mutex_unlock(&mutex_lock);
108
109     if (eCode != SUCCESS_RESPONSE)
110     {
111         return ;
112     }
113
114     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
115
116     if (rep.hasAttribute("n"))
117         std::cout << "\t\tDeviceName:" << rep.getValue< std::string >("n") << std::endl;
118     if (rep.hasAttribute("loc"))
119         std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
120     if (rep.hasAttribute("locn"))
121         std::cout << "\t\tLocationName:" << rep.getValue< std::string >("locn") << std::endl;
122     if (rep.hasAttribute("c"))
123         std::cout << "\t\tCurrency:" << rep.getValue< std::string >("c") << std::endl;
124     if (rep.hasAttribute("r"))
125         std::cout << "\t\tRegion:" << rep.getValue< std::string >("r") << std::endl;
126 }
127
128 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
129 {
130     pthread_mutex_lock(&mutex_lock);
131     isWaiting = 0;
132     pthread_mutex_unlock(&mutex_lock);
133
134     if (eCode != SUCCESS_RESPONSE)
135     {
136         return ;
137     }
138
139     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
140
141     if (rep.hasAttribute("n"))
142         std::cout << "\t\tDeviceName:" << rep.getValue< std::string >("n") << std::endl;
143     if (rep.hasAttribute("loc"))
144         std::cout << "\t\tLocation:" << rep.getValue< std::string >("loc") << std::endl;
145     if (rep.hasAttribute("locn"))
146         std::cout << "\t\tLocationName:" << rep.getValue< std::string >("locn") << 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;
151 }
152
153 // Callback to found collection resource
154 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
155 {
156     std::string resourceURI;
157     std::string hostAddress;
158     try
159     {
160         // Do some operations with resource object.
161         for (unsigned int i = 0; i < resources.size(); ++i)
162         {
163             std::shared_ptr< OCResource > resource = resources.at(i);
164
165             if (resource)
166             {
167                 if (resource->uri() == "/core/a/configuration/resourceset")
168                     g_configurationCollection = resource;
169                 else if (resource->uri() == "/core/a/maintenance/resourceset")
170                     g_maintenanceCollection = resource;
171                 else if (resource->uri() == "/core/a/factoryset/resourceset")
172                     g_setCollection = resource;
173                 else
174                 {
175                     pthread_mutex_lock(&mutex_lock);
176                     isWaiting = 0;
177                     pthread_mutex_unlock(&mutex_lock);
178                     return;
179                 }
180             }
181             else
182             {
183                 // Resource is invalid
184                 std::cout << "Resource is invalid" << std::endl;
185             }
186         }
187
188     }
189     catch (std::exception& e)
190     {
191         std::cout << "Exception: " << e.what() << std::endl;
192     }
193
194     pthread_mutex_lock(&mutex_lock);
195     isWaiting = 0;
196     pthread_mutex_unlock(&mutex_lock);
197 }
198
199 // Callback to found resources
200 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
201 {
202     std::string resourceURI;
203     std::string hostAddress;
204
205     try
206     {
207         // Do some operations with resource object.
208         for (unsigned int i = 0; i < resources.size(); ++i)
209         {
210             std::shared_ptr< OCResource > resource = resources.at(i);
211
212             if (resource)
213             {
214                 // Check if the resource is new one. If so, store it.
215
216                 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
217                         resourceTable.find(resource->host() + resource->uri());
218
219                 if (iter == resourceTable.end()) // new one
220                 {
221                     resourceTable[resource->host() + resource->uri()] = resource;
222
223                     OCResourceHandle foundResourceHandle = NULL;
224                     OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
225                             resource);
226                     std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
227                             << std::endl;
228                     if (result == OC_STACK_OK)
229                     {
230                         if (resource->uri() == "/oic/con")
231                         {
232                             OCPlatform::bindResource(configurationCollectionHandle,
233                                     foundResourceHandle);
234                             if (g_configurationResource == NULL)
235                                 g_configurationResource = resource;
236                         }
237                         else if (resource->uri() == "/oic/mnt")
238                         {
239                             OCPlatform::bindResource(maintenanceCollectionHandle,
240                                     foundResourceHandle);
241                             if (g_maintenanceResource == NULL)
242                                 g_maintenanceResource = resource;
243                         }
244                         else if (resource->uri() == "/factoryset")
245                         {
246                             OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
247                             if (g_setResource == NULL)
248                                 g_setResource = resource;
249                         }
250
251                         resourceHandleVector.push_back(foundResourceHandle);
252                     }
253                     else
254                     {
255                         cout << "\tresource Error!" << endl;
256                     }
257
258                 }
259
260             }
261             else
262             {
263                 // Resource is invalid
264                 std::cout << "Resource is invalid" << std::endl;
265             }
266         }
267
268     }
269     catch (std::exception& e)
270     {
271         std::cout << "Exception: " << e.what() << std::endl;
272     }
273
274     pthread_mutex_lock(&mutex_lock);
275     isWaiting = 0;
276     pthread_mutex_unlock(&mutex_lock);
277 }
278
279 int main()
280 {
281     std::string str_steps;
282
283     try
284     {
285         //**************************************************************
286         // STEP 0
287         PlatformConfig cfg
288         { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
289
290         OCPlatform::Configure(cfg);
291         //g_thingsmanager = new ThingsManager();
292         g_thingsConf = new ThingsConfiguration();
293         g_thingsMnt = new ThingsMaintenance();
294         g_groupmanager = new GroupManager();
295
296         //**************************************************************
297
298         while (true)
299         {
300             pthread_mutex_lock(&mutex_lock);
301             if (isWaiting > 0)
302             {
303                 pthread_mutex_unlock(&mutex_lock);
304                 continue;
305             }
306
307             isWaiting = 0;
308             pthread_mutex_unlock(&mutex_lock);
309
310             cout << endl << endl << "(0) Quit" << std::endl;
311             cout << "(1) Find all resources(URI: /oic/con, /oic/mnt, /factoryset)" << std::endl;
312             cout << "(2) Find all groups" << std::endl;
313             cout << "(3) Get a Configuration resource" << std::endl;
314             cout << "(4) Update a device name attribute value" << std::endl;
315             cout << "(5) FactoryReset (for the group)" << std::endl;
316             cout << "(6) Reboot (for the group)" << std::endl;
317             cout << "(10) Show Configuration Units" << std::endl;
318
319             try
320             {
321                 std::getline (std::cin, str_steps);
322
323                 if(str_steps == "")
324                 {
325                     continue;
326                 }
327                 else
328                 {
329                     g_Steps = std::stoi(str_steps);
330                 }
331             }
332             catch(std::invalid_argument&)
333             {
334                 std::cout << "Please put a digit, not string" << std::endl;
335                 continue;
336             }
337             catch(std::out_of_range&)
338             {
339                 std::cout << "Please put a number within the supported range" << std::endl;
340                 continue;
341             }
342
343             if (g_Steps == 0)
344             {
345                 break;
346             }
347             else if (g_Steps == 1)
348             {
349                 std::vector< std::string > types;
350
351                 // For Registering a collection resource for configuration resources
352                 if (configurationCollectionHandle == NULL)
353                 {
354                     string resourceURI = "/core/a/configuration/resourceset";
355                     string resourceTypeName = "core.configuration.resourceset";
356                     string resourceInterface = BATCH_INTERFACE;
357
358                     OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
359                         resourceTypeName, resourceInterface, NULL,
360                         //&entityHandler, // entityHandler
361                         OC_DISCOVERABLE);
362
363                     OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
364                     OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
365                         DEFAULT_INTERFACE);
366                 }
367
368                 // For Registering a collection resource for maintenance resources
369                 if (maintenanceCollectionHandle == NULL)
370                 {
371                     string resourceURI = "/core/a/maintenance/resourceset";
372                     string resourceTypeName = "core.maintenance.resourceset";
373                     string resourceInterface = BATCH_INTERFACE;
374
375                     OCPlatform::registerResource(maintenanceCollectionHandle, resourceURI,
376                         resourceTypeName, resourceInterface, NULL,
377                         //&entityHandler, // entityHandler
378                         OC_DISCOVERABLE);
379
380                     OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, GROUP_INTERFACE);
381                     OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, DEFAULT_INTERFACE);
382                 }
383
384                 // For Registering a collection resource for set resources
385                 if (setCollectionHandle == NULL)
386                 {
387                     string resourceURI = "/core/a/factoryset/resourceset";
388                     string resourceTypeName = "core.factoryset.resourceset";
389                     string resourceInterface = BATCH_INTERFACE;
390
391                     OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
392                         resourceInterface, NULL,
393                         //&entityHandler, // entityHandler
394                         OC_DISCOVERABLE);
395
396                     OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
397                     OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
398                 }
399
400                 types.push_back("oic.wk.con");
401                 types.push_back("oic.wk.mnt");
402                 types.push_back("factoryset");
403
404                 std::cout << "Finding Configuration Resource... " << std::endl;
405                 std::cout << "Finding Maintenance Resource... " << std::endl;
406                 std::cout << "Finding Set Resource... " << std::endl;
407
408                 g_groupmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
409
410                 pthread_mutex_lock(&mutex_lock);
411                 isWaiting = 1;
412                 pthread_mutex_unlock(&mutex_lock);
413
414                 thread t(&timeCheck, 5);
415                 t.join();       // After 5 seconds, isWaiting value will be 0.
416             }
417             else if (g_Steps == 2) // make a group with found things
418             {
419                 std::vector< std::string > types;
420                 types.push_back("core.configuration.resourceset");
421                 types.push_back("core.maintenance.resourceset");
422                 types.push_back("core.factoryset.resourceset");
423
424                 g_groupmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
425
426                 std::cout << "Finding Collection resource... " << std::endl;
427
428                 pthread_mutex_lock(&mutex_lock);
429                 isWaiting = 1;
430                 pthread_mutex_unlock(&mutex_lock);
431
432                 thread t(&timeCheck, 5);
433                 t.join();       // After 5 seconds, isWaiting value will be 0.
434             }
435             else if (g_Steps == 3)
436             {
437                 // get a value
438
439                 ConfigurationName name = "all";
440
441                 std::cout << "For example, get configuration resources's value" << std::endl;
442
443                 std::vector< ConfigurationName > configurations;
444
445                 configurations.push_back(name);
446
447                 if (g_thingsConf->getConfigurations(g_configurationCollection, configurations, &onGet)
448                         != OC_STACK_ERROR)
449                 {
450                     pthread_mutex_lock(&mutex_lock);
451                     isWaiting = 0;
452                     pthread_mutex_unlock(&mutex_lock);
453                 }
454             }
455             else if (g_Steps == 4)
456             {
457                 ConfigurationName name = "n";
458                 ConfigurationValue value = "OIC Device";
459
460                 if(g_configurationCollection == NULL)
461                 {
462                     std::cout<<"Note that you first create a group to use this command." << std::endl;
463                     continue;
464                 }
465
466                 std::cout << "For example, change a device name" << std::endl;
467                 std::cout << g_configurationCollection->uri() << std::endl;
468
469                 std::map< ConfigurationName, ConfigurationValue > configurations;
470
471                 configurations.insert(std::make_pair(name, value));
472
473                 if (g_thingsConf->updateConfigurations(g_configurationCollection, configurations,
474                         &onUpdate) != OC_STACK_ERROR)
475                 {
476                     pthread_mutex_lock(&mutex_lock);
477                     isWaiting = 0;
478                     pthread_mutex_unlock(&mutex_lock);
479                 }
480             }
481             else if (g_Steps == 5)
482             {
483                 // factory reset
484                 if(g_maintenanceCollection == NULL)
485                 {
486                     std::cout<<"Note that you first create a group to use this command." << std::endl;
487                     continue;
488                 }
489
490                 if (g_thingsMnt->factoryReset(g_maintenanceCollection, &onFactoryReset)
491                         != OC_STACK_ERROR)
492                 {
493                     pthread_mutex_lock(&mutex_lock);
494                     isWaiting = 0;
495                     pthread_mutex_unlock(&mutex_lock);
496                 }
497             }
498             else if (g_Steps == 6)
499             {
500                 // reboot
501                 if(g_maintenanceCollection == NULL)
502                 {
503                     std::cout<<"Note that you first create a group to use this command." << std::endl;
504                     continue;
505                 }
506
507                 if (g_thingsMnt->reboot(g_maintenanceCollection, &onReboot) != OC_STACK_ERROR)
508                 {
509                     pthread_mutex_lock(&mutex_lock);
510                     isWaiting = 0;
511                     pthread_mutex_unlock(&mutex_lock);
512                 }
513             }
514             else if (g_Steps == 10)
515             {
516                 std::cout << g_thingsConf->getListOfSupportedConfigurationUnits() << std::endl;
517
518             }
519         }
520     }catch (OCException e)
521     {
522         std::cout << "Exception in main: " << e.what();
523     }
524
525     return 0;
526 }
527