1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 #include <condition_variable>
28 #include "OCPlatform.h"
33 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
35 DiscoveredResourceMap discoveredResources;
36 std::shared_ptr<OCResource> curResource;
37 std::shared_ptr<OCResource> DISensorResource;
38 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
39 std::mutex curResourceLock;
51 Light() : m_on_off(false), m_color(0), m_dim(0), m_name("")
63 LightSensor() : m_intensity(0), m_name("")
76 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
77 const int &eCode, const int &sequenceNumber)
82 if (eCode == OC_STACK_OK)
84 std::cout << "OBSERVE RESULT:" << std::endl;
85 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
86 rep.getValue("on-off", mylight.m_on_off);
87 rep.getValue("color", mylight.m_color);
88 rep.getValue("dim", mylight.m_dim);
89 rep.getValue("name", mylight.m_name);
91 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
92 std::cout << "\tcolor: " << mylight.m_color << std::endl;
93 std::cout << "\tdim: " << mylight.m_dim << std::endl;
95 if (observe_count() > 10)
97 std::cout << "Cancelling Observe..." << std::endl;
98 OCStackResult result = curResource->cancelObserve();
100 std::cout << "Cancel result: " << result << std::endl;
102 std::cout << "DONE" << std::endl;
108 std::cout << "onObserve Response error: " << eCode << std::endl;
111 catch (std::exception &e)
113 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
119 void onLightIntensityObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
120 const int &eCode, const int &sequenceNumber)
125 if (eCode == OC_STACK_OK)
127 std::cout << "OBSERVE RESULT:" << std::endl;
128 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
131 std::cout << "\tintensity: " << rep.getValue<int>("intensity") << std::endl;
134 if (observe_count() > 10)
136 std::cout << "Cancelling Observe..." << std::endl;
137 OCStackResult result = curResource->cancelObserve();
139 std::cout << "Cancel result: " << result << std::endl;
141 std::cout << "DONE" << std::endl;
147 std::cout << "onObserve Response error: " << eCode << std::endl;
150 catch (std::exception &e)
152 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
157 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
162 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
164 std::cout << "POST request was successful" << std::endl;
166 if (rep.hasAttribute("createduri"))
168 std::cout << "\tUri of the created resource: "
169 << rep.getValue<std::string>("createduri") << std::endl;
173 rep.getValue("on-off", mylight.m_on_off);
174 rep.getValue("color", mylight.m_color);
175 rep.getValue("dim", mylight.m_dim);
177 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
178 std::cout << "\tcolor: " << mylight.m_color << std::endl;
179 std::cout << "\tdim: " << mylight.m_dim << std::endl;
182 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
184 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
186 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
188 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
191 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
196 std::cout << "onPost2 Response error: " << eCode << std::endl;
199 catch (std::exception &e)
201 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
206 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
211 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
213 std::cout << "POST request was successful" << std::endl;
215 if (rep.hasAttribute("createduri"))
217 std::cout << "\tUri of the created resource: "
218 << rep.getValue<std::string>("createduri") << std::endl;
222 rep.getValue("on-off", mylight.m_on_off);
223 rep.getValue("color", mylight.m_color);
224 rep.getValue("dim", mylight.m_dim);
226 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
227 std::cout << "\tcolor: " << mylight.m_color << std::endl;
228 std::cout << "\tdim: " << mylight.m_dim << std::endl;
231 OCRepresentation rep2;
233 std::cout << "Posting light representation..." << std::endl;
235 mylight.m_on_off = true;
237 rep2.setValue("on-off", mylight.m_on_off);
239 curResource->post(rep2, QueryParamsMap(), &onPost2);
243 std::cout << "onPost Response error: " << eCode << std::endl;
246 catch (std::exception &e)
248 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
252 // Local function to put a different state for this re<< std::endlsource
253 void postLightRepresentation(std::shared_ptr<OCResource> resource)
257 OCRepresentation rep;
259 std::cout << "Posting light representation..." << std::endl;
261 mylight.m_on_off = false;
263 rep.setValue("on-off", mylight.m_on_off);
265 // Invoke resource's post API with rep, query map and the callback parameter
266 resource->post(rep, QueryParamsMap(), &onPost);
270 // callback handler on PUT request
271 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
277 if (eCode == OC_STACK_OK)
279 std::cout << "PUT request was successful" << std::endl;
281 /*rep.getValue("on-off", mylight.m_on_off);
282 rep.getValue("dim", mylight.m_dim);
283 rep.getValue("color", mylight.m_color);
285 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
286 std::cout << "\tcolor: " << mylight.m_color << std::endl;
287 std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
289 //postLightRepresentation(curResource);
293 std::cout << "onPut Response error: " << eCode << std::endl;
296 catch (std::exception &e)
298 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
302 void onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
305 void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
312 if (eCode == OC_STACK_OK)
314 std::cout << "PUT request was successful" << std::endl;
317 std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
318 DISensorResource->get(test, &onGetForDISensor);
322 std::cout << "onPut Response error: " << eCode << std::endl;
325 catch (std::exception &e)
327 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
331 // Local function to put a different state for this resource
332 void putLightRepresentation(std::shared_ptr<OCResource> resource)
336 OCRepresentation rep;
338 std::cout << "Putting light representation..." << std::endl;
340 mylight.m_on_off = true;
342 std::cout << "Sending request to: " << resource->uri() << std::endl;
343 rep.setValue("on-off", mylight.m_on_off);
345 // Invoke resource's put API with rep, query map and the callback parameter
347 resource->post(rep, QueryParamsMap(), &onPut);
351 // Callback handler on GET request
352 void onGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
357 if (eCode == OC_STACK_OK)
359 std::cout << "GET request was successful" << std::endl;
360 std::cout << "Resource URI: " << rep.getUri() << std::endl;
362 std::cout << "Payload: " << rep.getPayload() << std::endl;
363 std::cout << "On-off: " << rep.getValueToString("on-off") << std::endl;
365 rep.getValue("on-off", mylight.m_on_off);
367 std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
369 postLightRepresentation(curResource);
373 std::cout << "onGET Response error: " << eCode << std::endl;
376 catch (std::exception &e)
378 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
382 void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
388 if (eCode == OC_STACK_OK)
390 std::cout << "GET request was successful" << std::endl;
391 std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
393 std::cout << "Payload: " << rep.getPayload() << std::endl;
395 std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
399 std::cout << "onGET Response error: " << eCode << std::endl;
402 catch (std::exception &e)
404 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
409 void onGetForLightIntensitySensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
415 if (eCode == OC_STACK_OK)
417 std::cout << "GET request was successful" << std::endl;
418 //std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;
420 std::cout << "Payload: " << rep.getPayload() << std::endl;
422 std::cout << "\tlightIntensity: " << rep.getValue<int>("intensity") << std::endl;
424 // iterating over all elements
425 OCRepresentation::const_iterator itr = rep.begin();
426 OCRepresentation::const_iterator endItr = rep.end();
428 for(;itr!=endItr;++itr)
430 std::cout << itr->attrname() << " ";
431 std::cout << itr->getValue<int>() << std::endl;
434 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onLightIntensityObserve);
438 std::cout << "onGET Response error: " << eCode << std::endl;
441 catch (std::exception &e)
443 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
447 // Local function to get representation of light resource
448 void getLightRepresentation(std::shared_ptr<OCResource> resource)
452 std::cout << "Getting Light Representation..." << std::endl;
453 // Invoke resource's get API with the callback parameter
456 std::cout << "Sending request to: " << resource->uri() << std::endl;
457 resource->get(test, &onGet);
461 void onGetDiscomfortIndex(const HeaderOptions &headerOptions, const OCRepresentation &rep,
465 std::cout << "onGetDiscomfortIndex" << std::endl;
468 if (eCode == OC_STACK_OK)
470 std::cout << "GET request was successful" << std::endl;
472 std::cout << "Payload: " << rep.getPayload() << std::endl;
474 std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
478 std::cout << "onGET Response error: " << eCode << std::endl;
481 catch (std::exception &e)
483 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
487 void onObserveDiscomfort(const HeaderOptions headerOptions, const OCRepresentation &rep,
488 const int &eCode, const int &sequenceNumber)
493 if (eCode == OC_STACK_OK)
495 std::cout << "OBSERVE RESULT:" << std::endl;
496 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
497 std::cout << "\tdiscomfortIndex: " << rep.getValue<double>("discomfortIndex") << std::endl;
499 if (observe_count() > 10)
501 std::cout << "Cancelling Observe..." << std::endl;
502 OCStackResult result = curResource->cancelObserve();
504 std::cout << "Cancel result: " << result << std::endl;
506 std::cout << "DONE" << std::endl;
512 std::cout << "onObserve Response error: " << eCode << std::endl;
515 catch (std::exception &e)
517 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
522 // Local function to get representation of light resource
523 void getDiscomfortRepresentation(std::shared_ptr<OCResource> resource)
527 std::cout << "Getting Discomfort Representation..." << std::endl;
528 // Invoke resource's get API with the callback parameter
531 std::cout << "Sending request to: " << resource->uri() << std::endl;
532 resource->get(test, &onGetDiscomfortIndex);
533 //resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
540 // Local function to get representation of light resource
541 void getLightIntensityRepresentation(std::shared_ptr<OCResource> resource)
545 std::cout << "Getting Light Representation..." << std::endl;
546 // Invoke resource's get API with the callback parameter
549 std::cout << "Sending request to: " << resource->uri() << std::endl;
550 resource->get(test, &onGetForLightIntensitySensor);
554 // Callback to found resources
555 void foundResource(std::shared_ptr<OCResource> resource)
557 std::cout << "In foundResource\n";
558 std::string resourceURI = resource->uri();
559 std::string hostAddress;
564 // Do some operations with resource object.
567 std::cout << "DISCOVERED Resource:" << std::endl;
568 // Get the resource URI
569 resourceURI = resource->uri();
570 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
572 // Get the resource host address
573 hostAddress = resource->host();
574 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
576 // Get the resource types
577 std::cout << "\tList of resource types: " << std::endl;
578 for (auto &resourceTypes : resource->getResourceTypes())
580 std::cout << "\t\t" << resourceTypes << std::endl;
581 /*if (resourceTypes == "oic.r.light")
583 curResource = resource;
584 // Call a local function which will internally invoke get API on the resource pointer
585 getLightRepresentation(resource);
587 if (resourceTypes == "oic.r.discomfortindex")
589 curResource = resource;
590 std::cout << "\t\tGet discomfort representation " << std::endl;
591 // Call a local function which will internally invoke get API on the resource pointer
592 getDiscomfortRepresentation(resource);
596 // Get the resource interfaces
597 std::cout << "\tList of resource interfaces: " << std::endl;
598 for (auto &resourceInterfaces : resource->getResourceInterfaces())
600 std::cout << "\t\t" << resourceInterfaces << std::endl;
605 // Resource is invalid
606 std::cout << "Resource is invalid" << std::endl;
610 catch (std::exception &e)
612 std::cerr << "Exception in foundResource: " << e.what() << std::endl;
618 std::cout << std::endl;
619 std::cout << "---------------------------------------------------------------------\n";
620 std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
621 std::cout << " ObserveType : 1 - Observe" << std::endl;
622 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
623 std::cout << "---------------------------------------------------------------------\n\n";
626 void checkObserverValue(int value)
630 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
631 std::cout << "<===Setting ObserveType to Observe===>\n\n";
635 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
636 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
640 std::cout << "<===Invalid ObserveType selected."
641 << " Setting ObserveType to Observe===>\n\n";
645 static FILE *client_open(const char *path, const char *mode)
649 return fopen("./oic_svr_db_client.json", mode);
652 int main(int argc, char *argv[])
655 std::ostringstream requestURI;
656 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
662 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
666 checkObserverValue(std::stoi(argv[1]));
670 std::cout << "<===Invalid number of command line arguments===>\n\n";
674 catch (std::exception &)
676 std::cout << "<===Invalid input arguments===>\n\n";
680 // Create PlatformConfig object
683 OC::ServiceType::InProc,
687 OC::QualityOfService::LowQos,
691 OCPlatform::Configure(cfg);
694 // makes it so that all boolean values are printed as 'true/false' in this stream
695 std::cout.setf(std::ios::boolalpha);
696 // Find all resources
697 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
699 OCPlatform::findResource("", requestURI.str(),
700 CT_DEFAULT, &foundResource);
701 std::cout << "Finding Resource... " << std::endl;
703 // Find resource is done twice so that we discover the original resources a second time.
704 // These resources will have the same uniqueidentifier (yet be different objects), so that
705 // we can verify/show the duplicate-checking code in foundResource(above);
706 OCPlatform::findResource("", requestURI.str(),
707 CT_DEFAULT, &foundResource);
708 std::cout << "Finding Resource for second time..." << std::endl;
710 // A condition variable will free the mutex it is given, then do a non-
711 // intensive block until 'notify' is called on it. In this case, since we
712 // don't ever call cv.notify, this should be a non-processor intensive version
715 std::condition_variable cv;
716 std::unique_lock<std::mutex> lock(blocker);
720 catch (const OCException &e)
722 std::cout << "Exception in main: " << e.what();