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;
39 static OCConnectivityType connectivityType = OC_WIFI;
40 std::mutex curResourceLock;
50 Light() : m_state(false), m_power(0), m_name("")
63 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
64 const int& eCode, const int& sequenceNumber)
68 if(eCode == OC_STACK_OK)
70 std::cout << "OBSERVE RESULT:"<<std::endl;
71 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
73 rep.getValue("state", mylight.m_state);
74 rep.getValue("power", mylight.m_power);
75 rep.getValue("name", mylight.m_name);
77 std::cout << "\tstate: " << mylight.m_state << std::endl;
78 std::cout << "\tpower: " << mylight.m_power << std::endl;
79 std::cout << "\tname: " << mylight.m_name << std::endl;
81 if(observe_count() > 30)
83 std::cout<<"Cancelling Observe..."<<std::endl;
84 OCStackResult result = curResource->cancelObserve();
86 std::cout << "Cancel result: "<< result <<std::endl;
88 std::cout << "DONE"<<std::endl;
94 std::cout << "onObserve Response error: " << eCode << std::endl;
98 catch(std::exception& e)
100 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
105 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
109 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
111 std::cout << "POST request was successful" << std::endl;
113 if(rep.hasAttribute("createduri"))
115 std::cout << "\tUri of the created resource: "
116 << rep.getValue<std::string>("createduri") << std::endl;
120 rep.getValue("state", mylight.m_state);
121 rep.getValue("power", mylight.m_power);
122 rep.getValue("name", mylight.m_name);
124 std::cout << "\tstate: " << mylight.m_state << std::endl;
125 std::cout << "\tpower: " << mylight.m_power << std::endl;
126 std::cout << "\tname: " << mylight.m_name << std::endl;
129 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
130 std::cout << endl << "Observe is used." << endl << endl;
131 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
132 std::cout << endl << "ObserveAll is used." << endl << endl;
134 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
139 std::cout << "onPost2 Response error: " << eCode << std::endl;
143 catch(std::exception& e)
145 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
150 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
154 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
156 std::cout << "POST request was successful" << std::endl;
158 if(rep.hasAttribute("createduri"))
160 std::cout << "\tUri of the created resource: "
161 << rep.getValue<std::string>("createduri") << std::endl;
165 rep.getValue("state", mylight.m_state);
166 rep.getValue("power", mylight.m_power);
167 rep.getValue("name", mylight.m_name);
169 std::cout << "\tstate: " << mylight.m_state << std::endl;
170 std::cout << "\tpower: " << mylight.m_power << std::endl;
171 std::cout << "\tname: " << mylight.m_name << std::endl;
174 OCRepresentation rep2;
176 std::cout << "Posting light representation..."<<std::endl;
178 mylight.m_state = true;
179 mylight.m_power = 55;
181 rep2.setValue("state", mylight.m_state);
182 rep2.setValue("power", mylight.m_power);
184 curResource->post(rep2, QueryParamsMap(), &onPost2);
188 std::cout << "onPost Response error: " << eCode << std::endl;
192 catch(std::exception& e)
194 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
198 // Local function to put a different state for this resource
199 void postLightRepresentation(std::shared_ptr<OCResource> resource)
203 OCRepresentation rep;
205 std::cout << "Posting light representation..."<<std::endl;
207 mylight.m_state = false;
208 mylight.m_power = 105;
210 rep.setValue("state", mylight.m_state);
211 rep.setValue("power", mylight.m_power);
213 // Invoke resource's post API with rep, query map and the callback parameter
214 resource->post(rep, QueryParamsMap(), &onPost);
218 // callback handler on PUT request
219 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
223 if(eCode == OC_STACK_OK)
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;
243 catch(std::exception& e)
245 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
249 // Local function to put a different state for this resource
250 void putLightRepresentation(std::shared_ptr<OCResource> resource)
254 OCRepresentation rep;
256 std::cout << "Putting light representation..."<<std::endl;
258 mylight.m_state = true;
259 mylight.m_power = 15;
261 rep.setValue("state", mylight.m_state);
262 rep.setValue("power", mylight.m_power);
264 // Invoke resource's put API with rep, query map and the callback parameter
265 resource->put(rep, QueryParamsMap(), &onPut);
269 // Callback handler on GET request
270 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
274 if(eCode == OC_STACK_OK)
276 std::cout << "GET request was successful" << std::endl;
277 std::cout << "Resource URI: " << rep.getUri() << std::endl;
279 rep.getValue("state", mylight.m_state);
280 rep.getValue("power", mylight.m_power);
281 rep.getValue("name", mylight.m_name);
283 std::cout << "\tstate: " << mylight.m_state << std::endl;
284 std::cout << "\tpower: " << mylight.m_power << std::endl;
285 std::cout << "\tname: " << mylight.m_name << std::endl;
287 putLightRepresentation(curResource);
291 std::cout << "onGET Response error: " << eCode << std::endl;
295 catch(std::exception& e)
297 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
301 // Local function to get representation of light resource
302 void getLightRepresentation(std::shared_ptr<OCResource> resource)
306 std::cout << "Getting Light Representation..."<<std::endl;
307 // Invoke resource's get API with the callback parameter
310 resource->get(test, &onGet);
314 // Callback to found resources
315 void foundResource(std::shared_ptr<OCResource> resource)
317 cout << "In foundResource\n";
318 std::string resourceURI;
319 std::string hostAddress;
323 std::lock_guard<std::mutex> lock(curResourceLock);
324 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
326 std::cout << "Found resource " << resource->uniqueIdentifier() <<
327 " for the first time on server with ID: "<< resource->sid()<<std::endl;
328 discoveredResources[resource->uniqueIdentifier()] = resource;
332 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
337 std::cout << "Found another resource, ignoring"<<std::endl;
342 // Do some operations with resource object.
345 std::cout<<"DISCOVERED Resource:"<<std::endl;
346 // Get the resource URI
347 resourceURI = resource->uri();
348 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
350 // Get the resource host address
351 hostAddress = resource->host();
352 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
354 // Get the resource types
355 std::cout << "\tList of resource types: " << std::endl;
356 for(auto &resourceTypes : resource->getResourceTypes())
358 std::cout << "\t\t" << resourceTypes << std::endl;
361 // Get the resource interfaces
362 std::cout << "\tList of resource interfaces: " << std::endl;
363 for(auto &resourceInterfaces : resource->getResourceInterfaces())
365 std::cout << "\t\t" << resourceInterfaces << std::endl;
368 if(resourceURI == "/a/light")
370 curResource = resource;
371 // Call a local function which will internally invoke get API on the resource pointer
372 getLightRepresentation(resource);
377 // Resource is invalid
378 std::cout << "Resource is invalid" << std::endl;
382 catch(std::exception& e)
390 std::cout << std::endl;
391 std::cout << "---------------------------------------------------------------------\n";
392 std::cout << "Usage : simpleclient <ObserveType> <ConnectivityType>" << std::endl;
393 std::cout << " ObserveType : 1 - Observe" << std::endl;
394 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
395 std::cout << " connectivityType: Default WIFI" << std::endl;
396 std::cout << " ConnectivityType : 0 - ETHERNET"<< std::endl;
397 std::cout << " ConnectivityType : 1 - WIFI"<< std::endl;
398 std::cout << "---------------------------------------------------------------------\n\n";
401 void checkObserverValue(int value)
405 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
406 std::cout << "<===Setting ObserveType to Observe===>\n\n";
410 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
411 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
415 std::cout << "<===Invalid ObserveType selected."
416 <<" Setting ObserveType to Observe===>\n\n";
420 void checkConnectivityValue(int value)
424 connectivityType = OC_ETHERNET;
425 std::cout << "<===Setting connectivityType to Ethernet===>\n\n";
429 connectivityType = OC_WIFI;
430 std::cout << "<===Setting connectivityType to WIFI===>\n\n";
434 std::cout << "<===Invalid ConnectivitType selected."
435 <<"Setting ConnectivityType to WIFI===>\n\n";
439 int main(int argc, char* argv[]) {
441 ostringstream requestURI;
448 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to WIFI===>\n\n";
453 checkObserverValue(stoi(argv[1]));
454 std::cout << "<===No ConnectivtyType selected. "
455 << "Setting ConnectivityType to WIFI===>\n\n";
459 checkObserverValue(stoi(argv[1]));
460 checkConnectivityValue(stoi(argv[2]));
464 std::cout << "<===Invalid number of command line arguments===>\n\n";
470 std::cout << "<===Invalid input arguments===>\n\n";
474 // Create PlatformConfig object
476 OC::ServiceType::InProc,
477 OC::ModeType::Client,
480 OC::QualityOfService::LowQos
483 OCPlatform::Configure(cfg);
486 // makes it so that all boolean values are printed as 'true/false' in this stream
487 std::cout.setf(std::ios::boolalpha);
488 // Find all resources
489 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
491 OCPlatform::findResource("", requestURI.str(),
492 connectivityType, &foundResource);
493 std::cout<< "Finding Resource... " <<std::endl;
495 // Find resource is done twice so that we discover the original resources a second time.
496 // These resources will have the same uniqueidentifier (yet be different objects), so that
497 // we can verify/show the duplicate-checking code in foundResource(above);
498 OCPlatform::findResource("", requestURI.str(),
499 connectivityType, &foundResource);
500 std::cout<< "Finding Resource for second time..." << std::endl;
502 // A condition variable will free the mutex it is given, then do a non-
503 // intensive block until 'notify' is called on it. In this case, since we
504 // don't ever call cv.notify, this should be a non-processor intensive version
507 std::condition_variable cv;
508 std::unique_lock<std::mutex> lock(blocker);
511 }catch(OCException& e)