blob: 02e3a6a926bc027971b1de72eaf945d334a064c0 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2000-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 * This file contains functions that manages ACL link modes.
22 * This includes operations such as active, hold,
23 * park and sniff modes.
24 *
25 * This module contains both internal and external (API)
26 * functions. External (API) functions are distinguishable
27 * by their names beginning with uppercase BTM.
28 *
29 *****************************************************************************/
30
31#include <stdlib.h>
32#include <string.h>
33#include <stdio.h>
34#include <stddef.h>
35#include "bt_types.h"
36#include "gki.h"
37#include "hcimsgs.h"
38#include "btu.h"
39#include "btm_api.h"
40#include "btm_int.h"
41#include "l2c_int.h"
42#include "hcidefs.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080043#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080044
45#if BTM_PWR_MGR_INCLUDED == TRUE
46
47/* This compile option is only useful when the FW has a bug
48 * it automatically uses single slot when entering SNIFF mode, but does not restore the setting
49 * This issue was found when A2DP link goes into sniff and existing sniff still has choppy audio.
50 * If this issue is seen, look for FW solution first.
51 * This work around code will be removed later. */
52#ifndef BTM_PM_SNIFF_SLOT_WORK_AROUND
53#define BTM_PM_SNIFF_SLOT_WORK_AROUND FALSE
54#endif
55
56/*****************************************************************************/
57/* to handle different modes */
58/*****************************************************************************/
59#define BTM_PM_STORED_MASK 0x80 /* set this mask if the command is stored */
60#define BTM_PM_NUM_SET_MODES 3 /* only hold, sniff & park */
61
62/* Usage: (ptr_features[ offset ] & mask )?TRUE:FALSE */
63/* offset to supported feature */
64const UINT8 btm_pm_mode_off[BTM_PM_NUM_SET_MODES] = {0, 0, 1};
65/* mask to supported feature */
66const UINT8 btm_pm_mode_msk[BTM_PM_NUM_SET_MODES] = {0x40, 0x80, 0x01};
67
68#define BTM_PM_GET_MD1 1
69#define BTM_PM_GET_MD2 2
70#define BTM_PM_GET_COMP 3
71
72const UINT8 btm_pm_md_comp_matrix[BTM_PM_NUM_SET_MODES*BTM_PM_NUM_SET_MODES] =
73{
74 BTM_PM_GET_COMP,
75 BTM_PM_GET_MD2,
76 BTM_PM_GET_MD2,
77
78 BTM_PM_GET_MD1,
79 BTM_PM_GET_COMP,
80 BTM_PM_GET_MD1,
81
82 BTM_PM_GET_MD1,
83 BTM_PM_GET_MD2,
84 BTM_PM_GET_COMP
85};
86
87/* function prototype */
88static int btm_pm_find_acl_ind(BD_ADDR remote_bda);
89static tBTM_STATUS btm_pm_snd_md_req( UINT8 pm_id, int link_ind, tBTM_PM_PWR_MD *p_mode );
90
91/*
92#ifdef BTM_PM_DEBUG
93#undef BTM_PM_DEBUG
94#define BTM_PM_DEBUG TRUE
95#endif
96*/
97
98#if BTM_PM_DEBUG == TRUE
99const char * btm_pm_state_str[] =
100{
101 "pm_active_state",
102 "pm_hold_state",
103 "pm_sniff_state",
104 "pm_park_state",
105 "pm_pend_state"
106};
107
108const char * btm_pm_event_str[] =
109{
110 "pm_set_mode_event",
111 "pm_hci_sts_event",
112 "pm_mod_chg_event",
113 "pm_update_event"
114};
115
116const char * btm_pm_action_str[] =
117{
118 "pm_set_mode_action",
119 "pm_update_db_action",
120 "pm_mod_chg_action",
121 "pm_hci_sts_action",
122 "pm_update_action"
123};
124#endif
125
126/*****************************************************************************/
127/* P U B L I C F U N C T I O N S */
128/*****************************************************************************/
129/*******************************************************************************
130**
131** Function BTM_PmRegister
132**
133** Description register or deregister with power manager
134**
135** Returns BTM_SUCCESS if successful,
136** BTM_NO_RESOURCES if no room to hold registration
137** BTM_ILLEGAL_VALUE
138**
139*******************************************************************************/
140tBTM_STATUS BTM_PmRegister (UINT8 mask, UINT8 *p_pm_id, tBTM_PM_STATUS_CBACK *p_cb)
141{
142 int xx;
143
144 /* de-register */
145 if(mask & BTM_PM_DEREG)
146 {
147 if(*p_pm_id >= BTM_MAX_PM_RECORDS)
148 return BTM_ILLEGAL_VALUE;
149 btm_cb.pm_reg_db[*p_pm_id].mask = BTM_PM_REC_NOT_USED;
150 return BTM_SUCCESS;
151 }
152
153 for(xx=0; xx<BTM_MAX_PM_RECORDS; xx++)
154 {
155 /* find an unused entry */
156 if(btm_cb.pm_reg_db[xx].mask == BTM_PM_REC_NOT_USED)
157 {
158 /* if register for notification, should provide callback routine */
159 if(mask & BTM_PM_REG_NOTIF)
160 {
161 if(p_cb == NULL)
162 return BTM_ILLEGAL_VALUE;
163 btm_cb.pm_reg_db[xx].cback = p_cb;
164 }
165 btm_cb.pm_reg_db[xx].mask = mask;
166 *p_pm_id = xx;
167 return BTM_SUCCESS;
168 }
169 }
170
171 return BTM_NO_RESOURCES;
172}
173
174/*******************************************************************************
175**
176** Function BTM_SetPowerMode
177**
178** Description store the mode in control block or
179** alter ACL connection behavior.
180**
181** Returns BTM_SUCCESS if successful,
182** BTM_UNKNOWN_ADDR if bd addr is not active or bad
183**
184*******************************************************************************/
185tBTM_STATUS BTM_SetPowerMode (UINT8 pm_id, BD_ADDR remote_bda, tBTM_PM_PWR_MD *p_mode)
186{
187 UINT8 *p_features;
188 int ind, acl_ind;
189 tBTM_PM_MCB *p_cb = NULL; /* per ACL link */
190 tBTM_PM_MODE mode;
191 int temp_pm_id;
192
193
194 if(pm_id >= BTM_MAX_PM_RECORDS)
195 pm_id = BTM_PM_SET_ONLY_ID;
196
197 if(p_mode == NULL)
198 return BTM_ILLEGAL_VALUE;
199
200 BTM_TRACE_API3( "BTM_SetPowerMode: pm_id %d BDA: %08x mode:0x%x", pm_id,
201 (remote_bda[2]<<24)+(remote_bda[3]<<16)+(remote_bda[4]<<8)+remote_bda[5], p_mode->mode);
202
203 /* take out the force bit */
204 mode = p_mode->mode & ~BTM_PM_MD_FORCE;
205
206 acl_ind = btm_pm_find_acl_ind(remote_bda);
207 if(acl_ind == MAX_L2CAP_LINKS)
208 return (BTM_UNKNOWN_ADDR);
209
210 p_cb = &(btm_cb.pm_mode_db[acl_ind]);
211
212 if(mode != BTM_PM_MD_ACTIVE)
213 {
214 /* check if the requested mode is supported */
215 ind = mode - BTM_PM_MD_HOLD; /* make it base 0 */
216 p_features = BTM_ReadLocalFeatures();
217 if( !(p_features[ btm_pm_mode_off[ind] ] & btm_pm_mode_msk[ind] ) )
218 return BTM_MODE_UNSUPPORTED;
219 }
220
221 if(mode == p_cb->state) /* the requested mode is current mode */
222 {
223 /* already in the requested mode and the current interval has less latency than the max */
224 if( (mode == BTM_PM_MD_ACTIVE) ||
225 ((p_mode->mode & BTM_PM_MD_FORCE) && (p_mode->max >= p_cb->interval) && (p_mode->min <= p_cb->interval)) ||
226 ((p_mode->mode & BTM_PM_MD_FORCE)==0 && (p_mode->max >= p_cb->interval)) )
227 {
228 BTM_TRACE_DEBUG4( "BTM_SetPowerMode: mode:0x%x interval %d max:%d, min:%d", p_mode->mode, p_cb->interval, p_mode->max, p_mode->min);
229 return BTM_SUCCESS;
230 }
231 }
232
233 temp_pm_id = pm_id;
234 if(pm_id == BTM_PM_SET_ONLY_ID)
235 temp_pm_id = BTM_MAX_PM_RECORDS;
236
237 /* update mode database */
238 if( ((pm_id != BTM_PM_SET_ONLY_ID) &&
239 (btm_cb.pm_reg_db[pm_id].mask & BTM_PM_REG_SET))
240 || ((pm_id == BTM_PM_SET_ONLY_ID) && (btm_cb.pm_pend_link != MAX_L2CAP_LINKS)) )
241 {
242#if BTM_PM_DEBUG == TRUE
243 BTM_TRACE_DEBUG2( "BTM_SetPowerMode: Saving cmd acl_ind %d temp_pm_id %d", acl_ind,temp_pm_id);
244#endif
245 /* Make sure mask is set to BTM_PM_REG_SET */
246 btm_cb.pm_reg_db[temp_pm_id].mask |= BTM_PM_REG_SET;
247 *(&p_cb->req_mode[temp_pm_id]) = *((tBTM_PM_PWR_MD *)p_mode);
248 p_cb->chg_ind = TRUE;
249 }
250
251#if BTM_PM_DEBUG == TRUE
252 BTM_TRACE_DEBUG2( "btm_pm state:0x%x, pm_pend_link: %d", p_cb->state, btm_cb.pm_pend_link);
253#endif
254 /* if mode == hold or pending, return */
255 if( (p_cb->state == BTM_PM_STS_HOLD) ||
256 (p_cb->state == BTM_PM_STS_PENDING) ||
257 (btm_cb.pm_pend_link != MAX_L2CAP_LINKS) ) /* command pending */
258 {
259 if(acl_ind != btm_cb.pm_pend_link)
260 {
261 /* set the stored mask */
262 p_cb->state |= BTM_PM_STORED_MASK;
263 BTM_TRACE_DEBUG1( "btm_pm state stored:%d",acl_ind);
264 }
265 return BTM_CMD_STORED;
266 }
267
268
269
270 return btm_pm_snd_md_req(pm_id, acl_ind, p_mode);
271}
272
273/*******************************************************************************
274**
275** Function BTM_ReadPowerMode
276**
277** Description This returns the current mode for a specific
278** ACL connection.
279**
280** Input Param remote_bda - device address of desired ACL connection
281**
282** Output Param p_mode - address where the current mode is copied into.
283** BTM_ACL_MODE_NORMAL
284** BTM_ACL_MODE_HOLD
285** BTM_ACL_MODE_SNIFF
286** BTM_ACL_MODE_PARK
287** (valid only if return code is BTM_SUCCESS)
288**
289** Returns BTM_SUCCESS if successful,
290** BTM_UNKNOWN_ADDR if bd addr is not active or bad
291**
292*******************************************************************************/
293tBTM_STATUS BTM_ReadPowerMode (BD_ADDR remote_bda, tBTM_PM_MODE *p_mode)
294{
295 int acl_ind;
296
297 if( (acl_ind = btm_pm_find_acl_ind(remote_bda)) == MAX_L2CAP_LINKS)
298 return (BTM_UNKNOWN_ADDR);
299
300 *p_mode = btm_cb.pm_mode_db[acl_ind].state;
301 return BTM_SUCCESS;
302}
303
304/*******************************************************************************
305**
306** Function BTM_SetSsrParams
307**
308** Description This sends the given SSR parameters for the given ACL
309** connection if it is in ACTIVE mode.
310**
311** Input Param remote_bda - device address of desired ACL connection
312** max_lat - maximum latency (in 0.625ms)(0-0xFFFE)
313** min_rmt_to - minimum remote timeout
314** min_loc_to - minimum local timeout
315**
316**
317** Returns BTM_SUCCESS if the HCI command is issued successful,
318** BTM_UNKNOWN_ADDR if bd addr is not active or bad
319** BTM_CMD_STORED if the command is stored
320**
321*******************************************************************************/
322tBTM_STATUS BTM_SetSsrParams (BD_ADDR remote_bda, UINT16 max_lat,
323 UINT16 min_rmt_to, UINT16 min_loc_to)
324{
325#if (BTM_SSR_INCLUDED == TRUE)
326 int acl_ind;
327 tBTM_PM_MCB *p_cb;
328
329 if( (acl_ind = btm_pm_find_acl_ind(remote_bda)) == MAX_L2CAP_LINKS)
330 return (BTM_UNKNOWN_ADDR);
331
332 if(BTM_PM_STS_ACTIVE == btm_cb.pm_mode_db[acl_ind].state ||
333 BTM_PM_STS_SNIFF == btm_cb.pm_mode_db[acl_ind].state)
334 {
335 if (btsnd_hcic_sniff_sub_rate(btm_cb.acl_db[acl_ind].hci_handle, max_lat,
336 min_rmt_to, min_loc_to))
337 return BTM_SUCCESS;
338 else
339 return BTM_NO_RESOURCES;
340 }
341 p_cb = &btm_cb.pm_mode_db[acl_ind];
342 p_cb->max_lat = max_lat;
343 p_cb->min_rmt_to = min_rmt_to;
344 p_cb->min_loc_to = min_loc_to;
345 return BTM_CMD_STORED;
346#else
347 return BTM_ILLEGAL_ACTION;
348#endif
349}
350
351/*******************************************************************************
352**
353** Function btm_pm_reset
354**
355** Description as a part of the BTM reset process.
356**
357** Returns void
358**
359*******************************************************************************/
360void btm_pm_reset(void)
361{
362 int xx;
363 tBTM_PM_STATUS_CBACK *cb = NULL;
364
365 /* clear the pending request for application */
366 if( (btm_cb.pm_pend_id != BTM_PM_SET_ONLY_ID) &&
367 (btm_cb.pm_reg_db[btm_cb.pm_pend_id].mask & BTM_PM_REG_NOTIF) )
368 {
369 cb = btm_cb.pm_reg_db[btm_cb.pm_pend_id].cback;
370 }
371
The Android Open Source Project5738f832012-12-12 16:00:35 -0800372
373 /* clear the register record */
374 for(xx=0; xx<BTM_MAX_PM_RECORDS; xx++)
375 {
376 btm_cb.pm_reg_db[xx].mask = BTM_PM_REC_NOT_USED;
377 }
378
Kim Schulz2a2701c2013-09-16 15:59:33 +0200379 if(cb != NULL && btm_cb.pm_pend_link < MAX_L2CAP_LINKS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800380 (*cb)(btm_cb.acl_db[btm_cb.pm_pend_link].remote_addr, BTM_PM_STS_ERROR, BTM_DEV_RESET, 0);
Kim Schulz2a2701c2013-09-16 15:59:33 +0200381
382 /* no command pending */
383 btm_cb.pm_pend_link = MAX_L2CAP_LINKS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800384}
385
386/*******************************************************************************
387**
388** Function btm_pm_sm_alloc
389**
390** Description This function initializes the control block of an ACL link.
391** It is called when an ACL connection is created.
392**
393** Returns void
394**
395*******************************************************************************/
396void btm_pm_sm_alloc(UINT8 ind)
397{
398 tBTM_PM_MCB *p_db = &btm_cb.pm_mode_db[ind]; /* per ACL link */
399 memset (p_db, 0, sizeof(tBTM_PM_MCB));
400 p_db->state = BTM_PM_ST_ACTIVE;
401#if BTM_PM_DEBUG == TRUE
402 BTM_TRACE_DEBUG2( "btm_pm_sm_alloc ind:%d st:%d", ind, p_db->state);
403#endif
404}
405
406/*******************************************************************************
407**
408** Function btm_pm_find_acl_ind
409**
410** Description This function initializes the control block of an ACL link.
411** It is called when an ACL connection is created.
412**
413** Returns void
414**
415*******************************************************************************/
416static int btm_pm_find_acl_ind(BD_ADDR remote_bda)
417{
418 tACL_CONN *p = &btm_cb.acl_db[0];
419 UINT8 xx;
420
421 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
422 {
423 if ((p->in_use) && (!memcmp (p->remote_addr, remote_bda, BD_ADDR_LEN)))
424 {
425#if BTM_PM_DEBUG == TRUE
426 BTM_TRACE_DEBUG2( "btm_pm_find_acl_ind ind:%d, st:%d", xx, btm_cb.pm_mode_db[xx].state);
427#endif
428 break;
429 }
430 }
431 return xx;
432}
433
434/*******************************************************************************
435**
436** Function btm_pm_compare_modes
437** Description get the "more active" mode of the 2
438** Returns void
439**
440*******************************************************************************/
441static tBTM_PM_PWR_MD * btm_pm_compare_modes(tBTM_PM_PWR_MD *p_md1, tBTM_PM_PWR_MD *p_md2, tBTM_PM_PWR_MD *p_res)
442{
443 UINT8 res;
444
445 if(p_md1 == NULL)
446 {
447 *p_res = *p_md2;
448 p_res->mode &= ~BTM_PM_MD_FORCE;
449
450 return p_md2;
451 }
452
453 if(p_md2->mode == BTM_PM_MD_ACTIVE || p_md1->mode == BTM_PM_MD_ACTIVE)
454 {
455 return NULL;
456 }
457
458 /* check if force bit is involved */
459 if(p_md1->mode & BTM_PM_MD_FORCE)
460 {
461 *p_res = *p_md1;
462 p_res->mode &= ~BTM_PM_MD_FORCE;
463 return p_res;
464 }
465
466 if(p_md2->mode & BTM_PM_MD_FORCE)
467 {
468 *p_res = *p_md2;
469 p_res->mode &= ~BTM_PM_MD_FORCE;
470 return p_res;
471 }
472
473 res = (p_md1->mode - 1) * BTM_PM_NUM_SET_MODES + (p_md2->mode - 1);
474 res = btm_pm_md_comp_matrix[res];
475 switch(res)
476 {
477 case BTM_PM_GET_MD1:
478 *p_res = *p_md1;
479 return p_md1;
480
481 case BTM_PM_GET_MD2:
482 *p_res = *p_md2;
483 return p_md2;
484
485 case BTM_PM_GET_COMP:
486 p_res->mode = p_md1->mode;
487 /* min of the two */
488 p_res->max = (p_md1->max < p_md2->max)? (p_md1->max) : (p_md2->max);
489 /* max of the two */
490 p_res->min = (p_md1->min > p_md2->min)? (p_md1->min) : (p_md2->min);
491
492 /* the intersection is NULL */
493 if( p_res->max < p_res->min)
494 return NULL;
495
496 if(p_res->mode == BTM_PM_MD_SNIFF)
497 {
498 /* max of the two */
499 p_res->attempt = (p_md1->attempt > p_md2->attempt)? (p_md1->attempt) : (p_md2->attempt);
500 p_res->timeout = (p_md1->timeout > p_md2->timeout)? (p_md1->timeout) : (p_md2->timeout);
501 }
502 return p_res;
503 }
504 return NULL;
505}
506
507/*******************************************************************************
508**
509** Function btm_pm_get_set_mode
510** Description get the resulting mode from the registered parties, then compare it
511** with the requested mode, if the command is from an unregistered party.
512** Returns void
513**
514*******************************************************************************/
515static tBTM_PM_MODE btm_pm_get_set_mode(UINT8 pm_id, tBTM_PM_MCB *p_cb, tBTM_PM_PWR_MD *p_mode, tBTM_PM_PWR_MD *p_res)
516{
517 int xx, loop_max;
518 tBTM_PM_PWR_MD *p_md = NULL;
519
520 if(p_mode != NULL && p_mode->mode & BTM_PM_MD_FORCE)
521 {
522 *p_res = *p_mode;
523 p_res->mode &= ~BTM_PM_MD_FORCE;
524 return p_res->mode;
525 }
526
527 if(!p_mode)
528 loop_max = BTM_MAX_PM_RECORDS+1;
529 else
530 loop_max = BTM_MAX_PM_RECORDS;
531
532 for( xx=0; xx<loop_max; xx++)
533 {
534 /* g through all the registered "set" parties */
535 if(btm_cb.pm_reg_db[xx].mask & BTM_PM_REG_SET)
536 {
537 if(p_cb->req_mode[xx].mode == BTM_PM_MD_ACTIVE)
538 {
539 /* if at least one registered (SET) party says ACTIVE, stay active */
540 return BTM_PM_MD_ACTIVE;
541 }
542 else
543 {
544 /* if registered parties give conflicting information, stay active */
545 if( (btm_pm_compare_modes(p_md, &p_cb->req_mode[xx], p_res)) == NULL)
546 return BTM_PM_MD_ACTIVE;
547 p_md = p_res;
548 }
549 }
550 }
551
552 /* if the resulting mode is NULL(nobody registers SET), use the requested mode */
553 if(p_md == NULL)
554 {
555 if(p_mode)
556 *p_res = *((tBTM_PM_PWR_MD *)p_mode);
557 else /* p_mode is NULL when btm_pm_snd_md_req is called from btm_pm_proc_mode_change */
558 return BTM_PM_MD_ACTIVE;
559 }
560 else
561 {
562 /* if the command is from unregistered party,
563 compare the resulting mode from registered party*/
564 if( (pm_id == BTM_PM_SET_ONLY_ID) &&
565 ((btm_pm_compare_modes(p_mode, p_md, p_res)) == NULL) )
566 return BTM_PM_MD_ACTIVE;
567 }
568
569 return p_res->mode;
570}
571
572/*******************************************************************************
573**
574** Function btm_pm_snd_md_req
575** Description get the resulting mode and send the resuest to host controller
576** Returns tBTM_STATUS
577**, BOOLEAN *p_chg_ind
578*******************************************************************************/
579static tBTM_STATUS btm_pm_snd_md_req(UINT8 pm_id, int link_ind, tBTM_PM_PWR_MD *p_mode)
580{
581 tBTM_PM_PWR_MD md_res;
582 tBTM_PM_MODE mode;
583 tBTM_PM_MCB *p_cb = &btm_cb.pm_mode_db[link_ind];
584 BOOLEAN chg_ind = FALSE;
585
586 mode = btm_pm_get_set_mode(pm_id, p_cb, p_mode, &md_res);
587 md_res.mode = mode;
588
589#if BTM_PM_DEBUG == TRUE
590 BTM_TRACE_DEBUG2( "btm_pm_snd_md_req link_ind:%d, mode: %d",
591 link_ind, mode);
592#endif
593
594 if( p_cb->state == mode)
595 {
596 /* already in the resulting mode */
597 if( (mode == BTM_PM_MD_ACTIVE) ||
598 ((md_res.max >= p_cb->interval) && (md_res.min <= p_cb->interval)) )
599 return BTM_CMD_STORED;
600 /* Otherwise, needs to wake, then sleep */
601 chg_ind = TRUE;
602 }
603 p_cb->chg_ind = chg_ind;
604
605 /* cannot go directly from current mode to resulting mode. */
606 if( mode != BTM_PM_MD_ACTIVE && p_cb->state != BTM_PM_MD_ACTIVE)
607 p_cb->chg_ind = TRUE; /* needs to wake, then sleep */
608
609 if(p_cb->chg_ind == TRUE) /* needs to wake first */
610 md_res.mode = BTM_PM_MD_ACTIVE;
611#if (BTM_SSR_INCLUDED == TRUE)
612 else if(BTM_PM_MD_SNIFF == md_res.mode && p_cb->max_lat)
613 {
614 btsnd_hcic_sniff_sub_rate(btm_cb.acl_db[link_ind].hci_handle, p_cb->max_lat,
615 p_cb->min_rmt_to, p_cb->min_loc_to);
616 p_cb->max_lat = 0;
617 }
618#endif
619 /* Default is failure */
620 btm_cb.pm_pend_link = MAX_L2CAP_LINKS;
621
622 /* send the appropriate HCI command */
623 btm_cb.pm_pend_id = pm_id;
624
625#if BTM_PM_DEBUG == TRUE
626 BTM_TRACE_DEBUG2("btm_pm_snd_md_req state:0x%x, link_ind: %d", p_cb->state, link_ind);
627#endif
628 switch(md_res.mode)
629 {
630 case BTM_PM_MD_ACTIVE:
631 switch(p_cb->state)
632 {
633 case BTM_PM_MD_SNIFF:
634 if (btsnd_hcic_exit_sniff_mode(btm_cb.acl_db[link_ind].hci_handle))
635 {
636 btm_cb.pm_pend_link = link_ind;
637 }
638 break;
639 case BTM_PM_MD_PARK:
640 if (btsnd_hcic_exit_park_mode(btm_cb.acl_db[link_ind].hci_handle))
641 {
642 btm_cb.pm_pend_link = link_ind;
643 }
644 break;
645 default:
646 /* Failure btm_cb.pm_pend_link = MAX_L2CAP_LINKS */
647 break;
648 }
649 break;
650
651 case BTM_PM_MD_HOLD:
652 if (btsnd_hcic_hold_mode (btm_cb.acl_db[link_ind].hci_handle,
653 md_res.max, md_res.min))
654 {
655 btm_cb.pm_pend_link = link_ind;
656 }
657 break;
658
659 case BTM_PM_MD_SNIFF:
660 if (btsnd_hcic_sniff_mode (btm_cb.acl_db[link_ind].hci_handle,
661 md_res.max, md_res.min, md_res.attempt,
662 md_res.timeout))
663 {
664 btm_cb.pm_pend_link = link_ind;
665 }
666 break;
667
668 case BTM_PM_MD_PARK:
669 if (btsnd_hcic_park_mode (btm_cb.acl_db[link_ind].hci_handle,
670 md_res.max, md_res.min))
671 {
672 btm_cb.pm_pend_link = link_ind;
673 }
674 break;
675 default:
676 /* Failure btm_cb.pm_pend_link = MAX_L2CAP_LINKS */
677 break;
678 }
679
680 if(btm_cb.pm_pend_link == MAX_L2CAP_LINKS)
681 {
682 /* the command was not sent */
683#if BTM_PM_DEBUG == TRUE
684 BTM_TRACE_DEBUG1( "pm_pend_link: %d",btm_cb.pm_pend_link);
685#endif
686 return (BTM_NO_RESOURCES);
687 }
688
689 return BTM_CMD_STARTED;
690}
691
692/*******************************************************************************
693**
694** Function btm_pm_check_stored
695**
696** Description This function is called when an HCI command status event occurs
697** to check if there's any PM command issued while waiting for
698** HCI command status.
699**
700** Returns none.
701**
702*******************************************************************************/
703static void btm_pm_check_stored(void)
704{
705 int xx;
706 for(xx=0; xx<MAX_L2CAP_LINKS; xx++)
707 {
708 if(btm_cb.pm_mode_db[xx].state & BTM_PM_STORED_MASK)
709 {
710 btm_cb.pm_mode_db[xx].state &= ~BTM_PM_STORED_MASK;
711 BTM_TRACE_DEBUG1( "btm_pm_check_stored :%d", xx);
712 btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, xx, NULL);
713 break;
714 }
715 }
716}
717
718
719/*******************************************************************************
720**
721** Function btm_pm_proc_cmd_status
722**
723** Description This function is called when an HCI command status event occurs
724** for power manager related commands.
725**
726** Input Parms status - status of the event (HCI_SUCCESS if no errors)
727**
728** Returns none.
729**
730*******************************************************************************/
731void btm_pm_proc_cmd_status(UINT8 status)
732{
733 tBTM_PM_MCB *p_cb;
734 tBTM_PM_STATUS pm_status;
735
736 if(btm_cb.pm_pend_link >= MAX_L2CAP_LINKS)
737 return;
738
739 p_cb = &btm_cb.pm_mode_db[btm_cb.pm_pend_link];
740
741 if(status == HCI_SUCCESS)
742 {
743 p_cb->state = BTM_PM_ST_PENDING;
744 pm_status = BTM_PM_STS_PENDING;
745#if BTM_PM_DEBUG == TRUE
746 BTM_TRACE_DEBUG1( "btm_pm_proc_cmd_status new state:0x%x", p_cb->state);
747#endif
748 }
749 else /* the command was not successfull. Stay in the same state */
750 {
751 pm_status = BTM_PM_STS_ERROR;
752 }
753
754 /* notify the caller is appropriate */
755 if( (btm_cb.pm_pend_id != BTM_PM_SET_ONLY_ID) &&
756 (btm_cb.pm_reg_db[btm_cb.pm_pend_id].mask & BTM_PM_REG_NOTIF) )
757 {
758 (*btm_cb.pm_reg_db[btm_cb.pm_pend_id].cback)(btm_cb.acl_db[btm_cb.pm_pend_link].remote_addr, pm_status, 0, status);
759 }
760
761 /* no pending cmd now */
762#if BTM_PM_DEBUG == TRUE
763 BTM_TRACE_DEBUG3( "btm_pm_proc_cmd_status state:0x%x, pm_pend_link: %d(new: %d)",
764 p_cb->state, btm_cb.pm_pend_link, MAX_L2CAP_LINKS);
765#endif
766 btm_cb.pm_pend_link = MAX_L2CAP_LINKS;
767
768 btm_pm_check_stored();
769}
770
771/*******************************************************************************
772**
773** Function btm_process_mode_change
774**
775** Description This function is called when an HCI mode change event occurs.
776**
777** Input Parms hci_status - status of the event (HCI_SUCCESS if no errors)
778** hci_handle - connection handle associated with the change
779** mode - HCI_MODE_ACTIVE, HCI_MODE_HOLD, HCI_MODE_SNIFF, or HCI_MODE_PARK
780** interval - number of baseband slots (meaning depends on mode)
781**
782** Returns none.
783**
784*******************************************************************************/
785void btm_pm_proc_mode_change (UINT8 hci_status, UINT16 hci_handle, UINT8 mode, UINT16 interval)
786{
787 tACL_CONN *p;
788 tBTM_PM_MCB *p_cb = NULL;
789 int xx, yy, zz;
790 tBTM_PM_STATE old_state;
791 tL2C_LCB *p_lcb;
792
793 /* get the index to acl_db */
794 if ((xx = btm_handle_to_acl_index(hci_handle)) >= MAX_L2CAP_LINKS)
795 return;
796
797 p = &btm_cb.acl_db[xx];
798
799 /*** 2035 and 2045 work around: If mode is active and coming out of a SCO disconnect, restore packet types ***/
800 if (mode == HCI_MODE_ACTIVE)
801 {
802 if(BTM_GetNumScoLinks() == 0)
803 {
804 if(p->restore_pkt_types)
805 {
806 BTM_TRACE_DEBUG3("btm mode change AFTER unsniffing; hci hdl 0x%x, types 0x%02x/0x%02x",
807 hci_handle, p->pkt_types_mask, p->restore_pkt_types);
808 p->pkt_types_mask = p->restore_pkt_types;
809 p->restore_pkt_types = 0; /* Only exists while SCO is active */
810 btsnd_hcic_change_conn_type (p->hci_handle, p->pkt_types_mask);
811 }
812#if (BTM_PM_SNIFF_SLOT_WORK_AROUND == TRUE)
813 else
814 {
815 BTM_TRACE_DEBUG2("btm mode change AFTER unsniffing; hci hdl 0x%x, types 0x%02x",
816 hci_handle, btm_cb.btm_acl_pkt_types_supported);
817 btm_set_packet_types (p, btm_cb.btm_acl_pkt_types_supported);
818 }
819#endif
820 }
821#if (BTM_PM_SNIFF_SLOT_WORK_AROUND == TRUE)
822 else
823 {
824 /* Mode changed from Sniff to Active while SCO is open. */
825 /* Packet types of active mode, not sniff mode, should be used for ACL when SCO is closed. */
826 p->restore_pkt_types = btm_cb.btm_acl_pkt_types_supported;
827
828 /* Exclude packet types not supported by the peer */
829 btm_acl_chk_peer_pkt_type_support (p, &p->restore_pkt_types);
830 }
831#endif
832 }
833#if (BTM_PM_SNIFF_SLOT_WORK_AROUND == TRUE)
834 else if (mode == HCI_MODE_SNIFF)
835 {
836 BTM_TRACE_DEBUG1("btm mode change to sniff; hci hdl 0x%x use single slot",
837 hci_handle);
838 btm_set_packet_types (p, (HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1));
839 }
840#endif
841
842 /* update control block */
843 p_cb = &(btm_cb.pm_mode_db[xx]);
844 old_state = p_cb->state;
845 p_cb->state = mode;
846 p_cb->interval = interval;
847#if BTM_PM_DEBUG == TRUE
848 BTM_TRACE_DEBUG2( "btm_pm_proc_mode_change new state:0x%x (old:0x%x)", p_cb->state, old_state);
849#endif
850
851 if ((p_cb->state == HCI_MODE_ACTIVE) &&
852 ((p_lcb = l2cu_find_lcb_by_bd_addr (p->remote_addr)) != NULL))
853 {
854 /* There might be any pending packets due to SNIFF or PENDING state */
855 /* Trigger L2C to start transmission of the pending packets. */
856 BTM_TRACE_DEBUG0 ("btm mode change to active; check l2c_link for outgoing packets");
857 l2c_link_check_send_pkts (p_lcb, NULL, NULL);
858
859 //btu_stop_timer (&p_lcb->timer_entry);
860 }
861
862 /* notify registered parties */
863 for(yy=0; yy<=BTM_MAX_PM_RECORDS; yy++)
864 {
865 /* set req_mode HOLD mode->ACTIVE */
866 if( (mode == BTM_PM_MD_ACTIVE) && (p_cb->req_mode[yy].mode == BTM_PM_MD_HOLD) )
867 p_cb->req_mode[yy].mode = BTM_PM_MD_ACTIVE;
868 }
869
870 /* new request has been made. - post a message to BTU task */
871 if(old_state & BTM_PM_STORED_MASK)
872 {
873#if BTM_PM_DEBUG == TRUE
874 BTM_TRACE_DEBUG1( "btm_pm_proc_mode_change: Sending stored req:%d", xx);
875#endif
876 btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, xx, NULL);
877 }
878 else
879 {
880 for(zz=0; zz<MAX_L2CAP_LINKS; zz++)
881 {
882 if(btm_cb.pm_mode_db[zz].chg_ind == TRUE)
883 {
884#if BTM_PM_DEBUG == TRUE
885 BTM_TRACE_DEBUG1( "btm_pm_proc_mode_change: Sending PM req :%d", zz);
886#endif
887 btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, zz, NULL);
888 break;
889 }
890 }
891 }
892
893
894 /* notify registered parties */
895 for(yy=0; yy<BTM_MAX_PM_RECORDS; yy++)
896 {
897 if(btm_cb.pm_reg_db[yy].mask & BTM_PM_REG_NOTIF)
898 {
899 (*btm_cb.pm_reg_db[yy].cback)( p->remote_addr, mode, interval, hci_status);
900 }
901 }
902
903 /* If mode change was because of an active role switch or change link key */
904 btm_cont_rswitch_or_chglinkkey(p, btm_find_dev(p->remote_addr), hci_status);
905}
906
907/*******************************************************************************
908**
909** Function btm_pm_proc_ssr_evt
910**
911** Description This function is called when an HCI sniff subrating event occurs.
912**
913** Returns none.
914**
915*******************************************************************************/
916#if (BTM_SSR_INCLUDED == TRUE)
917void btm_pm_proc_ssr_evt (UINT8 *p, UINT16 evt_len)
918{
919 UINT8 status;
920 UINT16 handle;
921 UINT16 max_tx_lat, max_rx_lat;
922 int xx, yy;
923 tBTM_PM_MCB *p_cb;
924 tACL_CONN *p_acl=NULL;
925 UINT16 use_ssr = TRUE;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800926 UNUSED(evt_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800927
928 STREAM_TO_UINT8 (status, p);
929
930 STREAM_TO_UINT16 (handle, p);
931 /* get the index to acl_db */
932 if ((xx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
933 return;
934
935 STREAM_TO_UINT16 (max_tx_lat, p);
936 STREAM_TO_UINT16 (max_rx_lat, p);
937 p_cb = &(btm_cb.pm_mode_db[xx]);
938
939 p_acl = &btm_cb.acl_db[xx];
940 if(p_cb->interval == max_rx_lat)
941 {
942 /* using legacy sniff */
943 use_ssr = FALSE;
944 }
945
946 /* notify registered parties */
947 for(yy=0; yy<BTM_MAX_PM_RECORDS; yy++)
948 {
949 if(btm_cb.pm_reg_db[yy].mask & BTM_PM_REG_NOTIF)
950 {
951 if( p_acl)
952 {
953 (*btm_cb.pm_reg_db[yy].cback)( p_acl->remote_addr, BTM_PM_STS_SSR, use_ssr, status);
954 }
955 }
956 }
957}
958#endif
959#else /* BTM_PWR_MGR_INCLUDED == TRUE */
960
961/*******************************************************************************
962**
963** Functions BTM_PmRegister, BTM_SetPowerMode, and BTM_ReadPowerMode
964**
965** Description Stubbed versions for BTM_PWR_MGR_INCLUDED = FALSE
966**
967** Returns BTM_MODE_UNSUPPORTED.
968**
969*******************************************************************************/
970tBTM_STATUS BTM_PmRegister (UINT8 mask, UINT8 *p_pm_id, tBTM_PM_STATUS_CBACK *p_cb)
971{
972 return BTM_MODE_UNSUPPORTED;
973}
974
975tBTM_STATUS BTM_SetPowerMode (UINT8 pm_id, BD_ADDR remote_bda, tBTM_PM_PWR_MD *p_mode)
976{
977 return BTM_MODE_UNSUPPORTED;
978}
979
980tBTM_STATUS BTM_ReadPowerMode (BD_ADDR remote_bda, tBTM_PM_MODE *p_mode)
981{
982 return BTM_MODE_UNSUPPORTED;
983}
984
985#endif
986
987/*******************************************************************************
988**
989** Function BTM_IsPowerManagerOn
990**
991** Description This function is called to check if power manager is included.
992** in the BTE version.
993**
994** Returns BTM_PWR_MGR_INCLUDED.
995**
996*******************************************************************************/
997BOOLEAN BTM_IsPowerManagerOn (void)
998{
999 return BTM_PWR_MGR_INCLUDED;
1000}