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