From 0deddab8528ce57ee745d2f42bea9a7532f2023d Mon Sep 17 00:00:00 2001 From: Sudipto Date: Fri, 27 Sep 2019 13:26:41 +0530 Subject: [PATCH] Provide methods to initialize and deinitialize battery monitor feature Provide callback during initialization so that the following bluetooth data for the session is provided to battery monitor frwk when bluetooth adapter is disabled: session_start_time session_end_time session_scan_time session_connected_time app-wise data transaction details for the session Change-Id: If48adf6944a50d67e91d3b18c54b163b5adc745b Signed-off-by: Sudipto --- include/bluetooth_internal.h | 30 +++++++++++++++++++++++++ include/bluetooth_private.h | 1 + include/bluetooth_type_internal.h | 14 ++++++++++++ src/bluetooth-adapter.c | 33 +++++++++++++++++++++++++++ src/bluetooth-common.c | 7 +++++- test/bt_unit_test.c | 37 +++++++++++++++++++++++++++++++ test/bt_unit_test.h | 2 ++ 7 files changed, 123 insertions(+), 1 deletion(-) diff --git a/include/bluetooth_internal.h b/include/bluetooth_internal.h index ebcac01..8344a44 100644 --- a/include/bluetooth_internal.h +++ b/include/bluetooth_internal.h @@ -123,6 +123,36 @@ int bt_adapter_read_battery_info(bt_battery_info_s *data); */ int bt_adapter_disable(void); +/** + * @ingroup CAPI_NETWORK_BLUETOOTH_ADAPTER_MODULE + * @brief Initializes and activates battery monitor + * @since_tizen5.5 + * + * @param[in] callback Address of callback function + * @param[in] user_data user + * + * @pre This function will initialize and activate the battery monitor feature. + * It sets a callback function that will be called when bluetooth adapter + * is disabled. + * + * @see *bt_adapter_disable_battery_cb + */ +int bt_adapter_init_battery_monitor(bt_adapter_disable_battery_cb callback, + void *user_data); + +/** + * @ingroup CAPI_NETWORK_BLUETOOTH_ADAPTER_MODULE + * @brief Deinitializes and disables battery monitor. + * @since_tizen5.5 + * + * @pre This function will disable the battery monitor feature + * and unset the related callback function + * + * @see bt_adapter_init_battery_monitor + * @see *bt_adapter_disable_battery_cb + */ +int bt_adapter_deinit_battery_monitor(); + /** * @internal * @ingroup CAPI_NETWORK_BLUETOOTH_ADAPTER_MODULE diff --git a/include/bluetooth_private.h b/include/bluetooth_private.h index a95b30f..882ed04 100644 --- a/include/bluetooth_private.h +++ b/include/bluetooth_private.h @@ -67,6 +67,7 @@ extern "C" { */ typedef enum { BT_EVENT_STATE_CHANGED = 0x00, /**< Adapter state is changed */ + BT_EVENT_ADAPTER_DISABLED_BATTERY_DATA, /**Notify BT usage data to battery monitor frwk**/ BT_EVENT_LE_STATE_CHANGED, /**< Adapter le state is changed */ BT_EVENT_NAME_CHANGED, /**< Adapter name is changed */ BT_EVENT_VISIBILITY_MODE_CHANGED, /**< Adapter visibility mode is changed */ diff --git a/include/bluetooth_type_internal.h b/include/bluetooth_type_internal.h index 2823237..1eca276 100644 --- a/include/bluetooth_type_internal.h +++ b/include/bluetooth_type_internal.h @@ -336,6 +336,20 @@ typedef void (*bt_map_client_get_message_cb)(int result, bt_map_client_message_h typedef void (*bt_adapter_connectable_changed_cb) (int result, bool connectable, void *user_data); +/** + * @ingroup CAPI_NETWORK_BLUETOOTH_ADAPTER_MODULE + * @brief Called when adapter is disabled. + * @since_tizen5.5 + * + * @param[in] data Bluetooth session data + * + * @pre This function will be invoked when Bluetooth adapter is disabled + * if you activate the battery monitor feature using bt_adapter_init_battery_monitor + * + * @see bt_adapter_init_battery_monitor + */ +typedef void (*bt_adapter_disable_battery_cb)(bt_battery_info_s *data); + /** * @ingroup CAPI_NETWORK_BLUETOOTH_OPP_SERVER_MODULE * @brief Called when the push is requested. diff --git a/src/bluetooth-adapter.c b/src/bluetooth-adapter.c index 5d9e469..4427f6f 100644 --- a/src/bluetooth-adapter.c +++ b/src/bluetooth-adapter.c @@ -612,6 +612,39 @@ int bt_adapter_unset_connectable_changed_cb(void) return BT_ERROR_NONE; } +int bt_adapter_init_battery_monitor(bt_adapter_disable_battery_cb callback, + void *user_data) +{ + BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON); + BT_CHECK_INIT_STATUS(); + BT_CHECK_INPUT_PARAMETER(callback); + + int ret = bluetooth_set_battery_monitor_state(TRUE); + if (ret != BT_ERROR_NONE) { + BT_ERR("Battery monitor could not be activated"); + return BT_ERROR_OPERATION_FAILED; + } + + _bt_set_cb(BT_EVENT_ADAPTER_DISABLED_BATTERY_DATA, callback, user_data); + + return BT_ERROR_NONE; +} + +int bt_adapter_deinit_battery_monitor(void) +{ + BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON); + BT_CHECK_INIT_STATUS(); + + int ret = bluetooth_set_battery_monitor_state(FALSE); + if (ret != BT_ERROR_NONE) { + BT_ERR("Battery monitor could not be deactivated"); + return BT_ERROR_OPERATION_FAILED; + } + + _bt_unset_cb(BT_EVENT_ADAPTER_DISABLED_BATTERY_DATA); + return BT_ERROR_NONE; +} + int bt_adapter_get_connectable(bool *connectable) { gboolean is_connectable = FALSE; diff --git a/src/bluetooth-common.c b/src/bluetooth-common.c index d5966fb..c59f074 100644 --- a/src/bluetooth-common.c +++ b/src/bluetooth-common.c @@ -127,6 +127,7 @@ typedef struct { static bt_event2index_table_t event2index[] = { { BLUETOOTH_EVENT_ENABLED, BT_EVENT_STATE_CHANGED }, { BLUETOOTH_EVENT_DISABLED, BT_EVENT_STATE_CHANGED }, + { BLUETOOTH_EVENT_DISABLED_BATTERY_DATA, BT_EVENT_ADAPTER_DISABLED_BATTERY_DATA}, { BLUETOOTH_EVENT_LE_ENABLED, BT_EVENT_LE_STATE_CHANGED }, { BLUETOOTH_EVENT_LE_DISABLED, BT_EVENT_LE_STATE_CHANGED }, { BLUETOOTH_EVENT_LOCAL_NAME_CHANGED, BT_EVENT_NAME_CHANGED }, @@ -1347,7 +1348,11 @@ static void __bt_event_proxy(int event, bluetooth_event_param_t *param, void *us case BLUETOOTH_EVENT_DISABLED: BT_INFO("bt_adapter_state_changed_cb() will be called with BT_ADAPTER_DISABLED"); ((bt_adapter_state_changed_cb) bt_event_slot_container[event_index].callback) - (_bt_get_error_code(param->result), BT_ADAPTER_DISABLED, bt_event_slot_container[event_index].user_data); + (_bt_get_error_code(param->result), BT_ADAPTER_DISABLED, bt_event_slot_container[event_index].user_data); + break; + case BLUETOOTH_EVENT_DISABLED_BATTERY_DATA: + BT_INFO("bt_adapter_disable_battery_cb() will be called"); + ((bt_adapter_disable_battery_cb)bt_event_slot_container[event_index].callback)((bt_battery_info_s *)(param->param_data)); break; case BLUETOOTH_EVENT_LOCAL_NAME_CHANGED: BT_INFO("bt_adapter_name_changed_cb() will be called"); diff --git a/test/bt_unit_test.c b/test/bt_unit_test.c index ee3c185..5815367 100644 --- a/test/bt_unit_test.c +++ b/test/bt_unit_test.c @@ -239,6 +239,10 @@ tc_table_t tc_adapter[] = { , BT_UNIT_TEST_FUNCTION_ADAPTER_SET_REMOTE_OOB_EXT_DATA}, {"bt_adapter_read_battery_info" , BT_UNIT_TEST_FUNCTION_ADAPTER_READ_BATTERY_INFO}, + {"bt_adapter_init_battery_monitor" + , BT_UNIT_TEST_FUNCTION_ADAPTER_INIT_BATTERY_MONITOR}, + {"bt_adapter_deinit_battery_monitor" + , BT_UNIT_TEST_FUNCTION_ADAPTER_DEINIT_BATTERY_MONITOR}, {"bt_adapter_set_visibility_mode_changed_cb" , BT_UNIT_TEST_FUNCTION_ADAPTER_SET_VISIBILITY_MODE_CHANGED_CB}, {"bt_adapter_unset_visibility_mode_changed_cb" @@ -1467,6 +1471,23 @@ void __bt_adapter_state_changed_cb(int result, "ENABLED" : "DISABLED"); } +static void __bt_adapter_disable_battery_cb(bt_battery_info_s *data) +{ + TC_PRT("Battery data: %ld %ld %d %d", data->session_start_time, + data->session_end_time, data->session_scan_time, data->session_connected_time); + if (data->atm_list == NULL) { + TC_PRT("No data transaction in this session"); + return; + } + + TC_PRT("Displaying app-wise transaction details"); + for (GSList *l = data->atm_list; l != NULL; l = g_slist_next(l)) { + bt_battery_app_info_s *t = (bt_battery_app_info_s *)(l->data); + TC_PRT("uid: %ld, pid: %ld, received bytes: %d, sent bytes: %d", + (long int)(t->uid), (long int)(t->pid), t->rx_bytes, t->tx_bytes); + } +} + static void __bt_adapter_device_visibility_mode_changed_cb(int result, bt_adapter_visibility_mode_e visibility_mode, void *user_data) @@ -4243,6 +4264,22 @@ int test_input_callback(void *data) } break; } + case BT_UNIT_TEST_FUNCTION_ADAPTER_INIT_BATTERY_MONITOR: { + int ret = bt_adapter_init_battery_monitor(__bt_adapter_disable_battery_cb, NULL); + if (ret == BLUETOOTH_ERROR_NONE) + TC_PRT("Battery monitor initialized and activated"); + else + TC_PRT("Battery monitor could not be initialized and activated"); + break; + } + case BT_UNIT_TEST_FUNCTION_ADAPTER_DEINIT_BATTERY_MONITOR: { + int ret = bt_adapter_deinit_battery_monitor(); + if (ret == BLUETOOTH_ERROR_NONE) + TC_PRT("Battery monitor deinitialized successfully"); + else + TC_PRT("Battery monitor could not be deinitialized"); + break; + } case BT_UNIT_TEST_FUNCTION_ADAPTER_SET_REMOTE_OOB_EXT_DATA: { char remote_addr[18]; unsigned char *param_data[4]; diff --git a/test/bt_unit_test.h b/test/bt_unit_test.h index 2278306..43294e2 100644 --- a/test/bt_unit_test.h +++ b/test/bt_unit_test.h @@ -85,6 +85,8 @@ typedef enum { BT_UNIT_TEST_FUNCTION_ADAPTER_SET_REMOTE_OOB_EXT_DATA, BT_UNIT_TEST_FUNCTION_ADAPTER_REMOVE_REMOTE_OOB_DATA, BT_UNIT_TEST_FUNCTION_ADAPTER_READ_BATTERY_INFO, + BT_UNIT_TEST_FUNCTION_ADAPTER_INIT_BATTERY_MONITOR, + BT_UNIT_TEST_FUNCTION_ADAPTER_DEINIT_BATTERY_MONITOR, BT_UNIT_TEST_FUNCTION_ADAPTER_SET_VISIBILITY_MODE_CHANGED_CB, BT_UNIT_TEST_FUNCTION_ADAPTER_UNSET_VISIBILITY_MODE_CHANGED_CB, BT_UNIT_TEST_FUNCTION_ADAPTER_SET_VISIBILITY_DURATION_CHANGED_CB, -- 2.34.1