Mbed OS Reference
Loading...
Searching...
No Matches
BLE Class Reference

Abstract away BLE-capable radio transceivers or SOCs. More...

#include <BLE.h>

Data Structures

struct  InitializationCompleteCallbackContext
 Initialization complete event. More...
 
struct  OnEventsToProcessCallbackContext
 Events to process event. More...
 

Public Types

typedef unsigned InstanceID_t
 Opaque type used to store the ID of a BLE instance. More...
 
typedef FunctionPointerWithContext< OnEventsToProcessCallbackContext * > OnEventsToProcessCallback_t
 Events to process event handler. More...
 
typedef void(* InitializationCompleteCallback_t) (InitializationCompleteCallbackContext *context)
 Initialization complete event handler. More...
 

Public Member Functions

InstanceID_t getInstanceID () const
 Fetch the ID of a BLE instance. More...
 
void onEventsToProcess (const OnEventsToProcessCallback_t &on_event_cb)
 Register a callback called when the BLE stack has pending work. More...
 
void processEvents ()
 Process ALL pending events living in the BLE stack and return once all events have been consumed. More...
 
ble_error_t init (InitializationCompleteCallback_t completion_cb=nullptr)
 Initialize the BLE controller/stack. More...
 
template<typename T >
ble_error_t init (T *object, void(T::*completion_cb)(InitializationCompleteCallbackContext *context))
 Initialize the BLE controller/stack. More...
 
bool hasInitialized () const
 Indicate if the BLE instance has been initialized. More...
 
ble_error_t shutdown ()
 Shut down the underlying stack, and reset state of this BLE instance. More...
 
const char * getVersion ()
 This call allows the application to get the BLE stack version information. More...
 
ble::Gapgap ()
 Accessor to Gap. More...
 
const ble::Gapgap () const
 A const alternative to gap(). More...
 
ble::GattServergattServer ()
 Accessor to GattServer. More...
 
const ble::GattServergattServer () const
 A const alternative to gattServer(). More...
 
ble::GattClientgattClient ()
 Accessors to GattClient. More...
 
const ble::GattClientgattClient () const
 A const alternative to gattClient(). More...
 
ble::SecurityManagersecurityManager ()
 Accessors to SecurityManager. More...
 
const ble::SecurityManagersecurityManager () const
 A const alternative to securityManager(). More...
 
void signalEventsToProcess ()
 This function allows the BLE stack to signal that there is work to do and event processing should be done (BLE::processEvent()). More...
 

Static Public Member Functions

static BLEInstance ()
 Get a reference to the BLE singleton. More...
 
static BLEInstance (InstanceID_t id)
 Get a reference to the BLE singleton corresponding to a given interface. More...
 
static const char * errorToString (ble_error_t error)
 Translate error code into a printable string. More...
 

Static Public Attributes

static const InstanceID_t DEFAULT_INSTANCE = 0
 The value of the BLE::InstanceID_t for the default BLE instance. More...
 
static const InstanceID_t NUM_INSTANCES = 1
 The number of permitted BLE instances for the application. More...
 

Detailed Description

Abstract away BLE-capable radio transceivers or SOCs.

Instances of this class have three responsibilities:

  • Initialize the inner BLE subsystem.
  • Signal user code that BLE events are available and an API to process them.
  • Manage access to the instances abstracting each BLE layer:
    • GAP: Handle advertising and scan, as well as connection and disconnection.
    • GATTServer: API to construct and manage a GATT server, which connected peers can access.
    • GATTClient: API to interact with a peer GATT server.
    • SecurityManager: API to manage security.

The user should not create BLE instances directly but rather access to the singleton(s) holding the BLE interfaces present in the system by using the static function Instance().

#include "ble/BLE.h"
BLE& ble_interface = BLE::Instance();
Abstract away BLE-capable radio transceivers or SOCs.
Definition: BLE.h:137
static BLE & Instance()
Get a reference to the BLE singleton.

Next, the signal handling/process mechanism should be set up. By design, Mbed BLE does not impose to the user an event handling/processing mechanism; however, it exposes APIs, which allows an application to compose its own:

  • onEventsToProcess(), which registers a callback that the BLE subsystem will call when there is an event ready to be processed.
  • processEvents(), which processes all the events present in the BLE subsystem.

It is common to bind BLE event mechanism with Mbed EventQueue:

