1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // OCClient.cpp : Defines the entry point for the console application.
27 #include <condition_variable>
29 #include "OCPlatform.h"
34 std::shared_ptr<OCResource> curResource;
35 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
36 std::mutex curResourceLock;
46 Light() : m_state(false), m_power(0), m_name("")
59 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
60 const int& eCode, const int& sequenceNumber)
64 if(eCode == OC_STACK_OK)
66 std::cout << "OBSERVE RESULT:"<<std::endl;
67 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
69 rep.getValue("state", mylight.m_state);
70 rep.getValue("power", mylight.m_power);
71 rep.getValue("name", mylight.m_name);
73 std::cout << "\tstate: " << mylight.m_state << std::endl;
74 std::cout << "\tpower: " << mylight.m_power << std::endl;
75 std::cout << "\tname: " << mylight.m_name << std::endl;
77 if(observe_count() > 30)
79 std::cout<<"Cancelling Observe..."<<std::endl;
80 OCStackResult result = curResource->cancelObserve();
82 std::cout << "Cancel result: "<< result <<std::endl;
84 std::cout << "DONE"<<std::endl;
90 std::cout << "onObserve Response error: " << eCode << std::endl;
94 catch(std::exception& e)
96 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
101 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
105 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
107 std::cout << "POST request was successful" << std::endl;
109 if(rep.hasAttribute("createduri"))
111 std::cout << "\tUri of the created resource: "
112 << rep.getValue<std::string>("createduri") << std::endl;
116 rep.getValue("state", mylight.m_state);
117 rep.getValue("power", mylight.m_power);
118 rep.getValue("name", mylight.m_name);
120 std::cout << "\tstate: " << mylight.m_state << std::endl;
121 std::cout << "\tpower: " << mylight.m_power << std::endl;
122 std::cout << "\tname: " << mylight.m_name << std::endl;
125 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
126 std::cout << endl << "Observe is used." << endl << endl;
127 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
128 std::cout << endl << "ObserveAll is used." << endl << endl;
130 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
135 std::cout << "onPost2 Response error: " << eCode << std::endl;
139 catch(std::exception& e)
141 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
146 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
150 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
152 std::cout << "POST request was successful" << std::endl;
154 if(rep.hasAttribute("createduri"))
156 std::cout << "\tUri of the created resource: "
157 << rep.getValue<std::string>("createduri") << std::endl;
161 rep.getValue("state", mylight.m_state);
162 rep.getValue("power", mylight.m_power);
163 rep.getValue("name", mylight.m_name);
165 std::cout << "\tstate: " << mylight.m_state << std::endl;
166 std::cout << "\tpower: " << mylight.m_power << std::endl;
167 std::cout << "\tname: " << mylight.m_name << std::endl;
170 OCRepresentation rep2;
172 std::cout << "Posting light representation..."<<std::endl;
174 mylight.m_state = true;
175 mylight.m_power = 55;
177 rep2.setValue("state", mylight.m_state);
178 rep2.setValue("power", mylight.m_power);
180 curResource->post(rep2, QueryParamsMap(), &onPost2);
184 std::cout << "onPost Response error: " << eCode << std::endl;
188 catch(std::exception& e)
190 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
194 // Local function to put a different state for this resource
195 void postLightRepresentation(std::shared_ptr<OCResource> resource)
199 OCRepresentation rep;
201 std::cout << "Posting light representation..."<<std::endl;
203 mylight.m_state = false;
204 mylight.m_power = 105;
206 rep.setValue("state", mylight.m_state);
207 rep.setValue("power", mylight.m_power);
209 // Invoke resource's post API with rep, query map and the callback parameter
210 resource->post(rep, QueryParamsMap(), &onPost);
214 // callback handler on PUT request
215 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
219 if(eCode == OC_STACK_OK)
221 std::cout << "PUT request was successful" << std::endl;
223 rep.getValue("state", mylight.m_state);
224 rep.getValue("power", mylight.m_power);
225 rep.getValue("name", mylight.m_name);
227 std::cout << "\tstate: " << mylight.m_state << std::endl;
228 std::cout << "\tpower: " << mylight.m_power << std::endl;
229 std::cout << "\tname: " << mylight.m_name << std::endl;
231 postLightRepresentation(curResource);
235 std::cout << "onPut Response error: " << eCode << std::endl;
239 catch(std::exception& e)
241 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
245 // Local function to put a different state for this resource
246 void putLightRepresentation(std::shared_ptr<OCResource> resource)
250 OCRepresentation rep;
252 std::cout << "Putting light representation..."<<std::endl;
254 mylight.m_state = true;
255 mylight.m_power = 15;
257 rep.setValue("state", mylight.m_state);
258 rep.setValue("power", mylight.m_power);
260 // Invoke resource's put API with rep, query map and the callback parameter
261 resource->put(rep, QueryParamsMap(), &onPut);
265 // Callback handler on GET request
266 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
270 if(eCode == OC_STACK_OK)
272 std::cout << "GET request was successful" << std::endl;
273 std::cout << "Resource URI: " << rep.getUri() << std::endl;
275 rep.getValue("state", mylight.m_state);
276 rep.getValue("power", mylight.m_power);
277 rep.getValue("name", mylight.m_name);
279 std::cout << "\tstate: " << mylight.m_state << std::endl;
280 std::cout << "\tpower: " << mylight.m_power << std::endl;
281 std::cout << "\tname: " << mylight.m_name << std::endl;
283 putLightRepresentation(curResource);
287 std::cout << "onGET Response error: " << eCode << std::endl;
291 catch(std::exception& e)
293 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
297 // Local function to get representation of light resource
298 void getLightRepresentation(std::shared_ptr<OCResource> resource)
302 std::cout << "Getting Light Representation..."<<std::endl;
303 // Invoke resource's get API with the callback parameter
306 resource->get(test, &onGet);
310 // Callback to found resources
311 void foundResource(std::shared_ptr<OCResource> resource)
313 std::lock_guard<std::mutex> lock(curResourceLock);
316 std::cout << "Found another resource, ignoring"<<std::endl;
320 std::string resourceURI;
321 std::string hostAddress;
324 // Do some operations with resource object.
327 std::cout<<"DISCOVERED Resource:"<<std::endl;
328 // Get the resource URI
329 resourceURI = resource->uri();
330 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
332 // Get the resource host address
333 hostAddress = resource->host();
334 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
336 // Get the resource types
337 std::cout << "\tList of resource types: " << std::endl;
338 for(auto &resourceTypes : resource->getResourceTypes())
340 std::cout << "\t\t" << resourceTypes << std::endl;
343 // Get the resource interfaces
344 std::cout << "\tList of resource interfaces: " << std::endl;
345 for(auto &resourceInterfaces : resource->getResourceInterfaces())
347 std::cout << "\t\t" << resourceInterfaces << std::endl;
350 if(resourceURI == "/a/light")
352 curResource = resource;
353 // Call a local function which will internally invoke get API on the resource pointer
354 getLightRepresentation(resource);
359 // Resource is invalid
360 std::cout << "Resource is invalid" << std::endl;
364 catch(std::exception& e)
372 std::cout << std::endl;
373 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
374 std::cout << " ObserveType : 1 - Observe" << std::endl;
375 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
378 int main(int argc, char* argv[]) {
381 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
385 int value = atoi(argv[1]);
387 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
389 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
391 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
399 // Create PlatformConfig object
401 OC::ServiceType::InProc,
402 OC::ModeType::Client,
405 OC::QualityOfService::LowQos
408 OCPlatform::Configure(cfg);
411 // makes it so that all boolean values are printed as 'true/false' in this stream
412 std::cout.setf(std::ios::boolalpha);
413 // Find all resources
414 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource);
415 std::cout<< "Finding Resource... " <<std::endl;
417 // A condition variable will free the mutex it is given, then do a non-
418 // intensive block until 'notify' is called on it. In this case, since we
419 // don't ever call cv.notify, this should be a non-processor intensive version
422 std::condition_variable cv;
423 std::unique_lock<std::mutex> lock(blocker);
426 }catch(OCException& e)