源:蓝牙协议 基于TI cc2540 模块的理解

Bluetooth 4.0开发
Platform:TI IC:cc2540 Environment:windows 7 tools:IAR 8.20.2

demo Code:BLE_CC254x_1.4.0 from TI

蓝牙协议 基于TI cc2540 模块的理解(转)-LMLPHP

物理层:是1Mbps自适应跳频GFSK射频,工作于免许可证的2.4GHz ISM(工业、科学与医疗)频段。
链路层:用于控制设备的辐射状态,设备将处于五种状态之一:等待、广告、扫描、初始化、连接。广播设备不需要建立连接 就可以发送数据,扫描设备接收广播设备发送的数据;发起连接的设备通过发送连接请求来回应广播设备,如果广播 设备接受连接请求,那么广播设备与发起连接的设备将会进入连接状态。发起练级的的设备称为主机,接受连接请求 的设备称为从机。
主机控制层:为主机和控制器之间提供标准通信接口,这一层,可以是软件或者硬件接口,如UART、SPI、USB等
逻辑链路控制及自适应协议层:为上层提供数据封装服务,允许逻辑上的点对点数据通信。
安全管理层:定义了配对和秘钥分配方式,并为协议栈其他层与另一个设备之间的安全连接和数据交换提供服务
属性协议层:允许设备向另外一个设备展示一块特定的数据,称之为“属性”,在ATT环境中,展示“属性”的设备称为服务器

与之配对的设备称为客户端,链路层状态(主机和从机)与设备的ATT角色是相互独立的。

例如:主机设备既可以是ATT服务器,也可以是ATT客户端;从机设备既可以是ATT服务器,也可以是ATT客户端。

个人理解:(主机和从机定义谁练接谁,服务器和客户端定义于谁给谁共享数据)!!!

如:手机A的蓝牙请求手机B的蓝牙连接,那么对于链路层来说:A是从机,B是主机,连接好以后,A在蓝牙上共享,一块特定数据(比如某个文件)给B,那么A就是ATT的服务器,B就是客户端。

通用属性配置层:定义了使用ATT的服务框架。GATT规定配置文件profile的结构,在BLE中,所有被profile或者服务用到的数据 块称为“特性”,两个建立连接的设备之间的所有数据通信都是通过GATT子程序处理。应用程序和profile直 接使用GATT层。

//========================分割线===========================

demo程序添加打印消息,我手中有两块板一块有OLED液晶屏显示,一块没有液晶屏,我使用了串口作为提示信息!
主机:下载SimpleBLECentral程序 ,使用液晶屏打印

从机:下载SimpleBLEPeripheral程序,使用串口打印
在SimpleBLECentral程序中添加自己的LCD程序,然后使用该液晶作为打印口

在SimpleBLEPeripheral程序添加串口打印程序,然后使用串口作为打印口
串口打印程序,要在工程中添加HAL_UART=TRUE宏定义

void uart_printNotline(char str[]){
char * pstr=str;
if(pstr == NULL) return;
HalUARTWrite(,(unsigned char*)pstr,strlen(pstr));
}
void uart_printLine(char str[]){
char * pstr=str;
if(pstr == NULL) return;
HalUARTWrite(,(unsigned char*)pstr,strlen(pstr));
HalUARTWrite(,"\n",);
}

液晶屏的可根据自己具体的屏写响应的驱动。

本次使用的模块是:OLED LCD 128*64点阵液晶屏

OLED lcd打印信息

LCD_CLSLineAt8x16Pix(CLS_START_LINE1,CLS_LINE1);  //清楚指定的行

LCD_P8x16Str(0, WRITE_START_LINE1, "Read rsp:");  //在指定的行打印信息
实验效果,如期完成,串口 打印如下:
 BLE Peripheral
0x20CD399FCFCB

Initialized

Advertising

Connected

Char 1:   0

Char 1:   1

Char 1:   2

Char 1:   3

Char 1:   4

Char 1:   5

Char 1:   6

Char 1:   7

Char 1:   8

Disconnected

Advertising
主机使用按键连接 BLE Peripheral从机

BLE协议解析 参考《TI_BLE_Software_Developer's_Guide.pdf》

位于C:\Texas Instruments\BLE-CC254x-1.4.0\Documents 只要安装了TI的开发工具包就会有这个文件。
要点:

1、 The protocol stack consists of two sections: the controller and the host.  This separation of controller and host goes back to standard Bluetooth BR/EDR devices.

2、 Any profiles and applications that are being used sit on top of the GAP and GATT layers of the stack.

