[Mediator]Java SDK for the new C++ APIs
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / tizen / EnrolleeGUISample / src / enrollee_wifi.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 "enrollee_wifi.h"
22 #include <pthread.h>
23 #include "logger.h"
24 #include <iostream>
25
26 using namespace std;
27
28 static Evas_Object *log_entry = NULL;
29 static Evas_Object *list = NULL;
30 static Evas_Object *naviframe = NULL;
31
32 // Function to update the log in UI
33 void *updateGroupLog(void *data)
34 {
35     string *log = (string *) data;
36     *log = *log + "<br>";
37     // Show the log
38     elm_entry_entry_append(log_entry, (*log).c_str());
39     elm_entry_cursor_end_set(log_entry);
40     return NULL;
41 }
42
43 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
44
45 {
46     string logmessage = "callback!!! in app";
47     OC_LOG(INFO,LOG_TAG,"callback!!! in app");
48
49     if (esResult == ES_OK)
50     {
51         if (!g_OnBoardingSucceeded)
52         {
53             OC_LOG(INFO,LOG_TAG,"Device is successfully OnBoarded");
54             logmessage += "Device is successfully OnBoarded";
55             g_OnBoardingSucceeded = true;
56         }
57         else if (g_OnBoardingSucceeded & enrolleeState == ES_ON_BOARDED_STATE)
58         {
59             OC_LOG(INFO,LOG_TAG,"Device is successfully OnBoared with SoftAP");
60             logmessage += "Device is successfully OnBoared with SoftAP";
61             g_ProvisioningSucceeded = true;
62         }
63
64         else if (enrolleeState == ES_PROVISIONED_STATE)
65         {
66             OC_LOG(INFO,LOG_TAG,"Device is provisioned");
67             logmessage += "Device is provisioned";
68             g_ProvisioningSucceeded = true;
69         }
70
71     }
72     else if (esResult == ES_ERROR)
73     {
74         if (g_OnBoardingSucceeded)
75         {
76             OC_LOG_V(ERROR,LOG_TAG,
77                     "Failure in Provisioning. \
78                                         Current Enrollee State: %d",
79                     enrolleeState);
80             logmessage += "Failure in Provisioning\n";
81             g_OnBoardingSucceeded = false;
82
83         }
84         else if (g_ProvisioningSucceeded)
85         {
86             OC_LOG_V(ERROR,LOG_TAG,
87                     "Failure in connect to target network. \
88                                         Current Enrollee State: %d",
89                     enrolleeState);
90             logmessage += "Failure in connect to target network\n";
91             g_ProvisioningSucceeded = false;
92         }
93     }
94
95     ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
96 }
97
98 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
99 {
100     Elm_Object_Item *it = (Elm_Object_Item *) event_info;
101     elm_list_item_selected_set(it, EINA_FALSE);
102 }
103
104 void copy_files()
105 {
106     dlog_print(DLOG_DEBUG, LOG_TAG, "#####text=%s", app_get_data_path());
107     FILE *source, *target;
108     char ch, *source_file, *target_file;
109     source_file = "/opt/usr/apps/org.tizen.esenrolleesample/res/oic_svr_db_server.json";
110     target_file = "/opt/usr/media/Images/oic_svr_db_server.json";
111     source = fopen(source_file, "r");
112
113     if (source == NULL)
114     {
115         dlog_print(DLOG_DEBUG, LOG_TAG, "source is null");
116     }
117
118     target = fopen(target_file, "w");
119
120     if (target == NULL)
121     {
122         dlog_print(DLOG_DEBUG, LOG_TAG, "target is null");
123         fclose(source);
124     }
125
126     while ((ch = fgetc(source)) != ';')
127         fputc(ch, target);
128     fputc(EOF, target);
129     dlog_print(DLOG_DEBUG, LOG_TAG, "File copied successfully.\n");
130
131     fclose(source);
132     fclose(target);
133 }
134 static void init_easysetup(void *data, Evas_Object *obj, void *event_info)
135 {
136
137     string logmessage = "Init Easysetup\n";
138     copy_files();
139     if (InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
140     {
141         OC_LOG(ERROR,LOG_TAG,"OnBoarding Failed");
142         logmessage = logmessage + "OnBoarding Failed. Connect to " + ssid + " pasword= " + passwd;
143         ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
144         return;
145
146     }
147     g_isInitialized = true;
148     OC_LOG_V(ERROR,LOG_TAG, "OnBoarding succeeded. Successfully connected to ssid : %s",
149             ssid);
150     logmessage = logmessage + "OnBoarding succeeded. Successfully connected to ssid:" + ssid;
151     ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
152 }
153
154 void StartProvisioning(void *data, Evas_Object *obj, void *event_info)
155 {
156     OC_LOG(INFO,LOG_TAG,"StartProvisioning");
157     string logmessage = "StartProvisioning\n";
158
159     if (InitProvisioning() == ES_ERROR)
160     {
161         OC_LOG(ERROR,LOG_TAG,"Init Provisioning Failed");
162         logmessage += "Init Provisioning Failed\n";
163         return;
164     }
165
166     pthread_t thread_handle;
167
168     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
169     {
170         OC_LOG(ERROR,LOG_TAG,"Thread creation failed");
171
172     }
173
174     ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
175 }
176 void *listeningFunc(void*)
177 {
178     while (true)
179     {
180         OCStackResult result;
181
182         result = OCProcess();
183
184         if (result != OC_STACK_OK)
185         {
186             OC_LOG(ERROR,LOG_TAG,"OCStack stop error");
187         }
188
189         // To minimize CPU utilization we may wish to do this with sleep
190
191     }
192     return NULL;
193 }
194 static Eina_Bool naviframe_pop_cb(void *data, Elm_Object_Item *it)
195 {
196
197     if (NULL != log_entry)
198     {
199         evas_object_del(log_entry);
200         log_entry = NULL;
201     }
202     if (NULL != list)
203     {
204         evas_object_del(list);
205         list = NULL;
206     }
207     return EINA_TRUE;
208 }
209 void client_cb(void *data)
210 {
211     Evas_Object *layout;
212     Evas_Object *scroller;
213     Evas_Object *nf = (Evas_Object *) data;
214     Evas_Object *button1;
215     Evas_Object *button2;
216     Elm_Object_Item *nf_it;
217
218     naviframe = nf;
219
220     // Scroller
221     scroller = elm_scroller_add(nf);
222     elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
223     elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
224
225     // Layout
226     layout = elm_layout_add(nf);
227     elm_layout_file_set(layout, ELM_DEMO_EDJ, "group_layout");
228     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
229
230     elm_object_content_set(scroller, layout);
231
232     // Init Easysetup Discovery Button
233     button1 = elm_button_add(layout);
234     elm_object_part_content_set(layout, "button1", button1);
235     elm_object_text_set(button1, "Init Easysetup");
236     evas_object_smart_callback_add(button1, "clicked", init_easysetup, NULL);
237
238     // start Provisioning Discovery Button
239     button2 = elm_button_add(layout);
240     elm_object_part_content_set(layout, "button2", button2);
241     elm_object_text_set(button2, "start Provisioning");
242     evas_object_smart_callback_add(button2, "clicked", StartProvisioning, NULL);
243
244     // List
245     list = elm_list_add(layout);
246     elm_list_mode_set(list, ELM_LIST_COMPRESS);
247     evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
248     elm_object_part_content_set(layout, "list", list);
249     elm_list_go(list);
250
251     // log_entry - textarea for log
252     log_entry = elm_entry_add(layout);
253     elm_entry_scrollable_set(log_entry, EINA_TRUE);
254     elm_entry_editable_set(log_entry, EINA_FALSE);
255     elm_object_part_text_set(log_entry, "elm.guide",
256             "Logs will be updated here!!!<br>Please connect to network :Easysetup before starting!!");
257     evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
258     evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
259     elm_object_part_content_set(layout, "log", log_entry);
260     nf_it = elm_naviframe_item_push(nf, "EasySetup-Enrollee", NULL, NULL, scroller, NULL);
261     elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
262 }
263
264 void StartEnrollee(void *data, Evas_Object *obj, void *event_info)
265 {
266     client_cb(data);
267 }