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));
149 std::cout<<"result:" <<
150 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", f)
152 std::cout<<"Finding Resource..."<<std::endl;
155 std::unique_lock<std::mutex> lk(m_mutex);
161 std::mutex m_resourceLock;
162 std::condition_variable m_cv;
163 std::shared_ptr<OCResource> m_resource;
170 OCResourceHandle m_resourceHandle;
171 OCRepresentation m_rep;
173 FooResource(): m_isFoo(true), m_barCount (0)
175 m_rep.setUri("/q/foo");
176 m_rep.setValue("isFoo", m_isFoo);
177 m_rep.setValue("barCount", m_barCount);
180 bool createResource()
182 std::string resourceURI = "/q/foo";
183 std::string resourceTypeName = "core.foo";
184 std::string resourceInterface = DEFAULT_INTERFACE;
186 uint8_t resourceProperty = OC_DISCOVERABLE | OC_SECURE ;
188 EntityHandler eh(std::bind(&FooResource::entityHandler,
189 this, std::placeholders::_1, std::placeholders::_2));
190 OCStackResult result = OCPlatform::registerResource(m_resourceHandle,
191 resourceURI, resourceTypeName,
193 eh, resourceProperty);
194 if(OC_STACK_OK != result)
196 std::cout<<"Resource creation unsuccessful"<<std::endl;
203 OCRepresentation get()
205 m_rep.setValue("isFoo", m_isFoo);
206 m_rep.setValue("barCount", m_barCount);
211 void put(OCRepresentation& rep)
213 rep.getValue("isFoo", m_isFoo);
214 rep.getValue("barCount", m_barCount);
217 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request,
218 std::shared_ptr<OCResourceResponse> response)
220 std::cout<<"\tConsumer Entity Handler:"<<std::endl;
224 // Note: Most of the handlers are not here, since this is for demoing client/server co-process existence.
225 // See simpleserver for a more complete example.
226 if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag)
228 std::cout << "\t\trequestFlag : Request"<<std::endl;
230 if(request->getRequestType() == "GET")
232 std::cout<<"\t\t\trequestType : GET"<<std::endl;
236 response->setErrorCode(200);
237 response->setResourceRepresentation(get(), "");
240 else if (request->getRequestType() == "PUT")
242 std::cout<<"\t\t\trequestType : PUT"<<std::endl;
244 OCRepresentation rep = request->getResourceRepresentation();
249 response->setErrorCode(200);
250 response->setResourceRepresentation(get(), "");
255 std::cout<<"\t\t\trequestType : UNSUPPORTED: "<<request->getRequestType()<<std::endl;
260 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
262 if(request->getRequestHandlerFlag()==RequestHandlerFlag::InitFlag)
264 std::cout<<"InitFlag"<<std::endl;
266 else if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
268 std::cout<<"ObserverFlag"<<std::endl;
274 std::cout << "Request Invalid!"<<std::endl;
284 OC::ServiceType::InProc,
286 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
287 0, // Uses randomly available port
288 OC::QualityOfService::LowQos
291 OCPlatform::Configure(cfg);
297 if(!fooRes.createResource())
305 catch(OCException& e)
307 std::cout<< "Exception in main: "<<e.what()<<std::endl;