Home | History | Annotate | Download | only in co
      1 /*****************************************************************************
      2 **
      3 **  Name:           bta_gattc_co.c
      4 **
      5 **  Description:    This file contains the GATT client call-out
      6 **                  function implementation for Insight.
      7 **
      8 **  Copyright (c) 2010, Broadcom Corp., All Rights Reserved.
      9 **  Broadcom Bluetooth Core. Proprietary and confidential.
     10 **
     11 *****************************************************************************/
     12 
     13 #include "bta_api.h"
     14 
     15 #if( defined BLE_INCLUDED ) && (BLE_INCLUDED == TRUE)
     16 #if( defined BTA_GATT_INCLUDED ) && (BTA_GATT_INCLUDED == TRUE)
     17 
     18 #include <stdlib.h>
     19 #include "gki.h"
     20 #include "bd.h"
     21 #include "bta_gatts_co.h"
     22 
     23 /*****************************************************************************
     24 **  Local type definitions
     25 *****************************************************************************/
     26 
     27 #define BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE 50
     28 
     29 typedef struct
     30 {
     31     BOOLEAN             enable;
     32     UINT8               num_clients;
     33     tBTA_GATTS_SRV_CHG  srv_chg[BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE];
     34 } __attribute__((packed)) btif_gatts_srv_chg_cb_t;
     35 
     36 /*****************************************************************************
     37 **  Static variables
     38 *****************************************************************************/
     39 
     40 static btif_gatts_srv_chg_cb_t btif_gatts_srv_chg_cb;
     41 
     42 /*****************************************************************************
     43 **  Static functions
     44 *****************************************************************************/
     45 
     46 static void btif_gatts_check_init(void)
     47 {
     48     btif_gatts_srv_chg_cb_t *p_cb= &btif_gatts_srv_chg_cb;
     49 
     50     if (!p_cb->enable)
     51     {
     52        memset(p_cb, 0, sizeof(btif_gatts_srv_chg_cb_t));
     53        p_cb->enable = TRUE;
     54     }
     55 }
     56 
     57 static BOOLEAN btif_gatts_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd,
     58                                   tBTA_GATTS_SRV_CHG_REQ *p_req,
     59                                   tBTA_GATTS_SRV_CHG_RSP *p_rsp)
     60 {
     61     BOOLEAN status = TRUE;
     62     BOOLEAN found = FALSE;
     63     UINT8   i, j, idx, last_idx;
     64     btif_gatts_srv_chg_cb_t *p_cb = &btif_gatts_srv_chg_cb;
     65 
     66     btif_gatts_check_init();
     67 
     68     switch (cmd)
     69     {
     70         case BTA_GATTS_SRV_CHG_CMD_ADD_CLIENT:
     71 
     72             if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE)
     73             {
     74                 memcpy(&p_cb->srv_chg[p_cb->num_clients], &p_req->srv_chg, sizeof(tBTA_GATTS_SRV_CHG));
     75                 p_cb->num_clients++;
     76             } else {
     77                 status = FALSE;
     78             }
     79             break;
     80 
     81         case BTA_GATTS_SRV_CHG_CMD_UPDATE_CLIENT:
     82 
     83             for (i=0; i != p_cb->num_clients; ++i)
     84             {
     85                 if (!memcmp(p_cb->srv_chg[i].bda, p_req->srv_chg.bda, sizeof(BD_ADDR)))
     86                 {
     87                     found = TRUE;
     88                     memcpy(&p_cb->srv_chg[i], &p_req->srv_chg, sizeof(tBTA_GATTS_SRV_CHG));
     89                     break;
     90                 }
     91             }
     92 
     93             if (!found)
     94                 status = FALSE;
     95             break;
     96 
     97         case BTA_GATTS_SRV_CHG_CMD_REMOVE_CLIENT:
     98 
     99             for (i=0; i != p_cb->num_clients; ++i)
    100             {
    101                 if (!memcmp(p_cb->srv_chg[i].bda, p_req->srv_chg.bda, sizeof(BD_ADDR)))
    102                 {
    103                     found = TRUE;
    104                     last_idx = p_cb->num_clients - 1;
    105 
    106                     if (i != last_idx )
    107                     {
    108                         /* Update the array so there is no gap */
    109                         for (j=i; j != last_idx; ++j )
    110                         {
    111                             memcpy(&p_cb->srv_chg[j], &p_cb->srv_chg[j+1], sizeof(tBTA_GATTS_SRV_CHG));
    112                         }
    113 
    114                     }
    115 
    116                     /* Reset the last client and update num_clients */
    117                     memset(&p_cb->srv_chg[last_idx], 0, sizeof(tBTA_GATTS_SRV_CHG));
    118                     p_cb->num_clients--;
    119                     break;
    120                 }
    121             }
    122 
    123             if (!found)
    124                 status = FALSE;
    125             break;
    126 
    127         case BTA_GATTS_SRV_CHG_CMD_READ_NUM_CLENTS:
    128             p_rsp->num_clients = p_cb->num_clients;
    129             break;
    130 
    131         case BTA_GATTS_SRV_CHG_CMD_READ_CLENT:
    132             idx = p_req->client_read_index - 1;
    133 
    134             if (idx < p_cb->num_clients )
    135                 memcpy(&p_rsp->srv_chg, &p_cb->srv_chg[idx], sizeof(tBTA_GATTS_SRV_CHG));
    136             else
    137                 status = FALSE;
    138             break;
    139 
    140         default:
    141             status = FALSE;
    142             break;
    143     }
    144 
    145     return status;
    146 }
    147 
    148 /*****************************************************************************
    149 **  Externally called functions
    150 *****************************************************************************/
    151 
    152 void btif_gatts_add_bonded_dev_from_nv(BD_ADDR bda)
    153 {
    154     btif_gatts_srv_chg_cb_t *p_cb= &btif_gatts_srv_chg_cb;
    155     BOOLEAN                 found = FALSE;
    156     UINT8                   i;
    157 
    158     btif_gatts_check_init();
    159 
    160     for (i=0; i != p_cb->num_clients; ++i)
    161     {
    162         if (!memcmp(p_cb->srv_chg[i].bda,  bda, sizeof(BD_ADDR)))
    163         {
    164             found = TRUE;
    165             break;
    166         }
    167     }
    168 
    169     if (!found)
    170     {
    171         if (p_cb->num_clients < BTIF_GATTS_MAX_SRV_CHG_CLT_SIZE)
    172         {
    173             bdcpy(p_cb->srv_chg[p_cb->num_clients].bda, bda);
    174             p_cb->srv_chg[p_cb->num_clients].srv_changed = FALSE;
    175             p_cb->num_clients++;
    176         }
    177     }
    178 }
    179 
    180 /*****************************************************************************
    181 **  Call-out functions
    182 *****************************************************************************/
    183 
    184 /*******************************************************************************
    185 **
    186 ** Function         bta_gatts_co_update_handle_range
    187 **
    188 ** Description      This callout function is executed by GATTS when a GATT server
    189 **                  handle range ios to be added or removed.
    190 **
    191 ** Parameter        is_add: true is to add a handle range; otherwise is to delete.
    192 **                  p_hndl_range: handle range.
    193 **
    194 ** Returns          void.
    195 **
    196 *******************************************************************************/
    197 void bta_gatts_co_update_handle_range(BOOLEAN is_add, tBTA_GATTS_HNDL_RANGE *p_hndl_range)
    198 {
    199 }
    200 
    201 /*******************************************************************************
    202 **
    203 ** Function         bta_gatts_co_srv_chg
    204 **
    205 ** Description      This call-out is to read/write/remove service change related
    206 **                  informaiton. The request consists of the cmd and p_req and the
    207 **                  response is returned in p_rsp
    208 **
    209 ** Parameter        cmd - request command
    210 **                  p_req - request paramters
    211 **                  p_rsp - response data for the request
    212 **
    213 ** Returns          TRUE - if the request is processed successfully and
    214 **                         the response is returned in p_rsp.
    215 **                  FASLE - if the request can not be processed
    216 **
    217 *******************************************************************************/
    218 BOOLEAN bta_gatts_co_srv_chg(tBTA_GATTS_SRV_CHG_CMD cmd,
    219                              tBTA_GATTS_SRV_CHG_REQ *p_req,
    220                              tBTA_GATTS_SRV_CHG_RSP *p_rsp)
    221 {
    222     return FALSE;
    223 }
    224 
    225 /*******************************************************************************
    226 **
    227 ** Function         bta_gatts_co_load_handle_range
    228 **
    229 ** Description      This callout function is executed by GATTS when a GATT server
    230 **                  handle range is requested to be loaded from NV.
    231 **
    232 ** Parameter
    233 **
    234 ** Returns          void.
    235 **
    236 *******************************************************************************/
    237 BOOLEAN bta_gatts_co_load_handle_range(UINT8 index,
    238                                        tBTA_GATTS_HNDL_RANGE *p_handle_range)
    239 {
    240    return FALSE;
    241 }
    242 #endif
    243 #endif
    244