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 resourceLock;
44 // Forward declaration
45 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
46 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
48 void printRoomRepresentation(const OCRepresentation& rep)
50 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
52 if(rep.hasAttribute("name"))
54 std::cout << "\tRoom name: " << rep.getValue<std::string>("name") << std::endl;
57 std::vector<OCRepresentation> children = rep.getChildren();
59 for(auto oit = children.begin(); oit != children.end(); ++oit)
61 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
62 if(oit->getUri().find("light") != std::string::npos)
64 if(oit->hasAttribute("state") && oit->hasAttribute("color"))
66 std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
67 std::cout << "\t\tcolor:" << oit->getValue<int>("color") << std::endl;
70 else if(oit->getUri().find("fan") != std::string::npos)
72 if(oit->hasAttribute("state") && oit->hasAttribute("speed"))
74 std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
75 std::cout << "\t\tspeed:" << oit->getValue<int>("speed") << std::endl;
81 // callback handler on GET request
82 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
84 if(eCode == SUCCESS_RESPONSE)
86 std::cout << "GET request was successful" << std::endl;
88 printRoomRepresentation(rep);
90 putRoomRepresentation(curResource);
94 std::cout << "onGET Response error: " << eCode << std::endl;
99 void onGet1(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
101 if(eCode == SUCCESS_RESPONSE)
103 std::cout << "GET request was successful" << std::endl;
105 printRoomRepresentation(rep);
109 std::cout << "onGET Response error: " << eCode << std::endl;
114 // Local function to get representation of room resource
115 void getRoomRepresentation(std::shared_ptr<OCResource> resource,
116 std::string interface, GetCallback getCallback)
120 std::cout << "Getting room representation on: "<< interface << std::endl;
122 resource->get("core.room", interface, QueryParamsMap(), getCallback);
126 // Local function to put a different state for this resource
127 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
131 OCRepresentation rep;
132 std::cout << "Putting room representation on: " << BATCH_INTERFACE << std::endl;
136 rep.setValue("state", state);
137 rep.setValue("speed", speed);
139 // Invoke resource's pit API with attribute map, query map and the callback parameter
140 resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
144 // callback handler on PUT request
145 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
147 if(eCode == SUCCESS_RESPONSE)
149 std::cout << "PUT request was successful" << std::endl;
151 printRoomRepresentation(rep);
153 getRoomRepresentation(curResource, DEFAULT_INTERFACE, onGet1);
157 std::cout << "onPut Response error: " << eCode << std::endl;
162 // Callback to found resources
163 void foundResource(std::shared_ptr<OCResource> resource)
165 std::lock_guard<std::mutex> lock(resourceLock);
168 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, BATCH_INTERFACE, onGet);
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 std::ostringstream requestURI;
226 OCConnectivityType connectivityType = CT_DEFAULT;
231 std::size_t inputValLen;
232 int optionSelected = std::stoi(argv[1], &inputValLen);
234 if(inputValLen == strlen(argv[1]))
236 if(optionSelected == 0)
238 connectivityType = CT_ADAPTER_IP;
242 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
247 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
250 catch(std::exception& e)
252 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
257 std::cout << "Usage roomclient 0" << std::endl;
258 std::cout<<"connectivityType: Default" << std::endl;
259 std::cout << "connectivityType 0: IP" << std::endl;
262 // Create PlatformConfig object
264 OC::ServiceType::InProc,
265 OC::ModeType::Client,
268 OC::QualityOfService::LowQos
271 OCPlatform::Configure(cfg);
275 // Find all resources
276 requestURI << OC_MULTICAST_DISCOVERY_URI;
278 OCPlatform::findResource("", requestURI.str(), connectivityType, &foundResource);
279 std::cout<< "Finding Resource... " <<std::endl;
281 // A condition variable will free the mutex it is given, then do a non-
282 // intensive block until 'notify' is called on it. In this case, since we
283 // don't ever call cv.notify, this should be a non-processor intensive version
286 std::condition_variable cv;
287 std::unique_lock<std::mutex> lock(blocker);
290 }catch(OCException& e)
292 oclog() << "Exception in main: "<< e.what();