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 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
36 DiscoveredResourceMap discoveredResources;
37 std::shared_ptr<OCResource> curResource;
38 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
48 Light() : m_state(false), m_power(0), m_name("")
61 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
62 const int& eCode, const int& sequenceNumber)
66 if(eCode == OC_STACK_OK)
68 std::cout << "OBSERVE RESULT:"<<std::endl;
69 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
71 rep.getValue("state", mylight.m_state);
72 rep.getValue("power", mylight.m_power);
73 rep.getValue("name", mylight.m_name);
75 std::cout << "\tstate: " << mylight.m_state << std::endl;
76 std::cout << "\tpower: " << mylight.m_power << std::endl;
77 std::cout << "\tname: " << mylight.m_name << std::endl;
79 if(observe_count() > 30)
81 std::cout<<"Cancelling Observe..."<<std::endl;
82 OCStackResult result = curResource->cancelObserve();
84 std::cout << "Cancel result: "<< result <<std::endl;
86 std::cout << "DONE"<<std::endl;
92 std::cout << "onObserve Response error: " << eCode << std::endl;
96 catch(std::exception& e)
98 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
103 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
107 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
109 std::cout << "POST request was successful" << std::endl;
111 if(rep.hasAttribute("createduri"))
113 std::cout << "\tUri of the created resource: "
114 << rep.getValue<std::string>("createduri") << std::endl;
118 rep.getValue("state", mylight.m_state);
119 rep.getValue("power", mylight.m_power);
120 rep.getValue("name", mylight.m_name);
122 std::cout << "\tstate: " << mylight.m_state << std::endl;
123 std::cout << "\tpower: " << mylight.m_power << std::endl;
124 std::cout << "\tname: " << mylight.m_name << std::endl;
127 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
128 std::cout << endl << "Observe is used." << endl << endl;
129 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
130 std::cout << endl << "ObserveAll is used." << endl << endl;
132 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
137 std::cout << "onPost2 Response error: " << eCode << std::endl;
141 catch(std::exception& e)
143 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
148 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
152 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
154 std::cout << "POST request was successful" << std::endl;
156 if(rep.hasAttribute("createduri"))
158 std::cout << "\tUri of the created resource: "
159 << rep.getValue<std::string>("createduri") << std::endl;
163 rep.getValue("state", mylight.m_state);
164 rep.getValue("power", mylight.m_power);
165 rep.getValue("name", mylight.m_name);
167 std::cout << "\tstate: " << mylight.m_state << std::endl;
168 std::cout << "\tpower: " << mylight.m_power << std::endl;
169 std::cout << "\tname: " << mylight.m_name << std::endl;
172 OCRepresentation rep2;
174 std::cout << "Posting light representation..."<<std::endl;
176 mylight.m_state = true;
177 mylight.m_power = 55;
179 rep2.setValue("state", mylight.m_state);
180 rep2.setValue("power", mylight.m_power);
182 curResource->post(rep2, QueryParamsMap(), &onPost2);
186 std::cout << "onPost Response error: " << eCode << std::endl;
190 catch(std::exception& e)
192 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
196 // Local function to put a different state for this resource
197 void postLightRepresentation(std::shared_ptr<OCResource> resource)
201 OCRepresentation rep;
203 std::cout << "Posting light representation..."<<std::endl;
205 mylight.m_state = false;
206 mylight.m_power = 105;
208 rep.setValue("state", mylight.m_state);
209 rep.setValue("power", mylight.m_power);
211 // Invoke resource's post API with rep, query map and the callback parameter
212 resource->post(rep, QueryParamsMap(), &onPost);
216 // callback handler on PUT request
217 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
221 if(eCode == OC_STACK_OK)
223 std::cout << "PUT request was successful" << std::endl;
225 rep.getValue("state", mylight.m_state);
226 rep.getValue("power", mylight.m_power);
227 rep.getValue("name", mylight.m_name);
229 std::cout << "\tstate: " << mylight.m_state << std::endl;
230 std::cout << "\tpower: " << mylight.m_power << std::endl;
231 std::cout << "\tname: " << mylight.m_name << std::endl;
233 postLightRepresentation(curResource);
237 std::cout << "onPut Response error: " << eCode << std::endl;
241 catch(std::exception& e)
243 std::cout << "Exception: " << e.what() << " in onPut" << 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);
267 // Callback handler on GET request
268 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
272 if(eCode == OC_STACK_OK)
274 std::cout << "GET request was successful" << std::endl;
275 std::cout << "Resource URI: " << rep.getUri() << std::endl;
277 rep.getValue("state", mylight.m_state);
278 rep.getValue("power", mylight.m_power);
279 rep.getValue("name", mylight.m_name);
281 std::cout << "\tstate: " << mylight.m_state << std::endl;
282 std::cout << "\tpower: " << mylight.m_power << std::endl;
283 std::cout << "\tname: " << mylight.m_name << std::endl;
285 putLightRepresentation(curResource);
289 std::cout << "onGET Response error: " << eCode << std::endl;
293 catch(std::exception& e)
295 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
299 // Local function to get representation of light resource
300 void getLightRepresentation(std::shared_ptr<OCResource> resource)
304 std::cout << "Getting Light Representation..."<<std::endl;
305 // Invoke resource's get API with the callback parameter
308 resource->get(test, &onGet);
312 // Callback to found resources
313 void foundResource(std::shared_ptr<OCResource> resource)
315 std::string resourceURI;
316 std::string hostAddress;
319 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
321 std::cout << "Found resource " << resource->uniqueIdentifier() <<
322 " for the first time on server with ID: "<< resource->sid()<<std::endl;
323 discoveredResources[resource->uniqueIdentifier()] = resource;
327 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
332 std::cout << "Found another resource, ignoring"<<std::endl;
336 // Do some operations with resource object.
339 std::cout<<"DISCOVERED Resource:"<<std::endl;
340 // Get the resource URI
341 resourceURI = resource->uri();
342 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
344 // Get the resource host address
345 hostAddress = resource->host();
346 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
348 // Get the resource types
349 std::cout << "\tList of resource types: " << std::endl;
350 for(auto &resourceTypes : resource->getResourceTypes())
352 std::cout << "\t\t" << resourceTypes << std::endl;
355 // Get the resource interfaces
356 std::cout << "\tList of resource interfaces: " << std::endl;
357 for(auto &resourceInterfaces : resource->getResourceInterfaces())
359 std::cout << "\t\t" << resourceInterfaces << std::endl;
362 if(resourceURI == "/a/light")
364 curResource = resource;
365 // Call a local function which will internally invoke get API on the resource pointer
366 getLightRepresentation(resource);
371 // Resource is invalid
372 std::cout << "Resource is invalid" << std::endl;
376 catch(std::exception& e)
384 std::cout << std::endl;
386 std::cout << "Usage : simpleclient <ObserveType> <ConnectivityType>" << std::endl;
388 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
390 std::cout << " ObserveType : 1 - Observe" << std::endl;
391 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
393 std::cout<<" connectivityType: Default WIFI" << std::endl;
394 std::cout << " ConnectivityType : 0 - ETHERNET"<< std::endl;
395 std::cout << " ConnectivityType : 1 - WIFI"<< std::endl;
399 int main(int argc, char* argv[]) {
401 ostringstream requestURI;
404 OCConnectivityType connectivityType = OC_WIFI;
410 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
418 int value = stoi(argv[1]);
420 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
422 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
424 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
429 std::size_t inputValLen;
430 int optionSelected = stoi(argv[2], &inputValLen);
432 if(inputValLen == strlen(argv[2]))
434 if(optionSelected == 0)
436 connectivityType = OC_ETHERNET;
438 else if(optionSelected == 1)
440 connectivityType = OC_WIFI;
444 std::cout << "Invalid connectivity type selected. Using default WIFI"
450 std::cout << "Invalid connectivity type selected. Using default WIFI"
464 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
468 // Create PlatformConfig object
470 OC::ServiceType::InProc,
471 OC::ModeType::Client,
474 OC::QualityOfService::LowQos
477 OCPlatform::Configure(cfg);
480 // makes it so that all boolean values are printed as 'true/false' in this stream
481 std::cout.setf(std::ios::boolalpha);
482 // Find all resources
483 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
486 OCPlatform::findResource("", requestURI.str(),
487 connectivityType, &foundResource);
489 OCPlatform::findResource("", requestURI.str(), &foundResource);
491 std::cout<< "Finding Resource... " <<std::endl;
493 // Find resource is done twice so that we discover the original resources a second time.
494 // These resources will have the same uniqueidentifier (yet be different objects), so that
495 // we can verify/show the duplicate-checking code in foundResource(above);
497 OCPlatform::findResource("", requestURI.str(),
498 connectivityType, &foundResource);
500 OCPlatform::findResource("", requestURI.str(), &foundResource);
502 std::cout<< "Finding Resource for second time... " <<std::endl;
508 // A condition variable will free the mutex it is given, then do a non-
509 // intensive block until 'notify' is called on it. In this case, since we
510 // don't ever call cv.notify, this should be a non-processor intensive version
513 std::condition_variable cv;
514 std::unique_lock<std::mutex> lock(blocker);
517 }catch(OCException& e)