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