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
|
/*****************************************************************************/
/* */
/* Name: hidd_pm.c */
/* */
/* Description: this file contains the HID Device Power Management logic */
/* */
/* */
/* Copyright (c) 2002-2004, WIDCOMM Inc., All Rights Reserved. */
/* WIDCOMM Bluetooth Core. Proprietary and confidential. */
/*****************************************************************************/
#include "gki.h"
#include "bt_types.h"
#include "hiddefs.h"
#include "btm_api.h"
#include "string.h"
#include "hiddefs.h"
#include "hidd_api.h"
#include "hidd_int.h"
#include "btu.h"
#if HID_DEV_PM_INCLUDED == TRUE
/*******************************************************************************
**
** Function hidd_pm_init
**
** Description This function is called when connection is established. It
** initializes the control block for power management.
**
** Returns void
**
*******************************************************************************/
static const tHID_DEV_PM_PWR_MD pwr_modes[] =
{
HID_DEV_BUSY_MODE_PARAMS,
HID_DEV_IDLE_MODE_PARAMS,
HID_DEV_SUSP_MODE_PARAMS
};
void hidd_pm_init( void )
{
int i;
hd_cb.curr_pm.mode = HCI_MODE_ACTIVE ;
hd_cb.final_pm.mode = 0xff;
for( i=0; i<3; i++ )
memcpy( &hd_cb.pm_params[i], &pwr_modes[i], sizeof( tHID_DEV_PM_PWR_MD ) ) ;
hd_cb.pm_ctrl_busy = FALSE;
}
/*******************************************************************************
**
** Function hidd_pm_set_now
**
** Description This drives the BTM for power management settings.
**
** Returns TRUE if Success, FALSE otherwise
**
*******************************************************************************/
BOOLEAN hidd_pm_set_now( tHID_DEV_PM_PWR_MD *p_req_mode)
{
tHID_DEV_PM_PWR_MD act_pm = { 0, 0, 0, 0, HCI_MODE_ACTIVE } ;
UINT8 st = BTM_SUCCESS;
/* Do nothing if already in required state or already performing a pm function */
if( (hd_cb.pm_ctrl_busy) ||
((p_req_mode->mode == hd_cb.curr_pm.mode) && ( (p_req_mode->mode == HCI_MODE_ACTIVE) ||
((hd_cb.curr_pm.interval >= p_req_mode->min) && (hd_cb.curr_pm.interval <= p_req_mode->max)) )) )
{
hd_cb.final_pm.mode = 0xff;
return TRUE;
}
switch( p_req_mode->mode )
{
case HCI_MODE_ACTIVE:
if( hd_cb.curr_pm.mode == HCI_MODE_SNIFF )
{
#if BTM_PWR_MGR_INCLUDED == TRUE
st = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, hd_cb.host_addr, (tBTM_PM_PWR_MD *) &act_pm);
#else
st = BTM_CancelSniffMode (hd_cb.host_addr);
#endif
hd_cb.pm_ctrl_busy = TRUE;
}
else if( hd_cb.curr_pm.mode == HCI_MODE_PARK )
{
#if BTM_PWR_MGR_INCLUDED == TRUE
st = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, hd_cb.host_addr, (tBTM_PM_PWR_MD *) &act_pm);
#else
st = BTM_CancelParkMode (hd_cb.host_addr);
#endif
hd_cb.pm_ctrl_busy = TRUE;
}
break;
case HCI_MODE_SNIFF:
if( hd_cb.curr_pm.mode != HCI_MODE_ACTIVE ) /* Transition through active state required */
hidd_pm_set_now (&act_pm);
else
{
#if BTM_PWR_MGR_INCLUDED == TRUE
st = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, hd_cb.host_addr, (tBTM_PM_PWR_MD *) p_req_mode);
#else
st = BTM_SetSniffMode (hd_cb.host_addr, p_req_mode->min, p_req_mode->max, p_req_mode->attempt, p_req_mode->timeout);
#endif
hd_cb.pm_ctrl_busy = TRUE;
}
break;
case HCI_MODE_PARK:
if( hd_cb.curr_pm.mode != HCI_MODE_ACTIVE ) /* Transition through active state required */
hidd_pm_set_now (&act_pm);
else
{
#if BTM_PWR_MGR_INCLUDED == TRUE
st = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, hd_cb.host_addr, (tBTM_PM_PWR_MD *) p_req_mode);
#else
st = BTM_SetParkMode (hd_cb.host_addr, p_req_mode->min, p_req_mode->max);
#endif
hd_cb.pm_ctrl_busy = TRUE;
}
break;
default:
break;
}
if( st == BTM_SUCCESS || st == BTM_CMD_STARTED )
return TRUE;
else
{
st += HCI_ERR_MAX_ERR ;
if( hd_cb.callback )
hd_cb.callback( HID_DEV_EVT_PM_FAILED, hd_cb.conn_substate, (tHID_DEV_CBACK_DATA *) &st ) ;
return FALSE;
}
}
/*******************************************************************************
**
** Function hidd_pm_set_power_mode
**
** Description This stores the power management setting and calls fn to set
** the power.
**
** Returns TRUE if Success, FALSE otherwise
**
*******************************************************************************/
BOOLEAN hidd_pm_set_power_mode( tHID_DEV_PM_PWR_MD *p_req_mode)
{
memcpy( &hd_cb.final_pm, p_req_mode, sizeof( tHID_DEV_PM_PWR_MD ) ) ;
return hidd_pm_set_now( p_req_mode ) ;
}
/*******************************************************************************
**
** Function hidd_pm_proc_mode_change
**
** Description This is the callback function, when power mode changes.
**
** Returns void
**
*******************************************************************************/
void hidd_pm_proc_mode_change( UINT8 hci_status, UINT8 mode, UINT16 interval )
{
if (!hd_cb.reg_flag )
return;
hd_cb.pm_ctrl_busy = FALSE;
if( hci_status != HCI_SUCCESS )
{
if( hd_cb.callback )
hd_cb.callback( HID_DEV_EVT_PM_FAILED, hd_cb.conn_substate, (tHID_DEV_CBACK_DATA *) &hci_status ) ;
}
else
{
hd_cb.curr_pm.mode = mode;
hd_cb.curr_pm.interval = interval;
if( hd_cb.final_pm.mode != 0xff )
{
/* If we haven't reached the final power mode, set it now */
if( (hd_cb.final_pm.mode != hd_cb.curr_pm.mode) ||
( (hd_cb.final_pm.mode != HCI_MODE_ACTIVE) &&
((hd_cb.curr_pm.interval < hd_cb.final_pm.min) || (hd_cb.curr_pm.interval > hd_cb.final_pm.max))
) )
{
hidd_pm_set_now( &(hd_cb.final_pm) ) ;
}
else
hd_cb.final_pm.mode = 0xff;
}
else
{
if( hd_cb.curr_pm.mode == HCI_MODE_ACTIVE )
hidd_pm_start();
}
if( hd_cb.callback )
hd_cb.callback( HID_DEV_EVT_MODE_CHG, mode, (tHID_DEV_CBACK_DATA *) &interval) ;
}
}
/*******************************************************************************
**
** Function hidd_pm_inact_timeout
**
** Description Called when idle timer expires.
**
** Returns void
**
*******************************************************************************/
void hidd_pm_inact_timeout (TIMER_LIST_ENT *p_tle)
{
hidd_pm_set_power_mode ( &(hd_cb.pm_params[HID_DEV_IDLE_CONN_ST]));
hd_cb.conn_substate = HID_DEV_IDLE_CONN_ST;
}
/*******************************************************************************
**
** Function hidd_pm_start
**
** Description Starts the power management function in a given state.
**
** Returns tHID_STATUS
**
*******************************************************************************/
tHID_STATUS hidd_pm_start( void )
{
hidd_pm_set_power_mode ( &(hd_cb.pm_params[HID_DEV_BUSY_CONN_ST]) );
hd_cb.conn_substate = HID_DEV_BUSY_CONN_ST;
hd_cb.idle_tle.param = (UINT32) hidd_pm_inact_timeout;
btu_start_timer (&hd_cb.idle_tle, BTU_TTYPE_USER_FUNC, HID_DEV_INACT_TIMEOUT);
return( HID_SUCCESS );
}
/*******************************************************************************
**
** Function hidd_pm_stop
**
** Description Stops the idle timer.
**
** Returns tHID_STATUS
**
*******************************************************************************/
tHID_STATUS hidd_pm_stop( void )
{
tHID_DEV_PM_PWR_MD p_md = { 0, 0, 0, 0, HCI_MODE_ACTIVE };
hidd_pm_set_power_mode( &p_md );
btu_stop_timer( &hd_cb.idle_tle ) ;
return( HID_SUCCESS );
}
/*******************************************************************************
**
** Function hidd_pm_suspend_evt
**
** Description Called when host suspends the device.
**
** Returns tHID_STATUS
**
*******************************************************************************/
tHID_STATUS hidd_pm_suspend_evt( void )
{
if( hd_cb.conn_substate == HID_DEV_BUSY_CONN_ST )
btu_stop_timer( &hd_cb.idle_tle ) ;
hidd_pm_set_power_mode ( &(hd_cb.pm_params[HID_DEV_SUSP_CONN_ST]) );
hd_cb.conn_substate = HID_DEV_SUSP_CONN_ST;
return( HID_SUCCESS );
}
#endif /* HID_DEV_PM_INCLUDED == TRUE */
|