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