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.
28 #include <condition_variable>
29 #include "OCPlatform.h"
34 struct dereference_compare
36 bool operator()(std::shared_ptr<OCResource> lhs, std::shared_ptr<OCResource> rhs )const
41 typedef std::set<std::shared_ptr<OCResource>, dereference_compare> DiscoveredResourceSet;
43 DiscoveredResourceSet discoveredResources;
44 const int SUCCESS_RESPONSE = 0;
45 std::shared_ptr<OCResource> curResource;
46 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
56 Light() : m_state(false), m_power(0), m_name("")
69 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
70 const int& eCode, const int& sequenceNumber)
72 if(eCode == SUCCESS_RESPONSE)
74 std::cout << "OBSERVE RESULT:"<<std::endl;
75 if(sequenceNumber == 0)
77 std::cout << "\tObserve Registration Confirmed: "<< endl;
79 else if (sequenceNumber == 1)
81 std::cout << "\tObserve Cancel Confirmed: "<< endl;
83 std::cout << "DONE"<<std::endl;
88 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
91 rep.getValue("state", mylight.m_state);
92 rep.getValue("power", mylight.m_power);
93 rep.getValue("name", mylight.m_name);
95 std::cout << "\tstate: " << mylight.m_state << std::endl;
96 std::cout << "\tpower: " << mylight.m_power << std::endl;
97 std::cout << "\tname: " << mylight.m_name << std::endl;
99 if(observe_count() > 30)
101 std::cout<<"Cancelling Observe..."<<std::endl;
102 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
104 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
109 std::cout << "onObserve Response error: " << eCode << std::endl;
114 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
116 if(eCode == SUCCESS_RESPONSE)
118 std::cout << "POST request was successful" << std::endl;
120 if(rep.hasAttribute("createduri"))
122 std::cout << "\tUri of the created resource: "
123 << rep.getValue<std::string>("createduri") << std::endl;
127 rep.getValue("state", mylight.m_state);
128 rep.getValue("power", mylight.m_power);
129 rep.getValue("name", mylight.m_name);
131 std::cout << "\tstate: " << mylight.m_state << std::endl;
132 std::cout << "\tpower: " << mylight.m_power << std::endl;
133 std::cout << "\tname: " << mylight.m_name << std::endl;
136 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
137 std::cout << endl << "Observe is used." << endl << endl;
138 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
139 std::cout << endl << "ObserveAll is used." << endl << endl;
141 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
142 OC::QualityOfService::HighQos);
147 std::cout << "onPost2 Response error: " << eCode << std::endl;
152 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
154 if(eCode == SUCCESS_RESPONSE)
156 std::cout << "POST request was successful" << std::endl;
158 if(rep.hasAttribute("createduri"))
160 std::cout << "\tUri of the created resource: "
161 << rep.getValue<std::string>("createduri") << std::endl;
165 rep.getValue("state", mylight.m_state);
166 rep.getValue("power", mylight.m_power);
167 rep.getValue("name", mylight.m_name);
169 std::cout << "\tstate: " << mylight.m_state << std::endl;
170 std::cout << "\tpower: " << mylight.m_power << std::endl;
171 std::cout << "\tname: " << mylight.m_name << std::endl;
174 OCRepresentation rep2;
176 std::cout << "Posting light representation..."<<std::endl;
178 mylight.m_state = true;
179 mylight.m_power = 55;
181 rep2.setValue("state", mylight.m_state);
182 rep2.setValue("power", mylight.m_power);
184 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
188 std::cout << "onPost Response error: " << eCode << std::endl;
193 // Local function to put a different state for this resource
194 void postLightRepresentation(std::shared_ptr<OCResource> resource)
198 OCRepresentation rep;
200 std::cout << "Posting light representation..."<<std::endl;
202 mylight.m_state = false;
203 mylight.m_power = 105;
205 rep.setValue("state", mylight.m_state);
206 rep.setValue("power", mylight.m_power);
208 // Invoke resource's post API with rep, query map and the callback parameter
209 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
213 // callback handler on PUT request
214 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
216 if(eCode == SUCCESS_RESPONSE)
218 std::cout << "PUT request was successful" << std::endl;
220 rep.getValue("state", mylight.m_state);
221 rep.getValue("power", mylight.m_power);
222 rep.getValue("name", mylight.m_name);
224 std::cout << "\tstate: " << mylight.m_state << std::endl;
225 std::cout << "\tpower: " << mylight.m_power << std::endl;
226 std::cout << "\tname: " << mylight.m_name << std::endl;
228 postLightRepresentation(curResource);
232 std::cout << "onPut Response error: " << eCode << std::endl;
237 // Local function to put a different state for this resource
238 void putLightRepresentation(std::shared_ptr<OCResource> resource)
242 OCRepresentation rep;
244 std::cout << "Putting light representation..."<<std::endl;
246 mylight.m_state = true;
247 mylight.m_power = 15;
249 rep.setValue("state", mylight.m_state);
250 rep.setValue("power", mylight.m_power);
252 // Invoke resource's put API with rep, query map and the callback parameter
253 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
257 // Callback handler on GET request
258 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
260 if(eCode == SUCCESS_RESPONSE)
262 std::cout << "GET request was successful" << std::endl;
263 std::cout << "Resource URI: " << rep.getUri() << std::endl;
265 rep.getValue("state", mylight.m_state);
266 rep.getValue("power", mylight.m_power);
267 rep.getValue("name", mylight.m_name);
269 std::cout << "\tstate: " << mylight.m_state << std::endl;
270 std::cout << "\tpower: " << mylight.m_power << std::endl;
271 std::cout << "\tname: " << mylight.m_name << std::endl;
273 putLightRepresentation(curResource);
277 std::cout << "onGET Response error: " << eCode << std::endl;
282 // Local function to get representation of light resource
283 void getLightRepresentation(std::shared_ptr<OCResource> resource)
287 std::cout << "Getting Light Representation..."<<std::endl;
288 // Invoke resource's get API with the callback parameter
291 resource->get(test, &onGet,OC::QualityOfService::HighQos);
295 // Callback to found resources
296 void foundResource(std::shared_ptr<OCResource> resource)
298 std::string resourceURI;
299 std::string hostAddress;
302 if(discoveredResources.find(resource) == discoveredResources.end())
304 std::cout << "Found resource " << resource->uniqueIdentifier() <<
305 " for the first time on server with ID: "<< resource->sid()<<std::endl;
306 discoveredResources.insert(resource);
310 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
315 std::cout << "Found another resource, ignoring"<<std::endl;
319 // Do some operations with resource object.
322 std::cout<<"DISCOVERED Resource:"<<std::endl;
323 // Get the resource URI
324 resourceURI = resource->uri();
325 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
327 // Get the resource host address
328 hostAddress = resource->host();
329 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
331 // Get the resource types
332 std::cout << "\tList of resource types: " << std::endl;
333 for(auto &resourceTypes : resource->getResourceTypes())
335 std::cout << "\t\t" << resourceTypes << std::endl;
338 // Get the resource interfaces
339 std::cout << "\tList of resource interfaces: " << std::endl;
340 for(auto &resourceInterfaces : resource->getResourceInterfaces())
342 std::cout << "\t\t" << resourceInterfaces << std::endl;
345 if(resourceURI == "/a/light")
347 curResource = resource;
349 // Call a local function which will internally invoke get API on the resource pointer
350 getLightRepresentation(resource);
355 // Resource is invalid
356 std::cout << "Resource is invalid" << std::endl;
360 catch(std::exception& e)
368 std::cout << std::endl;
369 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
370 std::cout << " ObserveType : 1 - Observe" << std::endl;
371 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
374 int main(int argc, char* argv[]) {
377 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
381 int value = atoi(argv[1]);
383 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
385 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
387 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
395 // Create PlatformConfig object
397 OC::ServiceType::InProc,
398 OC::ModeType::Client,
401 OC::QualityOfService::LowQos
404 OCPlatform::Configure(cfg);
408 // Find all resources
410 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light",
411 OC_WIFI, &foundResource, OC::QualityOfService::LowQos);
413 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource,
414 OC::QualityOfService::LowQos);
416 std::cout<< "Finding Resource... " <<std::endl;
418 // Find resource is done twice so that we discover the original resources a second time.
419 // These resources will have the same uniqueidentifier (yet be different objects), so that
420 // we can verify/show the duplicate-checking code in foundResource(above);
422 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light",
423 OC_WIFI, &foundResource, OC::QualityOfService::LowQos);
425 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource,
426 OC::QualityOfService::LowQos);
428 std::cout<< "Finding Resource for second time... " <<std::endl;
434 // A condition variable will free the mutex it is given, then do a non-
435 // intensive block until 'notify' is called on it. In this case, since we
436 // don't ever call cv.notify, this should be a non-processor intensive version
439 std::condition_variable cv;
440 std::unique_lock<std::mutex> lock(blocker);
443 }catch(OCException& e)