1/******************************************************************************
2 *
3 *  Copyright (C) 2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 *  Filename:      bt_utils.c
22 *
23 *  Description:   Miscellaneous helper functions
24 *
25 *
26 ***********************************************************************************/
27
28#include <cutils/properties.h>
29#include <cutils/sched_policy.h>
30#include <errno.h>
31#include <pthread.h>
32#include <sys/resource.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <utils/ThreadDefs.h>
37
38#define LOG_TAG "BT_UTILS"
39
40#include <utils/Log.h>
41
42#include "data_types.h"
43#include "bt_utils.h"
44
45
46/*******************************************************************************
47**  Type definitions for callback functions
48********************************************************************************/
49static pthread_once_t g_DoSchedulingGroupOnce[TASK_HIGH_MAX];
50static BOOLEAN g_DoSchedulingGroup[TASK_HIGH_MAX];
51static pthread_mutex_t         gIdxLock;
52static int g_TaskIdx;
53
54/*****************************************************************************
55**
56** Function        bt_utils_init
57**
58** Description     Initialize bluedroid util
59**
60** Returns         void
61**
62*******************************************************************************/
63void bt_utils_init() {
64    int i;
65    pthread_mutexattr_t lock_attr;
66
67    for(i = 0; i < TASK_HIGH_MAX; i++) {
68        g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
69        g_DoSchedulingGroup[i] = TRUE;
70    }
71    pthread_mutexattr_init(&lock_attr);
72    pthread_mutex_init(&gIdxLock, &lock_attr);
73}
74
75/*****************************************************************************
76**
77** Function        bt_utils_cleanup
78**
79** Description     Clean up bluedroid util
80**
81** Returns         void
82**
83*******************************************************************************/
84void bt_utils_cleanup() {
85    pthread_mutex_destroy(&gIdxLock);
86}
87
88/*****************************************************************************
89**
90** Function        check_do_scheduling_group
91**
92** Description     check if it is ok to change schedule group
93**
94** Returns         void
95**
96*******************************************************************************/
97static void check_do_scheduling_group(void) {
98    char buf[PROPERTY_VALUE_MAX];
99    int len = property_get("debug.sys.noschedgroups", buf, "");
100    if (len > 0) {
101        int temp;
102        if (sscanf(buf, "%d", &temp) == 1) {
103            g_DoSchedulingGroup[g_TaskIdx] = temp == 0;
104        }
105    }
106}
107
108/*****************************************************************************
109**
110** Function        raise_priority_a2dp
111**
112** Description     Raise task priority for A2DP streaming
113**
114** Returns         void
115**
116*******************************************************************************/
117void raise_priority_a2dp(tHIGH_PRIORITY_TASK high_task) {
118    int rc = 0;
119    int tid = gettid();
120
121    pthread_mutex_lock(&gIdxLock);
122    g_TaskIdx = high_task;
123
124    pthread_once(&g_DoSchedulingGroupOnce[g_TaskIdx], check_do_scheduling_group);
125    if (g_DoSchedulingGroup[g_TaskIdx]) {
126        // set_sched_policy does not support tid == 0
127        rc = set_sched_policy(tid, SP_FOREGROUND);
128    }
129    pthread_mutex_unlock(&gIdxLock);
130
131    if (rc) {
132        ALOGW("failed to change sched policy, tid %d, err: %d", tid, errno);
133    }
134
135    if (setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_AUDIO) < 0) {
136        ALOGW("failed to change priority tid: %d to %d", tid, ANDROID_PRIORITY_AUDIO);
137    }
138}
139
140