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"
32 #if defined(__msys_nt__)
34 #define sleep(x) Sleep(1000*x)
39 struct dereference_compare
41 bool operator()(std::shared_ptr<OCResource> lhs, std::shared_ptr<OCResource> rhs )const
46 typedef std::set<std::shared_ptr<OCResource>, dereference_compare> DiscoveredResourceSet;
48 DiscoveredResourceSet discoveredResources;
49 const int SUCCESS_RESPONSE = 0;
50 std::shared_ptr<OCResource> curResource;
51 std::mutex resourceLock;
52 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
62 Light() : m_state(false), m_power(0), m_name("")
75 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
76 const int& eCode, const int& sequenceNumber)
78 if(eCode == SUCCESS_RESPONSE)
80 std::cout << "OBSERVE RESULT:"<<std::endl;
81 if(sequenceNumber == (int) ObserveAction::ObserveRegister)
83 std::cout << "\tObserve Registration Confirmed: "<< std::endl;
85 else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
87 std::cout << "\tObserve Cancel Confirmed: "<< std::endl;
89 std::cout << "DONE"<<std::endl;
94 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
97 rep.getValue("state", mylight.m_state);
98 rep.getValue("power", mylight.m_power);
99 rep.getValue("name", mylight.m_name);
101 std::cout << "\tstate: " << mylight.m_state << std::endl;
102 std::cout << "\tpower: " << mylight.m_power << std::endl;
103 std::cout << "\tname: " << mylight.m_name << std::endl;
105 if(observe_count() == 11)
107 std::cout<<"Cancelling Observe..."<<std::endl;
108 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
110 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
115 std::cout << "onObserve Response error: " << eCode << std::endl;
120 void onPost2(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
122 if(eCode == SUCCESS_RESPONSE)
124 std::cout << "POST request was successful" << std::endl;
126 if(rep.hasAttribute("createduri"))
128 std::cout << "\tUri of the created resource: "
129 << rep.getValue<std::string>("createduri") << std::endl;
133 rep.getValue("state", mylight.m_state);
134 rep.getValue("power", mylight.m_power);
135 rep.getValue("name", mylight.m_name);
137 std::cout << "\tstate: " << mylight.m_state << std::endl;
138 std::cout << "\tpower: " << mylight.m_power << std::endl;
139 std::cout << "\tname: " << mylight.m_name << std::endl;
142 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
143 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
144 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
145 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
147 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
148 OC::QualityOfService::HighQos);
153 std::cout << "onPost2 Response error: " << eCode << std::endl;
158 void onPost(const HeaderOptions& /*headerOptions*/,
159 const OCRepresentation& rep, const int eCode)
161 if(eCode == SUCCESS_RESPONSE)
163 std::cout << "POST request was successful" << std::endl;
165 if(rep.hasAttribute("createduri"))
167 std::cout << "\tUri of the created resource: "
168 << rep.getValue<std::string>("createduri") << std::endl;
172 rep.getValue("state", mylight.m_state);
173 rep.getValue("power", mylight.m_power);
174 rep.getValue("name", mylight.m_name);
176 std::cout << "\tstate: " << mylight.m_state << std::endl;
177 std::cout << "\tpower: " << mylight.m_power << std::endl;
178 std::cout << "\tname: " << mylight.m_name << std::endl;
181 OCRepresentation rep2;
183 std::cout << "Posting light representation..."<<std::endl;
185 mylight.m_state = true;
186 mylight.m_power = 55;
188 rep2.setValue("state", mylight.m_state);
189 rep2.setValue("power", mylight.m_power);
191 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
195 std::cout << "onPost Response error: " << eCode << std::endl;
200 // Local function to put a different state for this resource
201 void postLightRepresentation(std::shared_ptr<OCResource> resource)
205 OCRepresentation rep;
207 std::cout << "Posting light representation..."<<std::endl;
209 mylight.m_state = false;
210 mylight.m_power = 105;
212 rep.setValue("state", mylight.m_state);
213 rep.setValue("power", mylight.m_power);
215 // Invoke resource's post API with rep, query map and the callback parameter
216 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
220 // callback handler on PUT request
221 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
223 if(eCode == SUCCESS_RESPONSE)
225 std::cout << "PUT request was successful" << std::endl;
227 rep.getValue("state", mylight.m_state);
228 rep.getValue("power", mylight.m_power);
229 rep.getValue("name", mylight.m_name);
231 std::cout << "\tstate: " << mylight.m_state << std::endl;
232 std::cout << "\tpower: " << mylight.m_power << std::endl;
233 std::cout << "\tname: " << mylight.m_name << std::endl;
235 postLightRepresentation(curResource);
239 std::cout << "onPut Response error: " << eCode << std::endl;
244 // Local function to put a different state for this resource
245 void putLightRepresentation(std::shared_ptr<OCResource> resource)
249 OCRepresentation rep;
251 std::cout << "Putting light representation..."<<std::endl;
253 mylight.m_state = true;
254 mylight.m_power = 15;
256 rep.setValue("state", mylight.m_state);
257 rep.setValue("power", mylight.m_power);
259 // Invoke resource's put API with rep, query map and the callback parameter
260 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
264 // Callback handler on GET request
265 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
267 if(eCode == SUCCESS_RESPONSE)
269 std::cout << "GET request was successful" << std::endl;
270 std::cout << "Resource URI: " << rep.getUri() << std::endl;
272 rep.getValue("state", mylight.m_state);
273 rep.getValue("power", mylight.m_power);
274 rep.getValue("name", mylight.m_name);
276 std::cout << "\tstate: " << mylight.m_state << std::endl;
277 std::cout << "\tpower: " << mylight.m_power << std::endl;
278 std::cout << "\tname: " << mylight.m_name << std::endl;
280 putLightRepresentation(curResource);
284 std::cout << "onGET Response error: " << eCode << std::endl;
289 // Local function to get representation of light resource
290 void getLightRepresentation(std::shared_ptr<OCResource> resource)
294 std::cout << "Getting Light Representation..."<<std::endl;
295 // Invoke resource's get API with the callback parameter
298 resource->get(test, &onGet,OC::QualityOfService::HighQos);
302 // Callback to found resources
303 void foundResource(std::shared_ptr<OCResource> resource)
305 std::string resourceURI;
306 std::string hostAddress;
309 // Do some operations with resource object.
312 std::lock_guard<std::mutex> lk(resourceLock);
314 if(discoveredResources.find(resource) == discoveredResources.end())
316 std::cout << "Found resource " << resource->uniqueIdentifier() <<
317 " for the first time on server with ID: "<< resource->sid()<<std::endl;
318 discoveredResources.insert(resource);
322 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
327 std::cout << "Found another resource, ignoring"<<std::endl;
331 std::cout<<"DISCOVERED Resource:"<<std::endl;
332 // Get the resource URI
333 resourceURI = resource->uri();
334 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
336 // Get the resource host address
337 hostAddress = resource->host();
338 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
340 // Get the resource types
341 std::cout << "\tList of resource types: " << std::endl;
342 for(auto &resourceTypes : resource->getResourceTypes())
344 std::cout << "\t\t" << resourceTypes << std::endl;
347 // Get the resource interfaces
348 std::cout << "\tList of resource interfaces: " << std::endl;
349 for(auto &resourceInterfaces : resource->getResourceInterfaces())
351 std::cout << "\t\t" << resourceInterfaces << std::endl;
354 if(resourceURI == "/a/light")
356 curResource = resource;
358 // Call a local function which will internally invoke get
359 // API on the resource pointer
360 getLightRepresentation(resource);
365 // Resource is invalid
366 std::cout << "Resource is invalid" << std::endl;
370 catch(std::exception& e)
372 std::cerr << "Exception in foundResource: "<< e.what() <<std::endl;
378 std::cout << std::endl;
379 std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
380 std::cout << " ObserveType : 1 - Observe" << std::endl;
381 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
382 std::cout << " ConnectivityType: Default IP" << std::endl;
383 std::cout << " ConnectivityType : 0 - IP"<< std::endl;
386 int main(int argc, char* argv[]) {
388 std::ostringstream requestURI;
390 OCConnectivityType connectivityType = CT_ADAPTER_IP;
395 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
397 else if (argc ==2 || argc==3)
399 int value = std::stoi(argv[1]);
401 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
403 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
405 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
409 std::size_t inputValLen;
410 int optionSelected = std::stoi(argv[2], &inputValLen);
412 if(inputValLen == strlen(argv[2]))
414 if(optionSelected == 0)
416 std::cout << "Using IP."<< std::endl;
417 connectivityType = CT_ADAPTER_IP;
421 std::cout << "Invalid connectivity type selected. Using default IP"
427 std::cout << "Invalid connectivity type selected. Using default IP"
438 catch(std::exception&)
440 std::cout << "Invalid input argument." << std::endl;
446 // Create PlatformConfig object
448 OC::ServiceType::InProc,
449 OC::ModeType::Client,
452 OC::QualityOfService::HighQos
455 OCPlatform::Configure(cfg);
459 // Find all resources
460 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
462 OCPlatform::findResource("", requestURI.str(),
463 connectivityType, &foundResource, OC::QualityOfService::LowQos);
464 std::cout<< "Finding Resource... " <<std::endl;
466 // Find resource is done twice so that we discover the original resources a second time.
467 // These resources will have the same uniqueidentifier (yet be different objects), so that
468 // we can verify/show the duplicate-checking code in foundResource(above);
469 OCPlatform::findResource("", requestURI.str(),
470 connectivityType, &foundResource, OC::QualityOfService::LowQos);
471 std::cout<< "Finding Resource for second time... " <<std::endl;
473 // A condition variable will free the mutex it is given, then do a non-
474 // intensive block until 'notify' is called on it. In this case, since we
475 // don't ever call cv.notify, this should be a non-processor intensive version
478 std::condition_variable cv;
479 std::unique_lock<std::mutex> lock(blocker);
483 catch(OCException& e)
485 oclog() << "Exception in main: "<<e.what();