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.
36 #include <condition_variable>
37 #include "OCPlatform.h"
40 #define maxSequenceNumber 0xFFFFFF
44 static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";
45 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
47 DiscoveredResourceMap discoveredResources;
48 std::shared_ptr<OCResource> curResource;
49 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
50 static OCConnectivityType TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
51 std::mutex curResourceLock;
61 Light() : m_state(false), m_power(0), m_name("")
74 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
75 const int& eCode, const int& sequenceNumber)
79 if(eCode == OC_STACK_OK && sequenceNumber != maxSequenceNumber + 1)
81 if(sequenceNumber == OC_OBSERVE_REGISTER)
83 std::cout << "Observe registration action is successful" << std::endl;
86 std::cout << "OBSERVE RESULT:"<<std::endl;
87 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
88 rep.getValue("state", mylight.m_state);
89 rep.getValue("power", mylight.m_power);
90 rep.getValue("name", mylight.m_name);
92 std::cout << "\tstate: " << mylight.m_state << std::endl;
93 std::cout << "\tpower: " << mylight.m_power << std::endl;
94 std::cout << "\tname: " << mylight.m_name << std::endl;
96 if(observe_count() == 11)
98 std::cout<<"Cancelling Observe..."<<std::endl;
99 OCStackResult result = curResource->cancelObserve();
101 std::cout << "Cancel result: "<< result <<std::endl;
103 std::cout << "DONE"<<std::endl;
109 if(eCode == OC_STACK_OK)
111 std::cout << "Observe registration failed or de-registration action failed/succeeded" << std::endl;
115 std::cout << "onObserve Response error: " << eCode << std::endl;
120 catch(std::exception& e)
122 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
127 void onPost2(const HeaderOptions& /*headerOptions*/,
128 const OCRepresentation& rep, const int eCode)
132 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
133 || eCode == OC_STACK_RESOURCE_CHANGED)
135 std::cout << "POST request was successful" << std::endl;
137 if(rep.hasAttribute("createduri"))
139 std::cout << "\tUri of the created resource: "
140 << rep.getValue<std::string>("createduri") << std::endl;
144 rep.getValue("state", mylight.m_state);
145 rep.getValue("power", mylight.m_power);
146 rep.getValue("name", mylight.m_name);
148 std::cout << "\tstate: " << mylight.m_state << std::endl;
149 std::cout << "\tpower: " << mylight.m_power << std::endl;
150 std::cout << "\tname: " << mylight.m_name << std::endl;
153 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
154 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
155 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
156 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
158 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
163 std::cout << "onPost2 Response error: " << eCode << std::endl;
167 catch(std::exception& e)
169 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
174 void onPost(const HeaderOptions& /*headerOptions*/,
175 const OCRepresentation& rep, const int eCode)
179 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
180 || eCode == OC_STACK_RESOURCE_CHANGED)
182 std::cout << "POST request was successful" << std::endl;
184 if(rep.hasAttribute("createduri"))
186 std::cout << "\tUri of the created resource: "
187 << rep.getValue<std::string>("createduri") << std::endl;
191 rep.getValue("state", mylight.m_state);
192 rep.getValue("power", mylight.m_power);
193 rep.getValue("name", mylight.m_name);
195 std::cout << "\tstate: " << mylight.m_state << std::endl;
196 std::cout << "\tpower: " << mylight.m_power << std::endl;
197 std::cout << "\tname: " << mylight.m_name << std::endl;
200 OCRepresentation rep2;
202 std::cout << "Posting light representation..."<<std::endl;
204 mylight.m_state = true;
205 mylight.m_power = 55;
207 rep2.setValue("state", mylight.m_state);
208 rep2.setValue("power", mylight.m_power);
210 curResource->post(rep2, QueryParamsMap(), &onPost2);
214 std::cout << "onPost Response error: " << eCode << std::endl;
218 catch(std::exception& e)
220 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
224 // Local function to put a different state for this resource
225 void postLightRepresentation(std::shared_ptr<OCResource> resource)
229 OCRepresentation rep;
231 std::cout << "Posting light representation..."<<std::endl;
233 mylight.m_state = false;
234 mylight.m_power = 105;
236 rep.setValue("state", mylight.m_state);
237 rep.setValue("power", mylight.m_power);
239 // Invoke resource's post API with rep, query map and the callback parameter
240 resource->post(rep, QueryParamsMap(), &onPost);
244 // callback handler on PUT request
245 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
249 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
251 std::cout << "PUT request was successful" << std::endl;
253 rep.getValue("state", mylight.m_state);
254 rep.getValue("power", mylight.m_power);
255 rep.getValue("name", mylight.m_name);
257 std::cout << "\tstate: " << mylight.m_state << std::endl;
258 std::cout << "\tpower: " << mylight.m_power << std::endl;
259 std::cout << "\tname: " << mylight.m_name << std::endl;
261 postLightRepresentation(curResource);
265 std::cout << "onPut Response error: " << eCode << std::endl;
269 catch(std::exception& e)
271 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
275 // Local function to put a different state for this resource
276 void putLightRepresentation(std::shared_ptr<OCResource> resource)
280 OCRepresentation rep;
282 std::cout << "Putting light representation..."<<std::endl;
284 mylight.m_state = true;
285 mylight.m_power = 15;
287 rep.setValue("state", mylight.m_state);
288 rep.setValue("power", mylight.m_power);
290 // Invoke resource's put API with rep, query map and the callback parameter
291 resource->put(rep, QueryParamsMap(), &onPut);
295 // Callback handler on GET request
296 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
300 if(eCode == OC_STACK_OK)
302 std::cout << "GET request was successful" << std::endl;
303 std::cout << "Resource URI: " << rep.getUri() << std::endl;
305 rep.getValue("state", mylight.m_state);
306 rep.getValue("power", mylight.m_power);
307 rep.getValue("name", mylight.m_name);
309 std::cout << "\tstate: " << mylight.m_state << std::endl;
310 std::cout << "\tpower: " << mylight.m_power << std::endl;
311 std::cout << "\tname: " << mylight.m_name << std::endl;
313 putLightRepresentation(curResource);
317 std::cout << "onGET Response error: " << eCode << std::endl;
321 catch(std::exception& e)
323 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
327 // Local function to get representation of light resource
328 void getLightRepresentation(std::shared_ptr<OCResource> resource)
332 std::cout << "Getting Light Representation..."<<std::endl;
333 // Invoke resource's get API with the callback parameter
336 resource->get(test, &onGet);
340 // Callback to found resources
341 void foundResource(std::shared_ptr<OCResource> resource)
343 std::cout << "In foundResource\n";
344 std::string resourceURI;
345 std::string hostAddress;
349 std::lock_guard<std::mutex> lock(curResourceLock);
350 if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
352 std::cout << "Found resource " << resource->uniqueIdentifier() <<
353 " for the first time on server with ID: "<< resource->sid()<<std::endl;
354 discoveredResources[resource->uniqueIdentifier()] = resource;
358 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
363 std::cout << "Found another resource, ignoring"<<std::endl;
368 // Do some operations with resource object.
371 std::cout<<"DISCOVERED Resource:"<<std::endl;
372 // Get the resource URI
373 resourceURI = resource->uri();
374 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
376 // Get the resource host address
377 hostAddress = resource->host();
378 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
380 // Get the resource types
381 std::cout << "\tList of resource types: " << std::endl;
382 for(auto &resourceTypes : resource->getResourceTypes())
384 std::cout << "\t\t" << resourceTypes << std::endl;
387 // Get the resource interfaces
388 std::cout << "\tList of resource interfaces: " << std::endl;
389 for(auto &resourceInterfaces : resource->getResourceInterfaces())
391 std::cout << "\t\t" << resourceInterfaces << std::endl;
394 if(resourceURI == "/a/light")
396 if (resource->connectivityType() & TRANSPORT_TYPE_TO_USE)
398 curResource = resource;
399 // Get the resource host address
400 std::cout << "\tAddress of selected resource: " << resource->host() << std::endl;
402 // Call a local function which will internally invoke get API on the resource pointer
403 getLightRepresentation(resource);
409 // Resource is invalid
410 std::cout << "Resource is invalid" << std::endl;
414 catch(std::exception& e)
416 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
422 std::cout << std::endl;
423 std::cout << "---------------------------------------------------------------------\n";
424 std::cout << "Usage : simpleclient <ObserveType> <TransportType>" << std::endl;
425 std::cout << " ObserveType : 1 - Observe" << std::endl;
426 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
427 std::cout << " TransportType : 1 - IP" << std::endl;
428 std::cout << " TransportType : 2 - TCP" << std::endl;
429 std::cout << "---------------------------------------------------------------------\n\n";
432 void checkObserverValue(int value)
436 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
437 std::cout << "<===Setting ObserveType to Observe===>\n\n";
441 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
442 std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
446 std::cout << "<===Invalid ObserveType selected."
447 <<" Setting ObserveType to Observe===>\n\n";
451 void checkTransportValue(int value)
455 TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
456 std::cout << "<===Setting TransportType to IP===>\n\n";
460 TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_TCP;
461 std::cout << "<===Setting TransportType to TCP===>\n\n";
465 std::cout << "<===Invalid TransportType selected."
466 <<" Setting TransportType to IP===>\n\n";
470 static FILE* client_open(const char* /*path*/, const char *mode)
472 return fopen(SVR_DB_FILE_NAME, mode);
475 int main(int argc, char* argv[]) {
477 std::ostringstream requestURI;
478 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
484 std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
488 checkObserverValue(std::stoi(argv[1]));
492 checkObserverValue(std::stoi(argv[1]));
493 checkTransportValue(std::stoi(argv[2]));
497 std::cout << "<===Invalid number of command line arguments===>\n\n";
501 catch(std::exception& )
503 std::cout << "<===Invalid input arguments===>\n\n";
507 // Create PlatformConfig object
509 OC::ServiceType::InProc,
513 OC::QualityOfService::HighQos,
517 OCPlatform::Configure(cfg);
520 // makes it so that all boolean values are printed as 'true/false' in this stream
521 std::cout.setf(std::ios::boolalpha);
522 // Find all resources
523 requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
525 OCPlatform::findResource("", requestURI.str(),
526 CT_DEFAULT, &foundResource);
527 std::cout<< "Finding Resource... " <<std::endl;
529 // Find resource is done twice so that we discover the original resources a second time.
530 // These resources will have the same uniqueidentifier (yet be different objects), so that
531 // we can verify/show the duplicate-checking code in foundResource(above);
532 OCPlatform::findResource("", requestURI.str(),
533 CT_DEFAULT, &foundResource);
534 std::cout<< "Finding Resource for second time..." << std::endl;
536 // A condition variable will free the mutex it is given, then do a non-
537 // intensive block until 'notify' is called on it. In this case, since we
538 // don't ever call cv.notify, this should be a non-processor intensive version
541 std::condition_variable cv;
542 std::unique_lock<std::mutex> lock(blocker);
545 }catch(OCException& e)
547 oclog() << "Exception in main: "<<e.what();