modules/connection: rewrite hotspot
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Fri, 12 Feb 2016 13:08:49 +0000 (14:08 +0100)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Fri, 19 Feb 2016 10:17:00 +0000 (02:17 -0800)
Rewrite hotspot module using tethering API.

Change-Id: I215e333b938de4811621533abaa8001b3fa32981
Signed-off-by: Lukasz Stanislawski <l.stanislaws@samsung.com>
src/modules/connection/mobile_hotspot.c

index 4789fa0..01a72a9 100644 (file)
@@ -21,9 +21,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <vconf.h>
-#include <system_settings.h>
-#include <runtime_info.h>
+#include <tethering.h>
 
 #include "common.h"
 #include "indicator.h"
@@ -53,9 +51,19 @@ icon_s mobile_hotspot = {
        .wake_up = wake_up_cb
 };
 
+typedef struct {
+       tethering_h handle;
+       bool bt_enabled;
+       bool bt_client_connected;
+       bool wifi_enabled;
+       bool wifi_client_connected;
+       bool usb_enabled;
+       bool usb_client_connected;
+} tethering_data_t;
+
 enum {
-       TETHERING_ALL_ON_CONNECTED = 0,
-       TETHERING_ALL_ON_NOT_CONNECTED,
+       TETHERING_MULTI_ON_CONNECTED = 0,
+       TETHERING_MULTI_ON_NOT_CONNECTED,
        TETHERING_BT_ON_CONNECTED,
        TETHERING_BT_ON_NOT_CONNECTED,
        TETHERING_USB_ON_CONNECTED,
@@ -66,8 +74,8 @@ enum {
 };
 
 static const char *icon_path[TETHERING_MAX] = {
-       [TETHERING_ALL_ON_CONNECTED]            = "tethering/B03_All_connected.png",
-       [TETHERING_ALL_ON_NOT_CONNECTED]        = "tethering/B03_All_no_connected.png",
+       [TETHERING_MULTI_ON_CONNECTED]          = "tethering/B03_All_connected.png",
+       [TETHERING_MULTI_ON_NOT_CONNECTED]      = "tethering/B03_All_no_connected.png",
        [TETHERING_BT_ON_CONNECTED]             = "tethering/B03_BT_connected.png",
        [TETHERING_BT_ON_NOT_CONNECTED]         = "tethering/B03_BT_no_connected.png",
        [TETHERING_USB_ON_CONNECTED]            = "tethering/B03_USB_connected.png",
@@ -75,24 +83,21 @@ static const char *icon_path[TETHERING_MAX] = {
        [TETHERING_WIFI_ON_CONNECTED]           = "tethering/B03_Wi_Fi_connected.png",
        [TETHERING_WIFI_ON_NOT_CONNECTED]       = "tethering/B03_Wi_Fi_no_connected.png",
 };
+
 static int updated_while_lcd_off = 0;
 static int prevIndex = -1;
+static tethering_data_t tet_data;
 
 
-
-static void set_app_state(void* data)
+static void set_app_state(void *data)
 {
        mobile_hotspot.ad = data;
 }
 
-
-
 static void show_image_icon(int type)
 {
-       if(prevIndex == type)
-       {
+       if (prevIndex == type)
                return;
-       }
 
        mobile_hotspot.img_obj.data = icon_path[type];
        icon_show(&mobile_hotspot);
@@ -100,8 +105,6 @@ static void show_image_icon(int type)
        prevIndex = type;
 }
 
-
-
 static void hide_image_icon(void)
 {
        icon_hide(&mobile_hotspot);
@@ -109,14 +112,9 @@ static void hide_image_icon(void)
        prevIndex = -1;
 }
 
-
-
-static void indicator_mobile_hotspot_change_cb(void *user_data)
+static void _mobile_hotspot_view_update(tethering_data_t *data)
 {
-       int ret;
-       bool usb_enabled, wifi_enabled, bt_enabled;
-
-       retm_if(user_data == NULL, "Invalid parameter!");
+       retm_if(data == NULL, "Invalid parameter!");
 
        if (icon_get_update_flag() == 0) {
                updated_while_lcd_off = 1;
@@ -124,95 +122,168 @@ static void indicator_mobile_hotspot_change_cb(void *user_data)
        }
        updated_while_lcd_off = 0;
 
-       ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_TETHERING_ENABLED, &usb_enabled);
-       retm_if(ret != RUNTIME_INFO_ERROR_NONE, "runtime_info_get_value_bool failed");
-
-       ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, &wifi_enabled);
-       retm_if(ret != RUNTIME_INFO_ERROR_NONE, "runtime_info_get_value_bool failed");
-
-       ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BLUETOOTH_TETHERING_ENABLED, &bt_enabled);
-       retm_if(ret != RUNTIME_INFO_ERROR_NONE, "runtime_info_get_value_bool failed");
-
        /* How many tethering methods are on */
