iotivity 0.9.0
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / bootstrapserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 ///
22 /// This sample shows how one could create a resource (collection) with children.
23 ///
24
25 #include <functional>
26
27 #include <pthread.h>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33 using namespace std;
34
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);
39
40 #define DefaultConfigurationValue "Configuration Collection"
41 #define DefaultRegionValue "Seoul, Korea"
42 #define DefaultTimeValue "Time Collection"
43 #define DefaultCurrentTimeValue "00:00:00"
44 #define DefaultNetworkValue "Network Collection"
45 #define DefaultIPAddressValue "192.168.0.2"
46 #define DefaultSecurityValue "SecurityValue"
47 #define DefaultModeValue "NoSec"
48 #define DefaultFactorySetValue "FactorySet Value"
49
50 class BootstrapResource
51 {
52 public:
53     // Room members
54     std::string m_bootstrapUri;
55     std::vector< std::string > m_bootstrapTypes;
56     std::vector< std::string > m_bootstrapInterfaces;
57     OCResourceHandle m_bootstrapHandle;
58     OCRepresentation m_bootstrapRep;
59
60 public:
61     /// Constructor
62     BootstrapResource()
63     {
64         m_bootstrapUri = "/bootstrap"; // URI of the resource
65         m_bootstrapTypes.push_back("bootstrap"); // resource type name. In this case, it is light
66         m_bootstrapInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
67         m_bootstrapRep.setUri(m_bootstrapUri);
68         m_bootstrapRep.setResourceTypes(m_bootstrapTypes);
69         m_bootstrapRep.setResourceInterfaces(m_bootstrapInterfaces);
70         m_bootstrapHandle = NULL;
71     }
72
73     /// This function internally calls registerResource API.
74     void createResources()
75     {
76         using namespace OC::OCPlatform;
77         // This will internally create and register the resource.
78         OCStackResult result = registerResource(m_bootstrapHandle, m_bootstrapUri,
79                 m_bootstrapTypes[0], m_bootstrapInterfaces[0], entityHandlerBootstrap,
80                 OC_DISCOVERABLE | OC_OBSERVABLE);
81
82         if (OC_STACK_OK != result)
83         {
84             cout << "Resource creation (room) was unsuccessful\n";
85         }
86     }
87
88     void setBootstrapRepresentation(OCRepresentation& rep)
89     {
90         // Not allowed
91     }
92
93     OCRepresentation getBootstrapRepresentation()
94     {
95         m_bootstrapRep.setValue< std::string >("regionValue", DefaultRegionValue);
96         m_bootstrapRep.setValue< std::string >("timeValue", DefaultTimeValue);
97         m_bootstrapRep.setValue< std::string >("currentTimeValue", DefaultCurrentTimeValue);
98         m_bootstrapRep.setValue< std::string >("networkValue", DefaultNetworkValue);
99         m_bootstrapRep.setValue< std::string >("IPAddressValue", DefaultIPAddressValue);
100         m_bootstrapRep.setValue< std::string >("securityValue", DefaultSecurityValue);
101         m_bootstrapRep.setValue< std::string >("modeValue", DefaultModeValue);
102         m_bootstrapRep.setValue< std::string >("configurationValue", DefaultConfigurationValue);
103         m_bootstrapRep.setValue< std::string >("factorySetValue", DefaultFactorySetValue);
104
105         return m_bootstrapRep;
106     }
107 };
108
109 // Create the instance of the resource class (in this case instance of class 'RoomResource').
110 BootstrapResource myBootstrapResource;
111
112 // This function prepares a response for any incoming request to Light resource.
113 bool prepareResponse(std::shared_ptr< OCResourceRequest > request)
114 {
115     cout << "\tIn Server CPP prepareResponse:\n";
116     bool result = false;
117     if (request)
118     {
119         // Get the request type and request flag
120         std::string requestType = request->getRequestType();
121         int requestFlag = request->getRequestHandlerFlag();
122
123         if (requestFlag == RequestHandlerFlag::InitFlag)
124         {
125             cout << "\t\trequestFlag : Init\n";
126
127             // entity handler to perform resource initialization operations
128         }
129         else if (requestFlag == RequestHandlerFlag::RequestFlag)
130         {
131             cout << "\t\trequestFlag : Request\n";
132
133             // If the request type is GET
134             if (requestType == "GET")
135             {
136                 cout << "\t\t\trequestType : GET\n";
137                 // GET operations are directly handled while sending the response
138                 // in the sendLightResponse function
139                 result = true;
140             }
141             else if (requestType == "PUT")
142             {
143                 cout << "\t\t\trequestType : PUT\n";
144
145                 OCRepresentation rep = request->getResourceRepresentation();
146
147                 // Do related operations related to PUT request
148                 myBootstrapResource.setBootstrapRepresentation(rep);
149                 result = true;
150             }
151             else if (requestType == "POST")
152             {
153                 // POST request operations
154             }
155             else if (requestType == "DELETE")
156             {
157                 // DELETE request operations
158             }
159         }
160         else if (requestFlag == RequestHandlerFlag::ObserverFlag)
161         {
162             cout << "\t\trequestFlag : Observer\n";
163         }
164     }
165     else
166     {
167         std::cout << "Request invalid" << std::endl;
168     }
169
170     return result;
171 }
172
173 OCStackResult sendResponse(std::shared_ptr< OCResourceRequest > pRequest)
174 {
175     auto pResponse = std::make_shared< OC::OCResourceResponse >();
176     pResponse->setRequestHandle(pRequest->getRequestHandle());
177     pResponse->setResourceHandle(pRequest->getResourceHandle());
178     pResponse->setResourceRepresentation(myBootstrapResource.getBootstrapRepresentation());
179     pResponse->setErrorCode(200);
180     pResponse->setResponseResult(OC_EH_OK);
181
182     return OCPlatform::sendResponse(pResponse);
183 }
184
185 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request)
186 {
187     cout << "\tIn Server CPP (entityHandlerBootstrap) entity handler:\n";
188     OCEntityHandlerResult ehResult = OC_EH_ERROR;
189
190     if (prepareResponse(request))
191     {
192         if (OC_STACK_OK == sendResponse(request))
193         {
194             ehResult = OC_EH_OK;
195         }
196         else
197         {
198             std::cout << "sendResponse failed." << std::endl;
199         }
200     }
201     else
202     {
203         std::cout << "PrepareResponse failed." << std::endl;
204     }
205     return ehResult;
206 }
207
208 int main()
209 {
210     // Create PlatformConfig object
211     PlatformConfig cfg
212     { OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0",
213     // By setting to "0.0.0.0", it binds to all available interfaces
214             0,// Uses randomly available port
215             OC::QualityOfService::LowQos };
216
217     OCPlatform::Configure(cfg);
218     try
219     {
220
221         myBootstrapResource.createResources();
222
223         // Perform app tasks
224         while (true)
225         {
226             // some tasks
227         }
228     }
229     catch (OCException e)
230     {
231         std::cout << "Exception in main: " << e.what();
232     }
233
234     // No explicit call to stop the platform.
235     // When OCPlatform destructor is invoked, internally we do platform cleanup
236 }
237