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