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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
24 /// Additionally, it'll have a client example to discover it as well.
29 #include <condition_variable>
32 #include "OCPlatform.h"
41 void putResourceInfo(const HeaderOptions& headerOptions,
42 const OCRepresentation rep, const OCRepresentation rep2, const int eCode)
44 std::cout << "In PutResourceInfo" << std::endl;
46 std::cout <<"Clientside Put response to get was: "<<std::endl;
47 std::cout <<"ErrorCode: "<<eCode <<std::endl;
51 std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
53 rep.getValue("isFoo", m_isFoo);
54 rep.getValue("barCount", m_barCount);
56 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
57 std::cout << "\tbarCount: "<< m_barCount << std::endl;
59 std::cout<<"Actual New values are: "<<std::endl;
61 rep.getValue("isFoo", m_isFoo);
62 rep.getValue("barCount", m_barCount);
64 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
65 std::cout << "\tbarCount: "<< m_barCount << std::endl;
71 void getResourceInfo(const HeaderOptions& headerOptions, const OCRepresentation rep,
74 std::cout << "In getResourceInfo" << std::endl;
76 std::cout<<"Clientside response to get was: "<<std::endl;
77 std::cout<<"Error Code: "<<eCode<<std::endl;
81 std::cout <<"Successful Get. Attributes are: "<<std::endl;
83 rep.getValue("isFoo", m_isFoo);
84 rep.getValue("barCount", m_barCount);
86 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
87 std::cout << "\tbarCount: "<< m_barCount << std::endl;
89 std::cout << "Doing a put on q/foo" <<std::endl;
90 OCRepresentation rep2(rep);
94 rep2.setValue("isFoo", m_isFoo);
95 rep2.setValue("barCount", m_barCount);
97 m_resource->put(rep2, QueryParamsMap(),
98 PutCallback(std::bind(&ClientWorker::putResourceInfo, this, std::placeholders::_1,
99 rep2, std::placeholders::_2, std::placeholders::_3)));
103 void foundResource(std::shared_ptr<OCResource> resource)
105 std::cout << "In foundResource" << std::endl;
106 if(resource && resource->uri() == "/q/foo")
109 std::lock_guard<std::mutex> lock(m_resourceLock);
115 m_resource = resource;
118 std::cout << "Found Resource: "<<std::endl;
119 std::cout << "\tHost: "<< resource->host()<<std::endl;
120 std::cout << "\tURI: "<< resource->uri()<<std::endl;
122 // Get the resource types
123 std::cout << "\tList of resource types: " << std::endl;
124 for(auto &resourceTypes : resource->getResourceTypes())
126 std::cout << "\t\t" << resourceTypes << std::endl;
129 // Get the resource interfaces
130 std::cout << "\tList of resource interfaces: " << std::endl;
131 for(auto &resourceInterfaces : resource->getResourceInterfaces())
133 std::cout << "\t\t" << resourceInterfaces << std::endl;
136 std::cout<<"Doing a get on q/foo."<<std::endl;
138 resource->get(QueryParamsMap(),
139 GetCallback(std::bind(&ClientWorker::getResourceInfo, this,
140 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
147 std::cout<<"Starting Client find:"<<std::endl;
148 FindCallback f (std::bind(&ClientWorker::foundResource, this, std::placeholders::_1));
150 OCConnectivityType connectivityType = OC_WIFI;
151 std::cout<<"result:" <<
152 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", connectivityType, f)
155 std::cout<<"result:" <<
156 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", f)
160 std::cout<<"Finding Resource..."<<std::endl;
163 std::unique_lock<std::mutex> lk(m_mutex);
169 std::mutex m_resourceLock;
170 std::condition_variable m_cv;
171 std::shared_ptr<OCResource> m_resource;
178 OCResourceHandle m_resourceHandle;
179 OCRepresentation m_rep;
181 FooResource(): m_isFoo(true), m_barCount (0)
183 m_rep.setUri("/q/foo");
184 m_rep.setValue("isFoo", m_isFoo);
185 m_rep.setValue("barCount", m_barCount);
188 bool createResource()
190 std::string resourceURI = "/q/foo";
191 std::string resourceTypeName = "core.foo";
192 std::string resourceInterface = DEFAULT_INTERFACE;
194 uint8_t resourceProperty = OC_DISCOVERABLE;
196 EntityHandler eh(std::bind(&FooResource::entityHandler,
197 this, std::placeholders::_1));
198 OCStackResult result = OCPlatform::registerResource(m_resourceHandle,
199 resourceURI, resourceTypeName,
201 eh, resourceProperty);
202 if(OC_STACK_OK != result)
204 std::cout<<"Resource creation unsuccessful"<<std::endl;
211 OCRepresentation get()
213 m_rep.setValue("isFoo", m_isFoo);
214 m_rep.setValue("barCount", m_barCount);
219 void put(OCRepresentation& rep)
221 rep.getValue("isFoo", m_isFoo);
222 rep.getValue("barCount", m_barCount);
225 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
227 auto pResponse = std::make_shared<OC::OCResourceResponse>();
228 pResponse->setRequestHandle(pRequest->getRequestHandle());
229 pResponse->setResourceHandle(pRequest->getResourceHandle());
230 pResponse->setResourceRepresentation(get(), "");
231 pResponse->setErrorCode(200);
232 pResponse->setResponseResult(OC_EH_OK);
234 return OCPlatform::sendResponse(pResponse);
237 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
239 std::cout<<"\tConsumer Entity Handler:"<<std::endl;
240 OCEntityHandlerResult ehResult = OC_EH_ERROR;
244 // Note: Most of the handlers are not here, since this is for
245 // demoing client/server co-process existence.
246 // See simpleserver for a more complete example.
247 if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag)
249 std::cout << "\t\trequestFlag : Request"<<std::endl;
251 if(request->getRequestType() == "GET")
253 std::cout<<"\t\t\trequestType : GET"<<std::endl;
254 if(OC_STACK_OK == sendResponse(request))
259 else if (request->getRequestType() == "PUT")
261 std::cout<<"\t\t\trequestType : PUT"<<std::endl;
263 OCRepresentation rep = request->getResourceRepresentation();
265 if(OC_STACK_OK == sendResponse(request))
272 std::cout<<"\t\t\trequestType : UNSUPPORTED: "<<
273 request->getRequestType()<<std::endl;
278 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
280 if(request->getRequestHandlerFlag()==RequestHandlerFlag::InitFlag)
282 std::cout<<"InitFlag"<<std::endl;
284 else if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
286 std::cout<<"ObserverFlag"<<std::endl;
292 std::cout << "Request Invalid!"<<std::endl;
303 OC::ServiceType::InProc,
305 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
306 0, // Uses randomly available port
307 OC::QualityOfService::LowQos
310 OCPlatform::Configure(cfg);
316 if(!fooRes.createResource())
324 catch(OCException& e)
326 std::cout<< "Exception in main: "<<e.what()<<std::endl;