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 shows how one could create a resource (collection) with children.
29 #include "OCPlatform.h"
36 // Forward declaring the entityHandler (room)
37 OCEntityHandlerResult entityHandlerRoom(std::shared_ptr<OCResourceRequest> request,
38 std::shared_ptr<OCResourceResponse> response);
39 OCEntityHandlerResult entityHandlerLight(std::shared_ptr<OCResourceRequest> request,
40 std::shared_ptr<OCResourceResponse> response);
41 OCEntityHandlerResult entityHandlerFan(std::shared_ptr<OCResourceRequest> request,
42 std::shared_ptr<OCResourceResponse> response);
49 std::string m_roomUri;
50 std::string m_roomName;
51 std::vector<std::string> m_roomTypes;
52 std::vector<std::string> m_roomInterfaces;
53 OCResourceHandle m_roomHandle;
54 OCRepresentation m_roomRep;
59 std::string m_lightUri;
60 std::vector<std::string> m_lightTypes;
61 std::vector<std::string> m_lightInterfaces;
62 OCResourceHandle m_lightHandle;
63 OCRepresentation m_lightRep;
69 std::vector<std::string> m_fanTypes;
70 std::vector<std::string> m_fanInterfaces;
71 OCResourceHandle m_fanHandle;
72 OCRepresentation m_fanRep;
76 RoomResource(): m_roomName("John's Room"), m_lightState(false),
77 m_lightColor(0), m_fanState(false), m_fanSpeed(0)
79 m_lightUri = "/a/light"; // URI of the resource
80 m_lightTypes.push_back("core.light"); // resource type name. In this case, it is light
81 m_lightInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
83 m_lightRep.setUri(m_lightUri);
84 m_lightRep.setResourceTypes(m_lightTypes);
85 m_lightRep.setResourceInterfaces(m_lightInterfaces);
86 m_lightRep.setValue("state", m_lightState);
87 m_lightRep.setValue("color", m_lightColor);
89 m_fanUri = "/a/fan"; // URI of the resource
90 m_fanTypes.push_back("core.fan"); // resource type name. In this case, it is light
91 m_fanInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
93 m_fanRep.setUri(m_fanUri);
94 m_fanRep.setResourceTypes(m_fanTypes);
95 m_fanRep.setResourceInterfaces(m_fanInterfaces);
96 m_fanRep.setValue("state", m_fanState);
97 m_fanRep.setValue("speed", m_fanSpeed);
99 m_roomUri = "/a/room"; // URI of the resource
100 m_roomTypes.push_back("core.room"); // resource type name. In this case, it is light
101 m_roomInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
102 m_roomInterfaces.push_back(BATCH_INTERFACE); // resource interface.
103 m_roomInterfaces.push_back(LINK_INTERFACE); // resource interface.
104 m_roomRep.setValue("name", m_roomName);
105 m_roomRep.setUri(m_roomUri);
106 m_roomRep.setResourceTypes(m_roomTypes);
107 m_roomRep.setResourceInterfaces(m_roomInterfaces);
110 /// This function internally calls registerResource API.
111 void createResources()
113 using namespace OC::OCPlatform;
114 // This will internally create and register the resource.
115 OCStackResult result = registerResource(
116 m_roomHandle, m_roomUri, m_roomTypes[0],
117 m_roomInterfaces[0], entityHandlerRoom,
118 OC_DISCOVERABLE | OC_OBSERVABLE
121 if (OC_STACK_OK != result)
123 cout << "Resource creation (room) was unsuccessful\n";
126 result = bindInterfaceToResource(m_roomHandle, m_roomInterfaces[1]);
127 if (OC_STACK_OK != result)
129 cout << "Binding TypeName to Resource was unsuccessful\n";
132 result = bindInterfaceToResource(m_roomHandle, m_roomInterfaces[2]);
133 if (OC_STACK_OK != result)
135 cout << "Binding TypeName to Resource was unsuccessful\n";
138 result = registerResource(
139 m_lightHandle, m_lightUri, m_lightTypes[0],
140 m_lightInterfaces[0], entityHandlerLight,
141 OC_DISCOVERABLE | OC_OBSERVABLE
144 if (OC_STACK_OK != result)
146 cout << "Resource creation (light) was unsuccessful\n";
149 result = registerResource(
150 m_fanHandle, m_fanUri, m_fanTypes[0],
151 m_fanInterfaces[0], entityHandlerFan,
152 OC_DISCOVERABLE | OC_OBSERVABLE
155 if (OC_STACK_OK != result)
157 cout << "Resource creation (fan) was unsuccessful\n";
160 result = bindResource(m_roomHandle, m_lightHandle);
161 if (OC_STACK_OK != result)
163 cout << "Binding fan resource to room was unsuccessful\n";
166 result = bindResource(m_roomHandle, m_fanHandle);
167 if (OC_STACK_OK != result)
169 cout << "Binding light resource to room was unsuccessful\n";
174 void setLightRepresentation(OCRepresentation& rep)
176 bool tempState = false;
179 // If both entries exist
180 if(rep.getValue("state", tempState) && rep.getValue("color", tempColor))
182 m_lightState = tempState;
183 m_lightColor= tempColor;
185 cout << "\t\t\t\t" << "state: " << m_lightState << endl;
186 cout << "\t\t\t\t" << "color: " << m_lightColor << endl;
190 void setFanRepresentation(OCRepresentation& rep)
192 bool tempState = false;
195 // If both entries exist
196 if(rep.getValue("state", tempState) && rep.getValue("speed", tempSpeed))
198 m_fanState = tempState;
199 m_fanSpeed = tempSpeed;
201 cout << "\t\t\t\t" << "state: " << m_fanState << endl;
202 cout << "\t\t\t\t" << "speed: " << m_fanSpeed << endl;
207 OCRepresentation getLightRepresentation()
209 m_lightRep.setValue("state", m_lightState);
210 m_lightRep.setValue("color", m_lightColor);
215 OCRepresentation getFanRepresentation()
217 m_fanRep.setValue("state", m_fanState);
218 m_fanRep.setValue("speed", m_fanSpeed);
222 OCRepresentation getRoomRepresentation(void)
224 std::vector<OCRepresentation> children;
226 OCRepresentation light = getLightRepresentation();
227 children.push_back(light);
229 OCRepresentation fan = getFanRepresentation();
230 children.push_back(fan);
232 m_roomRep.setChildren(children);
239 // Create the instance of the resource class (in this case instance of class 'RoomResource').
240 RoomResource myRoomResource;
242 OCEntityHandlerResult entityHandlerRoom(std::shared_ptr<OCResourceRequest> request,
243 std::shared_ptr<OCResourceResponse> response)
245 cout << "\tIn Server CPP entity handler:\n";
249 // Get the request type and request flag
250 std::string requestType = request->getRequestType();
251 int requestFlag = request->getRequestHandlerFlag();
253 if(requestFlag == RequestHandlerFlag::InitFlag)
255 cout << "\t\trequestFlag : Init\n";
257 // entity handler to perform resource initialization operations
259 else if(requestFlag == RequestHandlerFlag::RequestFlag)
261 cout << "\t\trequestFlag : Request\n";
263 // If the request type is GET
264 if(requestType == "GET")
266 cout << "\t\t\trequestType : GET\n";
268 // Check for query params (if any)
269 QueryParamsMap queryParamsMap = request->getQueryParameters();
271 cout << "\t\t\tquery params: \n";
272 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
274 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
277 OCRepresentation rep;
278 rep = myRoomResource.getRoomRepresentation();
283 response->setErrorCode(200);
285 auto findRes = queryParamsMap.find("if");
287 if(findRes != queryParamsMap.end())
289 response->setResourceRepresentation(rep, findRes->second);
293 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
298 else if(requestType == "PUT")
300 cout << "\t\t\trequestType : PUT\n";
302 QueryParamsMap queryParamsMap = request->getQueryParameters();
304 entityHandlerLight(request, response);
305 entityHandlerFan(request, response);
307 OCRepresentation rep;
308 rep = myRoomResource.getRoomRepresentation();
312 response->setErrorCode(200);
314 auto findRes = queryParamsMap.find("if");
316 if(findRes != queryParamsMap.end())
318 response->setResourceRepresentation(rep, findRes->second);
322 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
326 else if(requestType == "POST")
328 // POST request operations
330 else if(requestType == "DELETE")
332 // DELETE request operations
335 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
337 cout << "\t\trequestFlag : Observer\n";
342 std::cout << "Request invalid" << std::endl;
348 OCEntityHandlerResult entityHandlerLight(std::shared_ptr<OCResourceRequest> request,
349 std::shared_ptr<OCResourceResponse> response)
351 cout << "\tIn Server CPP (Light) entity handler:\n";
355 // Get the request type and request flag
356 std::string requestType = request->getRequestType();
357 int requestFlag = request->getRequestHandlerFlag();
359 if(requestFlag == RequestHandlerFlag::InitFlag)
361 cout << "\t\trequestFlag : Init\n";
363 // entity handler to perform resource initialization operations
365 else if(requestFlag == RequestHandlerFlag::RequestFlag)
367 cout << "\t\trequestFlag : Request\n";
369 // If the request type is GET
370 if(requestType == "GET")
372 cout << "\t\t\trequestType : GET\n";
377 response->setErrorCode(200);
378 response->setResourceRepresentation(myRoomResource.getLightRepresentation());
382 else if(requestType == "PUT")
384 cout << "\t\t\trequestType : PUT\n";
386 OCRepresentation rep = request->getResourceRepresentation();
388 // Do related operations related to PUT request
389 myRoomResource.setLightRepresentation(rep);
394 response->setErrorCode(200);
395 response->setResourceRepresentation(myRoomResource.getLightRepresentation());
399 else if(requestType == "POST")
401 // POST request operations
403 else if(requestType == "DELETE")
405 // DELETE request operations
408 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
410 cout << "\t\trequestFlag : Observer\n";
415 std::cout << "Request invalid" << std::endl;
421 OCEntityHandlerResult entityHandlerFan(std::shared_ptr<OCResourceRequest> request,
422 std::shared_ptr<OCResourceResponse> response)
424 cout << "\tIn Server CPP (Fan) entity handler:\n";
428 // Get the request type and request flag
429 std::string requestType = request->getRequestType();
430 int requestFlag = request->getRequestHandlerFlag();
432 if(requestFlag == RequestHandlerFlag::InitFlag)
434 cout << "\t\trequestFlag : Init\n";
436 // entity handler to perform resource initialization operations
438 else if(requestFlag == RequestHandlerFlag::RequestFlag)
440 cout << "\t\trequestFlag : Request\n";
442 // If the request type is GET
443 if(requestType == "GET")
445 cout << "\t\t\trequestType : GET\n";
450 response->setErrorCode(200);
452 response->setResourceRepresentation(myRoomResource.getFanRepresentation());
456 else if(requestType == "PUT")
458 cout << "\t\t\trequestType : PUT\n";
460 OCRepresentation rep = request->getResourceRepresentation();
462 // Do related operations related to PUT request
463 myRoomResource.setFanRepresentation(rep);
468 response->setErrorCode(200);
469 response->setResourceRepresentation(myRoomResource.getFanRepresentation());
472 else if(requestType == "POST")
474 // POST request operations
476 else if(requestType == "DELETE")
478 // DELETE request operations
481 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
483 cout << "\t\trequestFlag : Observer\n";
488 std::cout << "Request invalid" << std::endl;
496 // Create PlatformConfig object
498 OC::ServiceType::InProc,
499 OC::ModeType::Server,
500 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
501 0, // Uses randomly available port
502 OC::QualityOfService::LowQos
505 OCPlatform::Configure(cfg);
509 myRoomResource.createResources();
519 std::cout << "Exception in main: " << e.what();
522 // No explicit call to stop the platform.
523 // When OCPlatform destructor is invoked, internally we do platform cleanup