1 //******************************************************************
3 // Copyright 2014 Samsung Electronics 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"
35 // Forward declaring the entityHandler (bootstrap)
36 bool prepareResponse(std::shared_ptr< OCResourceRequest > request);
37 OCStackResult sendResponse(std::shared_ptr< OCResourceRequest > pRequest);
38 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request);
40 #define DefaultDeviceName "Legacy Device"
41 #define DefaultLocation "37.256616, 127.052806"
42 #define DefaultLocationName "Living Room"
43 #define DefaultCurrency "Won"
44 #define DefaultRegion "Seoul, Korea"
46 class BootstrapResource
50 std::string m_bootstrapUri;
51 std::vector< std::string > m_bootstrapTypes;
52 std::vector< std::string > m_bootstrapInterfaces;
53 OCResourceHandle m_bootstrapHandle;
54 OCRepresentation m_bootstrapRep;
60 m_bootstrapUri = "/bootstrap"; // URI of the resource
61 m_bootstrapTypes.push_back("bootstrap"); // resource type name. In this case, it is light
62 m_bootstrapInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
63 m_bootstrapRep.setUri(m_bootstrapUri);
64 m_bootstrapRep.setResourceTypes(m_bootstrapTypes);
65 m_bootstrapRep.setResourceInterfaces(m_bootstrapInterfaces);
66 m_bootstrapHandle = NULL;
69 /// This function internally calls registerResource API.
70 void createResources()
72 using namespace OC::OCPlatform;
73 // This will internally create and register the resource.
74 OCStackResult result = registerResource(m_bootstrapHandle, m_bootstrapUri,
75 m_bootstrapTypes[0], m_bootstrapInterfaces[0], entityHandlerBootstrap,
76 OC_DISCOVERABLE | OC_OBSERVABLE);
78 if (OC_STACK_OK != result)
80 cout << "Resource creation (room) was unsuccessful\n";
84 void setBootstrapRepresentation(OCRepresentation& rep)
89 OCRepresentation getBootstrapRepresentation()
91 m_bootstrapRep.setValue< std::string >("n", DefaultDeviceName);
92 m_bootstrapRep.setValue< std::string >("loc", DefaultLocation);
93 m_bootstrapRep.setValue< std::string >("locn", DefaultLocationName);
94 m_bootstrapRep.setValue< std::string >("c", DefaultCurrency);
95 m_bootstrapRep.setValue< std::string >("r", DefaultRegion);
97 return m_bootstrapRep;
101 // Create the instance of the resource class (in this case instance of class 'RoomResource').
102 BootstrapResource myBootstrapResource;
104 // This function prepares a response for any incoming request to Light resource.
105 bool prepareResponse(std::shared_ptr< OCResourceRequest > request)
107 cout << "\tIn Server CPP prepareResponse:\n";
111 // Get the request type and request flag
112 std::string requestType = request->getRequestType();
113 int requestFlag = request->getRequestHandlerFlag();
115 if (requestFlag == RequestHandlerFlag::RequestFlag)
117 cout << "\t\trequestFlag : Request\n";
119 // If the request type is GET
120 if (requestType == "GET")
122 cout << "\t\t\trequestType : GET\n";
123 // GET operations are directly handled while sending the response
124 // in the sendLightResponse function
127 else if (requestType == "PUT")
129 cout << "\t\t\trequestType : PUT\n";
131 OCRepresentation rep = request->getResourceRepresentation();
133 // Do related operations related to PUT request
134 myBootstrapResource.setBootstrapRepresentation(rep);
137 else if (requestType == "POST")
139 // POST request operations
141 else if (requestType == "DELETE")
143 // DELETE request operations
146 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
148 cout << "\t\trequestFlag : Observer\n";
153 std::cout << "Request invalid" << std::endl;
159 OCStackResult sendResponse(std::shared_ptr< OCResourceRequest > pRequest)
161 auto pResponse = std::make_shared< OC::OCResourceResponse >();
162 pResponse->setRequestHandle(pRequest->getRequestHandle());
163 pResponse->setResourceHandle(pRequest->getResourceHandle());
164 pResponse->setResourceRepresentation(myBootstrapResource.getBootstrapRepresentation());
165 pResponse->setErrorCode(200);
166 pResponse->setResponseResult(OC_EH_OK);
168 return OCPlatform::sendResponse(pResponse);
171 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request)
173 cout << "\tIn Server CPP (entityHandlerBootstrap) entity handler:\n";
174 OCEntityHandlerResult ehResult = OC_EH_ERROR;
176 if (prepareResponse(request))
178 if (OC_STACK_OK == sendResponse(request))
184 std::cout << "sendResponse failed." << std::endl;
189 std::cout << "PrepareResponse failed." << std::endl;
196 // Create PlatformConfig object
198 { OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0",
199 // By setting to "0.0.0.0", it binds to all available interfaces
200 0,// Uses randomly available port
201 OC::QualityOfService::LowQos };
203 OCPlatform::Configure(cfg);
207 myBootstrapResource.createResources();
215 catch (OCException e)
217 std::cout << "Exception in main: " << e.what();
220 // No explicit call to stop the platform.
221 // When OCPlatform destructor is invoked, internally we do platform cleanup