javnson 发表于 2022-4-18 09:01

【沁恒CH582】蓝牙协议栈的结构分析

<h3 cid="n192" mdtype="heading">蓝牙协议栈的结构</h3>

<p cid="n193" mdtype="paragraph">在CH583芯片中内置了一个低功耗蓝牙模块(BLE)虽然在上一节我们已经稀里糊涂使用过一次了,但是对于这个功能实现的细节没有更加深入的分析。本节,我将就芯片开源的部分和芯片中能够获取到的信息对芯片的BLE模块展开初步分析。</p>

<h4 cid="n194" mdtype="heading">BLE部分</h4>

<p cid="n195" mdtype="paragraph">虽然没有言明(也可能是我没有找到重点),可以得知在芯片中实现的物理层是BLE5.0的硬件层。使用GFSK调制和2.4GHz的频道。</p>

<p cid="n196" mdtype="paragraph">CH582M芯片的协议栈是不开源的,所以目前留给我们的突破口就仅有面向用户接口部分了。配置和应用程序的实现基于的协议是GAP和GATT。</p>

<p cid="n197" mdtype="paragraph">GAP是通用访问协议,是BLE设备内部功能对外的配置接口,主要用于设置GAP的角色,工作模式,安全特性。主要负责蓝牙设备的广播、连接和设备绑定。</p>

<figure cid="n198" mdtype="table">
<table>
        <thead>
                <tr cid="n199" mdtype="table_row">
                        <th>GAP角色</th>
                        <th>描述</th>
                        <th>对应配置文件</th>
                </tr>
        </thead>
        <tbody>
                <tr cid="n203" mdtype="table_row">
                        <td>广播者</td>
                        <td>不可以直接连接的持续广播设备</td>
                        <td>Broadcaster role</td>
                </tr>
                <tr cid="n207" mdtype="table_row">
                        <td>观察者</td>
                        <td>可以扫描广播设备但是不能发起建立连接的设备</td>
                        <td>Observer role</td>
                </tr>
                <tr cid="n211" mdtype="table_row">
                        <td>从机</td>
                        <td>可以被连接的广播设备,可以在单个链路连接中作为从机</td>
                        <td>Peripheral role</td>
                </tr>
                <tr cid="n215" mdtype="table_row">
                        <td>主机</td>
                        <td>可以扫描广播设备并发起连接,在单个链路或多个链路中作为主机</td>
                        <td>Central role</td>
                </tr>
        </tbody>
</table>
</figure>

<p cid="n219" mdtype="paragraph">接下来我们观察一下Peripheral程序中的main的实现。</p>

<p cid="n220" mdtype="paragraph">在完成基础部分的初始化之后,系统调用函数<code>CH57X_BLEInit()</code>初始化BLE外设。这一段函数的实现,给出了具体的实现。</p>

<p cid="n221" mdtype="paragraph">整个库中执行做的核心任务就是在初始化<code>bleConfig_t</code>结构体。下面给出了这个结构体的详细信息:</p>

<pre cid="n222" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;/* BLE library config struct */
&nbsp;typedef struct tag_ble_config
&nbsp;{
&nbsp; &nbsp; &nbsp;uint32_t MEMAddr; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // library memory start address
&nbsp; &nbsp; &nbsp;uint16_t MEMLen; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// library memory size
&nbsp; &nbsp; &nbsp;uint32_t SNVAddr; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // SNV flash start address,must be data-flash area or NULL(bonding information will not be saved)
&nbsp; &nbsp; &nbsp;uint16_t SNVBlock; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// SNV flash block size ( default 512 )
&nbsp; &nbsp; &nbsp;uint8_t SNVNum; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // SNV flash block number ( default 1 )
&nbsp; &nbsp; &nbsp;uint8_t BufMaxLen; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The Controller supported the maximum length of the payload,Range 27-251,ATT_MTU = BufMaxLen-4 ( default 27 )
&nbsp; &nbsp; &nbsp;uint8_t BufNumber; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Maximum number of sent and received packages cached by the controller( default 5 )
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Must be greater than the number of connections.
&nbsp; &nbsp; &nbsp;uint8_t TxNumEvent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Maximum number of TX data in a connection event ( default 1 )
&nbsp; &nbsp; &nbsp;uint8_t TxPower; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Maximum transmit power level( default LL_TX_POWEER_0_DBM(0dBm) )
&nbsp; &nbsp; &nbsp;uint8_t WakeUpTime; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Wake up time value in one RTC count ( default 45 )
&nbsp; &nbsp; &nbsp;uint8_t SelRTCClock; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// RTC clock select LSE,LSI(32768Hz or 32000Hz)( default:0 LSE,1: LSI(32000Hz),2:LSI(32768Hz))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// bit7: select connect timer.0:RTC timer 1:system clock timer(must disable sleep)
&nbsp; &nbsp; &nbsp;uint8_t RxNumEvent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Maximum number of RX data in a connection event ( default equal to BufNumber )
&nbsp; &nbsp; &nbsp;uint8_t MacAddr; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // MAC address,little-endian( factory default )
&nbsp; &nbsp; &nbsp;uint8_t ConnectNumber; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Connect number,lower two bits are peripheral number,followed by central number
&nbsp; &nbsp; &nbsp;uint8_t WindowWidening; &nbsp; &nbsp; &nbsp; &nbsp; // Wait rf start window
&nbsp; &nbsp; &nbsp;uint8_t WaitWindow; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Wait event arrive window
&nbsp; &nbsp; &nbsp;pfnSrandCB srandCB; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Register a program that generate a random seed
&nbsp; &nbsp; &nbsp;pfnSleepCB sleepCB; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Register a program that set idle mode
&nbsp; &nbsp; &nbsp;pfnTempSampleCB tsCB; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Register a program that read the current temperature,determine whether calibration is need
&nbsp; &nbsp; &nbsp;pfnLSECalibrationCB rcCB; &nbsp; &nbsp; &nbsp; // Register a program that RC32K clock calibration
&nbsp; &nbsp; &nbsp;pfnLibStatusErrorCB staCB; &nbsp; &nbsp; &nbsp;// Register a program that library status callback
&nbsp; &nbsp; &nbsp;pfnFlashReadCB readFlashCB; &nbsp; &nbsp; // Register a program that read flash
&nbsp; &nbsp; &nbsp;pfnFlashWriteCB writeFlashCB; &nbsp; // Register a program that write flash
&nbsp;} bleConfig_t; // Library initialization call BLE_LibInit function</pre>

