svace issues fixed
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginDSHandler.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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 <errno.h>
18 #include "MsgDebug.h"
19 #include "MsgException.h"
20 #include "SmsPluginDSHandler.h"
21 #include "MsgGconfWrapper.h"
22
23 extern "C" {
24         #include <tapi_common.h>
25         #include <TelNetwork.h>
26         #include <ITapiNetwork.h>
27         #include <ITapiSim.h>
28         #include <telephony_common.h>
29         #include <telephony_sim.h>
30 }
31
32 /*==================================================================================================
33                                      IMPLEMENTATION OF SmsPluginCbMsgHandler - Member Functions
34 ==================================================================================================*/
35 SmsPluginDSHandler* SmsPluginDSHandler::pInstance = NULL;
36
37
38 SmsPluginDSHandler::SmsPluginDSHandler()
39 {
40         cp_list = NULL;
41         memset(&handle_list, 0x00, sizeof(handle_list));
42 }
43
44
45 SmsPluginDSHandler::~SmsPluginDSHandler()
46 {
47 }
48
49
50 SmsPluginDSHandler* SmsPluginDSHandler::instance()
51 {
52         if (!pInstance)
53                 pInstance = new SmsPluginDSHandler();
54
55         return pInstance;
56 }
57
58 int SmsPluginDSHandler::initTelHandle()
59 {
60         int cnt = 0;
61         cp_list = tel_get_cp_name_list();
62
63         if (!cp_list) {
64                 MSG_FATAL("tel_get_cp_name_list returns null");
65                 goto FINISH;
66         }
67
68         while (cp_list[cnt] && cnt < MAX_TELEPHONY_HANDLE_CNT) {
69                 MSG_SEC_INFO("cp_list[%d]:[%s]", cnt, cp_list[cnt]);
70                 handle_list.handle[cnt]= tel_init(cp_list[cnt]);
71                 cnt++;
72         }
73
74         g_strfreev(cp_list);
75
76 FINISH:
77         handle_list.count = cnt;
78         return cnt;
79 }
80
81 void SmsPluginDSHandler::deinitTelHandle()
82 {
83         int ret = 0;
84
85         for (int i = 0; i < handle_list.count; i++) {
86                 ret = tel_deinit(handle_list.handle[i]);
87                 MSG_DEBUG("tel_deinit ret=[%d]", ret);
88                 handle_list.handle[i] = NULL;
89         }
90
91         handle_list.count = 0;
92         cp_list = NULL;
93
94         return;
95 }
96
97 TapiHandle *SmsPluginDSHandler::getTelHandle(int sim_idx)
98 {
99         if (sim_idx > 0 && sim_idx < MAX_TELEPHONY_HANDLE_CNT) {
100                 return handle_list.handle[sim_idx-1];
101         } else {
102                 int SIM_Status = 0;
103                 if (MsgSettingGetInt(VCONFKEY_TELEPHONY_SIM_SLOT, &SIM_Status) != MSG_SUCCESS) {
104                         MSG_INFO("MsgSettingGetInt() is failed");
105                 }
106                 if (SIM_Status == 1) {
107                         return handle_list.handle[0];
108                 }
109
110                 if (MsgSettingGetInt(VCONFKEY_TELEPHONY_SIM_SLOT2, &SIM_Status) != MSG_SUCCESS) {
111                         MSG_INFO("MsgSettingGetInt() is failed");
112                 }
113                 if (SIM_Status == 1) {
114                         return handle_list.handle[1];
115                 }
116         }
117
118         return handle_list.handle[handle_list.count - 1];
119 }
120
121 int SmsPluginDSHandler::getSimIndex(TapiHandle *handle)
122 {
123         for (int index = 0; index < handle_list.count; ++index) {
124                 if (handle_list.handle[index] == handle)
125                         return index+1;
126         }
127         return 0;
128 }
129
130
131 void SmsPluginDSHandler::getDefaultNetworkSimId(int *simId)
132 {
133         TelNetworkDefaultDataSubs_t defaultSimId = TAPI_NETWORK_DEFAULT_DATA_SUBS_UNKNOWN;
134
135         int tapi_ret = TAPI_API_SUCCESS;
136
137         if (handle_list.count == 1) {
138                 *simId = 1;
139                 return;
140         }
141
142         tapi_ret = tel_get_network_default_data_subscription(handle_list.handle[0], &defaultSimId);
143
144         MSG_INFO("Default network subscription = [SIM %d]", (int)defaultSimId+1);
145
146         if (tapi_ret != TAPI_API_SUCCESS) {
147                 *simId = 0;
148                 THROW(MsgException::SMS_PLG_ERROR, "########  tel_get_network_default_data_subscription Fail !!! return : %d #######", tapi_ret);
149         } else {
150                 *simId = (int)defaultSimId + 1;
151         }
152 }
153
154
155 int SmsPluginDSHandler::getTelHandleCount()
156 {
157         return handle_list.count;
158 }
159
160 int SmsPluginDSHandler::getActiveSimCount()
161 {
162         int active_count = 0;
163         int sim_status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
164
165         if (MsgSettingGetInt(VCONFKEY_TELEPHONY_SIM_SLOT, &sim_status) != MSG_SUCCESS) {
166                 MSG_INFO("MsgSettingGetInt() is failed");
167         }
168         MSG_DEBUG("sim1 status : %d", sim_status);
169         if (sim_status == VCONFKEY_TELEPHONY_SIM_INSERTED)
170                 active_count++;
171
172         sim_status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
173         if (MsgSettingGetInt(VCONFKEY_TELEPHONY_SIM_SLOT2, &sim_status) != MSG_SUCCESS) {
174                 MSG_INFO("MsgSettingGetInt() is failed");
175         }
176         MSG_DEBUG("sim2 status : %d", sim_status);
177         if (sim_status == VCONFKEY_TELEPHONY_SIM_INSERTED)
178                 active_count++;
179
180         MSG_DEBUG("active sim count : %d", active_count);
181
182         return active_count;
183 }
184
185
186 int SmsPluginDSHandler::getSubscriberId(unsigned int simIndex, char **subscriber_id)
187 {
188         if (simIndex <= 0 || simIndex > (unsigned int)handle_list.count) {
189                 MSG_DEBUG("Invalid SIM index");
190                 return MSG_ERR_INVALID_PARAMETER;
191         }
192
193         int tel_ret = TELEPHONY_ERROR_NONE;
194         telephony_handle_list_s capi_tel_handle_list;
195
196         tel_ret = telephony_init(&capi_tel_handle_list);
197         if (tel_ret != TELEPHONY_ERROR_NONE) {
198                 MSG_DEBUG("Initialize failed!!!");
199                 return MSG_ERR_PLUGIN_TAPIINIT;
200         }
201
202         tel_ret = telephony_sim_get_subscriber_id(capi_tel_handle_list.handle[simIndex-1], subscriber_id);
203         if (tel_ret != TELEPHONY_ERROR_NONE) {
204                 MSG_DEBUG("telephony_sim_get_subscriber_id() failed!!! [%d]", tel_ret);
205         } else {
206                 MSG_DEBUG("Subscriber ID is [%s]", *subscriber_id);
207         }
208
209         tel_ret = telephony_deinit(&capi_tel_handle_list);
210         if (tel_ret != TELEPHONY_ERROR_NONE) {
211                 MSG_DEBUG("Deinitialize failed!!!");
212         }
213
214     return MSG_SUCCESS;
215 }