summaryrefslogtreecommitdiffstats
path: root/bta/hh/bta_hh_utils.c
blob: 43492824da3717957baf41feedf9da34e0790e94 (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
/******************************************************************************
 *
 *  Copyright (C) 2005-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.
 *
 ******************************************************************************/
#include <string.h>

#include "bt_target.h"
#if defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)


#include "bta_hh_int.h"

/*****************************************************************************
**  Constants
*****************************************************************************/
#define BTA_HH_KB_CTRL_MASK         0x11
#define BTA_HH_KB_SHIFT_MASK        0x22
#define BTA_HH_KB_ALT_MASK          0x44
#define BTA_HH_KB_GUI_MASK          0x88

#define BTA_HH_KB_CAPS_LOCK      0x39           /* caps lock */
#define BTA_HH_KB_NUM_LOCK       0x53           /* num lock */


#define BTA_HH_MAX_RPT_CHARS    8

static const UINT8 bta_hh_mod_key_mask[BTA_HH_MOD_MAX_KEY] =
{
    BTA_HH_KB_CTRL_MASK,
    BTA_HH_KB_SHIFT_MASK,
    BTA_HH_KB_ALT_MASK,
    BTA_HH_KB_GUI_MASK
};


/*******************************************************************************
**
** Function         bta_hh_find_cb
**
** Description      Find best available control block according to BD address.
**
**
** Returns          void
**
*******************************************************************************/
UINT8  bta_hh_find_cb(BD_ADDR bda)
{
    UINT8 xx;

    /* See how many active devices there are. */
    for (xx = 0; xx < BTA_HH_MAX_KNOWN; xx++)
    {
        /* check if any active/known devices is a match */
        if ((!bdcmp (bda, bta_hh_cb.kdev[xx].addr) &&
              bdcmp(bda, bd_addr_null) != 0) )
        {
#if BTA_HH_DEBUG
            APPL_TRACE_DEBUG2("found kdev_cb[%d] hid_handle = %d ", xx,
                                bta_hh_cb.kdev[xx].hid_handle)
#endif
            return xx;
        }
#if BTA_HH_DEBUG
        else
            APPL_TRACE_DEBUG4("in_use ? [%d] kdev[%d].hid_handle = %d state = [%d]",
                            bta_hh_cb.kdev[xx].in_use, xx,
                            bta_hh_cb.kdev[xx].hid_handle,
                            bta_hh_cb.kdev[xx].state);
#endif
    }

    /* if no active device match, find a spot for it */
    for (xx = 0; xx < BTA_HH_MAX_KNOWN; xx++)
    {
        if (!bta_hh_cb.kdev[xx].in_use)
        {
            bdcpy(bta_hh_cb.kdev[xx].addr, bda);
            break;
        }
    }
    /* If device list full, report BTA_HH_MAX_KNOWN */
#if BTA_HH_DEBUG
    APPL_TRACE_DEBUG2("bta_hh_find_cb:: index = %d while max = %d",
                        xx, BTA_HH_MAX_KNOWN);
#endif

    return xx;
}

/*******************************************************************************
**
** Function         bta_hh_clean_up_kdev
**
** Description      Clean up device control block when device is removed from
**                  manitainace list, and update control block index map.
**
** Returns          void
**
*******************************************************************************/
void bta_hh_clean_up_kdev(tBTA_HH_DEV_CB *p_cb)
{
    UINT8 index;

    if (p_cb->hid_handle != BTA_HH_INVALID_HANDLE )
        bta_hh_cb.cb_index[p_cb->hid_handle] = BTA_HH_MAX_KNOWN;

    /* reset device control block */
    index = p_cb->index;                        /* Preserve index for this control block */

    /* Free buffer for report descriptor info */
    utl_freebuf((void **)&p_cb->dscp_info.descriptor.dsc_list);

    memset(p_cb, 0, sizeof (tBTA_HH_DEV_CB));   /* Reset control block */

    p_cb->index = index;                        /* Restore index for this control block */
    p_cb->state      = BTA_HH_IDLE_ST;
    p_cb->hid_handle = BTA_HH_INVALID_HANDLE;

}
/*******************************************************************************
**
** Function         bta_hh_update_di_info
**
** Description      Maintain a known device list for BTA HH.
**
** Returns          void
**
*******************************************************************************/
void bta_hh_update_di_info(tBTA_HH_DEV_CB *p_cb, UINT16 vendor_id, UINT16 product_id,
                           UINT16 version)
{
#if BTA_HH_DEBUG
    APPL_TRACE_DEBUG3("vendor_id = 0x%2x product_id = 0x%2x version = 0x%2x",
                        vendor_id, product_id, version);
#endif
    p_cb->dscp_info.vendor_id     =   vendor_id;
    p_cb->dscp_info.product_id    =   product_id;
    p_cb->dscp_info.version       =   version;
}
/*******************************************************************************
**
** Function         bta_hh_add_device_to_list
**
** Description      Maintain a known device list for BTA HH.
**
** Returns          void
**
*******************************************************************************/
void bta_hh_add_device_to_list(tBTA_HH_DEV_CB *p_cb, UINT8 handle,
                               UINT16 attr_mask,
                               tHID_DEV_DSCP_INFO *p_dscp_info,
                               UINT8 sub_class,
                               UINT16 ssr_max_latency,
                               UINT16 ssr_min_tout,
                               UINT8 app_id)
{
#if BTA_HH_DEBUG
    APPL_TRACE_DEBUG1("subclass = 0x%2x", sub_class);
#endif

    p_cb->hid_handle = handle;
    p_cb->in_use = TRUE;
    p_cb->attr_mask = attr_mask;

    p_cb->sub_class = sub_class;
    p_cb->app_id    = app_id;

    if (ssr_max_latency == HID_SSR_PARAM_INVALID)
        p_cb->dscp_info.ssr_max_latency = BTA_HH_SSR_MAX_LATENCY_DEF;
    else
        p_cb->dscp_info.ssr_max_latency = ssr_max_latency;

    if (ssr_min_tout == HID_SSR_PARAM_INVALID)
        p_cb->dscp_info.ssr_min_tout = BTA_HH_SSR_MIN_TOUT_DEF;
    else
        p_cb->dscp_info.ssr_min_tout = ssr_min_tout;

    /* store report descriptor info */
    if ( p_dscp_info)
    {
        utl_freebuf((void **)&p_cb->dscp_info.descriptor.dsc_list);

        if (p_dscp_info->dl_len &&
        (p_cb->dscp_info.descriptor.dsc_list =
            (UINT8 *)GKI_getbuf(p_dscp_info->dl_len)) != NULL)
        {
            p_cb->dscp_info.descriptor.dl_len = p_dscp_info->dl_len;
            memcpy(p_cb->dscp_info.descriptor.dsc_list, p_dscp_info->dsc_list,
                    p_dscp_info->dl_len);
        }
    }
    return;
}

/*******************************************************************************
**
** Function         bta_hh_tod_spt
**
** Description      Check to see if this type of device is supported
**
** Returns
**
*******************************************************************************/
BOOLEAN bta_hh_tod_spt(tBTA_HH_DEV_CB *p_cb,UINT8 sub_class)
{
    UINT8    xx;
    UINT8    cod = (sub_class >> 2); /* lower two bits are reserved */

    for (xx = 0 ; xx < p_bta_hh_cfg->max_devt_spt; xx ++)
    {
        if (cod == (UINT8) p_bta_hh_cfg->p_devt_list[xx].tod)
        {
            p_cb->app_id = p_bta_hh_cfg->p_devt_list[xx].app_id;
#if BTA_HH_DEBUG
            APPL_TRACE_EVENT1("bta_hh_tod_spt sub_class:0x%x supported", sub_class);
#endif
            return TRUE;
        }
    }
#if BTA_HH_DEBUG
            APPL_TRACE_EVENT1("bta_hh_tod_spt sub_class:0x%x NOT supported", sub_class);
#endif
    return FALSE;
}


/*******************************************************************************
**
** Function         bta_hh_parse_keybd_rpt
**
** Description      This utility function parse a boot mode keyboard report.
**
** Returns          void
**
*******************************************************************************/
void bta_hh_parse_keybd_rpt(tBTA_HH_BOOT_RPT *p_kb_data, UINT8 *p_report,
                            UINT16 report_len)
{
    tBTA_HH_KB_CB       *p_kb = &bta_hh_cb.kb_cb;
    tBTA_HH_KEYBD_RPT   *p_data = &p_kb_data->data_rpt.keybd_rpt;

    UINT8        this_char, ctl_shift;
    UINT16       xx, yy, key_idx = 0;
    UINT8        this_report[BTA_HH_MAX_RPT_CHARS];

#if BTA_HH_DEBUG
    APPL_TRACE_DEBUG2("bta_hh_parse_keybd_rpt:  (report=%p, report_len=%d) called",
            p_report, report_len);
#endif

    if (report_len < 2)
        return;

    ctl_shift = *p_report++;
    report_len--;

    if (report_len > BTA_HH_MAX_RPT_CHARS)
        report_len = BTA_HH_MAX_RPT_CHARS;

    memset (this_report, 0, BTA_HH_MAX_RPT_CHARS);
    memset (p_data, 0, sizeof(tBTA_HH_KEYBD_RPT));
    memcpy (this_report, p_report, report_len);

    /* Take care of shift, control, GUI and alt, modifier keys  */
    for (xx = 0; xx < BTA_HH_MOD_MAX_KEY; xx ++ )
    {
        if (ctl_shift & bta_hh_mod_key_mask[xx])
        {
            APPL_TRACE_DEBUG1("Mod Key[%02x] pressed", bta_hh_mod_key_mask[xx] );
            p_kb->mod_key[xx] = TRUE;
        }
        else if (p_kb->mod_key[xx])
        {
            p_kb->mod_key[xx] = FALSE;
        }
        /* control key flag is set */
        p_data->mod_key[xx]       = p_kb->mod_key[xx];
    }

    /***************************************************************************/
    /*  First step is to remove all characters we saw in the last report       */
    /***************************************************************************/
    for (xx = 0; xx < report_len; xx++)
    {
        for (yy = 0; yy < BTA_HH_MAX_RPT_CHARS; yy++)
        {
            if (this_report[xx] == p_kb->last_report[yy])
            {
                this_report[xx] = 0;
            }
        }
    }
    /***************************************************************************/
    /*  Now, process all the characters in the report, up to 6 keycodes        */
    /***************************************************************************/
    for (xx = 0; xx < report_len; xx++)
    {
#if BTA_HH_DEBUG
        APPL_TRACE_DEBUG1("this_char = %02x", this_report[xx]);
#endif
        if ((this_char = this_report[xx]) == 0)
            continue;
        /* take the key code as the report data */
        if (this_report[xx] == BTA_HH_KB_CAPS_LOCK)
            p_kb->caps_lock = p_kb->caps_lock ? FALSE : TRUE;
        else if (this_report[xx] == BTA_HH_KB_NUM_LOCK)
            p_kb->num_lock = p_kb->num_lock ? FALSE : TRUE;
        else
            p_data->this_char[key_idx ++] = this_char;

#if BTA_HH_DEBUG
        APPL_TRACE_DEBUG1("found keycode %02x ",  this_report[xx]);
#endif
        p_data->caps_lock   = p_kb->caps_lock;
        p_kb->num_lock      = p_kb->num_lock;
    }

    memset (p_kb->last_report, 0, BTA_HH_MAX_RPT_CHARS);
    memcpy (p_kb->last_report, p_report, report_len);

    return;
}

/*******************************************************************************
**
** Function         bta_hh_parse_mice_rpt
**
** Description      This utility function parse a boot mode mouse report.
**
** Returns          void
**
*******************************************************************************/
void bta_hh_parse_mice_rpt(tBTA_HH_BOOT_RPT *p_mice_data, UINT8 *p_report,
                           UINT16 report_len)
{
    tBTA_HH_MICE_RPT   *p_data = &p_mice_data->data_rpt.mice_rpt;
#if BTA_HH_DEBUG
    UINT8       xx;

    APPL_TRACE_DEBUG2("bta_hh_parse_mice_rpt:  bta_keybd_rpt_rcvd(report=%p, \
                report_len=%d) called", p_report, report_len);
#endif

    if (report_len < 3)
        return;

    if (report_len > BTA_HH_MAX_RPT_CHARS)
        report_len = BTA_HH_MAX_RPT_CHARS;

#if BTA_HH_DEBUG
    for (xx = 0; xx < report_len; xx++)
    {
        APPL_TRACE_DEBUG1("this_char = %02x", p_report[xx]);
    }
#endif

    /* only first bytes lower 3 bits valid */
    p_data->mouse_button     = (p_report[0] & 0x07);

    /* x displacement */
    p_data->delta_x     = p_report[1];

    /* y displacement */
    p_data->delta_y     = p_report[2];

#if BTA_HH_DEBUG
    APPL_TRACE_DEBUG1("mice button: 0x%2x", p_data->mouse_button);
    APPL_TRACE_DEBUG2("mice move: x = %d y = %d", p_data->delta_x,
                        p_data->delta_y );
#endif

    return;

}

#if BTA_HH_DEBUG
/*******************************************************************************
**
** Function         bta_hh_trace_dev_db
**
** Description      Check to see if this type of device is supported
**
** Returns
**
*******************************************************************************/
void bta_hh_trace_dev_db(void)
{
    UINT8    xx;

    APPL_TRACE_DEBUG0("bta_hh_trace_dev_db:: Device DB list********************");

    for (xx = 0; xx < BTA_HH_MAX_KNOWN; xx++)
    {
        APPL_TRACE_DEBUG3("kdev[%d] in_use[%d]  handle[%d] ",xx,
            bta_hh_cb.kdev[xx].in_use, bta_hh_cb.kdev[xx].hid_handle);

        APPL_TRACE_DEBUG4("\t\t\t attr_mask[%04x] state [%d] sub_class[%02x] index = %d",
            bta_hh_cb.kdev[xx].attr_mask, bta_hh_cb.kdev[xx].state,
            bta_hh_cb.kdev[xx].sub_class, bta_hh_cb.kdev[xx].index);
    }
    APPL_TRACE_DEBUG0("*********************************************************");
}
#endif
#endif /* HL_INCLUDED */