Home | History | Annotate | Download | only in srvc
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-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 "bt_target.h"
     20 #include "bt_utils.h"
     21 #include "gatt_api.h"
     22 #include "gatt_int.h"
     23 #include "srvc_eng_int.h"
     24 #include "srvc_dis_int.h"
     25 
     26 #if BLE_INCLUDED == TRUE
     27 
     28 #define DIS_MAX_NUM_INC_SVR       0
     29 #define DIS_MAX_CHAR_NUM          9
     30 #define DIS_MAX_ATTR_NUM          (DIS_MAX_CHAR_NUM * 2 + DIS_MAX_NUM_INC_SVR + 1)
     31 
     32 #ifndef DIS_ATTR_DB_SIZE
     33 #define DIS_ATTR_DB_SIZE      GATT_DB_MEM_SIZE(DIS_MAX_NUM_INC_SVR, DIS_MAX_CHAR_NUM, 0)
     34 #endif
     35 
     36 #define UINT64_TO_STREAM(p, u64) {*(p)++ = (UINT8)(u64);       *(p)++ = (UINT8)((u64) >> 8);*(p)++ = (UINT8)((u64) >> 16); *(p)++ = (UINT8)((u64) >> 24); \
     37                                     *(p)++ = (UINT8)((u64) >> 32); *(p)++ = (UINT8)((u64) >> 40);*(p)++ = (UINT8)((u64) >> 48); *(p)++ = (UINT8)((u64) >> 56);}
     38 
     39 #define STREAM_TO_UINT64(u64, p) {u64 = (((UINT64)(*(p))) + ((((UINT64)(*((p) + 1)))) << 8) + ((((UINT64)(*((p) + 2)))) << 16) + ((((UINT64)(*((p) + 3)))) << 24) \
     40                                   + ((((UINT64)(*((p) + 4)))) << 32) + ((((UINT64)(*((p) + 5)))) << 40) + ((((UINT64)(*((p) + 6)))) << 48) + ((((UINT64)(*((p) + 7)))) << 56)); (p) += 8;}
     41 
     42 
     43 
     44 static const UINT16  dis_attr_uuid[DIS_MAX_CHAR_NUM] =
     45 {
     46     GATT_UUID_SYSTEM_ID,
     47     GATT_UUID_MODEL_NUMBER_STR,
     48     GATT_UUID_SERIAL_NUMBER_STR,
     49     GATT_UUID_FW_VERSION_STR,
     50     GATT_UUID_HW_VERSION_STR,
     51     GATT_UUID_SW_VERSION_STR,
     52     GATT_UUID_MANU_NAME,
     53     GATT_UUID_IEEE_DATA,
     54     GATT_UUID_PNP_ID
     55 };
     56 
     57 tDIS_CB dis_cb;
     58 
     59 static tDIS_ATTR_MASK dis_uuid_to_attr(UINT16 uuid)
     60 {
     61     switch (uuid)
     62     {
     63         case GATT_UUID_SYSTEM_ID:
     64             return DIS_ATTR_SYS_ID_BIT;
     65         case GATT_UUID_MODEL_NUMBER_STR:
     66             return DIS_ATTR_MODEL_NUM_BIT;
     67         case GATT_UUID_SERIAL_NUMBER_STR:
     68             return DIS_ATTR_SERIAL_NUM_BIT;
     69         case GATT_UUID_FW_VERSION_STR:
     70             return DIS_ATTR_FW_NUM_BIT;
     71         case GATT_UUID_HW_VERSION_STR:
     72             return DIS_ATTR_HW_NUM_BIT;
     73         case GATT_UUID_SW_VERSION_STR:
     74             return DIS_ATTR_SW_NUM_BIT;
     75         case GATT_UUID_MANU_NAME:
     76             return DIS_ATTR_MANU_NAME_BIT;
     77         case GATT_UUID_IEEE_DATA:
     78             return DIS_ATTR_IEEE_DATA_BIT;
     79         case GATT_UUID_PNP_ID:
     80             return DIS_ATTR_PNP_ID_BIT;
     81         default:
     82             return 0;
     83     };
     84 }
     85 
     86 /*******************************************************************************
     87 **   dis_valid_handle_range
     88 **
     89 **   validate a handle to be a DIS attribute handle or not.
     90 *******************************************************************************/
     91 BOOLEAN dis_valid_handle_range(UINT16 handle)
     92 {
     93     if (handle >= dis_cb.service_handle && handle <= dis_cb.max_handle)
     94         return TRUE;
     95     else
     96         return FALSE;
     97 }
     98 /*******************************************************************************
     99 **   dis_write_attr_value
    100 **
    101 **   Process write DIS attribute request.
    102 *******************************************************************************/
    103 UINT8 dis_write_attr_value(tGATT_WRITE_REQ * p_data, tGATT_STATUS *p_status)
    104 {
    105     UNUSED(p_data);
    106 
    107     *p_status = GATT_WRITE_NOT_PERMIT;
    108     return SRVC_ACT_RSP;
    109 }
    110 /*******************************************************************************
    111 **   DIS Attributes Database Server Request callback
    112 *******************************************************************************/
    113 UINT8 dis_read_attr_value (UINT8 clcb_idx, UINT16 handle, tGATT_VALUE *p_value,
    114                            BOOLEAN is_long, tGATT_STATUS *p_status)
    115 {
    116     tDIS_DB_ENTRY   *p_db_attr = dis_cb.dis_attr;
    117     UINT8           *p = p_value->value, i, *pp;
    118     UINT16          offset = p_value->offset;
    119     UINT8           act = SRVC_ACT_RSP;
    120     tGATT_STATUS    st = GATT_NOT_FOUND;
    121     UNUSED(clcb_idx);
    122 
    123     for (i = 0; i < DIS_MAX_CHAR_NUM; i ++, p_db_attr ++)
    124     {
    125         if (handle == p_db_attr->handle)
    126         {
    127             if ((p_db_attr->uuid == GATT_UUID_PNP_ID || p_db_attr->uuid == GATT_UUID_SYSTEM_ID)&&
    128                 is_long == TRUE)
    129             {
    130                 st = GATT_NOT_LONG;
    131                 break;
    132             }
    133             st = GATT_SUCCESS;
    134 
    135             switch (p_db_attr->uuid)
    136             {
    137                 case GATT_UUID_MANU_NAME:
    138                 case GATT_UUID_MODEL_NUMBER_STR:
    139                 case GATT_UUID_SERIAL_NUMBER_STR:
    140                 case GATT_UUID_FW_VERSION_STR:
    141                 case GATT_UUID_HW_VERSION_STR:
    142                 case GATT_UUID_SW_VERSION_STR:
    143                 case GATT_UUID_IEEE_DATA:
    144                     pp = dis_cb.dis_value.data_string[p_db_attr->uuid - GATT_UUID_MODEL_NUMBER_STR];
    145                     if (pp != NULL)
    146                     {
    147                         if (strlen ((char *)pp) > GATT_MAX_ATTR_LEN)
    148                             p_value->len = GATT_MAX_ATTR_LEN;
    149                         else
    150                             p_value->len = (UINT16)strlen ((char *)pp);
    151                     }
    152                     else
    153                         p_value->len = 0;
    154 
    155                     if (offset > p_value->len)
    156                     {
    157                         st = GATT_INVALID_OFFSET;
    158                         break;
    159                     }
    160                     else
    161                     {
    162                         p_value->len -= offset;
    163                         pp += offset;
    164                         ARRAY_TO_STREAM(p, pp, p_value->len);
    165                         GATT_TRACE_EVENT("GATT_UUID_MANU_NAME len=0x%04x", p_value->len);
    166                     }
    167                     break;
    168 
    169 
    170                 case GATT_UUID_SYSTEM_ID:
    171                     UINT64_TO_STREAM(p, dis_cb.dis_value.system_id); /* int_min */
    172                     p_value->len = DIS_SYSTEM_ID_SIZE;
    173                     break;
    174 
    175                 case  GATT_UUID_PNP_ID:
    176                     UINT8_TO_STREAM(p, dis_cb.dis_value.pnp_id.vendor_id_src);
    177                     UINT16_TO_STREAM(p, dis_cb.dis_value.pnp_id.vendor_id);
    178                     UINT16_TO_STREAM(p, dis_cb.dis_value.pnp_id.product_id);
    179                     UINT16_TO_STREAM(p, dis_cb.dis_value.pnp_id.product_version);
    180                     p_value->len = DIS_PNP_ID_SIZE;
    181                     break;
    182 
    183             }
    184             break;
    185         }
    186     }
    187     *p_status = st;
    188     return act;
    189 }
    190 
    191 /*******************************************************************************
    192 **
    193 ** Function         dis_gatt_c_read_dis_value_cmpl
    194 **
    195 ** Description      Client read DIS database complete callback.
    196 **
    197 ** Returns          void
    198 **
    199 *******************************************************************************/
    200 void dis_gatt_c_read_dis_value_cmpl(UINT16 conn_id)
    201 {
    202     tSRVC_CLCB *p_clcb =  srvc_eng_find_clcb_by_conn_id(conn_id);
    203 
    204     dis_cb.dis_read_uuid_idx = 0xff;
    205 
    206     srvc_eng_release_channel(conn_id);
    207 
    208     if (dis_cb.p_read_dis_cback && p_clcb)
    209     {
    210         GATT_TRACE_ERROR("dis_gatt_c_read_dis_value_cmpl: attr_mask = 0x%04x", p_clcb->dis_value.attr_mask);
    211         GATT_TRACE_EVENT("calling p_read_dis_cbackd");
    212 
    213         (*dis_cb.p_read_dis_cback)(p_clcb->bda, &p_clcb->dis_value);
    214         dis_cb.p_read_dis_cback=NULL;
    215     }
    216 
    217 }
    218 
    219 /*******************************************************************************
    220 **
    221 ** Function         dis_gatt_c_read_dis_req
    222 **
    223 ** Description      Read remote device DIS attribute request.
    224 **
    225 ** Returns          void
    226 **
    227 *******************************************************************************/
    228 BOOLEAN dis_gatt_c_read_dis_req(UINT16 conn_id)
    229 {
    230     tGATT_READ_PARAM   param;
    231 
    232     memset(&param, 0, sizeof(tGATT_READ_PARAM));
    233 
    234     param.service.uuid.len       = LEN_UUID_16;
    235     param.service.s_handle       = 1;
    236     param.service.e_handle       = 0xFFFF;
    237     param.service.auth_req       = 0;
    238 
    239     while (dis_cb.dis_read_uuid_idx < DIS_MAX_CHAR_NUM)
    240     {
    241         if (dis_uuid_to_attr(dis_attr_uuid[dis_cb.dis_read_uuid_idx]) &
    242                 dis_cb.request_mask)
    243         {
    244              param.service.uuid.uu.uuid16 = dis_attr_uuid[dis_cb.dis_read_uuid_idx];
    245 
    246              if (GATTC_Read(conn_id, GATT_READ_BY_TYPE, &param) == GATT_SUCCESS)
    247                  return TRUE;
    248 
    249             GATT_TRACE_ERROR ("Read DISInfo: 0x%04x GATT_Read Failed", param.service.uuid.uu.uuid16);
    250         }
    251 
    252         dis_cb.dis_read_uuid_idx++;
    253     }
    254 
    255     dis_gatt_c_read_dis_value_cmpl(conn_id);
    256 
    257     return(FALSE);
    258 }
    259 
    260 /*******************************************************************************
    261 **
    262 ** Function         dis_c_cmpl_cback
    263 **
    264 ** Description      Client operation complete callback.
    265 **
    266 ** Returns          void
    267 **
    268 *******************************************************************************/
    269 void dis_c_cmpl_cback (tSRVC_CLCB *p_clcb, tGATTC_OPTYPE op,
    270                               tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
    271 {
    272     UINT16      read_type = dis_attr_uuid[dis_cb.dis_read_uuid_idx];
    273     UINT8       *pp = NULL, *p_str;
    274     UINT16      conn_id = p_clcb->conn_id;
    275 
    276     GATT_TRACE_EVENT ("dis_c_cmpl_cback() - op_code: 0x%02x  status: 0x%02x  \
    277                         read_type: 0x%04x", op, status, read_type);
    278 
    279     if (op != GATTC_OPTYPE_READ)
    280         return;
    281 
    282     if (p_data != NULL && status == GATT_SUCCESS)
    283     {
    284         pp = p_data->att_value.value;
    285 
    286         switch (read_type)
    287         {
    288             case GATT_UUID_SYSTEM_ID:
    289                 GATT_TRACE_EVENT ("DIS_ATTR_SYS_ID_BIT");
    290                 if (p_data->att_value.len == DIS_SYSTEM_ID_SIZE)
    291                 {
    292                     p_clcb->dis_value.attr_mask |= DIS_ATTR_SYS_ID_BIT;
    293                     /* save system ID*/
    294                     STREAM_TO_UINT64 (p_clcb->dis_value.system_id, pp);
    295                 }
    296                 break;
    297 
    298             case GATT_UUID_PNP_ID:
    299                 if (p_data->att_value.len == DIS_PNP_ID_SIZE)
    300                 {
    301                     p_clcb->dis_value.attr_mask |= DIS_ATTR_PNP_ID_BIT;
    302                     STREAM_TO_UINT8 (p_clcb->dis_value.pnp_id.vendor_id_src, pp);
    303                     STREAM_TO_UINT16 (p_clcb->dis_value.pnp_id.vendor_id, pp);
    304                     STREAM_TO_UINT16 (p_clcb->dis_value.pnp_id.product_id, pp);
    305                     STREAM_TO_UINT16 (p_clcb->dis_value.pnp_id.product_version, pp);
    306                 }
    307                 break;
    308 
    309             case GATT_UUID_MODEL_NUMBER_STR:
    310             case GATT_UUID_SERIAL_NUMBER_STR:
    311             case GATT_UUID_FW_VERSION_STR:
    312             case GATT_UUID_HW_VERSION_STR:
    313             case GATT_UUID_SW_VERSION_STR:
    314             case GATT_UUID_MANU_NAME:
    315             case GATT_UUID_IEEE_DATA:
    316                 p_str = p_clcb->dis_value.data_string[read_type - GATT_UUID_MODEL_NUMBER_STR];
    317                 if (p_str != NULL)
    318                     GKI_freebuf(p_str);
    319                 if ((p_str = (UINT8 *)GKI_getbuf((UINT16)(p_data->att_value.len + 1))) != NULL)
    320                 {
    321                     p_clcb->dis_value.attr_mask |= dis_uuid_to_attr(read_type);
    322                     memcpy(p_str, p_data->att_value.value, p_data->att_value.len);
    323                     p_str[p_data->att_value.len] = 0;
    324                     p_clcb->dis_value.data_string[read_type - GATT_UUID_MODEL_NUMBER_STR] = p_str;
    325                 }
    326                 break;
    327 
    328             default:
    329                     break;
    330 
    331                 break;
    332         }/* end switch */
    333     }/* end if */
    334 
    335     dis_cb.dis_read_uuid_idx ++;
    336 
    337     dis_gatt_c_read_dis_req(conn_id);
    338 }
    339 
    340 
    341 /*******************************************************************************
    342 **
    343 ** Function         DIS_SrInit
    344 **
    345 ** Description      Initialize the Device Information Service Server.
    346 **
    347 *******************************************************************************/
    348 tDIS_STATUS DIS_SrInit (tDIS_ATTR_MASK dis_attr_mask)
    349 {
    350     tBT_UUID          uuid = {LEN_UUID_16, {UUID_SERVCLASS_DEVICE_INFO}};
    351     UINT16            i = 0;
    352     tGATT_STATUS      status;
    353     tDIS_DB_ENTRY        *p_db_attr = &dis_cb.dis_attr[0];
    354 
    355     if (dis_cb.enabled)
    356     {
    357         GATT_TRACE_ERROR("DIS already initalized");
    358         return DIS_SUCCESS;
    359     }
    360 
    361     memset(&dis_cb, 0, sizeof(tDIS_CB));
    362 
    363     dis_cb.service_handle = GATTS_CreateService (srvc_eng_cb.gatt_if , &uuid, 0, DIS_MAX_ATTR_NUM, TRUE);
    364 
    365     if (dis_cb.service_handle == 0)
    366     {
    367         GATT_TRACE_ERROR("Can not create service, DIS_Init failed!");
    368         return GATT_ERROR;
    369     }
    370     dis_cb.max_handle = dis_cb.service_handle + DIS_MAX_ATTR_NUM;
    371 
    372     while (dis_attr_mask != 0 && i < DIS_MAX_CHAR_NUM)
    373     {
    374         /* add Manufacturer name
    375         */
    376         uuid.uu.uuid16 = p_db_attr->uuid = dis_attr_uuid[i];
    377         p_db_attr->handle  = GATTS_AddCharacteristic(dis_cb.service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ);
    378         GATT_TRACE_DEBUG ("DIS_SrInit:  handle of new attribute 0x%04 = x%d", uuid.uu.uuid16, p_db_attr->handle  );
    379         p_db_attr ++;
    380         i ++;
    381         dis_attr_mask >>= 1;
    382     }
    383 
    384     /* start service
    385     */
    386     status = GATTS_StartService (srvc_eng_cb.gatt_if, dis_cb.service_handle, GATT_TRANSPORT_LE_BR_EDR);
    387 
    388     dis_cb.enabled = TRUE;
    389 
    390     return (tDIS_STATUS) status;
    391 }
    392 /*******************************************************************************
    393 **
    394 ** Function         DIS_SrUpdate
    395 **
    396 ** Description      Update the DIS server attribute values
    397 **
    398 *******************************************************************************/
    399 tDIS_STATUS DIS_SrUpdate(tDIS_ATTR_BIT dis_attr_bit, tDIS_ATTR *p_info)
    400 {
    401     UINT8           i = 1;
    402     tDIS_STATUS     st = DIS_SUCCESS;
    403 
    404     if (dis_attr_bit & DIS_ATTR_SYS_ID_BIT)
    405     {
    406         dis_cb.dis_value.system_id = p_info->system_id;
    407     }
    408     else if (dis_attr_bit & DIS_ATTR_PNP_ID_BIT)
    409     {
    410         dis_cb.dis_value.pnp_id.vendor_id         = p_info->pnp_id.vendor_id;
    411         dis_cb.dis_value.pnp_id.vendor_id_src     = p_info->pnp_id.vendor_id_src;
    412         dis_cb.dis_value.pnp_id.product_id        = p_info->pnp_id.product_id;
    413         dis_cb.dis_value.pnp_id.product_version   = p_info->pnp_id.product_version;
    414     }
    415     else
    416     {
    417         st = DIS_ILLEGAL_PARAM;
    418 
    419         while (dis_attr_bit && i < (DIS_MAX_CHAR_NUM -1 ))
    420         {
    421             if (dis_attr_bit & (UINT16)(1 << i))
    422             {
    423                 if (dis_cb.dis_value.data_string[i - 1] != NULL)
    424                     GKI_freebuf(dis_cb.dis_value.data_string[i - 1]);
    425 /* coverity[OVERRUN-STATIC] False-positive : when i = 8, (1 << i) == DIS_ATTR_PNP_ID_BIT, and it will never come down here
    426 CID 49902: Out-of-bounds read (OVERRUN_STATIC)
    427 Overrunning static array "dis_cb.dis_value.data_string", with 7 elements, at position 7 with index variable "i".
    428 */
    429                 if ((dis_cb.dis_value.data_string[i - 1] = (UINT8 *)GKI_getbuf((UINT16)(p_info->data_str.len + 1))) != NULL)
    430                 {
    431 
    432                     memcpy(dis_cb.dis_value.data_string[i - 1], p_info->data_str.p_data, p_info->data_str.len);
    433                     dis_cb.dis_value.data_string[i - 1][p_info->data_str.len] = 0; /* make sure null terminate */
    434                     st = DIS_SUCCESS;
    435                 }
    436                 else
    437                     st = DIS_NO_RESOURCES;
    438 
    439                 break;
    440             }
    441             i ++;
    442         }
    443     }
    444     return st;
    445 }
    446 /*******************************************************************************
    447 **
    448 ** Function         DIS_ReadDISInfo
    449 **
    450 ** Description      Read remote device DIS information.
    451 **
    452 ** Returns          void
    453 **
    454 *******************************************************************************/
    455 BOOLEAN DIS_ReadDISInfo(BD_ADDR peer_bda, tDIS_READ_CBACK *p_cback, tDIS_ATTR_MASK mask)
    456 {
    457     UINT16             conn_id;
    458 
    459     /* Initialize the DIS client if it hasn't been initialized already. */
    460     srvc_eng_init();
    461 
    462     /* For now we only handle one at a time */
    463     if (dis_cb.dis_read_uuid_idx != 0xff)
    464         return(FALSE);
    465 
    466     if (p_cback == NULL)
    467         return(FALSE);
    468 
    469     dis_cb.p_read_dis_cback = p_cback;
    470     /* Mark currently active operation */
    471     dis_cb.dis_read_uuid_idx = 0;
    472 
    473     dis_cb.request_mask = mask;
    474 
    475     GATT_TRACE_EVENT ("DIS_ReadDISInfo() - BDA: %08x%04x  cl_read_uuid: 0x%04x",
    476                       (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
    477                       (peer_bda[4]<<8)+peer_bda[5], dis_attr_uuid[dis_cb.dis_read_uuid_idx]);
    478 
    479 
    480     GATT_GetConnIdIfConnected(srvc_eng_cb.gatt_if, peer_bda, &conn_id, BT_TRANSPORT_LE);
    481 
    482     /* need to enhance it as multiple service is needed */
    483     srvc_eng_request_channel(peer_bda, SRVC_ID_DIS);
    484 
    485     if (conn_id == GATT_INVALID_CONN_ID)
    486     {
    487         return GATT_Connect(srvc_eng_cb.gatt_if, peer_bda, TRUE, BT_TRANSPORT_LE);
    488     }
    489 
    490     return dis_gatt_c_read_dis_req(conn_id);
    491 
    492 }
    493 #endif  /* BLE_INCLUDED */
    494 
    495 
    496