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;
42 std::vector<bool> m_lightStates;
43 std::vector<int> m_lightPowers;
44 OCRepresentation m_lightRep;
45 std::vector<OCRepresentation> m_reps;
47 Garage() : m_state(false), m_name("")
54 void printRepresentation(const OCRepresentation& rep)
57 // Check if attribute "name" exists, and then getValue
58 if(rep.hasAttribute("name"))
60 myGarage.m_name = rep.getValue<std::string>("name");
62 std::cout << "\tname: " << myGarage.m_name << std::endl;
64 // You can directly try to get the value. this function
65 // return false if there is no attribute "state"
66 if(!rep.getValue("state", myGarage.m_state))
68 std::cout << "Attribute state doesn't exist in the representation\n";
70 std::cout << "\tstate: " << myGarage.m_state << std::endl;
72 OCRepresentation rep2 = rep;
74 std::cout << "Number of attributes in rep2: "
75 << rep2.numberOfAttributes() << std::endl;
77 if(rep2.erase("name"))
79 std::cout << "attribute: name, was removed successfully from rep2.\n";
82 std::cout << "Number of attributes in rep2: "
83 << rep2.numberOfAttributes() << std::endl;
86 if(rep.isNULL("nullAttribute"))
88 std::cout << "\tnullAttribute is null." << std::endl;
92 std::cout << "\tnullAttribute is not null." << std::endl;
95 rep.getValue("light", myGarage.m_lightRep);
97 myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
98 myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
100 std::cout << "\tlightRep: states: ";
103 for(auto state: myGarage.m_lightStates)
112 std::cout << "," << state;
116 std::cout << std::endl;
117 std::cout << "\tlightRep: powers: ";
119 for(auto power: myGarage.m_lightPowers)
128 std::cout << "," << power;
131 std::cout << std::endl;
133 // Get vector of representations
134 rep.getValue("reps", myGarage.m_reps);
135 // Client know that server is sending two representations
136 // and has key1 and key2 repsectively
137 std::cout << "\treps[0].key1: " << myGarage.m_reps[0].getValue<int>("key1") << std::endl;
138 std::cout << "\treps[0].key2: " << myGarage.m_reps[1].getValue<int>("key2") << std::endl;
140 std::cout << "\tjson: " << rep.getValue<std::string>("json") << std::endl;
142 // callback handler on PUT request
143 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
145 if(eCode == SUCCESS_RESPONSE)
147 std::cout << "PUT request was successful" << std::endl;
149 printRepresentation(rep);
153 std::cout << "onPut Response error: " << eCode << std::endl;
158 // Local function to put a different state for this resource
159 void putLightRepresentation(std::shared_ptr<OCResource> resource)
163 OCRepresentation rep;
165 std::cout << "Putting light representation..."<<std::endl;
167 myGarage.m_state = true;
169 rep.setValue("state", myGarage.m_state);
171 // Create QueryParameters Map and add query params (if any)
172 QueryParamsMap queryParamsMap;
174 // Invoke resource's pit API with rep, query map and the callback parameter
175 resource->put(rep, queryParamsMap, &onPut);
179 // Callback handler on GET request
180 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
182 if(eCode == SUCCESS_RESPONSE)
184 std::cout << "GET request was successful" << std::endl;
185 std::cout << "Resource URI: " << rep.getUri() << std::endl;
187 printRepresentation(rep);
189 putLightRepresentation(curResource);
193 std::cout << "onGET Response error: " << eCode << std::endl;
198 // Local function to get representation of light resource
199 void getLightRepresentation(std::shared_ptr<OCResource> resource)
203 std::cout << "Getting Light Representation..."<<std::endl;
204 // Invoke resource's get API with the callback parameter
207 resource->get(test, &onGet);
211 // Callback to found resources
212 void foundResource(std::shared_ptr<OCResource> resource)
216 std::cout << "Found another resource, ignoring"<<std::endl;
219 std::string resourceURI;
220 std::string hostAddress;
223 // Do some operations with resource object.
226 std::cout<<"DISCOVERED Resource:"<<std::endl;
227 // Get the resource URI
228 resourceURI = resource->uri();
229 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
231 // Get the resource host address
232 hostAddress = resource->host();
233 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
235 // Get the resource types
236 std::cout << "\tList of resource types: " << std::endl;
237 for(auto &resourceTypes : resource->getResourceTypes())
239 std::cout << "\t\t" << resourceTypes << std::endl;
242 // Get the resource interfaces
243 std::cout << "\tList of resource interfaces: " << std::endl;
244 for(auto &resourceInterfaces : resource->getResourceInterfaces())
246 std::cout << "\t\t" << resourceInterfaces << std::endl;
249 if(resourceURI == "/a/garage")
251 curResource = resource;
252 // Call a local function which will internally invoke
253 // get API on the resource pointer
254 getLightRepresentation(resource);
259 // Resource is invalid
260 std::cout << "Resource is invalid" << std::endl;
264 catch(std::exception& e)
270 int main(int argc, char* argv[]) {
272 ostringstream requestURI;
274 OCConnectivityType connectivityType = OC_WIFI;
280 std::size_t inputValLen;
281 int optionSelected = stoi(argv[1], &inputValLen);
283 if(inputValLen == strlen(argv[1]))
285 if(optionSelected == 0)
287 connectivityType = OC_ETHERNET;
289 else if(optionSelected == 1)
291 connectivityType = OC_WIFI;
295 std::cout << "Invalid connectivity type selected. Using default WIFI"
301 std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
306 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
311 std::cout<<"Usage: garageclient <ConnectivityType(0|1)>\n";
312 std::cout<<"ConnectivityType: Default WIFI\n";
313 std::cout<<"ConnectivityType 0: ETHERNET\n";
314 std::cout<<"ConnectivityType 1: WIFI\n";
317 // Create PlatformConfig object
319 OC::ServiceType::InProc,
320 OC::ModeType::Client,
323 OC::QualityOfService::LowQos
326 OCPlatform::Configure(cfg);
329 // Find all resources
330 requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.garage";
332 OCPlatform::findResource("", requestURI.str(),
333 connectivityType, &foundResource);
335 std::cout<< "Finding Resource... " <<std::endl;
337 // A condition variable will free the mutex it is given, then do a non-
338 // intensive block until 'notify' is called on it. In this case, since we
339 // don't ever call cv.notify, this should be a non-processor intensive version
342 std::condition_variable cv;
343 std::unique_lock<std::mutex> lock(blocker);
346 catch(OCException& e)
348 std::cerr << "Exception in GarageClient: "<<e.what();