Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / examples / ContainerSampleClient.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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
22 #include <string>
23 #include <map>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include <mutex>
27 #include <condition_variable>
28 #include "OCPlatform.h"
29 #include "OCApi.h"
30
31 using namespace OC;
32
33 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
34
35 DiscoveredResourceMap discoveredResources;
36 std::shared_ptr<OCResource> curResource;
37 std::shared_ptr<OCResource> DISensorResource;
38 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
39 std::mutex curResourceLock;
40
41 class Light
42 {
43     public:
44
45         bool m_on_off;
46         int m_color;
47         int m_dim;
48         std::string m_name;
49
50
51         Light() : m_on_off(false), m_color(0), m_dim(0), m_name("")
52         {
53         }
54 };
55
56 class LightSensor
57 {
58 public:
59         int m_intensity;
60
61         std::string m_name;
62
63         LightSensor() : m_intensity(0), m_name("")
64         {
65         }
66 };
67
68 Light mylight;
69
70 int observe_count()
71 {
72     static int oc = 0;
73     return ++oc;
74 }
75
76 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
77                const int &eCode, const int &sequenceNumber)
78 {
79     (void)headerOptions;
80     try
81     {
82         if (eCode == OC_STACK_OK)
83         {
84             std::cout << "OBSERVE RESULT:" << std::endl;
85             std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
86             rep.getValue("on-off", mylight.m_on_off);
87             rep.getValue("color", mylight.m_color);
88             rep.getValue("dim", mylight.m_dim);
89             rep.getValue("name", mylight.m_name);
90
91             std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
92             std::cout << "\tcolor: " << mylight.m_color << std::endl;
93             std::cout << "\tdim: " << mylight.m_dim << std::endl;
94
95             if (observe_count() > 10)
96             {
97                 std::cout << "Cancelling Observe..." << std::endl;
98                 OCStackResult result = curResource->cancelObserve();
99
100                 std::cout << "Cancel result: " << result << std::endl;
101                 sleep(10);
102                 std::cout << "DONE" << std::endl;
103                 std::exit(0);
104             }
105         }
106         else
107         {
108             std::cout << "onObserve Response error: " << eCode << std::endl;
109         }
110     }
111     catch (std::exception &e)
112     {
113         std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
114     }
115
116 }
117
118
119 void onLightIntensityObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
120                const int &eCode, const int &sequenceNumber)
121 {
122     (void)headerOptions;
123     try
124     {
125         if (eCode == OC_STACK_OK)
126         {
127             std::cout << "OBSERVE RESULT:" << std::endl;
128             std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
129
130
131             std::cout << "\tintensity: " << rep.getValue<int>("intensity") << std::endl;
132
133
134             if (observe_count() > 10)
135             {
136                 std::cout << "Cancelling Observe..." << std::endl;
137                 OCStackResult result = curResource->cancelObserve();
138
139                 std::cout << "Cancel result: " << result << std::endl;
140                 sleep(10);
141                 std::cout << "DONE" << std::endl;
142                 std::exit(0);
143             }
144         }
145         else
146         {
147             std::cout << "onObserve Response error: " << eCode << std::endl;
148         }
149     }
150     catch (std::exception &e)
151     {
152         std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
153     }
154
155 }
156
157 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
158 {
159     (void)headerOptions;
160     try
161     {
162         if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
163         {
164             std::cout << "POST request was successful" << std::endl;
165
166             if (rep.hasAttribute("createduri"))
167             {
168                 std::cout << "\tUri of the created resource: "
169                           << rep.getValue<std::string>("createduri") << std::endl;
170             }
171             else
172             {
173                 rep.getValue("on-off", mylight.m_on_off);
174                 rep.getValue("color", mylight.m_color);
175                 rep.getValue("dim", mylight.m_dim);
176
177                 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
178                 std::cout << "\tcolor: " << mylight.m_color << std::endl;
179                 std::cout << "\tdim: " << mylight.m_dim << std::endl;
180             }
181
182             if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
183                 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
184             else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
185                 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
186
187             curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
188
189         }
190         else
191         {
192             std::cout << "onPost2 Response error: " << eCode << std::endl;
193         }
194     }
195     catch (std::exception &e)
196     {
197         std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
198     }
199
200 }
201
202 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
203 {
204     (void)headerOptions;
205     try
206     {
207         if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
208         {
209             std::cout << "POST request was successful" << std::endl;
210
211             if (rep.hasAttribute("createduri"))
212             {
213                 std::cout << "\tUri of the created resource: "
214                           << rep.getValue<std::string>("createduri") << std::endl;
215             }
216             else
217             {
218                 rep.getValue("on-off", mylight.m_on_off);
219                 rep.getValue("color", mylight.m_color);
220                 rep.getValue("dim", mylight.m_dim);
221
222                 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
223                 std::cout << "\tcolor: " << mylight.m_color << std::endl;
224                 std::cout << "\tdim: " << mylight.m_dim << std::endl;
225             }
226
227             OCRepresentation rep2;
228
229             std::cout << "Posting light representation..." << std::endl;
230
231             mylight.m_on_off = true;
232
233             rep2.setValue("on-off", mylight.m_on_off);
234
235             curResource->post(rep2, QueryParamsMap(), &onPost2);
236         }
237         else
238         {
239             std::cout << "onPost Response error: " << eCode << std::endl;
240         }
241     }
242     catch (std::exception &e)
243     {
244         std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
245     }
246 }
247
248 // Local function to put a different state for this re<< std::endlsource
249 void postLightRepresentation(std::shared_ptr<OCResource> resource)
250 {
251     if (resource)
252     {
253         OCRepresentation rep;
254
255         std::cout << "Posting light representation..." << std::endl;
256
257         mylight.m_on_off = false;
258
259         rep.setValue("on-off", mylight.m_on_off);
260
261         // Invoke resource's post API with rep, query map and the callback parameter
262         resource->post(rep, QueryParamsMap(), &onPost);
263     }
264 }
265
266 // callback handler on PUT request
267 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
268 {
269     (void)headerOptions;
270     (void)rep;
271     try
272     {
273         if (eCode == OC_STACK_OK)
274         {
275             std::cout << "PUT request was successful" << std::endl;
276
277             /*rep.getValue("on-off", mylight.m_on_off);
278             rep.getValue("dim", mylight.m_dim);
279             rep.getValue("color", mylight.m_color);
280
281             std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
282             std::cout << "\tcolor: " << mylight.m_color << std::endl;
283             std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
284
285             //postLightRepresentation(curResource);
286         }
287         else
288         {
289             std::cout << "onPut Response error: " << eCode << std::endl;
290         }
291     }
292     catch (std::exception &e)
293     {
294         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
295     }
296 }
297
298 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
299                       const int eCode)
300 {
301     void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
302                           const int eCode);
303
304     (void)headerOptions;
305     (void)rep;
306     try
307     {
308         if (eCode == OC_STACK_OK)
309         {
310             std::cout << "PUT request was successful" << std::endl;
311
312             QueryParamsMap test;
313             std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
314             DISensorResource->get(test, &onGetForDISensor);
315         }
316         else
317         {
318             std::cout << "onPut Response error: " << eCode << std::endl;
319         }
320     }
321     catch (std::exception &e)
322     {
323         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
324     }
325 }
326
327 // Local function to put a different state for this resource
328 void putLightRepresentation(std::shared_ptr<OCResource> resource)
329 {
330     if (resource)
331     {
332         OCRepresentation rep;
333
334         std::cout << "Putting light representation..." << std::endl;
335
336         mylight.m_on_off = true;
337
338         std::cout << "Sending request to: " << resource->uri() << std::endl;
339         rep.setValue("on-off", mylight.m_on_off);
340
341         // Invoke resource's put API with rep, query map and the callback parameter
342
343         resource->post(rep, QueryParamsMap(), &onPut);
344     }
345 }
346
347 // Callback handler on GET request
348 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
349 {
350     (void)headerOptions;
351     try
352     {
353         if (eCode == OC_STACK_OK)
354         {
355             std::cout << "GET request was successful" << std::endl;
356             std::cout << "Resource URI: " << rep.getUri() << std::endl;
357
358             std::cout << "Payload: " << rep.getPayload() << std::endl;
359             std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
360
361             rep.getValue("on-off", mylight.m_on_off);
362
363             std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
364
365             postLightRepresentation(curResource);
366         }
367         else
368         {
369             std::cout << "onGET Response error: " << eCode << std::endl;
370         }
371     }
372     catch (std::exception &e)
373     {
374         std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
375     }
376 }
377
378 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
379                       const int eCode)
380 {
381     (void)headerOptions;
382     try
383     {
384         if (eCode == OC_STACK_OK)
385         {
386             std::cout << "GET request was successful" << std::endl;
387             std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
388
389             std::cout << "Payload: " << rep.getPayload() << std::endl;
390
391             std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
392         }
393         else
394         {
395             std::cout << "onGET Response error: " << eCode << std::endl;
396         }
397     }
398     catch (std::exception &e)
399     {
400         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
401     }
402 }
403
404
405 void onGetForLightIntensitySensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
406                       const int eCode)
407 {
408     (void)headerOptions;
409     try
410     {
411         if (eCode == OC_STACK_OK)
412         {
413             std::cout << "GET request was successful" << std::endl;
414             //std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
415
416             std::cout << "Payload: " << rep.getPayload() << std::endl;
417
418             std::cout << "\tlightIntensity: " << rep.getValue<int>("intensity") << std::endl;
419
420             // iterating over all elements
421             OCRepresentation::const_iterator itr = rep.begin();
422             OCRepresentation::const_iterator endItr = rep.end();
423
424             for(;itr!=endItr;++itr)
425             {
426                 std::cout << itr->attrname() << " ";
427                 std::cout << itr->getValue<int>() << std::endl;
428             }
429
430             curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onLightIntensityObserve);
431         }
432         else
433         {
434             std::cout << "onGET Response error: " << eCode << std::endl;
435         }
436     }
437     catch (std::exception &e)
438     {
439         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
440     }
441 }
442
443 // Local function to get representation of light resource
444 void getLightRepresentation(std::shared_ptr<OCResource> resource)
445 {
446     if (resource)
447     {
448         std::cout << "Getting Light Representation..." << std::endl;
449         // Invoke resource's get API with the callback parameter
450
451         QueryParamsMap test;
452         std::cout << "Sending request to: " << resource->uri() << std::endl;
453         resource->get(test, &onGet);
454     }
455 }
456
457 void onGetDiscomfortIndex(const HeaderOptions &headerOptions, const OCRepresentation &rep,
458                       const int eCode)
459 {
460     (void)headerOptions;
461     std::cout << "onGetDiscomfortIndex" << std::endl;
462     try
463     {
464         if (eCode == OC_STACK_OK)
465         {
466             std::cout << "GET request was successful" << std::endl;
467
468             std::cout << "Payload: " << rep.getPayload() << std::endl;
469
470             std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
471         }
472         else
473         {
474             std::cout << "onGET Response error: " << eCode << std::endl;
475         }
476     }
477     catch (std::exception &e)
478     {
479         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
480     }
481 }
482
483 void onObserveDiscomfort(const HeaderOptions headerOptions, const OCRepresentation &rep,
484                const int &eCode, const int &sequenceNumber)
485 {
486     (void)headerOptions;
487     try
488     {
489         if (eCode == OC_STACK_OK)
490         {
491             std::cout << "OBSERVE RESULT:" << std::endl;
492             std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
493             std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
494
495             if (observe_count() > 10)
496             {
497                 std::cout << "Cancelling Observe..." << std::endl;
498                 OCStackResult result = curResource->cancelObserve();
499
500                 std::cout << "Cancel result: " << result << std::endl;
501                 sleep(10);
502                 std::cout << "DONE" << std::endl;
503                 std::exit(0);
504             }
505         }
506         else
507         {
508             std::cout << "onObserve Response error: " << eCode << std::endl;
509         }
510     }
511     catch (std::exception &e)
512     {
513         std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
514     }
515
516 }
517
518 // Local function to get representation of light resource
519 void getDiscomfortRepresentation(std::shared_ptr<OCResource> resource)
520 {
521     if (resource)
522     {
523         std::cout << "Getting Discomfort Representation..." << std::endl;
524         // Invoke resource's get API with the callback parameter
525
526         QueryParamsMap test;
527         std::cout << "Sending request to: " << resource->uri() << std::endl;
528         resource->get(test, &onGetDiscomfortIndex);
529         //resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
530     }
531 }
532
533
534
535
536 // Local function to get representation of light resource
537 void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
538 {
539     if (resource)
540     {
541         std::cout << "Getting Light Representation..." << std::endl;
542         // Invoke resource's get API with the callback parameter
543
544         QueryParamsMap test;
545         std::cout << "Sending request to: " << resource->uri() << std::endl;
546         resource->get(test, &onGetForLightIntensitySensor);
547     }
548 }
549
550 // Callback to found resources
551 void foundResource(std::shared_ptr<OCResource> resource)
552 {
553     std::cout << "In foundResource\n";
554     std::string resourceURI = resource->uri();
555     std::string hostAddress;
556     try
557     {
558
559
560         // Do some operations with resource object.
561         if (resource)
562         {
563             std::cout << "DISCOVERED Resource:" << std::endl;
564             // Get the resource URI
565             resourceURI = resource->uri();
566             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
567
568             // Get the resource host address
569             hostAddress = resource->host();
570             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
571
572             // Get the resource types
573             std::cout << "\tList of resource types: " << std::endl;
574             for (auto &resourceTypes : resource->getResourceTypes())
575             {
576                 std::cout << "\t\t" << resourceTypes << std::endl;
577                 /*if (resourceTypes == "oic.r.light")
578                 {
579                     curResource = resource;
580                     // Call a local function which will internally invoke get API on the resource pointer
581                     getLightRepresentation(resource);
582                 }*/
583                 if (resourceTypes == "oic.r.discomfortindex")
584                 {
585                     curResource = resource;
586                     std::cout << "\t\tGet discomfort representation " << std::endl;
587                     // Call a local function which will internally invoke get API on the resource pointer
588                     getDiscomfortRepresentation(resource);
589                 }
590             }
591
592             // Get the resource interfaces
593             std::cout << "\tList of resource interfaces: " << std::endl;
594             for (auto &resourceInterfaces : resource->getResourceInterfaces())
595             {
596                 std::cout << "\t\t" << resourceInterfaces << std::endl;
597             }
598         }
599         else
600         {
601             // Resource is invalid
602             std::cout << "Resource is invalid" << std::endl;
603         }
604
605     }
606     catch (std::exception &e)
607     {
608         std::cerr << "Exception in foundResource: " << e.what() << std::endl;
609     }
610 }
611
612 void printUsage()
613 {
614     std::cout << std::endl;
615     std::cout << "---------------------------------------------------------------------\n";
616     std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
617     std::cout << "   ObserveType : 1 - Observe" << std::endl;
618     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
619     std::cout << "---------------------------------------------------------------------\n\n";
620 }
621
622 void checkObserverValue(int value)
623 {
624     if (value == 1)
625     {
626         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
627         std::cout << "<===Setting ObserveType to Observe===>\n\n";
628     }
629     else if (value == 2)
630     {
631         OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
632         std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
633     }
634     else
635     {
636         std::cout << "<===Invalid ObserveType selected."
637                   << " Setting ObserveType to Observe===>\n\n";
638     }
639 }
640
641 static FILE *client_open(const char *path, const char *mode)
642 {
643     (void)path;
644
645     return fopen("./oic_svr_db_client.json", mode);
646 }
647
648 int main(int argc, char *argv[])
649 {
650
651     std::ostringstream requestURI;
652     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
653     try
654     {
655         printUsage();
656         if (argc == 1)
657         {
658             std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
659         }
660         else if (argc == 2)
661         {
662             checkObserverValue(std::stoi(argv[1]));
663         }
664         else
665         {
666             std::cout << "<===Invalid number of command line arguments===>\n\n";
667             return -1;
668         }
669     }
670     catch (std::exception &)
671     {
672         std::cout << "<===Invalid input arguments===>\n\n";
673         return -1;
674     }
675
676     // Create PlatformConfig object
677     PlatformConfig cfg
678     {
679         OC::ServiceType::InProc,
680         OC::ModeType::Both,
681         "0.0.0.0",
682         0,
683         OC::QualityOfService::LowQos,
684         &ps
685     };
686
687     OCPlatform::Configure(cfg);
688     try
689     {
690         // makes it so that all boolean values are printed as 'true/false' in this stream
691         std::cout.setf(std::ios::boolalpha);
692         // Find all resources
693         requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
694
695         OCPlatform::findResource("", requestURI.str(),
696                                  CT_DEFAULT, &foundResource);
697         std::cout << "Finding Resource... " << std::endl;
698
699         // Find resource is done twice so that we discover the original resources a second time.
700         // These resources will have the same uniqueidentifier (yet be different objects), so that
701         // we can verify/show the duplicate-checking code in foundResource(above);
702         OCPlatform::findResource("", requestURI.str(),
703                                  CT_DEFAULT, &foundResource);
704         std::cout << "Finding Resource for second time..." << std::endl;
705
706         // A condition variable will free the mutex it is given, then do a non-
707         // intensive block until 'notify' is called on it.  In this case, since we
708         // don't ever call cv.notify, this should be a non-processor intensive version
709         // of while(true);
710         std::mutex blocker;
711         std::condition_variable cv;
712         std::unique_lock<std::mutex> lock(blocker);
713         cv.wait(lock);
714
715     }
716     catch (OCException &e)
717     {
718         oclog() << "Exception in main: " << e.what();
719     }
720
721     return 0;
722 }
723
724