#include <events/mbed_events.h>
#include "ble/BLE.h"
// declare the event queue, which the whole application will share.
static EventQueue event_queue(4 * EVENTS_EVENT_SIZE);
// Function invoked when there is a BLE event available.
// Event processing is put into the event queue.
void schedule_ble_processing(BLE::OnEventsToProcessCallbackContext* context) {
event_queue.call(callback(&(context->ble), &BLE::processEvents));
}
int main()
{
BLE &ble_interface = BLE::Instance();
// Bind event signaling to schedule_ble_processing
ble_interface.onEventsToProcess(schedule_ble_processing);
// Launch BLE initialisation
// Dispatch events in the event queue
event_queue.dispatch_forever();
return 0;
}
void onEventsToProcess(const OnEventsToProcessCallback_t &on_event_cb)
Register a callback called when the BLE stack has pending work.
void processEvents()
Process ALL pending events living in the BLE stack and return once all events have been consumed.
#define EVENTS_EVENT_SIZE
EVENTS_EVENT_SIZE Minimum size of an event This size fits a Callback<void()> at minimum.
Definition: EventQueue.h:39
Events to process event.
Definition: BLE.h:211
BLE & ble
The ble instance which have events to process.
Definition: BLE.h:215

Once the event processing mechanism is in place, the Bluetooth subsystem can be initialized with the init() function. That function accepts in input a callback, which will be invoked once the initialization process has finished.

void on_ble_init_complete(BLE::InitializationCompleteCallbackContext *context)
{
BLE& ble_interface = context->ble;
ble_error_t initialization_error = context->error;
if (initialization_error) {
// handle error
return;
}
// The BLE interface can be accessed now.
}
int main() {
BLE &ble_interface = BLE::Instance();
ble_interface.onEventsToProcess(schedule_ble_processing);
// Initialize the BLE interface
ble_interface.init(on_ble_init_complete);
event_queue.dispatch_forever();
return 0;
}
ble_error_t
Error codes for the BLE API.
Initialization complete event.
Definition: BLE.h:248
BLE & ble
Reference to the BLE object that has been initialized.
Definition: BLE.h:252
ble_error_t error
Error status of the initialization.
Definition: BLE.h:260

Definition at line 137 of file BLE.h.

Member Typedef Documentation

◆ InstanceID_t

typedef unsigned InstanceID_t

Opaque type used to store the ID of a BLE instance.

Deprecated:
BLE singleton supports one instance. You may create multiple instances by using the constructor.

Definition at line 143 of file BLE.h.

◆ OnEventsToProcessCallback_t

Events to process event handler.

Definition at line 222 of file BLE.h.

◆ InitializationCompleteCallback_t

typedef void(* InitializationCompleteCallback_t) (InitializationCompleteCallbackContext *context)

Initialization complete event handler.

Note
There are two versions of init(). In addition to the function-pointer, init() can also take an <Object, member> tuple as its callback target. In case of the latter, the following declaration doesn't apply.

Definition at line 271 of file BLE.h.

Member Function Documentation

◆ Instance() [1/2]

static BLE & Instance ( )
static

Get a reference to the BLE singleton.

Note
Calling Instance() is preferred over constructing a BLE object directly because it returns a reference to singleton.
Returns
A reference to a single object.

◆ Instance() [2/2]

static BLE & Instance ( InstanceID_t  id)
static

Get a reference to the BLE singleton corresponding to a given interface.

There is a static array of BLE singletons.

Note
Calling Instance() is preferred over constructing a BLE object directly because it returns references to singletons.
Parameters
[in]idBLE Instance ID to get.
Returns
A reference to a single object.
Precondition
id shall be less than NUM_INSTANCES.

Definition at line 188 of file BLE.h.

◆ getInstanceID()

InstanceID_t getInstanceID ( ) const

Fetch the ID of a BLE instance.

Returns
Instance id of this BLE instance.

Definition at line 200 of file BLE.h.

◆ onEventsToProcess()

void onEventsToProcess ( const OnEventsToProcessCallback_t on_event_cb)

Register a callback called when the BLE stack has pending work.

By registering a callback, application code can know when event processing has to be scheduled.

Parameters
on_event_cbCallback invoked when there are new events to process.

◆ processEvents()

void processEvents ( )

Process ALL pending events living in the BLE stack and return once all events have been consumed.

See also
onEventsToProcess()

◆ init() [1/2]

ble_error_t init ( InitializationCompleteCallback_t  completion_cb = nullptr)

Initialize the BLE controller/stack.

init() hands control to the underlying BLE module to accomplish initialization. This initialization may tacitly depend on other hardware setup (such as clocks or power-modes) that happens early on during system startup. It may not be safe to call init() from a global static context where ordering is compiler-specific and can't be guaranteed - it is safe to call BLE::init() from within main().

