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 "OCPlatform.h"
32 const int SUCCESS_RESPONSE = 0;
33 std::shared_ptr<OCResource> curResource;
41 std::vector<bool> m_lightStates;
42 std::vector<int> m_lightPowers;
43 OCRepresentation m_lightRep;
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");
61 // You can directly try to get the value. this function
62 // return false if there is no attribute "state"
63 if(!rep.getValue("state", myGarage.m_state))
65 std::cout << "Attribute state doesn't exist in the representation\n";
69 OCRepresentation rep2 = rep;
71 std::cout << "Number of attributes in rep2: "
72 << rep2.numberOfAttributes() << std::endl;
74 if(rep2.erase("name"))
76 std::cout << "attribute: name, was removed successfully from rep2.\n";
79 std::cout << "Number of attributes in rep2: "
80 << rep2.numberOfAttributes() << std::endl;
83 rep.getValue("light", myGarage.m_lightRep);
85 myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
86 myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
88 std::cout << "\tstate: " << myGarage.m_state << std::endl;
89 std::cout << "\tname: " << myGarage.m_name << std::endl;
90 std::cout << "\tlightRep: states: ";
93 for(auto state: myGarage.m_lightStates)
102 std::cout << "," << state;
106 std::cout << std::endl;
107 std::cout << "\tlightRep: powers: ";
109 for(auto power: myGarage.m_lightPowers)
118 std::cout << "," << power;
121 std::cout << std::endl;
124 // callback handler on PUT request
125 void onPut(const OCRepresentation& rep, const int eCode)
127 if(eCode == SUCCESS_RESPONSE)
129 std::cout << "PUT request was successful" << std::endl;
131 printRepresentation(rep);
135 std::cout << "onPut Response error: " << eCode << std::endl;
140 // Local function to put a different state for this resource
141 void putLightRepresentation(std::shared_ptr<OCResource> resource)
145 OCRepresentation rep;
147 std::cout << "Putting light representation..."<<std::endl;
149 myGarage.m_state = true;
151 rep.setValue("state", myGarage.m_state);
153 // Create QueryParameters Map and add query params (if any)
154 QueryParamsMap queryParamsMap;
156 // Invoke resource's pit API with rep, query map and the callback parameter
157 resource->put(rep, queryParamsMap, &onPut);
161 // Callback handler on GET request
162 void onGet(const OCRepresentation& rep, const int eCode)
164 if(eCode == SUCCESS_RESPONSE)
166 std::cout << "GET request was successful" << std::endl;
167 std::cout << "Resource URI: " << rep.getUri() << std::endl;
169 printRepresentation(rep);
171 putLightRepresentation(curResource);
175 std::cout << "onGET Response error: " << eCode << std::endl;
180 // Local function to get representation of light resource
181 void getLightRepresentation(std::shared_ptr<OCResource> resource)
185 std::cout << "Getting Light Representation..."<<std::endl;
186 // Invoke resource's get API with the callback parameter
189 resource->get(test, &onGet);
193 // Callback to found resources
194 void foundResource(std::shared_ptr<OCResource> resource)
198 std::cout << "Found another resource, ignoring"<<std::endl;
201 std::string resourceURI;
202 std::string hostAddress;
205 // Do some operations with resource object.
208 std::cout<<"DISCOVERED Resource:"<<std::endl;
209 // Get the resource URI
210 resourceURI = resource->uri();
211 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
213 // Get the resource host address
214 hostAddress = resource->host();
215 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
217 // Get the resource types
218 std::cout << "\tList of resource types: " << std::endl;
219 for(auto &resourceTypes : resource->getResourceTypes())
221 std::cout << "\t\t" << resourceTypes << std::endl;
224 // Get the resource interfaces
225 std::cout << "\tList of resource interfaces: " << std::endl;
226 for(auto &resourceInterfaces : resource->getResourceInterfaces())
228 std::cout << "\t\t" << resourceInterfaces << std::endl;
231 if(resourceURI == "/a/garage")
233 curResource = resource;
234 // Call a local function which will internally invoke
235 // get API on the resource pointer
236 getLightRepresentation(resource);
241 // Resource is invalid
242 std::cout << "Resource is invalid" << std::endl;
246 catch(std::exception& e)
252 int main(int argc, char* argv[]) {
254 // Create PlatformConfig object
256 OC::ServiceType::InProc,
257 OC::ModeType::Client,
260 OC::QualityOfService::NonConfirmable
263 // Create a OCPlatform instance.
264 // Note: Platform creation is synchronous call.
268 OCPlatform platform(cfg);
269 std::cout << "Created Platform..."<<std::endl;
270 // Find all resources
271 platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.garage", &foundResource);
272 std::cout<< "Finding Resource... " <<std::endl;
279 catch(OCException& e)