Modifying version number for building on tizen 3.0
[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 <string>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include <mutex>
27 #include <condition_variable>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33
34 const int SUCCESS_RESPONSE = 0;
35 std::shared_ptr<OCResource> curResource;
36 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
37
38 class Light
39 {
40 public:
41
42     bool m_state;
43     int m_power;
44     std::string m_name;
45
46     Light() : m_state(false), m_power(0), m_name("")
47     {
48     }
49 };
50
51 Light mylight;
52
53 int observe_count()
54 {
55     static int oc = 0;
56     return ++oc;
57 }
58
59 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
60                     const int& eCode, const int& sequenceNumber)
61 {
62     if(eCode == SUCCESS_RESPONSE)
63     {
64         std::cout << "OBSERVE RESULT:"<<std::endl;
65         if(sequenceNumber == 0)
66         {
67             std::cout << "\tObserve Registration Confirmed: "<< endl;
68         }
69         else if (sequenceNumber == 1)
70         {
71             std::cout << "\tObserve Cancel Confirmed: "<< endl;
72             sleep(10);
73             std::cout << "DONE"<<std::endl;
74             std::exit(0);
75         }
76         else
77         {
78             std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
79         }
80
81         rep.getValue("state", mylight.m_state);
82         rep.getValue("power", mylight.m_power);
83         rep.getValue("name", mylight.m_name);
84
85         std::cout << "\tstate: " << mylight.m_state << std::endl;
86         std::cout << "\tpower: " << mylight.m_power << std::endl;
87         std::cout << "\tname: " << mylight.m_name << std::endl;
88
89         if(observe_count() > 30)
90         {
91             std::cout<<"Cancelling Observe..."<<std::endl;
92             OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
93
94             std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
95         }
96     }
97     else
98     {
99         std::cout << "onObserve Response error: " << eCode << std::endl;
100         std::exit(-1);
101     }
102 }
103
104 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
105 {
106     if(eCode == SUCCESS_RESPONSE)
107     {
108         std::cout << "POST request was successful" << std::endl;
109
110         if(rep.hasAttribute("createduri"))
111         {
112             std::cout << "\tUri of the created resource: "
113                       << rep.getValue<std::string>("createduri") << std::endl;
114         }
115         else
116         {
117             rep.getValue("state", mylight.m_state);
118             rep.getValue("power", mylight.m_power);
119             rep.getValue("name", mylight.m_name);
120
121             std::cout << "\tstate: " << mylight.m_state << std::endl;
122             std::cout << "\tpower: " << mylight.m_power << std::endl;
123             std::cout << "\tname: " << mylight.m_name << std::endl;
124         }
125
126         if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
127             std::cout << endl << "Observe is used." << endl << endl;
128         else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
129             std::cout << endl << "ObserveAll is used." << endl << endl;
130         sleep(1);
131         curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
132                 OC::QualityOfService::HighQos);
133
134     }
135     else
136     {
137         std::cout << "onPost2 Response error: " << eCode << std::endl;
138         std::exit(-1);
139     }
140 }
141
142 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
143 {
144     if(eCode == SUCCESS_RESPONSE)
145     {
146         std::cout << "POST request was successful" << std::endl;
147
148         if(rep.hasAttribute("createduri"))
149         {
150             std::cout << "\tUri of the created resource: "
151                       << rep.getValue<std::string>("createduri") << std::endl;
152         }
153         else
154         {
155             rep.getValue("state", mylight.m_state);
156             rep.getValue("power", mylight.m_power);
157             rep.getValue("name", mylight.m_name);
158
159             std::cout << "\tstate: " << mylight.m_state << std::endl;
160             std::cout << "\tpower: " << mylight.m_power << std::endl;
161             std::cout << "\tname: " << mylight.m_name << std::endl;
162         }
163
164         OCRepresentation rep2;
165
166         std::cout << "Posting light representation..."<<std::endl;
167
168         mylight.m_state = true;
169         mylight.m_power = 55;
170
171         rep2.setValue("state", mylight.m_state);
172         rep2.setValue("power", mylight.m_power);
173         sleep(1);
174         curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
175     }
176     else
177     {
178         std::cout << "onPost Response error: " << eCode << std::endl;
179         std::exit(-1);
180     }
181 }
182
183 // Local function to put a different state for this resource
184 void postLightRepresentation(std::shared_ptr<OCResource> resource)
185 {
186     if(resource)
187     {
188         OCRepresentation rep;
189
190         std::cout << "Posting light representation..."<<std::endl;
191
192         mylight.m_state = false;
193         mylight.m_power = 105;
194
195         rep.setValue("state", mylight.m_state);
196         rep.setValue("power", mylight.m_power);
197
198         // Invoke resource's post API with rep, query map and the callback parameter
199         resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
200     }
201 }
202
203 // callback handler on PUT request
204 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
205 {
206     if(eCode == SUCCESS_RESPONSE)
207     {
208         std::cout << "PUT request was successful" << std::endl;
209
210         rep.getValue("state", mylight.m_state);
211         rep.getValue("power", mylight.m_power);
212         rep.getValue("name", mylight.m_name);
213
214         std::cout << "\tstate: " << mylight.m_state << std::endl;
215         std::cout << "\tpower: " << mylight.m_power << std::endl;
216         std::cout << "\tname: " << mylight.m_name << std::endl;
217         sleep(1);
218         postLightRepresentation(curResource);
219     }
220     else
221     {
222         std::cout << "onPut Response error: " << eCode << std::endl;
223         std::exit(-1);
224     }
225 }
226
227 // Local function to put a different state for this resource
228 void putLightRepresentation(std::shared_ptr<OCResource> resource)
229 {
230     if(resource)
231     {
232         OCRepresentation rep;
233
234         std::cout << "Putting light representation..."<<std::endl;
235
236         mylight.m_state = true;
237         mylight.m_power = 15;
238
239         rep.setValue("state", mylight.m_state);
240         rep.setValue("power", mylight.m_power);
241
242         // Invoke resource's put API with rep, query map and the callback parameter
243         resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
244     }
245 }
246
247 // Callback handler on GET request
248 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
249 {
250     if(eCode == SUCCESS_RESPONSE)
251     {
252         std::cout << "GET request was successful" << std::endl;
253         std::cout << "Resource URI: " << rep.getUri() << std::endl;
254
255         rep.getValue("state", mylight.m_state);
256         rep.getValue("power", mylight.m_power);
257         rep.getValue("name", mylight.m_name);
258
259         std::cout << "\tstate: " << mylight.m_state << std::endl;
260         std::cout << "\tpower: " << mylight.m_power << std::endl;
261         std::cout << "\tname: " << mylight.m_name << std::endl;
262         sleep(1);
263         putLightRepresentation(curResource);
264     }
265     else
266     {
267         std::cout << "onGET Response error: " << eCode << std::endl;
268         std::exit(-1);
269     }
270 }
271
272 // Local function to get representation of light resource
273 void getLightRepresentation(std::shared_ptr<OCResource> resource)
274 {
275     if(resource)
276     {
277         std::cout << "Getting Light Representation..."<<std::endl;
278         // Invoke resource's get API with the callback parameter
279
280         QueryParamsMap test;
281         resource->get(test, &onGet,OC::QualityOfService::HighQos);
282     }
283 }
284
285 // Callback to found resources
286 void foundResource(std::shared_ptr<OCResource> resource)
287 {
288     if(curResource)
289     {
290         std::cout << "Found another resource, ignoring"<<std::endl;
291     }
292
293     std::string resourceURI;
294     std::string hostAddress;
295     try
296     {
297         // Do some operations with resource object.
298         if(resource)
299         {
300             std::cout<<"DISCOVERED Resource:"<<std::endl;
301             // Get the resource URI
302             resourceURI = resource->uri();
303             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
304
305             // Get the resource host address
306             hostAddress = resource->host();
307             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
308
309             // Get the resource types
310             std::cout << "\tList of resource types: " << std::endl;
311             for(auto &resourceTypes : resource->getResourceTypes())
312             {
313                 std::cout << "\t\t" << resourceTypes << std::endl;
314             }
315
316             // Get the resource interfaces
317             std::cout << "\tList of resource interfaces: " << std::endl;
318             for(auto &resourceInterfaces : resource->getResourceInterfaces())
319             {
320                 std::cout << "\t\t" << resourceInterfaces << std::endl;
321             }
322
323             if(resourceURI == "/a/light")
324             {
325                 curResource = resource;
326                 sleep(1);
327                 // Call a local function which will internally invoke get API on the resource pointer
328                 getLightRepresentation(resource);
329             }
330         }
331         else
332         {
333             // Resource is invalid
334             std::cout << "Resource is invalid" << std::endl;
335         }
336
337     }
338     catch(std::exception& e)
339     {
340         //log(e.what());
341     }
342 }
343
344 void PrintUsage()
345 {
346     std::cout << std::endl;
347     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
348     std::cout << "   ObserveType : 1 - Observe" << std::endl;
349     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
350 }
351
352 int main(int argc, char* argv[]) {
353     if (argc == 1)
354     {
355         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
356     }
357     else if (argc == 2)
358     {
359         int value = atoi(argv[1]);
360         if (value == 1)
361             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
362         else if (value == 2)
363             OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
364         else
365             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
366     }
367     else
368     {
369         PrintUsage();
370         return -1;
371     }
372
373     // Create PlatformConfig object
374     PlatformConfig cfg {
375         OC::ServiceType::InProc,
376         OC::ModeType::Client,
377         "0.0.0.0",
378         0,
379         OC::QualityOfService::LowQos
380     };
381
382     OCPlatform::Configure(cfg);
383
384     try
385     {
386         // Find all resources
387         OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource,
388                 OC::QualityOfService::LowQos);
389         std::cout<< "Finding Resource... " <<std::endl;
390
391         // A condition variable will free the mutex it is given, then do a non-
392         // intensive block until 'notify' is called on it.  In this case, since we
393         // don't ever call cv.notify, this should be a non-processor intensive version
394         // of while(true);
395         std::mutex blocker;
396         std::condition_variable cv;
397         std::unique_lock<std::mutex> lock(blocker);
398         cv.wait(lock);
399
400     }catch(OCException& e)
401     {
402         //log(e.what());
403     }
404
405     return 0;
406 }
407