<p cid="n223" mdtype="paragraph">其中最为用户需要关心的就是设备的MAC地址,在此处指定设备MAC地址之后将会将会绑定到BLE模块。并且之后调用<code>BLE_LibInit()</code>闭源函数执行蓝牙初始化。设备的固定MAC地址可以通过函数<code>GetMACAddress()</code>得到。这个MAC地址是烧录在芯片FLASH中的,可以通过FLASH直接读出。FLASH的读取函数也是闭源的,具体读出的细节并不重要。</p>

<p cid="n224" mdtype="paragraph">但是深究整个结构体,我们可以推测到BLE将会在初始化过程中校对系统时间,这个信息在实时通信过程中是非常关键的。</p>

<p cid="n225" mdtype="paragraph">接下来在<code>HAL_Init()</code>函数中注册了一个任务,并获取了其任务的ID。</p>

<pre cid="n226" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;halTaskID = TMOS_ProcessEventRegister( HAL_ProcessEvent );</pre>

<p cid="n227" mdtype="paragraph">执行这个任务的主函数声明如下。这个函数的具体内容就是执行HAL层的各种消息响应。</p>

<pre cid="n228" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;tmosEvents HAL_ProcessEvent( tmosTaskID task_id, tmosEvents events );</pre>

<p cid="n229" mdtype="paragraph">这一部分的内容我们下一节详细聊,我们先回到主线,观察BLE的初始化过程。</p>

<p cid="n230" mdtype="paragraph">接下来执行的任务是初始化GAP协议的相关内容,系统根据不同的角色提供了针对不同BLE所处角色的GAP初始化函数。在Peripheral例程中使用的是作为从机角色的初始化函数<code>GAPRole_PeripheralInit();</code>。这个函数的具体实现随协议栈仍然是闭源的。</p>

<p cid="n231" mdtype="paragraph">接下来到了初始化BLE的最关键部分,<code>void Peripheral_Init()</code>这个函数中给出了作为从机的BLE外设启动的全过程。接下来详细分析一下其中的步骤:</p>

<p cid="n232" mdtype="paragraph">首先使用函数<code>GAPRole_SetParameter()</code>设定GAP模块的参数,完成GAP模块的功能。其中支持的功能包括如下的这些:</p>

