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 static void printUsage()
46 std::cout << "Usage roomclient <0|1>" << std::endl;
47 std::cout<<"connectivityType: Default" << std::endl;
48 std::cout << "connectivityType 0: IP" << std::endl;
50 // Forward declaration
51 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
52 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
54 void printRoomRepresentation(const OCRepresentation& rep)
56 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
58 if(rep.hasAttribute("name"))
60 std::cout << "\tRoom name: " << rep.getValue<std::string>("name") << std::endl;
63 std::vector<OCRepresentation> children = rep.getChildren();
65 for(auto oit = children.begin(); oit != children.end(); ++oit)
67 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
68 if(oit->getUri().find("light") != std::string::npos)
70 if(oit->hasAttribute("state") && oit->hasAttribute("color"))
72 std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
73 std::cout << "\t\tcolor:" << oit->getValue<int>("color") << std::endl;
76 else if(oit->getUri().find("fan") != std::string::npos)
78 if(oit->hasAttribute("state") && oit->hasAttribute("speed"))
80 std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
81 std::cout << "\t\tspeed:" << oit->getValue<int>("speed") << std::endl;
87 // callback handler on GET request
88 void onGet(const HeaderOptions& /*headerOptions*/,
89 const OCRepresentation& rep, const int eCode)
91 if(eCode == SUCCESS_RESPONSE)
93 std::cout << "GET request was successful" << std::endl;
95 printRoomRepresentation(rep);
97 putRoomRepresentation(curResource);
101 std::cout << "onGET Response error: " << eCode << std::endl;
106 void onGet1(const HeaderOptions& /*headerOptions*/,
107 const OCRepresentation& rep, const int eCode)
109 if(eCode == SUCCESS_RESPONSE)
111 std::cout << "GET request was successful" << std::endl;
113 printRoomRepresentation(rep);
117 std::cout << "onGET Response error: " << eCode << std::endl;
122 // Local function to get representation of room resource
123 void getRoomRepresentation(std::shared_ptr<OCResource> resource,
124 std::string interface, GetCallback getCallback)
128 std::cout << "Getting room representation on: "<< interface << std::endl;
130 resource->get("core.room", interface, QueryParamsMap(), getCallback);
134 // Local function to put a different state for this resource
135 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
139 OCRepresentation rep;
140 std::cout << "Putting room representation on: " << BATCH_INTERFACE << std::endl;
144 rep.setValue("state", state);
145 rep.setValue("speed", speed);
147 // Invoke resource's pit API with attribute map, query map and the callback parameter
148 resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
152 // callback handler on PUT request
153 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
155 if(eCode == SUCCESS_RESPONSE)
157 std::cout << "PUT request was successful" << std::endl;
159 printRoomRepresentation(rep);
161 getRoomRepresentation(curResource, DEFAULT_INTERFACE, onGet1);
165 std::cout << "onPut Response error: " << eCode << std::endl;
170 void receivedPlatformInfo(const OCRepresentation& rep)
172 std::cout << "\nPlatform Information received ---->\n";
174 std::string values[] =
176 "pi", "Platform ID ",
177 "mnmn", "Manufacturer name ",
178 "mnml", "Manufacturer url ",
179 "mnmo", "Manufacturer Model No ",
180 "mndt", "Manufactured Date ",
181 "mnpv", "Manufacturer Platform Version ",
182 "mnos", "Manufacturer OS version ",
183 "mnhw", "Manufacturer hardware version ",
184 "mnfv", "Manufacturer firmware version ",
185 "mnsl", "Manufacturer support url ",
186 "st", "Manufacturer system time "
189 for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]); i += 2)
191 if(rep.getValue(values[i], value))
193 std::cout << values[i + 1] << " : "<< value << std::endl;
198 void receivedDeviceInfo(const OCRepresentation& rep)
200 std::cout << "\nDevice Information received ---->\n";
202 std::string values[] =
206 "lcv", "Spec version url ",
207 "dmv", "Data Model Model ",
210 for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]); i += 2)
212 if (rep.getValue(values[i], value))
214 std::cout << values[i + 1] << " : " << value << std::endl;
219 // Callback to found resources
220 void foundResource(std::shared_ptr<OCResource> resource)
222 std::lock_guard<std::mutex> lock(resourceLock);
225 std::cout << "Found another resource, ignoring"<<std::endl;
229 std::string resourceURI;
230 std::string hostAddress;
231 std::string platformDiscoveryURI = "/oic/p";
232 std::string deviceDiscoveryURI = "/oic/d";
235 // Do some operations with resource object.
238 std::cout<<"DISCOVERED Resource:"<<std::endl;
239 // Get the resource URI
240 resourceURI = resource->uri();
241 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
243 // Get the resource host address
244 hostAddress = resource->host();
245 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
247 // Get the resource types
248 std::cout << "\tList of resource types: " << std::endl;
249 for(auto &resourceTypes : resource->getResourceTypes())
251 std::cout << "\t\t" << resourceTypes << std::endl;
254 // Get the resource interfaces
255 std::cout << "\tList of resource interfaces: " << std::endl;
256 for(auto &resourceInterfaces : resource->getResourceInterfaces())
258 std::cout << "\t\t" << resourceInterfaces << std::endl;
262 std::cout << "Querying for platform information... " << std::endl;
264 ret = OCPlatform::getPlatformInfo(resource->host(), platformDiscoveryURI,
265 resource->connectivityType(),
266 &receivedPlatformInfo);
268 if (ret == OC_STACK_OK)
270 std::cout << "Get platform information is done." << std::endl;
274 std::cout << "Get platform information failed." << std::endl;
277 std::cout << "Querying for device information... " << std::endl;
279 ret = OCPlatform::getDeviceInfo(resource->host(), deviceDiscoveryURI,
280 resource->connectivityType(),
281 &receivedDeviceInfo);
282 if (ret == OC_STACK_OK)
284 std::cout << "Getting device information is done." << std::endl;
288 std::cout << "Getting device information failed." << std::endl;
291 if(resourceURI == "/a/room")
293 curResource = resource;
294 // Call a local function which will internally invoke get API on the resource pointer
295 getRoomRepresentation(resource, BATCH_INTERFACE, onGet);
300 // Resource is invalid
301 std::cout << "Resource is invalid" << std::endl;
305 catch(std::exception& e)
307 std::cerr << "Exception caught in Found Resource: "<< e.what() <<std::endl;
311 int main(int argc, char* argv[]) {
313 std::ostringstream requestURI;
315 OCConnectivityType connectivityType = CT_ADAPTER_IP;
320 std::size_t inputValLen;
321 int optionSelected = std::stoi(argv[1], &inputValLen);
323 if(inputValLen == strlen(argv[1]))
325 if(optionSelected == 0)
327 std::cout << "Using IP."<< std::endl;
328 connectivityType = CT_ADAPTER_IP;
332 std::cout << "Invalid connectivity type selected. Using default IP"<< std::endl;
337 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
340 catch(std::exception&)
342 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
347 std::cout << "Default input argument. Using IP as Default connectivity type" << std::endl;
351 // Create PlatformConfig object
353 OC::ServiceType::InProc,
354 OC::ModeType::Client,
357 OC::QualityOfService::LowQos
360 OCPlatform::Configure(cfg);
364 // Find all resources
365 requestURI << OC_RSRVD_WELL_KNOWN_URI;
367 OCPlatform::findResource("", requestURI.str(), connectivityType, &foundResource);
368 std::cout<< "Finding Resource... " <<std::endl;
370 // A condition variable will free the mutex it is given, then do a non-
371 // intensive block until 'notify' is called on it. In this case, since we
372 // don't ever call cv.notify, this should be a non-processor intensive version
375 std::condition_variable cv;
376 std::unique_lock<std::mutex> lock(blocker);
379 }catch(OCException& e)
381 oclog() << "Exception in main: "<< e.what();