+
+ if (current_session_data != NULL) {
+ g_slist_free_full(current_session_data->atm_list, g_free);
+ g_free(current_session_data);
+ current_session_data = NULL;
+ }
+}
+
+/* 1 app can operate the regacy and ble scan at the same time */
+static GSList* __is_scan_app_present(GSList *start, bt_bm_scan_type_e type, uid_t uid, pid_t pid)
+{
+ GSList *l = NULL;
+ bt_bm_scan_info_t *t;
+
+ for (l = start; l != NULL; l = g_slist_next(l)) {
+ t = (bt_bm_scan_info_t *)(l->data);
+
+ /* Find the regacy scan app for Inquiry stop */
+ if (type == SCAN_REGACY && t->type != SCAN_LE) {
+ BT_INFO("app already exist");
+ return l;
+ }
+
+ if (t->uid == uid && t->pid == pid) {
+ BT_INFO("app already exist");
+ return l;
+ }
+ }
+ return NULL;
+}
+
+static void __bt_bm_add_prev_time(uint32_t scan_time)
+{
+ GSList *l = NULL;
+ bt_bm_scan_info_t *t;
+
+ for (l = scan_app_list; l != NULL; l = g_slist_next(l)) {
+ t = (bt_bm_scan_info_t *)(l->data);
+ _bt_bm_add_transaction_details(t->uid, t->pid, scan_time, TIME_DATA);
+ }
+}
+
+/* 1 regacy scan is only allowed in the platform
+ * BLE scan is allowed for many apps
+*/
+/* When a app is added, we should add and reset the time. */
+void _bt_bm_add_scan_app(bt_bm_scan_type_e type, uid_t uid, pid_t pid)
+{
+ bt_bm_scan_info_t *scan_info = NULL;
+ GSList *app_list = NULL;
+ int app_cnt = 0;
+ uint32_t scan_time_per_app = 0;
+
+ BT_DBG("Scan type: %d", type);
+
+ if (scan_app_list) {
+ app_cnt = g_slist_length(scan_app_list);
+ app_list = __is_scan_app_present(scan_app_list, SCAN_BOTH, uid, pid);
+ }
+
+ if (app_list) {
+ /* app try to scan both Regacy and LE */
+ scan_info = (bt_bm_scan_info_t *)(app_list->data);
+
+ if (scan_info == NULL) {
+ BT_ERR("Can't get the scan info");
+ return;
+ }
+
+ BT_DBG("Previous type: %d", scan_info->type);
+
+ if (scan_info->type == type) {
+ BT_ERR("Same scan type is doing");
+ return;
+ }
+
+ scan_info->type = SCAN_BOTH;
+ } else {
+ scan_info = g_malloc0(sizeof(bt_bm_scan_info_t));
+ scan_info->uid = uid;
+ scan_info->pid = pid;
+ scan_info->type = type;
+
+ if (app_cnt > 0) {
+ struct timeval cur_time;
+
+ gettimeofday(&cur_time, 0);
+
+ scan_time_per_app = (uint32_t)(__bt_dm_time_diff_msec(app_scan_base, cur_time)) / app_cnt;
+ __bt_bm_add_prev_time(scan_time_per_app);
+
+ /* Update the base time */
+ gettimeofday(&app_scan_base, 0);
+ }
+
+ scan_app_list = g_slist_append(scan_app_list, scan_info);
+ }
+}
+
+/* When a app is removed, we should add and reset the time. */
+void _bt_bm_remove_scan_app(bt_bm_scan_type_e type, uid_t uid, pid_t pid)
+{
+ bt_bm_scan_info_t *scan_info = NULL;
+ GSList *app_list = NULL;
+ int app_cnt = 0;
+ uint32_t scan_time_per_app = 0;
+
+ BT_DBG("Scan type: %d", type);
+
+ if (scan_app_list == NULL) {
+ BT_ERR("No scan app in list");
+ return;
+ }
+
+ app_cnt = g_slist_length(scan_app_list);
+
+ if (app_cnt == 0) {
+ BT_ERR("No scan app in list");
+ return;
+ }
+
+ app_list = __is_scan_app_present(scan_app_list, type, uid, pid);
+
+ if (app_list) {
+ struct timeval cur_time;
+
+ scan_info = (bt_bm_scan_info_t *)(app_list->data);
+
+ if (scan_info->type == SCAN_BOTH) {
+ scan_info->type = (scan_info->type == SCAN_REGACY) ? SCAN_LE : SCAN_REGACY;
+ return;
+ }
+
+ gettimeofday(&cur_time, 0);
+
+ scan_time_per_app = (uint32_t)(__bt_dm_time_diff_msec(app_scan_base, cur_time)) / app_cnt;
+ __bt_bm_add_prev_time(scan_time_per_app);
+
+ /* Update the base time */
+ gettimeofday(&app_scan_base, 0);
+
+ scan_app_list = g_slist_remove(scan_app_list, scan_info);
+
+ g_free(scan_info);
+ }