tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Systeminfo / SystemInfoCellularNetwork.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <device.h>
19 #include <vconf.h>
20 #include <stdlib.h>
21 #include <ITapiModem.h>
22 #include <net_connection.h>
23 #include <ITapiNetwork.h>
24 #include "SystemInfoCellularNetwork.h"
25 #include "SystemInfoUtil.h"
26
27 #include <PlatformException.h>
28 #include <Logger.h>
29
30 namespace DeviceAPI {
31 namespace SystemInfo {
32
33 namespace {
34 const char* CONNECTION_OFF = "OFF";
35 const char* CONNECTION_ON = "ON";
36 }
37
38 SystemInfoCellularNetwork::SystemInfoCellularNetwork() :
39     m_mcc(0),
40     m_mnc(0),
41     m_cell_id(0),
42     m_lac(0),
43     m_is_roaming(false),
44     m_is_flight_mode(false)
45 {
46     LOGD("Entered");
47
48     fetchVconfSettings();
49     fetchConnection();
50 }
51
52 SystemInfoCellularNetwork::~SystemInfoCellularNetwork()
53 {
54     LOGD("Entered");
55 }
56
57 std::string SystemInfoCellularNetwork::getStatus() const
58 {
59     return m_status;
60 }
61
62 std::string SystemInfoCellularNetwork::getApn() const
63 {
64     return m_apn;
65 }
66
67 std::string SystemInfoCellularNetwork::getIpAddress() const
68 {
69     return m_ip_address;
70 }
71
72 std::string SystemInfoCellularNetwork::getIpv6Address() const
73 {
74     return m_ipv6_address;
75 }
76
77 unsigned short SystemInfoCellularNetwork::getMcc() const
78 {
79     return m_mcc;
80 }
81
82 unsigned short SystemInfoCellularNetwork::getMnc() const
83 {
84     return m_mnc;
85 }
86
87 unsigned short SystemInfoCellularNetwork::getCellId() const
88 {
89     return m_cell_id;
90 }
91
92 unsigned short SystemInfoCellularNetwork::getLac() const
93 {
94     return m_lac;
95 }
96
97 bool SystemInfoCellularNetwork::isRoaming() const
98 {
99     return m_is_roaming;
100 }
101
102 bool SystemInfoCellularNetwork::isFlightMode() const
103 {
104     return m_is_flight_mode;
105 }
106
107 std::string SystemInfoCellularNetwork::getImei() const
108 {
109     return m_imei;
110 }
111
112 void SystemInfoCellularNetwork::fetchVconfSettings()
113 {
114     int result;
115     int ret = 0;
116
117     ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &result);
118     if (ret != VCONF_OK) {
119         LOGE("Failed to get is_flight_mode value");
120     }
121     m_is_flight_mode = (0 != result) ? true : false;
122 }
123
124 void SystemInfoCellularNetwork::fetchConnection()
125 {
126     connection_type_e connection_type;
127     connection_profile_h profile_handle = NULL;
128     char* apn = NULL;
129     char* ip_addr = NULL;
130     char* imei = NULL;
131     connection_h connection_handle;
132     TelNetworkDefaultDataSubs_t default_subs = TAPI_NETWORK_DEFAULT_DATA_SUBS_SIM1;
133     TapiHandle *handle;
134     TelSimImsiInfo_t imsi;
135     char* plmn;
136     int plmnValue;
137     int roam;
138     int cellid;
139     int lac;
140     unsigned short mcc_devider = 1000;
141     char **cp_list = NULL;
142     int ret = 0;
143
144     //capi network doesn't support using connection handle created in
145     //other thread, so we need to create it here
146     ret = connection_create(&connection_handle);
147     if (ret != CONNECTION_ERROR_NONE) {
148         LOGE("Failed to create connection: %d, %s", ret,
149                 SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
150     }
151
152     try {
153         ret = connection_get_type(connection_handle, &connection_type);
154         if (ret != CONNECTION_ERROR_NONE) {
155             LOGE("Failed to get connection type: %d, %s", ret,
156                     SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
157         }
158
159         if (CONNECTION_TYPE_CELLULAR == connection_type) {
160             m_status = CONNECTION_ON;
161
162             ret = connection_get_current_profile(connection_handle, &profile_handle);
163             if (ret != CONNECTION_ERROR_NONE) {
164                 LOGE("Failed to get profile: %d, %s", ret,
165                         SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
166             }
167
168             ret = connection_profile_get_cellular_apn(profile_handle, &apn);
169             if (ret != CONNECTION_ERROR_NONE) {
170                 LOGE("Failed to get apn name: %d, %s", ret,
171                         SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
172             }
173             m_apn = apn;
174             free(apn);
175
176             ret = connection_profile_get_ip_address(profile_handle, CONNECTION_ADDRESS_FAMILY_IPV4,
177                     &ip_addr);
178             if (ret == CONNECTION_ERROR_NONE) {
179                 LOGD("Success to get ipv4");
180                 m_ip_address = ip_addr;
181                 free(ip_addr);
182             } else {
183                 LOGE("Failed to get ip address: %d, %s", ret,
184                         SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
185             }
186
187             ip_addr = NULL;
188             ret = connection_profile_get_ip_address(profile_handle, CONNECTION_ADDRESS_FAMILY_IPV6,
189                     &ip_addr);
190             if (ret == CONNECTION_ERROR_NONE) {
191                 LOGD("Success to get ipv6");
192                 m_ipv6_address = ip_addr;
193                 free(ip_addr);
194             } else if (CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED != ret) {
195                 LOGE("Failed to get ipv6 address: %d, %s", ret,
196                         SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
197             }
198         } else {
199             m_status = CONNECTION_OFF;
200
201             ret = connection_get_default_cellular_service_profile(connection_handle,
202                     CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET, &profile_handle);
203             if (ret == CONNECTION_ERROR_NONE) {
204                 ret = connection_profile_get_cellular_apn(profile_handle, &apn);
205                 if (ret == CONNECTION_ERROR_NONE) {
206                     LOGD("Success to get apn");
207                     m_apn = apn;
208                     free(apn);
209                 } else {
210                     LOGE("Failed to get default apn name: %d, %s", ret,
211                             SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
212                 }
213             } else {
214                 LOGE("Failed to get default profile: %d, %s", ret,
215                         SystemInfoUtil::getConnectionErrorMessage(ret).c_str());
216             }
217         }
218
219         handle = tel_init(NULL);
220         ret = tel_get_network_default_data_subscription(handle, &default_subs);
221         if (ret != TAPI_API_SUCCESS) {
222             LOGE("Failed to get default subscriber: %d, %s", ret,
223                     SystemInfoUtil::getTapiErrorMessage(ret).c_str());
224         } else {
225             LOGD("Success to get default subscription");
226             tel_deinit(handle);
227             handle = NULL;
228
229             cp_list = tel_get_cp_name_list();
230             handle = tel_init(cp_list[default_subs]);
231         }
232
233         if (handle == NULL) {
234             LOGE("It's failed to get handle");
235         }
236
237         imei = tel_get_misc_me_imei_sync(handle);
238         if (NULL != imei) {
239             LOGD("Success to get imei");
240             m_imei = imei;
241             free(imei);
242         } else {
243             LOGE("Failed to get imei, NULL pointer. Setting empty value.");
244             m_imei = "";
245         }
246
247         ret = tel_get_property_string(handle, TAPI_PROP_NETWORK_PLMN, &plmn);
248         if (ret != TAPI_API_SUCCESS) {
249             LOGE("Failed to get plmn value, %d, %s", ret,
250                     SystemInfoUtil::getTapiErrorMessage(ret).c_str());
251         } else {
252             LOGD("plmn : %s", plmn);
253             plmnValue = atoi(plmn);
254             if (plmnValue < 100000) {
255                 mcc_devider = 100;
256             }
257             m_mcc = plmnValue / mcc_devider;
258             m_mnc = plmnValue % mcc_devider;
259             LOGD("mcc : %d, mnc : %d", m_mcc, m_mnc);
260         }
261
262         ret = tel_get_property_int(handle, TAPI_PROP_NETWORK_ROAMING_STATUS, &roam);
263         if (ret != TAPI_API_SUCCESS) {
264             LOGE("Failed to get roaming status, %d, %s", ret,
265                     SystemInfoUtil::getTapiErrorMessage(ret).c_str());
266         } else {
267             LOGD("roaming : %d", roam);
268             m_is_roaming = (0 != roam) ? true : false;
269         }
270
271         ret = tel_get_property_int(handle, TAPI_PROP_NETWORK_CELLID, &cellid);
272         if (ret != TAPI_API_SUCCESS) {
273             LOGE("Failed to get cell id, %d, %s", ret,
274                     SystemInfoUtil::getTapiErrorMessage(ret).c_str());
275         } else {
276             LOGD("cellid : %d", cellid);
277             m_cell_id = cellid;
278         }
279
280         ret = tel_get_property_int(handle, TAPI_PROP_NETWORK_LAC, &lac);
281         if (ret != TAPI_API_SUCCESS) {
282             LOGE("Failed to get lac, %d, %s", ret,
283                     SystemInfoUtil::getTapiErrorMessage(ret).c_str());
284         } else {
285             LOGD("lac : %d", lac);
286             m_lac = lac;
287         }
288
289     } catch (...) {
290         if (NULL != profile_handle) {
291             connection_profile_destroy(profile_handle);
292         }
293         connection_destroy(connection_handle);
294         tel_deinit(handle);
295         throw;
296     }
297     if (NULL != profile_handle) {
298         connection_profile_destroy(profile_handle);
299     }
300     connection_destroy(connection_handle);
301     tel_deinit(handle);
302 }
303
304 } // SystemInfo
305 } // DeviceAPI