1 //******************************************************************
3 // Copyright 2014 Samsung Electronics 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.
27 #include "OCPlatform.h"
32 const int SUCCESS_RESPONSE = OC_STACK_OK;
33 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
35 std::shared_ptr< OCResource > g_curResource;
37 OCStackResult nmfindResource(const std::string& host , const std::string& resourceName);
38 void onObserve(const HeaderOptions &headerOption , const OCRepresentation& rep , const int& eCode, const int& sequenceNumber);
40 void findResourceCandidate()
44 nmfindResource("" , "coap://224.0.1.187/oc/core?rt=NotificationManager.Hosting");
45 std::cout << "Finding Resource... " << std::endl;
67 void startObserve(std::shared_ptr< OCResource > resource)
69 g_curResource = resource;
72 resource->observe(ObserveType::Observe , test , &onObserve);
81 void onObserve(const HeaderOptions &headerOption , const OCRepresentation& rep , const int& eCode, const int& sequenceNumber)
83 std::cout << "onObserve" << std::endl;
84 // if(eCode == SUCCESS_RESPONSE)
85 if(eCode <= OC_STACK_RESOURCE_DELETED)
88 AttributeMap attributeMap = rep.getAttributeMap();
90 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
92 if(attributeMap.find(it->first) == attributeMap.end())
98 if(rep.getUri().empty())
100 cout << "uri is null\n";
104 std::cout << std::endl;
105 std::cout << "========================================================" << std::endl;
106 std::cout << "Receive OBSERVE RESULT:" << std::endl;
107 std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
108 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
110 std::cout << "\tAttribute name: " << it->first << " value: ";
111 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
113 std::cout << "\t" << *valueItr << " ";
116 std::cout << std::endl;
119 if(observe_count() > 30)
121 std::cout << "Cancelling Observe..." << std::endl;
122 OCStackResult result = g_curResource->cancelObserve();
124 std::cout << "Cancel result: " << result << std::endl;
126 std::cout << "DONE" << std::endl;
132 std::cout << "onObserve Response error: " << eCode << std::endl;
137 void foundResource(std::shared_ptr< OCResource > resource)
139 std::string resourceURI;
140 std::string hostAddress;
145 // if(resource->uri().find("/a/NM/TempHumSensor/virtual") != std::string::npos)
146 if(resource->uri().find("/a/NM/TempHumSensor") != std::string::npos)
148 std::cout << std::endl;
149 std::cout << "========================================================" << std::endl;
150 std::cout << "DISCOVERED Resource(Consumer):" << std::endl;
152 resourceURI = resource->uri();
153 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
155 hostAddress = resource->host();
156 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
158 startObserve(resource);
163 std::cout << "Resource is invalid" << std::endl;
167 catch(std::exception& e)
172 OCStackResult nmfindResource(const std::string& host , const std::string& resourceName)
174 return OCPlatform::findResource(host , resourceName , &foundResource);
177 void getRepresentation(std::shared_ptr< OCResource > resource)
181 std::cout << "Getting Light Representation..." << std::endl;
185 void onPut(const OCRepresentation& rep , const int eCode)
187 if(eCode == SUCCESS_RESPONSE)
189 std::cout << "PUT request was successful" << std::endl;
191 AttributeMap attributeMap = rep.getAttributeMap();
193 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
195 std::cout << "\tAttribute name: " << it->first << " value: ";
196 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
198 std::cout << "\t" << *valueItr << " ";
201 std::cout << std::endl;
204 std::vector< OCRepresentation > children = rep.getChildren();
206 for(auto oit = children.begin() ; oit != children.end() ; ++oit)
208 attributeMap = oit->getAttributeMap();
210 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
212 std::cout << "\tAttribute name: " << it->first << " value: ";
213 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
215 std::cout << "\t" << *valueItr << " ";
218 std::cout << std::endl;
222 if(OBSERVE_TYPE_TO_USE == ObserveType::Observe)
223 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
224 else if(OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
225 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
229 g_curResource->observe(ObserveType::Observe , test , &onObserve);
234 std::cout << "onPut Response error: " << eCode << std::endl;
239 // callback handler on GET request
240 void onGet(const HeaderOptions &headerOption , const OCRepresentation& rep , const int eCode)
242 if(eCode == SUCCESS_RESPONSE)
244 std::cout << "GET request was successful" << std::endl;
246 AttributeMap attributeMap = rep.getAttributeMap();
248 std::cout << "Resource URI: " << rep.getUri() << std::endl;
250 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
252 std::cout << "\tAttribute name: " << it->first << " value: ";
253 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
255 std::cout << "\t" << *valueItr << " ";
258 std::cout << std::endl;
261 std::vector< OCRepresentation > children = rep.getChildren();
263 for(auto oit = children.begin() ; oit != children.end() ; ++oit)
265 std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
267 attributeMap = oit->getAttributeMap();
269 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
271 std::cout << "\tAttribute name: " << it->first << " value: ";
272 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
274 std::cout << "\t" << *valueItr << " ";
277 std::cout << std::endl;
283 std::cout << "onGET Response error: " << eCode << std::endl;
288 void getLightRepresentation(std::shared_ptr< OCResource > resource)
292 std::cout << "Getting Light Representation..." << std::endl;
295 resource->get(test , &onGet);
301 std::cout << std::endl;
302 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
303 std::cout << " ObserveType : 1 - Observe" << std::endl;
304 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
307 int main(int argc , char* argv[])
312 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
316 int value = atoi(argv[1]);
318 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
320 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
322 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
332 OCPlatform::Configure(cfg);
334 std::cout << "Created Platform..." << std::endl;
335 findResourceCandidate();