<pre cid="n233" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;#define GAPROLE_PROFILEROLE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x300 &nbsp;//!&lt; Reading this parameter will return GAP Role type. Read Only. Size is uint8_t.
&nbsp;#define GAPROLE_IRK &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x301 &nbsp;//!&lt; Identity Resolving Key. Read/Write. Size is uint8_t. Default is all 0, which means that the IRK will be randomly generated.
&nbsp;#define GAPROLE_SRK &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x302 &nbsp;//!&lt; Signature Resolving Key. Read/Write. Size is uint8_t. Default is all 0, which means that the SRK will be randomly generated.
&nbsp;#define GAPROLE_SIGNCOUNTER &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x303 &nbsp;//!&lt; Sign Counter. Read/Write. Size is uint32_t. Default is 0.
&nbsp;#define GAPROLE_BD_ADDR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x304 &nbsp;//!&lt; Device&#39;s Address. Read Only. Size is uint8_t. This item is read from the controller.
&nbsp;#define GAPROLE_ADVERT_ENABLED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x305 &nbsp;//!&lt; Enable/Disable Advertising. Read/Write. Size is uint8_t. Default is TRUE=Enabled.
&nbsp;#define GAPROLE_ADVERT_DATA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x306 &nbsp;//!&lt; Advertisement Data. Read/Write. Max size is B_MAX_ADV_EXT_LEN. Default to all 0.
&nbsp;#define GAPROLE_SCAN_RSP_DATA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x307 &nbsp;//!&lt; Scan Response Data. Read/Write. Max size is B_MAX_ADV_EXT_LEN. Defaults to all 0.
&nbsp;#define GAPROLE_ADV_EVENT_TYPE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x308 &nbsp;//!&lt; Advertisement Type. Read/Write. Size is uint8_t.Default is GAP_ADTYPE_ADV_IND.
&nbsp;#define GAPROLE_ADV_DIRECT_TYPE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x309 &nbsp;//!&lt; Direct Advertisement Address Type. Read/Write. Size is uint8_t. Default is ADDRTYPE_PUBLIC.
&nbsp;#define GAPROLE_ADV_DIRECT_ADDR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x30A &nbsp;//!&lt; Direct Advertisement Address. Read/Write. Size is uint8_t. Default is NULL.
&nbsp;#define GAPROLE_ADV_CHANNEL_MAP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x30B &nbsp;//!&lt; Which channels to advertise on. Read/Write Size is uint8_t. Default is GAP_ADVCHAN_ALL
&nbsp;#define GAPROLE_ADV_FILTER_POLICY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x30C &nbsp;//!&lt; Filter Policy. Ignored when directed advertising is used. Read/Write. Size is uint8_t. Default is GAP_FILTER_POLICY_ALL.
&nbsp;#define GAPROLE_STATE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x30D &nbsp;//!&lt; Reading this parameter will return GAP Peripheral Role State. Read Only. Size is uint8_t.
&nbsp;#define GAPROLE_MAX_SCAN_RES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x30E &nbsp;//!&lt; Maximum number of discover scan results to receive. Default is 0 = unlimited.
&nbsp;#define GAPROLE_MIN_CONN_INTERVAL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x311 &nbsp;//!&lt; Minimum Connection Interval to allow (n * 1.25ms).Range: 7.5 msec to 4 seconds (0x0006 to 0x0C80). Read/Write. Size is uint16_t. Default is 7.5 milliseconds (0x0006).
&nbsp;#define GAPROLE_MAX_CONN_INTERVAL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x312 &nbsp;//!&lt; Maximum Connection Interval to allow (n * 1.25ms).Range: 7.5 msec to 4 seconds (0x0006 to 0x0C80). Read/Write. Size is uint16_t. Default is 4 seconds (0x0C80).
&nbsp;// v5.x
&nbsp;#define GAPROLE_PHY_TX_SUPPORTED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x313 &nbsp;//!&lt; The transmitter PHYs that the Host prefers the Controller to use.Default is GAP_PHY_BIT_ALL
&nbsp;#define GAPROLE_PHY_RX_SUPPORTED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x314 &nbsp;//!&lt; The receiver PHYs that the Host prefers the Controller to use.Default is GAP_PHY_BIT_ALL
&nbsp;#define GAPROLE_PERIODIC_ADVERT_DATA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x315 &nbsp;//!&lt; Periodic advertisement Data. Read/Write. Max size is B_MAX_ADV_PERIODIC_LEN. Default to all 0.
&nbsp;#define GAPROLE_PERIODIC_ADVERT_ENABLED &nbsp; &nbsp; &nbsp; &nbsp; 0x316 &nbsp;//!&lt; bit0:Enable/Disable Periodic Advertising. Read/Write. Size is uint8_t. Default is FALSE=Disable.</pre>

<p cid="n234" mdtype="paragraph">接下来要设置GAP GATT Server的参数,在标准库中仅仅给出了设置设备名的参数,这个设备名将会被广播以方便连接。另外支持设置的参数如下:</p>

<pre cid="n235" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;#define GGS_DEVICE_NAME_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; // RWuint8_t
&nbsp;#define GGS_APPEARANCE_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; // RWuint16_t
&nbsp;#define GGS_PERI_PRIVACY_FLAG_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; // RWuint8_t
&nbsp;#define GGS_RECONNCT_ADDR_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; // RWuint8_t
&nbsp;#define GGS_PERI_CONN_PARAM_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 &nbsp; // RWsizeof(gapPeriConnectParams_t)
&nbsp;#define GGS_PERI_PRIVACY_FLAG_PROPS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5 &nbsp; // RWuint8_t
&nbsp;#define GGS_W_PERMIT_DEVICE_NAME_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;6 &nbsp; // W &nbsp; uint8_t
&nbsp;#define GGS_W_PERMIT_APPEARANCE_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7 &nbsp; // W &nbsp; uint8_t
&nbsp;#define GGS_W_PERMIT_PRIVACY_FLAG_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8 &nbsp; // W &nbsp; uint8_t
&nbsp;#define GGS_CENT_ADDR_RES_ATT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9 &nbsp; // RWuint8_t</pre>

<p cid="n236" mdtype="paragraph">接下来是使用函数<code>GAP_SetParamValue</code>,设定GAP的时间相关参数,具体支持设定的参数包括:</p>

