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)
62 if(eCode == OC_STACK_OK)
64 std::cout << "OBSERVE RESULT:"<<std::endl;
65 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
67 rep.getValue("state", mylight.m_state);
68 rep.getValue("power", mylight.m_power);
69 rep.getValue("name", mylight.m_name);
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;
75 if(observe_count() > 30)
77 std::cout<<"Cancelling Observe..."<<std::endl;
78 OCStackResult result = curResource->cancelObserve();
80 std::cout << "Cancel result: "<< result <<std::endl;
82 std::cout << "DONE"<<std::endl;
88 std::cout << "onObserve Response error: " << eCode << std::endl;
93 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
95 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
97 std::cout << "POST request was successful" << std::endl;
99 if(rep.hasAttribute("createduri"))
101 std::cout << "\tUri of the created resource: "
102 << rep.getValue<std::string>("createduri") << std::endl;
106 rep.getValue("state", mylight.m_state);
107 rep.getValue("power", mylight.m_power);
108 rep.getValue("name", mylight.m_name);
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;
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;
120 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
125 std::cout << "onPost2 Response error: " << eCode << std::endl;
130 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
132 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
134 std::cout << "POST request was successful" << std::endl;
136 if(rep.hasAttribute("createduri"))
138 std::cout << "\tUri of the created resource: "
139 << rep.getValue<std::string>("createduri") << std::endl;
143 rep.getValue("state", mylight.m_state);
144 rep.getValue("power", mylight.m_power);
145 rep.getValue("name", mylight.m_name);
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;
152 OCRepresentation rep2;
154 std::cout << "Posting light representation..."<<std::endl;
156 mylight.m_state = true;
157 mylight.m_power = 55;
159 rep2.setValue("state", mylight.m_state);
160 rep2.setValue("power", mylight.m_power);
162 curResource->post(rep2, QueryParamsMap(), &onPost2);
166 std::cout << "onPost Response error: " << eCode << std::endl;
171 // Local function to put a different state for this resource
172 void postLightRepresentation(std::shared_ptr<OCResource> resource)
176 OCRepresentation rep;
178 std::cout << "Posting light representation..."<<std::endl;
180 mylight.m_state = false;
181 mylight.m_power = 105;
183 rep.setValue("state", mylight.m_state);
184 rep.setValue("power", mylight.m_power);
186 // Invoke resource's post API with rep, query map and the callback parameter
187 resource->post(rep, QueryParamsMap(), &onPost);
191 // callback handler on PUT request
192 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
194 if(eCode == OC_STACK_OK)
196 std::cout << "PUT request was successful" << std::endl;
198 rep.getValue("state", mylight.m_state);
199 rep.getValue("power", mylight.m_power);
200 rep.getValue("name", mylight.m_name);
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;
206 postLightRepresentation(curResource);
210 std::cout << "onPut Response error: " << eCode << std::endl;
215 // Local function to put a different state for this resource
216 void putLightRepresentation(std::shared_ptr<OCResource> resource)
220 OCRepresentation rep;
222 std::cout << "Putting light representation..."<<std::endl;
224 mylight.m_state = true;
225 mylight.m_power = 15;
227 rep.setValue("state", mylight.m_state);
228 rep.setValue("power", mylight.m_power);
230 // Invoke resource's put API with rep, query map and the callback parameter
231 resource->put(rep, QueryParamsMap(), &onPut);
235 // Callback handler on GET request
236 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
238 if(eCode == OC_STACK_OK)
240 std::cout << "GET request was successful" << std::endl;
241 std::cout << "Resource URI: " << rep.getUri() << std::endl;
243 rep.getValue("state", mylight.m_state);
244 rep.getValue("power", mylight.m_power);
245 rep.getValue("name", mylight.m_name);
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;
251 putLightRepresentation(curResource);
255 std::cout << "onGET Response error: " << eCode << std::endl;
260 // Local function to get representation of light resource
261 void getLightRepresentation(std::shared_ptr<OCResource> resource)
265 std::cout << "Getting Light Representation..."<<std::endl;
266 // Invoke resource's get API with the callback parameter
269 resource->get(test, &onGet);
273 // Callback to found resources
274 void foundResource(std::shared_ptr<OCResource> resource)
276 std::lock_guard<std::mutex> lock(curResourceLock);
279 std::cout << "Found another resource, ignoring"<<std::endl;
283 std::string resourceURI;
284 std::string hostAddress;
287 // Do some operations with resource object.
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;
295 // Get the resource host address
296 hostAddress = resource->host();
297 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
299 // Get the resource types
300 std::cout << "\tList of resource types: " << std::endl;
301 for(auto &resourceTypes : resource->getResourceTypes())
303 std::cout << "\t\t" << resourceTypes << std::endl;
306 // Get the resource interfaces
307 std::cout << "\tList of resource interfaces: " << std::endl;
308 for(auto &resourceInterfaces : resource->getResourceInterfaces())
310 std::cout << "\t\t" << resourceInterfaces << std::endl;
313 if(resourceURI == "/a/light")
315 curResource = resource;
316 // Call a local function which will internally invoke get API on the resource pointer
317 getLightRepresentation(resource);
322 // Resource is invalid
323 std::cout << "Resource is invalid" << std::endl;
327 catch(std::exception& e)
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;
341 int main(int argc, char* argv[]) {
344 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
348 int value = atoi(argv[1]);
350 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
352 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
354 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
362 // Create PlatformConfig object
364 OC::ServiceType::InProc,
365 OC::ModeType::Client,
368 OC::QualityOfService::LowQos
371 OCPlatform::Configure(cfg);
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;
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
385 std::condition_variable cv;
386 std::unique_lock<std::mutex> lock(blocker);
389 }catch(OCException& e)