merge from master branch's latest code.
[apps/core/preloaded/ciss.git] / src / ciss-main.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <appcore-efl.h>
19 #include <aul.h>
20 #include <glib-2.0/glib.h>
21 #include <Ecore.h>
22 #include <app.h>
23 #include <app_service.h>
24
25 #include "ciss-common.h"
26 #include "ciss-util.h"
27 #include "ciss-ussd.h"
28 #include "ciss-resp.h"
29 #include "ciss-tapi-request.h"
30 #include "ciss-parser.h"
31 #include "ciss-widget.h"
32
33 static ciss_appdata_t application_data;
34
35 static Eina_Bool __ciss_app_create(void *data);
36 static void __ciss_app_terminate(void *data);
37 static void __ciss_app_pause(void *data);
38 static void __ciss_app_resume(void *data);
39 static void __ciss_app_service(service_h service, void *data);
40 static int __ciss_relaunch_handler(void *data);
41
42 static Eina_Bool __ciss_app_create(void *data)
43 {
44         DBG("Enter");
45
46         ciss_appdata_t *ad = (ciss_appdata_t *)data;
47         _ciss_tapi_event_setup(ad);
48         ad->win_main = NULL;
49         ad->conformant = NULL;
50         ad->layout_main = NULL;
51         ad->bg = NULL;
52         ad->popup = NULL;
53         ad->end_key_event_handler = NULL;
54         ad->is_sat_request = EINA_FALSE;
55         ad->service_handle = NULL;
56         memset(&(ad->tapi_event), 0, sizeof(ciss_tapi_event_t));
57         memset(&(ad->sat_terminal_response), 0, sizeof(ciss_sat_terminal_response_t));
58         _ciss_geinlist_item_class_new(ad);
59
60         return EINA_TRUE;
61 }
62
63 static void __ciss_app_terminate(void *data)
64 {
65         DBG("Enter");
66
67         ciss_appdata_t *ad = data;
68
69         _ciss_geinlist_item_class_free(ad);
70
71         if (ad->layout_main) {
72                 evas_object_del(ad->layout_main);
73                 ad->layout_main = NULL;
74         }
75
76         if (ad->conformant) {
77                 evas_object_del(ad->conformant);
78                 ad->conformant = NULL;
79         }
80
81         if (ad->bg) {
82                 evas_object_del(ad->bg);
83                 ad->bg = NULL;
84         }
85
86         if (ad->win_main) {
87                 evas_object_del(ad->win_main);
88                 if (ad->end_key_event_handler) {
89                         ecore_event_handler_del(ad->end_key_event_handler);
90                 }
91         }
92
93         _ciss_deregister_tel_event(ad);
94         DBG("Leave");
95 }
96
97 static void __ciss_app_pause(void *data)
98 {
99         DBG("Enter");
100
101         ciss_appdata_t *ad = (ciss_appdata_t *)data;
102         memset(ad->c_item, 0, sizeof(ad->c_item));
103
104         if ((ad->resp_ss_type == CISS_SERVICE_USSD) && (ad->ussd_action_require)) {
105                 ciss_mmi_context_t mmi_ctx;
106                 memset(&mmi_ctx, 0x0, sizeof(ciss_mmi_context_t));
107                 mmi_ctx.opcode = processUnstructuredSS_Request;
108                 mmi_ctx.ussd_type = TAPI_SS_USSD_TYPE_USER_REL;
109
110                 _ciss_send_tapi_request(&mmi_ctx, ad);
111         }
112
113         elm_exit();
114 }
115
116 static void __ciss_app_resume(void *data)
117 {
118         DBG("Enter");
119 }
120
121 static void __ciss_app_service(service_h service, void *data)
122 {
123         DBG("Enter");
124
125         ciss_appdata_t *ad = data;
126         int err = SERVICE_ERROR_NONE;
127
128         if (ad == NULL) {
129                 ERR("ad == NULL");
130                 return;
131         }
132
133         err = service_clone(&(ad->service_handle), service);
134         if (err != SERVICE_ERROR_NONE) {
135                 ERR("service_clone error - (%d)\n", err);
136
137                 if (ad->service_handle) {
138                         service_destroy(ad->service_handle);
139                 }
140                 ad->service_handle = NULL;
141                 elm_exit();
142                 return;
143         }
144
145         if (ad->win_main) {
146                 elm_win_activate(ad->win_main);
147         }
148
149         __ciss_relaunch_handler(data);
150
151         if (ad->service_handle) {
152                 service_destroy(ad->service_handle);
153                 ad->service_handle = NULL;
154         }
155
156         DBG("Leave");
157 }
158
159 static int __ciss_relaunch_handler(void *data)
160 {
161         DBG("Enter");
162
163         int ret = 0;
164         int event_type = 0;
165 #ifdef _CISS_USE_PARAM
166         int event_status = 0;
167         int encoded_data_length = 0;
168         char *key_status = NULL;
169         char *key_encoded_data_length = NULL;
170 #endif
171         int err = SERVICE_ERROR_NONE;
172
173         ciss_appdata_t *ad = (ciss_appdata_t *)data;
174         Evas_Object *popup;
175         char *launching_mode = NULL;
176         char *req_string = NULL;
177         char *key_event_type = NULL;
178         char *key_encoded_data = NULL;
179
180         err = service_get_extra_data(ad->service_handle, "CISS_LAUNCHING_MODE", &launching_mode);
181         if (err != SERVICE_ERROR_NONE) {
182                 DBG("service_get_extra_data for CISS_LAUNCHING_MODE failed - (%d)\n", err);
183                 return -1;
184         }
185
186         if (launching_mode == NULL) {
187                 DBG("Cannot get launching_mode value...\n");
188                 return -1;
189         }
190
191         if (strncmp(launching_mode, "REQ", 3) == 0) {
192                 err = service_get_extra_data(ad->service_handle, "CISS_REQ_STRING", &req_string);
193                 if (err != SERVICE_ERROR_NONE) {
194                         DBG("service_get_extra_data for CISS_LAUNCHING_MODE failed - (%d)\n", err);
195                 }
196                 retv_if(req_string == NULL, -1);
197                 DBG("CISS_REQ_STRING=%s", req_string);
198                 strncpy(ad->req_string, req_string, REQ_STRING_MAX_LEN - 1);
199                 ciss_err_t ciss_err_code;
200
201                 if (ad->win_main == NULL) {
202                         Evas_Object *win;
203                         win = _ciss_create_win(PACKAGE);
204                         retv_if(win == NULL, -1);
205                         ad->win_main = win;
206                         evas_object_show(win);
207                         ad->end_key_event_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _ciss_on_end_key_down, NULL);
208                 }
209
210                 if (ad->conformant == NULL) {
211                         Evas_Object *conform = NULL;
212                         conform = _ciss_create_conformant(ad->win_main);
213                         retv_if(conform == NULL, -1);
214                         ad->conformant = conform;
215                         evas_object_show(conform);
216                 }
217
218                 if (ad->popup != NULL)  {
219                         evas_object_del(ad->popup);
220                         ad->popup = NULL;
221                 }
222
223                 if (ad->layout_main == NULL) {
224                         Evas_Object *ly = NULL;
225                         ly = _ciss_create_layout_main(ad->conformant);
226                         retv_if(ly == NULL, -1);
227                         ad->layout_main = ly;
228                         evas_object_show(ly);
229                 }
230
231                 DBG("scale = %f", elm_config_scale_get());
232
233                 popup = _ciss_create_noti_popup(ad->layout_main, CISS_STR_REQUESTING, NULL, POPUP_DISPLAY_DURATION, ad);
234                 ad->popup = popup;
235                 ciss_mmi_context_t      mmi_ctx;
236
237                 ciss_err_code = _ciss_parse_req_string(req_string, &mmi_ctx);
238
239                 if (ciss_err_code != CISS_ERR_NONE) {
240                         _ciss_create_error_popup(ad->layout_main, ciss_err_code, 2.0, ad);
241                 } else {
242                         ciss_err_code = _ciss_send_tapi_request(&mmi_ctx, ad);
243                         DBG("ciss_err_code(_ciss_send_tapi_request) = %d", ciss_err_code);
244                         if (ciss_err_code != CISS_ERR_NONE) {
245                                 _ciss_create_error_popup(ad->layout_main, ciss_err_code, 2.0, ad);
246                         }
247                 }
248         } else if (strncmp(launching_mode, "RESP", 4)== 0) {
249                 err = service_get_extra_data(ad->service_handle, "KEY_EVENT_TYPE", &key_event_type);
250                 if (err != SERVICE_ERROR_NONE) {
251                         DBG("service_get_extra_data for KEY_EVENT_TYPE failed - (%d)\n", err);
252                 }
253                 event_type = atoi(key_event_type);
254                 ad->tapi_event.event_type = event_type;
255 #ifdef _CISS_USE_PARAM
256                 err = service_get_extra_data(ad->service_handle, "KEY_STATUS", &key_status);
257                 if (err != SERVICE_ERROR_NONE) {
258                         DBG("service_get_extra_data for KEY_STATUS failed - (%d)\n", err);
259                 }
260                 event_status = atoi(key_status);
261                 ad->tapi_event.status = event_status;
262
263                 err = service_get_extra_data(ad->service_handle, "KEY_ENCODED_DATA_LENGTH", &key_encoded_data_length);
264                 if (err != SERVICE_ERROR_NONE) {
265                         DBG("service_get_extra_data for KEY_ENCODED_DATA_LENGTH failed - (%d)\n", err);
266                 }
267                 encoded_data_length = atoi(key_encoded_data_length);
268 #endif
269                 err = service_get_extra_data(ad->service_handle, "KEY_ENCODED_DATA", &key_encoded_data);
270                 if (err != SERVICE_ERROR_NONE) {
271                         DBG("service_get_extra_data for KEY_ENCODED_DATA failed - (%d)\n", err);
272                 }
273                 gchar *decoded_data;
274                 gsize decoded_data_length;
275
276                 decoded_data = (gchar *)g_base64_decode((gchar *)key_encoded_data, &decoded_data_length);
277
278                 ad->tapi_event.data_length = decoded_data_length;
279                 ad->tapi_event.p_data = decoded_data;
280
281                 DBG("Received Event=0x%x", event_type);
282
283                 switch (event_type) {
284                 case TAPI_EVENT_SS_USSD_IND:
285                         _ciss_tapi_ussd_resp_handle(0, ad->tapi_event.p_data, ad);
286                         break;
287                 case TAPI_SAT_CMD_TYPE_SEND_USSD:
288                         _ciss_on_tapi_sat_ussd_event(ad->tapi_event.p_data, ad);
289                         break;
290                 case TAPI_SAT_CMD_TYPE_SEND_SS:
291                         _ciss_on_tapi_sat_ss_event(ad->tapi_event.p_data, ad);
292                         break;
293                 default:
294                         ret = -1;
295                         break;
296                 }
297         } else {
298                 ERR("wrong exec type");
299                 ret = -1;
300         }
301
302         if (launching_mode) {
303                 free(launching_mode);
304                 launching_mode = NULL;
305         }
306         if (req_string) {
307                 free(req_string);
308                 req_string = NULL;
309         }
310         if (key_event_type) {
311                 free(key_event_type);
312                 key_event_type = NULL;
313         }
314 #ifdef _CISS_USE_PARAM
315         if (key_status) {
316                 free(key_status);
317                 key_status = NULL;
318         }
319         if (key_encoded_data_length) {
320                 free(key_encoded_data_length);
321                 key_encoded_data_length = NULL;
322         }
323 #endif
324         if (key_encoded_data) {
325                 free(key_encoded_data);
326                 key_encoded_data = NULL;
327         }
328
329         DBG("Leave");
330         return ret;
331 }
332
333 int main(int argc, char *argv[])
334 {
335         DBG("Enter");
336
337         app_event_callback_s event_callback = {0,};
338
339         event_callback.create = __ciss_app_create;
340         event_callback.terminate = __ciss_app_terminate;
341         event_callback.pause = __ciss_app_pause;
342         event_callback.resume = __ciss_app_resume;
343         event_callback.service = __ciss_app_service;
344
345         event_callback.low_memory = NULL;
346         event_callback.low_battery = NULL;
347         event_callback.device_orientation = NULL;
348         event_callback.language_changed = NULL;
349         event_callback.region_format_changed = NULL;
350
351         int ret = APP_ERROR_NONE;
352
353         memset(&application_data, 0x0, sizeof(ciss_appdata_t));
354
355         ret = app_efl_main(&argc, &argv, &event_callback, &application_data);
356         if (ret != APP_ERROR_NONE) {
357                 DBG("app_efl_main() is failed (%d)\n", ret);
358         }
359
360         return ret;
361 }
362