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;
246 // Do some operations with resource object.
249 std::cout<<"DISCOVERED Resource:"<<std::endl;
250 // Get the resource URI
251 resourceURI = resource->uri();
252 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
254 // Get the resource host address
255 hostAddress = resource->host();
256 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
258 // Get the resource types
259 std::cout << "\tList of resource types: " << std::endl;
260 for(auto &resourceTypes : resource->getResourceTypes())
262 std::cout << "\t\t" << resourceTypes << std::endl;
265 // Get the resource interfaces
266 std::cout << "\tList of resource interfaces: " << std::endl;
267 for(auto &resourceInterfaces : resource->getResourceInterfaces())
269 std::cout << "\t\t" << resourceInterfaces << std::endl;
272 if(resourceURI == "/a/garage")
274 curResource = resource;
275 // Call a local function which will internally invoke
276 // get API on the resource pointer
277 getLightRepresentation(resource);
282 // Resource is invalid
283 std::cout << "Resource is invalid" << std::endl;
287 catch(std::exception& e)
289 std::cerr << "Exception in foundResource: "<< e.what()<<std::endl;
293 int main(int argc, char* argv[]) {
295 std::ostringstream requestURI;
297 OCConnectivityType connectivityType = CT_ADAPTER_IP;
303 std::size_t inputValLen;
304 int optionSelected = std::stoi(argv[1], &inputValLen);
306 if(inputValLen == strlen(argv[1]))
308 if(optionSelected == 0)
310 std::cout << "Using IP."<< std::endl;
311 connectivityType = CT_ADAPTER_IP;
315 std::cout << "Invalid connectivity type selected. Using default IP"
321 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
324 catch(std::exception&)
326 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
332 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
335 // Create PlatformConfig object
337 OC::ServiceType::InProc,
338 OC::ModeType::Client,
341 OC::QualityOfService::LowQos
344 OCPlatform::Configure(cfg);
347 // Find all resources
348 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.garage";
350 OCPlatform::findResource("", requestURI.str(),
351 connectivityType, &foundResource);
353 std::cout<< "Finding Resource... " <<std::endl;
355 // A condition variable will free the mutex it is given, then do a non-
356 // intensive block until 'notify' is called on it. In this case, since we
357 // don't ever call cv.notify, this should be a non-processor intensive version
360 std::condition_variable cv;
361 std::unique_lock<std::mutex> lock(blocker);
364 catch(OCException& e)
366 std::cerr << "Exception in GarageClient: "<<e.what();