zl程序教程

您现在的位置是:首页 >  其他

当前栏目

ble属性格式、权限与声明

2023-03-14 22:44:12 时间

点击上方[物联网思考],点击关注,第一时间查看物联网精彩分享!

0、前言

属性协议(ATT)有两个角色,Client和Server,ATT协议都是纯C/S架构,即Server存储属性,Client什么也不存储,Client主动发起请求读写Server端的属性,Server被动响应。但是服务端也有通知的能力,在服务端属性发生变化时,Server能够通知Client,这样避免了Client不停的Poll。

1、属性分类

BLE的属性类型是有限的,可以分为四大类。 Primary Service(首要服务项) Secondary Service(次要服务项) Include(包含服务项) Characteristic(特征)

2、Profile、服务、特征的关系

可以看出一个Profile里面至少包含一个服务里面,服务里面至少包含一个特征声明、特征值。

3、属性格式

Attribute handle:Attribute句柄,16-bit长度; Attribute type:Attribute类型,2字节或者16字节长,使用UUID来表示; Attribute value:就是数据真正的值; Attribute permissions:权限属性。

4、属性权限

目前有主要有以下四种: 访问权限(Access Permission):只读、只写、读写;服务器使用访问权限来确定客户端是否可以读取和/或写入属性值; 加密权限(Encryption Permission):加密、不加密; 认证权限(Authentication Permission) :需要认证、无需认证;服务器使用身份验证权限来确定当客户端试图访问某个属性时是否需要经过身份验证的物理链接。在向客户端发送通知或指示之前,服务器还使用身份验证权限来确定是否需要经过身份验证的物理链接; 授权权限(Authorization Permission):需要授权、无需授权;授权权限决定在访问属性值之前是否需要对客户端进行授权。

注意:某一条属性的权限可以是访问权限、加密权限、认证权限和授权权限的组合。

CH57X协议栈中关于权限的定义如下:

// GATT Attribute Access Permissions Bit Fields
#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

5、属性声明

注意: 两个必需的声明是特征声明和特征值声明; 特征值声明应紧跟着特征声明而存在; 特征声明是一个特征的开始。

5.1、服务声明

可以看出:服务分为首要服务和次要服务,UUID分别为0x2800和0x2801; 权限是只读。

5.2、包含声明

可以看出:包含声明的UUID为0x2802; 权限是只读。

5.3、特征声明

可以看出:特征声明的UUID为0x2803; 权限是只读。

5.3.1、特征声明的值字段
5.3.2、特征声明的值的性质

注意:特征性质和属性权限是两个概念。

CH57X协议栈中关于特征声明的值的性质定义如下:

// GATT Characteristic Properties Bit Fields
#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 acknowledgment
#define GATT_PROP_INDICATE               0x20 //!< Permits indications of a Characteristic Value with acknowledgment
#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
5.4、特征值声明
5.5、特征描述声明
5.5.1、特征扩展特性声明
5.5.2、特征用户描述声明

UUID是0x2901。

5.5.3、客户端特征配置声明

UUID是0x2902; 权限是可读(无认证,无授权),可写(无认证,无授权)或者由上层规定。

5.5.4、服务器特征配置声明
5.5.5、特征格式声明
5.5.6、特征聚合格式声明

6、属性定义

CH57X中属性定义如下:

typedef struct attAttribute_t
{
  gattAttrType_t type; //!< Attribute type (2 or 16 octet UUIDs)
  uint8 permissions;   //!< Attribute permissions
  uint16 handle;       //!< Attribute handle - assigned internally by attribute server
  uint8* pValue;       //!< Attribute value - encoding of the octet array is defined in 
                       //!< the applicable profile. The maximum length of an attribute 
                       //!< value shall be 512 octets.
} gattAttribute_t;

可以看出,与3中的属性格式相符。