2 * Copyright (c) 2016 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.
21 #include <telephony/telephony.h>
24 static telephony_handle_list_s handle_list;
25 static char *sim_plmn[LOCKSCREEN_SIM_MAX];
26 static int init_count;
27 int LOCKSCREEN_EVENT_SIM_STATUS_CHANGED;
29 static const telephony_noti_e notis[] = {
30 TELEPHONY_NOTI_SIM_STATUS,
31 TELEPHONY_NOTI_NETWORK_NETWORK_NAME,
32 TELEPHONY_NOTI_NETWORK_SERVICE_STATE,
35 static char *_sim_plmn_get(telephony_h handle)
39 int ret = telephony_network_get_network_name(handle, &network_name);
40 if (ret != TELEPHONY_ERROR_NONE) {
41 ERR("telephony_network_get_network_name failed: %s", get_error_message(ret));
48 static char *_sim_spn_get(telephony_h handle)
52 int ret = telephony_sim_get_spn(handle, &spn_name);
53 if (ret != TELEPHONY_ERROR_NONE) {
54 ERR("telephony_sim_get_spn failed: %s", get_error_message(ret));
60 static char *_sim_state_text_for_sim_get(telephony_h handle)
63 telephony_network_service_state_e service_state;
64 telephony_network_name_option_e name_option;
68 char buf[1024] = { 0, };
70 /* get service state */
71 ret = telephony_network_get_service_state(handle, &service_state);
72 if (ret != TELEPHONY_ERROR_NONE) {
73 ERR("telephony_network_get_service_state failed: %s", get_error_message(ret));
77 switch (service_state) {
78 case TELEPHONY_NETWORK_SERVICE_STATE_IN_SERVICE:
79 /* get network name option */
80 ret = telephony_network_get_network_name_option(handle, &name_option);
81 if (ret != TELEPHONY_ERROR_NONE) {
82 ERR("telephony_network_get_network_name_option failed: %s", get_error_message(ret));
86 switch (name_option) {
87 case TELEPHONY_NETWORK_NAME_OPTION_SPN:
88 spn = _sim_spn_get(handle);
89 if (spn != NULL && spn[0] != 0) {
90 INF("PLMN/SPN - Sim %p using SPN: %s", handle, spn);
91 snprintf(buf, sizeof(buf), "%s", spn);
94 case TELEPHONY_NETWORK_NAME_OPTION_NETWORK:
95 plmn = _sim_plmn_get(handle);
96 if (plmn != NULL && plmn[0] != 0) {
97 INF("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
98 snprintf(buf, sizeof(buf), "%s", plmn);
101 case TELEPHONY_NETWORK_NAME_OPTION_ANY:
102 spn = _sim_spn_get(handle);
103 plmn = _sim_plmn_get(handle);
104 if (spn != NULL && spn[0] != 0 && plmn != NULL && plmn[0] != 0) {
105 INF("PLMN/SPN - Sim %p using SPN: %s, PLMN: %s", handle, spn, plmn);
106 snprintf(buf, sizeof(buf), "%s - %s", plmn, spn);
107 } else if (spn != NULL && spn[0] != 0) {
108 INF("PLMN/SPN - Sim %p using SPN: %s", handle, spn);
109 snprintf(buf, sizeof(buf), "%s", spn);
110 } else if (plmn != NULL && plmn[0] != 0) {
111 INF("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
112 snprintf(buf, sizeof(buf), "%s", plmn);
116 ERR("Invalid name option[%d]", name_option);
117 plmn = _sim_plmn_get(handle);
118 if (plmn != NULL && plmn[0] != 0) {
119 INF("PLMN/SPN - Sim %p using PLMN: %s", handle, plmn);
120 snprintf(buf, sizeof(buf), "%s", plmn);
125 case TELEPHONY_NETWORK_SERVICE_STATE_OUT_OF_SERVICE:
126 snprintf(buf, sizeof(buf), "%s", _("IDS_COM_BODY_NO_SERVICE"));
128 case TELEPHONY_NETWORK_SERVICE_STATE_EMERGENCY_ONLY:
129 snprintf(buf, sizeof(buf), "%s", _("IDS_IDLE_MBODY_EMERGENCY_CALLS_ONLY"));
132 snprintf(buf, sizeof(buf), "%s", _("IDS_COM_BODY_NO_SERVICE"));
139 static void _update_sim_info(bool emit)
142 telephony_sim_state_e state;
144 for (i = 0; (i < handle_list.count) && (i < LOCKSCREEN_SIM_MAX); i++)
148 int ret = telephony_sim_get_state(handle_list.handle[i], &state);
149 if (ret != TELEPHONY_ERROR_NONE)
152 if (state == TELEPHONY_SIM_STATE_UNAVAILABLE)
155 out = _sim_state_text_for_sim_get(handle_list.handle[i]);
162 if (emit) ecore_event_add(LOCKSCREEN_EVENT_SIM_STATUS_CHANGED, NULL, NULL, NULL);
165 static void _on_telephony_state_changed_cb(telephony_state_e state, void *user_data)
167 _update_sim_info(true);
170 static void _on_sim_info_changed_cb(telephony_h handle, telephony_noti_e noti_id, void *data, void *user_data)
172 _update_sim_info(true);
175 int lockscreen_sim_init(void)
179 LOCKSCREEN_EVENT_SIM_STATUS_CHANGED = ecore_event_type_new();
180 int ret = telephony_init(&handle_list);
181 if (ret != TELEPHONY_ERROR_NONE)
183 ERR("telephony_init failed: %s", get_error_message(ret));
187 ret = telephony_set_state_changed_cb(_on_telephony_state_changed_cb, NULL);
188 if (ret != TELEPHONY_ERROR_NONE) {
189 telephony_deinit(&handle_list);
190 ERR("telephony_set_state_changed_cb failed: %s", get_error_message(ret));
194 for (i = 0; (i < handle_list.count) && (i < LOCKSCREEN_SIM_MAX); i++)
197 for (j = 0; j < SIZE(notis); j++)
199 ret = telephony_set_noti_cb(handle_list.handle[i], notis[j], _on_sim_info_changed_cb, NULL);
200 if (ret != TELEPHONY_ERROR_NONE) {
201 ERR("telephony_set_noti_cb failed: %s", get_error_message(ret));
206 _update_sim_info(false);
212 void lockscreen_sim_shutdown(void)
218 for (i = 0; i < LOCKSCREEN_SIM_MAX; i++) {
222 telephony_deinit(&handle_list);
223 telephony_unset_state_changed_cb(_on_telephony_state_changed_cb);
228 const char *lockscreen_sim_get_plmn(lockscreen_sim_num_e num)
230 return sim_plmn[num];