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 ******************************************************************/
21 #include "containerclient.h"
23 #include "clientmain.h"
24 #include "RCSDiscoveryManager.h"
25 #include "RCSResourceAttributes.h"
26 #include "RCSAddress.h"
32 using namespace OIC::Service;
34 std::shared_ptr<RCSRemoteResourceObject> g_containerResource;
35 std::vector<RCSRemoteResourceObject::Ptr> resourceList;
36 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
38 const std::string resourceTypeLight = "oic.r.light";
39 const std::string resourceTypeSoftsensor = "oic.r.sensor";
41 static Evas_Object *log_entry = NULL;
42 static Evas_Object *listnew = NULL;
43 static Evas_Object *naviframe = NULL;
45 // Function to update the log in UI
46 void *updateContainerLog(void *data)
48 string *log = (string *)data;
50 elm_entry_entry_append(log_entry, (*log).c_str());
51 elm_entry_cursor_end_set(log_entry);
55 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
57 Elm_Object_Item *it = (Elm_Object_Item *)event_info;
58 elm_list_item_selected_set(it, EINA_FALSE);
61 static void onDestroy()
63 dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
65 g_containerResource = nullptr;
68 void onContainerDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
70 dlog_print(DLOG_INFO, LOG_TAG, "#### onResourceDiscovered callback");
72 std::string resourceURI = foundResource->getUri();
73 std::string hostAddress = foundResource->getAddress();
75 int resourceSize = resourceList.size() + 1;
76 string logMessage = "Resource Found <br>";
77 logMessage = logMessage + "URI: " + resourceURI + "<br>";
78 logMessage = logMessage + "Host:" + hostAddress + "<br>";
79 logMessage += "----------------------<br>";
80 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
81 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateContainerLog,
84 resourceList.push_back(foundResource);
86 g_containerResource = foundResource;
88 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);
91 void *showContainerAPIs(void *data)
93 // Add items to the list only if the list is empty
94 const Eina_List *eina_list = elm_list_items_get(listnew);
95 int count = eina_list_count(eina_list);
98 elm_list_item_append(listnew, "1. Start Light resource Discovery", NULL, NULL,
101 elm_list_item_append(listnew, "2. Start Softsensor resource Discovery", NULL, NULL,
102 findSoftsensor, NULL);
104 elm_list_item_append(listnew, "3.Stop Discovery", NULL, NULL,
105 cancelDiscoverResource, NULL);
107 elm_list_go(listnew);
112 static void findLight(void *data, Evas_Object *obj, void *event_info)
114 dlog_print(DLOG_INFO, LOG_TAG, "#### Light discovery started");
116 while (!discoveryTask)
120 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
121 RCSAddress::multicast(), resourceTypeLight, &onContainerDiscovered);
123 catch (const RCSPlatformException &e)
125 std::cout << e.what() << std::endl;
129 dlog_print(DLOG_INFO, LOG_TAG, "#### Light Discovery over");
132 static void findSoftsensor(void *data, Evas_Object *obj, void *event_info)
134 dlog_print(DLOG_INFO, LOG_TAG, "#### SoftSensor discovery started");
136 while (!discoveryTask)
140 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
141 RCSAddress::multicast(), resourceTypeSoftsensor, &onContainerDiscovered);
143 catch (const RCSPlatformException &e)
145 std::cout << e.what() << std::endl;
149 dlog_print(DLOG_INFO, LOG_TAG, "#### SoftSensor Discovery over");
152 static void cancelDiscoverResource(void *data, Evas_Object *obj, void *event_info)
154 dlog_print(DLOG_INFO, LOG_TAG, "#### cancelDiscoverResource entry");
155 string logMessage = "";
159 logMessage += "There is no discovery request <br>";
163 discoveryTask->cancel();
165 logMessage += "Discovery canceled <br>";
167 int resourceSize = resourceList.size();
170 logMessage += "No Resource Discovered <br>";
174 logMessage += std::to_string(resourceSize) + " : Resource Discovered <br>";
179 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
180 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateContainerLog,
185 naviframe_pop_cb(void *data, Elm_Object_Item *it)
189 if (NULL != log_entry)
191 evas_object_del(log_entry);
196 evas_object_del(listnew);
202 // Method to set up server screens
203 void containerCreateUI(void *data, Evas_Object *obj, void *event_info)
206 Evas_Object *scroller;
207 Evas_Object *nf = (Evas_Object *)data;
208 Elm_Object_Item *nf_it;
212 scroller = elm_scroller_add(nf);
213 elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
214 elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
217 layout = elm_layout_add(nf);
218 elm_layout_file_set(layout, ELM_DEMO_EDJ, "container_layout");
219 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
221 elm_object_content_set(scroller, layout);
224 listnew = elm_list_add(layout);
225 elm_list_mode_set(listnew, ELM_LIST_COMPRESS);
226 evas_object_smart_callback_add(listnew, "selected", list_selected_cb, NULL);
227 elm_object_part_content_set(layout, "listnew", listnew);
228 elm_list_go(listnew);
230 // log_entry - text area for log
231 log_entry = elm_entry_add(layout);
232 elm_entry_scrollable_set(log_entry, EINA_TRUE);
233 elm_entry_editable_set(log_entry, EINA_FALSE);
234 elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
235 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
236 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
237 elm_object_part_content_set(layout, "log", log_entry);
239 nf_it = elm_naviframe_item_push(nf, "Container Client", NULL, NULL, scroller, NULL);
240 elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
242 // Show the UI list of group APIs
243 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);