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 std::shared_ptr<OCResource> curResource;
35 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
45 Light() : m_state(false), m_power(0), m_name("")
58 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
59 const int& eCode, const int& sequenceNumber)
61 if(eCode == OC_STACK_OK)
63 std::cout << "OBSERVE RESULT:"<<std::endl;
64 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
66 rep.getValue("state", mylight.m_state);
67 rep.getValue("power", mylight.m_power);
68 rep.getValue("name", mylight.m_name);
70 std::cout << "\tstate: " << mylight.m_state << std::endl;
71 std::cout << "\tpower: " << mylight.m_power << std::endl;
72 std::cout << "\tname: " << mylight.m_name << std::endl;
74 if(observe_count() > 30)
76 std::cout<<"Cancelling Observe..."<<std::endl;
77 OCStackResult result = curResource->cancelObserve();
79 std::cout << "Cancel result: "<< result <<std::endl;
81 std::cout << "DONE"<<std::endl;
87 std::cout << "onObserve Response error: " << eCode << std::endl;
92 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
94 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
96 std::cout << "POST request was successful" << std::endl;
98 if(rep.hasAttribute("createduri"))
100 std::cout << "\tUri of the created resource: "
101 << rep.getValue<std::string>("createduri") << std::endl;
105 rep.getValue("state", mylight.m_state);
106 rep.getValue("power", mylight.m_power);
107 rep.getValue("name", mylight.m_name);
109 std::cout << "\tstate: " << mylight.m_state << std::endl;
110 std::cout << "\tpower: " << mylight.m_power << std::endl;
111 std::cout << "\tname: " << mylight.m_name << std::endl;
114 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
115 std::cout << endl << "Observe is used." << endl << endl;
116 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
117 std::cout << endl << "ObserveAll is used." << endl << endl;
119 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
124 std::cout << "onPost2 Response error: " << eCode << std::endl;
129 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
131 if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
133 std::cout << "POST request was successful" << std::endl;
135 if(rep.hasAttribute("createduri"))
137 std::cout << "\tUri of the created resource: "
138 << rep.getValue<std::string>("createduri") << std::endl;
142 rep.getValue("state", mylight.m_state);
143 rep.getValue("power", mylight.m_power);
144 rep.getValue("name", mylight.m_name);
146 std::cout << "\tstate: " << mylight.m_state << std::endl;
147 std::cout << "\tpower: " << mylight.m_power << std::endl;
148 std::cout << "\tname: " << mylight.m_name << std::endl;
151 OCRepresentation rep2;
153 std::cout << "Posting light representation..."<<std::endl;
155 mylight.m_state = true;
156 mylight.m_power = 55;
158 rep2.setValue("state", mylight.m_state);
159 rep2.setValue("power", mylight.m_power);
161 curResource->post(rep2, QueryParamsMap(), &onPost2);
165 std::cout << "onPost Response error: " << eCode << std::endl;
170 // Local function to put a different state for this resource
171 void postLightRepresentation(std::shared_ptr<OCResource> resource)
175 OCRepresentation rep;
177 std::cout << "Posting light representation..."<<std::endl;
179 mylight.m_state = false;
180 mylight.m_power = 105;
182 rep.setValue("state", mylight.m_state);
183 rep.setValue("power", mylight.m_power);
185 // Invoke resource's post API with rep, query map and the callback parameter
186 resource->post(rep, QueryParamsMap(), &onPost);
190 // callback handler on PUT request
191 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
193 if(eCode == OC_STACK_OK)
195 std::cout << "PUT request was successful" << std::endl;
197 rep.getValue("state", mylight.m_state);
198 rep.getValue("power", mylight.m_power);
199 rep.getValue("name", mylight.m_name);
201 std::cout << "\tstate: " << mylight.m_state << std::endl;
202 std::cout << "\tpower: " << mylight.m_power << std::endl;
203 std::cout << "\tname: " << mylight.m_name << std::endl;
205 postLightRepresentation(curResource);
209 std::cout << "onPut Response error: " << eCode << std::endl;
214 // Local function to put a different state for this resource
215 void putLightRepresentation(std::shared_ptr<OCResource> resource)
219 OCRepresentation rep;
221 std::cout << "Putting light representation..."<<std::endl;
223 mylight.m_state = true;
224 mylight.m_power = 15;
226 rep.setValue("state", mylight.m_state);
227 rep.setValue("power", mylight.m_power);
229 // Invoke resource's put API with rep, query map and the callback parameter
230 resource->put(rep, QueryParamsMap(), &onPut);
234 // Callback handler on GET request
235 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
237 if(eCode == OC_STACK_OK)
239 std::cout << "GET request was successful" << std::endl;
240 std::cout << "Resource URI: " << rep.getUri() << std::endl;
242 rep.getValue("state", mylight.m_state);
243 rep.getValue("power", mylight.m_power);
244 rep.getValue("name", mylight.m_name);
246 std::cout << "\tstate: " << mylight.m_state << std::endl;
247 std::cout << "\tpower: " << mylight.m_power << std::endl;
248 std::cout << "\tname: " << mylight.m_name << std::endl;
250 putLightRepresentation(curResource);
254 std::cout << "onGET Response error: " << eCode << std::endl;
259 // Local function to get representation of light resource
260 void getLightRepresentation(std::shared_ptr<OCResource> resource)
264 std::cout << "Getting Light Representation..."<<std::endl;
265 // Invoke resource's get API with the callback parameter
268 resource->get(test, &onGet);
272 // Callback to found resources
273 void foundResource(std::shared_ptr<OCResource> resource)
277 std::cout << "Found another resource, ignoring"<<std::endl;
280 std::string resourceURI;
281 std::string hostAddress;
284 // Do some operations with resource object.
287 std::cout<<"DISCOVERED Resource:"<<std::endl;
288 // Get the resource URI
289 resourceURI = resource->uri();
290 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
292 // Get the resource host address
293 hostAddress = resource->host();
294 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
296 // Get the resource types
297 std::cout << "\tList of resource types: " << std::endl;
298 for(auto &resourceTypes : resource->getResourceTypes())
300 std::cout << "\t\t" << resourceTypes << std::endl;
303 // Get the resource interfaces
304 std::cout << "\tList of resource interfaces: " << std::endl;
305 for(auto &resourceInterfaces : resource->getResourceInterfaces())
307 std::cout << "\t\t" << resourceInterfaces << std::endl;
310 if(resourceURI == "/a/light")
312 curResource = resource;
313 // Call a local function which will internally invoke get API on the resource pointer
314 getLightRepresentation(resource);
319 // Resource is invalid
320 std::cout << "Resource is invalid" << std::endl;
324 catch(std::exception& e)
332 std::cout << std::endl;
333 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
334 std::cout << " ObserveType : 1 - Observe" << std::endl;
335 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
338 int main(int argc, char* argv[]) {
341 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
345 int value = atoi(argv[1]);
347 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
349 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
351 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
359 // Create PlatformConfig object
361 OC::ServiceType::InProc,
362 OC::ModeType::Client,
365 OC::QualityOfService::LowQos
368 OCPlatform::Configure(cfg);
371 // makes it so that all boolean values are printed as 'true/false' in this stream
372 std::cout.setf(std::ios::boolalpha);
373 // Find all resources
374 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource);
375 std::cout<< "Finding Resource... " <<std::endl;
377 // A condition variable will free the mutex it is given, then do a non-
378 // intensive block until 'notify' is called on it. In this case, since we
379 // don't ever call cv.notify, this should be a non-processor intensive version
382 std::condition_variable cv;
383 std::unique_lock<std::mutex> lock(blocker);
386 }catch(OCException& e)