Merge branch 'simulator'
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / linux / SampleResourceServer.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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 #include "PrimitiveResource.h"
22 #include "RCSResourceObject.h"
23 #include "OCPlatform.h"
24 #include "OCApi.h"
25
26 using namespace OC;
27 using namespace OC::OCPlatform;
28 using namespace OIC::Service;
29
30 constexpr int DEFALUT_VALUE = 0;
31
32 constexpr int REQUEST_TEMP = 1;
33 constexpr int REQUEST_LIGHT = 2;
34
35 constexpr int PRESENCE_ON = 1;
36 constexpr int PRESENCE_OFF = 2;
37
38 constexpr int DEFALUT_SERVER = 1;
39 constexpr int CUSTOM_SERVER = 2;
40 constexpr int STOP = 3;
41
42 constexpr int INCREASE_TEMPERATURE = 1;
43 constexpr int DECREASE_TEMPERATURE = 2;
44 constexpr int STOP_TEMPERATURE_SENSOR = 3;
45 constexpr int STOP_LIGHT_SENSOR = 1;
46
47 constexpr int CORRECT_INPUT = 1;
48 constexpr int INCORRECT_INPUT = 2;
49 constexpr int QUIT = 3;
50
51
52 std::string resourceUri = "/a/TempSensor";
53 std::string resourceType = "core.TemperatureSensor";
54 std::string resourceInterface = "oic.if.";
55 std::string attributeKey = "Temperature";
56 int isPresenceOn = PRESENCE_ON;
57
58 RCSResourceObject::Ptr server;
59
60 int processUserInput();
61
62 enum class Control{
63     INCREASE,
64     DECREASE
65 };
66
67 void displayMenu()
68 {
69     std::cout << "====================================================================="
70               << std::endl;
71     std::cout << "   1 - Creation of Resource [Auto control for requests]" << std::endl;
72     std::cout << "   2 - Creation of Resource [Developer control for Get and Set requests]"
73               << std::endl;
74     std::cout << "   3 - Quit" << std::endl;
75     std::cout << "====================================================================="
76               << std::endl;
77 }
78
79 void displayControlTemperatureMenu()
80 {
81     std::cout << "========================================================" << std::endl;
82     std::cout << "1. Increase Temperature by 10 degree" << std::endl;
83     std::cout << "2. Decrease Temperature by 10 degree" << std::endl;
84     std::cout << "3. Stop the Sensor" << std::endl;
85     std::cout << "========================================================" << std::endl;
86 }
87
88 void displayControlLightMenu()
89 {
90     std::cout << "========================================================" << std::endl;
91     std::cout << "1. Stop the Sensor" << std::endl;
92     std::cout << "========================================================" << std::endl;
93 }
94
95 void printAttribute(const RCSResourceAttributes& attrs)
96 {
97     for(const auto& attr : attrs)
98     {
99         std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
100                   << attr.value().toString() << std::endl;
101     }
102 }
103
104 //hander for get request (if developer choose second option for resource Creation)
105 RCSGetResponse requestHandlerForGet(const RCSRequest& request,
106         RCSResourceAttributes& attrs)
107 {
108     std::cout << "Recieved a Get request from Client" << std::endl;
109     RCSResourceObject::LockGuard lock(*server);
110     RCSResourceAttributes attributes = server->getAttributes();
111
112     std::cout << "\nSending response to Client : " << std::endl;
113     printAttribute(attributes);
114
115     return RCSGetResponse::defaultAction();
116 }
117
118 //hander for set request (if developer choose second option for resource Creation)
119 RCSSetResponse requestHandlerForSet(const RCSRequest& request,
120         RCSResourceAttributes& attrs)
121 {
122     std::cout << "Recieved a Set request from Client" << std::endl;
123
124     std::cout << "\n\nSending response to Client : " << std::endl;
125     RCSResourceObject::LockGuard lock(*server);
126     printAttribute(attrs);
127     return RCSSetResponse::defaultAction();
128 }
129
130 void createResource()
131 {
132     server = RCSResourceObject::Builder(resourceUri, resourceType,resourceInterface).
133             setDiscoverable(true).setObservable(true).build();
134 }
135
136 void initServer()
137 {
138     std::cout << "========================================================" << std::endl;
139     std::cout << "Select Resource Type" << std::endl;
140     std::cout << "1. Temperature" << std::endl;
141     std::cout << "2. Light" << std::endl;
142     std::cout << "========================================================" << std::endl;
143
144     switch(processUserInput())
145     {
146     case REQUEST_TEMP:
147         resourceUri = "/a/TempSensor";
148         resourceType = "core.TemperatureSensor";
149         break;
150     case REQUEST_LIGHT:
151         resourceUri = "/a/light";
152         resourceType = "core.light";
153         break;
154     default :
155         std::cout << "Invalid input, please try again" << std::endl;
156         return;
157     }
158
159     try
160     {
161         createResource();
162     }
163     catch (const PlatformException& e)
164     {
165         std::cout << "Exception in initServer : " << e.what() << std::endl;
166     }
167
168     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
169     server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
170     server->setAttribute(attributeKey, DEFALUT_VALUE);
171 }
172
173 void changeTemperature(Control control)
174 {
175     RCSResourceObject::LockGuard lock(server);
176     if(Control::INCREASE == control)
177     {
178         server->getAttributes()[attributeKey] =
179                 server->getAttribute<int>(attributeKey) + 10;
180         std::cout << "\nTemperature increased by 10 degree" << std::endl;
181     }
182     else if(Control::DECREASE == control)
183     {
184         server->getAttributes()[attributeKey] =
185                         server->getAttribute<int>(attributeKey) - 10;
186         std::cout << "\nTemperature Decreased by 10 degree" << std::endl;
187     }
188     std::cout << "\nCurrent Temperature : "
189             << server->getAttributeValue(attributeKey).get<int>() << std::endl;
190 }
191
192 int processUserInput()
193 {
194     int userInput;
195     std::cin >> userInput;
196     if (std::cin.fail())
197     {
198         std::cin.clear();
199         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
200         return -1;
201     }
202     return userInput;
203 }
204
205 int selectPresenceMenu()
206 {
207     std::cout << "========================================================" << std::endl;
208     std::cout << "1. Presence On" << std::endl;
209     std::cout << "2. Presence Off" << std::endl;
210     std::cout << "========================================================" << std::endl;
211
212     switch(processUserInput())
213         {
214         case PRESENCE_ON:
215             isPresenceOn = PRESENCE_ON;
216             startPresence(3);
217             return CORRECT_INPUT;
218         case PRESENCE_OFF:
219             isPresenceOn = PRESENCE_OFF;
220             return CORRECT_INPUT;
221         default :
222             std::cout << "Invalid input, please try again" << std::endl;
223             return INCORRECT_INPUT;
224         }
225 }
226 int selectServerMenu()
227 {
228     switch (processUserInput())
229     {
230         case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client.
231             initServer();
232             return CORRECT_INPUT;
233
234         case CUSTOM_SERVER:
235             // Creation of Resource & setting get and set handler for handling get and
236             // set request from client in application.
237             initServer();
238
239             server->setGetRequestHandler(requestHandlerForGet);
240             server->setSetRequestHandler(requestHandlerForSet);
241             return CORRECT_INPUT;
242         case STOP :
243             return QUIT;
244
245         default :
246             std::cout << "Invalid input, please try again" << std::endl;
247             return INCORRECT_INPUT;
248     }
249 }
250
251 int selectControlTemperatureMenu()
252 {
253    switch (processUserInput())
254    {
255        case INCREASE_TEMPERATURE:
256            changeTemperature(Control::INCREASE);
257            return CORRECT_INPUT;
258
259        case DECREASE_TEMPERATURE:
260            changeTemperature(Control::DECREASE);
261            return CORRECT_INPUT;
262
263        case STOP_TEMPERATURE_SENSOR:
264            return QUIT;
265
266        default:
267            std::cout << "Invalid input. Please try again." << std::endl;
268            return INCORRECT_INPUT;
269    }
270 }
271
272 int selectControlLightMenu()
273 {
274    switch (processUserInput())
275    {
276        case STOP_LIGHT_SENSOR:
277            return QUIT;
278
279        default:
280            std::cout << "Invalid input. Please try again." << std::endl;
281            return INCORRECT_INPUT;
282    }
283 }
284
285 void process()
286 {
287     while(true)
288     {
289         int ret = selectPresenceMenu();
290         if(ret == CORRECT_INPUT) break;
291     }
292
293     while(true)
294     {
295         displayMenu();
296         int ret = selectServerMenu();
297
298         if(ret == QUIT) return;
299         if(ret == CORRECT_INPUT) break;
300     }
301
302     while(true)
303     {
304         if(resourceType == "core.TemperatureSensor")
305         {
306             displayControlTemperatureMenu();
307             if (selectControlTemperatureMenu() == QUIT) return;
308         }
309         else if(resourceType == "core.light")
310         {
311             displayControlLightMenu();
312             if (selectControlLightMenu() == QUIT) return;
313         }
314     }
315 }
316
317 int main(void)
318 {
319     try
320     {
321         process();
322         server = NULL;
323     }
324     catch (const std::exception& e)
325     {
326         std::cout << "main exception  : " << e.what() << std::endl;
327     }
328
329     if(isPresenceOn == PRESENCE_ON)
330     {
331         stopPresence();
332     }
333     std::cout << "Stopping the Server" << std::endl;
334 }