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.
26 #include "OCPlatform.h"
31 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
40 std::vector<bool> m_lightStates;
41 std::vector<int> m_lightPowers;
42 OCRepresentation m_lightRep;
43 std::vector<OCRepresentation> m_reps;
45 Garage() : m_state(false), m_name("")
52 void printRepresentation(const OCRepresentation& rep)
55 // Check if attribute "name" exists, and then getValue
56 if(rep.hasAttribute("name"))
58 myGarage.m_name = rep.getValue<std::string>("name");
60 std::cout << "\tname: " << myGarage.m_name << std::endl;
62 // You can directly try to get the value. this function
63 // return false if there is no attribute "state"
64 if(!rep.getValue("state", myGarage.m_state))
66 std::cout << "Attribute state doesn't exist in the representation\n";
68 std::cout << "\tstate: " << myGarage.m_state << std::endl;
70 OCRepresentation rep2 = rep;
72 std::cout << "Number of attributes in rep2: "
73 << rep2.numberOfAttributes() << std::endl;
75 if(rep2.erase("name"))
77 std::cout << "attribute: name, was removed successfully from rep2.\n";
80 std::cout << "Number of attributes in rep2: "
81 << rep2.numberOfAttributes() << std::endl;
84 if(rep.isNULL("nullAttribute"))
86 std::cout << "\tnullAttribute is null." << std::endl;
90 std::cout << "\tnullAttribute is not null." << std::endl;
93 rep.getValue("light", myGarage.m_lightRep);
95 myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
96 myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
98 std::cout << "\tlightRep: states: ";
101 for(auto state: myGarage.m_lightStates)
110 std::cout << "," << state;
114 std::cout << std::endl;
115 std::cout << "\tlightRep: powers: ";
117 for(auto power: myGarage.m_lightPowers)
126 std::cout << "," << power;
129 std::cout << std::endl;
131 // Get vector of representations
132 rep.getValue("reps", myGarage.m_reps);
133 // Client know that server is sending two representations
134 // and has key1 and key2 repsectively
135 std::cout << "\treps[0].key1: " << myGarage.m_reps[0].getValue<int>("key1") << std::endl;
136 std::cout << "\treps[0].key2: " << myGarage.m_reps[1].getValue<int>("key2") << std::endl;
138 std::cout << "\tjson: " << rep.getValue<std::string>("json") << std::endl;
140 // callback handler on PUT request
141 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
143 if(eCode == SUCCESS_RESPONSE)
145 std::cout << "PUT request was successful" << std::endl;
147 printRepresentation(rep);
151 std::cout << "onPut Response error: " << eCode << std::endl;
156 // Local function to put a different state for this resource
157 void putLightRepresentation(std::shared_ptr<OCResource> resource)
161 OCRepresentation rep;
163 std::cout << "Putting light representation..."<<std::endl;
165 myGarage.m_state = true;
167 rep.setValue("state", myGarage.m_state);
169 // Create QueryParameters Map and add query params (if any)
170 QueryParamsMap queryParamsMap;
172 // Invoke resource's pit API with rep, query map and the callback parameter
173 resource->put(rep, queryParamsMap, &onPut);
177 // Callback handler on GET request
178 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
180 if(eCode == SUCCESS_RESPONSE)
182 std::cout << "GET request was successful" << std::endl;
183 std::cout << "Resource URI: " << rep.getUri() << std::endl;
185 printRepresentation(rep);
187 putLightRepresentation(curResource);
191 std::cout << "onGET Response error: " << eCode << std::endl;
196 // Local function to get representation of light resource
197 void getLightRepresentation(std::shared_ptr<OCResource> resource)
201 std::cout << "Getting Light Representation..."<<std::endl;
202 // Invoke resource's get API with the callback parameter
205 resource->get(test, &onGet);
209 // Callback to found resources
210 void foundResource(std::shared_ptr<OCResource> resource)
214 std::cout << "Found another resource, ignoring"<<std::endl;
217 std::string resourceURI;
218 std::string hostAddress;
221 // Do some operations with resource object.
224 std::cout<<"DISCOVERED Resource:"<<std::endl;
225 // Get the resource URI
226 resourceURI = resource->uri();
227 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
229 // Get the resource host address
230 hostAddress = resource->host();
231 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
233 // Get the resource types
234 std::cout << "\tList of resource types: " << std::endl;
235 for(auto &resourceTypes : resource->getResourceTypes())
237 std::cout << "\t\t" << resourceTypes << std::endl;
240 // Get the resource interfaces
241 std::cout << "\tList of resource interfaces: " << std::endl;
242 for(auto &resourceInterfaces : resource->getResourceInterfaces())
244 std::cout << "\t\t" << resourceInterfaces << std::endl;
247 if(resourceURI == "/a/garage")
249 curResource = resource;
250 // Call a local function which will internally invoke
251 // get API on the resource pointer
252 getLightRepresentation(resource);
257 // Resource is invalid
258 std::cout << "Resource is invalid" << std::endl;
262 catch(std::exception& e)
268 int main(int argc, char* argv[]) {
270 // Create PlatformConfig object
272 OC::ServiceType::InProc,
273 OC::ModeType::Client,
276 OC::QualityOfService::LowQos
279 OCPlatform::Configure(cfg);
282 // Find all resources
283 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.garage",
285 std::cout<< "Finding Resource... " <<std::endl;
292 catch(OCException& e)
294 std::cerr << "Exception in GarageClient: "<<e.what();