summaryrefslogtreecommitdiffstats
path: root/hci/src/lpm.c
blob: fb6f8378e09738daa971e5c5fc0a70c4dd3337a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/******************************************************************************
 *
 *  Copyright (C) 2009-2012 Broadcom Corporation
 *
 *  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.
 *
 ******************************************************************************/

/******************************************************************************
 *
 *  Filename:      lpm.c
 *
 *  Description:   Contains low power mode implementation
 *
 ******************************************************************************/

#define LOG_TAG "bt_lpm"

#include <utils/Log.h>
#include <signal.h>
#include <time.h>
#include "bt_hci_bdroid.h"
#include "bt_vendor_lib.h"

/******************************************************************************
**  Constants & Macros
******************************************************************************/

#ifndef BTLPM_DBG
#define BTLPM_DBG FALSE
#endif

#if (BTLPM_DBG == TRUE)
#define BTLPMDBG(param, ...) {ALOGD(param, ## __VA_ARGS__);}
#else
#define BTLPMDBG(param, ...) {}
#endif

#ifndef DEFAULT_LPM_IDLE_TIMEOUT
#define DEFAULT_LPM_IDLE_TIMEOUT    3000
#endif

/******************************************************************************
**  Externs
******************************************************************************/

extern bt_vendor_interface_t *bt_vnd_if;

/******************************************************************************
**  Local type definitions
******************************************************************************/

/* Low power mode state */
enum {
    LPM_DISABLED = 0,                    /* initial state */
    LPM_ENABLED,
    LPM_ENABLING,
    LPM_DISABLING
};

/* LPM WAKE state */
enum {
    LPM_WAKE_DEASSERTED = 0,              /* initial state */
    LPM_WAKE_W4_TX_DONE,
    LPM_WAKE_W4_TIMEOUT,
    LPM_WAKE_ASSERTED
};

/* low power mode control block */
typedef struct
{
    uint8_t state;                          /* Low power mode state */
    uint8_t wake_state;                     /* LPM WAKE state */
    uint8_t no_tx_data;
    uint8_t timer_created;
    timer_t timer_id;
    uint32_t timeout_ms;
} bt_lpm_cb_t;


/******************************************************************************
**  Static variables
******************************************************************************/

static bt_lpm_cb_t bt_lpm_cb;

/******************************************************************************
**   LPM Static Functions
******************************************************************************/

/*******************************************************************************
**
** Function        lpm_idle_timeout
**
** Description     Timeout thread of transport idle timer
**
** Returns         None
**
*******************************************************************************/
static void lpm_idle_timeout(union sigval arg)
{
    BTLPMDBG("..lpm_idle_timeout..");

    if ((bt_lpm_cb.state == LPM_ENABLED) && \
        (bt_lpm_cb.wake_state == LPM_WAKE_W4_TIMEOUT))
    {
        bthc_signal_event(HC_EVENT_LPM_IDLE_TIMEOUT);
    }
}

/*******************************************************************************
**
** Function        lpm_start_transport_idle_timer
**
** Description     Launch transport idle timer
**
** Returns         None
**
*******************************************************************************/
static void lpm_start_transport_idle_timer(void)
{
    int status;
    struct itimerspec ts;
    struct sigevent se;

    if (bt_lpm_cb.state != LPM_ENABLED)
        return;

    if (bt_lpm_cb.timer_created == FALSE)
    {
        se.sigev_notify = SIGEV_THREAD;
        se.sigev_value.sival_ptr = &bt_lpm_cb.timer_id;
        se.sigev_notify_function = lpm_idle_timeout;
        se.sigev_notify_attributes = NULL;

        status = timer_create(CLOCK_MONOTONIC, &se, &bt_lpm_cb.timer_id);

        if (status == 0)
            bt_lpm_cb.timer_created = TRUE;
    }

    if (bt_lpm_cb.timer_created == TRUE)
    {
        ts.it_value.tv_sec = bt_lpm_cb.timeout_ms/1000;
        ts.it_value.tv_nsec = 1000*(bt_lpm_cb.timeout_ms%1000);
        ts.it_interval.tv_sec = 0;
        ts.it_interval.tv_nsec = 0;

        status = timer_settime(bt_lpm_cb.timer_id, 0, &ts, 0);
        if (status == -1)
            ALOGE("[START] Failed to set LPM idle timeout");
    }
}

