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