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;
72 rep.getValue("state", mylight.m_state);
73 rep.getValue("power", mylight.m_power);
74 rep.getValue("name", mylight.m_name);
76 std::cout << "\tstate: " << mylight.m_state << std::endl;
77 std::cout << "\tpower: " << mylight.m_power << std::endl;
78 std::cout << "\tname: " << mylight.m_name << std::endl;
80 if(observe_count() > 30)
82 std::cout<<"Cancelling Observe..."<<std::endl;
83 OCStackResult result = curResource->cancelObserve();
85 std::cout << "Cancel result: "<< result <<std::endl;
87 std::cout << "DONE"<<std::endl;
93 std::cout << "onObserve Response error: " << eCode << std::endl;
97 catch(std::exception& e)
99 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
104 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
108 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
110 std::cout << "POST request was successful" << std::endl;
112 if(rep.hasAttribute("createduri"))
114 std::cout << "\tUri of the created resource: "
115 << rep.getValue<std::string>("createduri") << std::endl;
119 rep.getValue("state", mylight.m_state);
120 rep.getValue("power", mylight.m_power);
121 rep.getValue("name", mylight.m_name);
123 std::cout << "\tstate: " << mylight.m_state << std::endl;
124 std::cout << "\tpower: " << mylight.m_power << std::endl;
125 std::cout << "\tname: " << mylight.m_name << std::endl;
128 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
129 std::cout << endl << "Observe is used." << endl << endl;
130 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
131 std::cout << endl << "ObserveAll is used." << endl << endl;
133 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
138 std::cout << "onPost2 Response error: " << eCode << std::endl;
142 catch(std::exception& e)
144 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
149 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
153 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
155 std::cout << "POST request was successful" << std::endl;
157 if(rep.hasAttribute("createduri"))
159 std::cout << "\tUri of the created resource: "
160 << rep.getValue<std::string>("createduri") << std::endl;
164 rep.getValue("state", mylight.m_state);
165 rep.getValue("power", mylight.m_power);
166 rep.getValue("name", mylight.m_name);
168 std::cout << "\tstate: " << mylight.m_state << std::endl;
169 std::cout << "\tpower: " << mylight.m_power << std::endl;
170 std::cout << "\tname: " << mylight.m_name << std::endl;
173 OCRepresentation rep2;
175 std::cout << "Posting light representation..."<<std::endl;
177 mylight.m_state = true;
178 mylight.m_power = 55;
180 rep2.setValue("state", mylight.m_state);
181 rep2.setValue("power", mylight.m_power);
183 curResource->post(rep2, QueryParamsMap(), &onPost2);
187 std::cout << "onPost Response error: " << eCode << std::endl;
191 catch(std::exception& e)
193 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
197 // Local function to put a different state for this resource
198 void postLightRepresentation(std::shared_ptr<OCResource> resource)
202 OCRepresentation rep;
204 std::cout << "Posting light representation..."<<std::endl;
206 mylight.m_state = false;
207 mylight.m_power = 105;
209 rep.setValue("state", mylight.m_state);
210 rep.setValue("power", mylight.m_power);
212 // Invoke resource's post API with rep, query map and the callback parameter
213 resource->post(rep, QueryParamsMap(), &onPost);
217 // callback handler on PUT request
218 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
222 if(eCode == OC_STACK_OK)
224 std::cout << "PUT request was successful" << std::endl;
226 rep.getValue("state", mylight.m_state);
227 rep.getValue("power", mylight.m_power);
228 rep.getValue("name", mylight.m_name);
230 std::cout << "\tstate: " << mylight.m_state << std::endl;
231 std::cout << "\tpower: " << mylight.m_power << std::endl;
232 std::cout << "\tname: " << mylight.m_name << std::endl;
234 postLightRepresentation(curResource);
238 std::cout << "onPut Response error: " << eCode << std::endl;
242 catch(std::exception& e)
244 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
248 // Local function to put a different state for this resource
249 void putLightRepresentation(std::shared_ptr<OCResource> resource)
253 OCRepresentation rep;
255 std::cout << "Putting light representation..."<<std::endl;
257 mylight.m_state = true;
258 mylight.m_power = 15;
260 rep.setValue("state", mylight.m_state);
261 rep.setValue("power", mylight.m_power);
263 // Invoke resource's put API with rep, query map and the callback parameter
264 resource->put(rep, QueryParamsMap(), &onPut);
268 // Callback handler on GET request
269 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
273 if(eCode == OC_STACK_OK)
275 std::cout << "GET request was successful" << std::endl;
276 std::cout << "Resource URI: " << rep.getUri() << std::endl;
278 rep.getValue("state", mylight.m_state);
279 rep.getValue("power", mylight.m_power);
280 rep.getValue("name", mylight.m_name);
282 std::cout << "\tstate: " << mylight.m_state << std::endl;
283 std::cout << "\tpower: " << mylight.m_power << std::endl;
284 std::cout << "\tname: " << mylight.m_name << std::endl;
286 putLightRepresentation(curResource);
290 std::cout << "onGET Response error: " << eCode << std::endl;
294 catch(std::exception& e)
296 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
300 // Local function to get representation of light resource
301 void getLightRepresentation(std::shared_ptr<OCResource> resource)
305 std::cout << "Getting Light Representation..."<<std::endl;
306 // Invoke resource's get API with the callback parameter
309 resource->get(test, &onGet);
313 // Callback to found resources
314 void foundResource(std::shared_ptr<OCResource> resource)
316 cout << "In foundResource\n";
317 std::string resourceURI;
318 std::string hostAddress;
322 std::lock_guard<std::mutex> lock(curResourceLock);
323 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
325 std::cout << "Found resource " << resource->uniqueIdentifier() <<
326 " for the first time on server with ID: "<< resource->sid()<<std::endl;
327 discoveredResources[resource->uniqueIdentifier()] = resource;
331 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
336 std::cout << "Found another resource, ignoring"<<std::endl;
341 // Do some operations with resource object.
344 std::cout<<"DISCOVERED Resource:"<<std::endl;
345 // Get the resource URI
346 resourceURI = resource->uri();
347 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
349 // Get the resource host address
350 hostAddress = resource->host();
351 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
353 // Get the resource types
354 std::cout << "\tList of resource types: " << std::endl;
355 for(auto &resourceTypes : resource->getResourceTypes())
357 std::cout << "\t\t" << resourceTypes << std::endl;
360 // Get the resource interfaces
361 std::cout << "\tList of resource interfaces: " << std::endl;
362 for(auto &resourceInterfaces : resource->getResourceInterfaces())
364 std::cout << "\t\t" << resourceInterfaces << std::endl;
367 if(resourceURI == "/a/light")
369 curResource = resource;
370 // Call a local function which will internally invoke get API on the resource pointer
371 getLightRepresentation(resource);
376 // Resource is invalid
377 std::cout << "Resource is invalid" << std::endl;
381 catch(std::exception& e)
389 std::cout << std::endl;
390 std::cout << "---------------------------------------------------------------------\n";
391 std::cout << "Usage : simpleclient <ObserveType> <ConnectivityType>" << std::endl;
392 std::cout << " ObserveType : 1 - Observe" << std::endl;
393 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
394 std::cout << " connectivityType: Default WIFI" << std::endl;
395 std::cout << " ConnectivityType : 0 - ETHERNET"<< std::endl;
396 std::cout << " ConnectivityType : 1 - WIFI"<< std::endl;
397 std::cout << "---------------------------------------------------------------------\n\n";
400 void checkObserverValue(int value)
404 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
405 std::cout << "<===Setting ObserveType to Observe===>\n\n";
409 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
410 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
414 std::cout << "<===Invalid ObserveType selected."
415 <<" Setting ObserveType to Observe===>\n\n";
419 void checkConnectivityValue(int value)
423 connectivityType = OC_ETHERNET;
424 std::cout << "<===Setting connectivityType to Ethernet===>\n\n";
428 connectivityType = OC_WIFI;
429 std::cout << "<===Setting connectivityType to WIFI===>\n\n";
433 std::cout << "<===Invalid ConnectivitType selected."
434 <<"Setting ConnectivityType to WIFI===>\n\n";
438 int main(int argc, char* argv[]) {
440 ostringstream requestURI;
447 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to WIFI===>\n\n";
452 checkObserverValue(stoi(argv[1]));
453 std::cout << "<===No ConnectivtyType selected. "
454 << "Setting ConnectivityType to WIFI===>\n\n";
458 checkObserverValue(stoi(argv[1]));
459 checkConnectivityValue(stoi(argv[2]));
463 std::cout << "<===Invalid number of command line arguments===>\n\n";
469 std::cout << "<===Invalid input arguments===>\n\n";
473 // Create PlatformConfig object
475 OC::ServiceType::InProc,
476 OC::ModeType::Client,
479 OC::QualityOfService::LowQos
482 OCPlatform::Configure(cfg);
485 // makes it so that all boolean values are printed as 'true/false' in this stream
486 std::cout.setf(std::ios::boolalpha);
487 // Find all resources
488 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
490 OCPlatform::findResource("", requestURI.str(),
491 connectivityType, &foundResource);
492 std::cout<< "Finding Resource... " <<std::endl;
494 // Find resource is done twice so that we discover the original resources a second time.
495 // These resources will have the same uniqueidentifier (yet be different objects), so that
496 // we can verify/show the duplicate-checking code in foundResource(above);
497 OCPlatform::findResource("", requestURI.str(),
498 connectivityType, &foundResource);
499 std::cout<< "Finding Resource for second time..." << std::endl;
501 // A condition variable will free the mutex it is given, then do a non-
502 // intensive block until 'notify' is called on it. In this case, since we
503 // don't ever call cv.notify, this should be a non-processor intensive version
506 std::condition_variable cv;
507 std::unique_lock<std::mutex> lock(blocker);
510 }catch(OCException& e)