<pre cid="n237" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;// GAP_PARAMETER_ID_DEFINES GAP Parameter IDs
&nbsp;// Timers
&nbsp;#define TGAP_GEN_DISC_ADV_MIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; //!&lt; Minimum time to remain advertising, when in Discoverable mode.Default 0-turns off the timeout. (n * 0.625 mSec).
&nbsp;#define TGAP_LIM_ADV_TIMEOUT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; //!&lt; Maximum time to remain advertising, when in Limited Discoverable mode.Default 180 seconds. (n * 1 seconds)
&nbsp;#define TGAP_DISC_SCAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; //!&lt; Minimum time to perform scanning,Setting this parameter to 0 turns off the timeout.Default 10.24seconds. (n * 0.625 mSec)
&nbsp;​
&nbsp;// when in General Discovery process
&nbsp;#define TGAP_DISC_ADV_INT_MIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; //!&lt; Minimum advertising interval.Default 160. (n * 0.625 mSec)
&nbsp;#define TGAP_DISC_ADV_INT_MAX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 &nbsp; //!&lt; Maximum advertising interval.Default 160. (n * 0.625 mSec)
&nbsp;#define TGAP_DISC_SCAN_INT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; //!&lt; Scan interval used during Link Layer Scanning state.Default 16. (n * 0.625 mSec)
&nbsp;#define TGAP_DISC_SCAN_WIND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 &nbsp; //!&lt; Scan window used during Link Layer Scanning state.Default 16. (n * 0.625 mSec)
&nbsp;​
&nbsp;// when in Connection Establishment process(1M PHY)
&nbsp;#define TGAP_CONN_EST_INT_MIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7 &nbsp; //!&lt; Minimum Link Layer connection interval.Default 80. (n * 1.25 mSec)
&nbsp;#define TGAP_CONN_EST_INT_MAX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8 &nbsp; //!&lt; Maximum Link Layer connection interval.Default 80. (n * 1.25 mSec)
&nbsp;#define TGAP_CONN_EST_SCAN_INT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;9 &nbsp; //!&lt; Scan interval used during Link Layer Initiating state.Default 16. (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_SCAN_WIND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10 &nbsp;//!&lt; Scan window used during Link Layer Initiating state.Default 16. (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_HIGH_SCAN_INT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 11 &nbsp;//!&lt; Scan interval used during Link Layer Initiating state, high duty scan cycle scan parameters (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_HIGH_SCAN_WIND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;12 &nbsp;//!&lt; Scan window used during Link Layer Initiating state, high duty scan cycle scan parameters (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_SUPERV_TIMEOUT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;13 &nbsp;//!&lt; Link Layer connection supervision timeout.Default 2000. (n * 10 mSec)
&nbsp;#define TGAP_CONN_EST_LATENCY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 14 &nbsp;//!&lt; Link Layer connection slave latency.Default 0. (in number of connection events)
&nbsp;#define TGAP_CONN_EST_MIN_CE_LEN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;15 &nbsp;//!&lt; Local informational parameter about minimum length of connection needed.Default 0. (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_MAX_CE_LEN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;16 &nbsp;//!&lt; Local informational parameter about maximum length of connection needed.Default 0. (n * 0.625 mSec)
&nbsp;​
&nbsp;// Proprietary
&nbsp;#define TGAP_PRIVATE_ADDR_INT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 17 &nbsp;//!&lt; Minimum Time Interval between private (resolvable) address changes.Default 15. (n * 1 minute)
&nbsp;#define TGAP_SM_TIMEOUT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 18 &nbsp;//!&lt; SM Message Timeout (milliseconds). Default 30 seconds.
&nbsp;#define TGAP_SM_MIN_KEY_LEN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 19 &nbsp;//!&lt; SM Minimum Key Length supported. Default 7.
&nbsp;#define TGAP_SM_MAX_KEY_LEN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp;//!&lt; SM Maximum Key Length supported. Default 16.
&nbsp;#define TGAP_FILTER_ADV_REPORTS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 21 &nbsp;//!&lt; Filter duplicate advertising reports. Default TRUE.
&nbsp;#define TGAP_SCAN_RSP_RSSI_MIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;22 &nbsp;//!&lt; Minimum RSSI required for scan responses to be reported to the app. Default -127.
&nbsp;#define TGAP_REJECT_CONN_PARAMS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 23 &nbsp;//!&lt; Whether or not to reject Connection Parameter Update Request received on Central device. Default FALSE.
&nbsp;#define TGAP_AUTH_TASK_ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24 &nbsp;//!&lt; Task ID override for Task Authentication control (for stack internal use only)
&nbsp;​
&nbsp;// v5.x
&nbsp;#define TGAP_ADV_TX_POWER &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 25 &nbsp;//!&lt; Indicates the maximum power level Range: -127 &le; N &le; +126 Units: dBm.Default 127(Host has no preference).
&nbsp;#define TGAP_ADV_PRIMARY_PHY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;26 &nbsp;//!&lt; Indicates the PHY on which the advertising packets are transmitted on the primary advertising channel.LE 1M/LE Coded.Default GAP_PHY_VAL_LE_1M.
&nbsp;#define TGAP_ADV_SECONDARY_PHY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;27 &nbsp;//!&lt; LE 1M/LE 2M/LE Coded. Default GAP_PHY_VAL_LE_1M.
&nbsp;#define TGAP_ADV_SECONDARY_MAX_SKIP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 28 &nbsp;//!&lt; Maximum advertising events the Controller can skip before sending the AUX_ADV_IND packets on the secondary advertising channel. Default 0.
&nbsp;#define TGAP_ADV_ADVERTISING_SID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;29 &nbsp;//!&lt; Value of the Advertising SID subfield in the ADI field of the PDU Range:0-15. Default 0.
&nbsp;#define TGAP_ADV_SCAN_REQ_NOTIFY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;30 &nbsp;//!&lt; Scan request notifications enabled.Default 0-disabled.
&nbsp;#define TGAP_ADV_ADVERTISING_DURATION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 31 &nbsp;//!&lt; Advertising duration Range: 0x0001 &ndash; 0xFFFF Time = N * 10 ms. Default 0-No advertising duration.
&nbsp;#define TGAP_ADV_MAX_EVENTS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 32 &nbsp;//!&lt; indicates the maximum number of extended advertising events. Default 0.
&nbsp;​
&nbsp;// when in General Discovery process
&nbsp;#define TGAP_DISC_SCAN_PHY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;33 &nbsp;//!&lt; LE 1M/LE Coded. Default GAP_PHY_BIT_LE_1M.
&nbsp;#define TGAP_DISC_SCAN_CODED_INT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;34 &nbsp;//!&lt; Scan interval used during Link Layer coded Scanning state, when in General Discovery process (n * 0.625 mSec)
&nbsp;#define TGAP_DISC_SCAN_CODED_WIND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 35 &nbsp;//!&lt; Scan window used during Link Layer coded Scanning state, when in General Discovery process (n * 0.625 mSec)
&nbsp;#define TGAP_DISC_SCAN_DURATION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 36 &nbsp;//!&lt; Scan duration Range: 0x0001 &ndash; 0xFFFF Time = N * 10 ms. Default 0-Scan continuously until explicitly disable.
&nbsp;#define TGAP_DISC_SCAN_PERIOD &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 37 &nbsp;//!&lt; Time interval from when the Controller started its last Scan_Duration until it begins the subsequent Scan_Duration.
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//!&lt; Default 0 Periodic scanning disabled.
&nbsp;​
&nbsp;// when in Connection Establishment process(2M PHY)
&nbsp;#define TGAP_CONN_EST_INT_PHY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 38 &nbsp;//!&lt; LE 1M/LE Coded. Default GAP_PHY_BIT_LE_1M.
&nbsp;#define TGAP_CONN_EST_2M_INT_MIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;39 &nbsp;//!&lt; Minimum Link Layer connection interval.Default 80. (n * 1.25 mSec)
&nbsp;#define TGAP_CONN_EST_2M_INT_MAX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;40 &nbsp;//!&lt; Maximum Link Layer connection interval.Default 80. (n * 1.25 mSec)
&nbsp;#define TGAP_CONN_EST_2M_SUPERV_TIMEOUT &nbsp; &nbsp; &nbsp; &nbsp; 41 &nbsp;//!&lt; Link Layer connection supervision timeout.Default 2000. (n * 10 mSec)
&nbsp;#define TGAP_CONN_EST_2M_LATENCY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;42 &nbsp;//!&lt; Link Layer connection slave latency.Default 0. (in number of connection events)
&nbsp;#define TGAP_CONN_EST_2M_MIN_CE_LEN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 43 &nbsp;//!&lt; Local informational parameter about minimum length of connection needed.Default 0. (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_2M_MAX_CE_LEN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 44 &nbsp;//!&lt; Local informational parameter about maximum length of connection needed.Default 0. (n * 0.625 mSec)
&nbsp;​
&nbsp;// when in Connection Establishment process(Coded PHY)
&nbsp;#define TGAP_CONN_EST_CODED_INT_MIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 45 &nbsp;//!&lt; Minimum Link Layer connection interval.Default 80. (n * 1.25 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_INT_MAX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 46 &nbsp;//!&lt; Maximum Link Layer connection interval.Default 80. (n * 1.25 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_SCAN_INT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;47 &nbsp;//!&lt; Scan interval used during Link Layer Initiating state.Default 16. (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_SCAN_WIND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 48 &nbsp;//!&lt; Scan window used during Link Layer Initiating state.Default 16. (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_HIGH_SCAN_INT &nbsp; &nbsp; &nbsp; 49 &nbsp;//!&lt; Scan interval used during Link Layer Initiating state, high duty scan cycle scan parameters (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_HIGH_SCAN_WIND &nbsp; &nbsp;50 &nbsp;//!&lt; Scan window used during Link Layer Initiating state, high duty scan cycle scan parameters (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_SUPERV_TIMEOUT &nbsp; &nbsp;51 &nbsp;//!&lt; Link Layer connection supervision timeout.Default 2000. (n * 10 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_LATENCY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 52 &nbsp;//!&lt; Link Layer connection slave latency.Default 0. (in number of connection events)
&nbsp;#define TGAP_CONN_EST_CODED_MIN_CE_LEN &nbsp; &nbsp; &nbsp; &nbsp;53 &nbsp;//!&lt; Local informational parameter about minimum length of connection needed.Default 0. (n * 0.625 mSec)
&nbsp;#define TGAP_CONN_EST_CODED_MAX_CE_LEN &nbsp; &nbsp; &nbsp; &nbsp;54 &nbsp;//!&lt; Local informational parameter about maximum length of connection needed.Default 0. (n * 0.625 mSec)
&nbsp;​
&nbsp;// periodic advertising
&nbsp;#define TGAP_PERIODIC_ADV_INT_MIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 55 &nbsp;//!&lt; Minimum periodic advertising interval.Range: 0x0006 to 0xFFFF.Default 160. (n * 1.25 mSec)
&nbsp;#define TGAP_PERIODIC_ADV_INT_MAX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 56 &nbsp;//!&lt; Maximum periodic advertising interval.Range: 0x0006 to 0xFFFF.Default 160. (n * 1.25 mSec)
&nbsp;#define TGAP_PERIODIC_ADV_PROPERTIES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;57 &nbsp;//!&lt; Include TxPower in the periodic advertising PDU.
&nbsp;​
&nbsp;#define TGAP_PARAMID_MAX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;58 &nbsp;//!&lt; ID MAX-valid Parameter ID</pre>

