2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 <vconf/vconf.h>
18 #include <openssl/sha.h>
20 #include "stc-monitor.h"
21 #include "stc-firewall.h"
22 #include "stc-manager-gdbus.h"
23 #include "stc-default-connection.h"
25 /* connman service dbus details */
26 #define CONNMAN_SERVICE "net.connman"
27 #define CONNMAN_PATH "/net/connman"
29 #define CONNMAN_MANAGER_PATH "/"
30 #define CONNMAN_MANAGER_INTERFACE CONNMAN_SERVICE ".Manager"
31 #define CONNMAN_SERVICE_INTERFACE CONNMAN_SERVICE ".Service"
33 #define CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX CONNMAN_PATH "/service/cellular_"
34 #define CONNMAN_WIFI_SERVICE_PROFILE_PREFIX CONNMAN_PATH "/service/wifi_"
35 #define CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX CONNMAN_PATH "/service/ethernet_"
36 #define CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX CONNMAN_PATH "/service/bluetooth_"
38 #define CONNMAN_SIGNAL_PROPERTY_CHANGED "PropertyChanged"
40 /* telephony service dbus details */
41 #define TELEPHONY_SERVICE "org.tizen.telephony"
42 #define TELEPHONY_DEFAULT_PATH "/org/tizen/telephony"
44 #define TELEPHONY_SERVICE_MANAGER TELEPHONY_SERVICE".Manager"
45 #define TELEPHONY_SIM_INTERFACE TELEPHONY_SERVICE".Sim"
47 #define TELEPHONY_GET_MODEMS "GetModems"
48 #define TELEPHONY_GET_IMSI "GetIMSI"
50 #define SIM_SLOT_SINGLE 1
52 #define VCONF_TELEPHONY_DEFAULT_DATA_SERVICE "db/telephony/dualsim/default_data_service"
54 default_connection_s g_default_connection;
55 guint g_default_connection_sub_id = 0;
57 static int __telephony_get_current_sim(void)
59 int sim_slot_count = 0;
62 if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT_COUNT, &sim_slot_count) != 0) {
63 STC_LOGD("failed to get sim slot count"); //LCOV_EXCL_LINE
64 return -1; //LCOV_EXCL_LINE
67 if (sim_slot_count == SIM_SLOT_SINGLE) {
68 STC_LOGD("It's single sim model"); //LCOV_EXCL_LINE
69 return current_sim; //LCOV_EXCL_LINE
72 if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE, ¤t_sim) != 0) {
73 STC_LOGD("failed to get default data service = %d\n", //LCOV_EXCL_LINE
75 return -1; //LCOV_EXCL_LINE
81 static void __make_imsi_to_subscriber_id(char *imsi)
85 unsigned char md[SHA256_DIGEST_LENGTH];
88 SHA256_Update(&ctx, imsi, strlen(imsi));
89 SHA256_Final(md, &ctx);
91 for (i = 0; i < SHA256_DIGEST_LENGTH; ++i)
92 snprintf(g_default_connection.subscriber_id + (i * 2), 3, "%02x", md[i]);
95 static void __telephony_get_modem_subscriber_id(GDBusConnection *connection,
96 const char *default_modem_name)
98 GVariant *message = NULL;
99 char tel_path[MAX_PATH_LENGTH];
100 char imsi[IMSI_LENGTH];
101 const char *plmn = NULL;
103 const char *msin = NULL;
106 snprintf(tel_path, sizeof(tel_path), "%s/%s", TELEPHONY_DEFAULT_PATH,
108 message = stc_manager_gdbus_call_sync(connection,
111 TELEPHONY_SIM_INTERFACE,
114 if (message == NULL) {
115 STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
116 goto done; //LCOV_EXCL_LINE
119 DEBUG_PARAMS(message);
120 DEBUG_PARAM_TYPE(message);
121 g_variant_get(message, "(&s&s)", &plmn, &msin);
123 plmn_len = strlen(plmn);
125 msin_len = strlen(msin);
127 if (msin_len + plmn_len >= IMSI_LENGTH) {
128 STC_LOGD("Incorrect length of mobile subscriber identifier + net id"); //LCOV_EXCL_LINE
129 goto done; //LCOV_EXCL_LINE
132 snprintf(imsi, IMSI_LENGTH, "%s%s", plmn, msin);
133 __make_imsi_to_subscriber_id(imsi);
136 g_variant_unref(message);
140 static void __telephony_update_default_modem_subscriber_id(GDBusConnection *connection)
142 GVariant *message = NULL;
143 GVariantIter *iter = NULL;
144 gchar *default_modem_name = NULL;
145 gchar *modem_name = NULL;
146 int current_sim = __telephony_get_current_sim();
148 if (current_sim < 0) {
149 STC_LOGI("Sim not found"); //LCOV_EXCL_LINE
150 return; //LCOV_EXCL_LINE
153 message = stc_manager_gdbus_call_sync(connection,
155 TELEPHONY_DEFAULT_PATH,
156 TELEPHONY_SERVICE_MANAGER,
157 TELEPHONY_GET_MODEMS,
159 if (message == NULL) {
160 STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
161 return; //LCOV_EXCL_LINE
164 g_variant_get(message, "(as)", &iter);
165 DEBUG_PARAMS(message);
166 DEBUG_PARAM_TYPE(message);
167 while (g_variant_iter_loop(iter, "s", &modem_name)) {
168 if (current_sim == 0) {
169 default_modem_name = g_strdup(modem_name);
173 current_sim--; //LCOV_EXCL_LINE
176 __telephony_get_modem_subscriber_id(connection, default_modem_name);
178 FREE(default_modem_name);
179 g_variant_iter_free(iter);
180 g_variant_unref(message);
184 static void __print_default_connection_info(void)
186 STC_LOGI("============= default connection info ============");
187 STC_LOGI("path [%s]", g_default_connection.path);
188 STC_LOGI("type [%d]", g_default_connection.type);
189 STC_LOGI("ifname [%s]", g_default_connection.ifname);
190 STC_LOGI("roaming [%u]", g_default_connection.roaming ? TRUE : FALSE);
191 if (g_default_connection.type == STC_IFACE_DATACALL)
192 STC_LOGI("sub_id [%s]", g_default_connection.subscriber_id);
193 STC_LOGI("==================================================");
196 static void __reset_default_connection_data(void)
198 FREE(g_default_connection.path);
199 FREE(g_default_connection.ifname);
200 g_default_connection.type = STC_IFACE_UNKNOWN;
201 g_default_connection.roaming = FALSE;
204 static gboolean __is_cellular_internet_profile(const char *profile)
206 const char internet_suffix[] = "_1";
211 if (g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX)
213 char *suffix = strrchr(profile, '_');
214 if (g_strcmp0(suffix, internet_suffix) == 0)
221 static gboolean __is_cellular_profile(const char *profile)
226 return g_str_has_prefix(profile, //LCOV_EXCL_LINE
227 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX);
230 static gboolean __is_wifi_profile(const char *profile)
235 return g_str_has_prefix(profile, //LCOV_EXCL_LINE
236 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
239 static gboolean __is_ethernet_profile(const char *profile)
244 return g_str_has_prefix(profile, //LCOV_EXCL_LINE
245 CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX);
248 static gboolean __is_bluetooth_profile(const char *profile)
253 return g_str_has_prefix(profile, //LCOV_EXCL_LINE
254 CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX);
257 static gboolean __is_connected(GVariantIter *array)
259 gboolean is_connected = FALSE;
260 GVariant *variant = NULL;
263 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
264 if (g_strcmp0(key, "State") != 0)
267 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
268 const gchar *state = NULL;
270 state = g_variant_get_string(variant, NULL);
271 if (g_strcmp0(state, "ready") == 0 ||
272 g_strcmp0(state, "online") == 0)
277 g_variant_unref(variant);
284 static void __get_default_connection_info(GDBusConnection *connection,
285 const char *object_path)
287 GVariant *message = NULL;
288 GVariantIter *iter = NULL;
289 GVariant *variant = NULL;
292 if (object_path == NULL) {
293 STC_LOGI("Object path is NULL, so information not available.");
297 message = stc_manager_gdbus_call_sync(connection,
300 CONNMAN_SERVICE_INTERFACE,
301 "GetProperties", NULL);
302 if (message == NULL) {
303 STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
304 goto done; //LCOV_EXCL_LINE
307 g_variant_get(message, "(a{sv})", &iter);
309 STC_LOGE("Profile %s doesn't exist", object_path); //LCOV_EXCL_LINE
310 goto done; //LCOV_EXCL_LINE
313 while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
314 if (g_strcmp0(key, "Ethernet") == 0) {
315 GVariantIter *iter1 = NULL;
316 GVariant *variant1 = NULL;
319 g_variant_get(variant, "a{sv}", &iter1);
321 continue; //LCOV_EXCL_LINE
323 while (g_variant_iter_loop(iter1, "{sv}", &key1,
325 if (g_strcmp0(key1, "Interface") == 0) {
327 g_variant_get_string(variant1,
329 g_default_connection.ifname =
334 g_variant_iter_free(iter1);
336 } else if (g_strcmp0(key, "Roaming") == 0) {
337 gboolean roaming = 0;
339 if (g_variant_is_of_type(variant,
340 G_VARIANT_TYPE_BOOLEAN)) {
341 roaming = g_variant_get_boolean(variant);
342 g_default_connection.roaming = roaming;
349 g_variant_iter_free(iter);
352 g_variant_unref(message);
357 static stc_error_e __get_default_profile(GDBusConnection *connection)
359 GVariant *message = NULL;
360 GVariantIter *iter = NULL;
364 message = stc_manager_gdbus_call_sync(connection,
366 CONNMAN_MANAGER_PATH,
367 CONNMAN_MANAGER_INTERFACE,
368 "GetServices", NULL);
369 if (message == NULL) {
370 STC_LOGE("Failed to get profiles"); //LCOV_EXCL_LINE
371 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
374 g_variant_get(message, "(a(oa{sv}))", &iter);
375 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
376 if (object_path == NULL)
377 continue; //LCOV_EXCL_LINE
379 if (__is_cellular_profile(object_path) &&
380 !__is_cellular_internet_profile(object_path))
383 if (__is_connected(next) == TRUE) {
384 /* reset old default connection data */
385 FREE(g_default_connection.path);
386 FREE(g_default_connection.ifname);
387 g_default_connection.type = STC_IFACE_UNKNOWN;
388 g_default_connection.roaming = FALSE;
390 g_default_connection.path = g_strdup(object_path);
392 g_variant_iter_free(next);
397 g_variant_iter_free(iter);
398 g_variant_unref(message);
400 if (__is_cellular_profile(g_default_connection.path)) {
401 g_default_connection.type = STC_IFACE_DATACALL; //LCOV_EXCL_LINE
402 __telephony_update_default_modem_subscriber_id(connection); //LCOV_EXCL_LINE
403 } else if (__is_wifi_profile(g_default_connection.path)) {
404 g_default_connection.type = STC_IFACE_WIFI; //LCOV_EXCL_LINE
405 } else if (__is_ethernet_profile(g_default_connection.path)) {
406 g_default_connection.type = STC_IFACE_WIRED; //LCOV_EXCL_LINE
407 } else if (__is_bluetooth_profile(g_default_connection.path)) {
408 g_default_connection.type = STC_IFACE_BLUETOOTH; //LCOV_EXCL_LINE
410 g_default_connection.type = STC_IFACE_UNKNOWN; //LCOV_EXCL_LINE
413 __get_default_connection_info(connection, g_default_connection.path);
415 __print_default_connection_info();
417 stc_monitor_update_rstn_by_default_connection(&g_default_connection);
418 stc_firewall_update();
420 return STC_ERROR_NONE;
423 static void _service_signal_cb(GDBusConnection *conn,
424 const gchar *name, const gchar *path,
425 const gchar *interface, const gchar *sig,
426 GVariant *param, gpointer user_data)
428 gchar *sigvalue = NULL;
429 GVariant *variant = NULL;
430 stc_s *stc = (stc_s *)stc_get_manager();
431 ret_msg_if(stc == NULL, "failed to get stc data");
433 if (path == NULL || param == NULL)
436 g_variant_get(param, "(sv)", &sigvalue, &variant);
437 if (sigvalue == NULL)
440 if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0)
443 if (g_strcmp0(sigvalue, "State") == 0 &&
444 g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
445 const gchar *state = NULL;
447 state = g_variant_get_string(variant, NULL);
448 if (g_strcmp0(state, "ready") == 0 ||
449 g_strcmp0(state, "online") == 0) {
450 if (g_strcmp0(g_default_connection.path, path)) {
451 __reset_default_connection_data();
452 __get_default_profile(stc->connection);
455 if (g_strcmp0(g_default_connection.path, path) == 0) {
456 __reset_default_connection_data(); //LCOV_EXCL_LINE
457 __get_default_profile(stc->connection); //LCOV_EXCL_LINE
460 } else if (g_strcmp0(sigvalue, "Roaming") == 0) {
462 if (g_strcmp0(g_default_connection.path, path) == 0) {
463 gboolean roaming = 0;
465 if (g_variant_is_of_type(variant,
466 G_VARIANT_TYPE_BOOLEAN)) {
467 roaming = g_variant_get_boolean(variant);
468 g_default_connection.roaming = roaming;
480 g_variant_unref(variant);
485 stc_error_e stc_default_connection_monitor_init(stc_s *stc)
487 ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
489 __get_default_profile(stc->connection);
490 g_default_connection_sub_id =
491 stc_manager_gdbus_subscribe_signal(stc->connection,
493 CONNMAN_SERVICE_INTERFACE,
494 CONNMAN_SIGNAL_PROPERTY_CHANGED,
496 G_DBUS_SIGNAL_FLAGS_NONE,
500 STC_LOGI("Successfully subscribed connman [%s] signal", CONNMAN_SIGNAL_PROPERTY_CHANGED);
501 return STC_ERROR_NONE;
504 stc_error_e stc_default_connection_monitor_deinit(stc_s *stc)
506 ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
508 stc_manager_gdbus_unsubscribe_signal(stc->connection,
509 g_default_connection_sub_id);
510 FREE(g_default_connection.path);
511 FREE(g_default_connection.ifname);
512 return STC_ERROR_NONE;
515 gchar *stc_default_connection_get_ifname(void)
517 return g_strdup(g_default_connection.ifname);
520 default_connection_s *stc_get_default_connection(void)
522 return &g_default_connection;