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 std::mutex curResourceLock;
49 Light() : m_state(false), m_power(0), m_name("")
62 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
63 const int& eCode, const int& sequenceNumber)
67 if(eCode == OC_STACK_OK)
69 std::cout << "OBSERVE RESULT:"<<std::endl;
70 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 std::string resourceURI;
317 std::string hostAddress;
321 std::lock_guard<std::mutex> lock(curResourceLock);
322 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
324 std::cout << "Found resource " << resource->uniqueIdentifier() <<
325 " for the first time on server with ID: "<< resource->sid()<<std::endl;
326 discoveredResources[resource->uniqueIdentifier()] = resource;
330 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
335 std::cout << "Found another resource, ignoring"<<std::endl;
340 // Do some operations with resource object.
343 std::cout<<"DISCOVERED Resource:"<<std::endl;
344 // Get the resource URI
345 resourceURI = resource->uri();
346 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
348 // Get the resource host address
349 hostAddress = resource->host();
350 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
352 // Get the resource types
353 std::cout << "\tList of resource types: " << std::endl;
354 for(auto &resourceTypes : resource->getResourceTypes())
356 std::cout << "\t\t" << resourceTypes << std::endl;
359 // Get the resource interfaces
360 std::cout << "\tList of resource interfaces: " << std::endl;
361 for(auto &resourceInterfaces : resource->getResourceInterfaces())
363 std::cout << "\t\t" << resourceInterfaces << std::endl;
366 if(resourceURI == "/a/light")
368 curResource = resource;
369 // Call a local function which will internally invoke get API on the resource pointer
370 getLightRepresentation(resource);
375 // Resource is invalid
376 std::cout << "Resource is invalid" << std::endl;
380 catch(std::exception& e)
388 std::cout << std::endl;
390 std::cout << "Usage : simpleclient <ObserveType> <ConnectivityType>" << std::endl;
392 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
394 std::cout << " ObserveType : 1 - Observe" << std::endl;
395 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
397 std::cout<<" connectivityType: Default WIFI" << std::endl;
398 std::cout << " ConnectivityType : 0 - ETHERNET"<< std::endl;
399 std::cout << " ConnectivityType : 1 - WIFI"<< std::endl;
403 int main(int argc, char* argv[]) {
405 ostringstream requestURI;
408 OCConnectivityType connectivityType = OC_WIFI;
414 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
422 int value = stoi(argv[1]);
424 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
426 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
428 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
433 std::size_t inputValLen;
434 int optionSelected = stoi(argv[2], &inputValLen);
436 if(inputValLen == strlen(argv[2]))
438 if(optionSelected == 0)
440 connectivityType = OC_ETHERNET;
442 else if(optionSelected == 1)
444 connectivityType = OC_WIFI;
448 std::cout << "Invalid connectivity type selected. Using default WIFI"
454 std::cout << "Invalid connectivity type selected. Using default WIFI"
468 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
472 // Create PlatformConfig object
474 OC::ServiceType::InProc,
475 OC::ModeType::Client,
478 OC::QualityOfService::LowQos
481 OCPlatform::Configure(cfg);
484 // makes it so that all boolean values are printed as 'true/false' in this stream
485 std::cout.setf(std::ios::boolalpha);
486 // Find all resources
487 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
490 OCPlatform::findResource("", requestURI.str(),
491 connectivityType, &foundResource);
493 OCPlatform::findResource("", requestURI.str(), &foundResource);
495 std::cout<< "Finding Resource... " <<std::endl;
497 // Find resource is done twice so that we discover the original resources a second time.
498 // These resources will have the same uniqueidentifier (yet be different objects), so that
499 // we can verify/show the duplicate-checking code in foundResource(above);
501 OCPlatform::findResource("", requestURI.str(),
502 connectivityType, &foundResource);
504 OCPlatform::findResource("", requestURI.str(), &foundResource);
506 std::cout<< "Finding Resource for second time... " <<std::endl;
512 // A condition variable will free the mutex it is given, then do a non-
513 // intensive block until 'notify' is called on it. In this case, since we
514 // don't ever call cv.notify, this should be a non-processor intensive version
517 std::condition_variable cv;
518 std::unique_lock<std::mutex> lock(blocker);
521 }catch(OCException& e)