<p cid="n238" mdtype="paragraph">之后使用<code>GAPBondMgr_SetParameter</code>设置连接关系,用于描述来凝结相关的信息。</p>

<pre cid="n239" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;// GAPBOND_PROFILE_PARAMETERS GAP Bond Manager Parameters
&nbsp;#define GAPBOND_PERI_PAIRING_MODE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x400 &nbsp;//!&lt; Pairing Mode: @ref GAPBOND_PAIRING_MODE_DEFINES. Read/Write. Size is uint8_t. Default is GAPBOND_PAIRING_MODE_WAIT_FOR_REQ.
&nbsp;#define GAPBOND_PERI_MITM_PROTECTION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x401 &nbsp;//!&lt; Man-In-The-Middle (MITM) basically turns on Passkey protection in the pairing algorithm. Read/Write. Size is uint8_t. Default is 0(disabled).
&nbsp;#define GAPBOND_PERI_IO_CAPABILITIES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x402 &nbsp;//!&lt; I/O capabilities.Read/Write. Size is uint8_t. Default is GAPBOND_IO_CAP_DISPLAY_ONLY @ref GAPBOND_IO_CAP_DEFINES.
&nbsp;#define GAPBOND_PERI_OOB_ENABLED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x403 &nbsp;//!&lt; OOB data available for pairing algorithm. Read/Write. Size is uint8_t. Default is 0(disabled).
&nbsp;#define GAPBOND_PERI_OOB_DATA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x404 &nbsp;//!&lt; OOB Data. Read/Write. size uint8_t. Default is all 0&#39;s.
&nbsp;#define GAPBOND_PERI_BONDING_ENABLED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x405 &nbsp;//!&lt; Request Bonding during the pairing process if enabled.Read/Write. Size is uint8_t. Default is 0(disabled).
&nbsp;#define GAPBOND_PERI_KEY_DIST_LIST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x406 &nbsp;//!&lt; The key distribution list for bonding.size is uint8_t.@ref GAPBOND_KEY_DIST_DEFINES. Default is sEncKey, sIdKey, mIdKey, mSign enabled.
&nbsp;#define GAPBOND_PERI_DEFAULT_PASSCODE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x407 &nbsp;//!&lt; The default passcode for MITM protection. size is uint32_t. Range is 0 - 999,999. Default is 0.
&nbsp;#define GAPBOND_CENT_PAIRING_MODE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x408 &nbsp;//!&lt; Pairing Mode: @refGAPBOND_PAIRING_MODE_DEFINES. Read/Write. Size is uint8_t. Default is GAPBOND_PAIRING_MODE_WAIT_FOR_REQ.
&nbsp;#define GAPBOND_CENT_MITM_PROTECTION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x409 &nbsp;//!&lt; Man-In-The-Middle (MITM) basically turns on Passkey protection in the pairing algorithm. Read/Write. Size is uint8_t. Default is 0(disabled).
&nbsp;#define GAPBOND_CENT_IO_CAPABILITIES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x40A &nbsp;//!&lt; I/O capabilities.Read/Write. Size is uint8_t. Default is GAPBOND_IO_CAP_DISPLAY_ONLY @ref GAPBOND_IO_CAP_DEFINES.
&nbsp;#define GAPBOND_CENT_OOB_ENABLED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x40B &nbsp;//!&lt; OOB data available for pairing algorithm. Read/Write. Size is uint8_t. Default is 0(disabled).
&nbsp;#define GAPBOND_CENT_OOB_DATA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x40C &nbsp;//!&lt; OOB Data. Read/Write. size uint8_t. Default is all 0&#39;s.
&nbsp;#define GAPBOND_CENT_BONDING_ENABLED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x40D &nbsp;//!&lt; Request Bonding during the pairing process if enabled.Read/Write. Size is uint8_t. Default is 0(disabled).
&nbsp;#define GAPBOND_CENT_KEY_DIST_LIST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x40E &nbsp;//!&lt; The key distribution list for bonding.size is uint8_t.@ref GAPBOND_KEY_DIST_DEFINES. Default is sEncKey, sIdKey, mIdKey, mSign enabled.
&nbsp;#define GAPBOND_CENT_DEFAULT_PASSCODE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x40F &nbsp;//!&lt; The default passcode for MITM protection. size is uint32_t. Range is 0 - 999,999. Default is 0.
&nbsp;#define GAPBOND_ERASE_ALLBONDS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x410 &nbsp;//!&lt; Erase all of the bonded devices. Write Only. No Size.
&nbsp;#define GAPBOND_AUTO_FAIL_PAIRING &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x411 &nbsp;//!&lt; TEST MODE (DO NOT USE) to automatically send a Pairing Fail when a Pairing Request is received. Read/Write. size is uint8_t. Default is 0 (disabled).
&nbsp;#define GAPBOND_AUTO_FAIL_REASON &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x412 &nbsp;//!&lt; TEST MODE (DO NOT USE) Pairing Fail reason when auto failing. Read/Write. size is uint8_t. Default is 0x05 (SMP_PAIRING_FAILED_NOT_SUPPORTED).
&nbsp;#define GAPBOND_KEYSIZE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x413 &nbsp;//!&lt; Key Size used in pairing. Read/Write. size is uint8_t. Default is 16.
&nbsp;#define GAPBOND_AUTO_SYNC_WL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x414 &nbsp;//!&lt; Clears the White List adds to it each unique address stored by bonds in NV. Read/Write. Size is uint8_t. Default is FALSE.
&nbsp;#define GAPBOND_BOND_COUNT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x415 &nbsp;//!&lt; Gets the total number of bonds stored in NV. Read Only. Size is uint8_t. Default is 0 (no bonds).
&nbsp;#define GAPBOND_BOND_FAIL_ACTION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x416 &nbsp;//!&lt; Possible actions Central may take upon an unsuccessful bonding. Write Only. Size is uint8_t. Default is 0x02 (Terminate link upon unsuccessful bonding).
&nbsp;#define GAPBOND_ERASE_SINGLEBOND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x417 &nbsp;//!&lt; Erase a single bonded device. Write only. Must provide address type followed by device address.
&nbsp;#define GAPBOND_BOND_AUTO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x418 &nbsp;//!&lt; Auto save bonds into FLASH. Write Only. size is uint8_t. Default is 1(enabled).
&nbsp;#define GAPBOND_BOND_UPDATE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x419 &nbsp;//!&lt; Save current bonds into FLASH. Write Only. No Size.
&nbsp;#define GAPBOND_DISABLE_SINGLEBOND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x41A &nbsp;//!&lt; Disable a single bonded device. Write only. Must provide address type followed by device address.
&nbsp;#define GAPBOND_ENABLE_SINGLEBOND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x41B &nbsp;//!&lt; Ensable a single bonded device. Write only. Must provide address type followed by device address.
&nbsp;#define GAPBOND_DISABLE_ALLBONDS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x41C &nbsp;//!&lt; Disable all of the bonded devices. Write Only. No Size.
&nbsp;#define GAPBOND_ENABLE_ALLBONDS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x41D &nbsp;//!&lt; Ensable all of the bonded devices. Write Only. No Size.
&nbsp;#define GAPBOND_ERASE_AUTO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x41E &nbsp;//!&lt; Auto erase all of the bonded devices when the maximum number is reached.Size is uint8_t. Default is 1(enabled).
&nbsp;#define GAPBOND_AUTO_SYNC_RL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x41F &nbsp;//!&lt; Clears the Resolving List adds to it each unique address stored by bonds in NV. Read/Write. Size is uint8_t. Default is FALSE.</pre>

