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 DefaultRegion "Seoul, Korea"
41 #define DefaultSystemTime "00:00:00"
42 #define DefaultLocation "37.256616, 127.052806"
43 #define DefaultCurrency "Won"
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 >("r", DefaultRegion);
92 m_bootstrapRep.setValue< std::string >("st", DefaultSystemTime);
93 m_bootstrapRep.setValue< std::string >("loc", DefaultLocation);
94 m_bootstrapRep.setValue< std::string >("c", DefaultCurrency);
96 return m_bootstrapRep;
100 // Create the instance of the resource class (in this case instance of class 'RoomResource').
101 BootstrapResource myBootstrapResource;
103 // This function prepares a response for any incoming request to Light resource.
104 bool prepareResponse(std::shared_ptr< OCResourceRequest > request)
106 cout << "\tIn Server CPP prepareResponse:\n";
110 // Get the request type and request flag
111 std::string requestType = request->getRequestType();
112 int requestFlag = request->getRequestHandlerFlag();
114 if (requestFlag == RequestHandlerFlag::RequestFlag)
116 cout << "\t\trequestFlag : Request\n";
118 // If the request type is GET
119 if (requestType == "GET")
121 cout << "\t\t\trequestType : GET\n";
122 // GET operations are directly handled while sending the response
123 // in the sendLightResponse function
126 else if (requestType == "PUT")
128 cout << "\t\t\trequestType : PUT\n";
130 OCRepresentation rep = request->getResourceRepresentation();
132 // Do related operations related to PUT request
133 myBootstrapResource.setBootstrapRepresentation(rep);
136 else if (requestType == "POST")
138 // POST request operations
140 else if (requestType == "DELETE")
142 // DELETE request operations
145 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
147 cout << "\t\trequestFlag : Observer\n";
152 std::cout << "Request invalid" << std::endl;
158 OCStackResult sendResponse(std::shared_ptr< OCResourceRequest > pRequest)
160 auto pResponse = std::make_shared< OC::OCResourceResponse >();
161 pResponse->setRequestHandle(pRequest->getRequestHandle());
162 pResponse->setResourceHandle(pRequest->getResourceHandle());
163 pResponse->setResourceRepresentation(myBootstrapResource.getBootstrapRepresentation());
164 pResponse->setErrorCode(200);
165 pResponse->setResponseResult(OC_EH_OK);
167 return OCPlatform::sendResponse(pResponse);
170 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request)
172 cout << "\tIn Server CPP (entityHandlerBootstrap) entity handler:\n";
173 OCEntityHandlerResult ehResult = OC_EH_ERROR;
175 if (prepareResponse(request))
177 if (OC_STACK_OK == sendResponse(request))
183 std::cout << "sendResponse failed." << std::endl;
188 std::cout << "PrepareResponse failed." << std::endl;
195 // Create PlatformConfig object
197 { OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0",
198 // By setting to "0.0.0.0", it binds to all available interfaces
199 0,// Uses randomly available port
200 OC::QualityOfService::LowQos };
202 OCPlatform::Configure(cfg);
206 myBootstrapResource.createResources();
214 catch (OCException e)
216 std::cout << "Exception in main: " << e.what();
219 // No explicit call to stop the platform.
220 // When OCPlatform destructor is invoked, internally we do platform cleanup