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.
27 #include <condition_variable>
29 #include "OCPlatform.h"
34 const int SUCCESS_RESPONSE = 0;
35 std::shared_ptr<OCResource> curResource;
36 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
46 Light() : m_state(false), m_power(0), m_name("")
59 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
60 const int& eCode, const int& sequenceNumber)
62 if(eCode == SUCCESS_RESPONSE)
64 std::cout << "OBSERVE RESULT:"<<std::endl;
65 if(sequenceNumber == 0)
67 std::cout << "\tObserve Registration Confirmed: "<< endl;
69 else if (sequenceNumber == 1)
71 std::cout << "\tObserve Cancel Confirmed: "<< endl;
73 std::cout << "DONE"<<std::endl;
78 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
81 rep.getValue("state", mylight.m_state);
82 rep.getValue("power", mylight.m_power);
83 rep.getValue("name", mylight.m_name);
85 std::cout << "\tstate: " << mylight.m_state << std::endl;
86 std::cout << "\tpower: " << mylight.m_power << std::endl;
87 std::cout << "\tname: " << mylight.m_name << std::endl;
89 if(observe_count() > 30)
91 std::cout<<"Cancelling Observe..."<<std::endl;
92 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
94 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
99 std::cout << "onObserve Response error: " << eCode << std::endl;
104 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
106 if(eCode == SUCCESS_RESPONSE)
108 std::cout << "POST request was successful" << std::endl;
110 if(rep.hasAttribute("createduri"))
112 std::cout << "\tUri of the created resource: "
113 << rep.getValue<std::string>("createduri") << std::endl;
117 rep.getValue("state", mylight.m_state);
118 rep.getValue("power", mylight.m_power);
119 rep.getValue("name", mylight.m_name);
121 std::cout << "\tstate: " << mylight.m_state << std::endl;
122 std::cout << "\tpower: " << mylight.m_power << std::endl;
123 std::cout << "\tname: " << mylight.m_name << std::endl;
126 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
127 std::cout << endl << "Observe is used." << endl << endl;
128 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
129 std::cout << endl << "ObserveAll is used." << endl << endl;
131 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
132 OC::QualityOfService::HighQos);
137 std::cout << "onPost2 Response error: " << eCode << std::endl;
142 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
144 if(eCode == SUCCESS_RESPONSE)
146 std::cout << "POST request was successful" << std::endl;
148 if(rep.hasAttribute("createduri"))
150 std::cout << "\tUri of the created resource: "
151 << rep.getValue<std::string>("createduri") << std::endl;
155 rep.getValue("state", mylight.m_state);
156 rep.getValue("power", mylight.m_power);
157 rep.getValue("name", mylight.m_name);
159 std::cout << "\tstate: " << mylight.m_state << std::endl;
160 std::cout << "\tpower: " << mylight.m_power << std::endl;
161 std::cout << "\tname: " << mylight.m_name << std::endl;
164 OCRepresentation rep2;
166 std::cout << "Posting light representation..."<<std::endl;
168 mylight.m_state = true;
169 mylight.m_power = 55;
171 rep2.setValue("state", mylight.m_state);
172 rep2.setValue("power", mylight.m_power);
174 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
178 std::cout << "onPost Response error: " << eCode << std::endl;
183 // Local function to put a different state for this resource
184 void postLightRepresentation(std::shared_ptr<OCResource> resource)
188 OCRepresentation rep;
190 std::cout << "Posting light representation..."<<std::endl;
192 mylight.m_state = false;
193 mylight.m_power = 105;
195 rep.setValue("state", mylight.m_state);
196 rep.setValue("power", mylight.m_power);
198 // Invoke resource's post API with rep, query map and the callback parameter
199 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
203 // callback handler on PUT request
204 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
206 if(eCode == SUCCESS_RESPONSE)
208 std::cout << "PUT request was successful" << std::endl;
210 rep.getValue("state", mylight.m_state);
211 rep.getValue("power", mylight.m_power);
212 rep.getValue("name", mylight.m_name);
214 std::cout << "\tstate: " << mylight.m_state << std::endl;
215 std::cout << "\tpower: " << mylight.m_power << std::endl;
216 std::cout << "\tname: " << mylight.m_name << std::endl;
218 postLightRepresentation(curResource);
222 std::cout << "onPut Response error: " << eCode << std::endl;
227 // Local function to put a different state for this resource
228 void putLightRepresentation(std::shared_ptr<OCResource> resource)
232 OCRepresentation rep;
234 std::cout << "Putting light representation..."<<std::endl;
236 mylight.m_state = true;
237 mylight.m_power = 15;
239 rep.setValue("state", mylight.m_state);
240 rep.setValue("power", mylight.m_power);
242 // Invoke resource's put API with rep, query map and the callback parameter
243 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
247 // Callback handler on GET request
248 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
250 if(eCode == SUCCESS_RESPONSE)
252 std::cout << "GET request was successful" << std::endl;
253 std::cout << "Resource URI: " << rep.getUri() << std::endl;
255 rep.getValue("state", mylight.m_state);
256 rep.getValue("power", mylight.m_power);
257 rep.getValue("name", mylight.m_name);
259 std::cout << "\tstate: " << mylight.m_state << std::endl;
260 std::cout << "\tpower: " << mylight.m_power << std::endl;
261 std::cout << "\tname: " << mylight.m_name << std::endl;
263 putLightRepresentation(curResource);
267 std::cout << "onGET Response error: " << eCode << std::endl;
272 // Local function to get representation of light resource
273 void getLightRepresentation(std::shared_ptr<OCResource> resource)
277 std::cout << "Getting Light Representation..."<<std::endl;
278 // Invoke resource's get API with the callback parameter
281 resource->get(test, &onGet,OC::QualityOfService::HighQos);
285 // Callback to found resources
286 void foundResource(std::shared_ptr<OCResource> resource)
290 std::cout << "Found another resource, ignoring"<<std::endl;
293 std::string resourceURI;
294 std::string hostAddress;
297 // Do some operations with resource object.
300 std::cout<<"DISCOVERED Resource:"<<std::endl;
301 // Get the resource URI
302 resourceURI = resource->uri();
303 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
305 // Get the resource host address
306 hostAddress = resource->host();
307 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
309 // Get the resource types
310 std::cout << "\tList of resource types: " << std::endl;
311 for(auto &resourceTypes : resource->getResourceTypes())
313 std::cout << "\t\t" << resourceTypes << std::endl;
316 // Get the resource interfaces
317 std::cout << "\tList of resource interfaces: " << std::endl;
318 for(auto &resourceInterfaces : resource->getResourceInterfaces())
320 std::cout << "\t\t" << resourceInterfaces << std::endl;
323 if(resourceURI == "/a/light")
325 curResource = resource;
327 // Call a local function which will internally invoke get API on the resource pointer
328 getLightRepresentation(resource);
333 // Resource is invalid
334 std::cout << "Resource is invalid" << std::endl;
338 catch(std::exception& e)
346 std::cout << std::endl;
347 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
348 std::cout << " ObserveType : 1 - Observe" << std::endl;
349 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
352 int main(int argc, char* argv[]) {
355 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
359 int value = atoi(argv[1]);
361 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
363 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
365 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
373 // Create PlatformConfig object
375 OC::ServiceType::InProc,
376 OC::ModeType::Client,
379 OC::QualityOfService::LowQos
382 OCPlatform::Configure(cfg);
386 // Find all resources
387 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource,
388 OC::QualityOfService::LowQos);
389 std::cout<< "Finding Resource... " <<std::endl;
391 // A condition variable will free the mutex it is given, then do a non-
392 // intensive block until 'notify' is called on it. In this case, since we
393 // don't ever call cv.notify, this should be a non-processor intensive version
396 std::condition_variable cv;
397 std::unique_lock<std::mutex> lock(blocker);
400 }catch(OCException& e)