<p cid="n240" mdtype="paragraph">接下来的代码就是注册一个GATT的服务,并注册相关回调响应函数。</p>

<p cid="n241" mdtype="paragraph">最终进入主循环,将系统的权限交由TMOS操作系统。</p>

<p cid="n242" mdtype="paragraph">我们可以在回调函数中找到响应的处理代码段,比如在BLE_UART例程中的函数:</p>

<pre cid="n243" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;static bStatus_t ble_uart_ReadAttrCB(
&nbsp; &nbsp; &nbsp;uint16 connHandle,
&nbsp; &nbsp; &nbsp;gattAttribute_t *pAttr,
&nbsp; &nbsp; &nbsp;uint8 *pValue, uint16 *pLen,
&nbsp; &nbsp; &nbsp;uint16 offset,
&nbsp; &nbsp; &nbsp;uint16 maxLen,
&nbsp; &nbsp; &nbsp;uint8 method );</pre>

<p cid="n244" mdtype="paragraph">这个函数将有实时嵌入式系统进行调用,最终支持整个逻辑层功能的实现。</p>

<h4 cid="n245" mdtype="heading">操作系统层TMOS</h4>

<p cid="n246" mdtype="paragraph">在这套程序中提供了一个基础但是强大的操作系统。其基本的使用框架就是注册响应函数,发布消息,响应消息。TMOS的执行方式就是在主循环中按照特定的优先级执行相关的响应例程。从数据手册中描述上应该是通过队列的方式实现,是不是有可能用极左树(笑)。</p>