3、 PHY layer is a 1Mbps adaptive frequency-hopping GFSK, operating in the unlicensed 2.4 GHz ISM.

4、 The LL essentially controls the RF state of the device, LL层本质是控制设备的RF(射频)状态

状态有五个,分别是:

standby, advertising, scanning, initiating, or connected

代码中对应的枚举类型如下

/*
* GAP Peripheral + Broadcaster Role States.
*/
typedef enum
{
GAPROLE_INIT = , //!< Waiting to be started
GAPROLE_STARTED, //!< Started but not advertising
GAPROLE_ADVERTISING, //!< Currently Advertising
GAPROLE_WAITING, //!< Device is started but not advertising, is in waiting period before advertising again
GAPROLE_WAITING_AFTER_TIMEOUT, //!< Device just timed out from a connection but is not yet advertising, is in waiting period before advertising again
GAPROLE_CONNECTED, //!< In a connection
GAPROLE_CONNECTED_ADV, //!< In a connection and advertising
GAPROLE_ERROR //!< Error occurred - invalid state
} gaprole_States_t;

Advertisers transmit data without being in a connection, while scanners listen for advertisers. An initiator is a device that is responding to an advertiser with a connection request. If the advertiser accepts, both the advertiser and initiator will enter a connected state. When a device is in a connection, it will be connected in one of two roles: master or slave. The device that initiated the connection becomes the master, and the device that accepted the request becomes the slave.

广告发送数据不需要连接,扫描设备监听广告。发起者是一个响应广告者连接请求的设备。如果广告者接受连接,那么广告者和发起者将进入连接状态。当设备处于连接状态时,它就会扮演着主机或从机的角色。发起连接的设备将作为主机,被连接的设备作为从机。

The HCI layer provides a means of communication between the host and controller via a standardized interface. This layer can be implemented either through a software API, or by a hardware interface such as UART, SPI, or USB.

CHI层给主机和控制之间通信提供标准化接口。这一层既可以有软件的API实现也可以由硬件接口例如:UART、SPI或USB来实现。
The L2CAP layer provides data encapsulation services to the upper layers, allowing for logical end-to-end communication of data.

L2CAP层给上层提供数据封装服务,允许逻辑的端对端数据通信。

The SM layer defines the methods for pairing and key distribution, and provides functions for the other layers of the stack to securely connect and exchange data with another device.
T

he GAP layer directly interfaces with the application and/or profiles, and handles device discovery and connection-related services for the device. In addition, GAP handles the initiation of security features.

The ATT protocol allows a device to expose certain pieces of data, known as “attributes”, to another device. In the context of ATT, the device exposing attributes is referred to as the “server”, and the peer device is referred to as the “client”. The LL state (master or slave) of the device is independent of the ATT role of the device. For example, a master device may either be an ATT server or an ATT client, and a slave device may also be either an ATT server or an ATT client. It is also possible for a device to be both an ATT server and an ATT client simultaneously.

GAP层直接地与应用程序、配置、设备查找和连接服务管理进行交互。一般的,GAP掌管安全特征的启动。 ATT协议允许设备公开一些数据片段,例如设备的“属性”。...。LL状态下的主机从机和ATT下服务和客户是独立的。一个主机设备既可以是ATT服务端,也可以是ATT客户端。同样的一个从机既可以是ATT服务端也可以是ATT客户端。也有两者都是!

使用说明:

The GATT layer is a service framework that defines the sub-procedures for using ATT. GATT specifies the structure of profiles. In BLE, all pieces of data that are being used by a profile or service are called “characteristics”. All data communications that occur between two devices in a BLE connection are handled through GATT sub-procedures. Therefore, the application and/or profiles will directly use GATT.

App或者配置都是直接使用GATT的!

查看demo程序,可以知道协议栈的开始启动的地方在:osal_start_system();、

查找和执行任务的函数:osal_run_system()
流程图如下:

蓝牙协议 基于TI cc2540 模块的理解(转)-LMLPHP
OSAL的事件队列

蓝牙协议 基于TI cc2540 模块的理解(转)-LMLPHP

OSAL 事件和回调映射

蓝牙协议 基于TI cc2540 模块的理解(转)-LMLPHP

Central demo程序:

注意一下几个事件:

