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.
23 #include "iotivity_config.h"
29 #include <condition_variable>
30 #include "OCPlatform.h"
33 #if defined(HAVE_PTHREAD_H)
36 #if defined(HAVE_WINDOWS_H)
42 struct dereference_compare
44 bool operator()(std::shared_ptr<OCResource> lhs, std::shared_ptr<OCResource> rhs )const
49 typedef std::set<std::shared_ptr<OCResource>, dereference_compare> DiscoveredResourceSet;
51 DiscoveredResourceSet discoveredResources;
52 const int SUCCESS_RESPONSE = 0;
53 std::shared_ptr<OCResource> curResource;
54 std::mutex resourceLock;
55 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
65 Light() : m_state(false), m_power(0), m_name("")
78 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
79 const int& eCode, const int& sequenceNumber)
81 if(eCode == SUCCESS_RESPONSE)
83 std::cout << "OBSERVE RESULT:"<<std::endl;
84 if(sequenceNumber == (int) ObserveAction::ObserveRegister)
86 std::cout << "\tObserve Registration Confirmed: "<< std::endl;
88 else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
90 std::cout << "\tObserve Cancel Confirmed: "<< std::endl;
92 std::cout << "DONE"<<std::endl;
97 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
100 rep.getValue("state", mylight.m_state);
101 rep.getValue("power", mylight.m_power);
102 rep.getValue("name", mylight.m_name);
104 std::cout << "\tstate: " << mylight.m_state << std::endl;
105 std::cout << "\tpower: " << mylight.m_power << std::endl;
106 std::cout << "\tname: " << mylight.m_name << std::endl;
108 if(observe_count() == 11)
110 std::cout<<"Cancelling Observe..."<<std::endl;
111 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
113 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
118 std::cout << "onObserve Response error: " << eCode << std::endl;
123 void onPost2(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
125 if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
127 std::cout << "POST request was successful" << std::endl;
129 if(rep.hasAttribute("createduri"))
131 std::cout << "\tUri of the created resource: "
132 << rep.getValue<std::string>("createduri") << std::endl;
136 rep.getValue("state", mylight.m_state);
137 rep.getValue("power", mylight.m_power);
138 rep.getValue("name", mylight.m_name);
140 std::cout << "\tstate: " << mylight.m_state << std::endl;
141 std::cout << "\tpower: " << mylight.m_power << std::endl;
142 std::cout << "\tname: " << mylight.m_name << std::endl;
145 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
146 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
147 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
148 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
150 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
151 OC::QualityOfService::HighQos);
156 std::cout << "onPost2 Response error: " << eCode << std::endl;
161 void onPost(const HeaderOptions& /*headerOptions*/,
162 const OCRepresentation& rep, const int eCode)
164 if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
166 std::cout << "POST request was successful" << std::endl;
168 if(rep.hasAttribute("createduri"))
170 std::cout << "\tUri of the created resource: "
171 << rep.getValue<std::string>("createduri") << std::endl;
175 rep.getValue("state", mylight.m_state);
176 rep.getValue("power", mylight.m_power);
177 rep.getValue("name", mylight.m_name);
179 std::cout << "\tstate: " << mylight.m_state << std::endl;
180 std::cout << "\tpower: " << mylight.m_power << std::endl;
181 std::cout << "\tname: " << mylight.m_name << std::endl;
184 OCRepresentation rep2;
186 std::cout << "Posting light representation..."<<std::endl;
188 mylight.m_state = true;
189 mylight.m_power = 55;
191 rep2.setValue("state", mylight.m_state);
192 rep2.setValue("power", mylight.m_power);
194 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
198 std::cout << "onPost Response error: " << eCode << std::endl;
203 // Local function to put a different state for this resource
204 void postLightRepresentation(std::shared_ptr<OCResource> resource)
208 OCRepresentation rep;
210 std::cout << "Posting light representation..."<<std::endl;
212 mylight.m_state = false;
213 mylight.m_power = 105;
215 rep.setValue("state", mylight.m_state);
216 rep.setValue("power", mylight.m_power);
218 // Invoke resource's post API with rep, query map and the callback parameter
219 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
223 // callback handler on PUT request
224 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
226 if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
228 std::cout << "PUT request was successful" << std::endl;
230 rep.getValue("state", mylight.m_state);
231 rep.getValue("power", mylight.m_power);
232 rep.getValue("name", mylight.m_name);
234 std::cout << "\tstate: " << mylight.m_state << std::endl;
235 std::cout << "\tpower: " << mylight.m_power << std::endl;
236 std::cout << "\tname: " << mylight.m_name << std::endl;
238 postLightRepresentation(curResource);
242 std::cout << "onPut Response error: " << eCode << std::endl;
247 // Local function to put a different state for this resource
248 void putLightRepresentation(std::shared_ptr<OCResource> resource)
252 OCRepresentation rep;
254 std::cout << "Putting light representation..."<<std::endl;
256 mylight.m_state = true;
257 mylight.m_power = 15;
259 rep.setValue("state", mylight.m_state);
260 rep.setValue("power", mylight.m_power);
262 // Invoke resource's put API with rep, query map and the callback parameter
263 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
267 // Callback handler on GET request
268 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
270 if(eCode == SUCCESS_RESPONSE)
272 std::cout << "GET request was successful" << std::endl;
273 std::cout << "Resource URI: " << rep.getUri() << std::endl;
275 rep.getValue("state", mylight.m_state);
276 rep.getValue("power", mylight.m_power);
277 rep.getValue("name", mylight.m_name);
279 std::cout << "\tstate: " << mylight.m_state << std::endl;
280 std::cout << "\tpower: " << mylight.m_power << std::endl;
281 std::cout << "\tname: " << mylight.m_name << std::endl;
283 putLightRepresentation(curResource);
287 std::cout << "onGET Response error: " << eCode << std::endl;
292 // Local function to get representation of light resource
293 void getLightRepresentation(std::shared_ptr<OCResource> resource)
297 std::cout << "Getting Light Representation..."<<std::endl;
298 // Invoke resource's get API with the callback parameter
301 resource->get(test, &onGet,OC::QualityOfService::HighQos);
305 // Callback to found resources
306 void foundResource(std::shared_ptr<OCResource> resource)
308 std::string resourceURI;
309 std::string hostAddress;
312 // Do some operations with resource object.
315 std::lock_guard<std::mutex> lk(resourceLock);
317 if(discoveredResources.find(resource) == discoveredResources.end())
319 std::cout << "Found resource " << resource->uniqueIdentifier() <<
320 " for the first time on server with ID: "<< resource->sid()<<std::endl;
321 discoveredResources.insert(resource);
325 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
330 std::cout << "Found another resource, ignoring"<<std::endl;
334 std::cout<<"DISCOVERED Resource:"<<std::endl;
335 // Get the resource URI
336 resourceURI = resource->uri();
337 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
339 // Get the resource host address
340 hostAddress = resource->host();
341 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
343 // Get the resource types
344 std::cout << "\tList of resource types: " << std::endl;
345 for(auto &resourceTypes : resource->getResourceTypes())
347 std::cout << "\t\t" << resourceTypes << std::endl;
350 // Get the resource interfaces
351 std::cout << "\tList of resource interfaces: " << std::endl;
352 for(auto &resourceInterfaces : resource->getResourceInterfaces())
354 std::cout << "\t\t" << resourceInterfaces << std::endl;
357 if(resourceURI == "/a/light")
359 curResource = resource;
361 // Call a local function which will internally invoke get
362 // API on the resource pointer
363 getLightRepresentation(resource);
368 // Resource is invalid
369 std::cout << "Resource is invalid" << std::endl;
373 catch(std::exception& e)
375 std::cerr << "Exception in foundResource: "<< e.what() <<std::endl;
381 std::cout << std::endl;
382 std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
383 std::cout << " ObserveType : 1 - Observe" << std::endl;
384 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
385 std::cout << " ConnectivityType: Default IP" << std::endl;
386 std::cout << " ConnectivityType : 0 - IP"<< std::endl;
389 int main(int argc, char* argv[]) {
391 std::ostringstream requestURI;
393 OCConnectivityType connectivityType = CT_ADAPTER_IP;
398 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
400 else if (argc ==2 || argc==3)
402 int value = std::stoi(argv[1]);
404 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
406 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
408 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
412 std::size_t inputValLen;
413 int optionSelected = std::stoi(argv[2], &inputValLen);
415 if(inputValLen == strlen(argv[2]))
417 if(optionSelected == 0)
419 std::cout << "Using IP."<< std::endl;
420 connectivityType = CT_ADAPTER_IP;
424 std::cout << "Invalid connectivity type selected. Using default IP"
430 std::cout << "Invalid connectivity type selected. Using default IP"
441 catch(std::exception&)
443 std::cout << "Invalid input argument." << std::endl;
449 // Create PlatformConfig object
451 OC::ServiceType::InProc,
452 OC::ModeType::Client,
455 OC::QualityOfService::HighQos
458 OCPlatform::Configure(cfg);
462 // Find all resources
463 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
465 OCPlatform::findResource("", requestURI.str(),
466 connectivityType, &foundResource, OC::QualityOfService::LowQos);
467 std::cout<< "Finding Resource... " <<std::endl;
469 // Find resource is done twice so that we discover the original resources a second time.
470 // These resources will have the same uniqueidentifier (yet be different objects), so that
471 // we can verify/show the duplicate-checking code in foundResource(above);
472 OCPlatform::findResource("", requestURI.str(),
473 connectivityType, &foundResource, OC::QualityOfService::LowQos);
474 std::cout<< "Finding Resource for second time... " <<std::endl;
476 // A condition variable will free the mutex it is given, then do a non-
477 // intensive block until 'notify' is called on it. In this case, since we
478 // don't ever call cv.notify, this should be a non-processor intensive version
481 std::condition_variable cv;
482 std::unique_lock<std::mutex> lock(blocker);
486 catch(OCException& e)
488 oclog() << "Exception in main: "<<e.what();