Easysetup - Change OC_LOG to OIC_LOG & warning fixes
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / tizen / Enrolleesample / src / enrolleewifi.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 /**
33  * Function to update the log in UI
34  */
35 void *updateGroupLog(void *data)
36 {
37     string *log = (string *) data;
38     *log = *log + "<br>";
39     // Show the log
40     elm_entry_entry_append(log_entry, (*log).c_str());
41     elm_entry_cursor_end_set(log_entry);
42     return NULL;
43 }
44
45 /**
46  * EventCallback function implementation
47  * This callback to be used for Enrollee status change
48  */
49 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
50 {
51     string logmessage = "callback!!! in app";
52     OIC_LOG(INFO,LOG_TAG,"callback!!! in app");
53
54     if (esResult == ES_OK)
55     {
56         if (!g_OnBoardingSucceeded)
57         {
58             OIC_LOG(INFO,LOG_TAG,"Device is successfully OnBoarded");
59             logmessage += "Device is successfully OnBoarded";
60             g_OnBoardingSucceeded = true;
61         }
62         else if (g_OnBoardingSucceeded & enrolleeState == ES_ON_BOARDED_STATE)
63         {
64             OIC_LOG(INFO,LOG_TAG,"Device is successfully OnBoared with SoftAP");
65             logmessage += "Device is successfully OnBoared with SoftAP";
66             g_ProvisioningSucceeded = true;
67         }
68
69         else if (enrolleeState == ES_PROVISIONED_STATE)
70         {
71             OIC_LOG(INFO,LOG_TAG,"Device is provisioned");
72             logmessage += "Device is provisioned";
73             g_ProvisioningSucceeded = true;
74         }
75
76     }
77     else if (esResult == ES_ERROR)
78     {
79         if (g_OnBoardingSucceeded)
80         {
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;
85         }
86         else if (g_ProvisioningSucceeded)
87         {
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;
92         }
93     }
94     ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
95 }
96
97 /**
98  * List_selected_cb callback
99  */
100 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
101 {
102     Elm_Object_Item *it = (Elm_Object_Item *) event_info;
103     elm_list_item_selected_set(it, EINA_FALSE);
104 }
105
106 /**
107  * This function initializes easysetup and does onboarding
108  */
109 static void init_easysetup(void *data, Evas_Object *obj, void *event_info)
110 {
111     string logmessage = "Init Easysetup\n";
112     if (InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
113     {
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);
117         return;
118
119     }
120     g_isInitialized = true;
121     OIC_LOG_V(ERROR,LOG_TAGLOG_TAG, "OnBoarding succeeded. Successfully connected to ssid : %s",
122             ssid);
123     logmessage = logmessage + "OnBoarding succeeded. Successfully connected to ssid:" + ssid;
124     ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
125 }
126
127 /**
128  * Initialises and Creates provisioning resource
129  */
130 void StartProvisioning(void *data, Evas_Object *obj, void *event_info)
131 {
132     OIC_LOG(INFO,LOG_TAG"StartProvisioning");
133     string logmessage = "StartProvisioning\n";
134
135     if (InitProvisioning() == ES_ERROR)
136     {
137         OIC_LOG(ERROR,LOG_TAG,"Init Provisioning Failed");
138         logmessage += "Init Provisioning Failed\n";
139         return;
140     }
141
142     pthread_t thread_handle;
143     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
144     {
145         OIC_LOG(ERROR,LOG_TAG,"Thread creation failed");
146     }
147     ecore_main_loop_thread_safe_call_sync((void * (*)(void *))updateGroupLog, &logmessage);
148 }
149
150 /**
151  * Thread to execute OCProcess in a loop
152  */
153 void *listeningFunc(void*)
154 {
155     OCStackResult result;
156     while (true)
157     {
158         result = OCProcess();
159         if (result != OC_STACK_OK)
160         {
161             OIC_LOG(ERROR,LOG_TAG,"OCStack stop error");
162         }
163
164         // To minimize CPU utilization we may wish to do this with sleep
165         sleep(1);
166     }
167     return NULL;
168 }
169
170 /**
171  * naviframe_pop_cb callback impl
172  */
173 static Eina_Bool naviframe_pop_cb(void *data, Elm_Object_Item *it)
174 {
175     if (NULL != log_entry)
176     {
177         evas_object_del(log_entry);
178         log_entry = NULL;
179     }
180     if (NULL != list)
181     {
182         evas_object_del(list);
183         list = NULL;
184     }
185     return EINA_TRUE;
186 }
187
188 /**
189  * Client callback: client_cb impl
190  */
191 void client_cb(void *data)
192 {
193     Evas_Object *layout;
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;
199
200     naviframe = nf;
201
202     // Scroller
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);
206
207     // Layout
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);
211
212     elm_object_content_set(scroller, layout);
213
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);
219
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);
225
226     // List
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);
231     elm_list_go(list);
232
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);
242
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);
245 }
246
247 /**
248  * Starting Enrollee
249  */
250 void StartEnrollee(void *data, Evas_Object *obj, void *event_info)
251 {
252     client_cb(data);
253 }
254