1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
24 #include "PrimitiveResource.h"
25 #include "RCSResourceObject.h"
31 using namespace OIC::Service;
33 # define checkServer NULL!=server?true:false
35 RCSResourceObject::Ptr server;
36 static bool serverStarted = false;
37 static bool serverCallback = false;
38 int isPresenceOn = PRESENCE_ON;
40 std::string g_resourceUri;
41 std::string g_resourceType;
42 std::string g_attributeKey;
44 static Evas_Object *log_entry = NULL;
45 static Evas_Object *list = NULL;
46 static Evas_Object *naviframe = NULL;
48 typedef struct datetime_popup
52 } datetime_popup_fields;
54 // Function to update the log in UI
55 void *updateGroupLog(void *data)
57 string *log = (string *)data;
59 elm_entry_entry_append(log_entry, (*log).c_str());
60 elm_entry_cursor_end_set(log_entry);
64 void printAttribute(const RCSResourceAttributes &attrs)
66 string logMessage = "";
67 for (const auto & attr : attrs)
69 logMessage = logMessage + "KEY:" + attr.key().c_str() + "<br>";
70 logMessage = logMessage + "VALUE:" + attr.value().toString().c_str() + "<br>";
72 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
73 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
77 static void onDestroy()
79 string logMessage = "SERVER DESTROYED";
81 if(true == serverStarted)
84 if(isPresenceOn == PRESENCE_ON)
86 OCPlatform::stopPresence();
89 serverStarted = false;
93 //hander for get request (if developer choose second option for resource Creation)
94 RCSGetResponse requestHandlerForGet(const RCSRequest &request,
95 RCSResourceAttributes &attrs)
97 string logMessage = "GET REQUEST RECEIVED<br>";
99 RCSResourceObject::LockGuard lock(*server);
100 RCSResourceAttributes attributes = server->getAttributes();
101 printAttribute(attributes);
102 logMessage += "RESPONSE SENT<br>";
104 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
105 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
107 return RCSGetResponse::defaultAction();
110 //hander for set request (if developer choose second option for resource Creation)
111 RCSSetResponse requestHandlerForSet(const RCSRequest &request,
112 RCSResourceAttributes &attrs)
114 string logMessage = "SET REQUEST RECEIVED<br>";
116 RCSResourceObject::LockGuard lock(*server);
117 for (const auto & attr : attrs)
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());
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,
130 return RCSSetResponse::defaultAction();
133 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
135 Elm_Object_Item *it = (Elm_Object_Item *)event_info;
136 elm_list_item_selected_set(it, EINA_FALSE);
139 static void increaseAttribute(void *data, Evas_Object *obj, void *event_info)
141 string logMessage = "";
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>";
153 logMessage = "NO SERRVER <br>";
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,
162 static void decreaseAttribute(void *data, Evas_Object *obj, void *event_info)
164 string logMessage = "";
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>";
175 logMessage = "NO SERRVER <br>";
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,
184 static void initServer()
186 OCPlatform::startPresence(3);
190 server = RCSResourceObject::Builder(g_resourceUri, g_resourceType,
191 RESOURCE_INTERFACE).setDiscoverable(true).setObservable(true).build();
193 catch (const RCSPlatformException &e)
195 dlog_print(DLOG_ERROR, LOG_TAG, "#### Create resource exception! (%s)", e.what());
198 server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
199 server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
200 server->setAttribute(g_attributeKey, DEFALUT_VALUE);
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,
207 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showAPIs, NULL);
211 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
213 datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
214 evas_object_del(popup_fields->popup);
219 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
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 = "";
227 while (attributeString[beginning] == ' ')
232 int len = strlen(attributeString);
233 if (NULL == attributeString || 1 > len)
235 dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL Value");
236 logMessage = g_attributeKey + "Cannot be NULL<br>";
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>";
250 logMessage += "NO SERVER<br>";
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,
258 evas_object_del(popup_fields->popup);
263 list_get_attribute_cb(void *data, Evas_Object *obj, void *event_info)
265 Evas_Object *popup, *btn;
266 Evas_Object *nf = naviframe;
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)
277 elm_object_part_text_set(popup, "title,text", "Enter the brightness");
281 elm_object_part_text_set(popup, "title,text", "Enter the temperature");
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);
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)
297 elm_object_part_text_set(entry, "elm.guide", "RANGE (0 - 50)");
301 elm_object_part_text_set(entry, "elm.guide", "in degree celsius");
304 elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
305 elm_object_part_content_set(layout, "elm.swallow.content", entry);
307 datetime_popup_fields *popup_fields;
308 popup_fields = (datetime_popup_fields *)malloc(sizeof(datetime_popup_fields));
309 if (NULL == popup_fields)
311 dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
315 popup_fields->popup = popup;
316 popup_fields->entry = entry;
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);
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);
333 evas_object_show(popup);
336 void *showAPIs(void *data)
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);
343 if (LIGHT_RT == g_resourceType)
345 elm_list_item_append(list, "1. Increase Brightness", NULL, NULL,
346 increaseAttribute, NULL);
348 elm_list_item_append(list, "2. Decrease Brightness", NULL, NULL,
349 decreaseAttribute, NULL);
351 elm_list_item_append(list, "3. Set Brightness", NULL, NULL,
352 list_get_attribute_cb, NULL);
356 elm_list_item_append(list, "1. Increase Temperature", NULL, NULL,
357 increaseAttribute, NULL);
359 elm_list_item_append(list, "2. Decrease Temperature", NULL, NULL,
360 decreaseAttribute, NULL);
362 elm_list_item_append(list, "3. Set Temperature", NULL, NULL,
363 list_get_attribute_cb, NULL);
373 naviframe_pop_cb(void *data, Elm_Object_Item *it)
377 if (NULL != log_entry)
379 evas_object_del(log_entry);
384 evas_object_del(list);
390 // Method to set up server screens
391 void serverCreateUI(void *data, Evas_Object *obj, void *event_info)
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;
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);
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);
411 elm_object_content_set(scroller, layout);
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);
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);
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);
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);
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);
445 void start_server(void *data, Evas_Object *obj, void *event_info)
448 serverStarted = true;
449 string logMessage = "SERVER WITHOUT CALLBACK<br>";
451 serverCallback = false;
454 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
455 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
459 void start_server_cb(void *data, Evas_Object *obj, void *event_info)
462 serverStarted = true;
463 string logMessage = "SERVER WITH CALLBACK<br>";
465 serverCallback = true;
470 server->setGetRequestHandler(requestHandlerForGet);
471 server->setSetRequestHandler(requestHandlerForSet);
472 logMessage = "HANDLERS SET<br>";
476 logMessage = "NO SERRVER FOUND<br>";
479 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
480 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
485 void temperatureResource(void *data, Evas_Object *obj, void *event_info)
487 g_resourceUri = TEMPERATURE_URI;
488 g_resourceType = TEMPERATURE_RT;
489 g_attributeKey = TEMPERATURE_AK;
491 serverCreateUI(data, NULL, NULL);
494 void lightResource(void *data, Evas_Object *obj, void *event_info)
496 g_resourceUri = LIGHT_URI;
497 g_resourceType = LIGHT_RT;
498 g_attributeKey = LIGHT_AK;
500 serverCreateUI(data, NULL, NULL);