1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2013 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#include <string.h>
20
21#include "bt_target.h"
22#include "gap_int.h"
23#include "btm_int.h"
24#include "gki.h"
25#include "btu.h"
26
27/*******************************************************************************
28**
29** Function         GAP_SetDiscoverableMode
30**
31** Description      This function is called to allow or disallow a service to
32**                  discovered (Inquiry Scans).
33**
34** Parameters:      mode        - GAP_NON_DISCOVERABLE, GAP_LIMITED_DISCOVERABLE,
35**                                  or GAP_GENERAL_DISCOVERABLE
36**
37**                  duration    - Amount of time for the duration of an inquiry scan.
38**                                The step size is in 0.625 msec intervals.
39**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
40**
41**                                If a value of '0' is entered the default of
42**                                0x0012 (11.25 msecs) will be used.
43**                                Note: The duration must be less than or equal to
44**                                the interval.
45**
46**                  interval    - Amount of time between the start of two inquiry scans.
47**                                The step size is in 0.625 msec intervals.
48**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
49**                                If a value of '0' is entered the default of
50**                                0x800 (1.28 secs) will be used.
51**
52**
53** Returns          BT_PASS (0) if successful,
54**                  GAP_ERR_ILL_PARM if a bad parameter is detected,
55**                  GAP_DEVICE_NOT_UP if the device is not active,
56**                  GAP_ERR_PROCESSING if not enough resources to carry out request
57**
58*******************************************************************************/
59UINT16 GAP_SetDiscoverableMode (UINT16 mode, UINT16 duration, UINT16 interval)
60{
61    tBTM_STATUS status;
62
63    status = BTM_SetDiscoverability(mode, duration, interval);
64
65    return (gap_convert_btm_status (status));
66}
67
68
69/*******************************************************************************
70**
71** Function         GAP_ReadDiscoverableMode
72**
73** Description      This function is called to retrieve the current discoverable mode
74**                  for the local device.
75**
76** Parameters:      duration    - pointer to the amount of time of an inquiry scan.
77**                                The step size is in 0.625 msec intervals.
78**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
79**
80**                  interval    - pointer to the amount of time between the start of
81**                                two inquiry scans.
82**                                The step size is in 0.625 msec intervals.
83**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
84**
85**
86** Returns          GAP_NON_DISCOVERABLE, GAP_LIMITED_DISCOVERABLE, or
87**                  GAP_GENERAL_DISCOVERABLE
88**
89*******************************************************************************/
90UINT16 GAP_ReadDiscoverableMode (UINT16 *duration, UINT16 *interval)
91{
92    return (BTM_ReadDiscoverability(duration, interval));
93}
94
95
96/*******************************************************************************
97**
98** Function         GAP_SetConnectableMode
99**
100** Description      This function is called to allow or disallow a
101**                  connections on the local device.
102**
103** Parameters:      mode        - GAP_NON_CONNECTABLE, GAP_CONNECTABLE,
104**
105**                  duration    - Amount of time for the duration of a page scan.
106**                                The step size is in 0.625 msec intervals.
107**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
108**
109**                                If a value of '0' is entered the default of
110**                                0x0012 (11.25 msecs) will be used.
111**                                Note: The duration must be less than or equal to
112**                                the interval.
113**
114**                  interval    - Amount of time between the start of two page scans.
115**                                The step size is in 0.625 msec intervals.
116**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
117**                                If a value of '0' is entered the default of
118**                                0x800 (1.28 secs) will be used.
119**
120**
121** Returns          BT_PASS (0) if successful,
122**                  GAP_ERR_ILL_PARM if a bad parameter is detected,
123**                  GAP_DEVICE_NOT_UP if the device is not active,
124**                  GAP_ERR_PROCESSING if not enough resources to carry out request
125**
126*******************************************************************************/
127UINT16 GAP_SetConnectableMode (UINT16 mode, UINT16 duration, UINT16 interval)
128{
129    tBTM_STATUS status;
130
131    status = BTM_SetConnectability(mode, duration, interval);
132
133    return (gap_convert_btm_status (status));
134}
135
136
137/*******************************************************************************
138**
139** Function         GAP_FindAddrByName
140**
141** Description      This function is called to retrieve a device address given
142**                  a device name.  It first looks in the current local inquiry
143**                  database for the device with the specified name.  If not found
144**                  it initiates a general inquiry.  Upon completion, it retrieves
145**                  the name for each device until a match is found or all devices
146**                  have been checked.  Note:  This process can take a while to
147**                  complete.
148**
149** Parameters:      devname -
150**
151**                  inqparms - pointer to the inquiry information
152**                      mode - GAP_GENERAL_INQUIRY or GAP_LIMITED_INQUIRY inquiry
153**                      duration - length in 1.28 sec intervals
154**                      max_resps - maximum amount of devices to search for before ending the inquiry
155**                      filter_cond_type - GAP_CLR_INQUIRY_FILTER, GAP_FILTER_COND_DEVICE_CLASS, or
156**                                         GAP_FILTER_COND_BD_ADDR
157**                      filter_cond - value for the filter (based on filter_cond_type)
158**
159**
160** Returns          BT_PASS if the name was immediately available.  (BD_ADDR is returned)
161**                  GAP_CMD_INITIATED if an inquiry has been initiated
162**
163*******************************************************************************/
164UINT16 GAP_FindAddrByName (BD_NAME devname, tGAP_INQ_PARMS *p_inq_parms, tGAP_CALLBACK *p_addr_cb,
165                           BD_ADDR bd_addr)
166{
167    UINT16           status;
168    tBTM_STATUS      btm_status;
169
170
171    /* If the remote name is retrieved automatically during an inquiry search the local db first */
172    if ((status = gap_find_local_addr_by_name (devname, bd_addr)) != BT_PASS)
173    {
174        /* If this code is used, the name wasn't in the current inquiry database */
175        /* A general inquiry must now be initiated */
176        if (gap_cb.findaddr_cb.in_use == FALSE)
177        {
178            gap_cb.findaddr_cb.p_cback = p_addr_cb;
179            gap_cb.findaddr_cb.p_cur_inq = (tBTM_INQ_INFO *) NULL;     /* Reset to the beginning of the database */
180            BCM_STRNCPY_S ((char *)gap_cb.findaddr_cb.results.devname, sizeof(gap_cb.findaddr_cb.results.devname), (char *)devname, BTM_MAX_REM_BD_NAME_LEN);
181
182            /* make sure we have an end of string char */
183            gap_cb.findaddr_cb.results.devname[BTM_MAX_REM_BD_NAME_LEN] = 0;
184
185            btm_status = BTM_StartInquiry (p_inq_parms, (tBTM_INQ_RESULTS_CB *) NULL,
186                    (tBTM_CMPL_CB *) gap_find_addr_inq_cb);
187                gap_cb.findaddr_cb.in_use = TRUE;
188
189            /* convert the error code into a GAP code and check the results for any errors */
190            if ((status = gap_convert_btm_status (btm_status)) == GAP_CMD_INITIATED)
191                gap_cb.findaddr_cb.in_use = TRUE;
192        }
193        else
194            status = GAP_ERR_BUSY;
195    }
196
197    return (status);
198}
199
200
201/*******************************************************************************
202**
203** Function         GAP_ReadConnectableMode
204**
205** Description      This function is called to retrieve the current connectability
206**                  mode for the local device.
207**
208** Parameters:      duration    - pointer to the amount of time of an page scan.
209**                                The step size is in 0.625 msec intervals.
210**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
211**
212**                  interval    - pointer to the amount of time between the start of
213**                                two page scans.
214**                                The step size is in 0.625 msec intervals.
215**                                Range: 0x0012 - 0x1000 (11.25 - 2560 msecs)
216**
217**
218** Returns          GAP_NON_CONNECTABLE, GAP_CONNECTABLE
219**
220*******************************************************************************/
221
222UINT16 GAP_ReadConnectableMode (UINT16 *duration, UINT16 *interval)
223{
224    return (BTM_ReadConnectability(duration, interval));
225}
226
227
228/*******************************************************************************
229**
230** Function         GAP_SetSecurityMode
231**
232** Description      Set security mode for the device
233**
234** Returns          void
235**
236*******************************************************************************/
237void GAP_SetSecurityMode (UINT8 sec_mode)
238{
239    BTM_SetSecurityMode (sec_mode);
240}
241
242
243/*******************************************************************************
244**
245** Function         GAP_Bond
246**
247** Description      This function is called to perform bonding with peer device
248**
249** Parameters:      bd_addr      - Address of the device to bond
250**                  pin_len      - length in bytes of the PIN Code
251**                  p_pin        - pointer to array with the PIN Code
252**                  trusted_mask - bitwise OR of trusted services (array of UINT32)
253**
254*******************************************************************************/
255UINT8 GAP_Bond (BD_ADDR bd_addr, UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
256{
257    return ((UINT8) BTM_SecBond (bd_addr, pin_len, p_pin, trusted_mask));
258}
259
260
261/*******************************************************************************
262**
263** Function         GAP_SecRegister
264**
265** Description      Application manager calls this function to register for
266**                  security services.  There can be one and only one application
267**                  saving link keys.  BTM allows only first registration.
268**
269** Returns          TRUE if registered OK, else FALSE
270**
271*******************************************************************************/
272BOOLEAN  GAP_SecRegister (tBTM_APPL_INFO *p_cb_info)
273{
274    return (BTM_SecRegister (p_cb_info));
275}
276
277
278/*******************************************************************************
279**
280** Function         GAP_PinRsp
281**
282** Description      This function is called from UI after Security Manager submitted
283**                  PIN code request.
284**
285** Parameters:      bd_addr      - Address of the device for which PIN was requested
286**                  res          - result of the operation BTM_SUCCESS if success
287**                  pin_len      - length in bytes of the PIN Code
288**                  p_pin        - pointer to array with the PIN Code
289**                  trusted_mask - bitwise OR of trusted services (array of UINT32)
290**
291*******************************************************************************/
292void GAP_PinRsp (BD_ADDR bd_addr, UINT8 res, UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
293{
294    BTM_PINCodeReply (bd_addr, res, pin_len, p_pin, trusted_mask);
295}
296
297
298/*******************************************************************************
299**
300** Function         GAP_AuthorizeRsp
301**
302** Description      This function is called from UI after Security Manager submitted
303**                  authorization request
304**
305** Parameters:      bd_addr      - Address of the device for which PIN was requested
306**                  res          - result of the operation BTM_SUCCESS if success
307**                  trusted_mask - bitwise OR of trusted services (array of UINT32)
308**
309*******************************************************************************/
310void GAP_AuthorizeRsp (BD_ADDR bd_addr, UINT8 res, UINT32 trusted_mask[])
311{
312    BTM_DeviceAuthorized (bd_addr, res, trusted_mask);
313}
314
315
316/*******************************************************************************
317**
318** Function         GAP_SetPairableMode
319**
320** Description      This function is called to allow or disallow pairing
321**                  on the local device.
322**
323** Parameters:      mode        - GAP_ALLOW_PAIRING, GAP_DISALLOW_PAIRING
324**                  connect_only_pairable - TRUE or FALSE connect only to paired devices
325**
326**                  callback    - The callback is called when a pin number is requested.
327**
328** Returns          BT_PASS (0) if successful, or a non-zero error code
329**
330*******************************************************************************/
331
332UINT16 GAP_SetPairableMode (UINT16 mode, BOOLEAN connect_only_paired)
333{
334    tBTM_STATUS btm_status;
335    UINT16      status = BT_PASS;
336
337    if (mode == GAP_ALLOW_PAIRING)
338    {
339        btm_status = BTM_SetConnectability(BTM_CONNECTABLE, 0, 0);
340
341        if ((status = gap_convert_btm_status (btm_status)) == BT_PASS)
342            BTM_SetPairableMode (TRUE, connect_only_paired);
343    }
344    else if (mode == GAP_DISALLOW_PAIRING)
345    {
346        BTM_SetPairableMode (FALSE, connect_only_paired);
347    }
348    else
349    {
350        GAP_TRACE_ERROR1 ("GAP_SetPairableMode: illegal mode %d", mode);
351        status = GAP_ERR_ILL_MODE;
352    }
353    return (status);
354}
355
356
357/*******************************************************************************
358**
359** Function         GAP_StartInquiry
360**
361** Description      This function initiates a single inquiry.
362**
363** Parameters:      p_inqparms - pointer to the inquiry information
364**                      mode - GAP_GENERAL_INQUIRY or GAP_LIMITED_INQUIRY inquiry
365**                      duration - length in 1.28 sec intervals
366**                      max_resps - maximum amount of devices to search for before ending the inquiry
367**                      filter_cond_type - GAP_CLR_INQUIRY_FILTER, GAP_FILTER_COND_DEVICE_CLASS, or
368**                                         GAP_FILTER_COND_BD_ADDR
369**                      filter_cond - value for the filter (based on filter_cond_type)
370**
371**                  p_results_cb - Pointer to the callback routine which gets called
372**                                 upon receipt of an inquiry result. If this field is
373**                                 NULL, the application is not notified.
374**
375**                  p_cmpl_cb   - Pointer to the callback routine which gets called
376**                                upon completion.  If this field is NULL, the
377**                                application is not notified when completed.
378**
379**
380** Returns          BT_PASS (0) if successful,
381**                  GAP_ERR_ILL_MODE if a bad mode parameter was passed
382**                  GAP_ERR_ILL_INQ_TIME if a bad interval or duration was passed
383**                  GAP_ERR_NO_CTRL_BLK if out of control blocks
384**                  GAP_ERR_ILL_PARM if a bad parameter was detected in BTM
385**                  GAP_ERR_BUSY if the device already has an iquiry active
386**                  GAP_DEVICE_NOT_UP if the device is not initialized yet
387**                  GAP_ERR_PROCESSING if any other BTM error was returned
388**
389*******************************************************************************/
390UINT16 GAP_StartInquiry (tGAP_INQ_PARMS *p_inq_parms, tGAP_CALLBACK *p_results_cb, tGAP_CALLBACK *p_cmpl_cb)
391{
392    tGAP_INFO   *p_cb;
393    tBTM_STATUS  btm_status;
394    UINT16       retval;
395
396    /*** Make sure the parameters are valid before continuing ***/
397    if (p_inq_parms->mode != GAP_GENERAL_INQUIRY && p_inq_parms->mode != GAP_LIMITED_INQUIRY)
398        return (GAP_ERR_ILL_MODE);
399
400    if (p_inq_parms->duration < GAP_MIN_INQUIRY_LEN   ||
401        p_inq_parms->duration > GAP_MAX_INQUIRY_LENGTH)
402        return (GAP_ERR_ILL_INQ_TIME);
403
404    /*** get a control block for this operation ***/
405    if ((p_cb = gap_allocate_cb()) != NULL)
406    {
407        p_cb->gap_cback = p_cmpl_cb;
408        p_cb->gap_inq_rslt_cback = p_results_cb;
409        p_cb->event = GAP_EVT_INQUIRY_COMPLETE; /* Return event expected */
410
411        btm_status = BTM_StartInquiry(p_inq_parms, gap_inq_results_cb,
412                        (tBTM_CMPL_CB *) gap_cb.btm_cback[p_cb->index]);
413
414        /* convert the error code into a GAP code and check the results for any errors */
415        if ((retval = gap_convert_btm_status (btm_status)) != GAP_CMD_INITIATED)
416            gap_free_cb(p_cb);      /* Error starting the inquiry */
417    }
418    else
419        retval = GAP_ERR_NO_CTRL_BLK;
420
421    return (retval);
422}
423
424
425/*******************************************************************************
426**
427** Function         GAP_StartPeriodicInquiry
428**
429** Description      This function initiates a periodic inquiry.
430**
431** Parameters:      p_inqparms - pointer to the inquiry information
432**                      mode - GAP_GENERAL_INQUIRY or GAP_LIMITED_INQUIRY inquiry
433**                      duration - length in 1.28 sec intervals
434**                      max_resps - maximum amount of devices to search for before ending the inquiry
435**                      filter_cond_type - GAP_CLR_INQUIRY_FILTER, GAP_FILTER_COND_DEVICE_CLASS, or
436**                                         GAP_FILTER_COND_BD_ADDR
437**                      filter_cond - value for the filter (based on filter_cond_type)
438**
439**                  min_time    - Minimum amount of time between consecutive inquiries.
440**                                The value is in 1.28 second intervals.
441**                                Range: 0x0002 - 0xFFFE (2.56 - 83883.52 seconds)
442**
443**                  max_time    - Maximum amount of time between consecutive inquiries.
444**                                The value is in 1.28 sec intervals.
445**                                Range: 0x0003 - 0xFFFF (3.84 - 83884.8 seconds)
446**
447**                  p_results_cb - Pointer to the callback routine which gets called
448**                                 upon receipt of an inquiry result. If this field is
449**                                 NULL, the application is not notified.
450**
451**
452** Returns          BT_PASS (0) if successful,
453**                  GAP_ERR_ILL_MODE if a bad mode parameter was passed
454**                  GAP_ERR_ILL_INQ_TIME if a bad interval or duration was passed
455**                  GAP_ERR_NO_CTRL_BLK if out of control blocks
456**                  GAP_ERR_ILL_PARM if a bad parameter was detected in BTM
457**                  GAP_ERR_BUSY if the device already has an iquiry active
458**                  GAP_DEVICE_NOT_UP if the device is not initialized yet
459**                  GAP_ERR_PROCESSING if any other BTM error was returned
460**
461*******************************************************************************/
462
463UINT16 GAP_StartPeriodicInquiry (tGAP_INQ_PARMS *p_inq_parms, UINT16 min_time,
464                                 UINT16 max_time, tGAP_CALLBACK *p_results_cb)
465{
466    tGAP_INFO   *p_cb;
467    tBTM_STATUS  btm_status;
468    UINT16       retval = BT_PASS;
469
470    /*** Make sure the parameters are valid before continuing ***/
471    if (p_inq_parms->mode != GAP_GENERAL_INQUIRY && p_inq_parms->mode != GAP_LIMITED_INQUIRY)
472        return (GAP_ERR_ILL_MODE);
473
474    if (p_inq_parms->duration < GAP_MIN_INQUIRY_LEN     ||
475        p_inq_parms->duration > GAP_MAX_INQUIRY_LENGTH  ||
476        min_time <= p_inq_parms->duration               ||
477        min_time < GAP_PER_INQ_MIN_MIN_PERIOD           ||
478        min_time > GAP_PER_INQ_MAX_MIN_PERIOD           ||
479        max_time <= min_time                            ||
480        max_time < GAP_PER_INQ_MIN_MAX_PERIOD)
481    {
482        return (GAP_ERR_ILL_INQ_TIME);
483    }
484
485    /*** get a control block for this operation ***/
486    if ((p_cb = gap_allocate_cb()) != NULL)
487    {
488        p_cb->gap_inq_rslt_cback = p_results_cb;
489        p_cb->event = GAP_EVT_INQUIRY_COMPLETE; /* mark the inquiry event active */
490
491        btm_status = BTM_SetPeriodicInquiryMode(p_inq_parms, max_time, min_time,
492                                                gap_inq_results_cb);
493
494        /* convert the error code into a GAP code and check the results for any errors */
495        if ((retval = gap_convert_btm_status (btm_status)) != GAP_CMD_INITIATED)
496            gap_free_cb(p_cb);      /* Error starting the inquiry */
497    }
498    else
499        retval = GAP_ERR_NO_CTRL_BLK;
500
501    return (retval);
502}
503
504
505/*******************************************************************************
506**
507** Function         GAP_CancelInquiry
508**
509** Description      This function cancels a single inquiry (if in progress)
510**
511** Parameters:      None
512**
513** Returns          BOOLEAN (TRUE if successful, otherwise FALSE)
514**
515*******************************************************************************/
516UINT16 GAP_CancelInquiry(void)
517{
518    tGAP_INFO   *p_cb = &gap_cb.blk[0];
519    UINT8        x;
520    tBTM_STATUS  btm_status;
521    UINT16       status;
522
523    btm_status = BTM_CancelInquiry();
524    if ((status = gap_convert_btm_status (btm_status)) == BT_PASS)
525    {
526        /* Free the control block that is waiting for the inquiry complete event */
527        for (x = 0; x < GAP_MAX_BLOCKS; x++, p_cb++)
528        {
529            if (p_cb->in_use && p_cb->event == GAP_EVT_INQUIRY_COMPLETE)
530            {
531                gap_free_cb(p_cb);
532                return (BT_PASS);
533            }
534        }
535
536        /* If here the control block was not found */
537        status = GAP_ERR_NO_CTRL_BLK;
538    }
539
540    return (status);
541}
542
543
544/*******************************************************************************
545**
546** Function         GAP_CancelPeriodicInquiry
547**
548** Description      This function cancels a periodic inquiry (if in progress)
549**
550** Parameters:      None
551**
552** Returns          BOOLEAN: (TRUE if successful, otherwise FALSE)
553**
554*******************************************************************************/
555UINT16 GAP_CancelPeriodicInquiry(void)
556{
557    tGAP_INFO   *p_cb = &gap_cb.blk[0];
558    UINT8        x;
559    tBTM_STATUS  btm_status;
560    UINT16       status;
561
562    btm_status = BTM_CancelPeriodicInquiry();
563    if ((status = gap_convert_btm_status (btm_status)) == BT_PASS)
564    {
565        /* Free the control block that is waiting for the inquiry complete event */
566        for (x = 0; x < GAP_MAX_BLOCKS; x++, p_cb++)
567        {
568            if (p_cb->in_use && p_cb->event == GAP_EVT_INQUIRY_COMPLETE)
569            {
570                gap_free_cb(p_cb);
571                return (BT_PASS);
572            }
573        }
574
575        /* If here the control block was not found */
576        status = GAP_ERR_NO_CTRL_BLK;
577    }
578
579    return (status);
580}
581
582
583/*******************************************************************************
584**
585** Function         GAP_GetFirstInquiryResult
586**
587** Description      This function retrieves the first valid inquiry result.
588**
589** Parameters:      p_results - pointer to the inquiry results
590**
591** Returns          BT_PASS (0) if successful, or a non-zero error code
592**                  GAP_EOINQDB if no more entries in the database.
593**
594*******************************************************************************/
595UINT16 GAP_GetFirstInquiryResult(tGAP_INQ_RESULTS *p_results)
596{
597    UINT8 *ptr;
598
599    gap_cb.cur_inqptr = BTM_InqFirstResult();
600
601    if (gap_cb.cur_inqptr != NULL)
602    {
603        memcpy(p_results, &gap_cb.cur_inqptr->results, sizeof(tBTM_INQ_RESULTS));
604
605        ptr = (UINT8 *)gap_cb.cur_inqptr->results.remote_bd_addr;
606        GAP_TRACE_EVENT6("GAP_GetFirstInqResult %02x%02x%02x%02x%02x%02x",
607                    ptr[0],ptr[1],ptr[2],ptr[3],ptr[4],ptr[5]);
608        return(BT_PASS);
609    }
610    else
611    {
612        GAP_TRACE_EVENT0("GAP_FirstInqResults:  No BD_ADDRs Found");
613        memset(p_results, 0, sizeof(tBTM_INQ_RESULTS));
614        return(GAP_EOINQDB);
615    }
616}
617
618
619/*******************************************************************************
620**
621** Function         GAP_GetNextInquiryResult
622**
623** Description      This function retrieves the next valid inquiry result.
624**
625** Parameters:      p_results  - pointer to the inquiry results
626**
627** Returns          BT_PASS (0) if successful, or a non-zero status code
628**                  GAP_EOINQDB if no more entries in the database.
629**
630*******************************************************************************/
631UINT16 GAP_GetNextInquiryResult(tGAP_INQ_RESULTS *p_results)
632{
633    UINT8 *ptr;
634
635    /*** if the current inquiry db pointer is NULL then call the first entry ***/
636    if (gap_cb.cur_inqptr)
637    {
638        gap_cb.cur_inqptr = BTM_InqNextResult(gap_cb.cur_inqptr);
639        if (gap_cb.cur_inqptr != NULL)
640        {
641            memcpy(p_results, &gap_cb.cur_inqptr->results,
642                   sizeof(tGAP_INQ_RESULTS));
643
644            ptr = (UINT8 *)gap_cb.cur_inqptr->results.remote_bd_addr;
645            GAP_TRACE_EVENT6("GAP_GetNextInqResult %02x%02x%02x%02x%02x%02x",
646                        ptr[0],ptr[1],ptr[2],ptr[3],ptr[4],ptr[5]);
647
648            return(BT_PASS);
649        }
650        else
651        {
652            GAP_TRACE_EVENT0("GAP_NextInqResults:  No BD_ADDRs Found");
653            memset(p_results, 0, sizeof(tBTM_INQ_RESULTS));
654            return(GAP_EOINQDB);
655        }
656    }
657    else
658        return (GAP_GetFirstInquiryResult(p_results));
659}
660
661
662/*******************************************************************************
663**
664** Function         GAP_ReadLocalDeviceInfo
665**
666** Description      This function retrieves local device information to the caller.
667**
668** Parameters:      name        - (output) pointer to the UTF-8 encoded string representing
669**                                the device name.
670**
671**                  addr        - (output) pointer to the Bluetooth device address (BD_ADDR).
672**
673**                  verinfo     - (output) pointer to the LMP version information.
674**
675**                  features    - (output) pointer to the LMP features for the device.
676**
677**                  NOTE:  Return parameters that are set to NULL are not retrieved.
678**
679** Returns          BT_PASS (0) if successful, or a non-zero error code
680**
681*******************************************************************************/
682
683UINT16 GAP_ReadLocalDeviceInfo(UINT8 *name, BD_ADDR *addr, tGAP_LMP_VERSION *verinfo,
684                               tGAP_LMP_FEATURES *features)
685{
686    return (GAP_UNSUPPORTED);
687}
688
689
690
691/*******************************************************************************
692**
693** Function         GAP_GetRemoteDeviceName
694**
695** Description      The remote name is retrieved from the specified remote device.  If
696**                  GAP_CMD_INITIATED is returned by the function, the command was
697**                  successfully sent to the controller.  The GAP_EVT_NAME_RESP event
698**                  is passed in the callback when the remote device name has been retrieved.
699**
700** Parameters:      addr        - The Bluetooth device address (BD_ADDR) of the remote
701**                                device.
702**
703**                  callback    - pointer to the callback which is called after the
704**                                remote device has been retrieved.
705**                                p_data in the callback points to the structure containing the
706**                                status, device name length, and the UTF-8 encoded
707**                                device name. (type tBTM_REMOTE_DEV_NAME)
708**                                The event field in the callback is set to GAP_EVT_REM_NAME_COMPLETE.
709**  The callback is not called unless (GAP_CMD_INITIATED) is returned.
710**
711**
712** Returns
713**                  GAP_CMD_INITIATED if remote search successfully initiated
714**                  GAP_ERR_BUSY if a remote name request is already in progress,
715**                  GAP_ERR_NO_CTRL_BLK if out of control blocks (too many commands pending)
716**                  GAP_BAD_BD_ADDR if the device address is bad,
717**                  GAP_DEVICE_NOT_UP if the device has not been initialized yet
718**                  GAP_ERR_PROCESSING if any other BTM error has been returned
719**
720*******************************************************************************/
721UINT16 GAP_GetRemoteDeviceName (BD_ADDR addr, tGAP_CALLBACK *callback)
722{
723    tGAP_INFO   *p_cb;
724    UINT16       retval;
725    tBTM_STATUS  btm_status;
726
727    if ((p_cb = gap_allocate_cb()) != NULL)
728    {
729        p_cb->gap_cback = callback;
730        p_cb->event = GAP_EVT_REM_NAME_COMPLETE;     /* Return event expected */
731
732        btm_status = BTM_ReadRemoteDeviceName (addr, gap_cb.btm_cback[p_cb->index]);
733
734        /* If the name was not returned immediately, or if an error occurred, release the control block */
735        if ((retval = gap_convert_btm_status (btm_status)) != GAP_CMD_INITIATED)
736            gap_free_cb (p_cb);
737    }
738    else
739        retval = GAP_ERR_NO_CTRL_BLK;
740
741    return (retval);
742}
743
744/*******************************************************************************
745**
746** Function         GAP_SetDeviceClass
747**
748** Description      This function updates the local Device Class.
749**
750** Parameters:
751**                  p_cod   - Pointer to the device class to set to
752**
753**                  cmd     - the fields of the device class to update.
754**                            GAP_SET_COD_MAJOR_MINOR, - overwrite major, minor class
755**                            GAP_SET_COD_SERVICE_CLASS - set the bits in the input
756**                            GAP_CLR_COD_SERVICE_CLASS - clear the bits in the input
757**                            GAP_SET_COD_ALL - overwrite major, minor, set the bits in service class
758**                            GAP_INIT_COD - overwrite major, minor, and service class
759**
760** Returns          BT_PASS (0) if successful,
761**                  GAP_ERR_BUSY if a discovery is already in progress
762**                  GAP_ERR_ILL_PARM if an illegal parameter was detected
763**                  GAP_ERR_PROCESSING if any other BTM error has been returned
764**
765*******************************************************************************/
766UINT16 GAP_SetDeviceClass(tGAP_COD *p_cod, UINT8 cmd)
767{
768    tBTM_STATUS btm_status;
769    UINT8 *dev;
770    UINT16 service;
771    UINT8  minor, major;
772    DEV_CLASS dev_class;
773
774    dev = BTM_ReadDeviceClass();
775    BTM_COD_SERVICE_CLASS( service, dev );
776    BTM_COD_MINOR_CLASS(minor, dev );
777    BTM_COD_MAJOR_CLASS(major, dev );
778
779    switch(cmd)
780    {
781    case GAP_SET_COD_MAJOR_MINOR:
782        minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
783        major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
784        break;
785
786    case GAP_SET_COD_SERVICE_CLASS:
787        /* clear out the bits that is not SERVICE_CLASS bits */
788        p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
789        service = service | p_cod->service;
790        break;
791
792    case GAP_CLR_COD_SERVICE_CLASS:
793        p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
794        service = service & (~p_cod->service);
795        break;
796
797    case GAP_SET_COD_ALL:
798        minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
799        major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
800        p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
801        service = service | p_cod->service;
802        break;
803
804    case GAP_INIT_COD:
805        minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
806        major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
807        service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
808        break;
809
810    default:
811        return GAP_ERR_ILL_PARM;
812    }
813
814    /* convert the fields into the device class type */
815    FIELDS_TO_COD(dev_class, minor, major, service);
816
817    btm_status = BTM_SetDeviceClass(dev_class);
818    return (gap_convert_btm_status (btm_status));
819}
820
821/*******************************************************************************
822**
823** Function         GAP_ReadDeviceClass
824**
825** Description      This function reads the local Device Class.
826**
827** Parameters:
828**
829** Returns          PASS
830**
831*******************************************************************************/
832UINT16   GAP_ReadDeviceClass(tGAP_COD *p_cod)
833{
834    UINT8 *dev;
835
836    dev = BTM_ReadDeviceClass();
837
838    BTM_COD_SERVICE_CLASS( p_cod->service, dev );
839    BTM_COD_MINOR_CLASS( p_cod->minor, dev );
840    BTM_COD_MAJOR_CLASS( p_cod->major, dev );
841
842    return (BT_PASS);
843}
844
845/*******************************************************************************
846**
847** Function         GAP_SetTraceLevel
848**
849** Description      This function sets the trace level for GAP.  If called with
850**                  a value of 0xFF, it simply returns the current trace level.
851**
852** Returns          The new or current trace level
853**
854*******************************************************************************/
855UINT8 GAP_SetTraceLevel (UINT8 new_level)
856{
857    if (new_level != 0xFF)
858        gap_cb.trace_level = new_level;
859
860    return (gap_cb.trace_level);
861}
862
863/*******************************************************************************
864**
865** Function         GAP_Init
866**
867** Description      Initializes the control blocks used by GAP.
868**
869**                  This routine should not be called except once per
870**                      stack invocation.
871**
872** Returns          Nothing
873**
874*******************************************************************************/
875void GAP_Init(void)
876{
877    memset (&gap_cb, 0, sizeof (tGAP_CB));
878
879    /*** Initialize the callbacks for BTM; Needs to be one per GAP_MAX_BLOCKS ***/
880    gap_cb.btm_cback[0] = gap_btm_cback0;
881#if GAP_MAX_BLOCKS > 1
882    gap_cb.btm_cback[1] = gap_btm_cback1;
883#endif
884#if GAP_MAX_BLOCKS > 2
885    gap_cb.btm_cback[2] = gap_btm_cback2;
886#endif
887
888#if defined(GAP_INITIAL_TRACE_LEVEL)
889    gap_cb.trace_level = GAP_INITIAL_TRACE_LEVEL;
890#else
891    gap_cb.trace_level = BT_TRACE_LEVEL_NONE;    /* No traces */
892#endif
893
894    /* Initialize the connection control block if included in build */
895#if GAP_CONN_INCLUDED == TRUE
896    gap_conn_init();
897#endif  /* GAP_CONN_INCLUDED */
898
899#if BLE_INCLUDED == TRUE
900    gap_attr_db_init();
901#endif
902}
903
904