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 "enrollee_wifi.h"
28 static Evas_Object *log_entry = NULL;
29 static Evas_Object *list = NULL;
30 static Evas_Object *naviframe = NULL;
32 // Function to update the log in UI
33 void *updateGroupLog(void *data)
35 string *log = (string *) data;
38 elm_entry_entry_append(log_entry, (*log).c_str());
39 elm_entry_cursor_end_set(log_entry);
43 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
46 string logmessage = "callback!!! in app";
47 OC_LOG(INFO,LOG_TAG,"callback!!! in app");
49 if (esResult == ES_OK)
51 if (!g_OnBoardingSucceeded)
53 OC_LOG(INFO,LOG_TAG,"Device is successfully OnBoarded");
54 logmessage += "Device is successfully OnBoarded";
55 g_OnBoardingSucceeded = true;
57 else if (g_OnBoardingSucceeded & enrolleeState == ES_ON_BOARDED_STATE)
59 OC_LOG(INFO,LOG_TAG,"Device is successfully OnBoared with SoftAP");
60 logmessage += "Device is successfully OnBoared with SoftAP";
61 g_ProvisioningSucceeded = true;
64 else if (enrolleeState == ES_PROVISIONED_STATE)
66 OC_LOG(INFO,LOG_TAG,"Device is provisioned");
67 logmessage += "Device is provisioned";
68 g_ProvisioningSucceeded = true;
72 else if (esResult == ES_ERROR)
74 if (g_OnBoardingSucceeded)
76 OC_LOG_V(ERROR,LOG_TAG,
77 "Failure in Provisioning. \
78 Current Enrollee State: %d",
80 logmessage += "Failure in Provisioning\n";
81 g_OnBoardingSucceeded = false;
84 else if (g_ProvisioningSucceeded)
86 OC_LOG_V(ERROR,LOG_TAG,
87 "Failure in connect to target network. \
88 Current Enrollee State: %d",
90 logmessage += "Failure in connect to target network\n";
91 g_ProvisioningSucceeded = false;
95 ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
98 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
100 Elm_Object_Item *it = (Elm_Object_Item *) event_info;
101 elm_list_item_selected_set(it, EINA_FALSE);
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");
115 dlog_print(DLOG_DEBUG, LOG_TAG, "source is null");
118 target = fopen(target_file, "w");
122 dlog_print(DLOG_DEBUG, LOG_TAG, "target is null");
126 while ((ch = fgetc(source)) != ';')
129 dlog_print(DLOG_DEBUG, LOG_TAG, "File copied successfully.\n");
134 static void init_easysetup(void *data, Evas_Object *obj, void *event_info)
137 string logmessage = "Init Easysetup\n";
139 if (InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
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);
147 g_isInitialized = true;
148 OC_LOG_V(ERROR,LOG_TAG, "OnBoarding succeeded. Successfully connected to ssid : %s",
150 logmessage = logmessage + "OnBoarding succeeded. Successfully connected to ssid:" + ssid;
151 ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
154 void StartProvisioning(void *data, Evas_Object *obj, void *event_info)
156 OC_LOG(INFO,LOG_TAG,"StartProvisioning");
157 string logmessage = "StartProvisioning\n";
159 if (InitProvisioning() == ES_ERROR)
161 OC_LOG(ERROR,LOG_TAG,"Init Provisioning Failed");
162 logmessage += "Init Provisioning Failed\n";
166 pthread_t thread_handle;
168 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
170 OC_LOG(ERROR,LOG_TAG,"Thread creation failed");
174 ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
176 void *listeningFunc(void*)
180 OCStackResult result;
182 result = OCProcess();
184 if (result != OC_STACK_OK)
186 OC_LOG(ERROR,LOG_TAG,"OCStack stop error");
189 // To minimize CPU utilization we may wish to do this with sleep
194 static Eina_Bool naviframe_pop_cb(void *data, Elm_Object_Item *it)
197 if (NULL != log_entry)
199 evas_object_del(log_entry);
204 evas_object_del(list);
209 void client_cb(void *data)
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;
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);
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);
230 elm_object_content_set(scroller, layout);
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);
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);
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);
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);
264 void StartEnrollee(void *data, Evas_Object *obj, void *event_info)