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 // garageclient.cpp is the client program for garageserver.cpp, which
22 // uses different representation in OCRepresention.
27 #include <condition_variable>
28 #include "OCPlatform.h"
33 std::shared_ptr<OCResource> curResource;
34 std::mutex curResourceLock;
36 static void printUsage()
38 std::cout<<"Usage: garageclient <0|1> \n";
39 std::cout<<"ConnectivityType: Default IP\n";
40 std::cout<<"ConnectivityType 0: IP \n";
49 std::vector<bool> m_lightStates;
50 std::vector<int> m_lightPowers;
51 OCRepresentation m_lightRep;
52 std::vector<OCRepresentation> m_reps;
53 std::vector<std::vector<int>> m_hingeStates;
55 Garage() : m_state(false), m_name("")
62 void printRepresentation(const OCRepresentation& rep)
65 // Check if attribute "name" exists, and then getValue
66 if(rep.hasAttribute("name"))
68 myGarage.m_name = rep["name"];
70 std::cout << "\tname: " << myGarage.m_name << std::endl;
72 // You can directly try to get the value. this function
73 // return false if there is no attribute "state"
74 if(!rep.getValue("state", myGarage.m_state))
76 std::cout << "Attribute state doesn't exist in the representation\n";
78 std::cout << "\tstate: " << myGarage.m_state << std::endl;
80 OCRepresentation rep2 = rep;
82 std::cout << "Number of attributes in rep2: "
83 << rep2.numberOfAttributes() << std::endl;
85 if(rep2.erase("name"))
87 std::cout << "attribute: name, was removed successfully from rep2.\n";
90 std::cout << "Number of attributes in rep2: "
91 << rep2.numberOfAttributes() << std::endl;
94 if(rep.isNULL("nullAttribute"))
96 std::cout << "\tnullAttribute is null." << std::endl;
100 std::cout << "\tnullAttribute is not null." << std::endl;
103 myGarage.m_lightRep = rep["light"];
105 myGarage.m_lightStates = myGarage.m_lightRep["states"];
106 myGarage.m_lightPowers = myGarage.m_lightRep["powers"];
108 std::cout << "\tlightRep: states: ";
111 for(auto state: myGarage.m_lightStates)
120 std::cout << "," << state;
124 std::cout << std::endl;
125 std::cout << "\tlightRep: powers: ";
127 for(auto power: myGarage.m_lightPowers)
136 std::cout << "," << power;
139 std::cout << std::endl;
141 // Get vector of representations
142 myGarage.m_reps = rep["reps"];
145 for(auto& rep : myGarage.m_reps)
147 for(auto& attribute : rep)
149 std::cout<< "\treps["<<ct<<"]."<<attribute.attrname()<<":"
150 << attribute.type()<<" with value " <<attribute.getValueToString() <<std::endl;
155 std::cout << "\tjson: " << rep["json"] << std::endl;
156 myGarage.m_hingeStates = rep["hinges"];
158 std::cout<< "\tHinge parameter is type: " << rep["hinges"].type() << " with depth "<<
159 rep["hinges"].depth() << " and a base type of "<< rep["hinges"].base_type()<<std::endl;
163 // callback handler on PUT request
164 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
166 if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
168 std::cout << "PUT request was successful" << std::endl;
170 printRepresentation(rep);
174 std::cout << "onPut Response error: " << eCode << std::endl;
179 // Local function to put a different state for this resource
180 void putLightRepresentation(std::shared_ptr<OCResource> resource)
184 OCRepresentation rep;
186 std::cout << "Putting light representation..."<<std::endl;
188 myGarage.m_state = true;
190 rep["state"] = myGarage.m_state;
192 // Create QueryParameters Map and add query params (if any)
193 QueryParamsMap queryParamsMap;
195 // Invoke resource's pit API with rep, query map and the callback parameter
196 resource->put(rep, queryParamsMap, &onPut);
200 // Callback handler on GET request
201 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
203 if (eCode == OC_STACK_OK)
205 std::cout << "GET request was successful" << std::endl;
206 std::cout << "Resource URI: " << rep.getUri() << std::endl;
208 printRepresentation(rep);
210 putLightRepresentation(curResource);
214 std::cout << "onGET Response error: " << eCode << std::endl;
219 // Local function to get representation of light resource
220 void getLightRepresentation(std::shared_ptr<OCResource> resource)
224 std::cout << "Getting Light Representation..."<<std::endl;
225 // Invoke resource's get API with the callback parameter
228 resource->get(test, &onGet);
232 // Callback to found resources
233 void foundResource(std::shared_ptr<OCResource> resource)
235 std::lock_guard<std::mutex> lock(curResourceLock);
238 std::cout << "Found another resource, ignoring"<<std::endl;
242 std::string resourceURI;
243 std::string hostAddress;
244 std::string platformDiscoveryURI = "/oic/p";
245 std::string deviceDiscoveryURI = "/oic/d";
249 // Do some operations with resource object.
252 std::cout<<"DISCOVERED Resource:"<<std::endl;
253 // Get the resource URI
254 resourceURI = resource->uri();
255 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
257 // Get the resource host address
258 hostAddress = resource->host();
259 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
261 // Get the resource types
262 std::cout << "\tList of resource types: " << std::endl;
263 for(auto &resourceTypes : resource->getResourceTypes())
265 std::cout << "\t\t" << resourceTypes << std::endl;
268 // Get the resource interfaces
269 std::cout << "\tList of resource interfaces: " << std::endl;
270 for(auto &resourceInterfaces : resource->getResourceInterfaces())
272 std::cout << "\t\t" << resourceInterfaces << std::endl;
276 std::cout << "Querying for platform information... " << std::endl;
278 ret = OCPlatform::getPlatformInfo("", platformDiscoveryURI, CT_ADAPTER_IP, NULL);
280 if (ret == OC_STACK_OK)
282 std::cout << "Get platform information is done." << std::endl;
286 std::cout << "Get platform information failed." << std::endl;
289 std::cout << "Querying for device information... " << std::endl;
291 ret = OCPlatform::getDeviceInfo(resource->host(), deviceDiscoveryURI, CT_ADAPTER_IP,
294 if (ret == OC_STACK_OK)
296 std::cout << "Getting device information is done." << std::endl;
300 std::cout << "Getting device information failed." << std::endl;
303 if(resourceURI == "/a/garage")
305 curResource = resource;
306 // Call a local function which will internally invoke
307 // get API on the resource pointer
308 getLightRepresentation(resource);
313 // Resource is invalid
314 std::cout << "Resource is invalid" << std::endl;
318 catch(std::exception& e)
320 std::cerr << "Exception in foundResource: "<< e.what()<<std::endl;
324 int main(int argc, char* argv[]) {
326 std::ostringstream requestURI;
328 OCConnectivityType connectivityType = CT_ADAPTER_IP;
334 std::size_t inputValLen;
335 int optionSelected = std::stoi(argv[1], &inputValLen);
337 if(inputValLen == strlen(argv[1]))
339 if(optionSelected == 0)
341 std::cout << "Using IP."<< std::endl;
342 connectivityType = CT_ADAPTER_IP;
346 std::cout << "Invalid connectivity type selected. Using default IP"
352 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
355 catch(std::exception&)
357 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
363 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
366 // Create PlatformConfig object
368 OC::ServiceType::InProc,
369 OC::ModeType::Client,
372 OC::QualityOfService::LowQos
375 OCPlatform::Configure(cfg);
378 // Find all resources
379 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.garage";
381 OCPlatform::findResource("", requestURI.str(),
382 connectivityType, &foundResource);
384 std::cout<< "Finding Resource... " <<std::endl;
386 // A condition variable will free the mutex it is given, then do a non-
387 // intensive block until 'notify' is called on it. In this case, since we
388 // don't ever call cv.notify, this should be a non-processor intensive version
391 std::condition_variable cv;
392 std::unique_lock<std::mutex> lock(blocker);
395 catch(OCException& e)
397 std::cerr << "Exception in GarageClient: "<<e.what();