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.
26 #include "OCPlatform.h"
31 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
33 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
43 Light() : m_state(false), m_power(0), m_name("")
56 void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
57 const int& eCode, const int& sequenceNumber)
59 if(eCode == SUCCESS_RESPONSE)
61 std::cout << "OBSERVE RESULT:"<<std::endl;
62 if(sequenceNumber == 0)
64 std::cout << "\tObserve Registration Confirmed: "<< endl;
66 else if (sequenceNumber == 1)
68 std::cout << "\tObserve Cancel Confirmed: "<< endl;
70 std::cout << "DONE"<<std::endl;
75 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
78 rep.getValue("state", mylight.m_state);
79 rep.getValue("power", mylight.m_power);
80 rep.getValue("name", mylight.m_name);
82 std::cout << "\tstate: " << mylight.m_state << std::endl;
83 std::cout << "\tpower: " << mylight.m_power << std::endl;
84 std::cout << "\tname: " << mylight.m_name << std::endl;
86 if(observe_count() > 30)
88 std::cout<<"Cancelling Observe..."<<std::endl;
89 OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
91 std::cout << "Cancel result: "<< result << " waiting for confirmation ..." <<std::endl;
96 std::cout << "onObserve Response error: " << eCode << std::endl;
101 void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
103 if(eCode == SUCCESS_RESPONSE)
105 std::cout << "POST request was successful" << std::endl;
107 if(rep.hasAttribute("createduri"))
109 std::cout << "\tUri of the created resource: "
110 << rep.getValue<std::string>("createduri") << std::endl;
114 rep.getValue("state", mylight.m_state);
115 rep.getValue("power", mylight.m_power);
116 rep.getValue("name", mylight.m_name);
118 std::cout << "\tstate: " << mylight.m_state << std::endl;
119 std::cout << "\tpower: " << mylight.m_power << std::endl;
120 std::cout << "\tname: " << mylight.m_name << std::endl;
123 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
124 std::cout << endl << "Observe is used." << endl << endl;
125 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
126 std::cout << endl << "ObserveAll is used." << endl << endl;
128 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve,
129 OC::QualityOfService::HighQos);
134 std::cout << "onPost Response error: " << eCode << std::endl;
139 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
141 if(eCode == SUCCESS_RESPONSE)
143 std::cout << "POST request was successful" << std::endl;
145 if(rep.hasAttribute("createduri"))
147 std::cout << "\tUri of the created resource: "
148 << rep.getValue<std::string>("createduri") << std::endl;
152 rep.getValue("state", mylight.m_state);
153 rep.getValue("power", mylight.m_power);
154 rep.getValue("name", mylight.m_name);
156 std::cout << "\tstate: " << mylight.m_state << std::endl;
157 std::cout << "\tpower: " << mylight.m_power << std::endl;
158 std::cout << "\tname: " << mylight.m_name << std::endl;
161 OCRepresentation rep2;
163 std::cout << "Posting light representation..."<<std::endl;
165 mylight.m_state = true;
166 mylight.m_power = 55;
168 rep2.setValue("state", mylight.m_state);
169 rep2.setValue("power", mylight.m_power);
171 curResource->post(rep2, QueryParamsMap(), &onPost2, OC::QualityOfService::HighQos);
175 std::cout << "onPost Response error: " << eCode << std::endl;
180 // Local function to put a different state for this resource
181 void postLightRepresentation(std::shared_ptr<OCResource> resource)
185 OCRepresentation rep;
187 std::cout << "Posting light representation..."<<std::endl;
189 mylight.m_state = false;
190 mylight.m_power = 105;
192 rep.setValue("state", mylight.m_state);
193 rep.setValue("power", mylight.m_power);
195 // Invoke resource's post API with rep, query map and the callback parameter
196 resource->post(rep, QueryParamsMap(), &onPost, OC::QualityOfService::HighQos);
200 // callback handler on PUT request
201 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
203 if(eCode == SUCCESS_RESPONSE)
205 std::cout << "PUT request was successful" << std::endl;
207 rep.getValue("state", mylight.m_state);
208 rep.getValue("power", mylight.m_power);
209 rep.getValue("name", mylight.m_name);
211 std::cout << "\tstate: " << mylight.m_state << std::endl;
212 std::cout << "\tpower: " << mylight.m_power << std::endl;
213 std::cout << "\tname: " << mylight.m_name << std::endl;
215 postLightRepresentation(curResource);
219 std::cout << "onPut Response error: " << eCode << std::endl;
224 // Local function to put a different state for this resource
225 void putLightRepresentation(std::shared_ptr<OCResource> resource)
229 OCRepresentation rep;
231 std::cout << "Putting light representation..."<<std::endl;
233 mylight.m_state = true;
234 mylight.m_power = 15;
236 rep.setValue("state", mylight.m_state);
237 rep.setValue("power", mylight.m_power);
239 // Invoke resource's put API with rep, query map and the callback parameter
240 resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
244 // Callback handler on GET request
245 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
247 if(eCode == SUCCESS_RESPONSE)
249 std::cout << "GET request was successful" << std::endl;
250 std::cout << "Resource URI: " << rep.getUri() << std::endl;
252 rep.getValue("state", mylight.m_state);
253 rep.getValue("power", mylight.m_power);
254 rep.getValue("name", mylight.m_name);
256 std::cout << "\tstate: " << mylight.m_state << std::endl;
257 std::cout << "\tpower: " << mylight.m_power << std::endl;
258 std::cout << "\tname: " << mylight.m_name << std::endl;
260 putLightRepresentation(curResource);
264 std::cout << "onGET Response error: " << eCode << std::endl;
269 // Local function to get representation of light resource
270 void getLightRepresentation(std::shared_ptr<OCResource> resource)
274 std::cout << "Getting Light Representation..."<<std::endl;
275 // Invoke resource's get API with the callback parameter
278 resource->get(test, &onGet,OC::QualityOfService::HighQos);
282 // Callback to found resources
283 void foundResource(std::shared_ptr<OCResource> resource)
287 std::cout << "Found another resource, ignoring"<<std::endl;
290 std::string resourceURI;
291 std::string hostAddress;
294 // Do some operations with resource object.
297 std::cout<<"DISCOVERED Resource:"<<std::endl;
298 // Get the resource URI
299 resourceURI = resource->uri();
300 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
302 // Get the resource host address
303 hostAddress = resource->host();
304 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
306 // Get the resource types
307 std::cout << "\tList of resource types: " << std::endl;
308 for(auto &resourceTypes : resource->getResourceTypes())
310 std::cout << "\t\t" << resourceTypes << std::endl;
313 // Get the resource interfaces
314 std::cout << "\tList of resource interfaces: " << std::endl;
315 for(auto &resourceInterfaces : resource->getResourceInterfaces())
317 std::cout << "\t\t" << resourceInterfaces << std::endl;
320 if(resourceURI == "/a/light")
322 curResource = resource;
324 // Call a local function which will internally invoke get API on the resource pointer
325 getLightRepresentation(resource);
330 // Resource is invalid
331 std::cout << "Resource is invalid" << std::endl;
335 catch(std::exception& e)
343 std::cout << std::endl;
344 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
345 std::cout << " ObserveType : 1 - Observe" << std::endl;
346 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
349 int main(int argc, char* argv[]) {
352 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
356 int value = atoi(argv[1]);
358 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
360 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
362 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
370 // Create PlatformConfig object
372 OC::ServiceType::InProc,
373 OC::ModeType::Client,
376 OC::QualityOfService::LowQos
379 // Create a OCPlatform instance.
380 // Note: Platform creation is synchronous call.
384 OCPlatform platform(cfg);
385 std::cout << "Created Platform..."<<std::endl;
386 // Find all resources
387 platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource,
388 OC::QualityOfService::LowQos);
389 std::cout<< "Finding Resource... " <<std::endl;
395 }catch(OCException& e)