2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
4 * Contact: Sudipto Bal <sudipto.bal@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <pkgmgr-info.h>
25 #include <bluetooth.h>
26 #include <bluetooth_internal.h>
28 #include "bm_bt_plugin.h"
29 #include "bm_bt_util.h"
33 * To get AppID from PID - Needs to be handled by the respective framework owners:
34 * The PID might become outdated by the time data is read, which will create problems in mapping the application.
35 * Sample API's and fucntion is provided below to get app-id based on available pid.
36 * char *strAppId = NULL;
37 * pid_t nProcessId = getpid();
38 * app_manager_get_app_id(nProcessId, &strAppId);
40 static char* get_current_appid(int pid, uid_t uid)
43 _I("getting caller appid with pid=[%d], uid=[%d]", pid, uid);
46 char appid[128] = { 0, };
47 char *appid_ret = NULL;
49 ret = aul_app_get_appid_bypid_for_uid(pid, appid, sizeof(appid), uid);
52 _E("fail to get current appid ret=[%d], appid=%s\n", ret, appid);
54 appid_ret = strdup(appid);
55 if (appid_ret == NULL)
56 _E("Memory Allocation Failed");
66 /* Initialize the necessary paramters and callbacks if required */
68 bt_adapter_init_battery_monitor(NULL, NULL);
70 return BM_PLUGIN_ERROR_NONE;
77 /* De-initialize callbacks & parameters */
80 return BM_PLUGIN_ERROR_NONE;
83 static void free_app_list(GSList *app_list)
86 app_time_map_st2 *app_info;
87 for (l = app_list; l != NULL; l = g_slist_next(l)){
88 app_info = (app_time_map_st2 *)(l->data);
90 free(app_info->app_id);
92 g_slist_free(app_list);
95 int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
99 bm_bluetooth_st *bluetooth_record_head = NULL;
100 bm_bluetooth_st *bluetooth_record = NULL;
101 app_time_map_st2 *app_record = NULL;
103 bluetooth_record = (bm_bluetooth_st *)calloc(1, sizeof(bm_bluetooth_st));
104 if (bluetooth_record == NULL) {
106 return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
109 /* Fill bluetooth record with the values of the bm_bluetooth_st */
110 bt_battery_info_s battery_data;
111 memset(&battery_data, 0, sizeof(battery_data));
112 int ret = bt_adapter_read_battery_info(&battery_data);
116 _E("battery info not read");
117 free(bluetooth_record);
118 return BM_PLUGIN_ERROR_NO_DATA;
121 bluetooth_record->startTime = battery_data.session_start_time;
122 bluetooth_record->stopTime = battery_data.session_end_time;
123 bluetooth_record->scanTime = battery_data.session_scan_time;
124 bluetooth_record->connectedTime = battery_data.session_connected_time;
126 _I("BT usage data for battery monitor");
127 _I("startTime: %ld", bluetooth_record->startTime);
128 _I("stopTime: %ld", bluetooth_record->stopTime);
129 _I("scanTime: %u", bluetooth_record->scanTime);
130 _I("connectedTime: %u", bluetooth_record->connectedTime);
132 /* Getting app data */
133 GSList *app_list = NULL, *l = NULL;
134 app_list = bluetooth_record->atm_list;
135 bt_battery_app_info_s *app_info = NULL;
137 for (l = battery_data.atm_list; l != NULL; l = g_slist_next(l)) {
139 app_record = (app_time_map_st2 *)calloc(1, sizeof(app_time_map_st2));
140 if (app_record == NULL) {
142 free_app_list(bluetooth_record->atm_list);
143 free(bluetooth_record);
144 return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
147 app_info = (bt_battery_app_info_s *)(l->data);
148 if (app_info == NULL) {
149 _E("app_info is NULL");
150 free_app_list(bluetooth_record->atm_list);
151 free(bluetooth_record);
152 return BM_PLUGIN_ERROR_NO_DATA;
155 /* populate application record here with usage values */
156 app_record->app_id = get_current_appid(app_info->pid, app_info->uid);
157 app_record->time = app_info->time;
158 app_record->rx = app_info->rx_bytes;
159 app_record->tx = app_info->tx_bytes;
161 /* Fill the app_record with the values of the app_time_map_st2 */
162 app_list = g_slist_append(app_list, app_record);
165 /*Append BT data to head node as per design doc*/
166 bluetooth_record_head = (bm_bluetooth_st *)calloc(1, sizeof(bm_bluetooth_st));
167 if (bluetooth_record_head == NULL) {
169 free_app_list(bluetooth_record->atm_list);
170 free(bluetooth_record);
171 return BM_PLUGIN_ERROR_OUT_OF_MEMORY;
173 bluetooth_record_head->bt_data_list = g_slist_append(
174 bluetooth_record_head->bt_data_list, bluetooth_record);
175 *handle = bluetooth_record_head;
178 return BM_PLUGIN_ERROR_NONE;
181 static bm_api_st bluetooth_api = {
184 .get_feature_data = get_feature_data
187 int plugin_init(bm_api_st **api)
191 *api = &bluetooth_api;
197 int plugin_deinit(void)
205 BM_PLUGIN_ADD(BM_PLUGIN_ID_BLE, NAME, AUTHOR, VERSION, plugin_init, plugin_deinit);