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;
49 //TODO-CA: Since CA CON message support is still in progress, this client uses
50 // LowQos messages. This needs to change once Confirmable messages are supported in CA.
60 Light() : m_state(false), m_power(0), m_name("")
73 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
74 const int& eCode, const int& sequenceNumber)
76 if(eCode == SUCCESS_RESPONSE)
78 std::cout << "OBSERVE RESULT:"<<std::endl;
79 if(sequenceNumber == (int) ObserveAction::ObserveRegister)
81 std::cout << "\tObserve Registration Confirmed: "<< std::endl;
83 else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
85 std::cout << "\tObserve Cancel Confirmed: "<< std::endl;
87 std::cout << "DONE"<<std::endl;
92 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
95 rep.getValue("state", mylight.m_state);
96 rep.getValue("power", mylight.m_power);
97 rep.getValue("name", mylight.m_name);
99 std::cout << "\tstate: " << mylight.m_state << std::endl;
100 std::cout << "\tpower: " << mylight.m_power << std::endl;
101 std::cout << "\tname: " << mylight.m_name << std::endl;
103 if(observe_count() > 30)
105 std::cout<<"Cancelling Observe..."<<std::endl;
106 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::LowQos);
108 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
113 std::cout << "onObserve Response error: " << eCode << std::endl;
118 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
120 if(eCode == SUCCESS_RESPONSE)
122 std::cout << "POST request was successful" << std::endl;
124 if(rep.hasAttribute("createduri"))
126 std::cout << "\tUri of the created resource: "
127 << rep.getValue<std::string>("createduri") << std::endl;
131 rep.getValue("state", mylight.m_state);
132 rep.getValue("power", mylight.m_power);
133 rep.getValue("name", mylight.m_name);
135 std::cout << "\tstate: " << mylight.m_state << std::endl;
136 std::cout << "\tpower: " << mylight.m_power << std::endl;
137 std::cout << "\tname: " << mylight.m_name << std::endl;
140 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
141 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
142 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
143 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
145 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
146 OC::QualityOfService::LowQos);
151 std::cout << "onPost2 Response error: " << eCode << std::endl;
156 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
158 if(eCode == SUCCESS_RESPONSE)
160 std::cout << "POST request was successful" << std::endl;
162 if(rep.hasAttribute("createduri"))
164 std::cout << "\tUri of the created resource: "
165 << rep.getValue<std::string>("createduri") << std::endl;
169 rep.getValue("state", mylight.m_state);
170 rep.getValue("power", mylight.m_power);
171 rep.getValue("name", mylight.m_name);
173 std::cout << "\tstate: " << mylight.m_state << std::endl;
174 std::cout << "\tpower: " << mylight.m_power << std::endl;
175 std::cout << "\tname: " << mylight.m_name << std::endl;
178 OCRepresentation rep2;
180 std::cout << "Posting light representation..."<<std::endl;
182 mylight.m_state = true;
183 mylight.m_power = 55;
185 rep2.setValue("state", mylight.m_state);
186 rep2.setValue("power", mylight.m_power);
188 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::LowQos);
192 std::cout << "onPost Response error: " << eCode << std::endl;
197 // Local function to put a different state for this resource
198 void postLightRepresentation(std::shared_ptr<OCResource> resource)
202 OCRepresentation rep;
204 std::cout << "Posting light representation..."<<std::endl;
206 mylight.m_state = false;
207 mylight.m_power = 105;
209 rep.setValue("state", mylight.m_state);
210 rep.setValue("power", mylight.m_power);
212 // Invoke resource's post API with rep, query map and the callback parameter
213 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
217 // callback handler on PUT request
218 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
220 if(eCode == SUCCESS_RESPONSE)
222 std::cout << "PUT request was successful" << std::endl;
224 rep.getValue("state", mylight.m_state);
225 rep.getValue("power", mylight.m_power);
226 rep.getValue("name", mylight.m_name);
228 std::cout << "\tstate: " << mylight.m_state << std::endl;
229 std::cout << "\tpower: " << mylight.m_power << std::endl;
230 std::cout << "\tname: " << mylight.m_name << std::endl;
232 postLightRepresentation(curResource);
236 std::cout << "onPut Response error: " << eCode << std::endl;
241 // Local function to put a different state for this resource
242 void putLightRepresentation(std::shared_ptr<OCResource> resource)
246 OCRepresentation rep;
248 std::cout << "Putting light representation..."<<std::endl;
250 mylight.m_state = true;
251 mylight.m_power = 15;
253 rep.setValue("state", mylight.m_state);
254 rep.setValue("power", mylight.m_power);
256 // Invoke resource's put API with rep, query map and the callback parameter
257 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::LowQos);
261 // Callback handler on GET request
262 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
264 if(eCode == SUCCESS_RESPONSE)
266 std::cout << "GET request was successful" << std::endl;
267 std::cout << "Resource URI: " << rep.getUri() << std::endl;
269 rep.getValue("state", mylight.m_state);
270 rep.getValue("power", mylight.m_power);
271 rep.getValue("name", mylight.m_name);
273 std::cout << "\tstate: " << mylight.m_state << std::endl;
274 std::cout << "\tpower: " << mylight.m_power << std::endl;
275 std::cout << "\tname: " << mylight.m_name << std::endl;
277 putLightRepresentation(curResource);
281 std::cout << "onGET Response error: " << eCode << std::endl;
286 // Local function to get representation of light resource
287 void getLightRepresentation(std::shared_ptr<OCResource> resource)
291 std::cout << "Getting Light Representation..."<<std::endl;
292 // Invoke resource's get API with the callback parameter
295 resource->get(test, &onGet,OC::QualityOfService::LowQos);
299 // Callback to found resources
300 void foundResource(std::shared_ptr<OCResource> resource)
302 std::string resourceURI;
303 std::string hostAddress;
306 // Do some operations with resource object.
309 std::lock_guard<std::mutex> lk(resourceLock);
311 if(discoveredResources.find(resource) == discoveredResources.end())
313 std::cout << "Found resource " << resource->uniqueIdentifier() <<
314 " for the first time on server with ID: "<< resource->sid()<<std::endl;
315 discoveredResources.insert(resource);
319 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
324 std::cout << "Found another resource, ignoring"<<std::endl;
328 std::cout<<"DISCOVERED Resource:"<<std::endl;
329 // Get the resource URI
330 resourceURI = resource->uri();
331 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
333 // Get the resource host address
334 hostAddress = resource->host();
335 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
337 // Get the resource types
338 std::cout << "\tList of resource types: " << std::endl;
339 for(auto &resourceTypes : resource->getResourceTypes())
341 std::cout << "\t\t" << resourceTypes << std::endl;
344 // Get the resource interfaces
345 std::cout << "\tList of resource interfaces: " << std::endl;
346 for(auto &resourceInterfaces : resource->getResourceInterfaces())
348 std::cout << "\t\t" << resourceInterfaces << std::endl;
351 if(resourceURI == "/a/light")
353 curResource = resource;
355 // Call a local function which will internally invoke get
356 // API on the resource pointer
357 getLightRepresentation(resource);
362 // Resource is invalid
363 std::cout << "Resource is invalid" << std::endl;
367 catch(std::exception& e)
375 std::cout << std::endl;
376 std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
377 std::cout << " ObserveType : 1 - Observe" << std::endl;
378 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
379 std::cout<<" ConnectivityType: Default WIFI" << std::endl;
380 std::cout << " ConnectivityType : 0 - ETHERNET"<< std::endl;
381 std::cout << " ConnectivityType : 1 - WIFI"<< std::endl;
384 int main(int argc, char* argv[]) {
386 std::ostringstream requestURI;
388 OCConnectivityType connectivityType = OC_WIFI;
393 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
395 else if (argc ==2 || argc==3)
397 int value = std::stoi(argv[1]);
399 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
401 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
403 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
407 std::size_t inputValLen;
408 int optionSelected = std::stoi(argv[2], &inputValLen);
410 if(inputValLen == strlen(argv[2]))
412 if(optionSelected == 0)
414 connectivityType = OC_ETHERNET;
416 else if(optionSelected == 1)
418 connectivityType = OC_WIFI;
422 std::cout << "Invalid connectivity type selected. Using default WIFI"
428 std::cout << "Invalid connectivity type selected. Using default WIFI"
439 catch(std::exception& e)
441 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
445 // Create PlatformConfig object
447 OC::ServiceType::InProc,
448 OC::ModeType::Client,
451 OC::QualityOfService::LowQos
454 OCPlatform::Configure(cfg);
458 // Find all resources
459 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
461 OCPlatform::findResource("", requestURI.str(),
462 connectivityType, &foundResource, OC::QualityOfService::LowQos);
463 std::cout<< "Finding Resource... " <<std::endl;
465 // Find resource is done twice so that we discover the original resources a second time.
466 // These resources will have the same uniqueidentifier (yet be different objects), so that
467 // we can verify/show the duplicate-checking code in foundResource(above);
468 OCPlatform::findResource("", requestURI.str(),
469 connectivityType, &foundResource, OC::QualityOfService::LowQos);
470 std::cout<< "Finding Resource for second time... " <<std::endl;
472 // A condition variable will free the mutex it is given, then do a non-
473 // intensive block until 'notify' is called on it. In this case, since we
474 // don't ever call cv.notify, this should be a non-processor intensive version
477 std::condition_variable cv;
478 std::unique_lock<std::mutex> lock(blocker);
482 catch(OCException& e)
484 oclog() << "Exception in main: "<<e.what();