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