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.
30 #include <condition_variable>
31 #include "OCPlatform.h"
34 #if defined(HAVE_WINDOWS_H)
37 #include "platform_features.h"
41 struct dereference_compare
43 bool operator()(std::shared_ptr<OCResource> lhs, std::shared_ptr<OCResource> rhs )const
48 typedef std::set<std::shared_ptr<OCResource>, dereference_compare> DiscoveredResourceSet;
50 DiscoveredResourceSet discoveredResources;
51 const int SUCCESS_RESPONSE = 0;
52 std::shared_ptr<OCResource> curResource;
53 std::mutex resourceLock;
54 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
64 Light() : m_state(false), m_power(0), m_name("")
77 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
78 const int& eCode, const int& sequenceNumber)
80 if(eCode == SUCCESS_RESPONSE)
82 std::cout << "OBSERVE RESULT:"<<std::endl;
83 if(sequenceNumber == (int) ObserveAction::ObserveRegister)
85 std::cout << "\tObserve Registration Confirmed: "<< std::endl;
87 else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
89 std::cout << "\tObserve Cancel Confirmed: "<< std::endl;
91 std::cout << "DONE"<<std::endl;
96 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
99 rep.getValue("state", mylight.m_state);
100 rep.getValue("power", mylight.m_power);
101 rep.getValue("name", mylight.m_name);
103 std::cout << "\tstate: " << mylight.m_state << std::endl;
104 std::cout << "\tpower: " << mylight.m_power << std::endl;
105 std::cout << "\tname: " << mylight.m_name << std::endl;
107 if(observe_count() == 11)
109 std::cout<<"Cancelling Observe..."<<std::endl;
110 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
112 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
117 std::cout << "onObserve Response error: " << eCode << std::endl;
122 void onPost2(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
124 if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
126 std::cout << "POST request was successful" << std::endl;
128 if(rep.hasAttribute("createduri"))
130 std::cout << "\tUri of the created resource: "
131 << rep.getValue<std::string>("createduri") << std::endl;
135 rep.getValue("state", mylight.m_state);
136 rep.getValue("power", mylight.m_power);
137 rep.getValue("name", mylight.m_name);
139 std::cout << "\tstate: " << mylight.m_state << std::endl;
140 std::cout << "\tpower: " << mylight.m_power << std::endl;
141 std::cout << "\tname: " << mylight.m_name << std::endl;
144 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
145 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
146 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
147 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
149 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
150 OC::QualityOfService::HighQos);
155 std::cout << "onPost2 Response error: " << eCode << std::endl;
160 void onPost(const HeaderOptions& /*headerOptions*/,
161 const OCRepresentation& rep, const int eCode)
163 if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
165 std::cout << "POST request was successful" << std::endl;
167 if(rep.hasAttribute("createduri"))
169 std::cout << "\tUri of the created resource: "
170 << rep.getValue<std::string>("createduri") << std::endl;
174 rep.getValue("state", mylight.m_state);
175 rep.getValue("power", mylight.m_power);
176 rep.getValue("name", mylight.m_name);
178 std::cout << "\tstate: " << mylight.m_state << std::endl;
179 std::cout << "\tpower: " << mylight.m_power << std::endl;
180 std::cout << "\tname: " << mylight.m_name << std::endl;
183 OCRepresentation rep2;
185 std::cout << "Posting light representation..."<<std::endl;
187 mylight.m_state = true;
188 mylight.m_power = 55;
190 rep2.setValue("state", mylight.m_state);
191 rep2.setValue("power", mylight.m_power);
193 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
197 std::cout << "onPost Response error: " << eCode << std::endl;
202 // Local function to put a different state for this resource
203 void postLightRepresentation(std::shared_ptr<OCResource> resource)
207 OCRepresentation rep;
209 std::cout << "Posting light representation..."<<std::endl;
211 mylight.m_state = false;
212 mylight.m_power = 105;
214 rep.setValue("state", mylight.m_state);
215 rep.setValue("power", mylight.m_power);
217 // Invoke resource's post API with rep, query map and the callback parameter
218 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
222 // callback handler on PUT request
223 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
225 if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
227 std::cout << "PUT request was successful" << std::endl;
229 rep.getValue("state", mylight.m_state);
230 rep.getValue("power", mylight.m_power);
231 rep.getValue("name", mylight.m_name);
233 std::cout << "\tstate: " << mylight.m_state << std::endl;
234 std::cout << "\tpower: " << mylight.m_power << std::endl;
235 std::cout << "\tname: " << mylight.m_name << std::endl;
237 postLightRepresentation(curResource);
241 std::cout << "onPut Response error: " << eCode << std::endl;
246 // Local function to put a different state for this resource
247 void putLightRepresentation(std::shared_ptr<OCResource> resource)
251 OCRepresentation rep;
253 std::cout << "Putting light representation..."<<std::endl;
255 mylight.m_state = true;
256 mylight.m_power = 15;
258 rep.setValue("state", mylight.m_state);
259 rep.setValue("power", mylight.m_power);
261 // Invoke resource's put API with rep, query map and the callback parameter
262 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
266 // Callback handler on GET request
267 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
269 if(eCode == SUCCESS_RESPONSE)
271 std::cout << "GET request was successful" << std::endl;
272 std::cout << "Resource URI: " << rep.getUri() << std::endl;
274 rep.getValue("state", mylight.m_state);
275 rep.getValue("power", mylight.m_power);
276 rep.getValue("name", mylight.m_name);
278 std::cout << "\tstate: " << mylight.m_state << std::endl;
279 std::cout << "\tpower: " << mylight.m_power << std::endl;
280 std::cout << "\tname: " << mylight.m_name << std::endl;
282 putLightRepresentation(curResource);
286 std::cout << "onGET Response error: " << eCode << std::endl;
291 // Local function to get representation of light resource
292 void getLightRepresentation(std::shared_ptr<OCResource> resource)
296 std::cout << "Getting Light Representation..."<<std::endl;
297 // Invoke resource's get API with the callback parameter
300 resource->get(test, &onGet,OC::QualityOfService::HighQos);
304 // Callback to found resources
305 void foundResource(std::shared_ptr<OCResource> resource)
307 std::string resourceURI;
308 std::string hostAddress;
311 // Do some operations with resource object.
314 std::lock_guard<std::mutex> lk(resourceLock);
316 if(discoveredResources.find(resource) == discoveredResources.end())
318 std::cout << "Found resource " << resource->uniqueIdentifier() <<
319 " for the first time on server with ID: "<< resource->sid()<<std::endl;
320 discoveredResources.insert(resource);
324 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
329 std::cout << "Found another resource, ignoring"<<std::endl;
333 std::cout<<"DISCOVERED Resource:"<<std::endl;
334 // Get the resource URI
335 resourceURI = resource->uri();
336 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
338 // Get the resource host address
339 hostAddress = resource->host();
340 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
342 // Get the resource types
343 std::cout << "\tList of resource types: " << std::endl;
344 for(auto &resourceTypes : resource->getResourceTypes())
346 std::cout << "\t\t" << resourceTypes << std::endl;
349 // Get the resource interfaces
350 std::cout << "\tList of resource interfaces: " << std::endl;
351 for(auto &resourceInterfaces : resource->getResourceInterfaces())
353 std::cout << "\t\t" << resourceInterfaces << std::endl;
356 if(resourceURI == "/a/light")
358 curResource = resource;
360 // Call a local function which will internally invoke get
361 // API on the resource pointer
362 getLightRepresentation(resource);
367 // Resource is invalid
368 std::cout << "Resource is invalid" << std::endl;
372 catch(std::exception& e)
374 std::cerr << "Exception in foundResource: "<< e.what() <<std::endl;
380 std::cout << std::endl;
381 std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
382 std::cout << " ObserveType : 1 - Observe" << std::endl;
383 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
384 std::cout << " ConnectivityType: Default IP" << std::endl;
385 std::cout << " ConnectivityType : 0 - IP"<< std::endl;
388 int main(int argc, char* argv[]) {
390 std::ostringstream requestURI;
392 OCConnectivityType connectivityType = CT_ADAPTER_IP;
397 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
399 else if (argc ==2 || argc==3)
401 int value = std::stoi(argv[1]);
403 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
405 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
407 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
411 std::size_t inputValLen;
412 int optionSelected = std::stoi(argv[2], &inputValLen);
414 if(inputValLen == strlen(argv[2]))
416 if(optionSelected == 0)
418 std::cout << "Using IP."<< std::endl;
419 connectivityType = CT_ADAPTER_IP;
423 std::cout << "Invalid connectivity type selected. Using default IP"
429 std::cout << "Invalid connectivity type selected. Using default IP"
440 catch(std::exception&)
442 std::cout << "Invalid input argument." << std::endl;
448 // Create PlatformConfig object
450 OC::ServiceType::InProc,
451 OC::ModeType::Client,
454 OC::QualityOfService::HighQos
457 OCPlatform::Configure(cfg);
461 // Find all resources
462 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
464 OCPlatform::findResource("", requestURI.str(),
465 connectivityType, &foundResource, OC::QualityOfService::LowQos);
466 std::cout<< "Finding Resource... " <<std::endl;
468 // Find resource is done twice so that we discover the original resources a second time.
469 // These resources will have the same uniqueidentifier (yet be different objects), so that
470 // we can verify/show the duplicate-checking code in foundResource(above);
471 OCPlatform::findResource("", requestURI.str(),
472 connectivityType, &foundResource, OC::QualityOfService::LowQos);
473 std::cout<< "Finding Resource for second time... " <<std::endl;
475 // A condition variable will free the mutex it is given, then do a non-
476 // intensive block until 'notify' is called on it. In this case, since we
477 // don't ever call cv.notify, this should be a non-processor intensive version
480 std::condition_variable cv;
481 std::unique_lock<std::mutex> lock(blocker);
485 catch(OCException& e)
487 oclog() << "Exception in main: "<<e.what();