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;
33 * Function to update the log in UI
35 void *updateGroupLog(void *data)
37 string *log = (string *) data;
40 elm_entry_entry_append(log_entry, (*log).c_str());
41 elm_entry_cursor_end_set(log_entry);
46 * EventCallback function implementation
47 * This callback to be used for Enrollee status change
49 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
51 string logmessage = "callback!!! in app";
52 OIC_LOG(INFO,LOG_TAG,"callback!!! in app");
54 if (esResult == ES_OK)
56 if (!g_OnBoardingSucceeded)
58 OIC_LOG(INFO,LOG_TAG,"Device is successfully OnBoarded");
59 logmessage += "Device is successfully OnBoarded";
60 g_OnBoardingSucceeded = true;
62 else if (g_OnBoardingSucceeded & enrolleeState == ES_ON_BOARDED_STATE)
64 OIC_LOG(INFO,LOG_TAG,"Device is successfully OnBoared with SoftAP");
65 logmessage += "Device is successfully OnBoared with SoftAP";
66 g_ProvisioningSucceeded = true;
69 else if (enrolleeState == ES_PROVISIONED_STATE)
71 OIC_LOG(INFO,LOG_TAG,"Device is provisioned");
72 logmessage += "Device is provisioned";
73 g_ProvisioningSucceeded = true;
77 else if (esResult == ES_ERROR)
79 if (g_OnBoardingSucceeded)
81 OIC_LOG_V(ERROR,LOG_TAG,
82 "Failure in Provisioning.Current Enrollee State: %d", enrolleeState);
83 logmessage += "Failure in Provisioning\n";
84 g_OnBoardingSucceeded = false;
86 else if (g_ProvisioningSucceeded)
88 OIC_LOG_V(ERROR,LOG_TAG,
89 "Failure in connect to target network.Current Enrollee State: %d", enrolleeState);
90 logmessage += "Failure in connect to target network\n";
91 g_ProvisioningSucceeded = false;
94 ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
98 * List_selected_cb callback
100 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
102 Elm_Object_Item *it = (Elm_Object_Item *) event_info;
103 elm_list_item_selected_set(it, EINA_FALSE);
107 * This function initializes easysetup and does onboarding
109 static void init_easysetup(void *data, Evas_Object *obj, void *event_info)
111 string logmessage = "Init Easysetup\n";
112 if (InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
114 OIC_LOG(ERROR,LOG_TAG,"OnBoarding Failed");
115 logmessage = logmessage + "OnBoarding Failed. Connect to " + ssid + " pasword= " + passwd;
116 ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
120 g_isInitialized = true;
121 OIC_LOG_V(ERROR,LOG_TAGLOG_TAG, "OnBoarding succeeded. Successfully connected to ssid : %s",
123 logmessage = logmessage + "OnBoarding succeeded. Successfully connected to ssid:" + ssid;
124 ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
128 * Initialises and Creates provisioning resource
130 void StartProvisioning(void *data, Evas_Object *obj, void *event_info)
132 OIC_LOG(INFO,LOG_TAG"StartProvisioning");
133 string logmessage = "StartProvisioning\n";
135 if (InitProvisioning() == ES_ERROR)
137 OIC_LOG(ERROR,LOG_TAG,"Init Provisioning Failed");
138 logmessage += "Init Provisioning Failed\n";
142 pthread_t thread_handle;
143 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
145 OIC_LOG(ERROR,LOG_TAG,"Thread creation failed");
147 ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
151 * Thread to execute OCProcess in a loop
153 void *listeningFunc(void*)
155 OCStackResult result;
158 result = OCProcess();
159 if (result != OC_STACK_OK)
161 OIC_LOG(ERROR,LOG_TAG,"OCStack stop error");
164 // To minimize CPU utilization we may wish to do this with sleep
171 * naviframe_pop_cb callback impl
173 static Eina_Bool naviframe_pop_cb(void *data, Elm_Object_Item *it)
175 if (NULL != log_entry)
177 evas_object_del(log_entry);
182 evas_object_del(list);
189 * Client callback: client_cb impl
191 void client_cb(void *data)
194 Evas_Object *scroller;
195 Evas_Object *nf = (Evas_Object *) data;
196 Evas_Object *button1;
197 Evas_Object *button2;
198 Elm_Object_Item *nf_it;
203 scroller = elm_scroller_add(nf);
204 elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
205 elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
208 layout = elm_layout_add(nf);
209 elm_layout_file_set(layout, ELM_DEMO_EDJ, "group_layout");
210 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
212 elm_object_content_set(scroller, layout);
214 // Start Discovery Button
215 button1 = elm_button_add(layout);
216 elm_object_part_content_set(layout, "button1", button1);
217 elm_object_text_set(button1, "Init Easysetup");
218 evas_object_smart_callback_add(button1, "clicked", init_easysetup, NULL);
220 // Cancel Discovery Button
221 button2 = elm_button_add(layout);
222 elm_object_part_content_set(layout, "button2", button2);
223 elm_object_text_set(button2, "start Provisioning");
224 evas_object_smart_callback_add(button2, "clicked", StartProvisioning, NULL);
227 list = elm_list_add(layout);
228 elm_list_mode_set(list, ELM_LIST_COMPRESS);
229 evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
230 elm_object_part_content_set(layout, "list", list);
233 // log_entry - textarea for log
234 log_entry = elm_entry_add(layout);
235 elm_entry_scrollable_set(log_entry, EINA_TRUE);
236 elm_entry_editable_set(log_entry, EINA_FALSE);
237 elm_object_part_text_set(log_entry, "elm.guide",
238 "Logs will be updated here!!!<br>Please connect to network :Easysetup before starting!!");
239 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
240 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
241 elm_object_part_content_set(layout, "log", log_entry);
243 nf_it = elm_naviframe_item_push(nf, "EasySetup-Enrollee", NULL, NULL, scroller, NULL);
244 elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
250 void StartEnrollee(void *data, Evas_Object *obj, void *event_info)