Parameters
[in]completion_cbA callback for when initialization completes for a BLE instance. This is an optional parameter; if no callback is set up, the application can still determine the status of initialization using BLE::hasInitialized() (see below).
Returns
BLE_ERROR_NONE if the initialization procedure started successfully.
Note
If init() returns BLE_ERROR_NONE, the underlying stack must invoke the initialization completion callback at some point.
Nearly all BLE APIs would return BLE_ERROR_INITIALIZATION_INCOMPLETE if used on an instance before the corresponding transport is initialized.
There are two versions of init(). In addition to the function-pointer, init() can also take an <Object, member> pair as its callback target.
Attention
This should be called before using anything else in the BLE API.

Definition at line 306 of file BLE.h.

◆ init() [2/2]

ble_error_t init ( T *  object,
void(T::*)(InitializationCompleteCallbackContext *context)  completion_cb 
)

Initialize the BLE controller/stack.

This is an alternate declaration for init(). This one takes an <Object, member> pair as its callback target.

Parameters
[in]objectObject, which will be used to invoke the completion callback.
[in]completion_cbMember function pointer, which will be invoked when initialization is complete.

Definition at line 323 of file BLE.h.

◆ hasInitialized()

bool hasInitialized ( ) const

Indicate if the BLE instance has been initialized.

Returns
true if initialization has completed for the underlying BLE transport.
Note
The application should set up a callback to signal completion of initialization when using init().

◆ shutdown()

ble_error_t shutdown ( )

Shut down the underlying stack, and reset state of this BLE instance.

Returns
BLE_ERROR_NONE if the instance was shut down without error or the appropriate error code.
Attention
init() must be called afterward to reinstate services and GAP state. This API offers a way to repopulate the GATT database with new services and characteristics.

◆ getVersion()

const char * getVersion ( )

This call allows the application to get the BLE stack version information.

Returns
A pointer to a const string representing the version.
Note
The BLE API owns the string returned.

◆ gap() [1/2]

ble::Gap & gap ( )

Accessor to Gap.

All Gap-related functionality requires going through this accessor.

Returns
A reference to a Gap object associated to this BLE instance.

◆ gap() [2/2]

const ble::Gap & gap ( ) const

A const alternative to gap().

Returns
A const reference to a Gap object associated to this BLE instance.

◆ gattServer() [1/2]

ble::GattServer & gattServer ( )

Accessor to GattServer.

All GattServer related functionality requires going through this accessor.

Returns
A reference to a GattServer object associated to this BLE instance.

◆ gattServer() [2/2]

const ble::GattServer & gattServer ( ) const

A const alternative to gattServer().

Returns
A const reference to a GattServer object associated to this BLE instance.

◆ gattClient() [1/2]

ble::GattClient & gattClient ( )

Accessors to GattClient.

All GattClient related functionality requires going through this accessor.

Returns
A reference to a GattClient object associated to this BLE instance.

◆ gattClient() [2/2]

const ble::GattClient & gattClient ( ) const

A const alternative to gattClient().

Returns
A const reference to a GattClient object associated to this BLE instance.

◆ securityManager() [1/2]

ble::SecurityManager & securityManager ( )

Accessors to SecurityManager.

All SecurityManager-related functionality requires going through this accessor.

Returns
A reference to a SecurityManager object associated to this BLE instance.

◆ securityManager() [2/2]

const ble::SecurityManager & securityManager ( ) const

A const alternative to securityManager().

Returns
A const reference to a SecurityManager object associated to this BLE instance.

◆ errorToString()

static const char * errorToString ( ble_error_t  error)
static

Translate error code into a printable string.

Parameters
[in]errorError code returned by BLE functions.
Returns
A pointer to a const string describing the error.

◆ signalEventsToProcess()

void signalEventsToProcess ( )

This function allows the BLE stack to signal that there is work to do and event processing should be done (BLE::processEvent()).

Note
This function should be called by the port of BLE_API. It is not meant to be used by end users.

Field Documentation

◆ DEFAULT_INSTANCE

const InstanceID_t DEFAULT_INSTANCE = 0
static

The value of the BLE::InstanceID_t for the default BLE instance.

Deprecated:
BLE singleton supports one instance. You may create multiple instances by using the constructor.

Definition at line 149 of file BLE.h.

◆ NUM_INSTANCES

const InstanceID_t NUM_INSTANCES = 1
static

The number of permitted BLE instances for the application.

Deprecated:
BLE singleton supports one instance. You may create multiple instances by using the constructor.

Definition at line 155 of file BLE.h.