Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / resource-container / examples / tizen / ContainerClientApp / src / containerclient.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 "containerclient.h"
22
23 #include "clientmain.h"
24 #include "RCSDiscoveryManager.h"
25 #include "RCSResourceAttributes.h"
26 #include "RCSAddress.h"
27
28 #include <string>
29
30 using namespace std;
31 using namespace OC;
32 using namespace OIC::Service;
33
34 std::shared_ptr<RCSRemoteResourceObject>  g_containerResource;
35 std::vector<RCSRemoteResourceObject::Ptr> resourceList;
36 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
37
38 const std::string resourceTypeLight = "oic.r.light";
39 const std::string resourceTypeSoftsensor = "oic.r.sensor";
40
41 static Evas_Object *log_entry = NULL;
42 static Evas_Object *listnew = NULL;
43 static Evas_Object *naviframe = NULL;
44
45 // Function to update the log in UI
46 void *updateContainerLog(void *data)
47 {
48     string *log = (string *)data;
49     // Show the log
50     elm_entry_entry_append(log_entry, (*log).c_str());
51     elm_entry_cursor_end_set(log_entry);
52     return NULL;
53 }
54
55 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
56 {
57     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
58     elm_list_item_selected_set(it, EINA_FALSE);
59 }
60
61 static void onDestroy()
62 {
63         dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
64         resourceList.clear();
65     g_containerResource = nullptr;
66 }
67
68 void onContainerDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
69 {
70     dlog_print(DLOG_INFO, LOG_TAG, "#### onResourceDiscovered callback");
71
72     std::string resourceURI = foundResource->getUri();
73     std::string hostAddress = foundResource->getAddress();
74
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,
82                                           &logMessage);
83
84     resourceList.push_back(foundResource);
85
86     g_containerResource = foundResource;
87
88     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);
89 }
90
91 void *showContainerAPIs(void *data)
92 {
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);
96     if (!count)
97     {
98         elm_list_item_append(listnew, "1. Start Light resource Discovery", NULL, NULL,
99                              findLight, NULL);
100
101         elm_list_item_append(listnew, "2. Start Softsensor resource Discovery", NULL, NULL,
102                              findSoftsensor, NULL);
103
104         elm_list_item_append(listnew, "3.Stop Discovery", NULL, NULL,
105                                                 cancelDiscoverResource, NULL);
106
107         elm_list_go(listnew);
108     }
109     return NULL;
110 }
111
112 static void findLight(void *data, Evas_Object *obj, void *event_info)
113 {
114         dlog_print(DLOG_INFO, LOG_TAG, "#### Light discovery started");
115
116         while (!discoveryTask)
117         {
118                 try
119                 {
120                         discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
121                                                                 RCSAddress::multicast(), resourceTypeLight, &onContainerDiscovered);
122                 }
123                 catch (const RCSPlatformException &e)
124                 {
125                         std::cout << e.what() << std::endl;
126                 }
127         }
128
129         dlog_print(DLOG_INFO, LOG_TAG, "#### Light Discovery over");
130 }
131
132 static void findSoftsensor(void *data, Evas_Object *obj, void *event_info)
133 {
134         dlog_print(DLOG_INFO, LOG_TAG, "#### SoftSensor discovery started");
135
136         while (!discoveryTask)
137         {
138                 try
139                 {
140                         discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
141                                                                 RCSAddress::multicast(), resourceTypeSoftsensor, &onContainerDiscovered);
142                 }
143                 catch (const RCSPlatformException &e)
144                 {
145                         std::cout << e.what() << std::endl;
146                 }
147         }
148
149         dlog_print(DLOG_INFO, LOG_TAG, "#### SoftSensor Discovery over");
150 }
151
152 static void cancelDiscoverResource(void *data, Evas_Object *obj, void *event_info)
153 {
154     dlog_print(DLOG_INFO, LOG_TAG, "#### cancelDiscoverResource entry");
155     string logMessage = "";
156
157     if (!discoveryTask)
158     {
159         logMessage += "There is no discovery request <br>";
160     }
161     else
162     {
163         discoveryTask->cancel();
164
165         logMessage += "Discovery canceled <br>";
166
167         int resourceSize = resourceList.size();
168         if (!resourceSize)
169         {
170             logMessage += "No Resource Discovered <br>";
171         }
172         else
173         {
174             logMessage += std::to_string(resourceSize) + " : Resource Discovered <br>";
175         }
176
177     }
178
179     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
180     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateContainerLog,
181                                           &logMessage);
182 }
183
184 static Eina_Bool
185 naviframe_pop_cb(void *data, Elm_Object_Item *it)
186 {
187     onDestroy();
188
189     if (NULL != log_entry)
190     {
191         evas_object_del(log_entry);
192         log_entry = NULL;
193     }
194     if (NULL != listnew)
195     {
196         evas_object_del(listnew);
197         listnew = NULL;
198     }
199     return EINA_TRUE;
200 }
201
202 // Method to set up server screens
203 void containerCreateUI(void *data, Evas_Object *obj, void *event_info)
204 {
205     Evas_Object *layout;
206     Evas_Object *scroller;
207     Evas_Object *nf = (Evas_Object *)data;
208     Elm_Object_Item *nf_it;
209     naviframe = nf;
210
211     // Scroller
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);
215
216     // Layout
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);
220
221     elm_object_content_set(scroller, layout);
222
223     // List
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);
229
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);
238
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);
241
242     // Show the UI list of group APIs
243     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);
244 }