40662a7e737e12fbf88b1b413f8175fb59a02bf6
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / tizen / RESampleClientApp / src / recontainerclient.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 "recontainerclient.h"
22
23 #include "reclientmain.h"
24 #include "RCSDiscoveryManager.h"
25 #include "RCSResourceAttributes.h"
26 #include "RCSAddress.h"
27
28 #include <string>
29 #include "mutex"
30 #include "condition_variable"
31
32 using namespace std;
33 using namespace OC;
34 using namespace OIC::Service;
35
36 std::shared_ptr<RCSRemoteResourceObject>  g_containerResource;
37
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;
42
43 std::mutex g_containerMtx;
44 std::condition_variable g_containerCond;
45
46 static Evas_Object *log_entry = NULL;
47 static Evas_Object *listnew = NULL;
48 static Evas_Object *naviframe = NULL;
49
50 // Function to update the log in UI
51 void *updateContainerLog(void *data)
52 {
53     string *log = (string *)data;
54     // Show the log
55     elm_entry_entry_append(log_entry, (*log).c_str());
56     elm_entry_cursor_end_set(log_entry);
57     return NULL;
58 }
59
60 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
61 {
62     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
63     elm_list_item_selected_set(it, EINA_FALSE);
64 }
65
66 static void onDestroy()
67 {
68     g_containerResource = NULL;
69 }
70
71 void onContainerDiscovered(std::shared_ptr<RCSRemoteResourceObject> foundResource)
72 {
73     dlog_print(DLOG_INFO, LOG_TAG, "#### onResourceDiscovered callback");
74
75     std::string resourceURI = foundResource->getUri();
76     std::string hostAddress = foundResource->getAddress();
77
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());
80
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,
87                                           &logMessage);
88
89     g_containerResource = foundResource;
90
91     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);
92     g_containerCond.notify_all();
93 }
94
95 void *showContainerAPIs(void *data)
96 {
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);
100     if (!count)
101     {
102         elm_list_item_append(listnew, "1. Find Light resource", NULL, NULL,
103                              findLight, NULL);
104
105         elm_list_item_append(listnew, "1. Find Softsensor resource", NULL, NULL,
106                              findSoftsensor, NULL);
107
108         elm_list_go(listnew);
109     }
110     return NULL;
111 }
112
113 static void findLight(void *data, Evas_Object *obj, void *event_info)
114 {
115     dlog_print(DLOG_INFO, LOG_TAG, "#### Wait 2 seconds until discovered");
116
117     RCSDiscoveryManager::getInstance()->discoverResource(RCSAddress::multicast(), targetLightUri,
118             &onContainerDiscovered);
119
120     std::unique_lock<std::mutex> lck(g_containerMtx);
121     g_containerCond.wait_for(lck, std::chrono::seconds(2));
122
123     dlog_print(DLOG_INFO, LOG_TAG, "#### Light Discovery over");
124 }
125
126 static void findSoftsensor(void *data, Evas_Object *obj, void *event_info)
127 {
128     dlog_print(DLOG_INFO, LOG_TAG, "#### Wait 2 seconds until discovered");
129
130     RCSDiscoveryManager::getInstance()->discoverResource(RCSAddress::multicast(), targetSoftsensorUri,
131             &onContainerDiscovered);
132
133     std::unique_lock<std::mutex> lck(g_containerMtx);
134     g_containerCond.wait_for(lck, std::chrono::seconds(2));
135
136     dlog_print(DLOG_INFO, LOG_TAG, "#### Softsensor Discovery over");
137 }
138
139 static Eina_Bool
140 naviframe_pop_cb(void *data, Elm_Object_Item *it)
141 {
142     onDestroy();
143
144     if (NULL != log_entry)
145     {
146         evas_object_del(log_entry);
147         log_entry = NULL;
148     }
149     if (NULL != listnew)
150     {
151         evas_object_del(listnew);
152         listnew = NULL;
153     }
154     return EINA_TRUE;
155 }
156
157 // Method to set up server screens
158 void containerCreateUI(void *data, Evas_Object *obj, void *event_info)
159 {
160     Evas_Object *layout;
161     Evas_Object *scroller;
162     Evas_Object *nf = (Evas_Object *)data;
163     Elm_Object_Item *nf_it;
164     naviframe = nf;
165
166     // Scroller
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);
170
171     // Layout
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);
175
176     elm_object_content_set(scroller, layout);
177
178     // List
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);
184
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);
193
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);
196
197     // Show the UI list of group APIs
198     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showContainerAPIs, NULL);
199 }