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 "recontainerclient.h"
23 #include "reclientmain.h"
24 #include "RCSDiscoveryManager.h"
25 #include "RCSResourceAttributes.h"
26 #include "RCSAddress.h"
30 #include "condition_variable"
34 using namespace OIC::Service;
36 std::shared_ptr<RCSRemoteResourceObject> g_containerResource;
38 const std::string resourceTypeLight = "?rt=oic.light.control";
39 const std::string resourceTypeSoftsensor = "?rt=oic.softsensor";
40 const std::string targetLightUri = OC_RSRVD_WELL_KNOWN_URI + resourceTypeLight;
41 const std::string targetSoftsensorUri = OC_RSRVD_WELL_KNOWN_URI + resourceTypeSoftsensor;
43 std::mutex g_containerMtx;
44 std::condition_variable g_containerCond;
46 static Evas_Object *log_entry = NULL;
47 static Evas_Object *listnew = NULL;
48 static Evas_Object *naviframe = NULL;
50 // Function to update the log in UI
51 void *updateContainerLog(void *data)
53 string *log = (string *)data;
55 elm_entry_entry_append(log_entry, (*log).c_str());
56 elm_entry_cursor_end_set(log_entry);
60 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
62 Elm_Object_Item *it = (Elm_Object_Item *)event_info;
63 elm_list_item_selected_set(it, EINA_FALSE);
66 static void onDestroy()
68 g_containerResource = NULL;
71 void onContainerDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
73 dlog_print(DLOG_INFO, LOG_TAG, "#### onResourceDiscovered callback");
75 std::string resourceURI = foundResource->getUri();
76 std::string hostAddress = foundResource->getAddress();
78 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI : %s", resourceURI.c_str());
79 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource Host : %S", hostAddress.c_str());
81 string logMessage = "Resource Found <br>";
82 logMessage = logMessage + "URI: " + resourceURI + "<br>";
83 logMessage = logMessage + "Host:" + hostAddress + "<br>";
84 logMessage += "----------------------<br>";
85 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
86 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateContainerLog,
89 g_containerResource = foundResource;
91 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);
92 g_containerCond.notify_all();
95 void *showContainerAPIs(void *data)
97 // Add items to the list only if the list is empty
98 const Eina_List *eina_list = elm_list_items_get(listnew);
99 int count = eina_list_count(eina_list);
102 elm_list_item_append(listnew, "1. Find Light resource", NULL, NULL,
105 elm_list_item_append(listnew, "1. Find Softsensor resource", NULL, NULL,
106 findSoftsensor, NULL);
108 elm_list_go(listnew);
113 static void findLight(void *data, Evas_Object *obj, void *event_info)
115 dlog_print(DLOG_INFO, LOG_TAG, "#### Wait 2 seconds until discovered");
117 RCSDiscoveryManager::getInstance()->discoverResource(RCSAddress::multicast(), targetLightUri,
118 &onContainerDiscovered);
120 std::unique_lock<std::mutex> lck(g_containerMtx);
121 g_containerCond.wait_for(lck, std::chrono::seconds(2));
123 dlog_print(DLOG_INFO, LOG_TAG, "#### Light Discovery over");
126 static void findSoftsensor(void *data, Evas_Object *obj, void *event_info)
128 dlog_print(DLOG_INFO, LOG_TAG, "#### Wait 2 seconds until discovered");
130 RCSDiscoveryManager::getInstance()->discoverResource(RCSAddress::multicast(), targetSoftsensorUri,
131 &onContainerDiscovered);
133 std::unique_lock<std::mutex> lck(g_containerMtx);
134 g_containerCond.wait_for(lck, std::chrono::seconds(2));
136 dlog_print(DLOG_INFO, LOG_TAG, "#### Softsensor Discovery over");
140 naviframe_pop_cb(void *data, Elm_Object_Item *it)
144 if (NULL != log_entry)
146 evas_object_del(log_entry);
151 evas_object_del(listnew);
157 // Method to set up server screens
158 void containerCreateUI(void *data, Evas_Object *obj, void *event_info)
161 Evas_Object *scroller;
162 Evas_Object *nf = (Evas_Object *)data;
163 Elm_Object_Item *nf_it;
167 scroller = elm_scroller_add(nf);
168 elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
169 elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
172 layout = elm_layout_add(nf);
173 elm_layout_file_set(layout, ELM_DEMO_EDJ, "container_layout");
174 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
176 elm_object_content_set(scroller, layout);
179 listnew = elm_list_add(layout);
180 elm_list_mode_set(listnew, ELM_LIST_COMPRESS);
181 evas_object_smart_callback_add(listnew, "selected", list_selected_cb, NULL);
182 elm_object_part_content_set(layout, "listnew", listnew);
183 elm_list_go(listnew);
185 // log_entry - text area for log
186 log_entry = elm_entry_add(layout);
187 elm_entry_scrollable_set(log_entry, EINA_TRUE);
188 elm_entry_editable_set(log_entry, EINA_FALSE);
189 elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
190 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
191 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
192 elm_object_part_content_set(layout, "log", log_entry);
194 nf_it = elm_naviframe_item_push(nf, "Resource Container", NULL, NULL, scroller, NULL);
195 elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
197 // Show the UI list of group APIs
198 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);