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;
43 // Forward declaration
44 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
45 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
47 void printRoomRepresentation(const OCRepresentation& rep)
49 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
51 if(rep.hasAttribute("name"))
53 std::cout << "\tRoom name: " << rep.getValue<std::string>("name") << std::endl;
56 std::vector<OCRepresentation> children = rep.getChildren();
58 for(auto oit = children.begin(); oit != children.end(); ++oit)
60 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
61 if(oit->getUri().find("light") != std::string::npos)
63 if(oit->hasAttribute("state") && oit->hasAttribute("color"))
65 std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
66 std::cout << "\t\tcolor:" << oit->getValue<int>("color") << std::endl;
69 else if(oit->getUri().find("fan") != std::string::npos)
71 if(oit->hasAttribute("state") && oit->hasAttribute("speed"))
73 std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
74 std::cout << "\t\tspeed:" << oit->getValue<int>("speed") << std::endl;
80 // callback handler on GET request
81 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
83 if(eCode == SUCCESS_RESPONSE)
85 std::cout << "GET request was successful" << std::endl;
87 printRoomRepresentation(rep);
89 putRoomRepresentation(curResource);
93 std::cout << "onGET Response error: " << eCode << std::endl;
98 void onGet1(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
100 if(eCode == SUCCESS_RESPONSE)
102 std::cout << "GET request was successful" << std::endl;
104 printRoomRepresentation(rep);
108 std::cout << "onGET Response error: " << eCode << std::endl;
113 // Local function to get representation of room resource
114 void getRoomRepresentation(std::shared_ptr<OCResource> resource,
115 std::string interface, GetCallback getCallback)
119 std::cout << "Getting room representation on: "<< interface << std::endl;
121 resource->get("core.room", interface, QueryParamsMap(), getCallback);
125 // Local function to put a different state for this resource
126 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
130 OCRepresentation rep;
131 std::cout << "Putting room representation on: " << BATCH_INTERFACE << std::endl;
135 rep.setValue("state", state);
136 rep.setValue("speed", speed);
138 // Invoke resource's pit API with attribute map, query map and the callback parameter
139 resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
143 // callback handler on PUT request
144 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
146 if(eCode == SUCCESS_RESPONSE)
148 std::cout << "PUT request was successful" << std::endl;
150 printRoomRepresentation(rep);
152 getRoomRepresentation(curResource, DEFAULT_INTERFACE, onGet1);
156 std::cout << "onPut Response error: " << eCode << std::endl;
161 // Callback to found resources
162 void foundResource(std::shared_ptr<OCResource> resource)
166 std::cout << "Found another resource, ignoring"<<std::endl;
170 std::string resourceURI;
171 std::string hostAddress;
174 // Do some operations with resource object.
177 std::cout<<"DISCOVERED Resource:"<<std::endl;
178 // Get the resource URI
179 resourceURI = resource->uri();
180 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
182 // Get the resource host address
183 hostAddress = resource->host();
184 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
186 // Get the resource types
187 std::cout << "\tList of resource types: " << std::endl;
188 for(auto &resourceTypes : resource->getResourceTypes())
190 std::cout << "\t\t" << resourceTypes << std::endl;
193 // Get the resource interfaces
194 std::cout << "\tList of resource interfaces: " << std::endl;
195 for(auto &resourceInterfaces : resource->getResourceInterfaces())
197 std::cout << "\t\t" << resourceInterfaces << std::endl;
200 if(resourceURI == "/a/room")
202 curResource = resource;
203 // Call a local function which will internally invoke get API on the resource pointer
204 getRoomRepresentation(resource, BATCH_INTERFACE, onGet);
209 // Resource is invalid
210 std::cout << "Resource is invalid" << std::endl;
214 catch(std::exception& e)
220 int main(int argc, char* argv[]) {
222 // Create PlatformConfig object
224 OC::ServiceType::InProc,
225 OC::ModeType::Client,
228 OC::QualityOfService::LowQos
231 OCPlatform::Configure(cfg);
235 // Find all resources
236 OCPlatform::findResource("", "coap://224.0.1.187/oc/core", &foundResource);
237 std::cout<< "Finding Resource... " <<std::endl;
239 // A condition variable will free the mutex it is given, then do a non-
240 // intensive block until 'notify' is called on it. In this case, since we
241 // don't ever call cv.notify, this should be a non-processor intensive version
244 std::condition_variable cv;
245 std::unique_lock<std::mutex> lock(blocker);
248 }catch(OCException& e)