/*******************************************************************************
**
** Function        lpm_stop_transport_idle_timer
**
** Description     Launch transport idle timer
**
** Returns         None
**
*******************************************************************************/
static void lpm_stop_transport_idle_timer(void)
{
    int status;
    struct itimerspec ts;

    if (bt_lpm_cb.timer_created == TRUE)
    {
        ts.it_value.tv_sec = 0;
        ts.it_value.tv_nsec = 0;
        ts.it_interval.tv_sec = 0;
        ts.it_interval.tv_nsec = 0;

        status = timer_settime(bt_lpm_cb.timer_id, 0, &ts, 0);
        if (status == -1)
            ALOGE("[STOP] Failed to set LPM idle timeout");
    }
}

/*******************************************************************************
**
** Function         lpm_vnd_cback
**
** Description      Callback of vendor specific result for lpm enable/disable
**                  rquest
**
** Returns          None
**
*******************************************************************************/
void lpm_vnd_cback(uint8_t vnd_result)
{
    if (vnd_result == 0)
    {
        /* Status == Success */
        bt_lpm_cb.state = (bt_lpm_cb.state == LPM_ENABLING) ? \
                          LPM_ENABLED : LPM_DISABLED;
    }
    else
    {
        bt_lpm_cb.state = (bt_lpm_cb.state == LPM_ENABLING) ? \
                          LPM_DISABLED : LPM_ENABLED;
    }

    if (bt_hc_cbacks)
    {
        if (bt_lpm_cb.state == LPM_ENABLED)
            bt_hc_cbacks->lpm_cb(BT_HC_LPM_ENABLED);
        else
            bt_hc_cbacks->lpm_cb(BT_HC_LPM_DISABLED);
    }

    if (bt_lpm_cb.state == LPM_DISABLED)
    {
        if (bt_lpm_cb.timer_created == TRUE)
        {
            timer_delete(bt_lpm_cb.timer_id);
        }

        memset(&bt_lpm_cb, 0, sizeof(bt_lpm_cb_t));
    }
}


/*****************************************************************************
**   Low Power Mode Interface Functions
*****************************************************************************/

/*******************************************************************************
**
** Function        lpm_init
**
** Description     Init LPM
**
** Returns         None
**
*******************************************************************************/
void lpm_init(void)
{
    memset(&bt_lpm_cb, 0, sizeof(bt_lpm_cb_t));

    /* Calling vendor-specific part */
    if (bt_vnd_if)
        bt_vnd_if->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &(bt_lpm_cb.timeout_ms));
    else
        bt_lpm_cb.timeout_ms = DEFAULT_LPM_IDLE_TIMEOUT;
}

/*******************************************************************************
**
** Function        lpm_cleanup
**
** Description     Clean up
**
** Returns         None
**
*******************************************************************************/
void lpm_cleanup(void)
{
    if (bt_lpm_cb.timer_created == TRUE)
    {
        timer_delete(bt_lpm_cb.timer_id);
    }
}

