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;
48 //TODO-CA: Since CA CON message support is still in progress, this client uses
49 // LowQos messages. This needs to change once Confirmable messages are supported in CA.
59 Light() : m_state(false), m_power(0), m_name("")
72 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
73 const int& eCode, const int& sequenceNumber)
75 if(eCode == SUCCESS_RESPONSE)
77 std::cout << "OBSERVE RESULT:"<<std::endl;
78 if(sequenceNumber == 0)
80 std::cout << "\tObserve Registration Confirmed: "<< endl;
82 else if (sequenceNumber == 1)
84 std::cout << "\tObserve Cancel Confirmed: "<< endl;
86 std::cout << "DONE"<<std::endl;
91 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
94 rep.getValue("state", mylight.m_state);
95 rep.getValue("power", mylight.m_power);
96 rep.getValue("name", mylight.m_name);
98 std::cout << "\tstate: " << mylight.m_state << std::endl;
99 std::cout << "\tpower: " << mylight.m_power << std::endl;
100 std::cout << "\tname: " << mylight.m_name << std::endl;
102 if(observe_count() > 30)
104 std::cout<<"Cancelling Observe..."<<std::endl;
105 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::LowQos);
107 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
112 std::cout << "onObserve Response error: " << eCode << std::endl;
117 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
119 if(eCode == SUCCESS_RESPONSE)
121 std::cout << "POST request was successful" << std::endl;
123 if(rep.hasAttribute("createduri"))
125 std::cout << "\tUri of the created resource: "
126 << rep.getValue<std::string>("createduri") << std::endl;
130 rep.getValue("state", mylight.m_state);
131 rep.getValue("power", mylight.m_power);
132 rep.getValue("name", mylight.m_name);
134 std::cout << "\tstate: " << mylight.m_state << std::endl;
135 std::cout << "\tpower: " << mylight.m_power << std::endl;
136 std::cout << "\tname: " << mylight.m_name << std::endl;
139 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
140 std::cout << endl << "Observe is used." << endl << endl;
141 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
142 std::cout << endl << "ObserveAll is used." << endl << endl;
144 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
145 OC::QualityOfService::LowQos);
150 std::cout << "onPost2 Response error: " << eCode << std::endl;
155 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
157 if(eCode == SUCCESS_RESPONSE)
159 std::cout << "POST request was successful" << std::endl;
161 if(rep.hasAttribute("createduri"))
163 std::cout << "\tUri of the created resource: "
164 << rep.getValue<std::string>("createduri") << std::endl;
168 rep.getValue("state", mylight.m_state);
169 rep.getValue("power", mylight.m_power);
170 rep.getValue("name", mylight.m_name);
172 std::cout << "\tstate: " << mylight.m_state << std::endl;
173 std::cout << "\tpower: " << mylight.m_power << std::endl;
174 std::cout << "\tname: " << mylight.m_name << std::endl;
177 OCRepresentation rep2;
179 std::cout << "Posting light representation..."<<std::endl;
181 mylight.m_state = true;
182 mylight.m_power = 55;
184 rep2.setValue("state", mylight.m_state);
185 rep2.setValue("power", mylight.m_power);
187 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::LowQos);
191 std::cout << "onPost Response error: " << eCode << std::endl;
196 // Local function to put a different state for this resource
197 void postLightRepresentation(std::shared_ptr<OCResource> resource)
201 OCRepresentation rep;
203 std::cout << "Posting light representation..."<<std::endl;
205 mylight.m_state = false;
206 mylight.m_power = 105;
208 rep.setValue("state", mylight.m_state);
209 rep.setValue("power", mylight.m_power);
211 // Invoke resource's post API with rep, query map and the callback parameter
212 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
216 // callback handler on PUT request
217 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
219 if(eCode == SUCCESS_RESPONSE)
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;
240 // Local function to put a different state for this resource
241 void putLightRepresentation(std::shared_ptr<OCResource> resource)
245 OCRepresentation rep;
247 std::cout << "Putting light representation..."<<std::endl;
249 mylight.m_state = true;
250 mylight.m_power = 15;
252 rep.setValue("state", mylight.m_state);
253 rep.setValue("power", mylight.m_power);
255 // Invoke resource's put API with rep, query map and the callback parameter
256 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::LowQos);
260 // Callback handler on GET request
261 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
263 if(eCode == SUCCESS_RESPONSE)
265 std::cout << "GET request was successful" << std::endl;
266 std::cout << "Resource URI: " << rep.getUri() << std::endl;
268 rep.getValue("state", mylight.m_state);
269 rep.getValue("power", mylight.m_power);
270 rep.getValue("name", mylight.m_name);
272 std::cout << "\tstate: " << mylight.m_state << std::endl;
273 std::cout << "\tpower: " << mylight.m_power << std::endl;
274 std::cout << "\tname: " << mylight.m_name << std::endl;
276 putLightRepresentation(curResource);
280 std::cout << "onGET Response error: " << eCode << std::endl;
285 // Local function to get representation of light resource
286 void getLightRepresentation(std::shared_ptr<OCResource> resource)
290 std::cout << "Getting Light Representation..."<<std::endl;
291 // Invoke resource's get API with the callback parameter
294 resource->get(test, &onGet,OC::QualityOfService::LowQos);
298 // Callback to found resources
299 void foundResource(std::shared_ptr<OCResource> resource)
301 std::string resourceURI;
302 std::string hostAddress;
305 if(discoveredResources.find(resource) == discoveredResources.end())
307 std::cout << "Found resource " << resource->uniqueIdentifier() <<
308 " for the first time on server with ID: "<< resource->sid()<<std::endl;
309 discoveredResources.insert(resource);
313 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
318 std::cout << "Found another resource, ignoring"<<std::endl;
322 // Do some operations with resource object.
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 API on the resource pointer
353 getLightRepresentation(resource);
358 // Resource is invalid
359 std::cout << "Resource is invalid" << std::endl;
363 catch(std::exception& e)
371 std::cout << std::endl;
373 std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
375 std::cout << "Usage : simpleclientHQ <ObserveType>" << std::endl;
377 std::cout << " ObserveType : 1 - Observe" << std::endl;
378 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
380 std::cout<<" ConnectivityType: Default WIFI" << std::endl;
381 std::cout << " ConnectivityType : 0 - ETHERNET"<< std::endl;
382 std::cout << " ConnectivityType : 1 - WIFI"<< std::endl;
386 int main(int argc, char* argv[]) {
388 ostringstream requestURI;
391 OCConnectivityType connectivityType = OC_WIFI;
397 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
405 int value = stoi(argv[1]);
407 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
409 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
411 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
416 std::size_t inputValLen;
417 int optionSelected = stoi(argv[2], &inputValLen);
419 if(inputValLen == strlen(argv[2]))
421 if(optionSelected == 0)
423 connectivityType = OC_ETHERNET;
425 else if(optionSelected == 1)
427 connectivityType = OC_WIFI;
431 std::cout << "Invalid connectivity type selected. Using default WIFI"
437 std::cout << "Invalid connectivity type selected. Using default WIFI"
451 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
455 // Create PlatformConfig object
457 OC::ServiceType::InProc,
458 OC::ModeType::Client,
461 OC::QualityOfService::LowQos
464 OCPlatform::Configure(cfg);
468 // Find all resources
469 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
472 OCPlatform::findResource("", requestURI.str(),
473 connectivityType, &foundResource, OC::QualityOfService::LowQos);
475 OCPlatform::findResource("", requestURI.str(), &foundResource,
476 OC::QualityOfService::LowQos);
478 std::cout<< "Finding Resource... " <<std::endl;
480 // Find resource is done twice so that we discover the original resources a second time.
481 // These resources will have the same uniqueidentifier (yet be different objects), so that
482 // we can verify/show the duplicate-checking code in foundResource(above);
484 OCPlatform::findResource("", requestURI.str(),
485 connectivityType, &foundResource, OC::QualityOfService::LowQos);
487 OCPlatform::findResource("", requestURI.str(), &foundResource,
488 OC::QualityOfService::LowQos);
490 std::cout<< "Finding Resource for second time... " <<std::endl;
496 // A condition variable will free the mutex it is given, then do a non-
497 // intensive block until 'notify' is called on it. In this case, since we
498 // don't ever call cv.notify, this should be a non-processor intensive version
501 std::condition_variable cv;
502 std::unique_lock<std::mutex> lock(blocker);
505 }catch(OCException& e)