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::string m_roomType;
48 std::string m_roomInterface1;
49 std::string m_roomInterface2;
50 std::string m_roomInterface3;
51 OCResourceHandle m_roomHandle;
56 std::string m_lightInterface;
57 std::string m_lightUri;
58 std::string m_lightType;
59 OCResourceHandle m_lightHandle;
64 std::string m_fanInterface;
66 std::string m_fanType;
67 OCResourceHandle m_fanHandle;
71 RoomResource(): m_lightState(false), m_lightColor(0), m_fanState(false), m_fanSpeed(0)
73 m_roomUri = "/a/room"; // URI of the resource
74 m_roomType = "core.room"; // resource type name. In this case, it is light
75 m_roomInterface1 = DEFAULT_INTERFACE; // resource interface.
76 m_roomInterface2 = BATCH_INTERFACE; // resource interface.
77 m_roomInterface3 = LINK_INTERFACE; // resource interface.
79 m_lightUri = "/a/light"; // URI of the resource
80 m_lightType = "core.light"; // resource type name. In this case, it is light
81 m_lightInterface = DEFAULT_INTERFACE; // resource interface.
83 m_fanUri = "/a/fan"; // URI of the resource
84 m_fanType = "core.fan"; // resource type name. In this case, it is light
85 m_fanInterface = DEFAULT_INTERFACE; // resource interface.
88 /// This function internally calls registerResource API.
89 void createResources(OC::OCPlatform& platform)
91 // This will internally create and register the resource.
92 OCStackResult result = platform.registerResource(
93 m_roomHandle, m_roomUri, m_roomType,
94 m_roomInterface1, NULL, //entityHandlerRoom,
95 OC_DISCOVERABLE | OC_OBSERVABLE
98 if (OC_STACK_OK != result)
100 cout << "Resource creation (room) was unsuccessful\n";
103 result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterface2);
104 if (OC_STACK_OK != result)
106 cout << "Binding TypeName to Resource was unsuccessful\n";
109 result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterface3);
110 if (OC_STACK_OK != result)
112 cout << "Binding TypeName to Resource was unsuccessful\n";
115 result = platform.registerResource(
116 m_lightHandle, m_lightUri, m_lightType,
117 m_lightInterface, entityHandlerLight,
118 OC_DISCOVERABLE | OC_OBSERVABLE
121 if (OC_STACK_OK != result)
123 cout << "Resource creation (light) was unsuccessful\n";
126 result = platform.registerResource(
127 m_fanHandle, m_fanUri, m_fanType,
128 m_fanInterface, entityHandlerFan,
129 OC_DISCOVERABLE | OC_OBSERVABLE
132 if (OC_STACK_OK != result)
134 cout << "Resource creation (fan) was unsuccessful\n";
137 result = platform.bindResource(m_roomHandle, m_lightHandle);
138 if (OC_STACK_OK != result)
140 cout << "Binding fan resource to room was unsuccessful\n";
143 result = platform.bindResource(m_roomHandle, m_fanHandle);
144 if (OC_STACK_OK != result)
146 cout << "Binding light resource to room was unsuccessful\n";
151 void setRoomRepresentation(OCRepresentation& rep)
153 setLightRepresentation(rep);
154 setFanRepresentation(rep);
157 void setLightRepresentation(OCRepresentation& light)
159 AttributeMap attributeMap = light.getAttributeMap();
161 if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("color") != attributeMap.end())
163 m_lightState = attributeMap["state"][0].compare("true") == 0;
164 m_lightColor= std::stoi(attributeMap["color"][0]);
168 void setFanRepresentation(OCRepresentation& fan)
170 AttributeMap attributeMap = fan.getAttributeMap();
172 if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("speed") != attributeMap.end())
174 m_fanState = attributeMap["state"][0].compare("true") == 0;
175 m_fanSpeed = std::stoi(attributeMap["speed"][0]);
180 OCRepresentation getLightRepresentation() const
182 OCRepresentation light;
184 light.setUri(m_lightUri);
186 std::vector<std::string> interfaces;
187 interfaces.push_back(m_lightInterface);
189 light.setResourceInterfaces(interfaces);
191 std::vector<std::string> types;
192 types.push_back(m_lightType);
194 light.setResourceTypes(types);
196 AttributeMap attributeMap;
197 AttributeValues stateVal;
200 stateVal.push_back("true");
204 stateVal.push_back("false");
207 AttributeValues colorVal;
208 colorVal.push_back(to_string(m_lightColor));
210 attributeMap["state"] = stateVal;
211 attributeMap["color"] = colorVal;
213 light.setAttributeMap(attributeMap);
218 OCRepresentation getFanRepresentation() const
220 OCRepresentation fan;
221 fan.setUri(m_fanUri);
223 std::vector<std::string> interfaces;
224 interfaces.push_back(m_fanInterface);
226 fan.setResourceInterfaces(interfaces);
228 std::vector<std::string> types;
229 types.push_back(m_fanType);
231 fan.setResourceTypes(types);
233 AttributeMap attributeMap;
234 AttributeValues stateVal;
237 stateVal.push_back("true");
241 stateVal.push_back("false");
244 AttributeValues speedVal;
245 speedVal.push_back(to_string(m_fanSpeed));
247 attributeMap["state"] = stateVal;
248 attributeMap["speed"] = speedVal;
250 fan.setAttributeMap(attributeMap);
255 OCRepresentation getRoomRepresentation(void) const
257 OCRepresentation room;
259 room.setUri(m_roomUri);
261 std::vector<std::string> interfaces;
262 interfaces.push_back(m_roomInterface1);
263 interfaces.push_back(m_roomInterface2);
264 interfaces.push_back(m_roomInterface3);
266 room.setResourceInterfaces(interfaces);
268 std::vector<std::string> types;
269 types.push_back(m_roomType);
271 room.setResourceTypes(types);
273 std::vector<OCRepresentation> children;
275 OCRepresentation light = getLightRepresentation();
276 children.push_back(light);
278 OCRepresentation fan = getFanRepresentation();
279 children.push_back(fan);
280 room.setChildren(children);
287 // Create the instance of the resource class (in this case instance of class 'RoomResource').
288 RoomResource myRoomResource;
290 void entityHandlerRoom(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
292 cout << "\tIn Server CPP entity handler:\n";
296 // Get the request type and request flag
297 std::string requestType = request->getRequestType();
298 RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
300 if(requestFlag == RequestHandlerFlag::InitFlag)
302 cout << "\t\trequestFlag : Init\n";
304 // entity handler to perform resource initialization operations
306 else if(requestFlag == RequestHandlerFlag::RequestFlag)
308 cout << "\t\trequestFlag : Request\n";
310 // If the request type is GET
311 if(requestType == "GET")
313 cout << "\t\t\trequestType : GET\n";
315 // Check for query params (if any)
316 QueryParamsMap queryParamsMap = request->getQueryParameters();
318 cout << "\t\t\tquery params: \n";
319 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
321 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
324 OCRepresentation rep;
325 rep = myRoomResource.getRoomRepresentation();
330 response->setErrorCode(200);
332 auto findRes = queryParamsMap.find("if");
334 if(findRes != queryParamsMap.end())
336 response->setResourceRepresentation(rep, findRes->second);
340 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
345 else if(requestType == "PUT")
347 cout << "\t\t\trequestType : PUT\n";
349 // Check for query params (if any)
350 QueryParamsMap queryParamsMap = request->getQueryParameters();
352 cout << "\t\t\tquery params: \n";
353 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
355 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
358 // Get the representation from the request
359 OCRepresentation rep = request->getResourceRepresentation();
361 myRoomResource.setRoomRepresentation(rep);
363 // Do related operations related to PUT request
364 rep = myRoomResource.getRoomRepresentation();
369 response->setErrorCode(200);
371 auto findRes = queryParamsMap.find("if");
373 if(findRes != queryParamsMap.end())
375 response->setResourceRepresentation(rep, findRes->second);
379 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
384 else if(requestType == "POST")
386 // POST request operations
388 else if(requestType == "DELETE")
390 // DELETE request operations
393 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
395 cout << "\t\trequestFlag : Observer\n";
400 std::cout << "Request invalid" << std::endl;
404 void entityHandlerLight(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
406 cout << "\tIn Server CPP (Light) entity handler:\n";
410 // Get the request type and request flag
411 std::string requestType = request->getRequestType();
412 RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
414 if(requestFlag == RequestHandlerFlag::InitFlag)
416 cout << "\t\trequestFlag : Init\n";
418 // entity handler to perform resource initialization operations
420 else if(requestFlag == RequestHandlerFlag::RequestFlag)
422 cout << "\t\trequestFlag : Request\n";
424 // If the request type is GET
425 if(requestType == "GET")
427 cout << "\t\t\trequestType : GET\n";
429 // Check for query params (if any)
430 QueryParamsMap queryParamsMap = request->getQueryParameters();
432 cout << "\t\t\tquery params: \n";
433 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
435 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
438 OCRepresentation rep = myRoomResource.getLightRepresentation();
443 response->setErrorCode(200);
445 auto findRes = queryParamsMap.find("if");
447 if(findRes != queryParamsMap.end())
449 response->setResourceRepresentation(rep, findRes->second);
453 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
458 else if(requestType == "PUT")
460 cout << "\t\t\trequestType : PUT\n";
462 // Check for query params (if any)
463 QueryParamsMap queryParamsMap = request->getQueryParameters();
465 cout << "\t\t\tquery params: \n";
466 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
468 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
471 // Get the representation from the request
472 OCRepresentation rep = request->getResourceRepresentation();
474 myRoomResource.setLightRepresentation(rep);
476 // Do related operations related to PUT request
477 rep = myRoomResource.getLightRepresentation();
482 response->setErrorCode(200);
484 auto findRes = queryParamsMap.find("if");
486 if(findRes != queryParamsMap.end())
488 response->setResourceRepresentation(rep, findRes->second);
492 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
497 else if(requestType == "POST")
499 // POST request operations
501 else if(requestType == "DELETE")
503 // DELETE request operations
506 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
508 cout << "\t\trequestFlag : Observer\n";
513 std::cout << "Request invalid" << std::endl;
517 void entityHandlerFan(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
519 cout << "\tIn Server CPP (Fan) entity handler:\n";
523 // Get the request type and request flag
524 std::string requestType = request->getRequestType();
525 RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
527 if(requestFlag == RequestHandlerFlag::InitFlag)
529 cout << "\t\trequestFlag : Init\n";
531 // entity handler to perform resource initialization operations
533 else if(requestFlag == RequestHandlerFlag::RequestFlag)
535 cout << "\t\trequestFlag : Request\n";
537 // If the request type is GET
538 if(requestType == "GET")
540 cout << "\t\t\trequestType : GET\n";
542 // Check for query params (if any)
543 QueryParamsMap queryParamsMap = request->getQueryParameters();
545 cout << "\t\t\tquery params: \n";
546 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
548 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
551 OCRepresentation rep = myRoomResource.getFanRepresentation();
556 response->setErrorCode(200);
558 auto findRes = queryParamsMap.find("if");
560 if(findRes != queryParamsMap.end())
562 response->setResourceRepresentation(rep, findRes->second);
566 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
571 else if(requestType == "PUT")
573 cout << "\t\t\trequestType : PUT\n";
575 // Check for query params (if any)
576 QueryParamsMap queryParamsMap = request->getQueryParameters();
578 cout << "\t\t\tquery params: \n";
579 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
581 cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
584 // Get the representation from the request
585 OCRepresentation rep = request->getResourceRepresentation();
587 myRoomResource.setFanRepresentation(rep);
589 // Do related operations related to PUT request
590 rep = myRoomResource.getFanRepresentation();
595 response->setErrorCode(200);
597 auto findRes = queryParamsMap.find("if");
599 if(findRes != queryParamsMap.end())
601 response->setResourceRepresentation(rep, findRes->second);
605 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
610 else if(requestType == "POST")
612 // POST request operations
614 else if(requestType == "DELETE")
616 // DELETE request operations
619 else if(requestFlag == RequestHandlerFlag::ObserverFlag)
621 cout << "\t\trequestFlag : Observer\n";
626 std::cout << "Request invalid" << std::endl;
632 // Create PlatformConfig object
634 OC::ServiceType::InProc,
635 OC::ModeType::Server,
638 OC::QualityOfService::NonConfirmable
641 // Create a OCPlatform instance.
642 // Note: Platform creation is synchronous call.
645 OCPlatform platform(cfg);
647 // Invoke createResource function of class light.
649 myRoomResource.createResources(platform);
659 std::cout << "Exception in main: " << e.what();
662 // No explicit call to stop the platform.
663 // When OCPlatform destructor is invoked, internally we do platform cleanup