2016-09-26 09:37:39 -04:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2012 Broadcom Corporation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* this file contains GATT database building and query functions
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_target.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-04-20 07:18:05 -04:00
|
|
|
#if BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_trace.h"
|
|
|
|
#include "osi/allocator.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
//#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "gatt_int.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "stack/l2c_api.h"
|
2016-09-26 09:37:39 -04:00
|
|
|
#include "btm_int.h"
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
** L O C A L F U N C T I O N P R O T O T Y P E S *
|
|
|
|
*********************************************************************************/
|
|
|
|
static BOOLEAN allocate_svc_db_buf(tGATT_SVC_DB *p_db);
|
|
|
|
static void *allocate_attr_in_db(tGATT_SVC_DB *p_db, tBT_UUID *p_uuid, tGATT_PERM perm);
|
|
|
|
static BOOLEAN deallocate_attr_in_db(tGATT_SVC_DB *p_db, void *p_attr);
|
|
|
|
static BOOLEAN copy_extra_byte_in_db(tGATT_SVC_DB *p_db, void **p_dst, UINT16 len);
|
|
|
|
|
|
|
|
static BOOLEAN gatts_db_add_service_declaration(tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri);
|
|
|
|
static tGATT_STATUS gatts_send_app_read_request(tGATT_TCB *p_tcb, UINT8 op_code,
|
2017-01-11 11:48:48 -05:00
|
|
|
UINT16 handle, UINT16 offset, UINT32 trans_id, BOOLEAN need_rsp);
|
2017-06-07 08:06:49 -04:00
|
|
|
static BOOLEAN gatts_add_char_desc_value_check (tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_init_service_db
|
|
|
|
**
|
|
|
|
** Description This function initialize a memory space to be a service database.
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** len: size of the memory space.
|
|
|
|
**
|
|
|
|
** Returns Status of te operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
BOOLEAN gatts_init_service_db (tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri,
|
|
|
|
UINT16 s_hdl, UINT16 num_handle)
|
|
|
|
{
|
2017-08-17 09:13:45 -04:00
|
|
|
if (p_db->svc_buffer == NULL) { //in case already alloc
|
2018-07-01 22:59:51 -04:00
|
|
|
p_db->svc_buffer = fixed_queue_new(QUEUE_SIZE_MAX);
|
2017-08-17 09:13:45 -04:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!allocate_svc_db_buf(p_db)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR("gatts_init_service_db failed, no resources\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("gatts_init_service_db\n");
|
|
|
|
GATT_TRACE_DEBUG("s_hdl = %d num_handle = %d\n", s_hdl, num_handle );
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
/* update service database information */
|
|
|
|
p_db->next_handle = s_hdl;
|
|
|
|
p_db->end_handle = s_hdl + num_handle;
|
|
|
|
|
|
|
|
return gatts_db_add_service_declaration(p_db, p_service, is_pri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_init_service_db
|
|
|
|
**
|
|
|
|
** Description This function initialize a memory space to be a service database.
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** len: size of the memory space.
|
|
|
|
**
|
|
|
|
** Returns Status of te operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
2016-11-24 13:10:15 -05:00
|
|
|
tBT_UUID *gatts_get_service_uuid (tGATT_SVC_DB *p_db)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!p_db || !p_db->p_attr_list) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR("service DB empty\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
return NULL;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
return &((tGATT_ATTR16 *)p_db->p_attr_list)->p_value->uuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_check_attr_readability
|
|
|
|
**
|
|
|
|
** Description check attribute readability
|
|
|
|
**
|
|
|
|
** Returns status of operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static tGATT_STATUS gatts_check_attr_readability(tGATT_ATTR16 *p_attr,
|
2016-11-24 13:10:15 -05:00
|
|
|
UINT16 offset,
|
|
|
|
BOOLEAN read_long,
|
|
|
|
tGATT_SEC_FLAG sec_flag,
|
|
|
|
UINT8 key_size)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
|
|
|
UINT16 min_key_size;
|
|
|
|
tGATT_PERM perm = p_attr->permission;
|
|
|
|
|
|
|
|
UNUSED(offset);
|
|
|
|
min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
|
2016-11-24 13:10:15 -05:00
|
|
|
if (min_key_size != 0 ) {
|
|
|
|
min_key_size += 6;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (!(perm & GATT_READ_ALLOWED)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR( "GATT_READ_NOT_PERMIT\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_READ_NOT_PERMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((perm & GATT_READ_AUTH_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_UNAUTHED) &&
|
2016-11-24 13:10:15 -05:00
|
|
|
!(sec_flag & BTM_SEC_FLAG_ENCRYPTED)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR( "GATT_INSUF_AUTHENTICATION\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_INSUF_AUTHENTICATION;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((perm & GATT_READ_MITM_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_AUTHED)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR( "GATT_INSUF_AUTHENTICATION: MITM Required\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_INSUF_AUTHENTICATION;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((perm & GATT_READ_ENCRYPTED_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_ENCRYPTED)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR( "GATT_INSUF_ENCRYPTION\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_INSUF_ENCRYPTION;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if ( (perm & GATT_READ_ENCRYPTED_REQUIRED) && (sec_flag & GATT_SEC_FLAG_ENCRYPTED) && (key_size < min_key_size)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR( "GATT_INSUF_KEY_SIZE\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_INSUF_KEY_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (read_long) {
|
|
|
|
switch (p_attr->uuid) {
|
|
|
|
case GATT_UUID_PRI_SERVICE:
|
|
|
|
case GATT_UUID_SEC_SERVICE:
|
|
|
|
case GATT_UUID_CHAR_DECLARE:
|
|
|
|
case GATT_UUID_INCLUDE_SERVICE:
|
|
|
|
case GATT_UUID_CHAR_EXT_PROP:
|
|
|
|
case GATT_UUID_CHAR_CLIENT_CONFIG:
|
|
|
|
case GATT_UUID_CHAR_SRVR_CONFIG:
|
|
|
|
case GATT_UUID_CHAR_PRESENT_FORMAT:
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR("GATT_NOT_LONG\n");
|
2016-11-24 13:10:15 -05:00
|
|
|
return GATT_NOT_LONG;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GATT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function read_attr_value
|
|
|
|
**
|
|
|
|
** Description Utility function to read an attribute value.
|
|
|
|
**
|
|
|
|
** Parameter p_attr: pointer to the attribute to read.
|
|
|
|
** offset: read offset.
|
|
|
|
** p_value: output parameter to carry out the attribute value.
|
|
|
|
** p_len: output parameter to carry out the attribute length.
|
|
|
|
** read_long: this is a read blob request.
|
|
|
|
** mtu: MTU
|
|
|
|
** sec_flag: current link security status.
|
|
|
|
** key_size: encryption key size.
|
|
|
|
**
|
|
|
|
** Returns status of operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static tGATT_STATUS read_attr_value (void *p_attr,
|
|
|
|
UINT16 offset,
|
|
|
|
UINT8 **p_data,
|
|
|
|
BOOLEAN read_long,
|
|
|
|
UINT16 mtu,
|
|
|
|
UINT16 *p_len,
|
|
|
|
tGATT_SEC_FLAG sec_flag,
|
|
|
|
UINT8 key_size)
|
|
|
|
{
|
|
|
|
UINT16 len = 0, uuid16 = 0;
|
|
|
|
UINT8 *p = *p_data;
|
|
|
|
tGATT_STATUS status;
|
2016-11-24 13:10:15 -05:00
|
|
|
tGATT_ATTR16 *p_attr16 = (tGATT_ATTR16 *)p_attr;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("read_attr_value uuid=0x%04x perm=0x%0x sec_flag=0x%x offset=%d read_long=%d\n",
|
2016-11-24 13:10:15 -05:00
|
|
|
p_attr16->uuid,
|
|
|
|
p_attr16->permission,
|
|
|
|
sec_flag,
|
|
|
|
offset,
|
|
|
|
read_long);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
status = gatts_check_attr_readability((tGATT_ATTR16 *)p_attr, offset, read_long, sec_flag, key_size);
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (status != GATT_SUCCESS) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return status;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_attr16->uuid_type == GATT_ATTR_UUID_TYPE_16) {
|
2016-09-26 09:37:39 -04:00
|
|
|
uuid16 = p_attr16->uuid;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
status = GATT_NO_RESOURCES;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (uuid16 == GATT_UUID_PRI_SERVICE || uuid16 == GATT_UUID_SEC_SERVICE) {
|
2016-09-26 09:37:39 -04:00
|
|
|
len = p_attr16->p_value->uuid.len;
|
2016-11-24 13:10:15 -05:00
|
|
|
if (mtu >= p_attr16->p_value->uuid.len) {
|
2016-09-26 09:37:39 -04:00
|
|
|
gatt_build_uuid_to_stream(&p, p_attr16->p_value->uuid);
|
|
|
|
status = GATT_SUCCESS;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (uuid16 == GATT_UUID_CHAR_DECLARE) {
|
|
|
|
len = (((tGATT_ATTR16 *)(p_attr16->p_next))->uuid_type == GATT_ATTR_UUID_TYPE_16) ? 5 : 19;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (mtu >= len) {
|
2016-09-26 09:37:39 -04:00
|
|
|
UINT8_TO_STREAM(p, p_attr16->p_value->char_decl.property);
|
|
|
|
UINT16_TO_STREAM(p, p_attr16->p_value->char_decl.char_val_handle);
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (((tGATT_ATTR16 *)(p_attr16->p_next))->uuid_type == GATT_ATTR_UUID_TYPE_16) {
|
2016-09-26 09:37:39 -04:00
|
|
|
UINT16_TO_STREAM(p, ((tGATT_ATTR16 *)(p_attr16->p_next))->uuid);
|
|
|
|
}
|
|
|
|
/* convert a 32bits UUID to 128 bits */
|
2016-11-24 13:10:15 -05:00
|
|
|
else if (((tGATT_ATTR32 *)(p_attr16->p_next))->uuid_type == GATT_ATTR_UUID_TYPE_32) {
|
2016-09-26 09:37:39 -04:00
|
|
|
gatt_convert_uuid32_to_uuid128 (p, ((tGATT_ATTR32 *)(p_attr16->p_next))->uuid);
|
|
|
|
p += LEN_UUID_128;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *)(p_attr16->p_next))->uuid, LEN_UUID_128);
|
|
|
|
}
|
|
|
|
status = GATT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (uuid16 == GATT_UUID_INCLUDE_SERVICE) {
|
|
|
|
if (p_attr16->p_value->incl_handle.service_type.len == LEN_UUID_16) {
|
2016-09-26 09:37:39 -04:00
|
|
|
len = 6;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
len = 4;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (mtu >= len) {
|
2016-09-26 09:37:39 -04:00
|
|
|
UINT16_TO_STREAM(p, p_attr16->p_value->incl_handle.s_handle);
|
|
|
|
UINT16_TO_STREAM(p, p_attr16->p_value->incl_handle.e_handle);
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_attr16->p_value->incl_handle.service_type.len == LEN_UUID_16) {
|
2016-09-26 09:37:39 -04:00
|
|
|
UINT16_TO_STREAM(p, p_attr16->p_value->incl_handle.service_type.uu.uuid16);
|
|
|
|
}
|
|
|
|
status = GATT_SUCCESS;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
} else { /* characteristic description or characteristic value */
|
2016-12-23 11:28:47 -05:00
|
|
|
if (p_attr16->control.auto_rsp == GATT_RSP_BY_STACK) {
|
2017-03-06 04:20:45 -05:00
|
|
|
if (p_attr16->p_value == NULL || p_attr16->p_value->attr_val.attr_val == NULL) {
|
2017-05-31 05:20:29 -04:00
|
|
|
status = GATT_UNKNOWN_ERROR;
|
2017-03-06 04:20:45 -05:00
|
|
|
}
|
|
|
|
else if (offset > p_attr16->p_value->attr_val.attr_len){
|
2017-03-08 04:09:05 -05:00
|
|
|
/*if offset equal to max_len, should respond with zero byte value
|
|
|
|
//if offset is greater than max_len, should respond with an error*/
|
2017-03-06 04:20:45 -05:00
|
|
|
status = GATT_INVALID_OFFSET;
|
2017-03-08 04:00:56 -05:00
|
|
|
} else {
|
2017-03-06 04:20:45 -05:00
|
|
|
UINT8 *value = (UINT8 *)(p_attr16->p_value->attr_val.attr_val) + offset;
|
|
|
|
UINT16 len_left = p_attr16->p_value->attr_val.attr_len - offset;
|
|
|
|
len = (mtu >= len_left) ? (len_left) : mtu;
|
2017-01-11 11:48:48 -05:00
|
|
|
ARRAY_TO_STREAM(p, value, len);
|
2017-03-06 04:20:45 -05:00
|
|
|
status = GATT_STACK_RSP;
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
status = GATT_PENDING;
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
*p_len = len;
|
|
|
|
*p_data = p;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_db_read_attr_value_by_type
|
|
|
|
**
|
|
|
|
** Description Query attribute value by attribute type.
|
|
|
|
**
|
|
|
|
** Parameter p_db: pointer to the attribute database.
|
|
|
|
** p_rsp: Read By type response data.
|
|
|
|
** s_handle: starting handle of the range we are looking for.
|
|
|
|
** e_handle: ending handle of the range we are looking for.
|
|
|
|
** type: Attribute type.
|
|
|
|
** mtu: MTU.
|
|
|
|
** sec_flag: current link security status.
|
|
|
|
** key_size: encryption key size.
|
|
|
|
**
|
|
|
|
** Returns Status of the operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
tGATT_STATUS gatts_db_read_attr_value_by_type (tGATT_TCB *p_tcb,
|
2016-11-24 13:10:15 -05:00
|
|
|
tGATT_SVC_DB *p_db,
|
|
|
|
UINT8 op_code,
|
|
|
|
BT_HDR *p_rsp,
|
|
|
|
UINT16 s_handle,
|
|
|
|
UINT16 e_handle,
|
|
|
|
tBT_UUID type,
|
|
|
|
UINT16 *p_len,
|
|
|
|
tGATT_SEC_FLAG sec_flag,
|
|
|
|
UINT8 key_size,
|
|
|
|
UINT32 trans_id,
|
|
|
|
UINT16 *p_cur_handle)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
|
|
|
tGATT_STATUS status = GATT_NOT_FOUND;
|
|
|
|
tGATT_ATTR16 *p_attr;
|
|
|
|
UINT16 len = 0;
|
|
|
|
UINT8 *p = (UINT8 *)(p_rsp + 1) + p_rsp->len + L2CAP_MIN_OFFSET;
|
|
|
|
tBT_UUID attr_uuid;
|
|
|
|
#if (defined(BLE_DELAY_REQUEST_ENC) && (BLE_DELAY_REQUEST_ENC == TRUE))
|
|
|
|
UINT8 flag;
|
|
|
|
#endif
|
2017-08-10 08:05:10 -04:00
|
|
|
BOOLEAN need_rsp;
|
|
|
|
BOOLEAN have_send_request = false;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db && p_db->p_attr_list) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
while (p_attr && p_attr->handle <= e_handle) {
|
|
|
|
if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) {
|
2016-09-26 09:37:39 -04:00
|
|
|
attr_uuid.len = LEN_UUID_16;
|
|
|
|
attr_uuid.uu.uuid16 = p_attr->uuid;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32) {
|
2016-09-26 09:37:39 -04:00
|
|
|
attr_uuid.len = LEN_UUID_32;
|
|
|
|
attr_uuid.uu.uuid32 = ((tGATT_ATTR32 *)p_attr)->uuid;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
attr_uuid.len = LEN_UUID_128;
|
|
|
|
memcpy(attr_uuid.uu.uuid128, ((tGATT_ATTR128 *)p_attr)->uuid, LEN_UUID_128);
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_attr->handle >= s_handle && gatt_uuid_compare(type, attr_uuid)) {
|
|
|
|
if (*p_len <= 2) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_NO_RESOURCES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT16_TO_STREAM (p, p_attr->handle);
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
status = read_attr_value ((void *)p_attr, 0, &p, FALSE, (UINT16)(*p_len - 2), &len, sec_flag, key_size);
|
2017-08-10 08:05:10 -04:00
|
|
|
if (status == GATT_PENDING) {
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-08-10 08:05:10 -04:00
|
|
|
|
|
|
|
need_rsp = TRUE;
|
2017-01-11 11:48:48 -05:00
|
|
|
status = gatts_send_app_read_request(p_tcb, op_code, p_attr->handle, 0, trans_id, need_rsp);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
/* one callback at a time */
|
|
|
|
break;
|
2017-08-10 08:05:10 -04:00
|
|
|
} else if (status == GATT_SUCCESS || status == GATT_STACK_RSP) {
|
2017-08-22 06:11:01 -04:00
|
|
|
if (status == GATT_STACK_RSP){
|
2017-08-10 08:05:10 -04:00
|
|
|
need_rsp = FALSE;
|
|
|
|
status = gatts_send_app_read_request(p_tcb, op_code, p_attr->handle, 0, trans_id, need_rsp);
|
2017-08-22 06:11:01 -04:00
|
|
|
if(status == GATT_BUSY)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!have_send_request){
|
|
|
|
have_send_request = true;
|
|
|
|
trans_id = p_tcb->sr_cmd.trans_id;
|
|
|
|
}
|
2017-08-10 08:05:10 -04:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_rsp->offset == 0) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_rsp->offset = len + 2;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_rsp->offset == len + 2) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_rsp->len += (len + 2);
|
|
|
|
*p_len -= (len + 2);
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
GATT_TRACE_ERROR("format mismatch");
|
|
|
|
status = GATT_NO_RESOURCES;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
*p_cur_handle = p_attr->handle;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p_attr = (tGATT_ATTR16 *)p_attr->p_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (defined(BLE_DELAY_REQUEST_ENC) && (BLE_DELAY_REQUEST_ENC == TRUE))
|
2016-11-24 13:10:15 -05:00
|
|
|
if (BTM_GetSecurityFlags(p_tcb->peer_bda, &flag)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
if ((p_tcb->att_lcid == L2CAP_ATT_CID) && (status == GATT_PENDING) &&
|
2016-11-24 13:10:15 -05:00
|
|
|
(type.uu.uuid16 == GATT_UUID_GAP_DEVICE_NAME)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
if ((flag & (BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_FLAG_ENCRYPTED)) ==
|
2016-11-24 13:10:15 -05:00
|
|
|
BTM_SEC_LINK_KEY_KNOWN) {
|
2016-09-26 09:37:39 -04:00
|
|
|
tACL_CONN *p;
|
|
|
|
p = btm_bda_to_acl(p_tcb->peer_bda, BT_TRANSPORT_LE);
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((p != NULL) && (p->link_role == BTM_ROLE_MASTER)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
tBTM_BLE_SEC_ACT sec_act = BTM_BLE_SEC_ENCRYPT;
|
|
|
|
btm_ble_set_encryption(p_tcb->peer_bda, &sec_act, p->link_role);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_add_included_service
|
|
|
|
**
|
|
|
|
** Description This function adds an included service into a database.
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** inc_srvc_type: included service type.
|
|
|
|
**
|
|
|
|
** Returns Status of the operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
UINT16 gatts_add_included_service (tGATT_SVC_DB *p_db, UINT16 s_handle, UINT16 e_handle,
|
|
|
|
tBT_UUID service)
|
|
|
|
{
|
|
|
|
tGATT_ATTR16 *p_attr;
|
|
|
|
tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_INCLUDE_SERVICE}};
|
|
|
|
|
|
|
|
GATT_TRACE_DEBUG("gatts_add_included_service: s_hdl = 0x%04x e_hdl = 0x%04x uuid = 0x%04x",
|
2016-11-24 13:10:15 -05:00
|
|
|
s_handle, e_handle, service.uu.uuid16);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (service.len == 0 || s_handle == 0 || e_handle == 0) {
|
2016-09-26 09:37:39 -04:00
|
|
|
GATT_TRACE_ERROR("gatts_add_included_service Illegal Params.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((p_attr = (tGATT_ATTR16 *) allocate_attr_in_db(p_db, &uuid, GATT_PERM_READ)) != NULL) {
|
|
|
|
if (copy_extra_byte_in_db(p_db, (void **)&p_attr->p_value, sizeof(tGATT_INCL_SRVC))) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr->p_value->incl_handle.s_handle = s_handle;
|
|
|
|
p_attr->p_value->incl_handle.e_handle = e_handle;
|
|
|
|
memcpy(&p_attr->p_value->incl_handle.service_type, &service, sizeof(tBT_UUID));
|
|
|
|
|
|
|
|
return p_attr->handle;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
deallocate_attr_in_db(p_db, p_attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_add_characteristic
|
|
|
|
**
|
|
|
|
** Description This function add a characteristics and its descriptor into
|
|
|
|
** a servce identified by the service database pointer.
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** perm: permission (authentication and key size requirements)
|
|
|
|
** property: property of the characteristic.
|
|
|
|
** p_char: characteristic value information.
|
|
|
|
**
|
|
|
|
** Returns Status of te operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
UINT16 gatts_add_characteristic (tGATT_SVC_DB *p_db, tGATT_PERM perm,
|
|
|
|
tGATT_CHAR_PROP property,
|
2016-12-23 11:28:47 -05:00
|
|
|
tBT_UUID *p_char_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
|
|
|
tGATT_ATTR16 *p_char_decl, *p_char_val;
|
|
|
|
tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_CHAR_DECLARE}};
|
2017-06-07 08:06:49 -04:00
|
|
|
BOOLEAN status;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("gatts_add_characteristic perm=0x%0x property=0x%0x\n", perm, property);
|
2017-03-06 04:20:45 -05:00
|
|
|
/* parameter validation check */
|
2017-06-07 08:06:49 -04:00
|
|
|
status = gatts_add_char_desc_value_check(attr_val, control);
|
|
|
|
if (status == FALSE){
|
|
|
|
return 0;
|
2017-03-06 04:20:45 -05:00
|
|
|
}
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((p_char_decl = (tGATT_ATTR16 *)allocate_attr_in_db(p_db, &uuid, GATT_PERM_READ)) != NULL) {
|
|
|
|
if (!copy_extra_byte_in_db(p_db, (void **)&p_char_decl->p_value, sizeof(tGATT_CHAR_DECL))) {
|
2016-09-26 09:37:39 -04:00
|
|
|
deallocate_attr_in_db(p_db, p_char_decl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_char_val = (tGATT_ATTR16 *)allocate_attr_in_db(p_db, p_char_uuid, perm);
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_char_val == NULL) {
|
2016-09-26 09:37:39 -04:00
|
|
|
deallocate_attr_in_db(p_db, p_char_decl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_char_decl->p_value->char_decl.property = property;
|
|
|
|
p_char_decl->p_value->char_decl.char_val_handle = p_char_val->handle;
|
2016-12-23 11:28:47 -05:00
|
|
|
if (control != NULL) {
|
|
|
|
p_char_val->control.auto_rsp = control->auto_rsp;
|
|
|
|
} else {
|
|
|
|
p_char_val->control.auto_rsp = GATT_RSP_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2017-03-06 04:20:45 -05:00
|
|
|
if (attr_val != NULL) {
|
2016-12-23 11:28:47 -05:00
|
|
|
if (!copy_extra_byte_in_db(p_db, (void **)&p_char_val->p_value, sizeof(tGATT_ATTR_VAL))) {
|
|
|
|
deallocate_attr_in_db(p_db, p_char_val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
GATT_TRACE_DEBUG("attr_val->attr_len = %x, attr_val->attr_max_len = %x\n", attr_val->attr_len, attr_val->attr_max_len);
|
|
|
|
GATT_TRACE_DEBUG("attribute handle = %x\n", p_char_val->handle);
|
|
|
|
p_char_val->p_value->attr_val.attr_len = attr_val->attr_len;
|
|
|
|
p_char_val->p_value->attr_val.attr_max_len = attr_val->attr_max_len;
|
2017-08-17 09:13:45 -04:00
|
|
|
p_char_val->p_value->attr_val.attr_val = osi_malloc(attr_val->attr_max_len);
|
2017-03-06 04:20:45 -05:00
|
|
|
if (p_char_val->p_value->attr_val.attr_val == NULL) {
|
|
|
|
deallocate_attr_in_db(p_db, p_char_decl);
|
|
|
|
deallocate_attr_in_db(p_db, p_char_val);
|
2017-03-07 06:55:21 -05:00
|
|
|
GATT_TRACE_WARNING("Warning in %s, line=%d, insufficient resource to allocate for attribute value\n", __func__, __LINE__);
|
2017-03-06 04:20:45 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2017-04-11 23:09:55 -04:00
|
|
|
else {
|
|
|
|
//add mask to indicate that p_value->attr_val.attr_val is dynamic allocated
|
|
|
|
p_char_val->mask |= GATT_ATTR_VALUE_ALLOCATED;
|
|
|
|
}
|
2017-03-06 04:20:45 -05:00
|
|
|
|
|
|
|
//initiate characteristic attribute value part
|
|
|
|
memset(p_char_val->p_value->attr_val.attr_val, 0, attr_val->attr_max_len);
|
|
|
|
if (attr_val->attr_val != NULL) {
|
|
|
|
if (attr_val->attr_max_len < attr_val->attr_len){
|
2017-03-07 06:55:21 -05:00
|
|
|
GATT_TRACE_ERROR("Error in %s, Line=%d, attribute actual length (%d) should not larger than max size (%d)\n",
|
2017-03-06 04:20:45 -05:00
|
|
|
__func__, __LINE__, attr_val->attr_len, attr_val->attr_max_len);
|
|
|
|
}
|
|
|
|
UINT16 actual_len = (attr_val->attr_max_len < attr_val->attr_len) ? (attr_val->attr_max_len) : (attr_val->attr_len);
|
|
|
|
memcpy(p_char_val->p_value->attr_val.attr_val, attr_val->attr_val, actual_len);
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
return p_char_val->handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatt_convertchar_descr_type
|
|
|
|
**
|
|
|
|
** Description This function convert a char descript UUID into descriptor type.
|
|
|
|
**
|
|
|
|
** Returns descriptor type.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
UINT8 gatt_convertchar_descr_type(tBT_UUID *p_descr_uuid)
|
|
|
|
{
|
|
|
|
tBT_UUID std_descr = {LEN_UUID_16, {GATT_UUID_CHAR_EXT_PROP}};
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_DESCR_EXT_DSCPTOR;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
std_descr.uu.uuid16 ++;
|
2016-11-24 13:10:15 -05:00
|
|
|
if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_DESCR_USER_DSCPTOR;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
std_descr.uu.uuid16 ++;
|
2016-11-24 13:10:15 -05:00
|
|
|
if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_DESCR_CLT_CONFIG;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
std_descr.uu.uuid16 ++;
|
2016-11-24 13:10:15 -05:00
|
|
|
if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_DESCR_SVR_CONFIG;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
std_descr.uu.uuid16 ++;
|
2016-11-24 13:10:15 -05:00
|
|
|
if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_DESCR_PRES_FORMAT;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
std_descr.uu.uuid16 ++;
|
2016-11-24 13:10:15 -05:00
|
|
|
if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_DESCR_AGGR_FORMAT;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
std_descr.uu.uuid16 ++;
|
2016-11-24 13:10:15 -05:00
|
|
|
if (gatt_uuid_compare(std_descr, * p_descr_uuid)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return GATT_DESCR_VALID_RANGE;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
return GATT_DESCR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_add_char_descr
|
|
|
|
**
|
|
|
|
** Description This function add a characteristics descriptor.
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** perm: characteristic descriptor permission type.
|
|
|
|
** char_dscp_tpye: the characteristic descriptor masks.
|
|
|
|
** p_dscp_params: characteristic descriptors values.
|
|
|
|
**
|
|
|
|
** Returns Status of the operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
UINT16 gatts_add_char_descr (tGATT_SVC_DB *p_db, tGATT_PERM perm,
|
2016-12-23 11:28:47 -05:00
|
|
|
tBT_UUID *p_descr_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
|
|
|
tGATT_ATTR16 *p_char_dscptr;
|
2017-06-07 08:06:49 -04:00
|
|
|
BOOLEAN status;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("gatts_add_char_descr uuid=0x%04x\n", p_descr_uuid->uu.uuid16);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-03-06 04:20:45 -05:00
|
|
|
/* parameter validation check */
|
2017-06-07 08:06:49 -04:00
|
|
|
status = gatts_add_char_desc_value_check(attr_val, control);
|
|
|
|
if (status == FALSE){
|
|
|
|
return 0;
|
2017-03-06 04:20:45 -05:00
|
|
|
}
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
/* Add characteristic descriptors */
|
2016-12-23 11:28:47 -05:00
|
|
|
if ((p_char_dscptr = (tGATT_ATTR16 *)allocate_attr_in_db(p_db, p_descr_uuid, perm)) == NULL) {
|
2017-03-06 04:20:45 -05:00
|
|
|
deallocate_attr_in_db(p_db, p_char_dscptr);
|
2016-09-26 09:37:39 -04:00
|
|
|
GATT_TRACE_DEBUG("gatts_add_char_descr Fail for adding char descriptors.");
|
|
|
|
return 0;
|
2017-03-08 04:00:56 -05:00
|
|
|
} else {
|
2017-03-06 04:20:45 -05:00
|
|
|
p_char_dscptr->control.auto_rsp = (control == NULL) ? GATT_RSP_DEFAULT : (control->auto_rsp);
|
2016-12-23 11:28:47 -05:00
|
|
|
if (attr_val != NULL) {
|
|
|
|
if (!copy_extra_byte_in_db(p_db, (void **)&p_char_dscptr->p_value, sizeof(tGATT_ATTR_VAL))) {
|
|
|
|
deallocate_attr_in_db(p_db, p_char_dscptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p_char_dscptr->p_value->attr_val.attr_len = attr_val->attr_len;
|
|
|
|
p_char_dscptr->p_value->attr_val.attr_max_len = attr_val->attr_max_len;
|
2017-02-20 08:50:02 -05:00
|
|
|
if (attr_val->attr_max_len != 0) {
|
2017-08-17 09:13:45 -04:00
|
|
|
p_char_dscptr->p_value->attr_val.attr_val = osi_malloc(attr_val->attr_max_len);
|
2017-03-06 04:20:45 -05:00
|
|
|
if (p_char_dscptr->p_value->attr_val.attr_val == NULL) {
|
|
|
|
deallocate_attr_in_db(p_db, p_char_dscptr);
|
2017-03-07 06:55:21 -05:00
|
|
|
GATT_TRACE_WARNING("Warning in %s, line=%d, insufficient resource to allocate for descriptor value\n", __func__, __LINE__);
|
2017-03-06 04:20:45 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2017-04-11 23:09:55 -04:00
|
|
|
else {
|
|
|
|
//add mask to indicate that p_value->attr_val.attr_val is dynamic allocated
|
|
|
|
p_char_dscptr->mask |= GATT_ATTR_VALUE_ALLOCATED;
|
|
|
|
}
|
2017-03-06 04:20:45 -05:00
|
|
|
|
|
|
|
//initiate characteristic attribute value part
|
|
|
|
memset(p_char_dscptr->p_value->attr_val.attr_val, 0, attr_val->attr_max_len);
|
|
|
|
if(attr_val->attr_val != NULL) {
|
|
|
|
memcpy(p_char_dscptr->p_value->attr_val.attr_val, attr_val->attr_val, attr_val->attr_len);
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
return p_char_dscptr->handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_set_attribute_value
|
|
|
|
**
|
|
|
|
** Description This function add the attribute value in the database
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** attr_handle: the attribute handle
|
|
|
|
** length: the attribute value length
|
|
|
|
** value: the pointer to the data to be set to the attribute value in the database
|
|
|
|
**
|
|
|
|
** Returns Status of the operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
tGATT_STATUS gatts_set_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle,
|
2017-03-06 04:20:45 -05:00
|
|
|
UINT16 length, UINT8 *value)
|
2016-12-23 11:28:47 -05:00
|
|
|
{
|
2017-01-11 11:48:48 -05:00
|
|
|
tGATT_ATTR16 *p_cur;
|
2016-12-23 11:28:47 -05:00
|
|
|
|
|
|
|
if (p_db == NULL) {
|
|
|
|
GATT_TRACE_DEBUG("gatts_set_attribute_value Fail:p_db is NULL.\n");
|
|
|
|
return GATT_INVALID_PDU;
|
|
|
|
}
|
|
|
|
if (p_db->p_attr_list == NULL) {
|
|
|
|
GATT_TRACE_DEBUG("gatts_set_attribute_value Fail:p_db->p_attr_list is NULL.\n");
|
|
|
|
return GATT_INVALID_PDU;
|
|
|
|
}
|
2017-03-06 04:20:45 -05:00
|
|
|
if ((length > 0) && (value == NULL)){
|
|
|
|
GATT_TRACE_ERROR("Error in %s, line=%d, value should not be NULL here\n",__func__, __LINE__);
|
|
|
|
return GATT_INVALID_PDU;
|
|
|
|
}
|
2016-12-23 11:28:47 -05:00
|
|
|
|
|
|
|
p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
|
|
|
|
|
2017-01-11 11:48:48 -05:00
|
|
|
while (p_cur != NULL) {
|
2016-12-23 11:28:47 -05:00
|
|
|
if (p_cur->handle == attr_handle) {
|
2017-03-06 04:20:45 -05:00
|
|
|
/* for characteristic should not be set, return GATT_NOT_FOUND */
|
2016-12-23 11:28:47 -05:00
|
|
|
if (p_cur->uuid_type == GATT_ATTR_UUID_TYPE_16) {
|
|
|
|
switch (p_cur->uuid) {
|
2017-03-06 04:20:45 -05:00
|
|
|
case GATT_UUID_PRI_SERVICE:
|
|
|
|
case GATT_UUID_SEC_SERVICE:
|
|
|
|
case GATT_UUID_CHAR_DECLARE:
|
|
|
|
return GATT_NOT_FOUND;
|
|
|
|
break;
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-06 04:20:45 -05:00
|
|
|
|
|
|
|
/* in other cases, value can be set*/
|
|
|
|
if ((p_cur->p_value == NULL) || (p_cur->p_value->attr_val.attr_val == NULL) \
|
|
|
|
|| (p_cur->p_value->attr_val.attr_max_len == 0)){
|
|
|
|
GATT_TRACE_ERROR("Error in %s, line=%d, attribute value should not be NULL here\n", __func__, __LINE__);
|
|
|
|
return GATT_NOT_FOUND;
|
2017-03-08 04:09:05 -05:00
|
|
|
} else if (p_cur->p_value->attr_val.attr_max_len < length) {
|
2017-03-06 04:20:45 -05:00
|
|
|
GATT_TRACE_ERROR("gatts_set_attribute_value failed:Invalid value length");
|
|
|
|
return GATT_INVALID_ATTR_LEN;
|
2017-03-08 04:00:56 -05:00
|
|
|
} else{
|
2017-03-06 04:20:45 -05:00
|
|
|
memcpy(p_cur->p_value->attr_val.attr_val, value, length);
|
|
|
|
p_cur->p_value->attr_val.attr_len = length;
|
|
|
|
}
|
2016-12-23 11:28:47 -05:00
|
|
|
break;
|
|
|
|
}
|
2017-01-11 11:48:48 -05:00
|
|
|
p_cur = p_cur->p_next;
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return GATT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_get_attribute_value
|
|
|
|
**
|
|
|
|
** Description This function get the attribute value in the database
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** attr_handle: the attribute handle
|
|
|
|
** length: the attribute value length
|
|
|
|
** value: the pointer to the data to be get to the attribute value in the database
|
|
|
|
**
|
|
|
|
** Returns Status of the operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
tGATT_STATUS gatts_get_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle,
|
|
|
|
UINT16 *length, UINT8 **value)
|
|
|
|
{
|
2017-01-11 11:48:48 -05:00
|
|
|
tGATT_ATTR16 *p_cur;
|
2017-01-17 05:42:11 -05:00
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("attr_handle = %x\n", attr_handle);
|
2017-01-17 05:42:11 -05:00
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
if (p_db == NULL) {
|
|
|
|
GATT_TRACE_ERROR("gatts_get_attribute_value Fail:p_db is NULL.\n");
|
|
|
|
return GATT_INVALID_PDU;
|
|
|
|
}
|
|
|
|
if (p_db->p_attr_list == NULL) {
|
|
|
|
GATT_TRACE_ERROR("gatts_get_attribute_value Fail:p_db->p_attr_list is NULL.\n");
|
|
|
|
return GATT_INVALID_PDU;
|
|
|
|
}
|
2017-08-14 05:39:11 -04:00
|
|
|
if (length == NULL){
|
|
|
|
GATT_TRACE_ERROR("gatts_get_attribute_value Fail:length is NULL.\n");
|
|
|
|
return GATT_INVALID_PDU;
|
|
|
|
}
|
|
|
|
if (value == NULL){
|
|
|
|
GATT_TRACE_ERROR("gatts_get_attribute_value Fail:value is NULL.\n");
|
|
|
|
return GATT_INVALID_PDU;
|
|
|
|
}
|
2016-12-23 11:28:47 -05:00
|
|
|
|
|
|
|
p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
|
|
|
|
|
2017-01-11 11:48:48 -05:00
|
|
|
while (p_cur != NULL) {
|
2016-12-23 11:28:47 -05:00
|
|
|
if (p_cur->handle == attr_handle) {
|
|
|
|
|
|
|
|
if (p_cur->uuid_type == GATT_ATTR_UUID_TYPE_16) {
|
|
|
|
switch (p_cur->uuid) {
|
|
|
|
case GATT_UUID_CHAR_DECLARE:
|
|
|
|
case GATT_UUID_INCLUDE_SERVICE:
|
|
|
|
break;
|
|
|
|
default:
|
2017-01-17 05:42:11 -05:00
|
|
|
if (p_cur->p_value && p_cur->p_value->attr_val.attr_len != 0) {
|
2016-12-23 11:28:47 -05:00
|
|
|
*length = p_cur->p_value->attr_val.attr_len;
|
|
|
|
*value = p_cur->p_value->attr_val.attr_val;
|
|
|
|
return GATT_SUCCESS;
|
|
|
|
} else {
|
2018-03-09 03:23:27 -05:00
|
|
|
GATT_TRACE_ERROR("gatts_get_attribute_value failed:the value length is 0");
|
2016-12-23 11:28:47 -05:00
|
|
|
return GATT_INVALID_ATTR_LEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (p_cur->p_value->attr_val.attr_len != 0) {
|
|
|
|
*length = p_cur->p_value->attr_val.attr_len;
|
|
|
|
*value = p_cur->p_value->attr_val.attr_val;
|
|
|
|
return GATT_SUCCESS;
|
|
|
|
} else {
|
2018-03-09 03:23:27 -05:00
|
|
|
GATT_TRACE_ERROR("gatts_get_attribute_value failed:the value length is 0");
|
2016-12-23 11:28:47 -05:00
|
|
|
return GATT_INVALID_ATTR_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-11 11:48:48 -05:00
|
|
|
p_cur = p_cur->p_next;
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
|
2017-01-11 11:48:48 -05:00
|
|
|
return GATT_NOT_FOUND;
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN gatts_is_auto_response(UINT16 attr_handle)
|
|
|
|
{
|
|
|
|
tGATT_HDL_LIST_ELEM *p_decl = NULL;
|
|
|
|
BOOLEAN rsp = FALSE;
|
|
|
|
tGATT_SVC_DB *p_db = NULL;
|
|
|
|
if ((p_decl = gatt_find_hdl_buffer_by_attr_handle(attr_handle)) == NULL) {
|
|
|
|
GATT_TRACE_DEBUG("Service not created\n");
|
|
|
|
return rsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_db = &p_decl->svc_db;
|
|
|
|
|
|
|
|
tGATT_ATTR16 *p_cur, *p_next;
|
|
|
|
|
|
|
|
if (p_db == NULL) {
|
|
|
|
GATT_TRACE_DEBUG("gatts_get_attribute_value Fail:p_db is NULL.\n");
|
|
|
|
return rsp;
|
|
|
|
}
|
|
|
|
if (p_db->p_attr_list == NULL) {
|
|
|
|
GATT_TRACE_DEBUG("gatts_get_attribute_value Fail:p_db->p_attr_list is NULL.\n");
|
|
|
|
return rsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
|
|
|
|
p_next = (tGATT_ATTR16 *) p_cur->p_next;
|
|
|
|
|
|
|
|
for (; p_cur != NULL && p_next != NULL;
|
|
|
|
p_cur = p_next, p_next = (tGATT_ATTR16 *)p_next->p_next) {
|
|
|
|
if (p_cur->handle == attr_handle) {
|
|
|
|
if (p_cur->p_value != NULL && p_cur->control.auto_rsp == GATT_RSP_BY_STACK) {
|
|
|
|
rsp = true;
|
|
|
|
return rsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Service Attribute Database Query Utility Functions */
|
|
|
|
/*******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_read_attr_value_by_handle
|
|
|
|
**
|
|
|
|
** Description Query attribute value by attribute handle.
|
|
|
|
**
|
|
|
|
** Parameter p_db: pointer to the attribute database.
|
|
|
|
** handle: Attribute handle to read.
|
|
|
|
** offset: Read offset.
|
|
|
|
** p_value: output parameter to carry out the attribute value.
|
|
|
|
** p_len: output parameter as attribute length read.
|
|
|
|
** read_long: this is a read blob request.
|
|
|
|
** mtu: MTU.
|
|
|
|
** sec_flag: current link security status.
|
|
|
|
** key_size: encryption key size
|
|
|
|
**
|
|
|
|
** Returns Status of operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
tGATT_STATUS gatts_read_attr_value_by_handle(tGATT_TCB *p_tcb,
|
2016-11-24 13:10:15 -05:00
|
|
|
tGATT_SVC_DB *p_db,
|
|
|
|
UINT8 op_code,
|
|
|
|
UINT16 handle, UINT16 offset,
|
|
|
|
UINT8 *p_value, UINT16 *p_len,
|
|
|
|
UINT16 mtu,
|
|
|
|
tGATT_SEC_FLAG sec_flag,
|
|
|
|
UINT8 key_size,
|
|
|
|
UINT32 trans_id)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
|
|
|
tGATT_STATUS status = GATT_NOT_FOUND;
|
|
|
|
tGATT_ATTR16 *p_attr;
|
|
|
|
UINT8 *pp = p_value;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db && p_db->p_attr_list) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
while (p_attr && handle >= p_attr->handle) {
|
|
|
|
if (p_attr->handle == handle) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = read_attr_value (p_attr, offset, &pp,
|
|
|
|
(BOOLEAN)(op_code == GATT_REQ_READ_BLOB),
|
|
|
|
mtu, p_len, sec_flag, key_size);
|
|
|
|
|
2017-01-11 11:48:48 -05:00
|
|
|
if ((status == GATT_PENDING) || (status == GATT_STACK_RSP)) {
|
|
|
|
BOOLEAN need_rsp = (status != GATT_STACK_RSP);
|
|
|
|
status = gatts_send_app_read_request(p_tcb, op_code, p_attr->handle, offset, trans_id, need_rsp);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p_attr = (tGATT_ATTR16 *)p_attr->p_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 11:48:48 -05:00
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
tGATT_STATUS gatts_write_attr_value_by_handle(tGATT_SVC_DB *p_db,
|
|
|
|
UINT16 handle, UINT16 offset,
|
|
|
|
UINT8 *p_value, UINT16 len)
|
|
|
|
{
|
|
|
|
tGATT_STATUS status = GATT_NOT_FOUND;
|
|
|
|
tGATT_ATTR16 *p_attr;
|
|
|
|
|
|
|
|
if (p_db && p_db->p_attr_list) {
|
|
|
|
p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
|
|
|
|
|
|
|
|
while (p_attr && handle >= p_attr->handle) {
|
|
|
|
if (p_attr->handle == handle ) {
|
|
|
|
if (p_attr->control.auto_rsp == GATT_RSP_BY_APP) {
|
|
|
|
return GATT_APP_RSP;
|
|
|
|
}
|
|
|
|
|
2017-03-06 04:20:45 -05:00
|
|
|
if ((p_attr->p_value != NULL) &&
|
|
|
|
(p_attr->p_value->attr_val.attr_max_len >= offset + len) &&
|
|
|
|
p_attr->p_value->attr_val.attr_val != NULL) {
|
2016-12-23 11:28:47 -05:00
|
|
|
memcpy(p_attr->p_value->attr_val.attr_val + offset, p_value, len);
|
|
|
|
p_attr->p_value->attr_val.attr_len = len + offset;
|
|
|
|
return GATT_SUCCESS;
|
2017-03-08 04:09:05 -05:00
|
|
|
} else if (p_attr->p_value->attr_val.attr_max_len < offset + len){
|
2017-03-06 04:20:45 -05:00
|
|
|
GATT_TRACE_DEBUG("Remote device try to write with a length larger then attribute's max length\n");
|
|
|
|
return GATT_INVALID_ATTR_LEN;
|
2017-03-08 04:09:05 -05:00
|
|
|
} else if ((p_attr->p_value == NULL) || (p_attr->p_value->attr_val.attr_val == NULL)){
|
2017-03-06 04:20:45 -05:00
|
|
|
GATT_TRACE_ERROR("Error in %s, line=%d, %s should not be NULL here\n", __func__, __LINE__, \
|
|
|
|
(p_attr->p_value == NULL) ? "p_value" : "attr_val.attr_val");
|
2017-05-31 05:20:29 -04:00
|
|
|
return GATT_UNKNOWN_ERROR;
|
2016-12-23 11:28:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p_attr = (tGATT_ATTR16 *)p_attr->p_next;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_read_attr_perm_check
|
|
|
|
**
|
|
|
|
** Description Check attribute readability.
|
|
|
|
**
|
|
|
|
** Parameter p_db: pointer to the attribute database.
|
|
|
|
** handle: Attribute handle to read.
|
|
|
|
** offset: Read offset.
|
|
|
|
** p_value: output parameter to carry out the attribute value.
|
|
|
|
** p_len: output parameter as attribute length read.
|
|
|
|
** read_long: this is a read blob request.
|
|
|
|
** mtu: MTU.
|
|
|
|
** sec_flag: current link security status.
|
|
|
|
** key_size: encryption key size
|
|
|
|
**
|
|
|
|
** Returns Status of operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
tGATT_STATUS gatts_read_attr_perm_check(tGATT_SVC_DB *p_db,
|
|
|
|
BOOLEAN is_long,
|
|
|
|
UINT16 handle,
|
|
|
|
tGATT_SEC_FLAG sec_flag,
|
|
|
|
UINT8 key_size)
|
|
|
|
{
|
|
|
|
tGATT_STATUS status = GATT_NOT_FOUND;
|
|
|
|
tGATT_ATTR16 *p_attr;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db && p_db->p_attr_list) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
while (p_attr && handle >= p_attr->handle) {
|
|
|
|
if (p_attr->handle == handle) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = gatts_check_attr_readability (p_attr, 0,
|
|
|
|
is_long,
|
|
|
|
sec_flag, key_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p_attr = (tGATT_ATTR16 *) p_attr->p_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2016-12-23 11:28:47 -05:00
|
|
|
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_write_attr_perm_check
|
|
|
|
**
|
|
|
|
** Description Write attribute value into database.
|
|
|
|
**
|
|
|
|
** Parameter p_db: pointer to the attribute database.
|
|
|
|
** op_code:op code of this write.
|
|
|
|
** handle: handle of the attribute to write.
|
|
|
|
** offset: Write offset if write op code is write blob.
|
|
|
|
** p_data: Attribute value to write.
|
|
|
|
** len: attribute data length.
|
|
|
|
** sec_flag: current link security status.
|
|
|
|
** key_size: encryption key size
|
|
|
|
**
|
|
|
|
** Returns Status of the operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
tGATT_STATUS gatts_write_attr_perm_check (tGATT_SVC_DB *p_db, UINT8 op_code,
|
2016-11-24 13:10:15 -05:00
|
|
|
UINT16 handle, UINT16 offset, UINT8 *p_data,
|
|
|
|
UINT16 len, tGATT_SEC_FLAG sec_flag, UINT8 key_size)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
|
|
|
tGATT_STATUS status = GATT_NOT_FOUND;
|
|
|
|
tGATT_ATTR16 *p_attr;
|
|
|
|
UINT16 max_size = 0;
|
|
|
|
tGATT_PERM perm;
|
|
|
|
UINT16 min_key_size;
|
|
|
|
|
|
|
|
GATT_TRACE_DEBUG( "gatts_write_attr_perm_check op_code=0x%0x handle=0x%04x offset=%d len=%d sec_flag=0x%0x key_size=%d",
|
2016-11-24 13:10:15 -05:00
|
|
|
op_code, handle, offset, len, sec_flag, key_size);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db != NULL) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr = (tGATT_ATTR16 *) p_db->p_attr_list;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
while (p_attr != NULL) {
|
|
|
|
if (p_attr->handle == handle) {
|
2016-09-26 09:37:39 -04:00
|
|
|
perm = p_attr->permission;
|
|
|
|
min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
|
2016-11-24 13:10:15 -05:00
|
|
|
if (min_key_size != 0 ) {
|
|
|
|
min_key_size += 6;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
GATT_TRACE_DEBUG( "gatts_write_attr_perm_check p_attr->permission =0x%04x min_key_size==0x%04x",
|
2016-11-24 13:10:15 -05:00
|
|
|
p_attr->permission,
|
|
|
|
min_key_size);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
if ((op_code == GATT_CMD_WRITE || op_code == GATT_REQ_WRITE)
|
2016-11-24 13:10:15 -05:00
|
|
|
&& (perm & GATT_WRITE_SIGNED_PERM)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
/* use the rules for the mixed security see section 10.2.3*/
|
|
|
|
/* use security mode 1 level 2 when the following condition follows */
|
|
|
|
/* LE security mode 2 level 1 and LE security mode 1 level 2 */
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((perm & GATT_PERM_WRITE_SIGNED) && (perm & GATT_PERM_WRITE_ENCRYPTED)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
perm = GATT_PERM_WRITE_ENCRYPTED;
|
|
|
|
}
|
|
|
|
/* use security mode 1 level 3 when the following condition follows */
|
|
|
|
/* LE security mode 2 level 2 and security mode 1 and LE */
|
|
|
|
else if (((perm & GATT_PERM_WRITE_SIGNED_MITM) && (perm & GATT_PERM_WRITE_ENCRYPTED)) ||
|
2016-11-24 13:10:15 -05:00
|
|
|
/* LE security mode 2 and security mode 1 level 3 */
|
|
|
|
((perm & GATT_WRITE_SIGNED_PERM) && (perm & GATT_PERM_WRITE_ENC_MITM))) {
|
2016-09-26 09:37:39 -04:00
|
|
|
perm = GATT_PERM_WRITE_ENC_MITM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((op_code == GATT_SIGN_CMD_WRITE) && !(perm & GATT_WRITE_SIGNED_PERM)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_WRITE_NOT_PERMIT;
|
|
|
|
GATT_TRACE_DEBUG( "gatts_write_attr_perm_check - sign cmd write not allowed");
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((op_code == GATT_SIGN_CMD_WRITE) && (sec_flag & GATT_SEC_FLAG_ENCRYPTED)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INVALID_PDU;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - Error!! sign cmd write sent on a encypted link");
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (!(perm & GATT_WRITE_ALLOWED)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_WRITE_NOT_PERMIT;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_WRITE_NOT_PERMIT");
|
|
|
|
}
|
|
|
|
/* require authentication, but not been authenticated */
|
2016-11-24 13:10:15 -05:00
|
|
|
else if ((perm & GATT_WRITE_AUTH_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_UNAUTHED)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INSUF_AUTHENTICATION;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_AUTHENTICATION");
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if ((perm & GATT_WRITE_MITM_REQUIRED ) && !(sec_flag & GATT_SEC_FLAG_LKEY_AUTHED)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INSUF_AUTHENTICATION;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_AUTHENTICATION: MITM required");
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if ((perm & GATT_WRITE_ENCRYPTED_PERM ) && !(sec_flag & GATT_SEC_FLAG_ENCRYPTED)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INSUF_ENCRYPTION;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_ENCRYPTION");
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if ((perm & GATT_WRITE_ENCRYPTED_PERM ) && (sec_flag & GATT_SEC_FLAG_ENCRYPTED) && (key_size < min_key_size)) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INSUF_KEY_SIZE;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_KEY_SIZE");
|
|
|
|
}
|
|
|
|
/* LE security mode 2 attribute */
|
|
|
|
else if (perm & GATT_WRITE_SIGNED_PERM && op_code != GATT_SIGN_CMD_WRITE && !(sec_flag & GATT_SEC_FLAG_ENCRYPTED)
|
2016-11-24 13:10:15 -05:00
|
|
|
&& (perm & GATT_WRITE_ALLOWED) == 0) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INSUF_AUTHENTICATION;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INSUF_AUTHENTICATION: LE security mode 2 required");
|
2016-11-24 13:10:15 -05:00
|
|
|
} else { /* writable: must be char value declaration or char descritpors */
|
|
|
|
if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) {
|
|
|
|
switch (p_attr->uuid) {
|
2016-09-26 09:37:39 -04:00
|
|
|
case GATT_UUID_CHAR_PRESENT_FORMAT:/* should be readable only */
|
|
|
|
case GATT_UUID_CHAR_EXT_PROP:/* should be readable only */
|
|
|
|
case GATT_UUID_CHAR_AGG_FORMAT: /* should be readable only */
|
2016-11-24 13:10:15 -05:00
|
|
|
case GATT_UUID_CHAR_VALID_RANGE:
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_WRITE_NOT_PERMIT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GATT_UUID_CHAR_CLIENT_CONFIG:
|
2016-11-24 13:10:15 -05:00
|
|
|
/* coverity[MISSING_BREAK] */
|
|
|
|
/* intnended fall through, ignored */
|
|
|
|
/* fall through */
|
2016-09-26 09:37:39 -04:00
|
|
|
case GATT_UUID_CHAR_SRVR_CONFIG:
|
|
|
|
max_size = 2;
|
|
|
|
case GATT_UUID_CHAR_DESCRIPTION:
|
|
|
|
default: /* any other must be character value declaration */
|
|
|
|
status = GATT_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128 ||
|
|
|
|
p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32) {
|
|
|
|
status = GATT_SUCCESS;
|
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INVALID_PDU;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_data == NULL && len > 0) {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INVALID_PDU;
|
|
|
|
}
|
|
|
|
/* these attribute does not allow write blob */
|
|
|
|
// btla-specific ++
|
|
|
|
else if ( (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) &&
|
|
|
|
(p_attr->uuid == GATT_UUID_CHAR_CLIENT_CONFIG ||
|
|
|
|
p_attr->uuid == GATT_UUID_CHAR_SRVR_CONFIG) )
|
|
|
|
// btla-specific --
|
|
|
|
{
|
2016-11-24 13:10:15 -05:00
|
|
|
if (op_code == GATT_REQ_PREPARE_WRITE && offset != 0) { /* does not allow write blob */
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_NOT_LONG;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_NOT_LONG");
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (len != max_size) { /* data does not match the required format */
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_INVALID_ATTR_LEN;
|
|
|
|
GATT_TRACE_ERROR( "gatts_write_attr_perm_check - GATT_INVALID_PDU");
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
status = GATT_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr = (tGATT_ATTR16 *)p_attr->p_next;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function allocate_attr_in_db
|
|
|
|
**
|
|
|
|
** Description Allocate a memory space for a new attribute, and link this
|
|
|
|
** attribute into the database attribute list.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Parameter p_db : database pointer.
|
|
|
|
** p_uuid: pointer to attribute UUID
|
|
|
|
** service : type of attribute to be added.
|
|
|
|
**
|
|
|
|
** Returns pointer to the newly allocated attribute.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static void *allocate_attr_in_db(tGATT_SVC_DB *p_db, tBT_UUID *p_uuid, tGATT_PERM perm)
|
|
|
|
{
|
|
|
|
tGATT_ATTR16 *p_attr16 = NULL, *p_last;
|
|
|
|
tGATT_ATTR32 *p_attr32 = NULL;
|
|
|
|
tGATT_ATTR128 *p_attr128 = NULL;
|
|
|
|
UINT16 len = sizeof(tGATT_ATTR128);
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_uuid == NULL) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR("illegal UUID\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_uuid->len == LEN_UUID_16) {
|
2016-09-26 09:37:39 -04:00
|
|
|
len = sizeof(tGATT_ATTR16);
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (p_uuid->len == LEN_UUID_32) {
|
2016-09-26 09:37:39 -04:00
|
|
|
len = sizeof(tGATT_ATTR32);
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("allocate attr %d bytes\n", len);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db->end_handle <= p_db->next_handle) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("handle space full. handle_max = %d next_handle = %d\n",
|
2016-11-24 13:10:15 -05:00
|
|
|
p_db->end_handle, p_db->next_handle);
|
2016-09-26 09:37:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db->mem_free < len) {
|
|
|
|
if (!allocate_svc_db_buf(p_db)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR("allocate_attr_in_db failed, no resources\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(p_db->p_free_mem, 0, len);
|
|
|
|
p_attr16 = (tGATT_ATTR16 *) p_db->p_free_mem;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_uuid->len == LEN_UUID_16 && p_uuid->uu.uuid16 != GATT_ILLEGAL_UUID) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr16->uuid_type = GATT_ATTR_UUID_TYPE_16;
|
|
|
|
p_attr16->uuid = p_uuid->uu.uuid16;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (p_uuid->len == LEN_UUID_32) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr32 = (tGATT_ATTR32 *) p_db->p_free_mem;
|
|
|
|
p_attr32->uuid_type = GATT_ATTR_UUID_TYPE_32;
|
|
|
|
p_attr32->uuid = p_uuid->uu.uuid32;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (p_uuid->len == LEN_UUID_128) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr128 = (tGATT_ATTR128 *) p_db->p_free_mem;
|
|
|
|
p_attr128->uuid_type = GATT_ATTR_UUID_TYPE_128;
|
|
|
|
memcpy(p_attr128->uuid, p_uuid->uu.uuid128, LEN_UUID_128);
|
|
|
|
}
|
|
|
|
|
|
|
|
p_db->p_free_mem += len;
|
|
|
|
p_db->mem_free -= len;
|
|
|
|
|
|
|
|
p_attr16->handle = p_db->next_handle++;
|
|
|
|
p_attr16->permission = perm;
|
|
|
|
p_attr16->p_next = NULL;
|
|
|
|
|
|
|
|
/* link the attribute record into the end of DB */
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db->p_attr_list == NULL) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_db->p_attr_list = p_attr16;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_last = (tGATT_ATTR16 *)p_db->p_attr_list;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
while (p_last != NULL && p_last->p_next != NULL) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_last = (tGATT_ATTR16 *)p_last->p_next;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
p_last->p_next = p_attr16;
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_attr16->uuid_type == GATT_ATTR_UUID_TYPE_16) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("=====> handle = [0x%04x] uuid16 = [0x%04x] perm=0x%02x\n",
|
2016-11-24 13:10:15 -05:00
|
|
|
p_attr16->handle, p_attr16->uuid, p_attr16->permission);
|
|
|
|
} else if (p_attr16->uuid_type == GATT_ATTR_UUID_TYPE_32) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("=====> handle = [0x%04x] uuid32 = [0x%08x] perm=0x%02x\n",
|
2016-11-24 13:10:15 -05:00
|
|
|
p_attr32->handle, p_attr32->uuid, p_attr32->permission);
|
|
|
|
} else {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_DEBUG("=====> handle = [0x%04x] uuid128 = [0x%02x:0x%02x] perm=0x%02x\n",
|
2016-11-24 13:10:15 -05:00
|
|
|
p_attr128->handle, p_attr128->uuid[0], p_attr128->uuid[1],
|
|
|
|
p_attr128->permission);
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
return (void *)p_attr16;
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:28:47 -05:00
|
|
|
|
|
|
|
|
2016-09-26 09:37:39 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function deallocate_attr_in_db
|
|
|
|
**
|
|
|
|
** Description Free an attribute within the database.
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** p_attr: pointer to the attribute record to be freed.
|
|
|
|
**
|
|
|
|
** Returns BOOLEAN: success
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static BOOLEAN deallocate_attr_in_db(tGATT_SVC_DB *p_db, void *p_attr)
|
|
|
|
{
|
|
|
|
tGATT_ATTR16 *p_cur, *p_next;
|
|
|
|
BOOLEAN found = FALSE;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db->p_attr_list == NULL) {
|
2016-09-26 09:37:39 -04:00
|
|
|
return found;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
p_cur = (tGATT_ATTR16 *) p_db->p_attr_list;
|
|
|
|
p_next = (tGATT_ATTR16 *) p_cur->p_next;
|
|
|
|
|
|
|
|
for (; p_cur != NULL && p_next != NULL;
|
2016-11-24 13:10:15 -05:00
|
|
|
p_cur = p_next, p_next = (tGATT_ATTR16 *)p_next->p_next) {
|
|
|
|
if (p_next == p_attr) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_cur->p_next = p_next->p_next;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_cur == p_attr && p_cur == p_db->p_attr_list) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_db->p_attr_list = p_cur->p_next;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
/* else attr not found */
|
2016-11-24 13:10:15 -05:00
|
|
|
if ( found) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_db->next_handle --;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function copy_extra_byte_in_db
|
|
|
|
**
|
|
|
|
** Description Utility function to allocate extra bytes memory in DB and copy
|
|
|
|
** the value from a source place.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** p_dst: destination data pointer.
|
|
|
|
** p_src: source data pointer.
|
|
|
|
** len: data length to be copied.
|
|
|
|
**
|
|
|
|
** Returns None.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static BOOLEAN copy_extra_byte_in_db(tGATT_SVC_DB *p_db, void **p_dst, UINT16 len)
|
|
|
|
{
|
|
|
|
UINT8 *p = (UINT8 *)*p_dst;
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (p_db->mem_free < len) {
|
|
|
|
if (!allocate_svc_db_buf(p_db)) {
|
2016-12-23 11:28:47 -05:00
|
|
|
GATT_TRACE_ERROR("copy_extra_byte_in_db failed, no resources\n");
|
2016-09-26 09:37:39 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = p_db->p_free_mem;
|
|
|
|
p_db->p_free_mem += len;
|
|
|
|
p_db->mem_free -= len;
|
|
|
|
memset((void *)p, 0, len);
|
|
|
|
*p_dst = (void *)p;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function allocate_svc_db_buf
|
|
|
|
**
|
|
|
|
** Description Utility function to allocate extra buffer for service database.
|
|
|
|
**
|
|
|
|
** Returns TRUE if allocation succeed, otherwise FALSE.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static BOOLEAN allocate_svc_db_buf(tGATT_SVC_DB *p_db)
|
|
|
|
{
|
|
|
|
BT_HDR *p_buf;
|
|
|
|
|
|
|
|
GATT_TRACE_DEBUG("allocate_svc_db_buf allocating extra buffer");
|
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
if ((p_buf = (BT_HDR *)osi_calloc(GATT_DB_BUF_SIZE)) == NULL) {
|
2016-09-26 09:37:39 -04:00
|
|
|
GATT_TRACE_ERROR("allocate_svc_db_buf failed, no resources");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_db->p_free_mem = (UINT8 *) p_buf;
|
2017-08-17 09:13:45 -04:00
|
|
|
p_db->mem_free = GATT_DB_BUF_SIZE;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
2017-08-17 09:13:45 -04:00
|
|
|
fixed_queue_enqueue(p_db->svc_buffer, p_buf);
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_send_app_read_request
|
|
|
|
**
|
|
|
|
** Description Send application read request callback
|
|
|
|
**
|
|
|
|
** Returns status of operation.
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static tGATT_STATUS gatts_send_app_read_request(tGATT_TCB *p_tcb, UINT8 op_code,
|
2017-01-11 11:48:48 -05:00
|
|
|
UINT16 handle, UINT16 offset, UINT32 trans_id, BOOLEAN need_rsp)
|
2016-09-26 09:37:39 -04:00
|
|
|
{
|
|
|
|
tGATTS_DATA sr_data;
|
|
|
|
UINT8 i_rcb;
|
|
|
|
tGATT_SR_REG *p_sreg;
|
|
|
|
UINT16 conn_id;
|
|
|
|
|
|
|
|
i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
|
|
|
|
p_sreg = &gatt_cb.sr_reg[i_rcb];
|
|
|
|
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (trans_id == 0) {
|
2016-09-26 09:37:39 -04:00
|
|
|
trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
|
|
|
|
gatt_sr_update_cback_cnt(p_tcb, p_sreg->gatt_if, TRUE, TRUE);
|
|
|
|
}
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (trans_id != 0 ) {
|
2016-09-26 09:37:39 -04:00
|
|
|
memset(&sr_data, 0, sizeof(tGATTS_DATA));
|
|
|
|
|
|
|
|
sr_data.read_req.handle = handle;
|
|
|
|
sr_data.read_req.is_long = (BOOLEAN)(op_code == GATT_REQ_READ_BLOB);
|
|
|
|
sr_data.read_req.offset = offset;
|
2017-01-11 11:48:48 -05:00
|
|
|
sr_data.read_req.need_rsp = need_rsp;
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
gatt_sr_send_req_callback(conn_id,
|
|
|
|
trans_id, GATTS_REQ_TYPE_READ, &sr_data);
|
2017-01-24 05:38:50 -05:00
|
|
|
|
|
|
|
if (need_rsp) {
|
|
|
|
return (tGATT_STATUS) GATT_PENDING;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return (tGATT_STATUS) GATT_STACK_RSP;
|
|
|
|
}
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
|
|
|
return (tGATT_STATUS) GATT_BUSY; /* max pending command, application error */
|
2016-09-26 09:37:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_db_add_service_declaration
|
|
|
|
**
|
|
|
|
** Description Update a service database service declaration record.
|
|
|
|
**
|
|
|
|
** Parameter p_db: database pointer.
|
|
|
|
** service: UUID of the service.
|
|
|
|
**
|
|
|
|
** Returns void
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static BOOLEAN gatts_db_add_service_declaration(tGATT_SVC_DB *p_db, tBT_UUID *p_service, BOOLEAN is_pri)
|
|
|
|
{
|
|
|
|
tGATT_ATTR16 *p_attr;
|
|
|
|
tBT_UUID uuid = {LEN_UUID_16, {0}};
|
|
|
|
BOOLEAN rt = FALSE;
|
|
|
|
|
|
|
|
GATT_TRACE_DEBUG( "add_service_declaration");
|
|
|
|
|
2016-11-24 13:10:15 -05:00
|
|
|
if (is_pri) {
|
2016-09-26 09:37:39 -04:00
|
|
|
uuid.uu.uuid16 = GATT_UUID_PRI_SERVICE;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
uuid.uu.uuid16 = GATT_UUID_SEC_SERVICE;
|
2016-11-24 13:10:15 -05:00
|
|
|
}
|
2016-09-26 09:37:39 -04:00
|
|
|
|
|
|
|
/* add service declration record */
|
2016-11-24 13:10:15 -05:00
|
|
|
if ((p_attr = (tGATT_ATTR16 *)(allocate_attr_in_db(p_db, &uuid, GATT_PERM_READ))) != NULL) {
|
|
|
|
if (copy_extra_byte_in_db (p_db, (void **)&p_attr->p_value, sizeof(tBT_UUID))) {
|
|
|
|
if (p_service->len == LEN_UUID_16) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr->p_value->uuid.len = LEN_UUID_16;
|
|
|
|
p_attr->p_value->uuid.uu.uuid16 = p_service->uu.uuid16;
|
2016-11-24 13:10:15 -05:00
|
|
|
} else if (p_service->len == LEN_UUID_32) {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr->p_value->uuid.len = LEN_UUID_128;
|
|
|
|
gatt_convert_uuid32_to_uuid128(p_attr->p_value->uuid.uu.uuid128, p_service->uu.uuid32);
|
2016-11-24 13:10:15 -05:00
|
|
|
} else {
|
2016-09-26 09:37:39 -04:00
|
|
|
p_attr->p_value->uuid.len = LEN_UUID_128;
|
|
|
|
memcpy(p_attr->p_value->uuid.uu.uuid128, p_service->uu.uuid128, LEN_UUID_128);
|
|
|
|
}
|
|
|
|
rt = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return rt;
|
|
|
|
}
|
|
|
|
|
2017-06-07 08:06:49 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
**
|
|
|
|
** Function gatts_add_char_desc_value_check
|
|
|
|
**
|
|
|
|
** Description parameters validation check for gatts add char/descriptor functions
|
|
|
|
**
|
|
|
|
** Parameter attr_val: attribute value for char/descriptor.
|
|
|
|
** control: control variable for char/descriptor.
|
|
|
|
**
|
|
|
|
** Returns void
|
|
|
|
**
|
|
|
|
*******************************************************************************/
|
|
|
|
static BOOLEAN gatts_add_char_desc_value_check (tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
|
|
|
|
{
|
|
|
|
if ((control != NULL) && ((control->auto_rsp != GATT_RSP_BY_APP) && (control->auto_rsp != GATT_RSP_BY_STACK))){
|
|
|
|
GATT_TRACE_ERROR("Error in %s, line=%d, control->auto_rsp should be set to GATT_RSP_BY_APP or GATT_RSP_BY_STACK here\n",\
|
|
|
|
__func__, __LINE__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((control != NULL) && (control->auto_rsp == GATT_RSP_BY_STACK)){
|
|
|
|
if (attr_val == NULL){
|
|
|
|
GATT_TRACE_ERROR("Error in %s, line=%d, for stack respond attribute, attr_val should not be NULL here\n",\
|
|
|
|
__func__, __LINE__);
|
|
|
|
return FALSE;
|
|
|
|
} else if (attr_val->attr_max_len == 0){
|
|
|
|
GATT_TRACE_ERROR("Error in %s, line=%d, for stack respond attribute, attribute max length should not be 0\n",\
|
|
|
|
__func__, __LINE__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr_val != NULL){
|
|
|
|
if (attr_val->attr_len > attr_val->attr_max_len){
|
|
|
|
GATT_TRACE_ERROR("Error in %s, line=%d,attribute actual length should not be larger than max length\n",\
|
|
|
|
__func__, __LINE__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
2017-04-20 07:18:05 -04:00
|
|
|
#endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */
|