Changeset for reviewing RI-CA integration changes.
[platform/upstream/iotivity.git] / resource / examples / simpleclientHQ.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 <set>
24 #include <string>
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 struct dereference_compare
35 {
36     bool operator()(std::shared_ptr<OCResource> lhs, std::shared_ptr<OCResource> rhs )const
37     {
38         return *lhs < *rhs;
39     }
40 };
41 typedef std::set<std::shared_ptr<OCResource>, dereference_compare> DiscoveredResourceSet;
42
43 DiscoveredResourceSet discoveredResources;
44 const int SUCCESS_RESPONSE = 0;
45 std::shared_ptr<OCResource> curResource;
46 std::mutex resourceLock;
47 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
48
49 //TODO-CA: Since CA CON message support is still in progress, this client uses
50 // LowQos messages. This needs to change once Confirmable messages are supported in CA.
51
52 class Light
53 {
54 public:
55
56     bool m_state;
57     int m_power;
58     std::string m_name;
59
60     Light() : m_state(false), m_power(0), m_name("")
61     {
62     }
63 };
64
65 Light mylight;
66
67 int observe_count()
68 {
69     static int oc = 0;
70     return ++oc;
71 }
72
73 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
74                     const int& eCode, const int& sequenceNumber)
75 {
76     if(eCode == SUCCESS_RESPONSE)
77     {
78         std::cout << "OBSERVE RESULT:"<<std::endl;
79         if(sequenceNumber == (int) ObserveAction::ObserveRegister)
80         {
81             std::cout << "\tObserve Registration Confirmed: "<< std::endl;
82         }
83         else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
84         {
85             std::cout << "\tObserve Cancel Confirmed: "<< std::endl;
86             sleep(10);
87             std::cout << "DONE"<<std::endl;
88             std::exit(0);
89         }
90         else
91         {
92             std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
93         }
94
95         rep.getValue("state", mylight.m_state);
96         rep.getValue("power", mylight.m_power);
97         rep.getValue("name", mylight.m_name);
98
99         std::cout << "\tstate: " << mylight.m_state << std::endl;
100         std::cout << "\tpower: " << mylight.m_power << std::endl;
101         std::cout << "\tname: " << mylight.m_name << std::endl;
102
103         if(observe_count() > 30)
104         {
105             std::cout<<"Cancelling Observe..."<<std::endl;
106             OCStackResult result = curResource->cancelObserve(OC::QualityOfService::LowQos);
107
108             std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
109         }
110     }
111     else
112     {
113         std::cout << "onObserve Response error: " << eCode << std::endl;
114         std::exit(-1);
115     }
116 }
117
118 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
119 {
120     if(eCode == SUCCESS_RESPONSE)
121     {
122         std::cout << "POST request was successful" << std::endl;
123
124         if(rep.hasAttribute("createduri"))
125         {
126             std::cout << "\tUri of the created resource: "
127                       << rep.getValue<std::string>("createduri") << std::endl;
128         }
129         else
130         {
131             rep.getValue("state", mylight.m_state);
132             rep.getValue("power", mylight.m_power);
133             rep.getValue("name", mylight.m_name);
134
135             std::cout << "\tstate: " << mylight.m_state << std::endl;
136             std::cout << "\tpower: " << mylight.m_power << std::endl;
137             std::cout << "\tname: " << mylight.m_name << std::endl;
138         }
139
140         if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
141             std::cout << std::endl << "Observe is used." << std::endl << std::endl;
142         else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
143             std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
144         sleep(1);
145         curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
146                 OC::QualityOfService::LowQos);
147
148     }
149     else
150     {
151         std::cout << "onPost2 Response error: " << eCode << std::endl;
152         std::exit(-1);
153     }
154 }
155
156 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
157 {
158     if(eCode == SUCCESS_RESPONSE)
159     {
160         std::cout << "POST request was successful" << std::endl;
161
162         if(rep.hasAttribute("createduri"))
163         {
164             std::cout << "\tUri of the created resource: "
165                       << rep.getValue<std::string>("createduri") << std::endl;
166         }
167         else
168         {
169             rep.getValue("state", mylight.m_state);
170             rep.getValue("power", mylight.m_power);
171             rep.getValue("name", mylight.m_name);
172
173             std::cout << "\tstate: " << mylight.m_state << std::endl;
174             std::cout << "\tpower: " << mylight.m_power << std::endl;
175             std::cout << "\tname: " << mylight.m_name << std::endl;
176         }
177
178         OCRepresentation rep2;
179
180         std::cout << "Posting light representation..."<<std::endl;
181
182         mylight.m_state = true;
183         mylight.m_power = 55;
184
185         rep2.setValue("state", mylight.m_state);
186         rep2.setValue("power", mylight.m_power);
187         sleep(1);
188         curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::LowQos);
189     }
190     else
191     {
192         std::cout << "onPost Response error: " << eCode << std::endl;
193         std::exit(-1);
194     }
195 }
196
197 // Local function to put a different state for this resource
198 void postLightRepresentation(std::shared_ptr<OCResource> resource)
199 {
200     if(resource)
201     {
202         OCRepresentation rep;
203
204         std::cout << "Posting light representation..."<<std::endl;
205
206         mylight.m_state = false;
207         mylight.m_power = 105;
208
209         rep.setValue("state", mylight.m_state);
210         rep.setValue("power", mylight.m_power);
211
212         // Invoke resource's post API with rep, query map and the callback parameter
213         resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
214     }
215 }
216
217 // callback handler on PUT request
218 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
219 {
220     if(eCode == SUCCESS_RESPONSE)
221     {
222         std::cout << "PUT request was successful" << std::endl;
223
224         rep.getValue("state", mylight.m_state);
225         rep.getValue("power", mylight.m_power);
226         rep.getValue("name", mylight.m_name);
227
228         std::cout << "\tstate: " << mylight.m_state << std::endl;
229         std::cout << "\tpower: " << mylight.m_power << std::endl;
230         std::cout << "\tname: " << mylight.m_name << std::endl;
231         sleep(1);
232         postLightRepresentation(curResource);
233     }
234     else
235     {
236         std::cout << "onPut Response error: " << eCode << std::endl;
237         std::exit(-1);
238     }
239 }
240
241 // Local function to put a different state for this resource
242 void putLightRepresentation(std::shared_ptr<OCResource> resource)
243 {
244     if(resource)
245     {
246         OCRepresentation rep;
247
248         std::cout << "Putting light representation..."<<std::endl;
249
250         mylight.m_state = true;
251         mylight.m_power = 15;
252
253         rep.setValue("state", mylight.m_state);
254         rep.setValue("power", mylight.m_power);
255
256         // Invoke resource's put API with rep, query map and the callback parameter
257         resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::LowQos);
258     }
259 }
260
261 // Callback handler on GET request
262 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
263 {
264     if(eCode == SUCCESS_RESPONSE)
265     {
266         std::cout << "GET request was successful" << std::endl;
267         std::cout << "Resource URI: " << rep.getUri() << std::endl;
268
269         rep.getValue("state", mylight.m_state);
270         rep.getValue("power", mylight.m_power);
271         rep.getValue("name", mylight.m_name);
272
273         std::cout << "\tstate: " << mylight.m_state << std::endl;
274         std::cout << "\tpower: " << mylight.m_power << std::endl;
275         std::cout << "\tname: " << mylight.m_name << std::endl;
276         sleep(1);
277         putLightRepresentation(curResource);
278     }
279     else
280     {
281         std::cout << "onGET Response error: " << eCode << std::endl;
282         std::exit(-1);
283     }
284 }
285
286 // Local function to get representation of light resource
287 void getLightRepresentation(std::shared_ptr<OCResource> resource)
288 {
289     if(resource)
290     {
291         std::cout << "Getting Light Representation..."<<std::endl;
292         // Invoke resource's get API with the callback parameter
293
294         QueryParamsMap test;
295         resource->get(test, &onGet,OC::QualityOfService::LowQos);
296     }
297 }
298
299 // Callback to found resources
300 void foundResource(std::shared_ptr<OCResource> resource)
301 {
302     std::string resourceURI;
303     std::string hostAddress;
304     try
305     {
306         // Do some operations with resource object.
307         if(resource)
308         {
309             std::lock_guard<std::mutex> lk(resourceLock);
310
311             if(discoveredResources.find(resource) == discoveredResources.end())
312             {
313                 std::cout << "Found resource " << resource->uniqueIdentifier() <<
314                     " for the first time on server with ID: "<< resource->sid()<<std::endl;
315                 discoveredResources.insert(resource);
316             }
317             else
318             {
319                 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
320             }
321
322             if(curResource)
323             {
324                 std::cout << "Found another resource, ignoring"<<std::endl;
325                 return;
326             }
327
328             std::cout<<"DISCOVERED Resource:"<<std::endl;
329             // Get the resource URI
330             resourceURI = resource->uri();
331             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
332
333             // Get the resource host address
334             hostAddress = resource->host();
335             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
336
337             // Get the resource types
338             std::cout << "\tList of resource types: " << std::endl;
339             for(auto &resourceTypes : resource->getResourceTypes())
340             {
341                 std::cout << "\t\t" << resourceTypes << std::endl;
342             }
343
344             // Get the resource interfaces
345             std::cout << "\tList of resource interfaces: " << std::endl;
346             for(auto &resourceInterfaces : resource->getResourceInterfaces())
347             {
348                 std::cout << "\t\t" << resourceInterfaces << std::endl;
349             }
350
351             if(resourceURI == "/a/light")
352             {
353                 curResource = resource;
354                 sleep(1);
355                 // Call a local function which will internally invoke get
356                 // API on the resource pointer
357                 getLightRepresentation(resource);
358             }
359         }
360         else
361         {
362             // Resource is invalid
363             std::cout << "Resource is invalid" << std::endl;
364         }
365
366     }
367     catch(std::exception& e)
368     {
369         //log(e.what());
370     }
371 }
372
373 void PrintUsage()
374 {
375     std::cout << std::endl;
376     std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
377     std::cout << "   ObserveType : 1 - Observe" << std::endl;
378     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
379     std::cout<<"    ConnectivityType: Default WIFI" << std::endl;
380     std::cout << "   ConnectivityType : 0 - ETHERNET"<< std::endl;
381     std::cout << "   ConnectivityType : 1 - WIFI"<< std::endl;
382 }
383
384 int main(int argc, char* argv[]) {
385
386     std::ostringstream requestURI;
387
388     OCConnectivityType connectivityType = OC_WIFI;
389     try
390     {
391         if (argc == 1)
392         {
393             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
394         }
395         else if (argc ==2 || argc==3)
396         {
397             int value = std::stoi(argv[1]);
398             if (value == 1)
399                 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
400             else if (value == 2)
401                 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
402             else
403                 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
404
405             if(argc == 3)
406             {
407                 std::size_t inputValLen;
408                 int optionSelected = std::stoi(argv[2], &inputValLen);
409
410                 if(inputValLen == strlen(argv[2]))
411                 {
412                     if(optionSelected == 0)
413                     {
414                         connectivityType = OC_ETHERNET;
415                     }
416                     else if(optionSelected == 1)
417                     {
418                         connectivityType = OC_WIFI;
419                     }
420                     else
421                     {
422                         std::cout << "Invalid connectivity type selected. Using default WIFI"
423                             << std::endl;
424                     }
425                 }
426                 else
427                 {
428                     std::cout << "Invalid connectivity type selected. Using default WIFI"
429                     << std::endl;
430                 }
431             }
432         }
433         else
434         {
435             PrintUsage();
436             return -1;
437         }
438     }
439     catch(std::exception& e)
440     {
441         std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
442     }
443
444
445     // Create PlatformConfig object
446     PlatformConfig cfg {
447         OC::ServiceType::InProc,
448         OC::ModeType::Client,
449         "0.0.0.0",
450         0,
451         OC::QualityOfService::LowQos
452     };
453
454     OCPlatform::Configure(cfg);
455
456     try
457     {
458         // Find all resources
459         requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
460
461         OCPlatform::findResource("", requestURI.str(),
462                 connectivityType, &foundResource, OC::QualityOfService::LowQos);
463         std::cout<< "Finding Resource... " <<std::endl;
464
465         // Find resource is done twice so that we discover the original resources a second time.
466         // These resources will have the same uniqueidentifier (yet be different objects), so that
467         // we can verify/show the duplicate-checking code in foundResource(above);
468         OCPlatform::findResource("", requestURI.str(),
469                 connectivityType, &foundResource, OC::QualityOfService::LowQos);
470         std::cout<< "Finding Resource for second time... " <<std::endl;
471
472         // A condition variable will free the mutex it is given, then do a non-
473         // intensive block until 'notify' is called on it.  In this case, since we
474         // don't ever call cv.notify, this should be a non-processor intensive version
475         // of while(true);
476         std::mutex blocker;
477         std::condition_variable cv;
478         std::unique_lock<std::mutex> lock(blocker);
479         cv.wait(lock);
480
481     }
482     catch(OCException& e)
483     {
484         oclog() << "Exception in main: "<<e.what();
485     }
486
487     return 0;
488 }
489
490