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 void entityHandlerRoom(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
38 void entityHandlerLight(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
39 void entityHandlerFan(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
46 std::string m_roomUri;
47 std::vector<std::string> m_roomTypes;
48 std::vector<std::string> m_roomInterfaces;
49 OCResourceHandle m_roomHandle;
50 OCRepresentation m_roomRep;
55 std::string m_lightUri;
56 std::vector<std::string> m_lightTypes;
57 std::vector<std::string> m_lightInterfaces;
58 OCResourceHandle m_lightHandle;
59 OCRepresentation m_lightRep;
65 std::vector<std::string> m_fanTypes;
66 std::vector<std::string> m_fanInterfaces;
67 OCResourceHandle m_fanHandle;
68 OCRepresentation m_fanRep;
72 RoomResource(): m_lightState(false), m_lightColor(0), m_fanState(false), m_fanSpeed(0)
74 m_lightUri = "/a/light"; // URI of the resource
75 m_lightTypes.push_back("core.light"); // resource type name. In this case, it is light
76 m_lightInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
78 m_lightRep.setUri(m_lightUri);
79 m_lightRep.setResourceTypes(m_lightTypes);
80 m_lightRep.setResourceInterfaces(m_lightInterfaces);
81 m_lightRep.setValue("state", m_lightState);
82 m_lightRep.setValue("color", m_lightColor);
84 m_fanUri = "/a/fan"; // URI of the resource
85 m_fanTypes.push_back("core.fan"); // resource type name. In this case, it is light
86 m_fanInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
88 m_fanRep.setUri(m_fanUri);
89 m_fanRep.setResourceTypes(m_fanTypes);
90 m_fanRep.setResourceInterfaces(m_fanInterfaces);
91 m_fanRep.setValue("state", m_fanState);
92 m_fanRep.setValue("speed", m_fanSpeed);
94 m_roomUri = "/a/room"; // URI of the resource
95 m_roomTypes.push_back("core.room"); // resource type name. In this case, it is light
96 m_roomInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
97 m_roomInterfaces.push_back(BATCH_INTERFACE); // resource interface.
98 m_roomInterfaces.push_back(LINK_INTERFACE); // resource interface.
99 m_roomRep.setUri(m_roomUri);
100 m_roomRep.setResourceTypes(m_roomTypes);
101 m_roomRep.setResourceInterfaces(m_roomInterfaces);
104 /// This function internally calls registerResource API.
105 void createResources(OC::OCPlatform& platform)
107 // This will internally create and register the resource.
108 OCStackResult result = platform.registerResource(
109 m_roomHandle, m_roomUri, m_roomTypes[0],
110 m_roomInterfaces[0], NULL, //entityHandlerRoom,
111 OC_DISCOVERABLE | OC_OBSERVABLE
114 if (OC_STACK_OK != result)
116 cout << "Resource creation (room) was unsuccessful\n";
119 result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterfaces[1]);
120 if (OC_STACK_OK != result)
122 cout << "Binding TypeName to Resource was unsuccessful\n";
125 result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterfaces[2]);
126 if (OC_STACK_OK != result)
128 cout << "Binding TypeName to Resource was unsuccessful\n";
131 result = platform.registerResource(
132 m_lightHandle, m_lightUri, m_lightTypes[0],
133 m_lightInterfaces[0], entityHandlerLight,
134 OC_DISCOVERABLE | OC_OBSERVABLE
137 if (OC_STACK_OK != result)
139 cout << "Resource creation (light) was unsuccessful\n";
142 result = platform.registerResource(
143 m_fanHandle, m_fanUri, m_fanTypes[0],
144 m_fanInterfaces[0], entityHandlerFan,
145 OC_DISCOVERABLE | OC_OBSERVABLE
148 if (OC_STACK_OK != result)
150 cout << "Resource creation (fan) was unsuccessful\n";
153 result = platform.bindResource(m_roomHandle, m_lightHandle);
154 if (OC_STACK_OK != result)
156 cout << "Binding fan resource to room was unsuccessful\n";
159 result = platform.bindResource(m_roomHandle, m_fanHandle);
160 if (OC_STACK_OK != result)
162 cout << "Binding light resource to room was unsuccessful\n";
167 void setLightRepresentation(OCRepresentation& rep)
169 bool tempState = false;
172 // If both entries exist
173 if(rep.getValue("state", tempState) && rep.getValue("color", tempColor))
175 m_lightState = tempState;
176 m_lightColor= tempColor;
178 cout << "\t\t\t\t" << "state: " << m_lightState << endl;
179 cout << "\t\t\t\t" << "color: " << m_lightColor << endl;
183 void setFanRepresentation(OCRepresentation& rep)
185 bool tempState = false;
188 // If both entries exist
189 if(rep.getValue("state", tempState) && rep.getValue("speed", tempSpeed))
191 m_fanState = tempState;
192 m_fanSpeed = tempSpeed;
194 cout << "\t\t\t\t" << "state: " << m_fanState << endl;
195 cout << "\t\t\t\t" << "speed: " << m_fanSpeed << endl;
200 OCRepresentation getLightRepresentation()
202 m_lightRep.setValue("state", m_lightState);
203 m_lightRep.setValue("color", m_lightColor);
208 OCRepresentation getFanRepresentation()
210 m_fanRep.setValue("state", m_fanState);
211 m_fanRep.setValue("speed", m_fanSpeed);
215 OCRepresentation getRoomRepresentation(void)
217 std::vector<OCRepresentation> children;
219 OCRepresentation light = getLightRepresentation();
220 children.push_back(light);
222 OCRepresentation fan = getFanRepresentation();
223 children.push_back(fan);
225 m_roomRep.setChildren(children);
232 // Create the instance of the resource class (in this case instance of class 'RoomResource').
233 RoomResource myRoomResource;
235 void entityHandlerRoom(std::shared_ptr<OCResourceRequest> request,
236 std::shared_ptr<OCResourceResponse> response)
238 cout << "\tIn Server CPP entity handler:\n";
242 // Get the request type and request flag
243 std::string requestType = request->getRequestType();
244 int requestFlag = request->getRequestHandlerFlag();
246 if(requestFlag == RequestHandlerFlag::InitFlag)
248 cout << "\t\trequestFlag : Init\n";
250 // entity handler to perform resource initialization operations
252 else if(requestFlag == RequestHandlerFlag::RequestFlag)
254 cout << "\t\trequestFlag : Request\n";
256 // If the request type is GET
257 if(requestType == "GET")
259 cout << "\t\t\trequestType : GET\n";
261 // Check for query params (if any)
262 QueryParamsMap queryParamsMap = request->getQueryParameters();
264 cout << "\t\t\tquery params: \n";
265 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
267 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
270 OCRepresentation rep;
271 rep = myRoomResource.getRoomRepresentation();
276 response->setErrorCode(200);
278 auto findRes = queryParamsMap.find("if");
280 if(findRes != queryParamsMap.end())
282 response->setResourceRepresentation(rep, findRes->second);
286 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
291 else if(requestType == "PUT")
293 cout << "\t\t\trequestType : PUT\n";
295 entityHandlerLight(request, response);
296 entityHandlerFan(request, response);
300 response->setResourceRepresentation(myRoomResource.getRoomRepresentation());
303 else if(requestType == "POST")
305 // POST request operations
307 else if(requestType == "DELETE")
309 // DELETE request operations
312 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
314 cout << "\t\trequestFlag : Observer\n";
319 std::cout << "Request invalid" << std::endl;
323 void entityHandlerLight(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
325 cout << "\tIn Server CPP (Light) entity handler:\n";
329 // Get the request type and request flag
330 std::string requestType = request->getRequestType();
331 int requestFlag = request->getRequestHandlerFlag();
333 if(requestFlag == RequestHandlerFlag::InitFlag)
335 cout << "\t\trequestFlag : Init\n";
337 // entity handler to perform resource initialization operations
339 else if(requestFlag == RequestHandlerFlag::RequestFlag)
341 cout << "\t\trequestFlag : Request\n";
343 // If the request type is GET
344 if(requestType == "GET")
346 cout << "\t\t\trequestType : GET\n";
351 response->setErrorCode(200);
352 response->setResourceRepresentation(myRoomResource.getLightRepresentation());
356 else if(requestType == "PUT")
358 cout << "\t\t\trequestType : PUT\n";
360 OCRepresentation rep = request->getResourceRepresentation();
362 // Do related operations related to PUT request
363 myRoomResource.setLightRepresentation(rep);
368 response->setErrorCode(200);
369 response->setResourceRepresentation(myRoomResource.getLightRepresentation());
373 else if(requestType == "POST")
375 // POST request operations
377 else if(requestType == "DELETE")
379 // DELETE request operations
382 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
384 cout << "\t\trequestFlag : Observer\n";
389 std::cout << "Request invalid" << std::endl;
393 void entityHandlerFan(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
395 cout << "\tIn Server CPP (Fan) entity handler:\n";
399 // Get the request type and request flag
400 std::string requestType = request->getRequestType();
401 int requestFlag = request->getRequestHandlerFlag();
403 if(requestFlag == RequestHandlerFlag::InitFlag)
405 cout << "\t\trequestFlag : Init\n";
407 // entity handler to perform resource initialization operations
409 else if(requestFlag == RequestHandlerFlag::RequestFlag)
411 cout << "\t\trequestFlag : Request\n";
413 // If the request type is GET
414 if(requestType == "GET")
416 cout << "\t\t\trequestType : GET\n";
421 response->setErrorCode(200);
423 response->setResourceRepresentation(myRoomResource.getFanRepresentation());
427 else if(requestType == "PUT")
429 cout << "\t\t\trequestType : PUT\n";
431 OCRepresentation rep = request->getResourceRepresentation();
433 // Do related operations related to PUT request
434 myRoomResource.setFanRepresentation(rep);
439 response->setErrorCode(200);
440 response->setResourceRepresentation(myRoomResource.getFanRepresentation());
443 else if(requestType == "POST")
445 // POST request operations
447 else if(requestType == "DELETE")
449 // DELETE request operations
452 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
454 cout << "\t\trequestFlag : Observer\n";
459 std::cout << "Request invalid" << std::endl;
465 // Create PlatformConfig object
467 OC::ServiceType::InProc,
468 OC::ModeType::Server,
469 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
470 0, // Uses randomly available port
471 OC::QualityOfService::NonConfirmable
474 // Create a OCPlatform instance.
475 // Note: Platform creation is synchronous call.
478 OCPlatform platform(cfg);
480 // Invoke createResource function of class light.
482 myRoomResource.createResources(platform);
492 std::cout << "Exception in main: " << e.what();
495 // No explicit call to stop the platform.
496 // When OCPlatform destructor is invoked, internally we do platform cleanup