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 std::mutex curResourceLock;
37 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
47 Light() : m_state(false), m_power(0), m_name("")
60 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
61 const int& eCode, const int& sequenceNumber)
63 if(eCode == SUCCESS_RESPONSE)
65 std::cout << "OBSERVE RESULT:"<<std::endl;
66 if(sequenceNumber == (int) ObserveAction::ObserveRegister)
68 std::cout << "\tObserve Registration Confirmed: "<< endl;
70 else if (sequenceNumber == (int) ObserveAction::ObserveUnregister)
72 std::cout << "\tObserve Cancel Confirmed: "<< endl;
74 std::cout << "DONE"<<std::endl;
79 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
82 rep.getValue("state", mylight.m_state);
83 rep.getValue("power", mylight.m_power);
84 rep.getValue("name", mylight.m_name);
86 std::cout << "\tstate: " << mylight.m_state << std::endl;
87 std::cout << "\tpower: " << mylight.m_power << std::endl;
88 std::cout << "\tname: " << mylight.m_name << std::endl;
90 if(observe_count() > 30)
92 std::cout<<"Cancelling Observe..."<<std::endl;
93 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
95 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
100 std::cout << "onObserve Response error: " << eCode << std::endl;
105 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
107 if(eCode == SUCCESS_RESPONSE)
109 std::cout << "POST request was successful" << std::endl;
111 if(rep.hasAttribute("createduri"))
113 std::cout << "\tUri of the created resource: "
114 << rep.getValue<std::string>("createduri") << std::endl;
118 rep.getValue("state", mylight.m_state);
119 rep.getValue("power", mylight.m_power);
120 rep.getValue("name", mylight.m_name);
122 std::cout << "\tstate: " << mylight.m_state << std::endl;
123 std::cout << "\tpower: " << mylight.m_power << std::endl;
124 std::cout << "\tname: " << mylight.m_name << std::endl;
127 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
128 std::cout << endl << "Observe is used." << endl << endl;
129 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
130 std::cout << endl << "ObserveAll is used." << endl << endl;
132 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
133 OC::QualityOfService::HighQos);
138 std::cout << "onPost2 Response error: " << eCode << std::endl;
143 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
145 if(eCode == SUCCESS_RESPONSE)
147 std::cout << "POST request was successful" << std::endl;
149 if(rep.hasAttribute("createduri"))
151 std::cout << "\tUri of the created resource: "
152 << rep.getValue<std::string>("createduri") << std::endl;
156 rep.getValue("state", mylight.m_state);
157 rep.getValue("power", mylight.m_power);
158 rep.getValue("name", mylight.m_name);
160 std::cout << "\tstate: " << mylight.m_state << std::endl;
161 std::cout << "\tpower: " << mylight.m_power << std::endl;
162 std::cout << "\tname: " << mylight.m_name << std::endl;
165 OCRepresentation rep2;
167 std::cout << "Posting light representation..."<<std::endl;
169 mylight.m_state = true;
170 mylight.m_power = 55;
172 rep2.setValue("state", mylight.m_state);
173 rep2.setValue("power", mylight.m_power);
175 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
179 std::cout << "onPost Response error: " << eCode << std::endl;
184 // Local function to put a different state for this resource
185 void postLightRepresentation(std::shared_ptr<OCResource> resource)
189 OCRepresentation rep;
191 std::cout << "Posting light representation..."<<std::endl;
193 mylight.m_state = false;
194 mylight.m_power = 105;
196 rep.setValue("state", mylight.m_state);
197 rep.setValue("power", mylight.m_power);
199 // Invoke resource's post API with rep, query map and the callback parameter
200 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
204 // callback handler on PUT request
205 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
207 if(eCode == SUCCESS_RESPONSE)
209 std::cout << "PUT request was successful" << std::endl;
211 rep.getValue("state", mylight.m_state);
212 rep.getValue("power", mylight.m_power);
213 rep.getValue("name", mylight.m_name);
215 std::cout << "\tstate: " << mylight.m_state << std::endl;
216 std::cout << "\tpower: " << mylight.m_power << std::endl;
217 std::cout << "\tname: " << mylight.m_name << std::endl;
219 postLightRepresentation(curResource);
223 std::cout << "onPut Response error: " << eCode << std::endl;
228 // Local function to put a different state for this resource
229 void putLightRepresentation(std::shared_ptr<OCResource> resource)
233 OCRepresentation rep;
235 std::cout << "Putting light representation..."<<std::endl;
237 mylight.m_state = true;
238 mylight.m_power = 15;
240 rep.setValue("state", mylight.m_state);
241 rep.setValue("power", mylight.m_power);
243 // Invoke resource's put API with rep, query map and the callback parameter
244 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
248 // Callback handler on GET request
249 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
251 if(eCode == SUCCESS_RESPONSE)
253 std::cout << "GET request was successful" << std::endl;
254 std::cout << "Resource URI: " << rep.getUri() << std::endl;
256 rep.getValue("state", mylight.m_state);
257 rep.getValue("power", mylight.m_power);
258 rep.getValue("name", mylight.m_name);
260 std::cout << "\tstate: " << mylight.m_state << std::endl;
261 std::cout << "\tpower: " << mylight.m_power << std::endl;
262 std::cout << "\tname: " << mylight.m_name << std::endl;
264 putLightRepresentation(curResource);
268 std::cout << "onGET Response error: " << eCode << std::endl;
273 // Local function to get representation of light resource
274 void getLightRepresentation(std::shared_ptr<OCResource> resource)
278 std::cout << "Getting Light Representation..."<<std::endl;
279 // Invoke resource's get API with the callback parameter
282 resource->get(test, &onGet,OC::QualityOfService::HighQos);
286 // Callback to found resources
287 void foundResource(std::shared_ptr<OCResource> resource)
289 std::lock_guard<std::mutex> lock(curResourceLock);
292 std::cout << "Found another resource, ignoring"<<std::endl;
296 std::string resourceURI;
297 std::string hostAddress;
300 // Do some operations with resource object.
303 std::cout<<"DISCOVERED Resource:"<<std::endl;
304 // Get the resource URI
305 resourceURI = resource->uri();
306 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
308 // Get the resource host address
309 hostAddress = resource->host();
310 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
312 // Get the resource types
313 std::cout << "\tList of resource types: " << std::endl;
314 for(auto &resourceTypes : resource->getResourceTypes())
316 std::cout << "\t\t" << resourceTypes << std::endl;
319 // Get the resource interfaces
320 std::cout << "\tList of resource interfaces: " << std::endl;
321 for(auto &resourceInterfaces : resource->getResourceInterfaces())
323 std::cout << "\t\t" << resourceInterfaces << std::endl;
326 if(resourceURI == "/a/light")
328 curResource = resource;
330 // Call a local function which will internally invoke get API on the resource pointer
331 getLightRepresentation(resource);
336 // Resource is invalid
337 std::cout << "Resource is invalid" << std::endl;
341 catch(std::exception& e)
349 std::cout << std::endl;
350 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
351 std::cout << " ObserveType : 1 - Observe" << std::endl;
352 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
355 int main(int argc, char* argv[]) {
358 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
362 int value = atoi(argv[1]);
364 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
366 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
368 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
376 // Create PlatformConfig object
378 OC::ServiceType::InProc,
379 OC::ModeType::Client,
382 OC::QualityOfService::LowQos
385 OCPlatform::Configure(cfg);
389 // Find all resources
390 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource,
391 OC::QualityOfService::LowQos);
392 std::cout<< "Finding Resource... " <<std::endl;
394 // A condition variable will free the mutex it is given, then do a non-
395 // intensive block until 'notify' is called on it. In this case, since we
396 // don't ever call cv.notify, this should be a non-processor intensive version
399 std::condition_variable cv;
400 std::unique_lock<std::mutex> lock(blocker);
403 }catch(OCException& e)