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;
40 // Forward declaration
41 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
42 void onPut(const OCRepresentation& rep, const int eCode);
44 // callback handler on GET request
45 void onGet(const OCRepresentation& rep, const int eCode)
47 if(eCode == SUCCESS_RESPONSE)
49 std::cout << "GET request was successful" << std::endl;
51 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
53 std::vector<OCRepresentation> children = rep.getChildren();
55 for(auto oit = children.begin(); oit != children.end(); ++oit)
57 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
58 if(oit->getUri().find("light") != std::string::npos)
62 oit->getValue("state", state);
63 oit->getValue("color", color);
65 std::cout << "\t\tstate:" << state << std::endl;
66 std::cout << "\t\tcolor:" << color << std::endl;
68 else if(oit->getUri().find("fan") != std::string::npos)
72 oit->getValue("state", state);
73 oit->getValue("speed", speed);
75 std::cout << "\t\tstate:" << state << std::endl;
76 std::cout << "\t\tspeed:" << speed << std::endl;
80 putRoomRepresentation(curResource);
84 std::cout << "onGET Response error: " << eCode << std::endl;
89 // Local function to put a different state for this resource
90 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
95 std::cout << "Putting room representation..."<<std::endl;
99 rep.setValue("state", state);
100 rep.setValue("speed", speed);
102 // Invoke resource's pit API with attribute map, query map and the callback parameter
103 resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
107 // callback handler on PUT request
108 void onPut(const OCRepresentation& rep, const int eCode)
110 if(eCode == SUCCESS_RESPONSE)
112 std::cout << "PUT request was successful" << std::endl;
114 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
116 std::vector<OCRepresentation> children = rep.getChildren();
118 for(auto oit = children.begin(); oit != children.end(); ++oit)
120 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
121 if(oit->getUri().find("light") != std::string::npos)
125 oit->getValue("state", state);
126 oit->getValue("color", color);
127 std::cout << "\t\tstate:" << state << std::endl;
128 std::cout << "\t\tcolor:" << color << std::endl;
130 else if(oit->getUri().find("fan") != std::string::npos)
134 oit->getValue("state", state);
135 oit->getValue("speed", speed);
136 std::cout << "\t\tstate:" << state << std::endl;
137 std::cout << "\t\tspeed:" << speed << std::endl;
145 std::cout << "onPut Response error: " << eCode << std::endl;
151 // Local function to get representation of light resource
152 void getRoomRepresentation(std::shared_ptr<OCResource> resource)
156 std::cout << "Getting Room Representation..."<<std::endl;
157 // Invoke resource's get API with the callback parameter
160 resource->get("core.room", BATCH_INTERFACE, qp, &onGet);
164 // Callback to found resources
165 void foundResource(std::shared_ptr<OCResource> resource)
169 std::cout << "Found another resource, ignoring"<<std::endl;
172 std::string resourceURI;
173 std::string hostAddress;
176 // Do some operations with resource object.
179 std::cout<<"DISCOVERED Resource:"<<std::endl;
180 // Get the resource URI
181 resourceURI = resource->uri();
182 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
184 // Get the resource host address
185 hostAddress = resource->host();
186 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
188 // Get the resource types
189 std::cout << "\tList of resource types: " << std::endl;
190 for(auto &resourceTypes : resource->getResourceTypes())
192 std::cout << "\t\t" << resourceTypes << std::endl;
195 // Get the resource interfaces
196 std::cout << "\tList of resource interfaces: " << std::endl;
197 for(auto &resourceInterfaces : resource->getResourceInterfaces())
199 std::cout << "\t\t" << resourceInterfaces << std::endl;
202 if(resourceURI == "/a/room")
204 curResource = resource;
205 // Call a local function which will internally invoke get API on the resource pointer
206 getRoomRepresentation(resource);
211 // Resource is invalid
212 std::cout << "Resource is invalid" << std::endl;
216 catch(std::exception& e)
222 int main(int argc, char* argv[]) {
224 // Create PlatformConfig object
226 OC::ServiceType::InProc,
227 OC::ModeType::Client,
230 OC::QualityOfService::NonConfirmable
233 // Create a OCPlatform instance.
234 // Note: Platform creation is synchronous call.
238 OCPlatform platform(cfg);
239 std::cout << "Created Platform..."<<std::endl;
240 // Find all resources
241 platform.findResource("", "coap://224.0.1.187/oc/core", &foundResource);
242 std::cout<< "Finding Resource... " <<std::endl;
248 }catch(OCException& e)