#define GAP_DEVICE_INIT_DONE_EVENT            0x00 //!< Sent when the Device Initialization is complete.  This event is sent as an OSAL message defined as gapDeviceInitDoneEvent_t.
#define GAP_DEVICE_DISCOVERY_EVENT 0x01 //!< Sent when the Device Discovery Process is complete. This event is sent as an OSAL message defined as gapDevDiscEvent_t.
#define GAP_ADV_DATA_UPDATE_DONE_EVENT 0x02 //!< Sent when the Advertising Data or SCAN_RSP Data has been updated. This event is sent as an OSAL message defined as gapAdvDataUpdateEvent_t.
#define GAP_MAKE_DISCOVERABLE_DONE_EVENT 0x03 //!< Sent when the Make Discoverable Request is complete. This event is sent as an OSAL message defined as gapMakeDiscoverableRspEvent_t.
#define GAP_END_DISCOVERABLE_DONE_EVENT 0x04 //!< Sent when the Advertising has ended. This event is sent as an OSAL message defined as gapEndDiscoverableRspEvent_t.
#define GAP_LINK_ESTABLISHED_EVENT 0x05 //!< Sent when the Establish Link Request is complete. This event is sent as an OSAL message defined as gapEstLinkReqEvent_t.
#define GAP_LINK_TERMINATED_EVENT 0x06 //!< Sent when a connection was terminated. This event is sent as an OSAL message defined as gapTerminateLinkEvent_t.
#define GAP_LINK_PARAM_UPDATE_EVENT 0x07 //!< Sent when an Update Parameters Event is received. This event is sent as an OSAL message defined as gapLinkUpdateEvent_t.
#define GAP_RANDOM_ADDR_CHANGED_EVENT 0x08 //!< Sent when a random address was changed. This event is sent as an OSAL message defined as gapRandomAddrEvent_t.
#define GAP_SIGNATURE_UPDATED_EVENT 0x09 //!< Sent when the device's signature counter is updated. This event is sent as an OSAL message defined as gapSignUpdateEvent_t.
#define GAP_AUTHENTICATION_COMPLETE_EVENT 0x0A //!< Sent when the Authentication (pairing) process is complete. This event is sent as an OSAL message defined as gapAuthCompleteEvent_t.
#define GAP_PASSKEY_NEEDED_EVENT 0x0B //!< Sent when a Passkey is needed. This is part of the pairing process. This event is sent as an OSAL message defined as gapPasskeyNeededEvent_t.
#define GAP_SLAVE_REQUESTED_SECURITY_EVENT 0x0C //!< Sent when a Slave Security Request is received. This event is sent as an OSAL message defined as gapSlaveSecurityReqEvent_t.
#define GAP_DEVICE_INFO_EVENT 0x0D //!< Sent during the Device Discovery Process when a device is discovered. This event is sent as an OSAL message defined as gapDeviceInfoEvent_t.
#define GAP_BOND_COMPLETE_EVENT 0x0E //!< Sent when the bonding(bound) process is complete. This event is sent as an OSAL message defined as gapBondCompleteEvent_t.
#define GAP_PAIRING_REQ_EVENT 0x0F //!< Sent when an unexpected Pairing Request is received. This event is sent as an OSAL message defined as gapPairingReqEvent_t.

GAPP配置

GAPP层总是作为下面四中角色中的一种:

1、广播者(Broadcaster)——不能连接广告设备

2、观测者(Observer)——扫描广播,但不能启动连接

3、外设(Peripheral)——可连接的广告设备,在单链路层连接作为从机操作

4、集中器(Central)——扫描广告和启动连接,在单链路和多链路层连接作为主机操作。目前,BLE集中器协议栈支持连接三个设备。

在一个典型的低功耗蓝牙系统中,外设广告特定的数据来让集中器设备知道它是可以被连接的设备。广告的内容包括:设备的地址,和一些附加的数据,如设备名字。集中器收到广告数据后,向外设发送("scan request")扫描请求。外设会响应一个(“scan response”)扫描回应。这个过程称为设备发现。 通过这个过程,集中器已经识别了外设,并且知道这个外设和它自己可以形成一个连接。然后集中器可以发送一个建立连接的请求给外设。一个连接请求应该包括一些连接的参数,

如下所示:

1.Connection Interval(GAPROLE_MIN_CONN_INTERVAL && GAPROLE_MAX_CONN_INTERVAL)- 连接间隔,在BLE的两个设备的连接中使用跳频机制。两个设备使用特定的信道发送和接收数据, 然后过一段时间后再使用新的信道(BLE协议栈的链路层处理信道的切换)。两个设备在切换信道后发送和接收数据称为一个连接事件。尽管没有应用数据被发送和接收,两个设备仍旧会交换链路层数据来维持连接。这个连接间隔就是这个两个连接事件的之间的时间间隔。间隔以1.25ms为单元,连接间隔的范围的6-3200既7.5ms-4s(4000ms)之间。 不用的应用程序可以要求不同的连接间隔, 拥有长的连接间隔的优点是降低功耗,因为设备在连接事件之间有较长时间的休眠。缺点是设备有数据发送的时候,就必须等到下一个连接事件才可以发送出去。 短时间的连接间隔优点是两设备连接频繁,会有较多的功耗。

