Merging primitive services(TM, SSM and NM) from android-api branch.
[contrib/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;
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                     isWaiting = 0;
176                     return;
177                 }
178             }
179             else
180             {
181                 // Resource is invalid
182                 std::cout << "Resource is invalid" << std::endl;
183             }
184         }
185
186     }
187     catch (std::exception& e)
188     {
189         std::cout << "Exception: " << e.what() << std::endl;
190     }
191
192     isWaiting = 0;
193 }
194
195 // Callback to found resources
196 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
197 {
198     std::string resourceURI;
199     std::string hostAddress;
200
201     try
202     {
203         // Do some operations with resource object.
204         for (unsigned int i = 0; i < resources.size(); ++i)
205         {
206             std::shared_ptr< OCResource > resource = resources.at(i);
207
208             if (resource)
209             {
210                 // Check if the resource is new one. If so, store it.
211
212                 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
213                         resourceTable.find(resource->host() + resource->uri());
214
215                 if (iter == resourceTable.end()) // new one
216                 {
217                     resourceTable[resource->host() + resource->uri()] = resource;
218
219                     OCResourceHandle foundResourceHandle;
220                     OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
221                             resource);
222                     std::cout << "\tResource ( " << resource->host() << " ) is registed!\t"
223                             << std::endl;
224                     if (result == OC_STACK_OK)
225                     {
226                         if (resource->uri() == "/oic/con")
227                         {
228                             OCPlatform::bindResource(configurationCollectionHandle,
229                                     foundResourceHandle);
230                             if (g_configurationResource == NULL)
231                                 g_configurationResource = resource;
232                         }
233                         else if (resource->uri() == "/oic/diag")
234                         {
235                             OCPlatform::bindResource(diagnosticsCollectionHandle,
236                                     foundResourceHandle);
237                             if (g_diagnosticsResource == NULL)
238                                 g_diagnosticsResource = resource;
239                         }
240                         else if (resource->uri() == "/factorySet")
241                         {
242                             OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
243                             if (g_setResource == NULL)
244                                 g_setResource = resource;
245                         }
246
247                         resourceHandleVector.push_back(foundResourceHandle);
248                     }
249                     else
250                     {
251                         cout << "\tresource Error!" << endl;
252                     }
253
254                 }
255
256             }
257             else
258             {
259                 // Resource is invalid
260                 std::cout << "Resource is invalid" << std::endl;
261             }
262         }
263
264     }
265     catch (std::exception& e)
266     {
267         std::cout << "Exception: " << e.what() << std::endl;
268     }
269
270     isWaiting = 0;
271 }
272
273 int main(int argc, char* argv[])
274 {
275     std::string str_steps;
276     pthread_mutex_init(&mutex_lock, NULL);
277
278     //**************************************************************
279     // STEP 0
280     PlatformConfig cfg
281     { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
282
283     OCPlatform::Configure(cfg);
284     g_thingsmanager = new ThingsManager();
285
286     //**************************************************************
287
288     while (true)
289     {
290         pthread_mutex_lock(&mutex_lock);
291         if (isWaiting > 0)
292         {
293             pthread_mutex_unlock(&mutex_lock);
294             continue;
295         }
296
297         isWaiting = 0;
298         pthread_mutex_unlock(&mutex_lock);
299
300         cout << endl << endl << "(0) Quit" << std::endl;
301         cout << "(1) Find all resources(URI: /oic/con, /oic/diag, /factoryset)" << std::endl;
302         cout << "(2) Find all groups" << std::endl;
303         cout << "(3) Get a Configuration resource" << std::endl;
304         cout << "(4) Update a region attribute value" << std::endl;
305         cout << "(5) FactoryReset (for the group)" << std::endl;
306         cout << "(6) Reboot (for the group)" << std::endl;
307         cout << "(10) Show Configuration Units" << std::endl;
308
309         try
310         {
311             std::getline (std::cin, str_steps);
312
313             if(str_steps == "")
314             {
315                 continue;
316             }
317             else
318             {
319                 g_Steps = std::stoi(str_steps);
320             }
321         }
322         catch(std::invalid_argument&)
323         {
324             std::cout << "Please put a digit, not string" << std::endl;
325             continue;
326         }
327
328         if (g_Steps == 0)
329         {
330             break;
331         }
332         else if (g_Steps == 1)
333         {
334             std::vector< std::string > types;
335
336             // For Registering a collection resource for configuration resources
337             if (configurationCollectionHandle == NULL)
338             {
339                 string resourceURI = "/core/a/configuration/resourceset";
340                 string resourceTypeName = "core.configuration.resourceset";
341                 string resourceInterface = BATCH_INTERFACE;
342
343                 OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
344                     resourceTypeName, resourceInterface, NULL,
345                     //&entityHandler, // entityHandler
346                     OC_DISCOVERABLE);
347
348                 OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
349                 OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
350                     DEFAULT_INTERFACE);
351             }
352
353             // For Registering a collection resource for diagnostics resources
354             if (diagnosticsCollectionHandle == NULL)
355             {
356                 string resourceURI = "/core/a/diagnostics/resourceset";
357                 string resourceTypeName = "core.diagnostics.resourceset";
358                 string resourceInterface = BATCH_INTERFACE;
359
360                 OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
361                     resourceTypeName, resourceInterface, NULL,
362                     //&entityHandler, // entityHandler
363                     OC_DISCOVERABLE);
364
365                 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
366                 OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
367             }
368
369             // For Registering a collection resource for set resources
370             if (setCollectionHandle == NULL)
371             {
372                 string resourceURI = "/core/a/factoryset/resourceset";
373                 string resourceTypeName = "core.factoryset.resourceset";
374                 string resourceInterface = BATCH_INTERFACE;
375
376                 OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
377                     resourceInterface, NULL,
378                     //&entityHandler, // entityHandler
379                     OC_DISCOVERABLE);
380
381                 OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
382                 OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
383             }
384
385             types.push_back("oic.con");
386             types.push_back("oic.diag");
387             types.push_back("factorySet");
388
389             std::cout << "Finding Configuration Resource... " << std::endl;
390             std::cout << "Finding Diagnostics Resource... " << std::endl;
391             std::cout << "Finding Set Resource... " << std::endl;
392
393             g_thingsmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
394
395             isWaiting = 1;
396
397             thread t(&timeCheck, 5);
398             t.join();       // After 5 seconds, isWaiting value will be 0.
399         }
400         else if (g_Steps == 2) // make a group with found things
401         {
402             std::vector< std::string > types;
403             types.push_back("core.configuration.resourceset");
404             types.push_back("core.diagnostics.resourceset");
405             types.push_back("core.factoryset.resourceset");
406
407             g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
408
409             std::cout << "Finding Collection resource... " << std::endl;
410
411             isWaiting = 1;
412
413             thread t(&timeCheck, 5);
414             t.join();       // After 5 seconds, isWaiting value will be 0.
415         }
416         else if (g_Steps == 3)
417         {
418             // get a value
419
420             ConfigurationName name = "all";
421
422             std::cout << "For example, get configuration resources's value" << std::endl;
423
424             std::vector< ConfigurationName > configurations;
425
426             configurations.push_back(name);
427
428             if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations, &onGet)
429                     != OC_STACK_ERROR)
430             {
431                 pthread_mutex_lock(&mutex_lock);
432                 isWaiting = 0;
433                 pthread_mutex_unlock(&mutex_lock);
434             }
435         }
436         else if (g_Steps == 4)
437         {
438             ConfigurationName name = "r";
439             ConfigurationValue value = "U.S.A (new region)";
440
441             if(g_configurationCollection == NULL)
442             {
443                 std::cout<<"Note that you first create a group to use this command." << std::endl;
444                 continue;
445             }
446
447             std::cout << "For example, change region resource's value" << std::endl;
448             std::cout << g_configurationCollection->uri() << std::endl;
449
450             std::map< ConfigurationName, ConfigurationValue > configurations;
451
452             configurations.insert(std::make_pair(name, value));
453
454             if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
455                     &onUpdate) != OC_STACK_ERROR)
456             {
457                 pthread_mutex_lock(&mutex_lock);
458                 isWaiting = 0;
459                 pthread_mutex_unlock(&mutex_lock);
460             }
461         }
462         else if (g_Steps == 5)
463         {
464             // factory reset
465             if(g_diagnosticsCollection == NULL)
466             {
467                 std::cout<<"Note that you first create a group to use this command." << std::endl;
468                 continue;
469             }
470
471             if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
472                     != OC_STACK_ERROR)
473             {
474                 pthread_mutex_lock(&mutex_lock);
475                 isWaiting = 0;
476                 pthread_mutex_unlock(&mutex_lock);
477             }
478         }
479         else if (g_Steps == 6)
480         {
481             // reboot
482             if(g_diagnosticsCollection == NULL)
483             {
484                 std::cout<<"Note that you first create a group to use this command." << std::endl;
485                 continue;
486             }
487
488             if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
489             {
490                 pthread_mutex_lock(&mutex_lock);
491                 isWaiting = 0;
492                 pthread_mutex_unlock(&mutex_lock);
493             }
494         }
495         else if (g_Steps == 10)
496         {
497             std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;
498
499         }
500
501     }
502
503     return 0;
504 }
505