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;
33 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
43 Light() : m_state(false), m_power(0), m_name("")
56 void onObserve(const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
58 if(eCode == SUCCESS_RESPONSE)
60 std::cout << "OBSERVE RESULT:"<<std::endl;
61 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
63 rep.getValue("state", mylight.m_state);
64 rep.getValue("power", mylight.m_power);
65 rep.getValue("name", mylight.m_name);
67 std::cout << "\tstate: " << mylight.m_state << std::endl;
68 std::cout << "\tpower: " << mylight.m_power << std::endl;
69 std::cout << "\tname: " << mylight.m_name << std::endl;
71 if(observe_count() > 30)
73 std::cout<<"Cancelling Observe..."<<std::endl;
74 OCStackResult result = curResource->cancelObserve();
76 std::cout << "Cancel result: "<< result <<std::endl;
78 std::cout << "DONE"<<std::endl;
84 std::cout << "onObserve Response error: " << eCode << std::endl;
89 // callback handler on PUT request
90 void onPut(const OCRepresentation& rep, const int eCode)
92 if(eCode == SUCCESS_RESPONSE)
94 std::cout << "PUT request was successful" << std::endl;
96 rep.getValue("state", mylight.m_state);
97 rep.getValue("power", mylight.m_power);
98 rep.getValue("name", mylight.m_name);
100 std::cout << "\tstate: " << mylight.m_state << std::endl;
101 std::cout << "\tpower: " << mylight.m_power << std::endl;
102 std::cout << "\tname: " << mylight.m_name << std::endl;
104 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
105 std::cout << endl << "Observe is used." << endl << endl;
106 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
107 std::cout << endl << "ObserveAll is used." << endl << endl;
109 curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
114 std::cout << "onPut Response error: " << eCode << std::endl;
119 // Local function to put a different state for this resource
120 void putLightRepresentation(std::shared_ptr<OCResource> resource)
124 OCRepresentation rep;
126 std::cout << "Putting light representation..."<<std::endl;
128 mylight.m_state = true;
129 mylight.m_power = 15;
131 rep.setValue("state", mylight.m_state);
132 rep.setValue("power", mylight.m_power);
134 // Create QueryParameters Map and add query params (if any)
135 QueryParamsMap queryParamsMap;
137 // Invoke resource's pit API with rep, query map and the callback parameter
138 resource->put(rep, queryParamsMap, &onPut);
142 // Callback handler on GET request
143 void onGet(const OCRepresentation& rep, const int eCode)
145 if(eCode == SUCCESS_RESPONSE)
147 std::cout << "GET request was successful" << std::endl;
148 std::cout << "Resource URI: " << rep.getUri() << std::endl;
150 rep.getValue("state", mylight.m_state);
151 rep.getValue("power", mylight.m_power);
152 rep.getValue("name", mylight.m_name);
154 std::cout << "\tstate: " << mylight.m_state << std::endl;
155 std::cout << "\tpower: " << mylight.m_power << std::endl;
156 std::cout << "\tname: " << mylight.m_name << std::endl;
158 putLightRepresentation(curResource);
162 std::cout << "onGET Response error: " << eCode << std::endl;
167 // Local function to get representation of light resource
168 void getLightRepresentation(std::shared_ptr<OCResource> resource)
172 std::cout << "Getting Light Representation..."<<std::endl;
173 // Invoke resource's get API with the callback parameter
176 resource->get(test, &onGet);
180 // Callback to found resources
181 void foundResource(std::shared_ptr<OCResource> resource)
185 std::cout << "Found another resource, ignoring"<<std::endl;
188 std::string resourceURI;
189 std::string hostAddress;
192 // Do some operations with resource object.
195 std::cout<<"DISCOVERED Resource:"<<std::endl;
196 // Get the resource URI
197 resourceURI = resource->uri();
198 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
200 // Get the resource host address
201 hostAddress = resource->host();
202 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
204 // Get the resource types
205 std::cout << "\tList of resource types: " << std::endl;
206 for(auto &resourceTypes : resource->getResourceTypes())
208 std::cout << "\t\t" << resourceTypes << std::endl;
211 // Get the resource interfaces
212 std::cout << "\tList of resource interfaces: " << std::endl;
213 for(auto &resourceInterfaces : resource->getResourceInterfaces())
215 std::cout << "\t\t" << resourceInterfaces << std::endl;
218 if(resourceURI == "/a/light")
220 curResource = resource;
221 // Call a local function which will internally invoke get API on the resource pointer
222 getLightRepresentation(resource);
227 // Resource is invalid
228 std::cout << "Resource is invalid" << std::endl;
232 catch(std::exception& e)
240 std::cout << std::endl;
241 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
242 std::cout << " ObserveType : 1 - Observe" << std::endl;
243 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
246 int main(int argc, char* argv[]) {
249 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
253 int value = atoi(argv[1]);
255 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
257 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
259 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
267 // Create PlatformConfig object
269 OC::ServiceType::InProc,
270 OC::ModeType::Client,
273 OC::QualityOfService::NonConfirmable
276 // Create a OCPlatform instance.
277 // Note: Platform creation is synchronous call.
281 OCPlatform platform(cfg);
282 std::cout << "Created Platform..."<<std::endl;
283 // Find all resources
284 platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource);
285 std::cout<< "Finding Resource... " <<std::endl;
291 }catch(OCException& e)