<p cid="n247" mdtype="paragraph">接下来罗列一下最关键的几项函数:</p>

<pre cid="n248" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;// 创建一个任务:
&nbsp;extern tmosTaskID TMOS_ProcessEventRegister( pTaskEventHandlerFn eventCb );
&nbsp;// 回调函数原型;
&nbsp;typedef tmosEvents (*pTaskEventHandlerFn)( tmosTaskID taskID, tmosEvents event );
&nbsp;tmosEvents EventController( tmosTaskID task_id, tmosEvents events);
&nbsp;// 发送一个消息(执行一个时事件)
&nbsp;extern bStatus_t tmos_start_task( tmosTaskID taskID, tmosEvents event, tmosTimer time );</pre>

<p cid="n249" mdtype="paragraph">在回调函数中可以按照下面的模板进行任务的响应:</p>

<pre cid="n250" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;tmosEvents HAL_ProcessEvent( tmosTaskID task_id, tmosEvents events )
&nbsp;{
&nbsp; &nbsp; &nbsp;// 初始化局部变量
&nbsp;   uint8 * msgPtr;
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;// 从HAL层扒一点示例过来,有一个直观感受
&nbsp;    // 处理HAL层消息,调用tmos_msg_receive读取消息,处理完成后删除消息。
&nbsp;    if ( events &amp; SYS_EVENT_MSG )
&nbsp;    { &nbsp;
&nbsp; &nbsp;       // 样例
&nbsp; &nbsp;      msgPtr = tmos_msg_receive( task_id );
&nbsp; &nbsp;      if ( msgPtr )
&nbsp; &nbsp;      {
&nbsp; &nbsp; &nbsp;      /* De-allocate */
&nbsp; &nbsp; &nbsp;      tmos_msg_deallocate( msgPtr );
&nbsp; &nbsp;      }
&nbsp; &nbsp;      return events ^ SYS_EVENT_MSG;
&nbsp;    }
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;// 这里是消息响应的框架
&nbsp;   if ( events &amp; TASK1 )
&nbsp;   {}
&nbsp; &nbsp; &nbsp;if(events &amp; TASK2)
&nbsp; &nbsp;{}
&nbsp; &nbsp; &nbsp;return 0;
&nbsp;} &nbsp; &nbsp;</pre>