/*******************************************************************************
**
** Function        lpm_enable
**
** Description     Enalbe/Disable LPM
**
** Returns         None
**
*******************************************************************************/
void lpm_enable(uint8_t turn_on)
{
    if ((bt_lpm_cb.state!=LPM_DISABLED) && (bt_lpm_cb.state!=LPM_ENABLED))
    {
        ALOGW("Still busy on processing prior LPM enable/disable request...");
        return;
    }

    if ((turn_on == TRUE) && (bt_lpm_cb.state == LPM_ENABLED))
    {
        ALOGI("LPM is already on!!!");
        if (bt_hc_cbacks)
            bt_hc_cbacks->lpm_cb(BT_HC_LPM_ENABLED);
    }
    else if ((turn_on == FALSE) && (bt_lpm_cb.state == LPM_DISABLED))
    {
        ALOGI("LPM is already off!!!");
        if (bt_hc_cbacks)
            bt_hc_cbacks->lpm_cb(BT_HC_LPM_DISABLED);
    }

    /* Calling vendor-specific part */
    if (bt_vnd_if)
    {
        uint8_t lpm_cmd = (turn_on) ? BT_VND_LPM_ENABLE : BT_VND_LPM_DISABLE;
        bt_lpm_cb.state = (turn_on) ? LPM_ENABLING : LPM_DISABLING;
        bt_vnd_if->op(BT_VND_OP_LPM_SET_MODE, &lpm_cmd);
    }
}

/*******************************************************************************
**
** Function          lpm_tx_done
**
** Description       This function is to inform the lpm module
**                   if data is waiting in the Tx Q or not.
**
**                   IsTxDone: TRUE if All data in the Tx Q are gone
**                             FALSE if any data is still in the Tx Q.
**                   Typicaly this function must be called
**                   before USERIAL Write and in the Tx Done routine
**
** Returns           None
**
*******************************************************************************/
void lpm_tx_done(uint8_t is_tx_done)
{
    bt_lpm_cb.no_tx_data = is_tx_done;

    if ((bt_lpm_cb.wake_state==LPM_WAKE_W4_TX_DONE) && (is_tx_done==TRUE))
    {
        bt_lpm_cb.wake_state = LPM_WAKE_W4_TIMEOUT;
        lpm_start_transport_idle_timer();
    }
}

/*******************************************************************************
**
** Function        lpm_wake_assert
**
** Description     Called to wake up Bluetooth chip.
**                 Normally this is called when there is data to be sent
**                 over UART.
**
** Returns         TRUE/FALSE
**
*******************************************************************************/
void lpm_wake_assert(void)
{
    if (bt_lpm_cb.state != LPM_DISABLED)
    {
        BTLPMDBG("LPM WAKE assert");

        /* Calling vendor-specific part */
        if (bt_vnd_if)
        {
            uint8_t state = BT_VND_LPM_WAKE_ASSERT;
            bt_vnd_if->op(BT_VND_OP_LPM_WAKE_SET_STATE, &state);
        }

        lpm_stop_transport_idle_timer();

        bt_lpm_cb.wake_state = LPM_WAKE_ASSERTED;
    }

    lpm_tx_done(FALSE);
}

/*******************************************************************************
**
** Function        lpm_allow_bt_device_sleep
**
** Description     Start LPM idle timer if allowed
**
** Returns         None
**
*******************************************************************************/
void lpm_allow_bt_device_sleep(void)
{
    if ((bt_lpm_cb.state == LPM_ENABLED) && \
        (bt_lpm_cb.wake_state == LPM_WAKE_ASSERTED))
    {
        if(bt_lpm_cb.no_tx_data == TRUE)
        {
            bt_lpm_cb.wake_state = LPM_WAKE_W4_TIMEOUT;
            lpm_start_transport_idle_timer();
        }
        else
        {
            bt_lpm_cb.wake_state = LPM_WAKE_W4_TX_DONE;
        }
    }
}

/*******************************************************************************
**
** Function         lpm_wake_deassert
**
** Description      Deassert wake if allowed
**
** Returns          None
**
*******************************************************************************/
void lpm_wake_deassert(void)
{
    if ((bt_lpm_cb.state == LPM_ENABLED) && (bt_lpm_cb.no_tx_data == TRUE))
    {
        BTLPMDBG("LPM WAKE deassert");

        /* Calling vendor-specific part */
        if (bt_vnd_if)
        {
            uint8_t state = BT_VND_LPM_WAKE_DEASSERT;
            bt_vnd_if->op(BT_VND_OP_LPM_WAKE_SET_STATE, &state);
        }

        bt_lpm_cb.wake_state = LPM_WAKE_DEASSERTED;
    }
}