BLE--CC2541的入门之特征值

Posted:   2020-04-11

Status:   Completed

Categories :   CC2541 BLE

Previous:   BLE--CC2541的入门之绑定与配对

Next:   树莓派上HomeBridge 的安装


参考连接

环境要求

硬件

  • CCDebuger
  • CC2541开发板
  • USB Dongle

软件

SimpleBLEPeripheral工程分析

编译下载

  • 编译SimpleBLEPeripheral的CC2541配置,并下载到开发板中

服务Server发现

  • 打开BLE Device Monitor,扫描并连接到开发板
  • 这时会出现四个服务 serverlist
  • 对应的就是simpleBLEPeripheral.c文件内SimpleBLEPeripheral_Init函数中的服务添加 addservers
  • 如果我们把GGS_AddService( GATT_ALL_SERVICES );注释掉,重新编译下载,在BLE Device Monitor会看到Generic Access Service消失了 GGS
  • 同理可验证四个服务对应关系
服务 代码
Generic Access Service GGS_AddService( GATT_ALL_SERVICES )
Generic Attribute Service GATTServApp_AddService( GATT_ALL_SERVICES )
Device Information Service DevInfo_AddService()
Simple Profile Service SimpleProfile_AddService( GATT_ALL_SERVICES )

Generic Access Service

  • 通用访问配置服务
  • 点开服务可以看到,里边有五个特征值 GGS1

DeviceName

  • 设备名字
  • 通过代码GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );设置
  • 我们可以通过修改attDeviceName值去验证 GGS_name
  • BLE Device Monitor工具和Android系统中,APP是通过广播包中的Device Name来识别设备名的,在iOS系统中,则不管广播包,只认这个GAP服务下的Device Name
  • DeviceName

Appearance

Peripheral Privacy Flag

Reconnection Address

Peripheral Preferred Connection Parameters

Generic Attibute Service

Service Changed

Device Information Service

System ID

  • 系统ID

Model Number

  • 设备型号

Serial Number String

  • 设备序列号

Firmware Revision String

  • 固件版本

Hardware Revision String

  • 硬件版本

Software Revision String

  • 软件版本

Manufacturer Name String

  • 厂商名字

IEEE 11073-20601 Regulatory Certification Data List

  • 符合法规列表

PnP ID

  • 设备ID值,用来识别给定类型/型号/版本的所有设备

强制要求

  • 上边三个服务均为强制要求,正式生产需要进行设置,内容详见参考链接

Simple Profile Service

  • 自定义服务分析

UUID

  • BLE Device Monito可以看到服务ID为F0:FF,即0xFFF0,低8位在前,高8位在后 UUID

Properties

属性定义

gattservapp.h文件内

1
2
3
4
5
6
7
8
#define GATT_PROP_BCAST                  0x01 //!< Permits broadcasts of the Characteristic Value
#define GATT_PROP_READ                   0x02 //!< Permits reads of the Characteristic Value
#define GATT_PROP_WRITE_NO_RSP           0x04 //!< Permits writes of the Characteristic Value without response
#define GATT_PROP_WRITE                  0x08 //!< Permits writes of the Characteristic Value with response
#define GATT_PROP_NOTIFY                 0x10 //!< Permits notifications of a Characteristic Value without acknowledgement
#define GATT_PROP_INDICATE               0x20 //!< Permits indications of a Characteristic Value with acknowledgement
#define GATT_PROP_AUTHEN                 0x40 //!< Permits signed writes to the Characteristic Value
#define GATT_PROP_EXTENDED               0x80 //!< Additional characteristic properties are defined in the Characteristic Extended Properties Descriptor

值对应

属性 含义
GATT_PROP_BCAST 0x01 允许广播
GATT_PROP_READ 0x02 允许读取
GATT_PROP_WRITE_NO_RSP 0x04 允许写入而不响应
GATT_PROP_WRITE 0x08 允许写入
GATT_PROP_NOTIFY 0x10 允许通知
GATT_PROP_INDICATE 0x20 允许指示
GATT_PROP_AUTHEN 0x40 允许签名写入
GATT_PROP_EXTENDED 0x80 其他特性属性在特性扩展属性描述符中定义

Characteristic1的属性0A:25:00:F1:FF

  • 0A代表0x08+0x02即允许写入和读取
  • 25:00代表37,即Characteristic1的分配句柄,也可以在BLE Device Monito看到
  • F1:FF代表Characteristic1的UUID,0xFFF1

Permissions

权限定义

gatt.h文件内

1
2
3
4
5
6
7
8
#define GATT_PERMIT_READ                 0x01 //!< Attribute is Readable
#define GATT_PERMIT_WRITE                0x02 //!< Attribute is Writable
#define GATT_PERMIT_AUTHEN_READ          0x04 //!< Read requires Authentication
#define GATT_PERMIT_AUTHEN_WRITE         0x08 //!< Write requires Authentication
#define GATT_PERMIT_AUTHOR_READ          0x10 //!< Read requires Authorization
#define GATT_PERMIT_AUTHOR_WRITE         0x20 //!< Write requires Authorization
#define GATT_PERMIT_ENCRYPT_READ         0x40 //!< Read requires Encryption
#define GATT_PERMIT_ENCRYPT_WRITE        0x80 //!< Write requires Encryption

