Bluetooth Low Energy Application Developer's Guide
Bluetooth Low Energy Application Developer's Guide
Chapter 1
Introduction
This document explains how to integrate the Bluetooth® Low Energy (BLE) Host Stack in a BLE application and provides detailed
explanation of the most commonly used APIs and code examples.
The document also sets out the prerequisites and the initialization of the BLE Host Stack, followed by the presentation of APIs
grouped by layers and by application role, as described below.
First, the Generic Access Profile (GAP) layer is divided into two sections according to the GAP role of the device: Central and
Peripheral.
The basic setup of two such devices is explained with code examples, such as how to prepare the devices for connections, how
to connect them together, and pairing and bonding processes.
Next, the Generic Attribute Profile (GATT) layer introduces the APIs required for data transfer between the two connected devices.
Again, the chapter is divided into two sections according to the GATT role of the device: Client and Server.
The document further describes the usage of the GATT Database APIs in the application to manipulate the data in the GATT
server database.
Then, the document shows a user-friendly method to statically build a GATT Database. The method involves the use of a predefined
set of macros that the application may include to build the database at application compile-time.
The following section contains instructions on how to build a custom profile. The subsequent section is dedicated to the structure
of the typical application.
Additionally, the document has a chapter dedicated to low-power management and how the low-power modes of the hardware
and software can be used by an application.
The next section contains a description of the Over The Air Programming (OTAP) capabilities offered by the Host Stack via a
dedicated Service/Profile and how to use them in an application. This section also contains a detailed description of the
components of the Framework involved in the OTAP process and the Bootloader application, which does the actual upgrade of
the image on a device.
Finally, the document has a section, which describes how to build a BLE application when the Host Stack is running on a separate
processor.
Chapter 2
Prerequisites
The BLE Host Stack library contains a number of external references that the application must define to provide the full functionality
of the Host.
Failing to do so results in linkage errors when trying to build the application binary.
The application developer need not care about the format of the bonding information as this is handled by the host stack. Each
bonding data slot must contain one bonding header blob, one dynamic data blob, one static data blob, one device information
blob and an array of descriptor blobs equal to gcGapMaximumSavedCccds_c (ble_constants.h). A slot is uniquely identified by
the mEntryIdx parameter. A descriptor is uniquely identified by the pair mEntryIdx - mDescriptorIndex.
If one or more pointers passed as parameters are NULL, the read from or write to the corresponding blob of the bonding slot must
be ignored. The erase function must clear the entire bonding data slot specified by the entry index.
The current implementation of the aforementioned functions use either the framework NVM module or a RAM buffer . Additional
details about the NVM configuration and functionality can be found in the Connectivity Framework Reference Manual.
To enable the NVM mechanism make sure:
• gAppUseNvm_d (app_preinclude.h) is set to 1 and
• gUseNVMLink_d=1 in the linker options of the toolchain.
If gAppUseNvm_d is set to 0 then all bonding data will be stored in RAM and will be accesible until reset or power cycle.
NOTE
The default NVM module configurations are applied in the app_preinclude.h file if gAppUseNvm_d is set to 1.
Chapter 3
Host Stack Initialization and APIs
• ble_host_peripheral_lib.a
— Supports only APIs for the GAP Peripheral and GAP Broadcaster roles
— Supports only APIs for the GATT Server role
• ble_host_central_lib.a
— Supports only APIs for the GAP Central and GAP Observer roles
— Supports only APIs for the GATT Client role
If one attempts to use an API that is not supported (for instance, calling Gap_Connect with the ble_host_peripheral_lib.a), then
the API returns the gBleFeatureNotSupported_c error code.
NOTE
See the Bluetooth Low Energy Host StackAPI Reference Manual for explicit information regarding API support.
Each function documentation contains this information in the Remarks section.
4.1.1 Scanning
The most basic setup for a Central device begins with scanning, which is performed by the following function from gap_interface.h:
bleResult_t Gap_StartScanning
(
gapScanningParameters_t * pScanningParameters,
gapScanningCallback_t scanningCallback
);
If the pScanningParameters pointer is NULL, the currently set parameters are used. If no parameters have been set after a device
power-up, the standard default values are used:
#define gGapDefaultScanningParameters_d \
{ \
/* type */ gGapScanTypePassive_c, \
/* interval */ gGapScanIntervalDefault_d, \
/* window */ gGapScanWindowDefault_d, \
/* ownAddressType */ gBleAddrTypePublic_c, \
/* filterPolicy */ gScanAll_c \
}
The easiest way to define non-default scanning parameters is to initialize a gapScanningParameters_t structure with the above
default and change only the required fields.
For example, to perform active scanning and only scan for devices in the White List, the following code can be used:
gapScanningParameters_t scanningParameters = gGapDefaultScanningParameters_d;
scanningParameters.type = gGapScanTypeActive_c;
scanningParameters.filterPolicy = gScanWhiteListOnly_c;
Gap_StartScanning(&scanningParameters, scanningCallback);
The scanningCallback is triggered by the GAP layer to signal events related to scanning.
The most important event is the gDeviceScanned_c event, which is triggered each time an advertising device is scanned. This
event’s data contains information about the advertiser:
typedef struct gapScannedDevice_tag {
bleAddressType_t addressType ;
bleDeviceAddress_t aAddress ;
int8_t rssi ;
uint8_t dataLength ;
uint8_t * data ;
bleAdvertisingReportEventType_t advEventType ;
} gapScannedDevice_t;
If this information signals a known Peripheral that the Central wants to connect to, the latter must stop scanning and connect to
the Peripheral.
To stop scanning, call this function:
bleResult_t Gap_StopScanning (void);
By default, the GAP layer is configured to report all scanned devices to the application using the gDeviceScanned_c event type.
However, some use cases may require to perform specific GAP Discovery Procedures in which the advertising reports have to
be filtered by the Flags AD value from the advertising data. Other use cases require the Host stack to automatically initiate a
connection when a specific device has been scanned.
To enable filtering based on the Flags AD value or to set device addresses for automatic connections, the following function must
be called before the scanning is started:
bleResult_t Gap_SetScanMode
(
gapScanMode_t scanMode,
gapAutoConnectParams_t* pAutoConnectParams
);
The default value for the scan mode is gNoDiscovery_c, which reports all packets regardless of their content and does not perform
any automatic connection.
To enable Limited Discovery, the gLimitedDiscovery_c value must be used, while the gGeneralDiscovery_c value activates
General Discovery.
To enable automatic connection when specific devices are scanned, the gAutoConnect_c value must be set, in which case the
pAutoConnectParams parameter must point to the structure that holds the target device addresses and the connection
parameters to be used by the Host for these devices.
{
/* Handle error */
}
else
{
/* There is no need to wait for the gScanStateChanged_c event because
* the commands are queued in the host task
* and executed consecutively. */
result = Gap_Connect(&connReqParams, connectionCallback);
if (gBleSuccess_c != result)
{
/* Handle error */
}
}
}
break;
}
/* ... */
}
}
The connCallback is triggered by GAP to send all events related to the active connection. It has the following prototype:
typedef void (* gapConnectionCallback_t )
(
deviceId_t deviceId,
gapConnectionEvent_t * pConnectionEvent
);
The very first event that should be listened inside this callback is the gConnEvtConnected_c event. If the application decides to
drop the connection establishment before this event is generated, it should call the following macro:
#define Gap_CancelInitiatingConnection()\
Gap_Disconnect(gCancelOngoingInitiatingConnection_d)
This is useful, for instance, when the application chooses to use an expiration timer for the connection request.
Upon receiving the gConnEvtConnected_c event, the application may proceed to extract the necessary parameters from the
event data (pConnectionEvent->event.connectedEvent). The most important parameter to be saved is the deviceId.
The deviceId is an unique 8-bit, unsigned integer, used to identify an active connection for subsequent GAP and GATT API calls.
All functions related to a certain connection require a deviceId parameter. For example, to disconnect, call this function:
bleResult_t Gap_Disconnect
(
deviceId_t deviceId
);
bleResult_t Gap_Pair
(
deviceId_t deviceId,
gapPairingParameters_t * pPairingParameters
);
The pairing parameters are shown here:
typedef struct gapPairingParameters_tag {
bool_t withBonding ;
gapSecurityModeAndLevel_t securityModeAndLevel ;
uint8_t maxEncryptionKeySize ;
gapIoCapabilities_t localIoCapabilities ;
bool_t oobAvailable ;
gapSmpKeyFlags_t centralKeys ;
gapSmpKeyFlags_t peripheralKeys ;
bool_t leSecureConnectionSupported ;
bool_t useKeypressNotifications ;
} gapPairingParameters_t;
The names of the parameters are self-explanatory. The withBonding flag should be set to TRUE if the Central must/wants to
bond.
For the Security Mode and Level, the GAP layer defines them as follows:
• Security Mode 1 Level 1 stands for no security requirements
• Except for Level 1 (which is only used with Mode 1), Security Mode 1 requires encryption, while Security Mode 2 requires
data signing
• Mode 1 Level 2 and Mode 2 Level 1 do not require authentication (in other words, they allow Just Works pairing, which has
no MITM protection), while Mode 1 Level 3 and Mode 2 Level 2 require authentication (must pair with PIN or OOB data,
which provide MITM protection).
• Starting with Bluetooth specification 4.2 OOB pairing offers MITM protection only in certain conditions. The application
must inform the stack if its the OOB data exchange capabilities offer MITM protection via a dedicated API.
• Security Mode 1 Level 4 is reserved for authenticated pairing (with MITM protection) using a LE Secure Connections
pairing method.
• If a LE Secure Connections pairing method is used but it does not offer MITM protection then the pairing completes with
Security Mode 1 level 2.
The centralKeys should have the flags set for all the keys that are available in the application. The IRK is mandatory if the Central
is using a Private Resolvable Address, while the CSRK is necessary if the Central wants to use data signing. The LTK is provided
by the Peripheral and should only be included if the Central intends on becoming a Peripheral in future reconnections (GAP role
change).
The peripheralKeys should follow the same guidelines. The LTK is mandatory if encryption is to be performed, while the peer’s
IRK should be requested if the Peripheral is using Private Resolvable Addresses.
See Table 2. Key Distribution guidelines on page 14 for detailed guidelines regarding key distribution.
The first three rows are both guidelines for Pairing Parameters (centralKeys and peripheralKeys) and for distribution of keys with
Gap_SendSmpKeys.
If LE Secure Connections Pairing is performed (BLE 4.2 and above), then the LTK is generated internally, so the corresponding
bits in the key distribution fields from the pairing parameters are ignored by the devices.
The Identity Address shall be distributed if the IRK is also distributed (its flag has been set in the Pairing Parameters). Therefore,
it can be “asked” only by asking for IRK (it does not have a separate flag in a gapSmpKeyFlags_t structure), hence the N/A.
The negotiation of the distributed keys is as follows:
• In the SMP Pairing Request (started by Gap_Pair), the Central sets the flags for the keys it wants to distribute
(centralKeys) and receive (peripheralKeys).
— CENTRAL PERIPHERAL
Long Term Key (LTK) If it wants to be a If it wants encryption If it wants encryption If it wants to become a
+EDIV +RAND peripheral in a future central in a future
reconnection reconnection
Identity Resolving If it uses or intends to If a peripheral is using If it uses or intends to If a central is using a
Key (IRK) use private resolvable a private resolvable use private resolvable private resolvable
addresses address addresses address
Connection Signature If it wants to sign data If it wants the If it wants to sign data If it wants the Central to
Resolving Key as GATT Client peripheral to sign data as GATT Client sign data as GATT
(CSRK) as GATT Client Client
Identity address If it distributes the IRK N/A If it distributes the IRK N/A
• The Peripheral examines the two distributions and must send an SMP Pairing Response (started by the
Gap_AcceptPairingRequest) after performing any changes it deems necessary. The Peripheral is only allowed to set to 0
some flags that are set to 1 by the Central, but not the other way around. For example, it cannot request/distribute keys
that were not offered/requested by the Central. If the Peripheral is adverse to the Central’s distributions, it can reject the
pairing by using the Gap_RejectPairing function.
• The Central examines the updated distributions from the Pairing Response. If it is adverse to the changes made by the
Peripheral, it can reject the pairing (Gap_RejectPairing). Otherwise, the pairing continues and, during the key distribution
phase (the gConnEvtKeyExchangeRequest_c event) only the final negotiated keys are included in the key structure sent
with Gap_SendSmpKeys.
• For LE Secure Connections (Both devices set the SC bit in the AuthReq field of the Pairing Request and Pairing Response
packets) the LTK is not distribuited it is generated and the corresponding bit in the Inittiator Key Distribution and Responder
Key Distribution fields of the Pairing Response packet shall be set to 0.
If LE Secure Connections Pairing (BLE 4.2 and above) is used, and OOB data needs to be exchanged, the application must obtain
the local LE SC OOB Data from the host stack by calling the Gap_LeScGetLocalOobData function. The data is contained by the
generic gLeScLocalOobData_c event.
The local LE SC OOB Data is refreshed in the following situations:
• The Gap_LeScRegeneratePublicKey function is called (the gLeScPublicKeyRegenerated_c generic event is also
generated as a result of this API).
• The device is reset (which also causes the Public Key to be regenerated).
If the pairing continues, the following connection events may occur:
• Request events
— gConnEvtPasskeyRequest_c: a PIN is required for pairing; the application must respond with the
Gap_EnterPasskey(deviceId, passkey).
— gConnEvtOobRequest_c: if the pairing started with the oobAvailable set to TRUE by both sides; the application must
respond with the Gap_ProvideOob(deviceId, oob).
— gConnEvtKeyExchangeRequest_c: the pairing has reached the key exchange phase; the application must respond
with the Gap_SendSmpKeys(deviceId, smpKeys).
— gConnEvtLeScOobDataRequest_c: the stack requests the LE SC OOB Data received from the peer (r, Cr and Addr);
the application must respond with Gap_LeScSetPeerOobData(deviceId, leScOobData).
— gConnEvtLeScDisplayNumericValue_c: the stack requests the display and confirmation of the LE SC Numeric
Comparison Value; the application must respond with Gap_LeScValidateNumericValue(deviceId, ncvValidated).
• Informational events
— gConnEvtKeysReceived_c: the key exchange phase is complete; keys are automatically saved in the internal device
database and are also provided to the application for immediate inspection; application does not have to save the
keys in NVM storage because this is done internally if withBonding was set to TRUE by both sides.
— gConnEvtAuthenticationRejected_c: the peer device rejected the pairing; the rejectReason parameter of the event
data indicates the reason that the Peripheral does not agree with the pairing parameters (it cannot be
gLinkEncryptionFailed_c because that reason is reserved for the link encryption failure).
— gConnEvtPairingComplete_c: the pairing process is complete, either successfully, or an error may have occurred
during the SMP packet exchanges; note that this is different from the gConnEvtKeyExchangeRequest_c event; the
latter signals that the pairing was rejected by the peer, while the former is used for failures due to the SMP packet
exchanges.
— gConnEvtLeScKeypressNotification_c: the stack informs the application that a remote SMP Keypress Notification
has been received during Passkey Entry Pairing Method.
After the link encryption or pairing is completed successfully, the Central may immediately start exchanging data using the GATT
APIs.
Figure 3. Central pairing flow – APIs and eventsGap_RejectPairing may be called on any pairing event
4.2.1 Advertising
Before starting advertising, the advertising parameters should be configured. Otherwise, the following defaults are used:
#define gGapDefaultAdvertisingParameters_d \
{ \
/* minInterval */ gGapAdvertisingIntervalDefault_c, \
/* maxInterval */ gGapAdvertisingIntervalDefault_c, \
/* advertisingType */ gConnectableUndirectedAdv_c, \
/* addressType */ gBleAddrTypePublic_c, \
/* directedAddressType */ gBleAddrTypePublic_c, \
/* directedAddress */ {0, 0, 0, 0, 0, 0}, \
/* channelMap */ (gapAdvertisingChannelMapFlags_t) (gGapAdvChanMapFlag37_c |
gGapAdvChanMapFlag38_c | gGapAdvChanMapFlag39_c), \
/* filterPolicy */ gProcessAll_c \
}
To set different advertising parameters, a gapAdvertisingParameters_t structure should be allocated and initialized with defaults.
Then, the necessary fields may be modified.
After that, the following function should be called:
bleResult_t Gap_SetAdvertisingParameters
(
gapAdvertisingParameters_t * pAdvertisingParameters
);
The application should listen to the gAdvertisingParametersSetupComplete_c generic event.
Next, the advertising data should be configured and, if the advertising type supports active scanning, the scan response data
should also be configured. If either of these is not configured, they are defaulted to empty data.
The function used to configure the advertising and/or scan response data is shown here:
bleResult_t Gap_SetAdvertisingData
(
gapAdvertisingData_t * pAdvertisingData,
gapScanResponseData_t * pScanResponseData
);
Either of the two pointers may be NULL, in which case they are ignored (the corresponding data is left as it was previously
configured, or empty if it has never been set), but not both at the same time.
The application should listen to the gAdvertisingDataSetupComplete_c generic event.
After all the necessary setup is done, advertising may be started with this function:
bleResult_t Gap_StartAdvertising
(
gapAdvertisingCallback_t advertisingCallback,
gapConnectionCallback_t connectionCallback
);
The advertisingCallback is used to receive advertising events (advertising state changed or advertising command failed), while
the connectionCallback is only used if a connection is established during advertising.
The connection callback is the same as the callback used by the Central when calling the Gap_Connect function.
If a Central initiates a connection to this Peripheral, the gConnEvtConnected_c connection event is triggered.
To stop advertising while the Peripheral has not yet received any connection requests, use this function:
bleResult_t Gap_StopAdvertising (void);
This function should not be called after the Peripheral enters a connection.
Although the Peripheral does not initiate any kind of security procedure, it can inform the Central about its security requirements.
This is usually done immediately after the connection to avoid exchanging useless packets for requests that might be denied
because of insufficient security.
The informing is performed through the Slave Security Request packet at SMP level. To use it, the following GAP API is provided:
bleResult_tGap_SendSlaveSecurityRequest
(
deviceId_t deviceId,
bool_t bondAfterPairing,
gapSecurityModeAndLevel_t securityModeLevel
);
The bondAfterPairing parameter indicates to the Central whether this Peripheral can bond and the securityModeLevel informs
about the required security mode and level that the Central should pair for. See Section 4.1.3 for an explanation about security
modes and levels, as defined by the GAP module.
This request expects no reply, nor any immediate action from the Central. The Central may easily choose to ignore the Slave
Security Request.
If the two devices have bonded in the past, the Peripheral should expect to receive a gConnEvtLongTermKeyRequest_c
connection event (unless LE Secure Connections Pairing was performed, as specified in BLE 4.2 and above), which means that
the Central has also recognized the bond and, instead of pairing, it goes directly to encrypting the link using the previously shared
LTK. At this point, the local LE Controller requests that the Host provides the same LTK it exchanged during pairing.
When the devices have been previously paired, along with the Peripheral’s LTK, the EDIV (2 bytes) and RAND (8 bytes) values
were also sent (their meaning is defined by the SMP). Therefore, before providing the key to the Controller, the application should
check that the two values match with those received in the gConnEvtLongTermKeyRequest_c event. If they do, the application
should reply with:
bleResult_tGap_ProvideLongTermKey
(
deviceId_t deviceId,
uint8_t * aLtk,
uint8_t ltkSize
);
The LTK size cannot exceed the maximum value of 16.
If the EDIV and RAND values do not match, or if the Peripheral does not recognize the bond, it can reject the encryption request
with:
bleResult_tGap_DenyLongTermKey
(
deviceId_t deviceId
);
If LE SC Pairing was used then the LTK is generated internally by the host stack and it is not requested from the application during
post-bonding link encryption. In this scenario, the application is only notified of the link encryption through the
gConnEvtEncryptionChanged_c connection event.
If the devices are not bonded, the Peripheral should expect to receive the gConnEvtPairingRequest_c, indicating that the Central
has initiated pairing.
If the application agrees with the pairing parameters (see Pairing and bonding on page 12 for detailed explanations), it can reply
with:
bleResult_tGap_AcceptPairingRequest
(
deviceId_t deviceId,
gapPairingParameters_t * pPairingParameters
);
This time, the Peripheral sends its own pairing parameters, as defined by the SMP.
After sending this response, the application should expect to receive the same pairing events as the Central (see Pairing and
bonding on page 12), with one exception: the gConnEvtPasskeyRequest_c event is not called if the application sets the Passkey
(PIN) for pairing before the connection by calling the API:
bleResult_tGap_SetLocalPasskey
(
uint32_t passkey
);
This is done because, usually, the Peripheral has a static secret PIN that it distributes only to trusted devices. If, for any reason,
the Peripheral must dynamically change the PIN, it can call the aforementioned function every time it wants to, before the pairing
starts (for example, right before sending the pairing response with Gap_AcceptPairingRequest).
If the Peripheral application never calls Gap_SetLocalPasskey, then the gConnEvtPasskeyRequest_c event is sent to the
application as usual.
The following API can be used by the Peripheral to reject the pairing process:
bleResult_tGap_RejectPairing
(
deviceId_t deviceId,
gapAuthenticationRejectReason_t reason
);
The reason should indicate why the application rejects the pairing. The value gLinkEncryptionFailed_c is reserved for the
gConnEvtAuthenticationRejected_c connection event to indicate the link encryption failure rather than pairing failures. Therefore,
it is not meant as a pairing reject reason.
The Gap_RejectPairing function may be called not only after the Pairing Request was received, but also during the pairing process,
when handling pairing events or asynchronously, if for any reason the Peripheral decides to abort the pairing. This also holds true
for the Central.
Figure 4. Peripheral pairing flow – APIs and eventsGap_RejectPairing may be called on any pairing event
For both the Central and the Peripheral, bonding is performed internally and is not the application’s concern. The application is
informed about whether or not bonding occurred through the gConnEvtPairingComplete_c event parameters.
#ifndef gBleDefaultTxTime_c
#define gBleDefaultTxTime_c 0x0848
#endif
The device can update the data length anytime, while in connection. The function that triggers this mechanism is the following:
bleResult_tGap_UpdateLeDataLength
(
deviceId_t deviceId,
uint16_t txOctets,
uint16_t txTime
);
After the procedure executes, a gConnEvtLeDataLengthChanged_c connection event is triggered with the maximum values for
number of payload octets and time to transmit and receive a link layer data channel PDU. The event is send event if the remote
device initiates the procedure. This procedure is detailed below:
The lifetime of the Private Address (NRPA or RPA) is a number of seconds contained by the gGapHostPrivacyTimeout external
constant, which is defined in the ble_config.c source file. The default value for this is 900 (15 minutes).
bool_t enable,
uint8_t * aOwnIrk,
uint8_t peerIdCount,
gapIdentityInformation_t* aPeerIdentities
);
When enable is set to TRUE, aOwnIrk parameter shall not be NULL, peerIdCount shall not be zero or greater than
gcGapControllerResolvingListSize_c, and aPeerIdentities shall not be NULL.
The IRK defined by aOwnIrk is used by the Controller to periodically generate a new RPA. The lifetime of the RPA is a number
of seconds contained by the gGapControllerPrivacyTimeout external constant, which is defined in the ble_config.c source file.
The default value for this is 900 (15 minutes).
The aPeerIdentities is an array of identity information for each bonded device. The identity information contains the device’s
identity address (public or random static address) and the device’s IRK. This array can be obtained from the Host with the
Gap_GetBondedDevicesIdentityInformation API.
Enabling Controller Privacy involves a quick sequence of commands to the Controller. When the sequence is complete, the
gControllerPrivacyStateChanged_c generic event is triggered.
4.4.3.2 Advertising
When a Peripheral starts advertising while Controller Privacy is enabled, the ownAddressType field of the advertising parameter
structure is unused. Instead, the Controller always generates an RPA and advertises with it as Advertising Address.
4.4.3.3 Connected
When a device connects while Controller Privacy is enable, the gConnEvtConnected_c connection event parameter structure
contains more relevant fields than without Controller Privacy.
The peerRpaResolved field equals TRUE if the peer was using an RPA that was resolved using an IRK from the list. In that case,
the peerAddressType and peerAddress fields contain the identity address of the resolved device, and the actual RPA used to
create the connection (the RPA that a Central used when initiating the connection, or the RPA that the Peripheral advertised with)
is contained by the peerRpa field.
The localRpaUsed field equals TRUE if the local Controller was automatically generating an RPA when the connection was
created, and the actual RPA is contained by the localRpa field.
Chapter 5
Generic Attribute Profile (GATT) Layer
The GATT layer contains the APIs for discovering services and characteristics and transferring data between devices.
The GATT layer is built on top of the Attribute Protocol (ATT), which transfers data between BLE devices on a dedicated L2CAP
channel (channel ID 0x04).
As soon as a connection is established between devices, the GATT APIs are readily available. No initialization is required because
the L2CAP channel is automatically created.
To identify the GATT peer instance, the same deviceId value from the GAP layer (obtained in the
gConnEvtConnected_cconnection event) is used.
There are two GATT roles that define the two devices exchanging data over ATT:
• GATT Server – the device that contains a GATT Database, which is a collection of services and characteristics exposing
meaningful data. Usually, the Server responds to requests and commands sent by the Client, but it can be configured to
send data on its own through notifications and indications.
• GATT Client – the “active” device that usually sends requests and commands to the Server to discover Services and
Characteristics on the Server’s Database and to exchange data.
There is no fixed rule deciding which device is the Client and which one is the Server. Any device may initiate a request at any
moment, thus temporarily acting as a Client, at which the peer device may respond, provided it has the Server support and a
GATT Database.
Often, a GAP Central acts as a GATT Client to discover Services and Characteristics and obtain data from the GAP Peripheral,
which usually has a GATT Database. Many standard BLE profiles assume that the Peripheral has a database and must act as a
Server. However, this is by no means a general rule.
gattClientProcedureCallback_t callback
);
The procedureType parameter may be used to identify the procedure that was started and has reached completion. Only one
procedure may be active at a given moment. Trying to start another procedure while a procedure is already in progress returns
the error gGattAnotherProcedureInProgress_c.
The procedureResult parameter indicates whether the procedure completes successfully or an error occurs. In the latter case,
the error parameter contains the error code.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
}
}
GattClient_RegisterProcedureCallback(gattClientProcedureCallback);
NOTE
This number is fixed and cannot be increased in BLE 4.1.
To maintain a logical mapping between radio packets and ATT packets, the Standard has set the default length of an ATT packet
(the so-called ATT_MTU) also equal to 23. Thus, any ATT request fits in a single radio packet. If the layer above ATT wishes to
send more than 23 bytes of data, it needs to fragment the data into smaller packets and issue multiple ATT requests.
However, the ATT protocol allows devices to increase the ATT_MTU, only if both can support it. Increasing the ATT_MTU has
only one effect: the application does not have to fragment long data, however it can send more than 23 bytes in a single transaction.
The fragmentation is moved on to the L2CAP layer. Over the air though, there would still be more than one radio packet sent.
If the GATT Client supports a larger than default MTU, it should start an MTU exchange as soon as it connects to any Server.
During the MTU exchange, both devices would send their maximum MTU to the other, and the minimum of the two is chosen as
the new MTU.
For example, if the Client supports a maximum ATT_MTU of 250, and the Server supports maximum 120, after the exchange,
both devices set the new ATT_MTU value equal to 120.
To initiate the MTU exchange, call the following function from gatt_client_interface.h:
bleResult_t result = GattClient_ExchangeMtu(deviceId);
if (gBleSuccess_c != result)
{
/* Treat error */
}
The value of the maximum supported ATT_MTU of the local device does not have to be included in the request because it is
static. It is defined in the ble_constants.h file under the name gAttMaxMtu_c. Inside the GATT implementation, the ATT Exchange
MTU Request (and Response, for Servers) uses that value.
When the exchange is complete, the Client callback is triggered by the gGattProcExchangeMtu_c procedure type.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcExchangeMtu_c:
if (gGattProcSuccess_c == procedureResult)
{
/* To obtain the new MTU */
uint16_t newMtu;
bleResult_t result = Gatt_GetMtu(deviceId, &newMtu);
if (gBleSuccess_c == result)
{
/* Use the value of the new MTU */
(void) newMtu;
}
}
else
{
/* Handle error */
}
break;
/* ... */
}
}
if (gBleSuccess_c != result)
{
/* Treat error */
}
The operation triggers the Client Procedure Callback when complete. The application may read the number of discovered services
and each service’s handle range and UUID.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcDiscoverAllPrimaryServices_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Read number of discovered services */
PRINT( mcPrimaryServices );
/* Read each service's handle range and UUID */
for (int j = 0; j < mcPrimaryServices; j++)
{
PRINT( primaryServices[j]. startHandle );
PRINT( primaryServices[j]. endHandle );
PRINT( primaryServices[j]. uuidType );
PRINT( primaryServices[j]. uuid );
}
}
else
{
/* Handle error */
PRINT( error );
}
break;
/* ... */
}
}
/* ... */
case gGattProcDiscoverPrimaryServicesByUuid_c:
if (gGattProcSuccess_c == procedureResult)
{
if (1 == mcHrs)
{
/* HRS found, read the handle range */
PRINT( heartRateService. startHandle );
PRINT( heartRateService. endHandle );
}
else
{
/* HRS not found! */
}
}
else
{
/* Handle error */
PRINT( error );
}
break;
/* ... */
}
}
#define mxMaxIncludedServices_c 3
static gattService_t includedServices[mxMaxIncludedServices_c];
pPrimaryService,
mxMaxIncludedServices_c
);
if (gBleSuccess_c != result)
{
/* Treat error */
}
When the Client Procedure Callback is triggered, if any included services are found, the application can read their handle range
and their UUIDs.
void gattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
case gGattProcFindIncludedServices_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Read included services data */
PRINT( pPrimaryService-> cNumIncludedServices );
for (int j = 0; j < pPrimaryService-> cNumIncludedServices ; j++)
{
PRINT( pPrimaryService-> aIncludedServices [j]. startHandle );
PRINT( pPrimaryService-> aIncludedServices [j]. endHandle );
PRINT( pPrimaryService-> aIncludedServices [j]. uuidType );
PRINT( pPrimaryService-> aIncludedServices [j]. uuid );
}
}
else
{
/* Handle error */
PRINT( error );
}
break;
/* ... */
}
}
#define mcMaxCharacteristics_c 10
pService->aCharacteristics = hrsCharacteristics;
}
else
{
/* Handle error */
PRINT( error );
}
break;
/* ... */
}
}
Continuing the example from Discover primary services by UUID on page 28, let’s assume the Client wants to discover the Heart
Rate Control Point Characteristic inside the Heart Rate Service, as shown in the following code.
gattService_t * pService = &heartRateService;
PRINT(error);
}
break;
/* ... */
}
}
if (gBleSuccess_c != result)
{
/* Handle error */
}
The Client Procedure Callback is triggered at the end of the procedure.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcDiscoverAllCharacteristicDescriptors_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Read number of discovered descriptors */
PRINT(hrcpCharacteristic. cNumDescriptors );
/* Read descriptor data */
for ( uint8_t j = 0; j < hrcpCharacteristic. cNumDescriptors ; j++)
{
PRINT(hrcpCharacteristic. aDescriptors [j]. handle );
PRINT(hrcpCharacteristic. aDescriptors [j]. uuidType );
PRINT(hrcpCharacteristic. aDescriptors [j]. uuid );
}
}
else
{
/* Handle error */
PRINT(error);
}
break;
/* ... */
}
}
#define mcMaxValueLength_c 50
static uint8_t aValue[mcMaxValueLength_c];
if (gBleSuccess_c != result)
{
/* Handle error */
}
Regardless of the value length, the Client Procedure Callback is triggered when the reading is complete. The received value length
is also filled in the value structure.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcReadCharacteristicValue_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Read value length */
PRINT(myCharacteristic. value . valueLength );
/* Read data */
for ( uint16_t j = 0; j < myCharacteristic. value . valueLength ; j++)
{
PRINT(myCharacteristic. value . paValue [j]);
}
}
else
{
/* Handle error */
PRINT(error);
}
break;
/* ... */
}
}
bleUuid_t uuid = {
.uuid16 = gBleSig_GapDeviceName_d
};
if (gBleSuccess_c != result)
{
/* Handle error */
}
The Client Procedure Callback is triggered when the reading is complete. Because only one air packet is exchanged during this
procedure, it can only be used as a quick reading of Characteristic Values with length no greater than ATT_MTU – 1.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcReadUsingCharacteristicUuid_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Read characteristic value handle */
PRINT(aValue[0] | (aValue[1] << 8));
deviceNameLength -= 2;
/* ... */
}
}
This procedure also allows an optimization for a specific situation, which occurs when multiple Characteristics, whose values are
of known, fixed-length, can be all read in one single ATT transaction (usually one single over-the-air packet).
The application must know the value handle and value length of each Characteristic. It must also write the value.handle and
value.maxValueLength with the aforementioned values, respectively, and then link the value.paValue field with an allocated array
of size maxValueLength.
The following example involves reading three characteristics in a single packet.
#define mcNumCharacteristics_c 3
#define mcChar1Length_c 4
#define mcChar2Length_c 5
#define mcChar3Length_c 6
if (gBleSuccess_c != result)
{
/* Handle error */
}
When the Client Procedure Callback is triggered, if no error occurs, each Characteristic’s value length should be equal to the
requested lengths.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t p rocedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcReadMultipleCharacteristicValues_c:
if (gGattProcSuccess_c == procedureResult)
{
for ( uint8_t i = 0; i < mcNumCharacteristics_c; i++)
{
/* Read value length */
PRINT(myChars[i]. value . valueLength );
/* Read data */
for ( uint8_t j = 0; j < myChars[i]. value . valueLength ; j++)
{
PRINT(myChars[i]. value . paValue [j]);
}
}
}
else
{
/* Handle error */
PRINT(error);
}
break;
/* ... */
}
}
#define mcValueLength_c 3
uint8_t aValue[mcValueLength_c] = { 0x01, 0x02, 0x03 };
if (gBleSuccess_c != result)
{
/* Handle error */
}
The Client Procedure Callback is triggered when writing is complete.
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcWriteCharacteristicValue_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Continue */
}
else
{
/* Handle error */
PRINT(error);
}
break;
/* ... */
}
}
bleResult_tGattClient_WriteCharacteristicDescriptor
(
deviceId_t deviceId,
gattAttribute_t * pDescriptor,
uint16_t valueLength,
uint8_t * aValue
);
Only the pDescriptor->handle must be filled before calling the function.
One of the most frequently written descriptors is the Client Characteristic Configuration Descriptor (CCCD). It has a well-defined
UUID (gBleSig_CCCD_d) and a 2-byte long value that can be written to enable/disable notifications and/or indications.
In the following example, a Characteristic’s descriptors are discovered and its CCCD written to activate notifications.
static gattCharacteristic_t myChar;
myChar. value . handle = 0x00A0; /* Or maybe it was previously discovered? */
#define mcMaxDescriptors_c 5
static gattAttribute_t aDescriptors[mcMaxDescriptors_c];
myChar. aDescriptors = aDescriptors;
/* ... */
{
bleResult_t result = GattClient_DiscoverAllCharacteristicDescriptors
(
deviceId,
&myChar,
0xFFFF,
mcMaxDescriptors_c
);
if (gBleSuccess_c != result)
{
/* Handle error */
}
}
/* ... */
voidgattClientProcedureCallback
(
deviceId_t deviceId,
gattProcedureType_t procedureType,
gattProcedureResult_t procedureResult,
bleResult_t error
)
{
switch (procedureType)
{
/* ... */
casegGattProcDiscoverAllCharacteristicDescriptors_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Find CCCD */
for ( uint8_t j = 0; j < myChar. cNumDescriptors ; j++)
{
if (gBleUuidType16_c == myChar. aDescriptors [j]. uuidType
&& gBleSig_CCCD_d == myChar. aDescriptors [j]. uuid .
uuid16 )
{
uint8_t cccdValue[2];
packTwoByteValue(gCccdNotification_c, cccdValue);
bleResult_t result = GattClient_WriteCharacteristicDescriptor
(
deviceId,
&myChar. aDescriptors [j],
2,
cccdValue
);
if (gBleSuccess_c != result)
{
/* Handle error */
}
break;
}
}
}
else
{
/* Handle error */
PRINT(error);
}
break;
casegGattProcWriteCharacteristicDescriptor_c:
if (gGattProcSuccess_c == procedureResult)
{
/* Notification successfully activated */
}
else
{
/* Handle error */
PRINT(error);
}
/* ... */
}
}
gattServerCallback_t callback
);
The first member of the gattServerEvent_t structure is the eventType, an enumeration type with the following possible values:
• gEvtMtuChanged_c : Signals that the Client-initiated MTU Exchange Procedure has completed successfully and the
ATT_MTU has been increased. The event data contains the new value of the ATT_MTU. Is it possible that the application
flow depends on the value of the ATT_MTU, for example, there may be specific optimizations for different ATT_MTU
ranges. This event is not triggered if the ATT_MTU was not changed during the procedure.
• gEvtHandleValueConfirmation_c : A Confirmation was received from the Client after an Indication was sent by the Server.
• gEvtAttributeWritten_c, gEvtAttributeWrittenWithoutResponse_c : See Attribute write notifications on page 43.
• gEvtCharacteristicCccdWritten_c : The Client has written a CCCD. The application should save the CCCD value for
bonded devices with Gap_SaveCccd.
• gEvtError_c : An error occurred during a Server-initiated procedure.
NOTE
It is necessary to use these two functions with the Gap_SaveCccd only for bonded devices, because the data is
saved in NVM and reloaded at reconnection. For devices that do not bond, the application may also use its own
bookkeeping mechanism.
There is an important difference between sending Notifications and Indications: the latter can only be sent one at a time and the
application must wait for the Client Confirmation (signaled by the gEvtHandleValueConfirmation_c Server event, or by a
gEvtError_c event with gGattClientConfirmationTimeout_c error code) before sending a new Indication. Otherwise, a gEvtError_c
event with gGattIndicationAlreadyInProgress_c error code is triggered. The Notifications can be sent consecutively.
• gEvtAttributeWrittenWithoutResponse_c is triggered when the attribute is written with a Write Without Response procedure
(ATT Write Command). Because this procedure expects no response, the application may process it and, if necessary,
write it in the database. Regardless of whether the value is valid or not, no response is needed from the application.
• gEvtLongCharacteristicWritten_c is triggered when the Client has completed writing a Long Characteristic value; the event
data includes the handle of the Characteristic Value attribute and a pointer to its value in the database.
Chapter 6
GATT database application interface
For over-the-air packet exchanges between a Client and a Server, the GATT Server module automatically retrieves data from the
GATT Database and responds to all ATT Requests from the peer Client, provided it passes the security checks. This ensures that
the Server application does not have to perform any kind of searches over the database.
However, the application must have access to the database to write meaningful data into its Characteristics. For example, a
temperature sensor must periodically write the temperature, which is measured by an external thermometer, into the Temperature
Characteristic.
For these kinds of situations, a few APIs are provided in the gatt_db_app_interface.h file.
NOTE
All functions provided by this interface are executed synchronously. The result of the operation is saved in the return
value and it generates no event.
If the return value is gBleSuccess_c, the handle is written at pOutCharValueHandle. If the serviceHandle is invalid or not a valid
Service declaration, the gBleGattDbInvalidHandle_c is returned. Otherwise, the search is performed starting with the
serviceHandle+1. If no Characteristic of the given UUID is found, the function returns the gBleGattDbCharacteristicNotFound_c
value.
To find a Characteristic Descriptor of a given type in a Characteristic, when the Characteristic Value Handle is known, the following
API is provided:
bleResult_tGattDb_FindDescriptorHandleForCharValueHandle
(
uint16_t charValueHandle,
bleUuidType_t descriptorUuidType,
bleUuid_t * pDescriptorUuid,
uint16_t * pOutDescriptorHandle
);
Similarly, the function returns gBleGattDbInvalidHandle_c is the handle is invalid. Otherwise, it starts searching from the
charValueHandle+1. Then, gBleGattDbDescriptorNotFound_c is returned if no Descriptor of the specified type is found.
Otherwise, its attribute handle is written at the pOutDescriptorHandle and the function returns gBleSuccess_c.
One of the most commonly used Characteristic Descriptor is the Client Configuration Characteristic Descriptor (CCCD), which
has the UUID equal to gBleSig_CCCD_d. For this specific type, a special API is used as a shortcut:
bleResult_tGattDb_FindCccdHandleForCharValueHandle
(
uint16_t charValueHandle,
uint16_t * pOutCccdHandle
);
Chapter 7
Creating GATT database
The GATT Database contains several GATT Services where each Service must contain at least one GATT Characteristic.
The Attribute Database contains a collection of attributes. Each attribute has four fields:
• The attribute handle – a 2-byte database index, which starts from 0x0001 and increases with each new attribute, not
necessarily consecutive; maximum value is 0xFFFF.
• The attribute type or UUID – a 2-byte, 4-byte, or 16-byte UUID.
• The attribute permissions – 1 byte containing access flags; this defines whether the attribute’s value can be read or written
and the security requirements for each operation type
• The attribute value – an array of maximum 512 bytes.
The ATT does not interpret the UUIDs and values contained in the database. It only deals with data transfer based on the attributes’
handles.
The GATT gives meaning to the attributes based on their UUIDs and groups them into Characteristics and Services.
There are two possible ways of defining the GATT Database: at compile-time (statically) or at run-time (dynamically).
NOTE
On some occasions, it is desired to reuse an 128-bit UUID declared in gatt_uuid128.h. The 16 byte array is available
through its friendly name and be accessed by including gatt_db_handles.h in the application. It is strongly advised
to use it only in read-only operations. For example:
(gatt_uuid128.h)
UID128(uuid_service_wireless_uart, 0xE0, 0x1C, 0x4B, 0x5E, 0x1E, 0xEB, 0xA1, 0x5C, 0xEE,
0xF4, 0x5E, 0xBA, 0x00, 0x01, 0xFF, 0x01)
(app.c)
#include "gatt_db_handles.h"
........
/* Start Service Discovery*/
BleServDisc_FindService(peerDeviceId, gBleUuidType128_c, (bleUuid_t*)
&uuid_service_wireless_uart);
• INCLUDE (service_name)
— The service_name parameter is the friendly name used to declare the Secondary Service.
— This macro is used only for Secondary Services with a SIG-defined, 2-byte, Service UUID.
• INCLUDE_CUSTOM (service_name)
— This macro is used for Secondary Services that have either a 4-byte UUID or a 16-byte UUID.
The effect of the service inclusion is that the including Service is considered to contain all the Characteristics of the included
Service.
• CCCD (name)
This simple macro is basically equivalent to the following Descriptor declaration:
DESCRIPTOR (name,
0x2902,
(gGattAttPermAccessReadable_c
| gGattAttPermAccessWritable_c),
2, 0x00, 0x00)
NOTE
All “user-friendly” names given in declarations are statically defined as enum members, numerically equal to the
attribute handle of the declaration. This means that one of those names can be used in code wherever an attribute
handle is required as a parameter of a function if gatt_db_handles.h is included in the application source file. For
example, to write the value of the Scan Refresh Characteristic from the application-level code, use these
instructions:
#include "gatt_db_handles.h"
...
uint8_t scan_refresh_value = 0x12;
GattDb_WriteAttribute( char_scan_refresh, &scan_refresh_value, 1);
• GattDbDynamic_AddSecondaryServiceDeclaration
— The Service UUID is specified as parameter.
Memory requirements: one entry in the attribute buffer and UUID size in value buffer.
• GattDbDynamic_AddIncludeDeclaration
— The Service UUID and handle range are specified as parameters.
Memory requirements: one entry in the attribute buffer and 6 bytes in value buffer.
The functions have an optional out parameter pOutHandle. If its value is not NULL, the execution writes a 16-bit value in the
pointed location representing the attribute handle of the added declaration.
This handle can be used by the application as parameter in some GattDbApp APIs or in the Service removal functions.
At least one Service needs to be added before any Characteristic.
Chapter 8
Creating a Custom Profile
This chapter describes how the user can create customizable functionality over the BLE host stack by defining profiles and
services. The Temperature Profile, used by the Temeprature Sensor and Collector applications (found in the BLE SDK) is used
as a reference to explain the steps of building custom functionality.
if (result != gBleSuccess_c)
return result;
if (result != gBleSuccess_c)
return result;
Hts_SendTemperatureMeasurementNotification(handle);
return gBleSuccess_c;
}
To accommodate some use cases where the service is reset, the stop function is called. The reset also implies a service
unsubscribe. Below is an example for the Temperature Service:
bleResult_t Tms_Stop ( tmsConfig_t *pServiceConfig)
{
return Tms_Unsubscribe();
}
gattDbCharPresFormat_t tempFormat ;
} tmcConfig_t ;
Chapter 9
Application Structure
This chapter describes the organization of the Bluetooth Low Energy demo applications that can be found in the SDK. By
familiarizing with the application structure, the user is able to quickly adapt its design to an existing demo or create a new
application.
The Temperature Sensor application is used as a reference to showcase the architecture.
The app folder follows a specific structure which is recommended for any application developed using the BLE Host Stack:
• the common group contains the application framework shared by all profiles and demo applications:
— Application Main Framework
— BLE Connection Manager
— BLE Stack and Task Initialization and Configuration
— GATT Database
9.3.4 Privacy
To enable or disable Privacy, the following APIs may be used:
bleResult_t BleConnManager_EnablePrivacy
(
void
);
bleResult_t BleConnManager_DisablePrivacy
(
void
);
The function BleConnManager_EnablePrivacy will call BleConnManager_EnablePrivacyInternal after checking if the privacy is
enabled.
static bleResult_t BleConnManager_EnablePrivacyInternal
(
bool_t bCheckNewBond
);
If the privacy feature is supported (gAppUsePrivacy_d = 1), the Connection Manager will activate Controller Privacy or Host Privacy
depending on the board capabilities.
The bCheckNewBond is a boolean that tells the Manager whether it should check or not if a bond between the devices already
exists.
At the application layer, for privacy usage, the user must be aware that when connecting with more than one device, the privacy
must be turned off before the second and subsequent connections are established and enabled right after the establishment. This
does not apply to the first connection.
Below is an example that enables and disables the Privacy by requests (reqOff_c == mAppPrivacyChangeReq and reqOn_c ==
mAppPrivacyChangeReq) or until the timer expires (mPrivacyDisableDurationSec_c seconds).
if( reqOff_c == mAppPrivacyChangeReq )
{
if( gBleSuccess_c == BleConnManager_DisablePrivacy() )
{
TMR_StartLowPowerTimer(mPrivacyDisableTimerId, gTmrLowPowerSingleShotMillisTimer_c,
TmrSeconds(mPrivacyDisableDurationSec_c), PrivacyEnableTimerCallback, NULL);
}
mAppPrivacyChangeReq = reqDisabled_c;
}
else if( reqOn_c == mAppPrivacyChangeReq )
{
BleConnManager_EnablePrivacy();
mAppPrivacyChangeReq = reqDisabled_c;
}
NOTE
Do not modify any of the file contained in the gatt_db folder and its subfolder.
To complete the GATT Database initialization, this demo application includes the required gatt_db.h and gatt_uuid128.h files in
its specific application folder, along with other profile-specific configuration and code files.
#if (gUseHciTransportDownward_d == 1)
#elif (gUseHciTransportUpward_d == 1)
#else
/* BLE Host Stack Init */
return Ble_HostInitialize(gapGenericCallback,
( hciHostToControllerInterface_t ) Controller_RecvPacket);
#endif
}
NOTE
This function should be used by your application because it correctly performs all the necessary BLE initialization.
— If the compiler switch is not activated (equal to 0), which is the default setting for the demos, the Controller library is
available and the Controller task is initialized by the Controller_TaskInit. Then, the two stacks with Controller_Init and
Ble_HostInitialize are initialized linking the Controller’s HCI interface with the Host’s.
The Temperature Profile implements the custom Temperature service, the Battery, and Device Information services.
/* Start services */
tmsServiceConfig. initialTemperature = 100 * BOARD_GetTemperature();
Tms_Start(&tmsServiceConfig);
#if (cPWR_UsePowerDownMode)
PWR_ChangeDeepSleepMode(3);
PWR_AllowDeviceToSleep();
#endif
}
To start the application functionality, BleApp_Start is called. This function usually contains code to start advertising for sensor
nodes or scanning for central devices. In the example of the Temperature Sensor, the function is the following:
void BleApp_Start(void)
{
Led1On();
if (mPeerDeviceId == gInvalidDeviceId_c)
{
/* Device is not connected and not advertising*/
if (!mAdvState. advOn )
{
BleApp_Advertise();
}
}
else
{
BleApp_SendTemperature();
}
}
• app_config.c. This file contains data structures that are used to configure the stack.
This includes advertising data, scanning data, connection parameters, advertising parameters, SMP keys, security requirements,
and so on.
• app_preinclude.h.
This header file contains macros to override the default configuration of any module in the application. It is added as a preinclude
file in the preprocessor command line in IAR:
• gatt_db.h and gatt_uuid128.h. The two header files contain the definition of the GATT database and the custom UUIDs
used by the application. See Creating GATT database on page 47 for more information.
Chapter 10
Low-Power Management
#if gFSCI_IncludeLpmCommands_c
/* Send Wake Up indication to FSCI */
FSCI_SendWakeUpIndication();
#endif
1 LLS3 DSM x x x
2 LLS2 IDLE x x x
3 LLS3 IDLE x x x
4 VLLS0/1* IDLE x x
5 VLLS2 IDLE x x
6 STOP IDLE/RUN x x x x x
When a connection is created, the application is notified in BLE_SignalFromISRCallback to deny sleep and be ready for other
procedures like service discovery.
PWR_DisallowDeviceToSleep();
These functions are called on the gConnEvtConnected_c event. It needs to allow the system to sleep on account of the callback
executed previously that denied device to sleep.
casegConnEvtConnected_c:
{
PWR_ChangeDeepSleepMode(1);
PWR_AllowDeviceToSleep();
}
Chapter 11
Over the Air Programming (OTAP)
This chapter contains a detailed description of the Over The Air Programming capabilities of the BLE Host Stack enabled by
dedicated GATT Service/Profile, the support modules needed for OTA programming and the Bootloader application which
performs the actual image upgrade on a device.
The image transfer is done using a dedicated protocol which is designed to run on both the BLE transport and serial transport.
The container for the upgrade image is an image file which has a predefined format which is described in detail. The image file
format is independent of the protocol but must contain information specific to the image upgrade infrastructure on an OTAP Client
device. Detailed information on how to build an image file starting from a generic format executable generated by an embedded
cross-compiling toolchain is shown.
The demo applications implement a typical scenario where a new image is sent from a PC via serial interface to a BLE OTAP
Server and then over the air to an OTAP Client which is the target of the upgrade image. There are 3 applications involved in the
OTAP demo: 1 PC application which builds the image file and serves it to the embedded OTAP Server and 2 embedded
applications (OTAP Server and OTAP Client). This chapter contains enough information for building BLE OTAP applications which
implement different image upgrade scenarios specific to other use cases.
The OTAP Service custom 128-bit UUID is built using the base UUID by replacing the most significant 4 bytes which are 0 with
a value specific to the OTAP Service which is 01FF5550 in hexadecimal format.
The BLE OTAP Service Characteristics UUIDs are built the same as the BLE OTAP Service UUID starting from the base 128-bit
UUID but using other values for the most significant 4 bytes.
type protocol. Effectively the OTAP Client downloads the upgrade image from the PC and not from the OTAP Server. Other transfer
methods may be used based on application needs.
Commands are sent over the transport medium starting with the Command ID and continuing with the Command Payload.
All multibyte command parameters in the Command Payload are sent in a least significant octet first order (little endian).
A summary of the commands supported by the BLE OTAP Protocol is shown in the table below. Each of the commands is then
detailed in its own section.
The ImageId parameter should not be 0x0000 which is the reserved value for the current running image or 0xFFFF which is the
reserved value for “no image available”.
The CurrImageId parameter should be set to 0x0000 to signify the current running image.
The CurrImageVer parameter should contain sufficient information about the target device for the OTAP Server to determine if it
has an upgrade image available for the requesting OTAP Client.
A value of all zeroes for the CurrImageVer means that an OTAP Client is requesting information about all available images on an
OTAP Server and the OTAP Server should send a New Image Info Response for each image.
The ImageId parameter with a value of 0xFFFF is reserved for the situation where no upgrade image is available for the requesting
device.
ChunkSize 2 Should be
optimized to the
TransferChanne
l type. The
maximum
number of
chunks per
block is 256.
Value is in
bytes.
The optimal value of the ChunkSize parameter depends on the chosen transfer method and the Link Layer payload size. Ideally
it must be chosen in such a way that full packets are sent for every chunk in the block.
The default Link Layer payload is 27 bytes form which we subtract 4 for the L2CAP layer and 3 for the ATT layer (1 for the ATT
Cmd Opcode and 2 for the Handle) leaving us with a 20 byte OTAP protocol payload. From these 20 bytes we subtract 1 for the
OTAP CmdId and 1 for the chunk sequence number leaving us with an optimum chink size of 18 for the ATT transfer method –
which is the default in the demo applications. For the L2CAP PSM transfer method the chosen default chunk size is 111. This was
chosen so as a chunk fits exactly 5 link layer packets. The default L2CAP payload of 23 (27 - 4) multiplied by 5 gives us 115 from
which we subtract 2 bytes for the SDU Length (which is only sent in the first packet), 1 byte for the OTAP CmdId and 1 byte for
the chunk sequence number which leaves exactly 111 bytes for the actual payload.
If the Link layer supports Long Frames feature then the chunk size should be set according to the negotiated ATT MTU for the
ATT transfer method. From the negotiated ATT MTU (att_mtu) substract 3 bytes for the ATT layer (1 for the ATT Cmd Opcode and
2 for the Handle) then substract 2 bytes for the OTAP protocol (1 for the CmdId and 1 for the chunk sequence number) to determine
the optimum chunk size (optimum_att_chunk_size = att_mtu – 3 – 2). For the L2CAP PSM transfer method the chunk size can
be set based on the maximum L2CAP SDU size (max_l2cap_sdu_size) from which 4 bytes should be subtracted, 2 for the SDU
Length and 2 for the OTAP protocol (optimum_l2cap_chunk_size = max_l2cap_sdu_size – 3 – 2). In some particular cases
reducing the L2CAP chunk size could lead to better performance. If the L2CAP chunk size needs to be reduced it should be
reduced so it fits exactly a number of link layer packets. An example of how to compute an optimal reduced L2CAP chunk size is
given in the previous paragraph.
The SeqNumber parameter is the chunk sequence number and it has incremental values from 0 to 255 (0x00 to 0x FF) for a
maximum of 256 chunks per block.
The Data parameter is an array containing the actual image part being transferred starting from the BlockStartPosition +
SeqNumber * ChunkSize position in the image file and containing ChunkSize or less bytes depending on the position in the block.
Only the last chunk in a block can have less than ChunkSize bytes in the Image Chunk Command data payload.
The ImageId parameter contains the ID of the image file that was transferred.
The Status parameter is 0x00 (Success) if image integrity and possibly other checks have been successfully made after the image
is transferred and another value if integrity or other kind of errors have occurred.
If the status is 0x00 the OTAP Client can trigger the Bootloader to start flashing the new image. The image flashing should take
about 15 seconds for a 160 KB flash memory.
The CmdId parameter contains the ID of the command which caused the error (if applicable).
The ErrorStatus parameter contains the source of the error. All error statuses are defined in the otapStatus_t enumerated type
in the otap_interface.h file.
Figure 13. Typical BLE OTAP Image Transfer Scenario Message Sequence Chart
Image File CRC Sub-element 2 This is a 16 bit CCITT type CRC which
is calculated over all elements of the
image file with the exception of the
Image File CRC sub-element itself. This
must be the last sub-element in an
image file.
Each sub-element in a BLE OTAP Image File has a Type-Length-Value (TLV) format. The type identifier provides forward and
backward compatibility as new sub-elements are introduced. Existing devices that do not understand newer sub-elements may
ignore the data.
The following table shows the general format of a BLE Image File sub-element.
Some sub-element type identifiers are reserved while others are left for manufacturer-specific use. The table below shows the
reserved type identifiers and the manufacturer-specific ranges.
The OTAP Demo applications use two of the manufacturer-specific sub-element type identifiers while the rest remain free to use.
The two are shown in the table below along with a short description.
Table 20. Manufacturer-Specific Sub-element Type Identifiers Used by OTAP Demo Applications
0xf100 Image File CRC 16 bit CRC which is computed over the
image file with the exception of the CRC
sub-element itself.
(including header)
The fields are shown in the order they are placed in memory from the first location to the last.
The total size of the header without the optional fields (if defined by the Header Field Control) is 58 bytes.
All the fields in the header have a little endian format with the exception of the Header String field which is an ASCII character
string.
A packed structure type definition for the contents of the BLE OTAP Header can be found in the otap_interface.h file.
11.4.1.6 Image ID
This is a unique short identifier for the image file. It is used to request parts of an image file. This number should be unique for
all images available on a BLE OTAP Server.
The value 0x0000 is reserved for the current running image.
The value 0xFFFF is reserved as a “no image available” code for New Image Info Response commands.
This field value must be used in the ImageID field in the New Image Notification and New Image Info Response commands.
This field value must be used in the ImageVersion field in the New Image Notification and New Image Info Response commands.
The format of the SREC file is very simple. It contains lines of text called records which have a specific format. An example of the
contents of a SREC file is shown below.
S02000006F7461705F636C69656E745F6174745F4672656552544F532E73726563A1
S1130000F83F0020EB0500007506000075060000AF
S113001075060000750600007506000075060000F0
S113002075060000750600007506000075060000E0
S113003075060000750600007506000075060000D0
S113004000000000000000000000000000000000AC
S1130050000000000000000000000000000000009C
.............
S2140117900121380004F05FF8002866D12A003100E4
S2140117A06846008804F022F8A689002E16D0002884
S2140117B014D12569278801A868A11022F7F782FCB1
S2140117C06B4601AA0121380004F045F800284CD1E7
S2140117D02A0031006846008804F008F8A68A002E20
All records start with the ASCII letter ‘S’ followed by an ASCII digit from ‘0’ to ‘9’. These two characters from the record type which
identifies the format of the data field of the record.
The next 2 ASCII characters are 2 hex digits which indicate the number of bytes (hex digit pairs) which follow the rest of the record
(address, data and checksum).
The address follows next which can have 4, 6 or 8 ASCII hex digits depending on the record type.
The data field is placed after the address and it contains 2 * n ASCII hex digits for n bytes of actual data.
The last element of the S record is the checksum which comprises of 2 ASCII hex digits. The checksum is computed by adding
all the bytes of the byte count, address and data fields then computing the ones complement of the least significant octet of the
sum.
More details about the SREC file format can be found at this location: en.wikipedia.org/wiki/SREC_(file_format)
We are only interested in records which contain actual data. These are S1, S2 and S3 records. The other types of records can
be ignored.
The S1, S2 and S3 records are used to build the Upgrade Image Sub-element of the image file simply by placing the record data
at the location specified by the record address in the Value field of the Sub-element. It is recommended to fill all gaps in S record
addresses with 0xFF.
To build an OTAP Image File from a SREC file follow the procedure:
• Generate the SREC file by correctly configuring your toolchain to do so
• Create the image file header
— Set the Image ID field of the header to be unique on the OTAP Server.
— Leave the Total Image File Size Field blank for the moment.
• Create the Upgrade Image Sub-element
— Read the S1, S2 and S3 records from the SREC file and place the binary record data to the record addresses in the
Value filed of the sub-element. Fill all address gaps in the S records with 0xFF.
— Fill in the Length field of the sub-element with the length of the written Value filed.
• Create the Sector Bitmap Sub-element
— A default working setting would be all byes 0xFF for the Value field of this sub-element
The format of the BIN file is very simple. It contains the executable image in binary format as is, starting from address 0 and up
to the highest address. This type of file does not have any explicit address information.
To build an OTAP Image File from a BIN file follow the procedure:
• Generate the BIN file by correctly configuring your toolchain to do so
• Create the image file header
— Set the Image ID field of the header to be unique on the OTAP Server.
— Leave the Total Image File Size Field blank for the moment.
• Create the Upgrade Image Sub-element
— Compy the entire contents of the BIN file as is into the Value filed of the sub-element.
— Fill in the Length field of the sub-element with the length of the written Value filed.
• Create the Sector Bitmap Sub-element
— A default working setting would be all byes 0xFF for the Value field of this sub-element
• Create the Image File CRC Sub-element
— Compute the total image file size as the length of the header + the length of all 3 sub-elements and fill in the
appropriate filed in the header with this value
— Compute and write the Value field of this sub-element using the header and all sub-elements except this one
— The OTA_CrcCompute() function in the OtaSupport.c file can be used to incrementally compute the CRC
If the Image ID is not available when the image file is created then the CRC cannot be computed. It can be computed later after
the Image ID is established and written in the appropriate field in the header.
The OTAP Server waits in an idle state until a valid Image Block Request command is received and then moves to a pseudo-state
and starts sending the requested block. The transfer can be interrupted by some commands (Error Notification, Stop Image
Transfer, and so on) or other events (disconnection, user interruption, and so on).
The otap_interface.h file contains infrastructure for sending and receiving OTAP Commands and parsing OTAP image files.
Packed structure types are defined for all OTAP commands and type enumerations are defined for command parameter values
and some configuration values like the data payloads for the different transfer methods.
To receive ATT Indications and ATT Write Confirmations from the OTAP Client the OTAP Server application registers a set of
callbacks in the stack. This is done in the BleApp_Config() function.
App_RegisterGattClientProcedureCallback (BleApp_GattClientCallback);
App_RegisterGattClientIndicationCallback (BleApp_GattIndicationCallback);
This BleApp_GattIndicationCallback() function is called when any attribute is indicated so the handle of the indicated attribute
must be checked against a list of expected handles. In our case we are looking for the OTAP Control Point handle which was
obtained during the discovery procedure.
The BleApp_GattIndicationCallback() function from the demo calls an application-specific function called
BleApp_AttributeIndicated() in which the OTAP Commands are handled.
static void BleApp_AttributeIndicated
(
deviceId_t deviceId,
uint16_t handle,
uint8_t* pValue,
uint16_t length
)
{
if (handle == mPeerInformation.customInfo.otapServerConfig.hControlPoint)
{
/* Handle OTAP Commands here */
otapCommand_t* pOtaCmd = (otapCommand_t*)pValue;
App_HandleOtapCmd (pOtaCmd->cmdId,
(uint8_t*)(&(pOtaCmd->cmd)),
length);
}
elseif (handle == otherHandle)
{
/* Handle other attribute indications here */
/* ... Missing code here ... */
}
else
{
/*! A GATT Client is trying to GATT Indicate an unknown attribute value.
* This should not happen. Disconnect the link. */
Gap_Disconnect (deviceId);
}
}
The App_HandleOtapCmd() function is the one which deals with the received command, sending responses if necessary or
starting an image block transfer.
To send OTAP Commands to the OTAP Client the application running the OTAP Server calls the
OtapServer_SendCommandToOtapClient() function which performs an ATT Write operation on the OTAP Control Point attribute.
static void OtapServer_SendCommandToOtapClient (deviceId_t otapClientDevId,
void* pCommand,
uint16_t
cmdLength)
{
/* GATT Characteristic to be written - OTAP Client Control Point */
gattCharacteristic_t otapCtrlPointChar;
bleResult_t bleResult;
/* Only the value handle element of this structure is relevant for this operation. */
otapCtrlPointChar.value.handle =
mPeerInformation.customInfo.otapServerConfig.hControlPoint;
if (gBleSuccess_c == bleResult)
{
otapServerData.lastCmdSentToOtapClient =
(otapCmdIdt_t)(((otapCommand_t*)pCommand)->cmdId);
}
else
{
/*! A BLE error has occured - Disconnect */
Gap_Disconnect (otapClientDevId);
}
}
The ATT Confirmation for the ATT Write is received in the BleApp_GattClientCallback() set up earlier which receives a GATT
procedure success message for a gGattProcWriteCharacteristicValue_c procedure type.
static void BleApp_GattClientCallback (deviceId_t serverDeviceId,
gattProcedureType_t
procedureType,
gattProcedureResult_t
procedureResult,
bleResult_t error)
{
if (procedureResult == gGattProcError_c)
{
attErrorCode_t attError = (attErrorCode_t) (error & 0xFF);
if (attError == gAttErrCodeInsufficientEncryption_c ||
attError == gAttErrCodeInsufficientAuthorization_c ||
attError == gAttErrCodeInsufficientAuthentication_c)
{
/* Start Pairing Procedure */
Gap_Pair(serverDeviceId, &gPairingParams);
}
BleApp_StateMachineHandler(serverDeviceId, mAppEvt_GattProcError_c);
}
{
switch(procedureType)
{
case gGattProcWriteCharacteristicValue_c:
BleApp_HandleValueWriteConfirmations (serverDeviceId);
break;
default:
break;
}
BleApp_StateMachineHandler(serverDeviceId, mAppEvt_GattProcComplete_c);
}
}
The BleApp_HandleValueWriteConfirmations() function deals with ATT Write Confirmations based on the requirements of the
application.
There are 2 possible transfer methods for Image Chunks, the ATT transfer method and the L2CAP transfer method. The OTAP
server is prepared to handle both, as requested by the OTAP Client.
To be able to use the L2CAP transfer method the OTAP Server application must register a L2CAP LE PSM and 2 callbacks: a
data callback and a control callback. This is done in the BleApp_Config() function.
/* Register OTAP L2CAP PSM */
L2ca_RegisterLePsm (gOtap_L2capLePsm_c,
gOtapCmdImageChunkCocLength_c); /*!< The negotiated MTU must be
higher than the biggest data chunk that will be sent fragmented */
...
App_RegisterLeCbCallbacks(BleApp_L2capPsmDataCallback, BleApp_L2capPsmControlCallback);
The data callback BleApp_L2capPsmDataCallback() is not used by the OTAP Server.
The control callback is used to handle L2CAP LE PSM connection requests from the OTAP Client and other events: PSM
disconnections, No peer credits, and so on. The OTAP Client must initiate the L2CAP PSM connection if it wants to use the L2CAP
transfer method.
static void BleApp_L2capPsmControlCallback(l2capControlMessageType_t messageType,
void*
pMessage)
{
switch (messageType)
{
case gL2ca_LePsmConnectRequest_c:
{
l2caLeCbConnectionRequest_t *pConnReq = ( l2caLeCbConnectionRequest_t *)pMessage;
if (pConnComplete->result == gSuccessful_c)
{
/* Set the application L2CAP PSM Connection flag to TRUE beacuse there is no
gL2ca_LePsmConnectionComplete_c
* event on the responder of the PSM connection. */
otapServerData. l2capPsmConnected = TRUE;
otapServerData. l2capPsmChannelId = pConnComplete->cId;
}
break;
}
case gL2ca_LePsmDisconnectNotification_c:
{
l2caLeCbDisconnection_t *pCbDisconnect = ( l2caLeCbDisconnection_t *)pMessage;
break;
}
default:
break;
}
}
The ATT transfer method is supported by default but the L2CAP transfer method only works if the OTAP Client opens an L2CAP
PSM credit oriented channel.
To send data chunks to the OTAP Client the OTAP Server application calls the OtapServer_SendCImgChunkToOtapClient()
function which delivers the chunk via the selected transfer method. For the ATT transfer method the chunk is sent via the
GattClient_CharacteristicWriteWithoutResponse() function and for the L2CAP transfer method the chunk is sent via the
L2ca_SendLeCbData() function.
static void OtapServer_SendCImgChunkToOtapClient (deviceId_t otapClientDevId,
void* pChunk,
uint16_t chunkCmdLength)
{
bleResult_t bleResult = gBleSuccess_c;
if (otapServerData.transferMethod == gOtapTransferMethodAtt_c)
{
/* GATT Characteristic to be written without response - OTAP Client Data */
gattCharacteristic_t otapDataChar;
/* Only the value handle element of this structure is relevant for this operation. */
otapDataChar.value.handle = mPeerInformation.customInfo.otapServerConfig.hData;
bleResult = GattClient_CharacteristicWriteWithoutResponse
(mPeerInformation.deviceId,
&otapDataChar,
chunkCmdLength,
pChunk);
}
else if (otapServerData.transferMethod == gOtapTransferMethodL2capCoC_c)
{
bleResult = L2ca_SendLeCbData (mPeerInformation.deviceId,
otapServerData.l2capPsmChannelId,
pChunk,
chunkCmdLength);
}
if (gBleSuccess_c != bleResult)
{
/*! A BLE error has occured - Disconnect */
Gap_Disconnect (otapClientDevId);
}
}
The OTAP Server demo application relays all commands received from the OTAP Client to a PC through the FSCI type protocol
running over a serial interface. It also directly relays all responses from the PC back to the OTAP Client.
Other implementations can bring the image to an external memory through other means of communication and directly respond
to the OTAP Client requests.
To use internal storage set up the gUseInternalStorageLink_d=1 symbol in the linker configuration window (Linker->Config tab in
the IAR project properties) and set the gEepromType_d value to gEepromDevice_InternalFlash_c in the app_preinclude.h file:
/* Specifies the type of EEPROM available on the target board */
#define gEepromType_d gEepromDevice_InternalFlash_c
The OTAP demo applications for the IAR EW IDE have some settings in the Linker options tab which must be configured to use
OtaSupport and the OTAP Bootloader.
In the Project Target Options->Linker->Config tab, 3 symbols must be correctly defined. To use NVM storage the gUseNVMLink_d
symbol must be set to 1. The gUseInternalStorageLink_d symbol must be set to 0 when OTAP external storage is used and to 1
when internal storage is used. To enable the OTAP Bootloader linking the gUseBootloaderLink_d symbol must be set to 1 to
offset the application. An example linker configuration window for IAR is shown below.
Figure 17. Linker Config IAR EW IDE - OTAP Client External Storage and Bootloader Configuration
For MCUXpresso IDE the linker settings required for OTAP applications can be set up in the Project Properties->C/C++ Build-
>Settings->Tool Settings->MCU Linker->Miscellaneous tab. In this location symbols can be set up to be passed via command line
to the linker as shown in the screen-shot below.
The same linker settings for OTAP applications can be configured for Keil MDK in the Options for Target->Linker->Misc controls
text box. Edit the text box to add more linker symbols as shown in the figure.
Once the application starts and bidirectional OTAP communication is established via the OTAP Service then the OTAP Client
must determine if the connected OTAP Server has a newer image than the one currently present on the device. This can be done
in two ways. Either the OTAP Server knows by some application-specific means that it has a newer image and sends a New
Image Notification to the OTAP Client or the OTAP Client sends a New Image Info Request to the OTAP Server and waits for a
response. The example application uses the second method. The New Image Info Request contains enough information about
the currently running image to allow the OTAP Server to determine if it has a newer image for the requesting device. The New
Image Info Response contains enough information for the OTAP Client to determine that de “advertised” image is newer and it
wants to download it. The best method is entirely dependent on application requirements.
An example function which checks if an ImageVerison field from a New Image Notification or a New Image Info Response
corresponds to a newer image (based on the suggested format of this field) is provided in the OTAP Client demo applications.
The function is called OtapClient_IsRemoteImageNewer().
The OTAP Client application is a little more complicated than the OTAP Server application because more state information needs
to be handled (current image position, current chink sequence number, image file parsing information, and so on). An example
state diagram for the OTAP Client is shown below. Note that some of the states may not be explicitly present in the demo
applications, this diagram is meant to emphasize the steps of the image download process.
After the OTAP Client determines that the peer OTAP Server has a suitable upgrade image available it can start the download
process. This is done by sending multiple Image Block Request messages and waiting for the Image Chunks via the selected
transfer method.
While receiving the image file blocks the OTAP Client application parses the image file and if any parameter of an image file sub-
element is invalid or the image file format is invalid it sends an Error Notification to the OTAP Server and tries to restart the
download process from the beginning or a known good position.
When an Image Chunk received its sequence number is checked and its content is parsed in the context of the image file format.
If the sequence number is not as expected then the block transfer is restarted from the last known good position. When all chunks
of an Image Block are received ne next block is requested if there are more blocks to download. When the last Image Block in an
Image File is received then the image integrity is checked (the received CRC from the Image File CRC sub-element is compared
to the computed CRC). The computed image integrity initialization and intermediary value must be reset to 0 before starting the
download of an image and when restarting the download of an image. If the image integrity check fails then the image download
process is restarted from the beginning. If the image integrity check is successful then the Bootloader is triggered, an Image
Download Complete message is sent to the OTAP Server and the MCU is restarted. After the restart the bootloader kicks in and
writes the new image to the flash memory and gives CPU control to the newly installed application.
If at any time during the download process a Link Layer disconnection occurs then the image download process is restarted from
the last known good position when the link is reestablished.
As noted earlier the OTAP Client application needs to handle a lot of state information. In the demo application all this information
is held in the otapClientData structure of the otapClientAppData_t type. The type is defined and the structure is initialized in the
app.c file of the application. This structure is defined and initialized differently for the OTAP Client ATT and L2CAP example
applications. Mainly the transferMethod member of the structure is constant and has different values for the two example
applications and the L2CAP application structure has an extra member.
To receive write notifications when the OTAP Server writes the OTAP Control Point attribute and ATT Confirmations when it
indicates the OTAP Control Point attribute, the OTAP Client application must register a GATT Server callback and enable write
notifications for the OTAP Control Point attribute. This is done in the BleApp_Config() function in the app.c file.
static uint16_t otapWriteNotifHandles[] = {value_otap_control_point,
value_otap_data};
...
static void BleApp_Config()
{
...
/* Register for callbacks*/
App_RegisterGattServerCallback (BleApp_GattServerCallback);
GattServer_RegisterHandlesForWriteNotifications (sizeof(otapWriteNotifHandles)/
sizeof(otapWriteNotifHandles[0]),
otapWriteNotifHandles);
..
}
The BleApp_GattServerCallback() function handles all incoming communication from the OTAP Server.
static void BleApp_GattServerCallback (deviceId_t deviceId,
gattServerEvent_t*
pServerEvent)
{
switch (pServerEvent->eventType)
{
case gEvtCharacteristicCccdWritten_c:
BleApp_CccdWritten (...) ;
break;
case gEvtAttributeWritten_c:
BleApp_AttributeWritten (...);
break;
case gEvtAttributeWrittenWithoutResponse_c:
BleApp_AttributeWrittenWithoutResponse (...);
break;
case gEvtHandleValueConfirmation_c:
BleApp_HandleValueConfirmation (...);
break;
default:
break;
}
}
When the OTAP Server Writes a CCCD the BleApp_GattServerCallback() function calls the BleApp_CccdWritten() function which
sends a New Image Info Request when the OTAP Control Point CCCD is written it – this is the starting point of OATP transactions
in the demo applications.
When an ATT Write Request is made by the OTAP Server the the BleApp_GattServerCallback() function calls the
BleApp_AttributeWritten() function which handles the data as an OTAP command. Only writes to the OTAP Control Point are
handled as OTAP commands. For each command received from the OTAP Server there is a separate handler function which
performs required OTAP operations. These are:
• OtapClient_HandleNewImageNotification()
• OtapClient_HandleNewImageInfoResponse()
• OtapClient_HandleErrorNotification()
When an ATT Write Command (GATT Write Without Response) is sent by the OTAP Server the BleApp_GattServerCallback()
function calls the BleApp_AttributeWrittenWithoutResponse() function which handles Data Chunks if the selected transfer method
is ATT and returns an error if any problems are encountered. Data chunks are handled by the OtapClient_HandleDataChunk()
function.
static void BleApp_AttributeWrittenWithoutResponse (deviceId_t deviceId,
uint16_t handle,
uint16_t length,
uint8_t* pValue)
{
/* ... Missing code here ... */
if (handle == value_otap_data)
{
/* ... Missing code here ... */
if (otapClientData.transferMethod == gOtapTransferMethodAtt_c)
{
if (((otapCommand_t*)pValue)->cmdId == gOtapCmdIdImageChunk_c)
{
OtapClient_HandleDataChunk (deviceId,
length,
pValue);
}
}
/* ... Missing code here ... */
}
/* ... Missing code here ... */
}
Finally, when an ATT Confirmation is received for a previously sent ATT Indication the BleApp_GattServerCallback() function
calls the BleApp_ HandleValueConfirmation() function which based on the last sent command to the OTAP Server performs the
necessary OTAP operations. This is done using separate confirmation handling functions for each command that is sent to the
OTAP Server. These functions are:
• OtapClient_HandleNewImageInfoRequestConfirmation()
• OtapClient_HandleImageBlockRequestConfirmation()
• OtapClient_HandleImageTransferCompleteConfirmation()
• OtapClient_HandleErrorNotificationConfirmation()
• OtapClient_HandleStopImageTransferConfirmation()
Outgoing communication from the OTAP Client to the OTAP Server are done using the OtapCS_SendCommandToOtapServer()
function. This function writes the value to be indicated to the OTAP Control Point attribute in the GATT database and then calls
the OtapCS_SendControlPointIndication() which checks if indications are enabled for the target device and sends the actual ATT
Indication. Both functions are implemented in the otap_service.c file.
bleResult_t OtapCS_SendCommandToOtapServer (uint16_t serviceHandle,
void* pCommand,
uint16_t cmdLength)
{
uint16_t handle;
bleUuid_t* pUuid = (bleUuid_t*)&uuid_char_otap_control_point;
/* This message is unexpected on the OTAP Client, the OTAP Client sends L2CAP
* PSM connection requests and expects L2CAP PSM connection responses.
* Disconnect the peer. */
Gap_Disconnect (pConnReq->deviceId);
break;
}
case gL2ca_LePsmConnectionComplete_c:
{
l2caLeCbConnectionComplete_t *pConnComplete =
(l2caLeCbConnectionComplete_t *)pMessage;
break;
}
case gL2ca_LePsmDisconnectNotification_c:
{
l2caLeCbDisconnection_t *pCbDisconnect = (l2caLeCbDisconnection_t *)pMessage;
break;
}
case gL2ca_NoPeerCredits_c:
{
l2caLeCbNoPeerCredits_t *pCbNoPeerCredits =
(l2caLeCbNoPeerCredits_t *)pMessage;
L2ca_SendLeCredit (pCbNoPeerCredits->deviceId,
otapClientData.l2capPsmChannelId,
mAppLeCbInitialCredits_c);
break;
}
case gL2ca_LocalCreditsNotification_c:
{
l2caLeCbLocalCreditsNotification_t *pMsg =
(l2caLeCbLocalCreditsNotification_t *)pMessage;
break;
}
default:
break;
}
}
The OTAP Client must initiate the L2CAP PSM connection if it wants to use the L2CAP transfer method. This is done using the
L2ca_ConnectLePsm() function which is called by the OtapClient_ContinueImageDownload() if the transfer method is L2CAP
and the PSM is found to be disconnected.
static void OtapClient_ContinueImageDownload (deviceId_t deviceId)
{
/* ... Missing code here ... */
/* Check if the L2CAP OTAP PSM is connected and if not try to connect and exit
immediately. */
if ((otapClientData.l2capPsmConnected == FALSE) &&
(otapClientData.state != mOtapClientStateImageDownloadComplete_c))
{
L2ca_ConnectLePsm (gOtap_L2capLePsm_c,
deviceId,
mAppLeCbInitialCredits_c);
return;
}
/* ... Missing code here ... */
}
The PSM data callback BleApp_L2capPsmDataCallback() is used by the OTAP Client to handle incoming image file parts from
the OTAP Server.
static void BleApp_L2capPsmDataCallback (deviceId_t deviceId,
uint8_t* pPacket,
uint16_t packetLength)
{
OtapClient_HandleDataChunk (deviceId,
packetLength,
pPacket);
}
All data chunks regardless of their source (ATT or L2CAP) are handled by the OtapClient_HandleDataChunk() function. This
function checks the validity of Image Chunk messages, parses the image file, requests the continuation or restart of the image
download and triggers the bootloader when the image download is complete.
static void OtapClient_HandleDataChunk (deviceId_t deviceId,
uint16_t length,
uint8_t* pData);
The Image File CRC Value is computed on the fly as the image chunks are received using the OTA_CrcCompute() function from
the OtaSupport module which is called by the OtapClient_HandleDataChunk() function. The OTA_CrcCompute() function has a
parameter for the intermediary CRC value which must be initialized to 0 every time a new image download is started.
The actual write of the received image parts to the storage medium is also done in the OtapClient_HandleDataChunk() function
using the OtaSupport module. This is achieved using the following functions:
• OTA_StartImage() – called before the start of writing a new image to the storage medium.
• OTA_CancelImage() – called whenever an error occurs and the image download process needs to be stopped/restarted
from the beginning.
• OTA_PushImageChunk() – called to write a received image chunk to the storage medium. Note that only the Upgrade
Image Sub-element of the image file is actually written to the storage medium.
• OTA_CommitImage() - called to set up what parts of the downloaded image are written to flash and other information for
the bootloader. The Value field of the Sector Bitmap Sub-element of the Image File is given as a parameter to this function.
• OTA_SetNewImageFlag() – called to set bootloader flags when a new image and the sector bitmap write to the storage
medium are complete. When the MCU is restarted, the bootloader transfers the new image from the storage medium to the
program flash.
To continue the image download process after a block is transferred or to restart it after an error has occurred the
OtapClient_ContinueImageDownload() function is called. This function is used in multiple situations during the image download
process.
To summarize, an outline of the steps required to perform the image download process is shown below:
• Wait for a connection from an OTAP Server
• Wait for the OTAP Server to write the OTAP Control Point CCCD
• Ask or wait for image information from the server
• If a new image is available on the server start the download process using the OtapClient_ContinueImageDownload()
function.
— If the transfer method is L2CAP CoC then initiate a PSM connection to the OTAP Server
• Repeat while image download is not complete
— Wait for image chunks
— Call the OtapClient_HandleDataChunk() function for all received image chunks regardless of the selected transfer
method
◦ Check image file header integrity using the OtapClient_IsImageFileHeaderValid() function.
◦ Write the Upgrade Image Sub-element to the storage medium using OtaSupport module functions.
◦ When the download is complete check image integrity
▪ If the integrity check is successful commit the image using the Sector Bitmap Sub-element and trigger the
bootloader
▪ If integrity check fails restart the image download from the beginning
◦ If the download is not complete ask for a new image chunk
— If any error occurred during the processing of the image chunk restart the download form the last known good
position
• If an image was successfully downloaded and transferred to the storage medium and the bootloader triggered then reset
the MCU to start the flashing process of the new image.
If the image upgrade progress is interrupted before it is finished (by a power loss for example) the bootloader restarts the copy
procedure on the next MCU reset. It uses some flags in non-volatile memory to do this which are set only when the image copy
process has been completed successfully.
The OTAP Bootloader project and source code can be found in the \boards\<board>\wireless_examples\framework
\bootloader_otap\ folder.
For each target board a different executable image is generated. For the FRDMKW41Z demo boards the
bootloader_otap_frdmkw41z.bin is the appropriate bootloader binary image file. Usually the bootloade_otap_<board>.bin file is
the bootloader binary image file name for a specific board.
The next figure shows the memory layout of the device with the relevant sections and their size: the bootloader, the application
and the reserved areas for the situation where external storage is used for the image received over-the-air.
The OTAP Bootloader image occupies the first part of the flash memory. In the current implementation it has a reserved area of
1/32 of the flash size regardless of the actual size of the image.
The OTAP Bootloader is configured to not overwrite itself so any image sent over the air must not contain the Bootloader
application in the reserved section. See the The OTAP Client section which describes how the Bootloader application can be
added to your image.
A typical application image has its memory divided into multiple sections.
• The ISR_TABLE section contains the MCU interrupt table, it has a fixed reserved size.
• The BOOT_FLAGS section contains bootloader flags and the target bootloader version. The OTAP Bootloader looks for
this section immediately after the ISR_TABLE section which has a fixed size.
— New Image Flag – set by the application to tell the OTAP Bootloader that a new image is available. This flag is set by
calling the OTA_SetNewImageFlag() function from the OtaSupport module.
— Image Upgrade Complete Flag – set by the OTAP Bootloader when the new image copy process is completed
successfully.
— Bootloader Version – bootloader version expected by the application – set at compile time.
• The APPLICATION section contains actual application code
— The optional application non-volatile memory (NVM_STORAGE) area is placed right before the FSL_PROD_DATA
section if it is present.
— The optional internal image storage area (OTAP_INTERNAL_IMAGE_STORAGE) is placed before the non-volatile
memory area if it the non-volatile memory area is present or before the FSL_PROD_DATA section if the non-volatile
memory area is not present.
• The NVM_STOARGE section contains data which the application wishes to save between device power cycles.
• The OTAP_INTERNAL_IMAGE_STORAGE section is a reserved space where an image received over-the-air is stored
before it is copied over the APPLICATION section when the OTAP Bootloader is triggered.
• The FSL_PROD_DATA section contains the location of the upgrade image. The location is a 32bit number which is set at
compile time. It is set to 0xFFFFFFFF if external SPI flash storage is used or to a location inside the internal flash memory
(which is always smaller than 0xFFFFFFFF) if internal image storage is used. This is necessary for the OTAP Bootloader
to know the source of the upgrade image. This location in the flash memory is written with the correct value for the type of
storage used (internal or external) when the OTA_StartImage() function is called.
When internal storage is used for the image received over-the-air the memory layout changes as shown in the following image.
The OTAP Bootloader expects a certain image format in the image storage location which is identical regardless if the storage is
internal or external.
The format of the raw image is detailed in the following table.
Chapter 12
Creating a BLE Application When the BLE Host
Stack is Running on Another Processor
This chapter describes how to create a BLE application (host), when the Bluetooth Low Energy Host Stack is running on another
processor (blackbox) and offers code exemples to explain how to achieve this.
The supported serial interfaces between the two chips(application and the BLE Host Stack) are UART, SPI and I2C.
The typical applications employing BLE Host Stack blackboxes are host systems such as a PC tool or an embedded system that
has an application implementation. This chapter describes an embedded application.
See FSCI for BLE Host Stack Reference Manual for explicit information on exercising the BLE Host Stack functionality through
a serial communication interface to a host system.
#if gSerialMgrUseUart_c
/* Init FSCI */
FSCI_Init((void*) mFsciSerials);
pOutServiceHandles);
bleResult_t GattDbDynamic_AddBatteryService (batteryServiceHandles_t* pOutServiceHandles);
bleResult_t GattDbDynamic_AddDeviceInformationService (deviceInfoServiceHandles_t*
pOutServiceHandles);
The service handles are optional.
Also, a generic function is provided, so that the user can add any generic service to the database:
bleResult_t GattDbDynamic_AddServiceInDatabase (serviceInfo_t* pServiceInfo);
Usually, a BLE Application is going to be ported from a single chip solution, where the BLE App and the BLE stack reside on the
same processor and the GATT database is populated statically. The user will need to remove all the attribute handles from any
structure and replace them with gGattDbInvalidHandle_d and then populate them after the services are added dynamically to
the database with the handles returned by the previous API.
Chapter 13
Hybrid (Dual-Mode) Bluetooth® Low Energy and
IEEE® 802.15.4 Applications
This section describes how to add IEEE 802.15.4 functionality to an existing BLE application in order to create a dual mode
application.
QN908X MCU-based platforms do not support 802.15.4, so this chapter about hybrid applications does not apply to these
platforms.
As one can observe, the ieee_802.15.4 folder is added to the existing structure to include the Phy and Mac functionality specific
to IEEE 802.15.4. The Phy folder contains interface and sources, while the Mac folder contains the precompiled MAC library and
interface. The App folder contains global MAC definitions.
App_Init();
mMacInstance = BindToMAC(0);
Mac_RegisterSapHandlers(MCPS_NWK_SapHandler, MLME_NWK_SapHandler, mMacInstance);
/* Start 802.15.4 */
App_StartScan(gScanModeED_c);
}
Example of a MLME SAP to handle the MAC command responses:
resultType_t MLME_NWK_SapHandler (nwkMessage_t* pMsg, instanceId_t instanceId)
{
switch(pMsg-> msgType )
{
case gMlmeScanCnf_c:
/* Process the Scan confirm. */
break;
case gMlmeStartCnf_c:
/* Process the MLME-START confirm. */
break;
case gMlmeAssociateInd_c:
/* A device sent us an Associate Request. We must send back a response. */
break;
}
MEM_BufferFree( pMsg );
return gSuccess_c;
}
Example of a MCPS SAP which handles the MAC data indications and confirms:
resultType_t MCPS_NWK_SapHandler ( mcpsToNwkMessage_t * pMsg, instanceId_t instanceId)
{
switch (pMsg-> msgType )
{
case gMcpsDataCnf_c:
/* The MCPS-Data confirm is sent by the MAC to the network
or application layer when data has been sent. */
break;
case gMcpsDataInd_c:
/* The MCPS-Data indication is sent by the MAC to the network
or application layer when data has been received. */
break;
}
MEM_BufferFree( pMsg );
return gSuccess_c;
}
While NXP has implemented advanced security features, all products may be subject to
unidentified vulnerabilities. Customers are responsible for the design and operation of their
applications and products to reduce the effect of these vulnerabilities on customer’s applications
and products, and NXP accepts no liability for any vulnerability that is discovered. Customers
should implement appropriate design and operating safeguards to minimize the risks associated
with their applications and products.
NXP, the NXP logo, NXP SECURE CONNECTIONS FOR A SMARTER WORLD, COOLFLUX,
EMBRACE, GREENCHIP, HITAG, I2C BUS, ICODE, JCOP, LIFE VIBES, MIFARE, MIFARE
CLASSIC, MIFARE DESFire, MIFARE PLUS, MIFARE FLEX, MANTIS, MIFARE ULTRALIGHT,
MIFARE4MOBILE, MIGLO, NTAG, ROADLINK, SMARTLX, SMARTMX, STARPLUG, TOPFET,
TRENCHMOS, UCODE, Freescale, the Freescale logo, AltiVec, C‑5, CodeTEST, CodeWarrior,
ColdFire, ColdFire+, C‑Ware, the Energy Efficient Solutions logo, Kinetis, Layerscape, MagniV,
mobileGT, PEG, PowerQUICC, Processor Expert, QorIQ, QorIQ Qonverge, Ready Play,
SafeAssure, the SafeAssure logo, StarCore, Symphony, VortiQa, Vybrid, Airfast, BeeKit,
BeeStack, CoreNet, Flexis, MXC, Platform in a Package, QUICC Engine, SMARTMOS, Tower,
TurboLink, and UMEMS are trademarks of NXP B.V. All other product or service names are the
property of their respective owners. Arm, AMBA, Arm Powered, Artisan, Cortex, Jazelle, Keil,
SecurCore, Thumb, TrustZone, and μVision are registered trademarks of Arm Limited (or its
subsidiaries) in the EU and/or elsewhere. Arm7, Arm9, Arm11, big.LITTLE, CoreLink, CoreSight,
DesignStart, Mali, Mbed, NEON, POP, Sensinode, Socrates, ULINK and Versatile are trademarks
of Arm Limited (or its subsidiaries) in the EU and/or elsewhere. All rights reserved. Oracle and
Java are registered trademarks of Oracle and/or its affiliates. The Power Architecture and
Power.org word marks and the Power and Power.org logos and related marks are trademarks
and service marks licensed by Power.org.