<p cid="n251" mdtype="paragraph">除此之外,系统也提供了堆管理的功能提供了堆的申请、释放和内存基础操作。</p>

<pre cid="n252" lang="C++" mdtype="fences" spellcheck="false">
&nbsp;extern uint32_t tmos_rand( void ); // pseudo-random number
&nbsp;extern BOOL tmos_memcmp( const void *src1, const void *src2, uint32_t len ); // TRUE - same, FALSE - different
&nbsp;extern BOOL tmos_isbufset( uint8_t *buf, uint8_t val, uint32_t len ); // TRUE if all &quot;val&quot;,FALSE otherwise
&nbsp;extern uint32_t tmos_strlen( char *pString );
&nbsp;extern void tmos_memset( void * pDst, uint8_t Value, uint32_t len );
&nbsp;extern void tmos_memcpy( void *dst, const void *src, uint32_t len ); // Generic memory copy.</pre>

<p cid="n253" mdtype="paragraph">由于这样的任务并非抢占式,因此,我们在书写每一个event时应当尽量保证其中的内容尽可能精练。</p>

lugl4313820 发表于 2022-4-19 13:11

分析这个确实需要耐心跟时间,我也就用什么看什么,但是这个任务系统要处理复杂的程序还是不是很合适。

nmg 发表于 2022-4-27 17:37

<p>代码你用编辑器的代码插入功能会清晰些</p>

javnson 发表于 2022-4-28 09:22

nmg 发表于 2022-4-27 17:37
代码你用编辑器的代码插入功能会清晰些

<p>是这样的,我是markdown直接编辑好贴过来的,其实还好</p>

iampxj 发表于 2022-8-4 18:37

<p>很不错<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan88.gif" width="59" /></p>

OKJust 发表于 2022-8-14 15:26

<p>感觉挺nb的,要是能硬件pin对pin,软件完全兼容一些现有的方案就太棒了!</p>

RunDong 发表于 2023-6-29 02:12

<p>感谢分享 用ch571f做过一个mesh灯具</p>

<p>&nbsp;</p>
页: [1]
查看完整版本: 【沁恒CH582】蓝牙协议栈的结构分析