要点:
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 = 0, //!< 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.
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.
The 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.
使用说明:
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的!
#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.
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)