值对应

权限 含义
GATT_PERMIT_READ 0x01 可读
GATT_PERMIT_WRITE 0x02 可写
GATT_PERMIT_AUTHEN_READ 0x04 读取需要身份验证
GATT_PERMIT_AUTHEN_WRITE 0x08 写入需要身份验证
GATT_PERMIT_AUTHOR_READ 0x10 读取需要授权
GATT_PERMIT_AUTHOR_WRITE 0x20 写入需要授权
GATT_PERMIT_ENCRYPT_READ 0x40 读取需要加密
GATT_PERMIT_ENCRYPT_WRITE 0x80 写入需要加密

Properties和Permissions的关系与区别

  • Properties表示整个Characteristic的属性
  • Permissions代表属性表各个属性的权限
  • Characteristic由属性表内的多个属性组成

权限测试

  • BLE Device Monito查看各个Characteristic值
特征 属性 权限
Characteristic1 0x0A 读:正常 写:正常
Characteristic2 0x02 读:正常 写:err3
Characteristic3 0x08 读:err2 写:正常
Characteristic4 0x10 读:err2 写:err3
Characteristic5 0x02 读:err5 写:err3
  • 查看源文件simpleGATTprofile.c
特征 属性 权限
Characteristic1 读写 读写
Characteristic2
Characteristic3
Characteristic4 通知 0
Characteristic5 读取需要身份验证
  • 验证可知属性权限影响读写

错误列表

  • att.h文件中
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    #define ATT_ERR_INVALID_HANDLE           0x01 //!< Attribute handle value given was not valid on this attribute server
    #define ATT_ERR_READ_NOT_PERMITTED       0x02 //!< Attribute cannot be read
    #define ATT_ERR_WRITE_NOT_PERMITTED      0x03 //!< Attribute cannot be written
    #define ATT_ERR_INVALID_PDU              0x04 //!< The attribute PDU was invalid
    #define ATT_ERR_INSUFFICIENT_AUTHEN      0x05 //!< The attribute requires authentication before it can be read or written
    #define ATT_ERR_UNSUPPORTED_REQ          0x06 //!< Attribute server doesn't support the request received from the attribute client
    #define ATT_ERR_INVALID_OFFSET           0x07 //!< Offset specified was past the end of the attribute
    #define ATT_ERR_INSUFFICIENT_AUTHOR      0x08 //!< The attribute requires an authorization before it can be read or written
    #define ATT_ERR_PREPARE_QUEUE_FULL       0x09 //!< Too many prepare writes have been queued
    #define ATT_ERR_ATTR_NOT_FOUND           0x0a //!< No attribute found within the given attribute handle range
    #define ATT_ERR_ATTR_NOT_LONG            0x0b //!< Attribute cannot be read or written using the Read Blob Request or Prepare Write Request
    #define ATT_ERR_INSUFFICIENT_KEY_SIZE    0x0c //!< The Encryption Key Size used for encrypting this link is insufficient
    #define ATT_ERR_INVALID_VALUE_SIZE       0x0d //!< The attribute value length is invalid for the operation
    #define ATT_ERR_UNLIKELY                 0x0e //!< The attribute request that was requested has encountered an error that was very unlikely, and therefore could not be completed as requested
    #define ATT_ERR_INSUFFICIENT_ENCRYPT     0x0f //!< The attribute requires encryption before it can be read or written
    #define ATT_ERR_UNSUPPORTED_GRP_TYPE     0x10 //!< The attribute type is not a supported grouping attribute as defined by a higher layer specification
    #define ATT_ERR_INSUFFICIENT_RESOURCES   0x11 //!< Insufficient Resources to complete the request
    
错误 含义
ATT_ERR_INVALID_HANDLE 0x01 属性句柄值无效
ATT_ERR_READ_NOT_PERMITTED 0x02 无权限读取
ATT_ERR_WRITE_NOT_PERMITTED 0x03 无权限写入
ATT_ERR_INVALID_PDU 0x04 PDU无效
ATT_ERR_INSUFFICIENT_AUTHEN 0x05 需要身份验证
ATT_ERR_UNSUPPORTED_REQ 0x06 服务器不支持此请求
ATT_ERR_INVALID_OFFSET 0x07 指定偏移量溢出
ATT_ERR_INSUFFICIENT_AUTHOR 0x08 需要授权
ATT_ERR_PREPARE_QUEUE_FULL 0x09 写入队列已满
ATT_ERR_ATTR_NOT_FOUND 0x0a 没有找到属性
ATT_ERR_ATTR_NOT_LONG 0x0b 属性不能用Blob读请求或Prepare写请求
ATT_ERR_INSUFFICIENT_KEY_SIZE 0x0c 加密密钥大小不够
ATT_ERR_INVALID_VALUE_SIZE 0x0d 属性值长度无效
ATT_ERR_UNLIKELY 0x0e 请求发生错误,无法完成请求
ATT_ERR_INSUFFICIENT_ENCRYPT 0x0f 需要加密
ATT_ERR_UNSUPPORTED_GRP_TYPE 0x10 属性类型不支持
ATT_ERR_INSUFFICIENT_RESOURCES 0x11 资源不足