2.Slave Latency(GAPROLE_SLAVE_LATENCY)从机延时——这个参数给从机(外设)跳过若干个连接事件的选项。给外设一些灵活度,如果外设没有任何数据发送,就可以选额跳过连接事件 和保持休眠。因此提供了一些功耗的节省。 从机延时值表示跳过最大的连接事件值。范围是0到499,0:不跳过任何连接事件,然而最大值必须不能影响连接间隔大于16.0s。

3.Supervision Timerout(GAPROLE_TIMEOUT_MULTIPLIER)超时管理——这个事两个成功连接事件之间的最大允许间隔。如果超过了这个事件而没有连接成功的连接事件,设备认为连接已经丢失。设备就会返回一个未连接的状态。这个参数值的单位是10ms,管理超时值的范围是10-3200即(100ms - 32s)。一般超时值必须大于有效连接间隔,如下所示:

Effective Connection Interval = (Connection Interval) * ( 1 + (Slave Latency) )

有效连接间隔 = 连接间隔 *(1 + 从机延时)

这些连接设置都可以通过请求集中器修改的,外设只需要向集中器发送“连接参数更新请求”来改变连接设置。这个请求由协议栈的L2ACP层处理。这个请求包括4个参数:连接最小间隔 (GAPROLE_MIN_CONN_INTERVAL)、连接最大间隔(GAPROLE_MAX_CONN_INTERVAL)、从机延时(GAPROLE_SLAVE_LATENCY)、超时(GAPROLE_TIMEOUT_MULTIPLIER)。这些值代表外设所要求的连接参数。接到请求后,集中器可以接受或者拒绝这些新的参数。

连接可以被主机或从机一方以任何原因终止,当一方发起终止连接时,另一方必须响应。 GAP层也处理BLE连接中安全特征初始化,只有在已认证的连接中,特定的数据才能被读写。一旦建立两个设备进行配对,当配对完成以后,形成加密连接的秘钥。在典型的应用中,外设请求集中器提供秘钥来完成配对工作,秘钥可以是一个固定值,也可以是随机生成一个数据提供给使用者,当集中器设备发送正确的秘钥后,两个设备交换安全秘钥并加密认证链接。

GAP 配置参数说明:

3.4.1 GAP Peripheral Role Profile The peripheral role profile provides the means for the keyfob to advertise, connect with a central device (though the initiation of the connection must come from the  central device), and request a specific set of connection parameters from a master device. The primary API function prototypes for the peripheral role profile can be found in the file peripheral.h. The API provides functions to get and set certain GAP 
profile parameters: GAPRole_GetParameter and GAPRole_SetParameter, respectively. Here are a few GAP role parameters of interest:  GAPROLE_ADVERT_ENABLED – This parameter enables or disables advertisements. The default value for this parameter is TRUE.  GAPROLE_ADVERT_DATA – This is a string containing the data to appear in the advertisement packets. By setting this value to { 0x02, 0x01, 0x05 }, the device will 
use limited discoverable mode when advertising. More information on advertisement data types and definitions can be found in [7].  GAPROLE_SCAN_RSP_DATA – This is a string containing the name of the device that will appear in scan response data. If an observer or central device is scanning and 
sends a scan request to the peripheral device, the peripheral will respond back with a scan response containing this string.  GAPROLE_ADVERT_OFF_TIME – This parameter is used when the device is put into limited discoverable mode. It sets how long the device should wait before becoming 
discoverable again at the end of the limited discovery period. By setting this value to 0, the device will not become discoverable again until the 
GAPROLE_ADVERT_ENABLED is set back to TRUE.  GAPROLE_PARAM_UPDATE_ENABLE – This enables automatic connection parameter update requests. The profile default value is FALSE.  GAPROLE_MIN_CONN_INTERVAL – This parameter is the minimum desired connection interval value. The default value is 80, corresponding to 100ms.  GAPROLE_MAX_CONN_INTERVAL – This parameter is the maximum desired connection interval value. The default value is 3200, corresponding to 4.0s.  GAPROLE_SLAVE_LATENCY – This parameter is the desired slave latency. The default value is 0.  GAPROLE_TIMEOUT_MULTIPLIER – This parameter is the desired connection supervision timeout. The default value is 1000 (corresponding to 10.0s)

05-08 08:17