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 OCRepresentation rep, const OCRepresentation rep2, const int eCode)
43 std::cout << "In PutResourceInfo" << std::endl;
45 std::cout <<"Clientside Put response to get was: "<<std::endl;
46 std::cout <<"ErrorCode: "<<eCode <<std::endl;
50 std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
52 rep.getValue("isFoo", m_isFoo);
53 rep.getValue("barCount", m_barCount);
55 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
56 std::cout << "\tbarCount: "<< m_barCount << std::endl;
58 std::cout<<"Actual New values are: "<<std::endl;
60 rep.getValue("isFoo", m_isFoo);
61 rep.getValue("barCount", m_barCount);
63 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
64 std::cout << "\tbarCount: "<< m_barCount << std::endl;
70 void getResourceInfo(const OCRepresentation rep, const int eCode)
72 std::cout << "In getResourceInfo" << std::endl;
74 std::cout<<"Clientside response to get was: "<<std::endl;
75 std::cout<<"Error Code: "<<eCode<<std::endl;
79 std::cout <<"Successful Get. Attributes are: "<<std::endl;
81 rep.getValue("isFoo", m_isFoo);
82 rep.getValue("barCount", m_barCount);
84 std::cout << "\tisFoo: "<< m_isFoo << std::endl;
85 std::cout << "\tbarCount: "<< m_barCount << std::endl;
87 std::cout << "Doing a put on q/foo" <<std::endl;
88 OCRepresentation rep2(rep);
92 rep2.setValue("isFoo", m_isFoo);
93 rep2.setValue("barCount", m_barCount);
95 m_resource->put(rep2, QueryParamsMap(), PutCallback(std::bind(&ClientWorker::putResourceInfo, this, rep2, std::placeholders::_1, std::placeholders::_2)));
99 void foundResource(std::shared_ptr<OCResource> resource)
101 std::cout << "In foundResource" << std::endl;
102 if(resource && resource->uri() == "/q/foo")
105 std::lock_guard<std::mutex> lock(m_resourceLock);
111 m_resource = resource;
114 std::cout << "Found Resource: "<<std::endl;
115 std::cout << "\tHost: "<< resource->host()<<std::endl;
116 std::cout << "\tURI: "<< resource->uri()<<std::endl;
118 // Get the resource types
119 std::cout << "\tList of resource types: " << std::endl;
120 for(auto &resourceTypes : resource->getResourceTypes())
122 std::cout << "\t\t" << resourceTypes << std::endl;
125 // Get the resource interfaces
126 std::cout << "\tList of resource interfaces: " << std::endl;
127 for(auto &resourceInterfaces : resource->getResourceInterfaces())
129 std::cout << "\t\t" << resourceInterfaces << std::endl;
132 std::cout<<"Doing a get on q/foo."<<std::endl;
134 resource->get(QueryParamsMap(), GetCallback(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2)));
139 void start(OCPlatform& platform)
141 std::cout<<"Starting Client find:"<<std::endl;
142 FindCallback f (std::bind(&ClientWorker::foundResource, this, std::placeholders::_1));
143 std::cout<<"result:" << platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", f)<< std::endl;
144 std::cout<<"Finding Resource..."<<std::endl;
147 std::unique_lock<std::mutex> lk(m_mutex);
153 std::mutex m_resourceLock;
154 std::condition_variable m_cv;
155 std::shared_ptr<OCResource> m_resource;
162 OCResourceHandle m_resourceHandle;
163 OCRepresentation m_rep;
165 FooResource(): m_isFoo(true), m_barCount (0)
167 m_rep.setUri("/q/foo");
168 m_rep.setValue("isFoo", m_isFoo);
169 m_rep.setValue("barCount", m_barCount);
172 bool createResource(OCPlatform& platform)
174 std::string resourceURI = "/q/foo";
175 std::string resourceTypeName = "core.foo";
176 std::string resourceInterface = DEFAULT_INTERFACE;
178 uint8_t resourceProperty = OC_DISCOVERABLE;
180 RegisterCallback eh(std::bind(&FooResource::entityHandler, this, std::placeholders::_1, std::placeholders::_2));
181 OCStackResult result = platform.registerResource(m_resourceHandle, resourceURI, resourceTypeName,
183 eh, resourceProperty);
184 if(OC_STACK_OK != result)
186 std::cout<<"Resource creation unsuccessful"<<std::endl;
193 OCRepresentation get()
195 m_rep.setValue("isFoo", m_isFoo);
196 m_rep.setValue("barCount", m_barCount);
201 void put(OCRepresentation& rep)
203 rep.getValue("isFoo", m_isFoo);
204 rep.getValue("barCount", m_barCount);
207 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
209 std::cout<<"\tConsumer Entity Handler:"<<std::endl;
213 // Note: Most of the handlers are not here, since this is for demoing client/server co-process existence.
214 // See simpleserver for a more complete example.
215 if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag)
217 std::cout << "\t\trequestFlag : Request"<<std::endl;
219 if(request->getRequestType() == "GET")
221 std::cout<<"\t\t\trequestType : GET"<<std::endl;
225 response->setErrorCode(200);
226 response->setResourceRepresentation(get(), "");
229 else if (request->getRequestType() == "PUT")
231 std::cout<<"\t\t\trequestType : PUT"<<std::endl;
233 OCRepresentation rep = request->getResourceRepresentation();
238 response->setErrorCode(200);
239 response->setResourceRepresentation(get(), "");
244 std::cout<<"\t\t\trequestType : UNSUPPORTED: "<<request->getRequestType()<<std::endl;
249 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
251 if(request->getRequestHandlerFlag()==RequestHandlerFlag::InitFlag)
253 std::cout<<"InitFlag"<<std::endl;
255 else if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
257 std::cout<<"ObserverFlag"<<std::endl;
263 std::cout << "Request Invalid!"<<std::endl;
271 OC::ServiceType::InProc,
273 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
274 0, // Uses randomly available port
275 OC::QualityOfService::NonConfirmable
282 OCPlatform platform(cfg);
284 if(!fooRes.createResource(platform))
292 catch(OCException& e)
294 std::cout<< "Exception in main: "<<e.what()<<std::endl;