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 // OCClient.cpp : Defines the entry point for the console application.
26 #include "OCPlatform.h"
31 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
40 // Forward declaration
41 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
42 void onPut(const OCRepresentation& rep, const int eCode);
44 // callback handler on GET request
45 void onGet(const OCRepresentation& rep, const int eCode)
47 if(eCode == SUCCESS_RESPONSE)
49 std::cout << "GET request was successful" << std::endl;
51 AttributeMap attributeMap = rep.getAttributeMap();
53 std::cout << "Resource URI: " << rep.getUri() << std::endl;
55 for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
57 std::cout << "\tAttribute name: "<< it->first << " value: ";
58 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
60 std::cout <<"\t"<< *valueItr << " ";
63 std::cout << std::endl;
66 std::vector<OCRepresentation> children = rep.getChildren();
68 for(auto oit = children.begin(); oit != children.end(); ++oit)
70 std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
72 attributeMap = oit->getAttributeMap();
74 for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
76 std::cout << "\tAttribute name: "<< it->first << " value: ";
77 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
79 std::cout <<"\t"<< *valueItr << " ";
82 std::cout << std::endl;
86 putRoomRepresentation(curResource);
90 std::cout << "onGET Response error: " << eCode << std::endl;
95 // Local function to put a different state for this resource
96 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
100 OCRepresentation rep;
101 std::cout << "Putting room representation..."<<std::endl;
102 // Create AttributeMap
103 AttributeMap attributeMap;
104 // Add the attribute name and values in the attribute map
105 AttributeValues stateVal;
106 stateVal.push_back("true");
108 AttributeValues powerVal;
109 powerVal.push_back("8");
111 attributeMap["state"] = stateVal;
112 attributeMap["speed"] = powerVal;
114 // Create QueryParameters Map and add query params (if any)
116 rep.setAttributeMap(attributeMap);
118 // Invoke resource's pit API with attribute map, query map and the callback parameter
119 resource->put("core.room", BATCH_INTERFACE, rep, qp, &onPut);
123 // callback handler on PUT request
124 void onPut(const OCRepresentation& rep, const int eCode)
126 if(eCode == SUCCESS_RESPONSE)
128 std::cout << "PUT request was successful" << std::endl;
130 AttributeMap attributeMap = rep.getAttributeMap();
132 for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
134 std::cout << "\tAttribute name: "<< it->first << " value: ";
135 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
137 std::cout <<"\t"<< *valueItr << " ";
140 std::cout << std::endl;
143 std::vector<OCRepresentation> children = rep.getChildren();
145 for(auto oit = children.begin(); oit != children.end(); ++oit)
147 attributeMap = oit->getAttributeMap();
149 for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
151 std::cout << "\tAttribute name: "<< it->first << " value: ";
152 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
154 std::cout <<"\t"<< *valueItr << " ";
157 std::cout << std::endl;
164 std::cout << "onPut Response error: " << eCode << std::endl;
170 // Local function to get representation of light resource
171 void getRoomRepresentation(std::shared_ptr<OCResource> resource)
175 std::cout << "Getting Room Representation..."<<std::endl;
176 // Invoke resource's get API with the callback parameter
179 resource->get("core.room", BATCH_INTERFACE, qp, &onGet);
183 // Callback to found resources
184 void foundResource(std::shared_ptr<OCResource> resource)
188 std::cout << "Found another resource, ignoring"<<std::endl;
191 std::string resourceURI;
192 std::string hostAddress;
195 // Do some operations with resource object.
198 std::cout<<"DISCOVERED Resource:"<<std::endl;
199 // Get the resource URI
200 resourceURI = resource->uri();
201 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
203 // Get the resource host address
204 hostAddress = resource->host();
205 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
207 // Get the resource types
208 std::cout << "\tList of resource types: " << std::endl;
209 for(auto &resourceTypes : resource->getResourceTypes())
211 std::cout << "\t\t" << resourceTypes << std::endl;
214 // Get the resource interfaces
215 std::cout << "\tList of resource interfaces: " << std::endl;
216 for(auto &resourceInterfaces : resource->getResourceInterfaces())
218 std::cout << "\t\t" << resourceInterfaces << std::endl;
221 if(resourceURI == "/a/room")
223 curResource = resource;
224 // Call a local function which will internally invoke get API on the resource pointer
225 getRoomRepresentation(resource);
230 // Resource is invalid
231 std::cout << "Resource is invalid" << std::endl;
235 catch(std::exception& e)
241 int main(int argc, char* argv[]) {
243 // Create PlatformConfig object
245 OC::ServiceType::InProc,
246 OC::ModeType::Client,
249 OC::QualityOfService::NonConfirmable
252 // Create a OCPlatform instance.
253 // Note: Platform creation is synchronous call.
257 OCPlatform platform(cfg);
258 std::cout << "Created Platform..."<<std::endl;
259 // Find all resources
260 platform.findResource("", "coap://224.0.1.187/oc/core", &foundResource);
261 std::cout<< "Finding Resource... " <<std::endl;
267 }catch(OCException& e)