Merge branch 'master' into resource-container
[platform/upstream/iotivity.git] / service / resource-container / examples / ContainerSampleClient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 <map>
25 #include <cstdlib>
26 #include <pthread.h>
27 #include <mutex>
28 #include <condition_variable>
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33
34 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
35
36 DiscoveredResourceMap discoveredResources;
37 std::shared_ptr<OCResource> curResource;
38 std::shared_ptr<OCResource> DISensorResource;
39 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
40 std::mutex curResourceLock;
41
42 class Light
43 {
44     public:
45
46         bool m_on_off;
47         int m_color;
48         int m_dim;
49         std::string m_name;
50
51
52         Light() : m_on_off(false), m_color(0), m_dim(0), m_name("")
53         {
54         }
55 };
56
57 Light mylight;
58
59 int observe_count()
60 {
61     static int oc = 0;
62     return ++oc;
63 }
64
65 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
66                const int &eCode, const int &sequenceNumber)
67 {
68     (void)headerOptions;
69     try
70     {
71         if (eCode == OC_STACK_OK)
72         {
73             std::cout << "OBSERVE RESULT:" << std::endl;
74             std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
75             rep.getValue("on-off", mylight.m_on_off);
76             rep.getValue("color", mylight.m_color);
77             rep.getValue("dim", mylight.m_dim);
78             rep.getValue("name", mylight.m_name);
79
80             std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
81             std::cout << "\tcolor: " << mylight.m_color << std::endl;
82             std::cout << "\tdim: " << mylight.m_dim << std::endl;
83
84             if (observe_count() > 10)
85             {
86                 std::cout << "Cancelling Observe..." << std::endl;
87                 OCStackResult result = curResource->cancelObserve();
88
89                 std::cout << "Cancel result: " << result << std::endl;
90                 sleep(10);
91                 std::cout << "DONE" << std::endl;
92                 std::exit(0);
93             }
94         }
95         else
96         {
97             std::cout << "onObserve Response error: " << eCode << std::endl;
98         }
99     }
100     catch (std::exception &e)
101     {
102         std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
103     }
104
105 }
106
107 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
108 {
109     (void)headerOptions;
110     try
111     {
112         if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
113         {
114             std::cout << "POST request was successful" << std::endl;
115
116             if (rep.hasAttribute("createduri"))
117             {
118                 std::cout << "\tUri of the created resource: "
119                           << rep.getValue<std::string>("createduri") << std::endl;
120             }
121             else
122             {
123                 rep.getValue("on-off", mylight.m_on_off);
124                 rep.getValue("color", mylight.m_color);
125                 rep.getValue("dim", mylight.m_dim);
126
127                 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
128                 std::cout << "\tcolor: " << mylight.m_color << std::endl;
129                 std::cout << "\tdim: " << mylight.m_dim << std::endl;
130             }
131
132             if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
133                 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
134             else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
135                 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
136
137             curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
138
139         }
140         else
141         {
142             std::cout << "onPost2 Response error: " << eCode << std::endl;
143         }
144     }
145     catch (std::exception &e)
146     {
147         std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
148     }
149
150 }
151
152 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
153 {
154     (void)headerOptions;
155     try
156     {
157         if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
158         {
159             std::cout << "POST request was successful" << std::endl;
160
161             if (rep.hasAttribute("createduri"))
162             {
163                 std::cout << "\tUri of the created resource: "
164                           << rep.getValue<std::string>("createduri") << std::endl;
165             }
166             else
167             {
168                 rep.getValue("on-off", mylight.m_on_off);
169                 rep.getValue("color", mylight.m_color);
170                 rep.getValue("dim", mylight.m_dim);
171
172                 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
173                 std::cout << "\tcolor: " << mylight.m_color << std::endl;
174                 std::cout << "\tdim: " << mylight.m_dim << std::endl;
175             }
176
177             OCRepresentation rep2;
178
179             std::cout << "Posting light representation..." << std::endl;
180
181             mylight.m_on_off = true;
182
183             rep2.setValue("on-off", mylight.m_on_off);
184
185             curResource->post(rep2, QueryParamsMap(), &onPost2);
186         }
187         else
188         {
189             std::cout << "onPost Response error: " << eCode << std::endl;
190         }
191     }
192     catch (std::exception &e)
193     {
194         std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
195     }
196 }
197
198 // Local function to put a different state for this re<< std::endlsource
199 void postLightRepresentation(std::shared_ptr<OCResource> resource)
200 {
201     if (resource)
202     {
203         OCRepresentation rep;
204
205         std::cout << "Posting light representation..." << std::endl;
206
207         mylight.m_on_off = "false";
208
209         rep.setValue("on-off", mylight.m_on_off);
210
211         // Invoke resource's post API with rep, query map and the callback parameter
212         resource->post(rep, QueryParamsMap(), &onPost);
213     }
214 }
215
216 // callback handler on PUT request
217 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
218 {
219     (void)headerOptions;
220     (void)rep;
221     try
222     {
223         if (eCode == OC_STACK_OK)
224         {
225             std::cout << "PUT request was successful" << std::endl;
226
227             /*rep.getValue("on-off", mylight.m_on_off);
228             rep.getValue("dim", mylight.m_dim);
229             rep.getValue("color", mylight.m_color);
230
231             std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
232             std::cout << "\tcolor: " << mylight.m_color << std::endl;
233             std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
234
235             //postLightRepresentation(curResource);
236         }
237         else
238         {
239             std::cout << "onPut Response error: " << eCode << std::endl;
240         }
241     }
242     catch (std::exception &e)
243     {
244         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
245     }
246 }
247
248 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
249                       const int eCode)
250 {
251     void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
252                           const int eCode);
253
254     (void)headerOptions;
255     (void)rep;
256     try
257     {
258         if (eCode == OC_STACK_OK)
259         {
260             std::cout << "PUT request was successful" << std::endl;
261
262             QueryParamsMap test;
263             std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
264             DISensorResource->get(test, &onGetForDISensor);
265         }
266         else
267         {
268             std::cout << "onPut Response error: " << eCode << std::endl;
269         }
270     }
271     catch (std::exception &e)
272     {
273         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
274     }
275 }
276
277 // Local function to put a different state for this resource
278 void putLightRepresentation(std::shared_ptr<OCResource> resource)
279 {
280     if (resource)
281     {
282         OCRepresentation rep;
283
284         std::cout << "Putting light representation..." << std::endl;
285
286         mylight.m_on_off = true;
287
288         std::cout << "Sending request to: " << resource->uri() << std::endl;
289         rep.setValue("on-off", mylight.m_on_off);
290
291         // Invoke resource's put API with rep, query map and the callback parameter
292
293         resource->put(rep, QueryParamsMap(), &onPut);
294     }
295 }
296
297 // Callback handler on GET request
298 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
299 {
300     (void)headerOptions;
301     try
302     {
303         if (eCode == OC_STACK_OK)
304         {
305             std::cout << "GET request was successful" << std::endl;
306             std::cout << "Resource URI: " << rep.getUri() << std::endl;
307
308             std::cout << "Payload: " << rep.getPayload() << std::endl;
309             std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
310
311             rep.getValue("on-off", mylight.m_on_off);
312
313             std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
314
315             putLightRepresentation(curResource);
316         }
317         else
318         {
319             std::cout << "onGET Response error: " << eCode << std::endl;
320         }
321     }
322     catch (std::exception &e)
323     {
324         std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
325     }
326 }
327
328 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
329                       const int eCode)
330 {
331     (void)headerOptions;
332     try
333     {
334         if (eCode == OC_STACK_OK)
335         {
336             std::cout << "GET request was successful" << std::endl;
337             std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
338
339             std::cout << "Payload: " << rep.getPayload() << std::endl;
340
341             std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
342         }
343         else
344         {
345             std::cout << "onGET Response error: " << eCode << std::endl;
346         }
347     }
348     catch (std::exception &e)
349     {
350         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
351     }
352 }
353
354 // Local function to get representation of light resource
355 void getLightRepresentation(std::shared_ptr<OCResource> resource)
356 {
357     if (resource)
358     {
359         std::cout << "Getting Light Representation..." << std::endl;
360         // Invoke resource's get API with the callback parameter
361
362         QueryParamsMap test;
363         std::cout << "Sending request to: " << resource->uri() << std::endl;
364         resource->get(test, &onGet);
365     }
366 }
367
368 // Callback to found resources
369 void foundResource(std::shared_ptr<OCResource> resource)
370 {
371     std::cout << "In foundResource\n";
372     std::string resourceURI = resource->uri();
373     std::string hostAddress;
374     try
375     {
376
377
378         // Do some operations with resource object.
379         if (resource)
380         {
381             std::cout << "DISCOVERED Resource:" << std::endl;
382             // Get the resource URI
383             resourceURI = resource->uri();
384             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
385
386             // Get the resource host address
387             hostAddress = resource->host();
388             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
389
390             // Get the resource types
391             std::cout << "\tList of resource types: " << std::endl;
392             for (auto &resourceTypes : resource->getResourceTypes())
393             {
394                 std::cout << "\t\t" << resourceTypes << std::endl;
395                 if (resourceTypes == "oic.r.light")
396                 {
397                     curResource = resource;
398                     // Call a local function which will internally invoke get API on the resource pointer
399                     getLightRepresentation(resource);
400                 }
401             }
402
403             // Get the resource interfaces
404             std::cout << "\tList of resource interfaces: " << std::endl;
405             for (auto &resourceInterfaces : resource->getResourceInterfaces())
406             {
407                 std::cout << "\t\t" << resourceInterfaces << std::endl;
408             }
409         }
410         else
411         {
412             // Resource is invalid
413             std::cout << "Resource is invalid" << std::endl;
414         }
415
416     }
417     catch (std::exception &e)
418     {
419         std::cerr << "Exception in foundResource: " << e.what() << std::endl;
420     }
421 }
422
423 void printUsage()
424 {
425     std::cout << std::endl;
426     std::cout << "---------------------------------------------------------------------\n";
427     std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
428     std::cout << "   ObserveType : 1 - Observe" << std::endl;
429     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
430     std::cout << "---------------------------------------------------------------------\n\n";
431 }
432
433 void checkObserverValue(int value)
434 {
435     if (value == 1)
436     {
437         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
438         std::cout << "<===Setting ObserveType to Observe===>\n\n";
439     }
440     else if (value == 2)
441     {
442         OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
443         std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
444     }
445     else
446     {
447         std::cout << "<===Invalid ObserveType selected."
448                   << " Setting ObserveType to Observe===>\n\n";
449     }
450 }
451
452 static FILE *client_open(const char *path, const char *mode)
453 {
454     (void)path;
455
456     return fopen("./oic_svr_db_client.json", mode);
457 }
458
459 int main(int argc, char *argv[])
460 {
461
462     std::ostringstream requestURI;
463     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
464     try
465     {
466         printUsage();
467         if (argc == 1)
468         {
469             std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
470         }
471         else if (argc == 2)
472         {
473             checkObserverValue(std::stoi(argv[1]));
474         }
475         else
476         {
477             std::cout << "<===Invalid number of command line arguments===>\n\n";
478             return -1;
479         }
480     }
481     catch (std::exception &)
482     {
483         std::cout << "<===Invalid input arguments===>\n\n";
484         return -1;
485     }
486
487     // Create PlatformConfig object
488     PlatformConfig cfg
489     {
490         OC::ServiceType::InProc,
491         OC::ModeType::Both,
492         "0.0.0.0",
493         0,
494         OC::QualityOfService::LowQos,
495         &ps
496     };
497
498     OCPlatform::Configure(cfg);
499     try
500     {
501         // makes it so that all boolean values are printed as 'true/false' in this stream
502         std::cout.setf(std::ios::boolalpha);
503         // Find all resources
504         requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
505
506         OCPlatform::findResource("", requestURI.str(),
507                                  CT_DEFAULT, &foundResource);
508         std::cout << "Finding Resource... " << std::endl;
509
510         // Find resource is done twice so that we discover the original resources a second time.
511         // These resources will have the same uniqueidentifier (yet be different objects), so that
512         // we can verify/show the duplicate-checking code in foundResource(above);
513         OCPlatform::findResource("", requestURI.str(),
514                                  CT_DEFAULT, &foundResource);
515         std::cout << "Finding Resource for second time..." << std::endl;
516
517         // A condition variable will free the mutex it is given, then do a non-
518         // intensive block until 'notify' is called on it.  In this case, since we
519         // don't ever call cv.notify, this should be a non-processor intensive version
520         // of while(true);
521         std::mutex blocker;
522         std::condition_variable cv;
523         std::unique_lock<std::mutex> lock(blocker);
524         cv.wait(lock);
525
526     }
527     catch (OCException &e)
528     {
529         oclog() << "Exception in main: " << e.what();
530     }
531
532     return 0;
533 }
534
535