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 std::mutex resourceLock;
47 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
57 Light() : m_state(false), m_power(0), m_name("")
70 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
71 const int& eCode, const int& sequenceNumber)
73 if(eCode == SUCCESS_RESPONSE)
75 std::cout << "OBSERVE RESULT:"<<std::endl;
76 if(sequenceNumber == (int) ObserveAction::ObserveRegister)
78 std::cout << "\tObserve Registration Confirmed: "<< std::endl;
80 else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
82 std::cout << "\tObserve Cancel Confirmed: "<< std::endl;
84 std::cout << "DONE"<<std::endl;
89 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
92 rep.getValue("state", mylight.m_state);
93 rep.getValue("power", mylight.m_power);
94 rep.getValue("name", mylight.m_name);
96 std::cout << "\tstate: " << mylight.m_state << std::endl;
97 std::cout << "\tpower: " << mylight.m_power << std::endl;
98 std::cout << "\tname: " << mylight.m_name << std::endl;
100 if(observe_count() > 10)
102 std::cout<<"Cancelling Observe..."<<std::endl;
103 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
105 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
110 std::cout << "onObserve Response error: " << eCode << std::endl;
115 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
117 if(eCode == SUCCESS_RESPONSE)
119 std::cout << "POST request was successful" << std::endl;
121 if(rep.hasAttribute("createduri"))
123 std::cout << "\tUri of the created resource: "
124 << rep.getValue<std::string>("createduri") << std::endl;
128 rep.getValue("state", mylight.m_state);
129 rep.getValue("power", mylight.m_power);
130 rep.getValue("name", mylight.m_name);
132 std::cout << "\tstate: " << mylight.m_state << std::endl;
133 std::cout << "\tpower: " << mylight.m_power << std::endl;
134 std::cout << "\tname: " << mylight.m_name << std::endl;
137 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
138 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
139 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
140 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
142 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
143 OC::QualityOfService::HighQos);
148 std::cout << "onPost2 Response error: " << eCode << std::endl;
153 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
155 if(eCode == SUCCESS_RESPONSE)
157 std::cout << "POST request was successful" << std::endl;
159 if(rep.hasAttribute("createduri"))
161 std::cout << "\tUri of the created resource: "
162 << rep.getValue<std::string>("createduri") << std::endl;
166 rep.getValue("state", mylight.m_state);
167 rep.getValue("power", mylight.m_power);
168 rep.getValue("name", mylight.m_name);
170 std::cout << "\tstate: " << mylight.m_state << std::endl;
171 std::cout << "\tpower: " << mylight.m_power << std::endl;
172 std::cout << "\tname: " << mylight.m_name << std::endl;
175 OCRepresentation rep2;
177 std::cout << "Posting light representation..."<<std::endl;
179 mylight.m_state = true;
180 mylight.m_power = 55;
182 rep2.setValue("state", mylight.m_state);
183 rep2.setValue("power", mylight.m_power);
185 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
189 std::cout << "onPost Response error: " << eCode << 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, OC::QualityOfService::HighQos);
214 // callback handler on PUT request
215 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
217 if(eCode == SUCCESS_RESPONSE)
219 std::cout << "PUT request was successful" << std::endl;
221 rep.getValue("state", mylight.m_state);
222 rep.getValue("power", mylight.m_power);
223 rep.getValue("name", mylight.m_name);
225 std::cout << "\tstate: " << mylight.m_state << std::endl;
226 std::cout << "\tpower: " << mylight.m_power << std::endl;
227 std::cout << "\tname: " << mylight.m_name << std::endl;
229 postLightRepresentation(curResource);
233 std::cout << "onPut Response error: " << eCode << std::endl;
238 // Local function to put a different state for this resource
239 void putLightRepresentation(std::shared_ptr<OCResource> resource)
243 OCRepresentation rep;
245 std::cout << "Putting light representation..."<<std::endl;
247 mylight.m_state = true;
248 mylight.m_power = 15;
250 rep.setValue("state", mylight.m_state);
251 rep.setValue("power", mylight.m_power);
253 // Invoke resource's put API with rep, query map and the callback parameter
254 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
258 // Callback handler on GET request
259 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
261 if(eCode == SUCCESS_RESPONSE)
263 std::cout << "GET request was successful" << std::endl;
264 std::cout << "Resource URI: " << rep.getUri() << std::endl;
266 rep.getValue("state", mylight.m_state);
267 rep.getValue("power", mylight.m_power);
268 rep.getValue("name", mylight.m_name);
270 std::cout << "\tstate: " << mylight.m_state << std::endl;
271 std::cout << "\tpower: " << mylight.m_power << std::endl;
272 std::cout << "\tname: " << mylight.m_name << std::endl;
274 putLightRepresentation(curResource);
278 std::cout << "onGET Response error: " << eCode << std::endl;
283 // Local function to get representation of light resource
284 void getLightRepresentation(std::shared_ptr<OCResource> resource)
288 std::cout << "Getting Light Representation..."<<std::endl;
289 // Invoke resource's get API with the callback parameter
292 resource->get(test, &onGet,OC::QualityOfService::HighQos);
296 // Callback to found resources
297 void foundResource(std::shared_ptr<OCResource> resource)
299 std::string resourceURI;
300 std::string hostAddress;
303 // Do some operations with resource object.
306 std::lock_guard<std::mutex> lk(resourceLock);
308 if(discoveredResources.find(resource) == discoveredResources.end())
310 std::cout << "Found resource " << resource->uniqueIdentifier() <<
311 " for the first time on server with ID: "<< resource->sid()<<std::endl;
312 discoveredResources.insert(resource);
316 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
321 std::cout << "Found another resource, ignoring"<<std::endl;
325 std::cout<<"DISCOVERED Resource:"<<std::endl;
326 // Get the resource URI
327 resourceURI = resource->uri();
328 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
330 // Get the resource host address
331 hostAddress = resource->host();
332 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
334 // Get the resource types
335 std::cout << "\tList of resource types: " << std::endl;
336 for(auto &resourceTypes : resource->getResourceTypes())
338 std::cout << "\t\t" << resourceTypes << std::endl;
341 // Get the resource interfaces
342 std::cout << "\tList of resource interfaces: " << std::endl;
343 for(auto &resourceInterfaces : resource->getResourceInterfaces())
345 std::cout << "\t\t" << resourceInterfaces << std::endl;
348 if(resourceURI == "/a/light")
350 curResource = resource;
352 // Call a local function which will internally invoke get
353 // 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)
366 std::cerr << "Exception in foundResource: "<< e.what() <<std::endl;
372 std::cout << std::endl;
373 std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
374 std::cout << " ObserveType : 1 - Observe" << std::endl;
375 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
376 std::cout << " ConnectivityType: Default IP" << std::endl;
377 std::cout << " ConnectivityType : 0 - IP"<< std::endl;
380 int main(int argc, char* argv[]) {
382 std::ostringstream requestURI;
384 OCConnectivityType connectivityType = CT_ADAPTER_IP;
389 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
391 else if (argc ==2 || argc==3)
393 int value = std::stoi(argv[1]);
395 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
397 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
399 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
403 std::size_t inputValLen;
404 int optionSelected = std::stoi(argv[2], &inputValLen);
406 if(inputValLen == strlen(argv[2]))
408 if(optionSelected == 0)
410 std::cout << "Using IP."<< std::endl;
411 connectivityType = CT_ADAPTER_IP;
415 std::cout << "Invalid connectivity type selected. Using default IP"
421 std::cout << "Invalid connectivity type selected. Using default IP"
432 catch(std::exception&)
434 std::cout << "Invalid input argument." << std::endl;
440 // Create PlatformConfig object
442 OC::ServiceType::InProc,
443 OC::ModeType::Client,
446 OC::QualityOfService::HighQos
449 OCPlatform::Configure(cfg);
453 // Find all resources
454 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
456 OCPlatform::findResource("", requestURI.str(),
457 connectivityType, &foundResource, OC::QualityOfService::LowQos);
458 std::cout<< "Finding Resource... " <<std::endl;
460 // Find resource is done twice so that we discover the original resources a second time.
461 // These resources will have the same uniqueidentifier (yet be different objects), so that
462 // we can verify/show the duplicate-checking code in foundResource(above);
463 OCPlatform::findResource("", requestURI.str(),
464 connectivityType, &foundResource, OC::QualityOfService::LowQos);
465 std::cout<< "Finding Resource for second time... " <<std::endl;
467 // A condition variable will free the mutex it is given, then do a non-
468 // intensive block until 'notify' is called on it. In this case, since we
469 // don't ever call cv.notify, this should be a non-processor intensive version
472 std::condition_variable cv;
473 std::unique_lock<std::mutex> lock(blocker);
477 catch(OCException& e)
479 oclog() << "Exception in main: "<<e.what();