2 * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <Elementary.h>
27 #include <tapi_common.h>
30 #include <ITapiCall.h>
31 #include <TelNetwork.h>
32 #include "setting_utils.h"
34 #include "list_util.h"
35 #include "quickpanel-ui.h"
38 #include "handler_controller.h"
40 #define TAPI_HANDLE_MAX 2
44 TapiHandle *handle[TAPI_HANDLE_MAX+1];
45 Eina_Bool sim_card_ready[2];
49 int call_state; // 0:none, 1:call
56 .sim_card_ready[0] = EINA_FALSE,
57 .sim_card_ready[1] = EINA_FALSE,
62 static int _sim_controller_get_call_state(void);
64 static void register_sim_callbacks();
65 static void unregister_sim_callbacks();
67 static void __sim_controller_call_state_changed_cb(keynode_t *key, void *data)
69 int call_state = _sim_controller_get_call_state();
71 if (sim_state_info.call_state != call_state)
73 DBG("Call state changed[%d]", call_state);
74 sim_state_info.call_state = call_state;
78 static int _sim_controller_get_call_state(void)
83 ret = vconf_get_int(VCONFKEY_CALL_STATE, &value);
86 ERR("Failed to get call state");
90 if (value == VCONFKEY_CALL_OFF)
95 DBG("Call status[%d]", value);
99 static char *get_sim_plmn(TapiHandle *handle)
102 char *network_name = NULL;
104 /* Reading Network (PLMN) name - ‘string’ type Property */
105 ret = tel_get_property_string(handle,
106 TAPI_PROP_NETWORK_NETWORK_NAME, &network_name);
107 if(ret == TAPI_API_SUCCESS)
109 /* ‘network_name’ contains valid Network name based on Display condition */
114 ERR("Sim = %p PLMN = ERROR[%d]", handle, ret);
115 /* get property failed */
121 static char *get_sim_spn(TapiHandle *handle)
124 char *spn_name = NULL;
127 /* Reading SPN name - ‘string’ type Property */
128 ret = tel_get_property_string(handle,
129 TAPI_PROP_NETWORK_SPN_NAME, &spn_name);
130 if(ret == TAPI_API_SUCCESS)
132 /* ‘spn_name’ contains valid Service provider name */
137 ERR("Sim = %p SPN = ERROR[%d]", handle, ret);
138 /* get property failed */
143 static char *get_plmn_spn_network(int handle_num, TapiHandle *handle)
145 int ret = TAPI_API_SUCCESS;
146 int service_type = TAPI_NETWORK_SERVICE_TYPE_UNKNOWN;
147 int name_option = TAPI_NETWORK_DISP_INVALID;
150 char buf[1024] = { 0, };
153 ret = tel_get_property_int(handle, TAPI_PROP_NETWORK_SERVICE_TYPE, &service_type);
154 if (ret != TAPI_API_SUCCESS)
156 ERR("Failed to get service type[%d]", ret);
159 if (service_type >= TAPI_NETWORK_SERVICE_TYPE_2G)
161 // get network name option
162 ret = tel_get_property_int(handle, TAPI_PROP_NETWORK_NAME_OPTION, &name_option);
163 if (ret != TAPI_API_SUCCESS)
165 ERR("Failed to get name option[%d]", ret);
170 case TAPI_NETWORK_DISP_SPN:
171 spn = get_sim_spn(handle);
172 if (spn != NULL && spn[0] != 0)
174 INFO("PLMN/SPN - Sim %p using SPN: %s", handle, spn);
175 snprintf(buf, sizeof(buf), "%s", spn);
178 case TAPI_NETWORK_DISP_PLMN:
179 plmn = get_sim_plmn(handle);
180 if (plmn != NULL && plmn[0] != 0)
182 INFO("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
183 snprintf(buf, sizeof(buf), "%s", plmn);
186 case TAPI_NETWORK_DISP_SPN_PLMN:
187 spn = get_sim_spn(handle);
188 plmn = get_sim_plmn(handle);
189 if (spn != NULL && spn[0] != 0 && plmn != NULL && plmn[0] != 0)
191 INFO("PLMN/SPN - Sim %p using SPN: %s, PLMN: %s", handle, spn, plmn);
192 snprintf(buf, sizeof(buf), "%s - %s", plmn, spn);
194 else if (spn != NULL && spn[0] != 0)
196 INFO("PLMN/SPN - Sim %p using SPN: %s", handle, spn);
197 snprintf(buf, sizeof(buf), "%s", spn);
199 else if (plmn != NULL && plmn[0] != 0)
201 INFO("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
202 snprintf(buf, sizeof(buf), "%s", plmn);
206 ERR("Invalid name option[%d]", name_option);
207 plmn = get_sim_plmn(handle);
208 if (plmn != NULL && plmn[0] != 0)
210 INFO("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
211 snprintf(buf, sizeof(buf), "%s", plmn);
218 switch (service_type)
220 case TAPI_NETWORK_SERVICE_TYPE_NO_SERVICE:
221 snprintf(buf, sizeof(buf), "%s", _("IDS_IDLE_BODY_NO_SERVICE"));
223 case TAPI_NETWORK_SERVICE_TYPE_EMERGENCY:
224 snprintf(buf, sizeof(buf), "%s", _("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
226 case TAPI_NETWORK_SERVICE_TYPE_SEARCH:
227 snprintf(buf, sizeof(buf), "%s", _("IDS_COM_BODY_SEARCHING"));
230 ERR("invalid service type[%d]", service_type);
231 plmn = get_sim_plmn(handle);
232 if (plmn != NULL && plmn[0] != 0)
234 INFO("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
235 snprintf(buf, sizeof(buf), "%s", plmn);
241 DBG("handle[%d][%p] service_type[%d], name_option[%d] >> [%s]", handle_num, handle, service_type, name_option, buf);
243 if (strlen(buf) == 0)
246 snprintf(buf, sizeof(buf), "%s", _("IDS_IDLE_BODY_NO_SERVICE"));
248 else if (strncasecmp(buf, "No Service", strlen("No Service")) == 0)
250 ERR("USING SPECIAL NETWORK NAME: %s in handle: %d", _("IDS_IDLE_BODY_NO_SERVICE"), handle_num);
251 return strdup(_("IDS_IDLE_BODY_NO_SERVICE"));
253 else if (strncasecmp(buf, "EMERGENCY", strlen("EMERGENCY")) == 0)
255 ERR("USING SPECIAL NETWORK NAME: %s in handle: %d", _("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"), handle_num);
256 return strdup(_("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
258 else if (strncasecmp(buf, "Searching", strlen("Searching")) == 0)
260 ERR("USING SPECIAL NETWORK NAME: %s in handle: %d", _("IDS_COM_BODY_SEARCHING"), handle_num);
261 return strdup(_("IDS_COM_BODY_SEARCHING"));
263 else if (strncasecmp(buf, "SIM Error", strlen("SIM Error")) == 0)
265 ERR("USING SPECIAL NETWORK NAME: %s in handle: %d", _("IDS_IDLE_BODY_INVALID_SIM_CARD"), handle_num);
266 return strdup(_("IDS_IDLE_BODY_INVALID_SIM_CARD"));
268 else if (strncasecmp(buf, "NO SIM", strlen("NO SIM")) == 0)
270 ERR("USING SPECIAL NETWORK NAME: %s in handle: %d", _("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"), handle_num);
271 return strdup(_("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
277 // --------------------------------------------------------------------------------------------
278 static void print_sim_status(TelSimCardStatus_t sim_status, int card_changed)
282 case TAPI_SIM_STATUS_CARD_ERROR :
283 INFO("Sim card status: TAPI_SIM_STATUS_CARD_ERROR");
286 case TAPI_SIM_STATUS_CARD_NOT_PRESENT :
287 INFO("Sim card status: TAPI_SIM_STATUS_CARD_NOT_PRESENT");
290 case TAPI_SIM_STATUS_SIM_INITIALIZING :
291 INFO("Sim card status: TAPI_SIM_STATUS_SIM_INITIALIZING");
294 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
295 INFO("Sim card status: TAPI_SIM_STATUS_SIM_INIT_COMPLETED");
298 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED :
299 INFO("Sim card status: TAPI_SIM_STATUS_SIM_PIN_REQUIRED");
302 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED :
303 INFO("Sim card status: TAPI_SIM_STATUS_SIM_PUK_REQUIRED");
306 case TAPI_SIM_STATUS_CARD_BLOCKED :
307 INFO("Sim card status: TAPI_SIM_STATUS_CARD_BLOCKED");
310 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED :
311 INFO("Sim card status: TAPI_SIM_STATUS_SIM_NCK_REQUIRED");
314 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED :
315 INFO("Sim card status: TAPI_SIM_STATUS_SIM_NSCK_REQUIRED");
318 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED :
319 INFO("Sim card status: TAPI_SIM_STATUS_SIM_SPCK_REQUIRED");
322 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED :
323 INFO("Sim card status: TAPI_SIM_STATUS_SIM_CCK_REQUIRED");
326 case TAPI_SIM_STATUS_CARD_REMOVED :
327 INFO("Sim card status: TAPI_SIM_STATUS_CARD_REMOVED");
330 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED :
331 INFO("Sim card status: TAPI_SIM_STATUS_SIM_LOCK_REQUIRED");
334 case TAPI_SIM_STATUS_CARD_CRASHED :
335 INFO("Sim card status: TAPI_SIM_STATUS_CARD_CRASHED");
338 case TAPI_SIM_STATUS_CARD_POWEROFF :
339 INFO("Sim card status: TAPI_SIM_STATUS_CARD_POWEROFF");
342 case TAPI_SIM_STATUS_UNKNOWN :
343 INFO("Sim card status: TAPI_SIM_STATUS_UNKNOWN");
347 INFO("Sim_card_changed: %d", card_changed);
350 static void get_sim_status()
354 TelSimCardStatus_t sim_status;
357 for(i = 0; i < TAPI_HANDLE_MAX + 1; ++i)
359 if(sim_state_info.handle[i])
361 ret = tel_get_sim_init_info (sim_state_info.handle[i], &sim_status, &card_changed);
364 print_sim_status(sim_status, card_changed);
366 if(sim_status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED ||
367 sim_status == TAPI_SIM_STATUS_SIM_PIN_REQUIRED)
369 if (i < TAPI_HANDLE_MAX)
371 sim_state_info.sim_card_ready[i] = EINA_TRUE;
376 ERR("SIM[%d] is not completed initialization [%d]", i, sim_status);
381 ERR("Could not get sim[%d] status[%d]", i, ret);
387 static void sim_handler_text_set(Eina_Bool flight_mode)
391 // if flight mode, No service
392 quickpanel_handler_text_set(_("IDS_IDLE_BODY_NO_SERVICE"));
394 else if (sim_state_info.sim_card_ready[0] && sim_state_info.sim_card_ready[1])
396 quickpanel_handler_text_set(NULL);
398 else if(sim_state_info.sim_card_ready[0])
400 char *plmn_spn1 = get_plmn_spn_network(0, sim_state_info.handle[0]);
401 quickpanel_handler_text_set(plmn_spn1);
407 else if(sim_state_info.sim_card_ready[1])
409 char *plmn_spn1 = get_plmn_spn_network(1, sim_state_info.handle[1]);
410 quickpanel_handler_text_set(plmn_spn1);
418 quickpanel_handler_text_set(_("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
423 static void init_view()
425 struct appdata *ad = NULL;
427 ad = quickpanel_get_app_data();
434 int flight_mode_state = EINA_FALSE;
435 int ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode_state);
438 ERR("Could not get 'VCONFKEY_TELEPHONY_FLIGHT_MODE' value");
441 sim_handler_text_set(flight_mode_state);
444 /* Initialize TAPI */
445 static void _init_tel()
447 char **cp_list = NULL;
448 unsigned int modem_num = 0;
450 /* Get CP name list – cp_list */
451 cp_list = tel_get_cp_name_list();
455 ERR("Could not get the cp_name_list");
459 while (cp_list[modem_num])
461 /* Initialize TAPI handle */
462 sim_state_info.handle[modem_num] = tel_init(cp_list[modem_num]);
464 if(cp_list[modem_num])
465 ERR("sim_state_info.handle[%d] = %s; ptr = %p", modem_num,
466 cp_list[modem_num], sim_state_info.handle[modem_num]);
468 /* Move to next CP Name in cp_list */
472 sim_state_info.handle[modem_num] = NULL;
478 /* De-initialize TAPI */
479 static void _deinit_tel()
482 while (sim_state_info.handle[i])
484 /* De-initialize TAPI handle */
485 tel_deinit(sim_state_info.handle[i]);
486 sim_state_info.handle[i] = NULL;
488 /* Move to next handle */
493 /* Telephony state change callback */
494 void tel_ready_cb(keynode_t *key, void *data)
496 Eina_Bool status = EINA_FALSE;
498 status = vconf_keynode_get_bool(key);
501 { /* Telephony State - READY */
502 DBG("tel status[%d]", status);
504 register_sim_callbacks();
510 { /* Telephony State – NOT READY */
511 /* De-initialization is optional here (ONLY if required) */
512 ERR("tel status[%d]", status);
514 sim_state_info.sim_card_ready[0] = EINA_FALSE;
515 sim_state_info.sim_card_ready[1] = EINA_FALSE;
517 unregister_sim_callbacks();
521 static void tel_flight_mode_cb(keynode_t *key, void *data)
523 Eina_Bool flight_mode_state = EINA_FALSE;
525 flight_mode_state = vconf_keynode_get_bool(key);
526 sim_handler_text_set(flight_mode_state);
529 // --------------------------------------------------------------------------------------------
530 static void on_sim_card_status_changed(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
532 int handle_num = (int)user_data;
533 int *sim_status = data;
535 ERR("SIM[%p][%d] status[%d], [%d][%d]", handle, handle_num, *sim_status, sim_state_info.sim_card_ready[0], sim_state_info.sim_card_ready[1]);
537 if(*sim_status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED ||
538 *sim_status == TAPI_SIM_STATUS_SIM_PIN_REQUIRED)
540 sim_state_info.sim_card_ready[handle_num] = EINA_TRUE;
544 sim_state_info.sim_card_ready[handle_num] = EINA_FALSE;
550 static void on_plmn_spn_changed(TapiHandle *handle, const char *noti_id,
551 void *data, void *user_data)
555 ERR("handle == NULL");
559 int flight_mode_state = EINA_FALSE;
560 int ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode_state);
563 ERR("Could not get the 'VCONFKEY_TELEPHONY_FLIGHT_MODE' value");
565 sim_handler_text_set(flight_mode_state);
568 static void register_sim_callbacks()
572 for(i = 0; i < TAPI_HANDLE_MAX; ++i)
574 if(sim_state_info.handle[i])
576 ret = tel_register_noti_event (sim_state_info.handle[i],
577 TAPI_NOTI_SIM_STATUS, on_sim_card_status_changed, (void*)i);
579 if (ret != TAPI_API_SUCCESS)
581 ERR("Failed to register 'on_sim_card_status_changed' callback to handle[%d][%d]", i, ret);
585 ERR("SIM card status changed event registered");
588 ret = tel_register_noti_event(sim_state_info.handle[i],
589 TAPI_PROP_NETWORK_SPN_NAME, on_plmn_spn_changed, (void*)i);
590 if (ret != TAPI_API_SUCCESS)
592 ERR("Failed to register 'on_plmn_spn_changed' callback to handle[%d][%d]", i, ret);
595 ret = tel_register_noti_event(sim_state_info.handle[i],
596 TAPI_PROP_NETWORK_NETWORK_NAME, on_plmn_spn_changed, (void*)i);
597 if (ret != TAPI_API_SUCCESS)
599 ERR("Failed to register 'on_plmn_spn_changed' callback to handle: %i", i);
602 ret = tel_register_noti_event(sim_state_info.handle[i], TAPI_PROP_NETWORK_SERVICE_TYPE, on_plmn_spn_changed, (void*) i);
603 if (ret != TAPI_API_SUCCESS)
605 ERR("Failed to register network service type[%d][%d]", ret, i);
608 ret = tel_register_noti_event(sim_state_info.handle[i], TAPI_PROP_NETWORK_NAME_OPTION, on_plmn_spn_changed, (void*) i);
609 if (ret != TAPI_API_SUCCESS)
611 ERR("Failed to register network name option[%d][%d]", ret, i);
616 ERR("No handle [%d]", i);
621 static void unregister_sim_callbacks()
625 for(i = 0; i < TAPI_HANDLE_MAX; ++i)
627 if(sim_state_info.handle[i])
629 ret = tel_deregister_noti_event(sim_state_info.handle[i], TAPI_NOTI_SIM_STATUS);
630 if (ret != TAPI_API_SUCCESS)
632 ERR("Failed to dereregister TAPI_NOTI_SIM_STATUS callback from handle: %i", i);
636 DBG("SIM status changed event deregistered");
639 ret = tel_deregister_noti_event(sim_state_info.handle[i], TAPI_PROP_NETWORK_NETWORK_NAME);
640 if (ret != TAPI_API_SUCCESS)
642 ERR("Failed to dereregister TAPI_PROP_NETWORK_PLMN callback from handle: %i", i);
645 ret = tel_deregister_noti_event(sim_state_info.handle[i], TAPI_PROP_NETWORK_SPN_NAME);
646 if (ret != TAPI_API_SUCCESS)
648 ERR("Failed to dereregister TAPI_PROP_NETWORK_SPN_NAME callback from handle: %i", i);
651 ret = tel_deregister_noti_event(sim_state_info.handle[i], TAPI_PROP_NETWORK_SERVICE_TYPE);
652 if (ret != TAPI_API_SUCCESS)
654 ERR("Failed to deregister network service type[%d][%d]", ret, i);
657 ret = tel_deregister_noti_event(sim_state_info.handle[i], TAPI_PROP_NETWORK_NAME_OPTION);
658 if (ret != TAPI_API_SUCCESS)
660 ERR("Failed to deregister network name option[%d][%d]", ret, i);
665 ret = tel_deregister_noti_event(sim_state_info.handle[i], TAPI_NOTI_CALL_PREFERRED_VOICE_SUBSCRIPTION);
666 if (ret != TAPI_API_SUCCESS)
668 ERR("Failed to dereregister callback to handle: %d", i);
676 void sim_controller_init(Evas_Object *master_layout)
678 int state = EINA_FALSE;
680 /* Check if Telephony state - READY */
681 ret = vconf_get_bool(VCONFKEY_TELEPHONY_READY, &state);
683 DBG("VCONFKEY_TELEPHONY_READY == %d", state);
685 if (ret != -1 && state == TRUE)
686 { /* Telephony State - READY */
687 /* Initialize TAPI handles */
690 register_sim_callbacks();
696 { /* Telephony State – NOT READY, register for change in state */
697 DBG("Telephony state: [NOT Ready]");
700 /* Register for Telephony state change */
701 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY, tel_ready_cb, master_layout);
703 ERR("Failed to register VCONFKEY_TELEPHONY_READY key changed callback");
705 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE, tel_flight_mode_cb, master_layout);
707 ERR("Failed to register VCONFKEY_TELEPHONY_FLIGHT_MODE key changed callback");
709 ret = vconf_notify_key_changed(VCONFKEY_CALL_STATE, __sim_controller_call_state_changed_cb, NULL);
712 ERR("Failed to notify call state[%d]", ret);
716 void sim_controller_resume()
721 TelSimCardStatus_t sim_status;
724 ret = vconf_get_bool(VCONFKEY_TELEPHONY_READY, &state);
725 if (ret != 0 || state == FALSE)
727 ERR("Failed to get telephony state[%d][%d]", state, ret);
731 for (i = 0; i < TAPI_HANDLE_MAX; ++i)
733 if (sim_state_info.handle[i])
735 ret = tel_get_sim_init_info(sim_state_info.handle[i], &sim_status, &card_changed);
736 DBG("SIM[%d] info[%d][%d][%d]", i, ret, sim_status, card_changed);
737 if (sim_status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED ||
738 sim_status == TAPI_SIM_STATUS_SIM_PIN_REQUIRED)
740 if (sim_state_info.sim_card_ready[i] != EINA_TRUE)
742 ERR("SIM[%d] is init completed but local value is not ture", i);
748 ERR("No handle[%d]", i);
753 void sim_controller_on_language_change()
755 on_plmn_spn_changed(sim_state_info.handle[0], "SELF", NULL, (void*) 0);
756 on_plmn_spn_changed(sim_state_info.handle[1], "SELF", NULL, (void*) 1);
758 if (sim_state_info.handle[0] == NULL && sim_state_info.handle[1] == NULL)
760 int flight_mode = EINA_FALSE;
761 int ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode);
764 ERR("Failed to get flight mode[%d]", ret);
767 sim_handler_text_set(flight_mode);