1 //******************************************************************
3 // Copyright 2014 Intel Corporation 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;
41 void onObserve(const AttributeMap& attributeMap, const int& eCode, const int& sequenceNumber)
43 if(eCode == SUCCESS_RESPONSE)
45 std::cout << "OBSERVE RESULT:"<<std::endl;
46 std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
47 for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
49 std::cout << "\tAttribute name: "<< it->first << " value: ";
50 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
52 std::cout <<"\t"<< *valueItr << " ";
55 std::cout << std::endl;
58 if(observe_count() > 30)
60 std::cout<<"Cancelling Observe..."<<std::endl;
61 OCStackResult result = curResource->cancelObserve();
63 std::cout << "Cancel result: "<< result <<std::endl;
65 std::cout << "DONE"<<std::endl;
71 std::cout << "onObserve Response error: " << eCode << std::endl;
76 // callback handler on PUT request
77 void onPut(const AttributeMap attributeMap, const int eCode)
79 if(eCode == SUCCESS_RESPONSE)
81 std::cout << "PUT request was successful" << std::endl;
83 for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
85 std::cout << "\tAttribute name: "<< it->first << " value: ";
86 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
88 std::cout <<"\t"<< *valueItr << " ";
91 std::cout << std::endl;
94 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
95 std::cout << endl << "Observe is used." << endl << endl;
96 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
97 std::cout << endl << "ObserveAll is used." << endl << endl;
99 curResource->observe(OBSERVE_TYPE_TO_USE, &onObserve);
104 std::cout << "onPut Response error: " << eCode << std::endl;
109 // Local function to put a different state for this resource
110 void putLightRepresentation(std::shared_ptr<OCResource> resource)
114 std::cout << "Putting light representation..."<<std::endl;
115 // Create AttributeMap
116 AttributeMap attributeMap;
117 // Add the attribute name and values in the attribute map
118 AttributeValues stateVal;
119 stateVal.push_back("true");
121 AttributeValues powerVal;
122 powerVal.push_back("10");
124 attributeMap["state"] = stateVal;
125 attributeMap["power"] = powerVal;
127 // Create QueryParameters Map and add query params (if any)
128 QueryParamsMap queryParamsMap;
130 // Invoke resource's pit API with attribute map, query map and the callback parameter
131 resource->put(attributeMap, queryParamsMap, &onPut);
135 // callback handler on GET request
136 void onGet(const AttributeMap attributeMap, const int eCode)
138 if(eCode == SUCCESS_RESPONSE)
140 std::cout << "GET Succeeded:"<<std::endl;
141 for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
143 std::cout << "\tAttribute name: "<< it->first << " value: ";
144 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
146 std::cout <<"\t"<< *valueItr << " ";
149 std::cout << std::endl;
152 putLightRepresentation(curResource);
156 std::cout << "onGet Response error: " << eCode << std::endl;
160 // Local function to get representation of light resource
161 void getLightRepresentation(std::shared_ptr<OCResource> resource)
165 std::cout << "Getting Light Representation..."<<std::endl;
166 // Invoke resource's get API with the callback parameter
167 resource->get(&onGet);
171 // Callback to found resources
172 void foundResource(std::shared_ptr<OCResource> resource)
177 std::cout << "Found another resource, ignoring"<<std::endl;
180 std::string resourceURI;
181 std::string hostAddress;
184 // Do some operations with resource object.
187 std::cout<<"DISCOVERED Resource:"<<std::endl;
188 // Get the resource URI
189 resourceURI = resource->uri();
190 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
192 // Get the resource host address
193 hostAddress = resource->host();
194 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
196 if(resourceURI == "/a/light")
198 curResource = resource;
199 // Call a local function which will internally invoke get API on the resource pointer
200 getLightRepresentation(resource);
205 // Resource is invalid
206 std::cout << "Resource is invalid" << std::endl;
210 catch(std::exception& e)
218 std::cout << std::endl;
219 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
220 std::cout << " ObserveType : 1 - Observe" << std::endl;
221 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
224 int main(int argc, char* argv[]) {
227 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
231 int value = atoi(argv[1]);
233 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
235 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
237 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
245 // Create PlatformConfig object
247 cfg.ipAddress = "134.134.161.33";
249 cfg.mode = ModeType::Client;
250 cfg.serviceType = ServiceType::InProc;
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?rt=core.light", &foundResource);
261 std::cout<< "Finding Resource... " <<std::endl;
267 }catch(OCException& e)