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() == 11)
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 || eCode == OC_STACK_RESOURCE_CHANGED)
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*/,
154 const OCRepresentation& rep, const int eCode)
156 if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
158 std::cout << "POST request was successful" << std::endl;
160 if(rep.hasAttribute("createduri"))
162 std::cout << "\tUri of the created resource: "
163 << rep.getValue<std::string>("createduri") << std::endl;
167 rep.getValue("state", mylight.m_state);
168 rep.getValue("power", mylight.m_power);
169 rep.getValue("name", mylight.m_name);
171 std::cout << "\tstate: " << mylight.m_state << std::endl;
172 std::cout << "\tpower: " << mylight.m_power << std::endl;
173 std::cout << "\tname: " << mylight.m_name << std::endl;
176 OCRepresentation rep2;
178 std::cout << "Posting light representation..."<<std::endl;
180 mylight.m_state = true;
181 mylight.m_power = 55;
183 rep2.setValue("state", mylight.m_state);
184 rep2.setValue("power", mylight.m_power);
186 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
190 std::cout << "onPost Response error: " << eCode << std::endl;
195 // Local function to put a different state for this resource
196 void postLightRepresentation(std::shared_ptr<OCResource> resource)
200 OCRepresentation rep;
202 std::cout << "Posting light representation..."<<std::endl;
204 mylight.m_state = false;
205 mylight.m_power = 105;
207 rep.setValue("state", mylight.m_state);
208 rep.setValue("power", mylight.m_power);
210 // Invoke resource's post API with rep, query map and the callback parameter
211 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
215 // callback handler on PUT request
216 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
218 if(eCode == SUCCESS_RESPONSE)
220 std::cout << "PUT request was successful" << std::endl;
222 rep.getValue("state", mylight.m_state);
223 rep.getValue("power", mylight.m_power);
224 rep.getValue("name", mylight.m_name);
226 std::cout << "\tstate: " << mylight.m_state << std::endl;
227 std::cout << "\tpower: " << mylight.m_power << std::endl;
228 std::cout << "\tname: " << mylight.m_name << std::endl;
230 postLightRepresentation(curResource);
234 std::cout << "onPut Response error: " << eCode << std::endl;
239 // Local function to put a different state for this resource
240 void putLightRepresentation(std::shared_ptr<OCResource> resource)
244 OCRepresentation rep;
246 std::cout << "Putting light representation..."<<std::endl;
248 mylight.m_state = true;
249 mylight.m_power = 15;
251 rep.setValue("state", mylight.m_state);
252 rep.setValue("power", mylight.m_power);
254 // Invoke resource's put API with rep, query map and the callback parameter
255 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
259 // Callback handler on GET request
260 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
262 if(eCode == SUCCESS_RESPONSE)
264 std::cout << "GET request was successful" << std::endl;
265 std::cout << "Resource URI: " << rep.getUri() << std::endl;
267 rep.getValue("state", mylight.m_state);
268 rep.getValue("power", mylight.m_power);
269 rep.getValue("name", mylight.m_name);
271 std::cout << "\tstate: " << mylight.m_state << std::endl;
272 std::cout << "\tpower: " << mylight.m_power << std::endl;
273 std::cout << "\tname: " << mylight.m_name << std::endl;
275 putLightRepresentation(curResource);
279 std::cout << "onGET Response error: " << eCode << std::endl;
284 // Local function to get representation of light resource
285 void getLightRepresentation(std::shared_ptr<OCResource> resource)
289 std::cout << "Getting Light Representation..."<<std::endl;
290 // Invoke resource's get API with the callback parameter
293 resource->get(test, &onGet,OC::QualityOfService::HighQos);
297 // Callback to found resources
298 void foundResource(std::shared_ptr<OCResource> resource)
300 std::string resourceURI;
301 std::string hostAddress;
304 // Do some operations with resource object.
307 std::lock_guard<std::mutex> lk(resourceLock);
309 if(discoveredResources.find(resource) == discoveredResources.end())
311 std::cout << "Found resource " << resource->uniqueIdentifier() <<
312 " for the first time on server with ID: "<< resource->sid()<<std::endl;
313 discoveredResources.insert(resource);
317 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
322 std::cout << "Found another resource, ignoring"<<std::endl;
326 std::cout<<"DISCOVERED Resource:"<<std::endl;
327 // Get the resource URI
328 resourceURI = resource->uri();
329 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
331 // Get the resource host address
332 hostAddress = resource->host();
333 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
335 // Get the resource types
336 std::cout << "\tList of resource types: " << std::endl;
337 for(auto &resourceTypes : resource->getResourceTypes())
339 std::cout << "\t\t" << resourceTypes << std::endl;
342 // Get the resource interfaces
343 std::cout << "\tList of resource interfaces: " << std::endl;
344 for(auto &resourceInterfaces : resource->getResourceInterfaces())
346 std::cout << "\t\t" << resourceInterfaces << std::endl;
349 if(resourceURI == "/a/light")
351 curResource = resource;
353 // Call a local function which will internally invoke get
354 // API on the resource pointer
355 getLightRepresentation(resource);
360 // Resource is invalid
361 std::cout << "Resource is invalid" << std::endl;
365 catch(std::exception& e)
367 std::cerr << "Exception in foundResource: "<< e.what() <<std::endl;
373 std::cout << std::endl;
374 std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
375 std::cout << " ObserveType : 1 - Observe" << std::endl;
376 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
377 std::cout << " ConnectivityType: Default IP" << std::endl;
378 std::cout << " ConnectivityType : 0 - IP"<< std::endl;
381 int main(int argc, char* argv[]) {
383 std::ostringstream requestURI;
385 OCConnectivityType connectivityType = CT_ADAPTER_IP;
390 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
392 else if (argc ==2 || argc==3)
394 int value = std::stoi(argv[1]);
396 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
398 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
400 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
404 std::size_t inputValLen;
405 int optionSelected = std::stoi(argv[2], &inputValLen);
407 if(inputValLen == strlen(argv[2]))
409 if(optionSelected == 0)
411 std::cout << "Using IP."<< std::endl;
412 connectivityType = CT_ADAPTER_IP;
416 std::cout << "Invalid connectivity type selected. Using default IP"
422 std::cout << "Invalid connectivity type selected. Using default IP"
433 catch(std::exception&)
435 std::cout << "Invalid input argument." << std::endl;
441 // Create PlatformConfig object
443 OC::ServiceType::InProc,
444 OC::ModeType::Client,
447 OC::QualityOfService::HighQos
450 OCPlatform::Configure(cfg);
454 // Find all resources
455 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
457 OCPlatform::findResource("", requestURI.str(),
458 connectivityType, &foundResource, OC::QualityOfService::LowQos);
459 std::cout<< "Finding Resource... " <<std::endl;
461 // Find resource is done twice so that we discover the original resources a second time.
462 // These resources will have the same uniqueidentifier (yet be different objects), so that
463 // we can verify/show the duplicate-checking code in foundResource(above);
464 OCPlatform::findResource("", requestURI.str(),
465 connectivityType, &foundResource, OC::QualityOfService::LowQos);
466 std::cout<< "Finding Resource for second time... " <<std::endl;
468 // A condition variable will free the mutex it is given, then do a non-
469 // intensive block until 'notify' is called on it. In this case, since we
470 // don't ever call cv.notify, this should be a non-processor intensive version
473 std::condition_variable cv;
474 std::unique_lock<std::mutex> lock(blocker);
478 catch(OCException& e)
480 oclog() << "Exception in main: "<<e.what();