Providing bluetooth usage data for battery monitor framework 33/213033/5
authorSudipto <sudipto.bal@samsung.com>
Thu, 29 Aug 2019 10:35:07 +0000 (16:05 +0530)
committerSudipto <sudipto.bal@samsung.com>
Mon, 16 Sep 2019 05:54:25 +0000 (11:24 +0530)
The following data is provided to BT CAPI to be used
by battery monitor framework:

Session start time
Session end time
Session scan time
Session connected time

Change-Id: I3873d521f35c76bd71a5cae1dfb6c4724b015997
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
bt-api/bt-adapter.c
bt-service-adaptation/CMakeLists.txt
bt-service-adaptation/services/bt-request-handler.c
bt-service-adaptation/services/bt-service-battery-monitor.c [new file with mode: 0644]
bt-service-adaptation/services/bt-service-event-receiver.c
bt-service-adaptation/services/include/bt-service-battery-monitor.h [new file with mode: 0644]
include/bluetooth-api.h
include/bt-internal-types.h

index ef01810..feeb85d 100644 (file)
@@ -104,6 +104,49 @@ BT_EXPORT_API int bluetooth_enable_adapter(void)
        return result;
 }
 
+BT_EXPORT_API int bluetooth_read_battery_data(bt_battery_data *data)
+{
+       int result;
+       bt_battery_data *info = NULL;
+
+       BT_CHECK_PARAMETER(data, return);
+
+       BT_INFO("### Requesting battery data");
+       retv_if(bluetooth_check_adapter() != BLUETOOTH_ADAPTER_ENABLED,
+                               BLUETOOTH_ADAPTER_DISABLED);
+
+#ifdef TIZEN_FEATURE_BT_DPM
+       retv_if(bluetooth_dpm_is_mode_allowed() == BLUETOOTH_ERROR_PERMISSION_DEINED,
+                               BLUETOOTH_ERROR_PERMISSION_DEINED);
+#endif
+
+       BT_INIT_PARAMS();
+       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+       result = _bt_send_request(BT_BLUEZ_SERVICE, BT_BATTERY_READ_DATA,
+               in_param1, in_param2, in_param3, in_param4, &out_param);
+
+       if (result != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("Error encountered");
+               return result;
+       }
+
+       BT_DBG("Received data from bt-service-adaptation");
+       info = &g_array_index(out_param, bt_battery_data, 0);
+
+       data->session_start_time = info->session_start_time;
+       data->session_end_time = info->session_end_time;
+       data->session_scan_time = info->session_scan_time;
+       data->session_connected_time = info->session_connected_time;
+       data->atm_list = NULL;
+
+       BT_DBG("Received battery data in bt-api: %ld %ld %d %d.", data->session_start_time, data->session_end_time, data->session_scan_time, data->session_connected_time);
+
+       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+       return result;
+}
+
 BT_EXPORT_API int bluetooth_disable_adapter(void)
 {
        int result;
index ed2cc9c..2822018 100644 (file)
@@ -7,6 +7,7 @@ SET(SRCS
 ./services/bt-service-main.c
 ./services/bt-service-common.c
 ./services/bt-service-event-sender.c
+./services/bt-service-battery-monitor.c
 ./services/bt-service-util.c
 ./services/bt-request-handler.c
 ./services/adapter/bt-service-core-adapter.c
index dc1fe9f..62d80b3 100644 (file)
@@ -54,6 +54,8 @@
 #include "bt-service-pbap.h"
 #include "bt-service-oob.h"
 
+#include "bt-service-battery-monitor.h"
+
 #ifdef TIZEN_FEATURE_BT_PAN_NAP
 #include "bt-service-network.h"
 #endif
@@ -69,7 +71,6 @@ static guint owner_id = 0;
 static guint owner_sig_id = 0;
 static gboolean is_le_intended = FALSE;
 
-
 static cynara *p_cynara;
 static cynara_configuration *conf;
 
@@ -155,7 +156,6 @@ void _bt_free_info_from_invocation_list(invocation_info_t *req_info)
 {
        GSList *l;
        invocation_info_t *info;
-
        ret_if(NULL == req_info);
        ret_if(NULL == req_info->sender);
 
@@ -254,6 +254,18 @@ void _bt_save_invocation_context(GDBusMethodInvocation *invocation, int result,
 
 }
 
+static int __bt_bm_request_data(_bt_battery_data_t *latest)
+{
+        int ret = _bt_bm_read_data(latest);
+       if (ret == BLUETOOTH_ERROR_NONE)
+               BT_DBG("Received data from battery monitor plugin: %ld %ld %d %d",
+                       latest->session_start_time, latest->session_end_time,
+                               latest->session_scan_time, latest->session_connected_time);
+       else
+               BT_ERR("Error encountered");
+        return ret;
+}
+
 static void __bt_service_method(GDBusConnection *connection,
                const gchar *sender,
                const gchar *object_path,
@@ -444,6 +456,12 @@ int __bt_bluez_request(int function_name,
        case BT_RESET_ADAPTER:
                result = _bt_reset_adapter();
                break;
+       case BT_BATTERY_READ_DATA: {
+               _bt_battery_data_t data;
+               result = __bt_bm_request_data(&data);
+               g_array_append_vals(*out_param1, &data, sizeof(_bt_battery_data_t));
+               break;
+       }
        case BT_CHECK_ADAPTER: {
                int enabled = BT_ADAPTER_DISABLED;
                result = _bt_check_adapter(&enabled);
@@ -3781,6 +3799,7 @@ gboolean __bt_service_check_privilege(int function_name,
        case BT_DISABLE_ADAPTER:
        case BT_RESET_ADAPTER:
        case BT_RECOVER_ADAPTER:
+       case BT_BATTERY_READ_DATA:
        case BT_ENABLE_ADAPTER_LE:
        case BT_DISABLE_ADAPTER_LE:
        case BT_SET_CONNECTABLE:
diff --git a/bt-service-adaptation/services/bt-service-battery-monitor.c b/bt-service-adaptation/services/bt-service-battery-monitor.c
new file mode 100644 (file)
index 0000000..fe7d7c3
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * Bluetooth-frwk
+ *
+ * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:  Sudipto Bal <sudipto.bal@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *              http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <glib.h>
+#include <string.h>
+#include <dlog.h>
+#include <time.h>
+
+#include <oal-event.h>
+
+#include "bt-service-battery-monitor.h"
+#include "bt-service-common.h"
+
+static time_t scan_start = 0;
+static time_t connect_start = 0;
+static int scan_cnt = 0;
+static int connect_cnt = 0;
+static gboolean is_session_started = FALSE;
+
+typedef struct {
+        int type;
+        void *data;
+} bt_service_oal_event_data_t;
+
+_bt_battery_data_t *current_session_data = NULL;
+
+static void __bt_display_session_data()
+{
+       BT_DBG("Displaying session data...");
+       BT_DBG("session_start_time = %ld", current_session_data->session_start_time);
+       BT_DBG("session_end_time = %ld", current_session_data->session_end_time);
+       BT_DBG("session_scan_time = %d", current_session_data->session_scan_time);
+       BT_DBG("session_connected_time = %d", current_session_data->session_connected_time);
+}
+
+/*After reading data, the function resets it*/
+int _bt_bm_read_data(_bt_battery_data_t *data)
+{
+       BT_DBG("");
+
+       if (data == NULL) {
+               BT_ERR("Received NULL pointer in argument, returning...");
+               return BLUETOOTH_ERROR_NO_DATA;
+       }
+
+       data->session_start_time = current_session_data->session_start_time;
+       data->session_end_time = time(NULL);
+       current_session_data->session_start_time = time(NULL);
+       current_session_data->session_end_time = 0;
+
+       data->session_scan_time = current_session_data->session_scan_time;
+       if (scan_cnt) {
+               data->session_scan_time += (uint16_t) (time(NULL) - scan_start);
+               scan_start = time(NULL);
+       }
+
+       data->session_connected_time = current_session_data->session_connected_time;
+       if (connect_cnt) {
+               data->session_connected_time += (uint16_t) (time(NULL) - connect_start);
+                connect_start = time(NULL);
+        }
+       return BLUETOOTH_ERROR_NONE;
+}
+
+void _bt_start_session_time()
+{
+       if (is_session_started == FALSE) {
+               BT_DBG("Bt session starting...");
+               is_session_started = TRUE;
+               current_session_data = g_malloc0(sizeof(_bt_battery_data_t));
+               current_session_data->session_start_time = time(NULL);
+               current_session_data->session_end_time = 0;
+               current_session_data->session_connected_time = 0;
+               current_session_data->session_scan_time = 0;
+               current_session_data->atm_list = NULL;
+       } else {
+               if (current_session_data == NULL)
+                       BT_ERR("Session in progress but data structure is not initialized"); //error handling
+               else
+                       BT_DBG("Bt session already in progress... Returning");
+       }
+}
+
+void _bt_stop_session_time()
+{
+       if (is_session_started == FALSE) {
+               BT_DBG("BT session not in progress... Returning"); //error handling
+               return;
+       }
+       BT_DBG("Bt session ending...");
+       is_session_started = FALSE;
+       current_session_data->session_end_time = time(NULL);
+       __bt_display_session_data();
+}
+
+void _bt_start_scan_time()
+{
+       if (current_session_data != NULL) {
+               if (scan_cnt == 0) {
+                       BT_DBG("Starting scan time");
+                       scan_start = time(NULL);
+               }
+               scan_cnt++;
+       } else {
+               BT_ERR("Data structure uninitialized"); //error handling
+       }
+}
+
+void _bt_stop_scan_time()
+{
+       if (scan_cnt == 0 || current_session_data == NULL)
+               BT_ERR("Error encountered, returning..."); //error handling
+       else {
+               scan_cnt--;
+               if(scan_cnt == 0) {
+                       time_t temp = time(NULL);
+                       current_session_data->session_scan_time += (uint16_t) (temp - scan_start);
+               }
+       }
+}
+
+void _bt_start_connect_time()
+{
+       if (current_session_data != NULL) {
+               if (connect_cnt == 0) {
+                       BT_DBG("Starting connect time");
+                       connect_start = time(NULL);
+               }
+               connect_cnt++;
+       }
+       else {
+               BT_ERR("Data structure uninitialized"); //error handling
+       }
+}
+
+void _bt_stop_connect_time()
+{
+       if(connect_cnt == 0 || current_session_data == NULL) {
+               BT_ERR("Error encountered, returning..."); //error handling
+       }
+       else {
+               connect_cnt--;
+               if(connect_cnt == 0) {
+                       time_t temp = time(NULL);
+                       current_session_data->session_connected_time += (uint16_t) (temp - connect_start);
+               }
+       }
+}
+
+void _bt_bm_event_handler(gpointer data)
+{
+       bt_service_oal_event_data_t *oal_event = data;
+       int event_type = oal_event->type;
+
+       switch(event_type) {
+       case OAL_EVENT_ADAPTER_ENABLED:
+               BT_DBG("Handling Adapter Enabled");
+               _bt_start_session_time();
+               break;
+       case OAL_EVENT_ADAPTER_DISABLED:
+               BT_DBG("Handling Adapter Disabled");
+               _bt_stop_session_time();
+               break;
+       case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
+       case OAL_EVENT_BLE_DISCOVERY_STARTED:
+               BT_DBG("Handling Adapter Discovery Start");
+               _bt_start_scan_time();
+               break;
+       case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
+       case OAL_EVENT_BLE_DISCOVERY_STOPPED:
+               BT_DBG("Handling Adapter Discovery Stop");
+               _bt_stop_scan_time();
+               break;
+       case OAL_EVENT_SOCKET_OUTGOING_CONNECTED:
+       case OAL_EVENT_GATTC_CONNECTION_COMPLETED:
+               BT_DBG("Handling Connection Start");
+               _bt_start_connect_time();
+               break;
+       case OAL_EVENT_SOCKET_DISCONNECTED:
+       case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED:
+               BT_DBG("Handling Connection Stop");
+               _bt_stop_connect_time();
+               break;
+       default:
+               BT_DBG("The event is not currently being handled");
+       }
+}
index b498b47..8b82971 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "bt-service-common.h"
 #include "bt-service-event-receiver.h"
+#include "bt-service-battery-monitor.h"
 
 typedef struct {
        int type;
@@ -163,6 +164,7 @@ static gboolean __bt_handle_oal_events(gpointer data)
        bt_service_oal_event_data_t *oal_event = data;
        int event_type = oal_event->type;
        gpointer event_data = oal_event->data;
+       _bt_bm_event_handler(data);
 
        switch (event_type) {
        case OAL_EVENT_ADAPTER_ENABLED:
diff --git a/bt-service-adaptation/services/include/bt-service-battery-monitor.h b/bt-service-adaptation/services/include/bt-service-battery-monitor.h
new file mode 100644 (file)
index 0000000..816da28
--- /dev/null
@@ -0,0 +1,52 @@
+ /*Bluetooth-frwk
+ *
+ * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:  Sudipto Bal <sudipto.bal@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *              http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef _BT_SERVICE_BATTERY_MONITOR_H_
+#define _BT_SERVICE_BATTERY_MONITOR_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <oal-event.h>
+
+typedef struct {
+       char* app_name;
+       uint16_t rx_bytes;
+       uint16_t tx_bytes;
+} _bt_battery_app_data_t;
+
+typedef struct {
+        time_t session_start_time;
+        time_t session_end_time;
+        uint16_t session_connected_time;
+        uint16_t session_scan_time;
+        GSList *atm_list;
+} _bt_battery_data_t;
+
+void _bt_bm_event_handler(gpointer data);
+
+int _bt_bm_read_data(_bt_battery_data_t *data);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* _BT_SERVICE_BATTERY_MONITOR_H_ */
index f7cca5f..f523ca6 100644 (file)
@@ -22,6 +22,8 @@
 #include <stdbool.h>
 #include <unistd.h>
 #include <glib.h>
+#include <stdint.h>
+
 
 #ifdef __cplusplus
 extern "C" {
@@ -319,6 +321,18 @@ typedef struct {
 } bluetooth_device_pin_code_t;
 
 /**
+ * This is data for battery usage monitoring
+ */
+
+typedef struct {
+        time_t session_start_time;
+        time_t session_end_time;
+        uint16_t session_connected_time;
+        uint16_t session_scan_time;
+        GSList *atm_list;
+} bt_battery_data;
+
+/**
  * Adapter state
  */
 typedef enum {
@@ -2372,6 +2386,7 @@ int ret = 0;
 ret = bluetooth_disable_adapter();
 @endcode
  */
+int bluetooth_read_battery_data(bt_battery_data *latest);
 int bluetooth_disable_adapter(void);
 int bluetooth_recover_adapter(void);
 int bluetooth_check_adapter_le(void);
index b3a0f03..c8db5e8 100644 (file)
@@ -153,6 +153,7 @@ typedef enum {
        BT_ENABLE_ADAPTER,
        BT_DISABLE_ADAPTER,
        BT_RECOVER_ADAPTER,
+       BT_BATTERY_READ_DATA,
        BT_SET_DISCOVERABLE_TIME,
        BT_GET_DISCOVERABLE_TIME,
        BT_IGNORE_AUTO_PAIRING,