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.
17 #include <Elementary.h>
19 #include <tapi_common.h>
23 #include <TelNetwork.h>
26 #include "sim-state.h"
28 #include "control-panel.h"
29 #include "lockscreen-lite.h"
30 #include "util-time.h"
32 #if !DISABLE_TELEPHONY
33 #include <telephony_network.h>
36 #define TAPI_HANDLE_MAX 2
41 TapiHandle *handle[TAPI_HANDLE_MAX+1];
42 Eina_Bool sim_card_ready[2];
43 Eina_Bool sim_card_roam[2];
44 int is_flight_mode_on;
49 char *network_name; //this name represents viewed string in lockscreen
56 .sim_card_ready[0] = EINA_FALSE,
57 .sim_card_ready[1] = EINA_FALSE,
58 .sim_card_roam[0] = EINA_FALSE,
59 .sim_card_roam[1] = EINA_FALSE,
60 .is_flight_mode_on = false,
62 .network_name_1 = NULL,
63 .network_name_2 = NULL,
68 static bool _get_sim_status(void);
69 static bool _sim_status_network_names_init(void);
70 static char *_get_spn_or_name(TapiHandle *handle);
73 * Network name functions
75 static char *_sim_network_name(TapiHandle *handle);
76 static char *_sim_spn_name(TapiHandle *handle);
79 * Callback registering and unregistering
81 static void _sim_state_register();
82 static void _sim_state_unregister();
87 static void _sim_status_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data);
88 static void _sim_name_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data);
89 static void _sim_roam_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data);
91 static void _ready_cb(keynode_t *key, void *data);
92 static void _flight_mode_cb(keynode_t *key, void *data);
94 #if !DISABLE_TELEPHONY
95 static void _sim_status_listener_init(void)
97 LOCK_SCREEN_TRACE_DBG("");
100 char **cp_list = NULL;
102 if (s_sim_state.is_initialized == 1) {
103 LOCK_SCREEN_TRACE_WARN("already initialized");
107 cp_list = tel_get_cp_name_list();
110 LOCK_SCREEN_TRACE_ERR("Failed to get tel_get_cp_name_list");
115 * Initialize sim handlers
117 while(cp_list[modem_num])
119 s_sim_state.handle[modem_num] = tel_init(cp_list[modem_num]);
120 if(!s_sim_state.handle[modem_num])
122 LOCK_SCREEN_TRACE_ERR("Failed to initialize modem %d ", modem_num);
126 s_sim_state.handle[modem_num] = NULL;
129 while (cp_list[i] != NULL) {
134 _sim_state_register();
135 if(!_sim_status_network_names_init())
137 LOCK_SCREEN_TRACE_ERR("Failed to init network names getter");
140 if(!_get_sim_status())
142 /* roam state : dual clock set */
143 if(s_sim_state.sim_card_roam[0] || s_sim_state.sim_card_roam[1])
145 util_time_roaming_state_set(1);
149 util_time_roaming_state_set(0);
152 LOCK_SCREEN_TRACE_ERR("Failed to get sim status ");
155 s_sim_state.is_initialized = 1;
158 bool sim_status_listener_init(void)
163 vconf_ret = vconf_get_bool(VCONFKEY_TELEPHONY_READY, &tel_status);
164 if(vconf_ret != VCONF_OK)
166 LOCK_SCREEN_TRACE_ERR("Failed to get VCONFKEY_TELEPHONY_READY");
168 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_READY, _ready_cb, NULL) != 0)
170 LOCK_SCREEN_TRACE_ERR("Failed to listen VCONFKEY_TELEPHONY_READY");
172 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE, _flight_mode_cb, NULL) != 0)
174 LOCK_SCREEN_TRACE_ERR("Failed to listen VCONFKEY_TELEPHONY_FLIGHT_MODE");
178 _sim_status_listener_init();
179 control_panel_sim_state_changed();
183 LOCK_SCREEN_TRACE_ERR("TAPI isn't ready yet");
189 void sim_status_listener_deinit(void)
193 LOCK_SCREEN_TRACE_DBG("");
195 if (s_sim_state.is_initialized == 0) {
196 LOCK_SCREEN_TRACE_ERR("TAPI handler is already initialized");
199 _sim_state_unregister();
201 while(s_sim_state.handle[it])
203 ret = tel_deregister_noti_event(s_sim_state.handle[it], TAPI_NOTI_SIM_STATUS);
204 if(ret != TAPI_API_SUCCESS)
206 LOCK_SCREEN_TRACE_ERR("Failed to deinitialize callback");
212 while(s_sim_state.handle[it])
214 tel_deinit(s_sim_state.handle[it]);
218 s_sim_state.is_initialized = 0;
221 char *sim_status_network_name_get(void)
225 if(s_sim_state.is_flight_mode_on == EINA_TRUE)
227 LOCK_SCREEN_TRACE_DBG("Fligh mode is enabled");
228 return strdup(_("IDS_COM_BODY_NO_SERVICE"));
230 else if(!s_sim_state.sim_card_ready[0] && !s_sim_state.sim_card_ready[1])
232 LOCK_SCREEN_TRACE_DBG("Sim card slots are empty");
233 return strdup(_("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
237 if(s_sim_state.network_name_1 && (!s_sim_state.network_name_2 || !s_sim_state.sim_card_ready[1]))
239 return strdup(s_sim_state.network_name_1);
241 else if((!s_sim_state.network_name_1 || !s_sim_state.sim_card_ready[0]) && s_sim_state.network_name_2)
243 return strdup(s_sim_state.network_name_2);
245 else if(s_sim_state.network_name_1 && s_sim_state.network_name_2)
248 snprintf(buf, sizeof(buf), "%s / %s", s_sim_state.network_name_1, s_sim_state.network_name_2);
256 int sim_status_roaming_status_is_on_check(void)
260 int status_sim1 = 0, status_sim2 = 0;
263 ret = tel_get_property_int(s_sim_state.handle[0], TAPI_PROP_NETWORK_ROAMING_STATUS, &status_sim1);
264 LOCK_SCREEN_TRACE_DBG("Roaming status for SIM1 is : %d ", status_sim1);
265 if(ret != TAPI_API_SUCCESS)
267 LOCK_SCREEN_TRACE_ERR("Failed to get roaming status ");
271 ret = tel_get_property_int(s_sim_state.handle[1], TAPI_PROP_NETWORK_ROAMING_STATUS, &status_sim2);
272 LOCK_SCREEN_TRACE_DBG("Roaming status for SIM2 is : %d ", status_sim2);
273 if(ret != TAPI_API_SUCCESS)
275 LOCK_SCREEN_TRACE_ERR("Failed to get roaming status ");
279 return status_sim1 | status_sim2;
283 * @brief Set network name
284 * @return TRUE if no error
286 static bool _sim_status_network_names_init(void)
288 free(s_sim_state.network_name_1);
289 s_sim_state.network_name_1 = NULL;
291 free(s_sim_state.network_name_2);
292 s_sim_state.network_name_2 = NULL;
294 s_sim_state.network_name_1 = _get_spn_or_name(s_sim_state.handle[0]);
295 if(!s_sim_state.network_name_1)
297 LOCK_SCREEN_TRACE_ERR("Failed to get sim 1 name");
299 s_sim_state.network_name_2 = _get_spn_or_name(s_sim_state.handle[1]);
300 if(!s_sim_state.network_name_2)
302 LOCK_SCREEN_TRACE_ERR("Failed to get sim 2 name");
305 LOCK_SCREEN_TRACE_WARN("sim1:%s", s_sim_state.network_name_1);
306 LOCK_SCREEN_TRACE_WARN("sim2:%s", s_sim_state.network_name_2);
312 * @brief Function initialize status table of sim cards. If status of sim card is correct it set proper table element as EINA_TRUE
314 static bool _get_sim_status(void)
322 TelSimCardStatus_t sim_status;
324 while(s_sim_state.handle[i])
326 ret = tel_get_sim_init_info(s_sim_state.handle[i], &sim_status, &card_changed);
327 if(ret != TAPI_API_SUCCESS)
329 LOCK_SCREEN_TRACE_ERR("Failed to get sim %d status ", i);
333 LOCK_SCREEN_TRACE_DBG("Sims status is %d ", sim_status);
335 if(sim_status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED ||
336 sim_status == TAPI_SIM_STATUS_SIM_PIN_REQUIRED)
338 LOCK_SCREEN_TRACE_DBG("Sim %d ready", i);
339 s_sim_state.sim_card_ready[i] = EINA_TRUE;
344 ret = tel_get_property_int (s_sim_state.handle[i], TAPI_PROP_NETWORK_ROAMING_STATUS, &roam);
347 LOCK_SCREEN_TRACE_DBG("Sim %d roaming is enabled", i);
348 s_sim_state.sim_card_roam[i] = EINA_TRUE;
352 LOCK_SCREEN_TRACE_DBG("Sim %d roaming is disabled", i);
353 s_sim_state.sim_card_roam[i] = EINA_FALSE;
362 static int _get_china_spn_or_name(char *mcc_mnc, int service_type, char *buf)
364 retv_if(mcc_mnc == NULL, 0);
365 retv_if(buf == NULL, 0);
367 const char *rat = NULL;
368 if (service_type == TAPI_NETWORK_SERVICE_TYPE_3G) {
372 LOCK_SCREEN_TRACE_DBG("PLMN:%s", mcc_mnc);
374 if (strcmp(mcc_mnc, "46001") == 0) {
376 strncpy(buf,_("IDS_SIM_BODY_CHINA_UNICOM_M_CHINA_OPERATOR_CHN"), BUF_SIZE - 1);
378 snprintf(buf, BUF_SIZE - 1,"%s %s",_("IDS_SIM_BODY_CHINA_UNICOM_M_CHINA_OPERATOR_CHN"), rat);
381 else if (strcmp(mcc_mnc, "46000") == 0 ||
382 strcmp(mcc_mnc, "46002") == 0 ||
383 strcmp(mcc_mnc, "46007") == 0 ||
384 strcmp(mcc_mnc, "46008") == 0) {
386 strncpy(buf,_("IDS_IDLE_BODY_CMCC_CHN"), BUF_SIZE - 1);
388 snprintf(buf, BUF_SIZE - 1, "%s %s", _("IDS_IDLE_BODY_CMCC_CHN"), rat);
391 else if (strcmp(mcc_mnc, "46692") == 0) {
392 strncpy(buf, _("IDS_SIM_BODY_CHUNGHWA_M_CHINA_OPERATOR_CHN"), BUF_SIZE - 1);//chunghwa_operator_name
394 else if (strcmp(mcc_mnc, "46601") == 0) {
395 strncpy(buf, _("IDS_IDLE_BODY_FAREASTONE_CHN"), BUF_SIZE - 1);//fareastone_operator_name
397 else if (strcmp(mcc_mnc, "46688") == 0) {
398 strncpy(buf, _("IDS_SIM_BODY_KGT_M_CHINA_OPERATOR_CHN"), BUF_SIZE - 1);//kgt_operator_name
400 else if (strcmp(mcc_mnc, "46689") == 0) {
401 strncpy(buf, _("IDS_SIM_BODY_T_STAR_M_CHINA_OPERATOR_CHN"), BUF_SIZE - 1);//kgt_operator_name
403 else if(strcmp(mcc_mnc, "46697") == 0 ||
404 strcmp(mcc_mnc, "46693") == 0 ||
405 strcmp(mcc_mnc, "46699") == 0) {
406 strncpy(buf, _("IDS_IDLE_BODY_TW_MOBILE_CHN"), BUF_SIZE - 1);//kgt_operator_name
409 if (strlen(buf) > 0) {
417 * @brief Function get spn or network name for sim card
418 * @details Function get network name property or it is not correct try to get SPN name for sim card.
419 * @param pointer to proper Sim card handle
420 * @return returns char pointer if no error, or empty string "" if network name or spin is NULL.
422 static char *_get_spn_or_name(TapiHandle *handle)
426 int ret = TAPI_API_SUCCESS;
427 int service_type = TAPI_NETWORK_SERVICE_TYPE_UNKNOWN;
428 int name_option = TAPI_NETWORK_DISP_INVALID;
433 memset(buf, 0, BUF_SIZE);
437 LOCK_SCREEN_TRACE_ERR("Invalid parameter");
441 ret = tel_get_property_int(handle, TAPI_PROP_NETWORK_SERVICE_TYPE, &service_type);
442 if(ret != TAPI_API_SUCCESS)
444 LOCK_SCREEN_TRACE_ERR("Failed to get service type");
448 if(service_type >= TAPI_NETWORK_SERVICE_TYPE_2G)
450 ret = tel_get_property_int(handle, TAPI_PROP_NETWORK_NAME_OPTION, &name_option);
451 if(ret != TAPI_API_SUCCESS)
453 LOCK_SCREEN_TRACE_ERR("Failed to get name_option");
456 char *mcc_mnc = NULL;
457 ret = tel_get_property_string(handle, TAPI_PROP_NETWORK_PLMN, &mcc_mnc);
458 if(ret != TAPI_API_SUCCESS)
460 LOCK_SCREEN_TRACE_ERR("Failed to get PLMN");
464 if (_get_china_spn_or_name(mcc_mnc, service_type, buf) == 0) {
467 case TAPI_NETWORK_DISP_SPN:
468 spn = _sim_spn_name(handle);
469 if (spn != NULL && spn[0] != 0)
471 snprintf(buf, sizeof(buf), "%s", spn);
474 case TAPI_NETWORK_DISP_PLMN:
475 plmn = _sim_network_name(handle);
476 if(plmn != NULL && plmn[0] != 0)
478 snprintf(buf, sizeof(buf), "%s", plmn);
481 case TAPI_NETWORK_DISP_SPN_PLMN:
482 spn = _sim_spn_name(handle);
483 plmn = _sim_network_name(handle);
484 if (spn != NULL && spn[0] != 0 && plmn != NULL && plmn[0] != 0)
486 snprintf(buf, sizeof(buf), "%s - %s", plmn, spn);
488 else if (spn != NULL && spn[0] != 0)
490 snprintf(buf, sizeof(buf), "%s", spn);
492 else if (plmn != NULL && plmn[0] != 0)
494 snprintf(buf, sizeof(buf), "%s", plmn);
498 plmn = _sim_network_name(handle);
499 if (plmn != NULL && plmn[0] != 0)
501 snprintf(buf, sizeof(buf), "%s", plmn);
507 LOCK_SCREEN_TRACE_WARN("name option:%d", name_option);
508 LOCK_SCREEN_TRACE_WARN("spn/plmn:%s", buf);
512 switch (service_type)
514 case TAPI_NETWORK_SERVICE_TYPE_NO_SERVICE:
515 LOCK_SCREEN_TRACE_DBG("No service");
516 snprintf(buf, sizeof(buf), "%s", _("IDS_COM_BODY_NO_SERVICE"));
518 case TAPI_NETWORK_SERVICE_TYPE_EMERGENCY:
519 LOCK_SCREEN_TRACE_DBG("Emergency");
520 snprintf(buf, sizeof(buf), "%s", _("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
522 case TAPI_NETWORK_SERVICE_TYPE_SEARCH:
523 LOCK_SCREEN_TRACE_DBG("Searching");
524 snprintf(buf, sizeof(buf), "%s", _("IDS_COM_BODY_SEARCHING"));
527 plmn = _sim_network_name(handle);
528 if (plmn != NULL && plmn[0] != 0)
530 snprintf(buf, sizeof(buf), "%s", plmn);
534 LOCK_SCREEN_TRACE_WARN("service_type:%d", service_type);
535 LOCK_SCREEN_TRACE_WARN("spn/plmn:%s", buf);
543 control_panel_sim_state_changed();
554 * @brief Register callbacks
555 * @details Register callback function for sim status, sim name, sim spn , sim plm
556 * @return FALSE if something went wrong.
558 static void _sim_state_register()
565 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &s_sim_state.is_flight_mode_on);
568 LOCK_SCREEN_TRACE_ERR("Could not get 'VCONFKEY_TELEPHONY_FLIGHT_MODE' value");
571 for(i = 0; i < TAPI_HANDLE_MAX; ++i)
573 if(s_sim_state.handle[i])
575 ret = tel_register_noti_event(s_sim_state.handle[i], TAPI_NOTI_SIM_STATUS, _sim_status_changed_cb, (void*)i);
576 if(ret != TAPI_API_SUCCESS)
578 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_status_changed_cb");
581 ret = tel_register_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_PLMN, _sim_name_changed_cb, (void*)i);
582 if(ret != TAPI_API_SUCCESS)
584 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_name_changed_cb");
587 ret = tel_register_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_SPN_NAME, _sim_name_changed_cb, (void*)i);
588 if(ret != TAPI_API_SUCCESS)
590 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_name_changed_cb");
593 ret = tel_register_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_NETWORK_NAME, _sim_name_changed_cb, (void*)i);
594 if(ret != TAPI_API_SUCCESS)
596 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_name_changed_cb");
599 ret = tel_register_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_SERVICE_TYPE, _sim_name_changed_cb, (void*)i);
600 if(ret != TAPI_API_SUCCESS)
602 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_name_changed_cb");
606 ret = tel_register_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_ROAMING_STATUS, _sim_roam_changed_cb, (void*)i);
607 if(ret != TAPI_API_SUCCESS)
609 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_roam_changed_cb for : %d" , i);
616 * Unregister sim callbacks
618 static void _sim_state_unregister()
625 for(i = 0; i < TAPI_HANDLE_MAX; ++i)
627 if(s_sim_state.handle[i])
629 ret = tel_deregister_noti_event(s_sim_state.handle[i], TAPI_NOTI_SIM_STATUS);
630 if(ret != TAPI_API_SUCCESS)
632 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_status_changed_cb");
635 ret = tel_deregister_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_PLMN);
636 if(ret != TAPI_API_SUCCESS)
638 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_plm_changed_cb");
641 ret = tel_deregister_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_SPN_NAME);
642 if(ret != TAPI_API_SUCCESS)
644 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_spn_changed_cb");
647 ret = tel_deregister_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_NETWORK_NAME);
648 if(ret != TAPI_API_SUCCESS)
650 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_spn_changed_cb");
653 ret = tel_deregister_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_SERVICE_TYPE);
654 if(ret != TAPI_API_SUCCESS)
656 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_spn_changed_cb");
659 ret = tel_deregister_noti_event(s_sim_state.handle[i], TAPI_PROP_NETWORK_ROAMING_STATUS);
660 if(ret != TAPI_API_SUCCESS)
662 LOCK_SCREEN_TRACE_ERR("Failed to register _sim_name_changed_cb");
671 static void _ready_cb(keynode_t *key, void *data)
673 Eina_Bool status = EINA_FALSE;
674 status = vconf_keynode_get_bool(key);
676 LOCK_SCREEN_TRACE_DBG("TAPI ready:%d", status);
680 #if !DISABLE_TELEPHONY
681 _sim_status_listener_init();
683 control_panel_sim_state_changed();
687 sim_status_listener_deinit();
692 * @brief Vconf callback function for flight mode key.
694 static void _flight_mode_cb(keynode_t *key, void *data)
696 s_sim_state.is_flight_mode_on = vconf_keynode_get_bool(key);
698 if(!_sim_status_network_names_init())
700 LOCK_SCREEN_TRACE_ERR("Failed to reinitialize network names");
703 control_panel_sim_state_changed();
707 * @brief Tapi callback for handle change of sim cards states.
709 static void _sim_status_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
713 int i = (int) user_data;
714 int *sim_status = data;
716 if(*sim_status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED || *sim_status == TAPI_SIM_STATUS_SIM_PIN_REQUIRED)
718 s_sim_state.sim_card_ready[i] = EINA_TRUE;
722 s_sim_state.sim_card_ready[i] = EINA_FALSE;
725 control_panel_sim_state_changed();
729 * @brief Tapi callback function for handle network name changes.
731 static void _sim_name_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
735 if(!_sim_status_network_names_init())
737 LOCK_SCREEN_TRACE_ERR("Failed to reinitialize network names");
741 control_panel_sim_state_changed();
744 static void _sim_roam_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
748 guchar *roaming_status = data;
749 int i_roaming_status = *roaming_status;
753 s_sim_state.sim_card_roam[(int)user_data] = EINA_TRUE;
757 s_sim_state.sim_card_roam[(int)user_data] = EINA_FALSE;
760 if(s_sim_state.sim_card_roam[0] || s_sim_state.sim_card_roam[1])
762 util_time_roaming_state_set(1);
766 util_time_roaming_state_set(0);
771 * @brief Gets network name from TAPI handle
772 * @param handle pointer
773 * @return name of network or NULL if error.
775 static char *_sim_network_name(TapiHandle *handle)
782 ret = tel_get_property_string(handle, TAPI_PROP_NETWORK_NETWORK_NAME, &name);
783 if(ret == TAPI_API_SUCCESS && name != NULL)
794 * @brief Gets spn name from TAPI handle
795 * @return spn network name or NULL if error.
797 static char *_sim_spn_name(TapiHandle *handle)
801 char *spn_name = NULL;
803 ret = tel_get_property_string(handle, TAPI_PROP_NETWORK_SPN_NAME, &spn_name);
804 if(ret == TAPI_API_SUCCESS && spn_name != NULL)
817 static Evas_Object *_create_view(Evas_Object *parent, LKD_Module_Data *md)
822 static int _destroy_view(LKD_Module_Data *md)
827 static void _resume(LKD_Module_Data *md)
831 static void _pause(LKD_Module_Data *md)
835 LKD_Module g_mod_sim_state = {
840 .create_view = _create_view,
841 .destroy_view = _destroy_view,