From 681a9dc90d29faa512d7bb6365847c4b76d550f5 Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Tue, 30 Aug 2022 17:30:27 +0900 Subject: [PATCH] Remove duplicated codes Change-Id: I4a10a0a94a543d2c876d49714ff1734576e789ea --- packaging/capi-network-tethering.spec | 2 +- src/tethering.c | 437 +++++++++------------------------- 2 files changed, 118 insertions(+), 321 deletions(-) diff --git a/packaging/capi-network-tethering.spec b/packaging/capi-network-tethering.spec index 88025c4..104a161 100644 --- a/packaging/capi-network-tethering.spec +++ b/packaging/capi-network-tethering.spec @@ -1,6 +1,6 @@ Name: capi-network-tethering Summary: Tethering Framework -Version: 1.2.0 +Version: 1.2.1 Release: 1 Group: System/Network License: Apache-2.0 diff --git a/src/tethering.c b/src/tethering.c index 1c334d7..1772308 100755 --- a/src/tethering.c +++ b/src/tethering.c @@ -360,6 +360,106 @@ static tethering_error_e __get_error(int agent_error) return err; } +static tethering_type_e __convert_to_tethering_type(mobile_ap_type_e type) +{ + switch (type) { + case MOBILE_AP_TYPE_USB: + return TETHERING_TYPE_USB; + case MOBILE_AP_TYPE_WIFI: + return TETHERING_TYPE_WIFI; + case MOBILE_AP_TYPE_BT: + return TETHERING_TYPE_BT; + case MOBILE_AP_TYPE_P2P: + return TETHERING_TYPE_P2P; + default: + return TETHERING_TYPE_MAX; + } +} + +static void __invoke_enable_cb(__tethering_h *th, tethering_type_e type, bool is_requested) +{ + if (th == NULL || th->enabled_cb[type] == NULL) { + ERR("th or enabled_cb is NULL"); + return; + } + + tethering_enabled_cb ecb = NULL; + void *data = NULL; + + if (!_tethering_check_handle(th)) { + DBG("Tethering handle is not valid now, ignore it."); + return; + } + + ecb = th->enabled_cb[type]; + data = th->enabled_user_data[type]; + ecb(TETHERING_ERROR_NONE, type, is_requested, data); +} + +static int __get_disabled_cause_by_interface_error(tethering_type_e type) +{ + switch (type) { + case TETHERING_TYPE_USB: + return TETHERING_DISABLED_BY_USB_DISCONNECTION; + case TETHERING_TYPE_WIFI: + return TETHERING_DISABLED_BY_WIFI_ON; + case TETHERING_TYPE_BT: + return TETHERING_DISABLED_BY_BT_OFF; + default: + return TETHERING_DISABLED_BY_OTHERS; + } +} + +static void __invoke_disabled_cb(__tethering_h *th, tethering_type_e type, GVariant *parameters) +{ + if (th == NULL || th->disabled_cb[type] == NULL) { + ERR("th or disabled_cb is NULL"); + return; + } + + tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS; + tethering_disabled_cb dcb = NULL; + void *data = NULL; + char *buf = NULL; + + if (!_tethering_check_handle(th)) { + DBG("Tethering handle is not valid now, ignore it."); + return; + } + + data = th->disabled_user_data[type]; + + g_variant_get(parameters, "(s)", &buf); + if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE)) + code = __get_disabled_cause_by_interface_error(type); + else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT)) + code = TETHERING_DISABLED_BY_TIMEOUT; + + dcb(TETHERING_ERROR_NONE, type, code, data); + g_free(buf); +} + +static void __invoke_disabled_cbs(__tethering_h *th, tethering_disabled_cause_e code) +{ + if (th == NULL) { + ERR("th is NULL"); + return; + } + + if (!_tethering_check_handle(th)) { + DBG("Tethering handle is not valid now, ignore it."); + return; + } + + for (tethering_type_e type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) { + tethering_disabled_cb dcb = th->disabled_cb[type]; + if (dcb == NULL) + continue; + void *data = th->disabled_user_data[type]; + dcb(TETHERING_ERROR_NONE, type, code, data); + } +} + static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name, const gchar *object_path, const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data) @@ -398,15 +498,8 @@ static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name, goto DONE; } - if (ap_type == MOBILE_AP_TYPE_USB) - type = TETHERING_TYPE_USB; - else if (ap_type == MOBILE_AP_TYPE_WIFI) - type = TETHERING_TYPE_WIFI; - else if (ap_type == MOBILE_AP_TYPE_BT) - type = TETHERING_TYPE_BT; - else if (ap_type == MOBILE_AP_TYPE_P2P) - type = TETHERING_TYPE_P2P; - else { + type = __convert_to_tethering_type(ap_type); + if (type == TETHERING_TYPE_MAX) { ERR("Not supported tethering type [%d]\n", ap_type); goto DONE; } @@ -444,30 +537,8 @@ static void __handle_net_closed(GDBusConnection *connection, const gchar *sender { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = 0; - tethering_disabled_cb dcb = NULL; - void *data = NULL; - tethering_disabled_cause_e code = TETHERING_DISABLED_BY_NETWORK_CLOSE; - SINFO("Tethering Disabled by network close !"); - - for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) { - dcb = th->disabled_cb[type]; - if (dcb == NULL) - continue; - data = th->disabled_user_data[type]; - - dcb(TETHERING_ERROR_NONE, type, code, data); - } - + __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_NETWORK_CLOSE); TETHERING_UNLOCK; DBG("-\n"); } @@ -478,35 +549,7 @@ static void __handle_wifi_tether_on(GDBusConnection *connection, const gchar *se { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = TETHERING_TYPE_WIFI; - bool is_requested = false; - tethering_enabled_cb ecb = NULL; - void *data = NULL; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - ecb = th->enabled_cb[type]; - if (ecb == NULL) { - TETHERING_UNLOCK; - return; - } - - data = th->enabled_user_data[type]; - - ecb(TETHERING_ERROR_NONE, type, is_requested, data); - + __invoke_enable_cb((__tethering_h *)user_data, TETHERING_TYPE_WIFI, false); TETHERING_UNLOCK; DBG("-\n"); } @@ -517,42 +560,7 @@ static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *s { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = TETHERING_TYPE_WIFI; - tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS; - tethering_disabled_cb dcb = NULL; - void *data = NULL; - char *buf = NULL; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - dcb = th->disabled_cb[type]; - if (dcb == NULL) { - TETHERING_UNLOCK; - return; - } - - data = th->disabled_user_data[type]; - g_variant_get(parameters, "(s)", &buf); - if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE)) - code = TETHERING_DISABLED_BY_WIFI_ON; - else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT)) - code = TETHERING_DISABLED_BY_TIMEOUT; - - g_free(buf); - dcb(TETHERING_ERROR_NONE, type, code, data); - + __invoke_disabled_cb((__tethering_h *)user_data, TETHERING_TYPE_WIFI, parameters); TETHERING_UNLOCK; DBG("-\n"); } @@ -563,35 +571,7 @@ static void __handle_usb_tether_on(GDBusConnection *connection, const gchar *sen { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = TETHERING_TYPE_USB; - bool is_requested = false; - tethering_enabled_cb ecb = NULL; - void *data = NULL; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - ecb = th->enabled_cb[type]; - if (ecb == NULL) { - TETHERING_UNLOCK; - return; - } - - data = th->enabled_user_data[type]; - - ecb(TETHERING_ERROR_NONE, type, is_requested, data); - + __invoke_enable_cb((__tethering_h *)user_data, TETHERING_TYPE_USB, false); TETHERING_UNLOCK; DBG("-\n"); } @@ -602,41 +582,7 @@ static void __handle_usb_tether_off(GDBusConnection *connection, const gchar *se { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = TETHERING_TYPE_USB; - tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS; - tethering_disabled_cb dcb = NULL; - void *data = NULL; - char *buf = NULL; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - dcb = th->disabled_cb[type]; - if (dcb == NULL) { - TETHERING_UNLOCK; - return; - } - - data = th->disabled_user_data[type]; - - g_variant_get(parameters, "(s)", &buf); - if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE)) - code = TETHERING_DISABLED_BY_USB_DISCONNECTION; - - dcb(TETHERING_ERROR_NONE, type, code, data); - g_free(buf); - + __invoke_disabled_cb((__tethering_h *)user_data, TETHERING_TYPE_USB, parameters); TETHERING_UNLOCK; DBG("-\n"); } @@ -647,35 +593,7 @@ static void __handle_bt_tether_on(GDBusConnection *connection, const gchar *send { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = TETHERING_TYPE_BT; - bool is_requested = false; - tethering_enabled_cb ecb = NULL; - void *data = NULL; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - ecb = th->enabled_cb[type]; - if (ecb == NULL) { - TETHERING_UNLOCK; - return; - } - - data = th->enabled_user_data[type]; - - ecb(TETHERING_ERROR_NONE, type, is_requested, data); - + __invoke_enable_cb((__tethering_h *)user_data, TETHERING_TYPE_BT, false); TETHERING_UNLOCK; DBG("-\n"); } @@ -686,43 +604,7 @@ static void __handle_bt_tether_off(GDBusConnection *connection, const gchar *sen { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = TETHERING_TYPE_BT; - tethering_disabled_cause_e code = TETHERING_DISABLED_BY_OTHERS; - tethering_disabled_cb dcb = NULL; - void *data = NULL; - char *buf = NULL; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - dcb = th->disabled_cb[type]; - if (dcb == NULL) { - TETHERING_UNLOCK; - return; - } - - data = th->disabled_user_data[type]; - g_variant_get(parameters, "(s)", &buf); - if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE)) - code = TETHERING_DISABLED_BY_BT_OFF; - else if (!g_strcmp0(buf, SIGNAL_MSG_TIMEOUT)) - code = TETHERING_DISABLED_BY_TIMEOUT; - - dcb(TETHERING_ERROR_NONE, type, code, data); - - g_free(buf); - + __invoke_disabled_cb((__tethering_h *)user_data, TETHERING_TYPE_BT, parameters); TETHERING_UNLOCK; DBG("-\n"); } @@ -733,34 +615,7 @@ static void __handle_no_data_timeout(GDBusConnection *connection, const gchar *s { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = 0; - tethering_disabled_cb dcb = NULL; - void *data = NULL; - tethering_disabled_cause_e code = TETHERING_DISABLED_BY_TIMEOUT; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) { - dcb = th->disabled_cb[type]; - if (dcb == NULL) - continue; - data = th->disabled_user_data[type]; - - dcb(TETHERING_ERROR_NONE, type, code, data); - } - + __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_TIMEOUT); TETHERING_UNLOCK; DBG("-\n"); } @@ -771,34 +626,7 @@ static void __handle_low_battery_mode(GDBusConnection *connection, const gchar * { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = 0; - tethering_disabled_cb dcb = NULL; - void *data = NULL; - tethering_disabled_cause_e code = TETHERING_DISABLED_BY_LOW_BATTERY; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) { - dcb = th->disabled_cb[type]; - if (dcb == NULL) - continue; - data = th->disabled_user_data[type]; - - dcb(TETHERING_ERROR_NONE, type, code, data); - } - + __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_LOW_BATTERY); TETHERING_UNLOCK; DBG("-\n"); } @@ -809,34 +637,7 @@ static void __handle_flight_mode(GDBusConnection *connection, const gchar *sende { DBG("+\n"); TETHERING_LOCK; - - if (user_data == NULL) { - ERR("parameter(user_data) is NULL"); - TETHERING_UNLOCK; - return; - } - - __tethering_h *th = (__tethering_h *)user_data; - tethering_type_e type = 0; - tethering_disabled_cb dcb = NULL; - void *data = NULL; - tethering_disabled_cause_e code = TETHERING_DISABLED_BY_FLIGHT_MODE; - - if (!_tethering_check_handle(th)) { - DBG("Tethering handle is not valid now, ignore it."); - TETHERING_UNLOCK; - return; - } - - for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) { - dcb = th->disabled_cb[type]; - if (dcb == NULL) - continue; - data = th->disabled_user_data[type]; - - dcb(TETHERING_ERROR_NONE, type, code, data); - } - + __invoke_disabled_cbs((__tethering_h *)user_data, TETHERING_DISABLED_BY_FLIGHT_MODE); TETHERING_UNLOCK; DBG("-\n"); } @@ -2946,20 +2747,16 @@ API int tethering_foreach_connected_clients(tethering_h tethering, tethering_typ while (g_variant_iter_loop(inner_iter, "{sv}", &key, &value)) { if (g_strcmp0(key, "Type") == 0) { interface = g_variant_get_int32(value); - if (interface == MOBILE_AP_TYPE_USB) - client.interface = TETHERING_TYPE_USB; - else if (interface == MOBILE_AP_TYPE_WIFI) - client.interface = TETHERING_TYPE_WIFI; - else if (interface == MOBILE_AP_TYPE_BT) - client.interface = TETHERING_TYPE_BT; - else if (interface == MOBILE_AP_TYPE_P2P) - client.interface = TETHERING_TYPE_P2P; - else { + tethering_type_e converted_type = __convert_to_tethering_type(interface); + if (converted_type == TETHERING_TYPE_MAX) { ERR("Invalid interface\n"); g_free(key); g_variant_unref(value); break; + } else { + client.interface = converted_type; } + DBG("interface is %d\n", client.interface); if (client.interface != type && (TETHERING_TYPE_ALL != type)) { g_free(key); -- 2.7.4