Rename resource-encapsulation exception classes.
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / tizen / RESampleServerApp / src / reserver.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 "reserver.h"
22
23 #include "remain.h"
24 #include "PrimitiveResource.h"
25 #include "RCSResourceObject.h"
26
27 #include <string>
28
29 using namespace std;
30 using namespace OC;
31 using namespace OIC::Service;
32
33 RCSResourceObject::Ptr server;
34 static bool serverCallback = false;
35
36 # define checkServer NULL!=server?true:false
37
38 static Evas_Object *log_entry = NULL;
39 static Evas_Object *list = NULL;
40 static Evas_Object *naviframe = NULL;
41
42 typedef struct datetime_popup
43 {
44     Evas_Object *popup;
45     Evas_Object *entry;
46 } datetime_popup_fields;
47
48 // Function to update the log in UI
49 void *updateGroupLog(void *data)
50 {
51     string *log = (string *)data;
52     // Show the log
53     elm_entry_entry_append(log_entry, (*log).c_str());
54     elm_entry_cursor_end_set(log_entry);
55     return NULL;
56 }
57
58 void printAttribute(const RCSResourceAttributes &attrs)
59 {
60     string logMessage = "";
61     for (const auto & attr : attrs)
62     {
63         logMessage = logMessage + "KEY:" + attr.key().c_str() + "<br>";
64         logMessage = logMessage + "VALUE:" + attr.value().toString().c_str() + "<br>";
65     }
66     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
67     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
68                                           &logMessage);
69 }
70
71 static void onDestroy()
72 {
73     server = NULL;
74     string logMessage = "SERVER DESTROYED";
75
76     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
77     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
78                                           &logMessage);
79 }
80
81 //hander for get request (if developer choose second option for resource Creation)
82 RCSGetResponse requestHandlerForGet(const RCSRequest &request,
83                                     RCSResourceAttributes &attrs)
84 {
85     string logMessage = "GET REQUEST RECEIVED<br>";
86
87     RCSResourceObject::LockGuard lock(*server);
88     RCSResourceAttributes attributes = server->getAttributes();
89     printAttribute(attributes);
90     logMessage += "RESPONSE SENT<br>";
91
92     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
93     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
94                                           &logMessage);
95     return RCSGetResponse::defaultAction();
96 }
97
98 //hander for set request (if developer choose second option for resource Creation)
99 RCSSetResponse requestHandlerForSet(const RCSRequest &request,
100                                     RCSResourceAttributes &attrs)
101 {
102     string logMessage = "SET REQUEST RECEIVED<br>";
103
104     RCSResourceObject::LockGuard lock(*server);
105     for (const auto & attr : attrs)
106     {
107         logMessage = logMessage + "KEY:" + attr.key().c_str() + "<br>";
108         logMessage = logMessage + "VALUE:" + attr.value().toString().c_str() + "<br>";
109         server->setAttribute(attr.key(), attr.value());
110     }
111
112     printAttribute(attrs);
113     logMessage += "RESPONSE SENT<br>";
114     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
115     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
116                                           &logMessage);
117
118     return RCSSetResponse::defaultAction();
119 }
120
121 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
122 {
123     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
124     elm_list_item_selected_set(it, EINA_FALSE);
125 }
126
127 static void increaseTemp(void *data, Evas_Object *obj, void *event_info)
128 {
129     string logMessage = "";
130
131     if (checkServer)
132     {
133         RCSResourceObject::LockGuard lock(server);
134         server->getAttributes()[attributeKey] = server->getAttribute<int>(attributeKey) + 10;
135         string tempString  = std::to_string(server->getAttribute<int>(attributeKey));
136         logMessage = "TEMPERATURE CHANGED : " + tempString + "<br>";
137
138     }
139     else
140     {
141         logMessage = "NO SERRVER <br>";
142     }
143
144     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
145     logMessage += "----------------------<br>";
146     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
147                                           &logMessage);
148 }
149
150 static void decreaseTemp(void *data, Evas_Object *obj, void *event_info)
151 {
152     string logMessage = "";
153
154     if (checkServer)
155     {
156         RCSResourceObject::LockGuard lock(server);
157         server->getAttributes()[attributeKey] = server->getAttribute<int>(attributeKey) - 10;
158         string tempString  = std::to_string(server->getAttribute<int>(attributeKey));
159         logMessage = "TEMPERATURE CHANGED : " + tempString + "<br>";
160     }
161     else
162     {
163         logMessage = "NO SERRVER <br>";
164     }
165
166     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
167     logMessage += "----------------------<br>";
168     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
169                                           &logMessage);
170 }
171
172 static void initServer()
173 {
174     OCPlatform::startPresence(3);
175
176     try
177     {
178         server = RCSResourceObject::Builder(resourceUri, resourceType,
179                                             resourceInterface).setDiscoverable(true).setObservable(true).build();
180     }
181     catch (const RCSPlatformException &e)
182     {
183         dlog_print(DLOG_ERROR, LOG_TAG, "#### Create resource exception! (%s)", e.what());
184     }
185
186     server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
187     server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
188     server->setAttribute(attributeKey, DEFALUT_VALUE);
189
190     string logMessage = "SERVER CREATED<br>";
191     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
192     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
193                                           &logMessage);
194
195     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showGroupAPIs, NULL);
196 }
197
198 static void
199 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
200 {
201     datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
202     evas_object_del(popup_fields->popup);
203     free(popup_fields);
204 }
205
206 static void
207 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
208 {
209     datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
210     Evas_Object *entry = popup_fields->entry;
211     const char *temperatureString = elm_entry_entry_get(entry);
212     // Remove white spaces(if any) at the beginning
213     string logMessage = "";
214     int beginning = 0;
215     while (temperatureString[beginning] == ' ')
216     {
217         (beginning)++;
218     }
219
220     int len = strlen(temperatureString);
221     if (NULL == temperatureString || 1 > len)
222     {
223         dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL Temperature Value");
224         logMessage = "Temperature Cannot be NULL<br>";
225     }
226     else
227     {
228         if (checkServer)
229         {
230             RCSResourceObject::LockGuard lock(server);
231             int temperate = atoi(temperatureString);
232             server->getAttributes()[attributeKey] = temperate;
233             logMessage = "TEMPERATURE CHANGED : " + to_string(server->getAttribute<int>
234                          (attributeKey)) + "<br>";
235         }
236         else
237         {
238             logMessage += "NO SERVER<br>";
239         }
240     }
241     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
242     logMessage += "----------------------<br>";
243     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
244                                           &logMessage);
245
246     evas_object_del(popup_fields->popup);
247     free(popup_fields);
248 }
249
250 static void
251 list_get_temperaure_cb(void *data, Evas_Object *obj, void *event_info)
252 {
253     Evas_Object *popup, *btn;
254     Evas_Object *nf = naviframe;
255     Evas_Object *entry;
256     Evas_Object *layout;
257
258     /* pop up */
259     popup = elm_popup_add(nf);
260     elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
261     eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
262     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
263     elm_object_part_text_set(popup, "title,text", "Enter the temperature");
264
265     layout = elm_layout_add(popup);
266     elm_layout_file_set(layout, ELM_DEMO_EDJ, "popup_datetime_text");
267     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
268     elm_object_content_set(popup, layout);
269
270     entry = elm_entry_add(layout);
271     elm_entry_single_line_set(entry, EINA_TRUE);
272     elm_entry_scrollable_set(entry, EINA_TRUE);
273     evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
274     evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
275     eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
276     elm_object_part_text_set(entry, "elm.guide", "in degree celsius");
277     elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
278     elm_object_part_content_set(layout, "elm.swallow.content", entry);
279
280     datetime_popup_fields *popup_fields;
281     popup_fields = (datetime_popup_fields *)malloc(sizeof(datetime_popup_fields));
282     if (NULL == popup_fields)
283     {
284         dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
285     }
286     else
287     {
288         popup_fields->popup = popup;
289         popup_fields->entry = entry;
290     }
291
292     /* Cancel button */
293     btn = elm_button_add(popup);
294     elm_object_style_set(btn, "popup");
295     elm_object_text_set(btn, "Cancel");
296     elm_object_part_content_set(popup, "button1", btn);
297     evas_object_smart_callback_add(btn, "clicked", popup_cancel_clicked_cb, popup_fields);
298
299     /* Set button */
300     btn = elm_button_add(popup);
301     elm_object_style_set(btn, "popup");
302     elm_object_text_set(btn, "Set");
303     elm_object_part_content_set(popup, "button2", btn);
304     evas_object_smart_callback_add(btn, "clicked", popup_set_clicked_cb, popup_fields);
305
306     evas_object_show(popup);
307 }
308
309 void *showGroupAPIs(void *data)
310 {
311     // Add items to the list only if the list is empty
312     const Eina_List *eina_list = elm_list_items_get(list);
313     int count = eina_list_count(eina_list);
314     if (!count)
315     {
316         elm_list_item_append(list, "1. Increase Temperature", NULL, NULL,
317                              increaseTemp, NULL);
318
319         elm_list_item_append(list, "2. Decrease Temperature", NULL, NULL,
320                              decreaseTemp, NULL);
321
322         elm_list_item_append(list, "3. Set Temperature", NULL, NULL,
323                              list_get_temperaure_cb, NULL);
324
325         elm_list_go(list);
326     }
327     return NULL;
328 }
329
330 static Eina_Bool
331 naviframe_pop_cb(void *data, Elm_Object_Item *it)
332 {
333     onDestroy();
334
335     if (NULL != log_entry)
336     {
337         evas_object_del(log_entry);
338         log_entry = NULL;
339     }
340     if (NULL != list)
341     {
342         evas_object_del(list);
343         list = NULL;
344     }
345     return EINA_TRUE;
346 }
347
348 // Method to set up server screens
349 void serverCreateUI(void *data, Evas_Object *obj, void *event_info)
350 {
351     Evas_Object *layout;
352     Evas_Object *scroller;
353     Evas_Object *nf = (Evas_Object *)data;
354     Evas_Object *button1;
355     Evas_Object *button2;
356     Elm_Object_Item *nf_it;
357     naviframe = nf;
358
359     // Scroller
360     scroller = elm_scroller_add(nf);
361     elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
362     elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
363
364     // Layout
365     layout = elm_layout_add(nf);
366     elm_layout_file_set(layout, ELM_DEMO_EDJ, "server_layout");
367     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
368
369     elm_object_content_set(scroller, layout);
370
371     // Button
372     button1 = elm_button_add(layout);
373     elm_object_part_content_set(layout, "button1", button1);
374     elm_object_text_set(button1, "Auto Control");
375     evas_object_smart_callback_add(button1, "clicked", start_server, NULL);
376
377     // Button
378     button2 = elm_button_add(layout);
379     elm_object_part_content_set(layout, "button2", button2);
380     elm_object_text_set(button2, "Dev Control");
381     evas_object_smart_callback_add(button2, "clicked", start_server_cb, NULL);
382
383     // List
384     list = elm_list_add(layout);
385     elm_list_mode_set(list, ELM_LIST_COMPRESS);
386     evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
387     elm_object_part_content_set(layout, "list", list);
388     elm_list_go(list);
389
390     // log_entry - text area for log
391     log_entry = elm_entry_add(layout);
392     elm_entry_scrollable_set(log_entry, EINA_TRUE);
393     elm_entry_editable_set(log_entry, EINA_FALSE);
394     elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
395     evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
396     evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
397     elm_object_part_content_set(layout, "log", log_entry);
398
399     nf_it = elm_naviframe_item_push(nf, "Resource Encapsulation", NULL, NULL, scroller, NULL);
400     elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
401 }
402
403 void start_server(void *data, Evas_Object *obj, void *event_info)
404 {
405     server = NULL;
406     string logMessage = "SERVER WITHOUT CALLBACK<br>";
407
408     serverCallback = false;
409     initServer();
410
411     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
412     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
413                                           &logMessage);
414 }
415
416 void start_server_cb(void *data, Evas_Object *obj, void *event_info)
417 {
418     server = NULL;
419     string logMessage = "SERVER WITH CALLBACK<br>";
420
421     serverCallback = true;
422     initServer();
423
424     if (checkServer)
425     {
426         server->setGetRequestHandler(requestHandlerForGet);
427         server->setSetRequestHandler(requestHandlerForSet);
428         logMessage = "HANDLERS SET<br>";
429     }
430     else
431     {
432         logMessage = "NO SERRVER FOUND<br>";
433     }
434
435     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
436     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
437                                           &logMessage);
438
439 }