Modify the term "Diagnostics" to "Maintenance" in file names
[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 DefaultDeviceName "Legacy Device"
41 #define DefaultLocation "37.256616, 127.052806"
42 #define DefaultLocationName "Living Room"
43 #define DefaultCurrency "Won"
44 #define DefaultRegion "Seoul, Korea"
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 >("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);
96
97         return m_bootstrapRep;
98     }
99 };
100
101 // Create the instance of the resource class (in this case instance of class 'RoomResource').
102 BootstrapResource myBootstrapResource;
103
104 // This function prepares a response for any incoming request to Light resource.
105 bool prepareResponse(std::shared_ptr< OCResourceRequest > request)
106 {
107     cout << "\tIn Server CPP prepareResponse:\n";
108     bool result = false;
109     if (request)
110     {
111         // Get the request type and request flag
112         std::string requestType = request->getRequestType();
113         int requestFlag = request->getRequestHandlerFlag();
114
115         if (requestFlag == RequestHandlerFlag::RequestFlag)
116         {
117             cout << "\t\trequestFlag : Request\n";
118
119             // If the request type is GET
120             if (requestType == "GET")
121             {
122                 cout << "\t\t\trequestType : GET\n";
123                 // GET operations are directly handled while sending the response
124                 // in the sendLightResponse function
125                 result = true;
126             }
127             else if (requestType == "PUT")
128             {
129                 cout << "\t\t\trequestType : PUT\n";
130
131                 OCRepresentation rep = request->getResourceRepresentation();
132
133                 // Do related operations related to PUT request
134                 myBootstrapResource.setBootstrapRepresentation(rep);
135                 result = true;
136             }
137             else if (requestType == "POST")
138             {
139                 // POST request operations
140             }
141             else if (requestType == "DELETE")
142             {
143                 // DELETE request operations
144             }
145         }
146         else if (requestFlag == RequestHandlerFlag::ObserverFlag)
147         {
148             cout << "\t\trequestFlag : Observer\n";
149         }
150     }
151     else
152     {
153         std::cout << "Request invalid" << std::endl;
154     }
155
156     return result;
157 }
158
159 OCStackResult sendResponse(std::shared_ptr< OCResourceRequest > pRequest)
160 {
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);
167
168     return OCPlatform::sendResponse(pResponse);
169 }
170
171 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request)
172 {
173     cout << "\tIn Server CPP (entityHandlerBootstrap) entity handler:\n";
174     OCEntityHandlerResult ehResult = OC_EH_ERROR;
175
176     if (prepareResponse(request))
177     {
178         if (OC_STACK_OK == sendResponse(request))
179         {
180             ehResult = OC_EH_OK;
181         }
182         else
183         {
184             std::cout << "sendResponse failed." << std::endl;
185         }
186     }
187     else
188     {
189         std::cout << "PrepareResponse failed." << std::endl;
190     }
191     return ehResult;
192 }
193
194 int main()
195 {
196     // Create PlatformConfig object
197     PlatformConfig cfg
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 };
202
203     OCPlatform::Configure(cfg);
204     try
205     {
206
207         myBootstrapResource.createResources();
208
209         // Perform app tasks
210         while (true)
211         {
212             // some tasks
213         }
214     }
215     catch (OCException e)
216     {
217         std::cout << "Exception in main: " << e.what();
218     }
219
220     // No explicit call to stop the platform.
221     // When OCPlatform destructor is invoked, internally we do platform cleanup
222 }
223