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