15738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/******************************************************************************
25738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
35738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Copyright (C) 2012 Broadcom Corporation
45738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
55738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Licensed under the Apache License, Version 2.0 (the "License");
65738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  you may not use this file except in compliance with the License.
75738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  You may obtain a copy of the License at:
85738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
95738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  http://www.apache.org/licenses/LICENSE-2.0
105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  See the License for the specific language governing permissions and
155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  limitations under the License.
165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project ******************************************************************************/
185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/************************************************************************************
205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Filename:      bt_utils.c
225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Description:   Miscellaneous helper functions
245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project ***********************************************************************************/
275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <cutils/properties.h>
295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <cutils/sched_policy.h>
305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <errno.h>
315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <pthread.h>
325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <sys/resource.h>
335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <stdio.h>
345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <stdlib.h>
355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <unistd.h>
365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <utils/ThreadDefs.h>
375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#define LOG_TAG "BT_UTILS"
395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <utils/Log.h>
415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "data_types.h"
435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "bt_utils.h"
445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**  Type definitions for callback functions
485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project********************************************************************************/
495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectstatic pthread_once_t g_DoSchedulingGroupOnce[TASK_HIGH_MAX];
505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectstatic BOOLEAN g_DoSchedulingGroup[TASK_HIGH_MAX];
515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectstatic pthread_mutex_t         gIdxLock;
525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectstatic int g_TaskIdx;
535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function        bt_utils_init
575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description     Initialize bluedroid util
595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns         void
615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectvoid bt_utils_init() {
645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    int i;
655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    pthread_mutexattr_t lock_attr;
665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    for(i = 0; i < TASK_HIGH_MAX; i++) {
685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        g_DoSchedulingGroup[i] = TRUE;
705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    pthread_mutexattr_init(&lock_attr);
725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    pthread_mutex_init(&gIdxLock, &lock_attr);
735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function        bt_utils_cleanup
785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description     Clean up bluedroid util
805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns         void
825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectvoid bt_utils_cleanup() {
855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    pthread_mutex_destroy(&gIdxLock);
865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function        check_do_scheduling_group
915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description     check if it is ok to change schedule group
935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns         void
955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectstatic void check_do_scheduling_group(void) {
985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    char buf[PROPERTY_VALUE_MAX];
995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    int len = property_get("debug.sys.noschedgroups", buf, "");
1005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (len > 0) {
1015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        int temp;
1025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        if (sscanf(buf, "%d", &temp) == 1) {
1035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            g_DoSchedulingGroup[g_TaskIdx] = temp == 0;
1045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        }
1055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*****************************************************************************
1095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Function        raise_priority_a2dp
1115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Description     Raise task priority for A2DP streaming
1135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project** Returns         void
1155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project**
1165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project*******************************************************************************/
1175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectvoid raise_priority_a2dp(tHIGH_PRIORITY_TASK high_task) {
1185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    int rc = 0;
1195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    int tid = gettid();
1205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    pthread_mutex_lock(&gIdxLock);
1225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    g_TaskIdx = high_task;
1235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    pthread_once(&g_DoSchedulingGroupOnce[g_TaskIdx], check_do_scheduling_group);
1255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (g_DoSchedulingGroup[g_TaskIdx]) {
1265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // set_sched_policy does not support tid == 0
1275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        rc = set_sched_policy(tid, SP_FOREGROUND);
1285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    pthread_mutex_unlock(&gIdxLock);
1305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (rc) {
1325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        ALOGW("failed to change sched policy, tid %d, err: %d", tid, errno);
1335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_AUDIO) < 0) {
1365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        ALOGW("failed to change priority tid: %d to %d", tid, ANDROID_PRIORITY_AUDIO);
1375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
140