-       int sum = (usb_enabled ? 1 : 0) + (wifi_enabled ? 1 : 0) + (bt_enabled ? 1 : 0);
+       int sum = (data->usb_enabled ? 1 : 0) + (data->wifi_enabled ? 1 : 0) + (data->bt_enabled ? 1 : 0);
 
-       if (sum == 0) {
+       if (sum == 0)
                hide_image_icon();
-       }
 
        if (sum >= 2) {
-               show_image_icon(TETHERING_ALL_ON_CONNECTED);
+               if (data->usb_client_connected || data->wifi_client_connected || data->bt_client_connected)
+                       show_image_icon(TETHERING_MULTI_ON_CONNECTED);
+               else
+                       show_image_icon(TETHERING_MULTI_ON_NOT_CONNECTED);
+               return;
        }
 
-       if (bt_enabled) {
-               show_image_icon(TETHERING_BT_ON_CONNECTED);
+       if (data->bt_enabled) {
+               if (data->bt_client_connected)
+                       show_image_icon(TETHERING_BT_ON_CONNECTED);
+               else
+                       show_image_icon(TETHERING_BT_ON_NOT_CONNECTED);
        }
 
-       if (usb_enabled) {
-               show_image_icon(TETHERING_USB_ON_CONNECTED);
+       if (data->usb_enabled) {
+               if (data->usb_client_connected)
+                       show_image_icon(TETHERING_USB_ON_CONNECTED);
+               else
+                       show_image_icon(TETHERING_USB_ON_NOT_CONNECTED);
        }
 
-       if (wifi_enabled) {
-               show_image_icon(TETHERING_WIFI_ON_CONNECTED);
+       if (data->wifi_enabled) {
+               if (data->wifi_client_connected)
+                       show_image_icon(TETHERING_WIFI_ON_CONNECTED);
+               else
+                       show_image_icon(TETHERING_WIFI_ON_NOT_CONNECTED);
        }
 }
 
-
-static void _runtime_info_key_update_cb(runtime_info_key_e key, void *user_data)
-{
-       indicator_mobile_hotspot_change_cb(user_data);
-}
-
-
 static int wake_up_cb(void *data)
 {
-       if(updated_while_lcd_off==0)
-       {
+       if (updated_while_lcd_off == 0)
                return OK;
-       }
 
-       indicator_mobile_hotspot_change_cb(data);
+       _mobile_hotspot_view_update(&tet_data);
        return OK;
 }
 
+static bool _teter_client_cb(tethering_client_h client, void *user_data)
+{
+       tethering_data_t *td = user_data;
+       tethering_type_e type;
+
+       int ret = tethering_client_get_tethering_type(client, &type);
+       retvm_if(ret != TETHERING_ERROR_NONE, false, "tethering_client_get_tethering_type failed: %d", get_error_message(ret));
+
+       switch (type) {
+       case TETHERING_TYPE_BT:
+               td->bt_client_connected = true;
+               break;
+       case TETHERING_TYPE_WIFI:
+               td->wifi_client_connected = true;
+               break;
+       case TETHERING_TYPE_USB:
+               td->usb_client_connected = true;
+               break;
+       case TETHERING_TYPE_ALL:
+               td->bt_client_connected = true;
+               td->wifi_client_connected = true;
+               td->usb_client_connected = true;
+               break;
+       default:
+               break;
+       }
+       return true;
+}
 
+static int _tethering_data_reload(tethering_data_t *data)
+{
+       data->bt_enabled = tethering_is_enabled(data->handle, TETHERING_TYPE_BT);
+       data->wifi_enabled = tethering_is_enabled(data->handle, TETHERING_TYPE_WIFI);
+       data->usb_enabled = tethering_is_enabled(data->handle, TETHERING_TYPE_USB);
 
-static int register_mobile_hotspot_module(void *data)
+       data->bt_client_connected = data->wifi_client_connected = data->usb_client_connected = false;
+
+       if (!data->bt_enabled && !data->wifi_enabled && !data->usb_enabled)
+               return 0;
+
+       int ret = tethering_foreach_connected_clients(data->handle, TETHERING_TYPE_ALL, _teter_client_cb, data);
+       retvm_if(ret != TETHERING_ERROR_NONE, FAIL, "tethering_foreach_connected_clients failed: %d", get_error_message(ret));
+
+       return 0;
+}
+
+static void _tethering_data_connection_state_changed(tethering_client_h client, bool opened, void *user_data)
 {
-       int ret;
+       _tethering_data_reload(user_data);
+       _mobile_hotspot_view_update(user_data);
+}
 
-       retvm_if(data == NULL, FAIL, "Invalid parameter!");
+static void _tethering_data_enabled_changed(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
+{
+       _tethering_data_reload(user_data);
+       _mobile_hotspot_view_update(user_data);
+}
 
-       set_app_state(data);
+static void _tethering_data_disabled(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
+{
+       _tethering_data_reload(user_data);
+       _mobile_hotspot_view_update(user_data);
+}
 
-       ret = util_runtime_info_set_changed_cb(RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, _runtime_info_key_update_cb, data);
-       retvm_if(ret != 0, FAIL, "util_runtime_info_set_changed_cb failed.");
+static int _tethering_data_init(tethering_data_t *data)
+{
+       int ret = tethering_create(&data->handle);
+       retvm_if(ret != TETHERING_ERROR_NONE, FAIL, "tethering_create failed: %d", get_error_message(ret));
+
+       ret = tethering_set_connection_state_changed_cb(data->handle, TETHERING_TYPE_ALL, _tethering_data_connection_state_changed, data);
+       if (ret != TETHERING_ERROR_NONE) {
+               tethering_destroy(data->handle);
+               ERR("tethering_set_connection_state_changed_cb failed: %d", get_error_message(ret));
+               return FAIL;
+       }
 
-       ret = util_runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BLUETOOTH_TETHERING_ENABLED, _runtime_info_key_update_cb, data);
-       if (ret != 0) {
-               util_runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, _runtime_info_key_update_cb);
-               ERR("util_runtime_info_set_changed_cb failed");
+       ret = tethering_set_enabled_cb(data->handle, TETHERING_TYPE_ALL, _tethering_data_enabled_changed, data);
+       if (ret != TETHERING_ERROR_NONE) {
+               tethering_destroy(data->handle);
+               ERR("tethering_set_enabled_cb failed: %d", get_error_message(ret));
                return FAIL;
        }
 
-       ret = util_runtime_info_set_changed_cb(RUNTIME_INFO_KEY_USB_TETHERING_ENABLED, _runtime_info_key_update_cb, data);
-       if (ret != 0) {
-               util_runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, _runtime_info_key_update_cb);
-               util_runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_BLUETOOTH_TETHERING_ENABLED, _runtime_info_key_update_cb);
-               ERR("util_runtime_info_set_changed_cb failed");
+       ret = tethering_set_disabled_cb(data->handle, TETHERING_TYPE_ALL, _tethering_data_disabled, data);
+       if (ret != TETHERING_ERROR_NONE) {
+               tethering_destroy(data->handle);
+               ERR("tethering_set_disabled_cb failed: %d", get_error_message(ret));
                return FAIL;
        }
 
-       indicator_mobile_hotspot_change_cb(data);
+       return _tethering_data_reload(data);
+}
+
+static void _tethering_data_shutdown(tethering_data_t *data)
+{
+       tethering_destroy(data->handle);
+}
+
+static int register_mobile_hotspot_module(void *data)
+{
+       int ret;
+
+       retvm_if(data == NULL, FAIL, "Invalid parameter!");
+
+       set_app_state(data);
+
+       ret = _tethering_data_init(&tet_data);
+       retvm_if(ret != 0, FAIL, "_tethering_data_init failed");
+
+       _mobile_hotspot_view_update(&tet_data);
 
        return OK;
 }
 
 static int unregister_mobile_hotspot_module(void)
 {
-       util_runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, _runtime_info_key_update_cb);
-       util_runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, _runtime_info_key_update_cb);
-       util_runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_USB_TETHERING_ENABLED, _runtime_info_key_update_cb);
+       _tethering_data_shutdown(&tet_data);
 
        return OK;
 }