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 const int SUCCESS_RESPONSE = 0;
34 std::shared_ptr<OCResource> curResource;
35 std::mutex curResourceLock;
37 static void printUsage()
39 std::cout<<"Usage: garageclient <0|1> \n";
40 std::cout<<"ConnectivityType: Default IP\n";
41 std::cout<<"ConnectivityType 0: IP \n";
50 std::vector<bool> m_lightStates;
51 std::vector<int> m_lightPowers;
52 OCRepresentation m_lightRep;
53 std::vector<OCRepresentation> m_reps;
54 std::vector<std::vector<int>> m_hingeStates;
56 Garage() : m_state(false), m_name("")
63 void printRepresentation(const OCRepresentation& rep)
66 // Check if attribute "name" exists, and then getValue
67 if(rep.hasAttribute("name"))
69 myGarage.m_name = rep["name"];
71 std::cout << "\tname: " << myGarage.m_name << std::endl;
73 // You can directly try to get the value. this function
74 // return false if there is no attribute "state"
75 if(!rep.getValue("state", myGarage.m_state))
77 std::cout << "Attribute state doesn't exist in the representation\n";
79 std::cout << "\tstate: " << myGarage.m_state << std::endl;
81 OCRepresentation rep2 = rep;
83 std::cout << "Number of attributes in rep2: "
84 << rep2.numberOfAttributes() << std::endl;
86 if(rep2.erase("name"))
88 std::cout << "attribute: name, was removed successfully from rep2.\n";
91 std::cout << "Number of attributes in rep2: "
92 << rep2.numberOfAttributes() << std::endl;
95 if(rep.isNULL("nullAttribute"))
97 std::cout << "\tnullAttribute is null." << std::endl;
101 std::cout << "\tnullAttribute is not null." << std::endl;
104 myGarage.m_lightRep = rep["light"];
106 myGarage.m_lightStates = myGarage.m_lightRep["states"];
107 myGarage.m_lightPowers = myGarage.m_lightRep["powers"];
109 std::cout << "\tlightRep: states: ";
112 for(auto state: myGarage.m_lightStates)
121 std::cout << "," << state;
125 std::cout << std::endl;
126 std::cout << "\tlightRep: powers: ";
128 for(auto power: myGarage.m_lightPowers)
137 std::cout << "," << power;
140 std::cout << std::endl;
142 // Get vector of representations
143 myGarage.m_reps = rep["reps"];
146 for(auto& rep : myGarage.m_reps)
148 for(auto& attribute : rep)
150 std::cout<< "\treps["<<ct<<"]."<<attribute.attrname()<<":"
151 << attribute.type()<<" with value " <<attribute.getValueToString() <<std::endl;
156 std::cout << "\tjson: " << rep["json"] << std::endl;
157 myGarage.m_hingeStates = rep["hinges"];
159 std::cout<< "\tHinge parameter is type: " << rep["hinges"].type() << " with depth "<<
160 rep["hinges"].depth() << " and a base type of "<< rep["hinges"].base_type()<<std::endl;
164 // callback handler on PUT request
165 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
167 if(eCode == SUCCESS_RESPONSE)
169 std::cout << "PUT request was successful" << std::endl;
171 printRepresentation(rep);
175 std::cout << "onPut Response error: " << eCode << std::endl;
180 // Local function to put a different state for this resource
181 void putLightRepresentation(std::shared_ptr<OCResource> resource)
185 OCRepresentation rep;
187 std::cout << "Putting light representation..."<<std::endl;
189 myGarage.m_state = true;
191 rep["state"] = myGarage.m_state;
193 // Create QueryParameters Map and add query params (if any)
194 QueryParamsMap queryParamsMap;
196 // Invoke resource's pit API with rep, query map and the callback parameter
197 resource->put(rep, queryParamsMap, &onPut);
201 // Callback handler on GET request
202 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
204 if(eCode == SUCCESS_RESPONSE)
206 std::cout << "GET request was successful" << std::endl;
207 std::cout << "Resource URI: " << rep.getUri() << std::endl;
209 printRepresentation(rep);
211 putLightRepresentation(curResource);
215 std::cout << "onGET Response error: " << eCode << std::endl;
220 // Local function to get representation of light resource
221 void getLightRepresentation(std::shared_ptr<OCResource> resource)
225 std::cout << "Getting Light Representation..."<<std::endl;
226 // Invoke resource's get API with the callback parameter
229 resource->get(test, &onGet);
233 // Callback to found resources
234 void foundResource(std::shared_ptr<OCResource> resource)
236 std::lock_guard<std::mutex> lock(curResourceLock);
239 std::cout << "Found another resource, ignoring"<<std::endl;
243 std::string resourceURI;
244 std::string hostAddress;
247 // Do some operations with resource object.
250 std::cout<<"DISCOVERED Resource:"<<std::endl;
251 // Get the resource URI
252 resourceURI = resource->uri();
253 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
255 // Get the resource host address
256 hostAddress = resource->host();
257 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
259 // Get the resource types
260 std::cout << "\tList of resource types: " << std::endl;
261 for(auto &resourceTypes : resource->getResourceTypes())
263 std::cout << "\t\t" << resourceTypes << std::endl;
266 // Get the resource interfaces
267 std::cout << "\tList of resource interfaces: " << std::endl;
268 for(auto &resourceInterfaces : resource->getResourceInterfaces())
270 std::cout << "\t\t" << resourceInterfaces << std::endl;
273 if(resourceURI == "/a/garage")
275 curResource = resource;
276 // Call a local function which will internally invoke
277 // get API on the resource pointer
278 getLightRepresentation(resource);
283 // Resource is invalid
284 std::cout << "Resource is invalid" << std::endl;
288 catch(std::exception& e)
290 std::cerr << "Exception in foundResource: "<< e.what()<<std::endl;
294 int main(int argc, char* argv[]) {
296 std::ostringstream requestURI;
298 OCConnectivityType connectivityType = CT_ADAPTER_IP;
304 std::size_t inputValLen;
305 int optionSelected = std::stoi(argv[1], &inputValLen);
307 if(inputValLen == strlen(argv[1]))
309 if(optionSelected == 0)
311 std::cout << "Using IP."<< std::endl;
312 connectivityType = CT_ADAPTER_IP;
316 std::cout << "Invalid connectivity type selected. Using default IP"
322 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
325 catch(std::exception& e)
327 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
333 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
336 // Create PlatformConfig object
338 OC::ServiceType::InProc,
339 OC::ModeType::Client,
342 OC::QualityOfService::LowQos
345 OCPlatform::Configure(cfg);
348 // Find all resources
349 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.garage";
351 OCPlatform::findResource("", requestURI.str(),
352 connectivityType, &foundResource);
354 std::cout<< "Finding Resource... " <<std::endl;
356 // A condition variable will free the mutex it is given, then do a non-
357 // intensive block until 'notify' is called on it. In this case, since we
358 // don't ever call cv.notify, this should be a non-processor intensive version
361 std::condition_variable cv;
362 std::unique_lock<std::mutex> lock(blocker);
365 catch(OCException& e)
367 std::cerr << "Exception in GarageClient: "<<e.what();