Imported Upstream version 0.9.1
[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 DefaultRegion "Seoul, Korea"
41 #define DefaultSystemTime "00:00:00"
42 #define DefaultLocation "37.256616, 127.052806"
43 #define DefaultCurrency "Won"
44
45
46 class BootstrapResource
47 {
48 public:
49     // Room members
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;
55
56 public:
57     /// Constructor
58     BootstrapResource()
59     {
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;
67     }
68
69     /// This function internally calls registerResource API.
70     void createResources()
71     {
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);
77
78         if (OC_STACK_OK != result)
79         {
80             cout << "Resource creation (room) was unsuccessful\n";
81         }
82     }
83
84     void setBootstrapRepresentation(OCRepresentation& rep)
85     {
86         // Not allowed
87     }
88
89     OCRepresentation getBootstrapRepresentation()
90     {
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);
95
96         return m_bootstrapRep;
97     }
98 };
99
100 // Create the instance of the resource class (in this case instance of class 'RoomResource').
101 BootstrapResource myBootstrapResource;
102
103 // This function prepares a response for any incoming request to Light resource.
104 bool prepareResponse(std::shared_ptr< OCResourceRequest > request)
105 {
106     cout << "\tIn Server CPP prepareResponse:\n";
107     bool result = false;
108     if (request)
109     {
110         // Get the request type and request flag
111         std::string requestType = request->getRequestType();
112         int requestFlag = request->getRequestHandlerFlag();
113
114         if (requestFlag == RequestHandlerFlag::RequestFlag)
115         {
116             cout << "\t\trequestFlag : Request\n";
117
118             // If the request type is GET
119             if (requestType == "GET")
120             {
121                 cout << "\t\t\trequestType : GET\n";
122                 // GET operations are directly handled while sending the response
123                 // in the sendLightResponse function
124                 result = true;
125             }
126             else if (requestType == "PUT")
127             {
128                 cout << "\t\t\trequestType : PUT\n";
129
130                 OCRepresentation rep = request->getResourceRepresentation();
131
132                 // Do related operations related to PUT request
133                 myBootstrapResource.setBootstrapRepresentation(rep);
134                 result = true;
135             }
136             else if (requestType == "POST")
137             {
138                 // POST request operations
139             }
140             else if (requestType == "DELETE")
141             {
142                 // DELETE request operations
143             }
144         }
145         else if (requestFlag == RequestHandlerFlag::ObserverFlag)
146         {
147             cout << "\t\trequestFlag : Observer\n";
148         }
149     }
150     else
151     {
152         std::cout << "Request invalid" << std::endl;
153     }
154
155     return result;
156 }
157
158 OCStackResult sendResponse(std::shared_ptr< OCResourceRequest > pRequest)
159 {
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);
166
167     return OCPlatform::sendResponse(pResponse);
168 }
169
170 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request)
171 {
172     cout << "\tIn Server CPP (entityHandlerBootstrap) entity handler:\n";
173     OCEntityHandlerResult ehResult = OC_EH_ERROR;
174
175     if (prepareResponse(request))
176     {
177         if (OC_STACK_OK == sendResponse(request))
178         {
179             ehResult = OC_EH_OK;
180         }
181         else
182         {
183             std::cout << "sendResponse failed." << std::endl;
184         }
185     }
186     else
187     {
188         std::cout << "PrepareResponse failed." << std::endl;
189     }
190     return ehResult;
191 }
192
193 int main()
194 {
195     // Create PlatformConfig object
196     PlatformConfig cfg
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 };
201
202     OCPlatform::Configure(cfg);
203     try
204     {
205
206         myBootstrapResource.createResources();
207
208         // Perform app tasks
209         while (true)
210         {
211             // some tasks
212         }
213     }
214     catch (OCException e)
215     {
216         std::cout << "Exception in main: " << e.what();
217     }
218
219     // No explicit call to stop the platform.
220     // When OCPlatform destructor is invoked, internally we do platform cleanup
221 }
222