mirror of https://github.com/ARMmbed/mbed-os.git
ble: GattClient PIMPL
parent
909676d159
commit
e3cf59e6cd
|
@ -90,7 +90,9 @@ namespace ble {
|
|||
* if it intends to register to server initiated events.
|
||||
*/
|
||||
#if !defined(DOXYGEN_ONLY)
|
||||
namespace interface {
|
||||
namespace impl {
|
||||
class GattClient;
|
||||
}
|
||||
#endif // !defined(DOXYGEN_ONLY)
|
||||
class GattClient {
|
||||
public:
|
||||
|
@ -703,6 +705,9 @@ public:
|
|||
* registered handlers.
|
||||
*/
|
||||
void processHVXEvent(const GattHVXCallbackParams *params);
|
||||
|
||||
private:
|
||||
impl::GattClient *impl;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -711,14 +716,11 @@ public:
|
|||
* @}
|
||||
*/
|
||||
|
||||
#if !defined(DOXYGEN_ONLY)
|
||||
} // namespace interface
|
||||
#endif // !defined(DOXYGEN_ONLY)
|
||||
} // namespace ble
|
||||
|
||||
/* This includes the concrete class implementation, to provide a an alternative API implementation
|
||||
* disable ble-api-implementation and place your header in a path with the same structure */
|
||||
#include "ble/internal/GattClientImpl.h"
|
||||
//#include "ble/internal/GattClientImpl.h"
|
||||
|
||||
/** @deprecated Use the namespaced ble::GattClient instead of the global GattClient. */
|
||||
using ble::GattClient;
|
||||
|
|
|
@ -19,9 +19,12 @@
|
|||
#ifndef IMPL_GATT_CLIENT_H__
|
||||
#define IMPL_GATT_CLIENT_H__
|
||||
|
||||
#include "ble/BLE.h"
|
||||
|
||||
#include "CallChainOfFunctionPointersWithContext.h"
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
#include "ble/common/ble/blecommon.h"
|
||||
#include "ble/common/ble/GattAttribute.h"
|
||||
#include "ble/common/ble/ServiceDiscovery.h"
|
||||
|
@ -33,14 +36,27 @@
|
|||
|
||||
namespace ble {
|
||||
|
||||
class BLEInstanceBase;
|
||||
|
||||
namespace impl {
|
||||
|
||||
class GattClient :
|
||||
public ble::interface::GattClient,
|
||||
public PalSigningMonitor,
|
||||
public PalGattClientEventHandler
|
||||
{
|
||||
public PalGattClientEventHandler {
|
||||
friend PalSigningMonitor;
|
||||
friend BLEInstanceBase;
|
||||
public:
|
||||
using EventHandler = ble::GattClient::EventHandler;
|
||||
using WriteOp_t = ble::GattClient::WriteOp_t;
|
||||
using HVXCallback_t = ble::GattClient::HVXCallback_t ;
|
||||
using GattClientShutdownCallback_t = ble::GattClient::GattClientShutdownCallback_t ;
|
||||
using GattClientShutdownCallbackChain_t = ble::GattClient::GattClientShutdownCallbackChain_t ;
|
||||
using HVXCallbackChain_t = ble::GattClient::HVXCallbackChain_t ;
|
||||
using ReadCallbackChain_t = ble::GattClient::ReadCallbackChain_t ;
|
||||
using WriteCallbackChain_t = ble::GattClient::WriteCallbackChain_t ;
|
||||
|
||||
|
||||
|
||||
void setEventHandler(EventHandler *handler);
|
||||
|
||||
ble_error_t launchServiceDiscovery(
|
||||
|
@ -144,6 +160,7 @@ public:
|
|||
private:
|
||||
/* Disallow copy and assignment. */
|
||||
GattClient(const GattClient &);
|
||||
|
||||
GattClient &operator=(const GattClient &);
|
||||
|
||||
/* ===================================================================== */
|
||||
|
@ -180,14 +197,21 @@ private:
|
|||
struct DescriptorDiscoveryControlBlock;
|
||||
|
||||
ProcedureControlBlock *get_control_block(connection_handle_t connection);
|
||||
|
||||
const ProcedureControlBlock *get_control_block(connection_handle_t connection) const;
|
||||
|
||||
void insert_control_block(ProcedureControlBlock *cb) const;
|
||||
|
||||
void remove_control_block(ProcedureControlBlock *cb) const;
|
||||
|
||||
void on_termination(connection_handle_t connection_handle);
|
||||
|
||||
void on_server_message_received(connection_handle_t, const AttServerMessage &);
|
||||
|
||||
void on_server_response(connection_handle_t, const AttServerMessage &);
|
||||
|
||||
void on_server_event(connection_handle_t, const AttServerMessage &);
|
||||
|
||||
void on_transaction_timeout(connection_handle_t);
|
||||
|
||||
uint16_t get_mtu(connection_handle_t connection) const;
|
||||
|
@ -228,15 +252,21 @@ private:
|
|||
mutable ProcedureControlBlock *control_blocks;
|
||||
bool _is_reseting;
|
||||
|
||||
// TODO initialize
|
||||
::ble::GattClient *client;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Create a PalGattClient from a PalGattClient
|
||||
*/
|
||||
GattClient(PalGattClient &pal_client);
|
||||
|
||||
~GattClient() { }
|
||||
~GattClient()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace impl
|
||||
} // namespace ble
|
||||
|
||||
#endif /* ifndef IMPL_GATT_CLIENT_H__ */
|
||||
|
|
|
@ -23,10 +23,10 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ble/internal/GattClientImpl.h"
|
||||
#include "ble/internal/AttServerMessage.h"
|
||||
#include "ble/common/ble/DiscoveredService.h"
|
||||
#include "ble/common/ble/DiscoveredCharacteristic.h"
|
||||
#include "ble/GattClient.h"
|
||||
#include "ble/common/ble/blecommon.h"
|
||||
#include "ble/internal/BLEInstanceBase.h"
|
||||
#include "ble/SecurityManager.h"
|
||||
|
@ -53,6 +53,14 @@ using ble::AttFindInformationResponse;
|
|||
#define MAC_COUNTER_LENGTH 4
|
||||
|
||||
namespace ble {
|
||||
namespace impl {
|
||||
|
||||
namespace {
|
||||
static constexpr auto GATT_OP_SIGNED_WRITE_CMD = ble::GattClient::GATT_OP_SIGNED_WRITE_CMD;
|
||||
static constexpr auto GATT_OP_WRITE_CMD = ble::GattClient::GATT_OP_WRITE_CMD;
|
||||
static constexpr auto GATT_OP_WRITE_REQ = ble::GattClient::GATT_OP_WRITE_REQ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Type of procedures which can be launched by the client.
|
||||
|
@ -74,9 +82,13 @@ struct GattClient::ProcedureControlBlock {
|
|||
* Base constructor for procedure control block.
|
||||
*/
|
||||
ProcedureControlBlock(procedure_type_t type, connection_handle_t handle) :
|
||||
type(type), connection_handle(handle), next(nullptr) { }
|
||||
type(type), connection_handle(handle), next(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~ProcedureControlBlock() { }
|
||||
virtual ~ProcedureControlBlock()
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Entry point of the control block stack machine.
|
||||
|
@ -118,10 +130,12 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
matching_service_uuid(matching_service_uuid),
|
||||
matching_characteristic_uuid(matching_characteristic_uuid),
|
||||
services_discovered(nullptr),
|
||||
done(false) {
|
||||
done(false)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~DiscoveryControlBlock() {
|
||||
virtual ~DiscoveryControlBlock()
|
||||
{
|
||||
while (services_discovered) {
|
||||
service_t *tmp = services_discovered->next;
|
||||
delete services_discovered;
|
||||
|
@ -129,15 +143,18 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
virtual void handle_timeout_error(GattClient* client) {
|
||||
virtual void handle_timeout_error(GattClient *client)
|
||||
{
|
||||
terminate(client);
|
||||
}
|
||||
|
||||
virtual void abort(GattClient *client) {
|
||||
virtual void abort(GattClient *client)
|
||||
{
|
||||
terminate(client);
|
||||
}
|
||||
|
||||
virtual void handle(GattClient* client, const AttServerMessage& message) {
|
||||
virtual void handle(GattClient *client, const AttServerMessage &message)
|
||||
{
|
||||
// if end of discovery has been requested, ends it immediately
|
||||
if (done) {
|
||||
terminate(client);
|
||||
|
@ -187,7 +204,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
|
||||
template<typename Response>
|
||||
void handle_service_discovered(GattClient* client, const Response& response) {
|
||||
void handle_service_discovered(GattClient *client, const Response &response)
|
||||
{
|
||||
if (!response.size()) {
|
||||
terminate(client);
|
||||
return;
|
||||
|
@ -230,7 +248,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
void start_characteristic_discovery(GattClient* client) {
|
||||
void start_characteristic_discovery(GattClient *client)
|
||||
{
|
||||
if (!services_discovered) {
|
||||
terminate(client);
|
||||
return;
|
||||
|
@ -261,7 +280,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
);
|
||||
}
|
||||
|
||||
void handle_characteristic_discovered(GattClient* client, const AttReadByTypeResponse& response) {
|
||||
void handle_characteristic_discovered(GattClient *client, const AttReadByTypeResponse &response)
|
||||
{
|
||||
for (size_t i = 0; i < response.size(); ++i) {
|
||||
if (last_characteristic.is_valid() == false) {
|
||||
last_characteristic.set_last_handle(response[i].handle - 1);
|
||||
|
@ -294,7 +314,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
void handle_all_characteristics_discovered(GattClient* client) {
|
||||
void handle_all_characteristics_discovered(GattClient *client)
|
||||
{
|
||||
if (last_characteristic.is_valid() == false) {
|
||||
if (matching_characteristic_uuid == UUID()
|
||||
|| matching_characteristic_uuid == last_characteristic.getUUID()) {
|
||||
|
@ -314,7 +335,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
void terminate(GattClient* client) {
|
||||
void terminate(GattClient *client)
|
||||
{
|
||||
// unknown error, terminate the procedure immediately
|
||||
client->remove_control_block(this);
|
||||
connection_handle_t handle = connection_handle;
|
||||
|
@ -322,23 +344,28 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
client->on_termination(handle);
|
||||
}
|
||||
|
||||
uint16_t get_start_handle(const AttReadByGroupTypeResponse::attribute_data_t& data) {
|
||||
uint16_t get_start_handle(const AttReadByGroupTypeResponse::attribute_data_t &data)
|
||||
{
|
||||
return data.group_range.begin;
|
||||
}
|
||||
|
||||
uint16_t get_start_handle(const attribute_handle_range_t& range) {
|
||||
uint16_t get_start_handle(const attribute_handle_range_t &range)
|
||||
{
|
||||
return range.begin;
|
||||
}
|
||||
|
||||
uint16_t get_end_handle(const AttReadByGroupTypeResponse::attribute_data_t& data) {
|
||||
uint16_t get_end_handle(const AttReadByGroupTypeResponse::attribute_data_t &data)
|
||||
{
|
||||
return data.group_range.end;
|
||||
}
|
||||
|
||||
uint16_t get_end_handle(const attribute_handle_range_t& range) {
|
||||
uint16_t get_end_handle(const attribute_handle_range_t &range)
|
||||
{
|
||||
return range.end;
|
||||
}
|
||||
|
||||
UUID get_uuid(const AttReadByGroupTypeResponse::attribute_data_t& data) {
|
||||
UUID get_uuid(const AttReadByGroupTypeResponse::attribute_data_t &data)
|
||||
{
|
||||
if (data.value.size() == 2) {
|
||||
return UUID(data.value[0] | data.value[1] << 8);
|
||||
} else {
|
||||
|
@ -346,13 +373,17 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
UUID get_uuid(const attribute_handle_range_t& range) {
|
||||
UUID get_uuid(const attribute_handle_range_t &range)
|
||||
{
|
||||
return matching_service_uuid;
|
||||
}
|
||||
|
||||
struct service_t {
|
||||
service_t(uint16_t begin, uint16_t end, const UUID &uuid) :
|
||||
begin(begin), end(end), uuid(uuid), next(nullptr) { }
|
||||
begin(begin), end(end), uuid(uuid), next(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
uint16_t begin;
|
||||
uint16_t end;
|
||||
UUID uuid;
|
||||
|
@ -360,7 +391,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
};
|
||||
|
||||
struct characteristic_t : DiscoveredCharacteristic {
|
||||
characteristic_t() : DiscoveredCharacteristic() {
|
||||
characteristic_t() : DiscoveredCharacteristic()
|
||||
{
|
||||
lastHandle = 0x0001;
|
||||
}
|
||||
|
||||
|
@ -369,8 +401,9 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
connection_handle_t connection_handle,
|
||||
uint16_t decl_handle,
|
||||
const Span<const uint8_t> value
|
||||
) : DiscoveredCharacteristic() {
|
||||
gattc = client;
|
||||
) : DiscoveredCharacteristic()
|
||||
{
|
||||
gattc = client->client;
|
||||
uuid = get_uuid(value);
|
||||
props = get_properties(value);
|
||||
declHandle = decl_handle;
|
||||
|
@ -379,7 +412,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
connHandle = connection_handle;
|
||||
}
|
||||
|
||||
static UUID get_uuid(const Span<const uint8_t>& value) {
|
||||
static UUID get_uuid(const Span<const uint8_t> &value)
|
||||
{
|
||||
if (value.size() == 5) {
|
||||
return UUID(value[3] | (value[4] << 8));
|
||||
} else {
|
||||
|
@ -387,7 +421,8 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
static DiscoveredCharacteristic::Properties_t get_properties(const Span<const uint8_t>& value) {
|
||||
static DiscoveredCharacteristic::Properties_t get_properties(const Span<const uint8_t> &value)
|
||||
{
|
||||
uint8_t raw_properties = value[0];
|
||||
DiscoveredCharacteristic::Properties_t result;
|
||||
result._broadcast = (raw_properties & (1 << 0)) ? true : false;
|
||||
|
@ -400,20 +435,24 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
return result;
|
||||
}
|
||||
|
||||
static uint16_t get_value_handle(const Span<const uint8_t>& value) {
|
||||
static uint16_t get_value_handle(const Span<const uint8_t> &value)
|
||||
{
|
||||
return value[1] | (value[2] << 8);
|
||||
}
|
||||
|
||||
void set_last_handle(uint16_t last_handle) {
|
||||
void set_last_handle(uint16_t last_handle)
|
||||
{
|
||||
lastHandle = last_handle;
|
||||
}
|
||||
|
||||
bool is_valid() const {
|
||||
bool is_valid() const
|
||||
{
|
||||
return lastHandle != 0x0000;
|
||||
}
|
||||
};
|
||||
|
||||
void insert_service(service_t* service) {
|
||||
void insert_service(service_t *service)
|
||||
{
|
||||
if (services_discovered == nullptr) {
|
||||
services_discovered = service;
|
||||
return;
|
||||
|
@ -436,7 +475,6 @@ struct GattClient::DiscoveryControlBlock : public ProcedureControlBlock {
|
|||
};
|
||||
|
||||
|
||||
|
||||
struct GattClient::ReadControlBlock : public ProcedureControlBlock {
|
||||
using ProcedureControlBlock::connection_handle;
|
||||
|
||||
|
@ -444,16 +482,19 @@ struct GattClient::ReadControlBlock : public ProcedureControlBlock {
|
|||
connection_handle_t connection_handle, uint16_t attribute_handle, uint16_t offset
|
||||
) : ProcedureControlBlock(READ_PROCEDURE, connection_handle),
|
||||
attribute_handle(attribute_handle),
|
||||
offset(offset), current_offset(offset), data(nullptr) {
|
||||
offset(offset), current_offset(offset), data(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~ReadControlBlock() {
|
||||
virtual ~ReadControlBlock()
|
||||
{
|
||||
if (data != nullptr) {
|
||||
free(data);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void handle_timeout_error(GattClient* client) {
|
||||
virtual void handle_timeout_error(GattClient *client)
|
||||
{
|
||||
GattReadCallbackParams response = {
|
||||
connection_handle,
|
||||
attribute_handle,
|
||||
|
@ -465,7 +506,8 @@ struct GattClient::ReadControlBlock : public ProcedureControlBlock {
|
|||
terminate(client, response);
|
||||
}
|
||||
|
||||
virtual void abort(GattClient *client) {
|
||||
virtual void abort(GattClient *client)
|
||||
{
|
||||
GattReadCallbackParams response = {
|
||||
connection_handle,
|
||||
attribute_handle,
|
||||
|
@ -477,13 +519,15 @@ struct GattClient::ReadControlBlock : public ProcedureControlBlock {
|
|||
terminate(client, response);
|
||||
}
|
||||
|
||||
void terminate(GattClient* client, const GattReadCallbackParams& response) {
|
||||
void terminate(GattClient *client, const GattReadCallbackParams &response)
|
||||
{
|
||||
client->remove_control_block(this);
|
||||
client->processReadResponse(&response);
|
||||
delete this;
|
||||
}
|
||||
|
||||
virtual void handle(GattClient* client, const AttServerMessage& message) {
|
||||
virtual void handle(GattClient *client, const AttServerMessage &message)
|
||||
{
|
||||
switch (message.opcode) {
|
||||
case AttributeOpcode::ERROR_RESPONSE:
|
||||
handle_error(client, static_cast<const AttErrorResponse &>(message));
|
||||
|
@ -514,7 +558,8 @@ struct GattClient::ReadControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
|
||||
template<typename ResponseType>
|
||||
void handle_read_response(GattClient* client, const ResponseType& read_response) {
|
||||
void handle_read_response(GattClient *client, const ResponseType &read_response)
|
||||
{
|
||||
uint16_t mtu_size = client->get_mtu(connection_handle);
|
||||
|
||||
// end of responses ?
|
||||
|
@ -578,7 +623,8 @@ struct GattClient::ReadControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
void handle_error(GattClient* client, const AttErrorResponse& error) {
|
||||
void handle_error(GattClient *client, const AttErrorResponse &error)
|
||||
{
|
||||
ble_error_t status = BLE_ERROR_UNSPECIFIED;
|
||||
|
||||
switch (error.error_code) {
|
||||
|
@ -643,14 +689,17 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
uint16_t write_length
|
||||
) : ProcedureControlBlock(WRITE_PROCEDURE, connection_handle),
|
||||
attribute_handle(attribute_handle), write_length(write_length), offset(0), data(data),
|
||||
prepare_success(false), status(BLE_ERROR_INITIALIZATION_INCOMPLETE), error_code(0xFF) {
|
||||
prepare_success(false), status(BLE_ERROR_INITIALIZATION_INCOMPLETE), error_code(0xFF)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~WriteControlBlock() {
|
||||
virtual ~WriteControlBlock()
|
||||
{
|
||||
free(data);
|
||||
}
|
||||
|
||||
virtual void handle_timeout_error(GattClient* client) {
|
||||
virtual void handle_timeout_error(GattClient* client)
|
||||
{
|
||||
GattWriteCallbackParams response = {
|
||||
connection_handle,
|
||||
attribute_handle,
|
||||
|
@ -661,7 +710,8 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
terminate(client, response);
|
||||
}
|
||||
|
||||
virtual void abort(GattClient *client) {
|
||||
virtual void abort(GattClient *client)
|
||||
{
|
||||
GattWriteCallbackParams response = {
|
||||
connection_handle,
|
||||
attribute_handle,
|
||||
|
@ -672,13 +722,15 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
terminate(client, response);
|
||||
}
|
||||
|
||||
void terminate(GattClient* client, const GattWriteCallbackParams& response) {
|
||||
void terminate(GattClient *client, const GattWriteCallbackParams &response)
|
||||
{
|
||||
client->remove_control_block(this);
|
||||
client->processWriteResponse(&response);
|
||||
delete this;
|
||||
}
|
||||
|
||||
virtual void handle(GattClient* client, const AttServerMessage& message) {
|
||||
virtual void handle(GattClient* client, const AttServerMessage& message)
|
||||
{
|
||||
switch(message.opcode) {
|
||||
case AttributeOpcode::ERROR_RESPONSE:
|
||||
handle_error(client, static_cast<const AttErrorResponse&>(message));
|
||||
|
@ -710,7 +762,8 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
void handle_write_response(GattClient* client, const AttWriteResponse& write_response) {
|
||||
void handle_write_response(GattClient* client, const AttWriteResponse& write_response)
|
||||
{
|
||||
GattWriteCallbackParams response = {
|
||||
connection_handle,
|
||||
attribute_handle,
|
||||
|
@ -722,7 +775,8 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
terminate(client, response);
|
||||
}
|
||||
|
||||
void handle_prepare_write_response(GattClient* client, const AttPrepareWriteResponse& write_response) {
|
||||
void handle_prepare_write_response(GattClient* client, const AttPrepareWriteResponse& write_response)
|
||||
{
|
||||
ble_error_t err = BLE_ERROR_UNSPECIFIED;
|
||||
offset += write_response.partial_value.size();
|
||||
uint16_t data_left = write_length - offset; /* offset is guaranteed to be less of equal to write_length */
|
||||
|
@ -749,7 +803,8 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
void handle_execute_write_response(GattClient* client, const AttExecuteWriteResponse& execute_response) {
|
||||
void handle_execute_write_response(GattClient* client, const AttExecuteWriteResponse& execute_response)
|
||||
{
|
||||
if (prepare_success) {
|
||||
status = BLE_ERROR_NONE;
|
||||
error_code = 0x00;
|
||||
|
@ -766,7 +821,8 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
terminate(client, response);
|
||||
}
|
||||
|
||||
void clear_prepare_queue(GattClient* client, ble_error_t s, uint8_t e) {
|
||||
void clear_prepare_queue(GattClient* client, ble_error_t s, uint8_t e)
|
||||
{
|
||||
prepare_success = false;
|
||||
status = s;
|
||||
error_code = e;
|
||||
|
@ -787,7 +843,8 @@ struct GattClient::WriteControlBlock : public ProcedureControlBlock {
|
|||
}
|
||||
}
|
||||
|
||||
void handle_error(GattClient* client, const AttErrorResponse& error) {
|
||||
void handle_error(GattClient* client, const AttErrorResponse& error)
|
||||
{
|
||||
ble_error_t status = BLE_ERROR_UNSPECIFIED;
|
||||
|
||||
switch (error.error_code) {
|
||||
|
@ -857,12 +914,16 @@ struct GattClient::DescriptorDiscoveryControlBlock : public ProcedureControlBloc
|
|||
discovery_cb(discoveryCallback),
|
||||
termination_cb(terminationCallback),
|
||||
next_handle(characteristic.getValueHandle() + 1),
|
||||
done(false) {
|
||||
done(false)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~DescriptorDiscoveryControlBlock() { }
|
||||
virtual ~DescriptorDiscoveryControlBlock()
|
||||
{
|
||||
}
|
||||
|
||||
ble_error_t start(GattClient* client) {
|
||||
ble_error_t start(GattClient *client)
|
||||
{
|
||||
return client->_pal_client.discover_characteristics_descriptors(
|
||||
connection_handle,
|
||||
attribute_handle_range(
|
||||
|
@ -872,15 +933,18 @@ struct GattClient::DescriptorDiscoveryControlBlock : public ProcedureControlBloc
|
|||
);
|
||||
}
|
||||
|
||||
virtual void handle_timeout_error(GattClient* client) {
|
||||
virtual void handle_timeout_error(GattClient *client)
|
||||
{
|
||||
terminate(client, BLE_ERROR_UNSPECIFIED);
|
||||
}
|
||||
|
||||
virtual void abort(GattClient *client) {
|
||||
virtual void abort(GattClient *client)
|
||||
{
|
||||
terminate(client, BLE_ERROR_INVALID_STATE);
|
||||
}
|
||||
|
||||
virtual void handle(GattClient* client, const AttServerMessage& message) {
|
||||
virtual void handle(GattClient *client, const AttServerMessage &message)
|
||||
{
|
||||
if (done) {
|
||||
terminate(client, BLE_ERROR_NONE);
|
||||
return;
|
||||
|
@ -900,7 +964,8 @@ struct GattClient::DescriptorDiscoveryControlBlock : public ProcedureControlBloc
|
|||
}
|
||||
}
|
||||
|
||||
void handle_error(GattClient* client, const AttErrorResponse& error) {
|
||||
void handle_error(GattClient *client, const AttErrorResponse &error)
|
||||
{
|
||||
if (error.error_code == AttErrorResponse::ATTRIBUTE_NOT_FOUND) {
|
||||
terminate(client, BLE_ERROR_NONE);
|
||||
} else {
|
||||
|
@ -908,10 +973,11 @@ struct GattClient::DescriptorDiscoveryControlBlock : public ProcedureControlBloc
|
|||
}
|
||||
}
|
||||
|
||||
void handle_response(GattClient* client, const AttFindInformationResponse& response) {
|
||||
void handle_response(GattClient *client, const AttFindInformationResponse &response)
|
||||
{
|
||||
for (size_t i = 0; i < response.size(); ++i) {
|
||||
DiscoveredCharacteristicDescriptor descriptor(
|
||||
client, connection_handle, response[i].handle, response[i].uuid
|
||||
client->client, connection_handle, response[i].handle, response[i].uuid
|
||||
);
|
||||
CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t params = {
|
||||
characteristic,
|
||||
|
@ -936,7 +1002,8 @@ struct GattClient::DescriptorDiscoveryControlBlock : public ProcedureControlBloc
|
|||
}
|
||||
}
|
||||
|
||||
void terminate(GattClient* client, ble_error_t status, uint8_t error_code = 0x00) {
|
||||
void terminate(GattClient *client, ble_error_t status, uint8_t error_code = 0x00)
|
||||
{
|
||||
client->remove_control_block(this);
|
||||
CharacteristicDescriptorDiscovery::TerminationCallbackParams_t params = {
|
||||
characteristic,
|
||||
|
@ -963,7 +1030,8 @@ GattClient::GattClient(PalGattClient& pal_client) :
|
|||
_signing_event_handler(nullptr),
|
||||
#endif
|
||||
control_blocks(nullptr),
|
||||
_is_reseting(false) {
|
||||
_is_reseting(false)
|
||||
{
|
||||
_pal_client.when_server_message_received(
|
||||
mbed::callback(this, &GattClient::on_server_message_received)
|
||||
);
|
||||
|
@ -1048,7 +1116,8 @@ ble_error_t GattClient::discoverServices(
|
|||
);
|
||||
}
|
||||
|
||||
bool GattClient::isServiceDiscoveryActive() const {
|
||||
bool GattClient::isServiceDiscoveryActive() const
|
||||
{
|
||||
ProcedureControlBlock *pcb = control_blocks;
|
||||
|
||||
while (pcb) {
|
||||
|
@ -1077,7 +1146,8 @@ void GattClient::terminateServiceDiscovery()
|
|||
ble_error_t GattClient::read(
|
||||
connection_handle_t connection_handle,
|
||||
GattAttribute::Handle_t attribute_handle,
|
||||
uint16_t offset) const
|
||||
uint16_t offset
|
||||
) const
|
||||
{
|
||||
// verify that there is no other procedures going on this connection
|
||||
if (_is_reseting || get_control_block(connection_handle)) {
|
||||
|
@ -1123,7 +1193,8 @@ ble_error_t GattClient::write(
|
|||
GattAttribute::Handle_t attribute_handle,
|
||||
size_t length,
|
||||
const uint8_t *value
|
||||
) const {
|
||||
) const
|
||||
{
|
||||
// verify that there is no other procedures going on this connection
|
||||
if (_is_reseting || get_control_block(connection_handle)) {
|
||||
return BLE_ERROR_INVALID_STATE;
|
||||
|
@ -1173,7 +1244,7 @@ ble_error_t GattClient::write(
|
|||
}
|
||||
return status;
|
||||
#endif // BLE_FEATURE_SIGNING
|
||||
} else if (cmd == GattClient::GATT_OP_WRITE_REQ) {
|
||||
} else if (cmd == GATT_OP_WRITE_REQ) {
|
||||
uint8_t *data = nullptr;
|
||||
|
||||
if (length > (uint16_t) (mtu - WRITE_HEADER_LENGTH)) {
|
||||
|
@ -1282,7 +1353,8 @@ ble_error_t GattClient::discoverCharacteristicDescriptors(
|
|||
|
||||
bool GattClient::isCharacteristicDescriptorDiscoveryActive(
|
||||
const DiscoveredCharacteristic &characteristic
|
||||
) const {
|
||||
) const
|
||||
{
|
||||
ProcedureControlBlock *pcb = control_blocks;
|
||||
|
||||
while (pcb) {
|
||||
|
@ -1330,7 +1402,7 @@ ble_error_t GattClient::negotiateAttMtu(
|
|||
ble_error_t GattClient::reset(void)
|
||||
{
|
||||
/* Notify that the instance is about to shut down. */
|
||||
shutdownCallChain.call(this);
|
||||
shutdownCallChain.call(client);
|
||||
shutdownCallChain.clear();
|
||||
|
||||
onDataReadCallbackChain.clear();
|
||||
|
@ -1357,6 +1429,7 @@ void GattClient::set_signing_event_handler(
|
|||
{
|
||||
_signing_event_handler = signing_event_handler;
|
||||
}
|
||||
|
||||
#endif // BLE_FEATURE_SIGNING
|
||||
|
||||
|
||||
|
@ -1389,7 +1462,6 @@ void GattClient::on_write_command_sent(
|
|||
}
|
||||
|
||||
|
||||
|
||||
void GattClient::on_termination(connection_handle_t connection_handle)
|
||||
{
|
||||
if (_termination_callback) {
|
||||
|
@ -1501,7 +1573,8 @@ GattClient::get_control_block(connection_handle_t connection)
|
|||
|
||||
|
||||
const typename GattClient::ProcedureControlBlock *
|
||||
GattClient::get_control_block(connection_handle_t connection) const {
|
||||
GattClient::get_control_block(connection_handle_t connection) const
|
||||
{
|
||||
ProcedureControlBlock *it = control_blocks;
|
||||
while (it && it->connection_handle != connection) {
|
||||
it = it->next;
|
||||
|
@ -1510,7 +1583,8 @@ GattClient::get_control_block(connection_handle_t connection) const {
|
|||
}
|
||||
|
||||
|
||||
void GattClient::insert_control_block(ProcedureControlBlock* cb) const {
|
||||
void GattClient::insert_control_block(ProcedureControlBlock *cb) const
|
||||
{
|
||||
if (control_blocks == nullptr) {
|
||||
control_blocks = cb;
|
||||
return;
|
||||
|
@ -1524,7 +1598,8 @@ void GattClient::insert_control_block(ProcedureControlBlock* cb) const {
|
|||
}
|
||||
|
||||
|
||||
void GattClient::remove_control_block(ProcedureControlBlock* cb) const {
|
||||
void GattClient::remove_control_block(ProcedureControlBlock *cb) const
|
||||
{
|
||||
if (control_blocks == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
@ -1548,7 +1623,8 @@ void GattClient::remove_control_block(ProcedureControlBlock* cb) const {
|
|||
}
|
||||
|
||||
|
||||
uint16_t GattClient::get_mtu(connection_handle_t connection) const {
|
||||
uint16_t GattClient::get_mtu(connection_handle_t connection) const
|
||||
{
|
||||
uint16_t result = 23;
|
||||
if (_pal_client.get_mtu_size((connection_handle_t) connection, result) != BLE_ERROR_NONE) {
|
||||
result = 23;
|
||||
|
@ -1663,6 +1739,7 @@ void GattClient::processHVXEvent(const GattHVXCallbackParams *params)
|
|||
}
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
} // namespace ble
|
||||
|
||||
#endif // BLE_FEATURE_GATT_SERVER
|
||||
|
|
|
@ -0,0 +1,211 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2020 ARM Limited
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2020 ARM Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ble/GattClient.h"
|
||||
#include "ble/internal/GattClientImpl.h"
|
||||
|
||||
namespace ble {
|
||||
|
||||
void GattClient::setEventHandler(EventHandler *handler)
|
||||
{
|
||||
impl->setEventHandler(handler);
|
||||
}
|
||||
|
||||
ble_error_t GattClient::launchServiceDiscovery(
|
||||
ble::connection_handle_t connectionHandle,
|
||||
ServiceDiscovery::ServiceCallback_t sc,
|
||||
ServiceDiscovery::CharacteristicCallback_t cc,
|
||||
const UUID &matchingServiceUUID,
|
||||
const UUID &matchingCharacteristicUUIDIn
|
||||
)
|
||||
{
|
||||
return impl->launchServiceDiscovery(
|
||||
connectionHandle, sc, cc, matchingServiceUUID, matchingCharacteristicUUIDIn
|
||||
);
|
||||
}
|
||||
|
||||
ble_error_t GattClient::discoverServices(
|
||||
ble::connection_handle_t connectionHandle,
|
||||
ServiceDiscovery::ServiceCallback_t callback,
|
||||
const UUID &matchingServiceUUID
|
||||
)
|
||||
{
|
||||
return impl->discoverServices(connectionHandle, callback, matchingServiceUUID);
|
||||
}
|
||||
|
||||
ble_error_t GattClient::discoverServices(
|
||||
ble::connection_handle_t connectionHandle,
|
||||
ServiceDiscovery::ServiceCallback_t callback,
|
||||
GattAttribute::Handle_t startHandle,
|
||||
GattAttribute::Handle_t endHandle
|
||||
)
|
||||
{
|
||||
return impl->discoverServices(connectionHandle, callback, startHandle, endHandle);
|
||||
}
|
||||
|
||||
bool GattClient::isServiceDiscoveryActive(void) const
|
||||
{
|
||||
return impl->isServiceDiscoveryActive();
|
||||
}
|
||||
|
||||
void GattClient::terminateServiceDiscovery(void)
|
||||
{
|
||||
return impl->terminateServiceDiscovery();
|
||||
}
|
||||
|
||||
ble_error_t GattClient::read(
|
||||
ble::connection_handle_t connHandle,
|
||||
GattAttribute::Handle_t attributeHandle,
|
||||
uint16_t offset
|
||||
) const
|
||||
{
|
||||
return impl->read(connHandle, attributeHandle, offset);
|
||||
}
|
||||
|
||||
ble_error_t GattClient::write(
|
||||
GattClient::WriteOp_t cmd,
|
||||
ble::connection_handle_t connHandle,
|
||||
GattAttribute::Handle_t attributeHandle,
|
||||
size_t length,
|
||||
const uint8_t *value
|
||||
) const
|
||||
{
|
||||
return impl->write(cmd, connHandle, attributeHandle, length, value);
|
||||
}
|
||||
|
||||
/* Event callback handlers. */
|
||||
|
||||
void GattClient::onDataRead(ble::ReadCallback_t callback)
|
||||
{
|
||||
return impl->onDataRead(callback);
|
||||
}
|
||||
|
||||
ble::ReadCallbackChain_t& GattClient::onDataRead()
|
||||
{
|
||||
return impl->onDataRead();
|
||||
}
|
||||
|
||||
void GattClient::onDataWritten(ble::WriteCallback_t callback)
|
||||
{
|
||||
impl->onDataWritten(callback);
|
||||
}
|
||||
|
||||
ble::WriteCallbackChain_t& GattClient::onDataWritten()
|
||||
{
|
||||
return impl->onDataWritten();
|
||||
}
|
||||
|
||||
void GattClient::onServiceDiscoveryTermination(
|
||||
ServiceDiscovery::TerminationCallback_t callback
|
||||
)
|
||||
{
|
||||
return impl->onServiceDiscoveryTermination(callback);
|
||||
}
|
||||
|
||||
ble_error_t GattClient::discoverCharacteristicDescriptors(
|
||||
const DiscoveredCharacteristic& characteristic,
|
||||
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
|
||||
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
|
||||
)
|
||||
{
|
||||
return impl->discoverCharacteristicDescriptors(
|
||||
characteristic,
|
||||
discoveryCallback,
|
||||
terminationCallback
|
||||
);
|
||||
}
|
||||
|
||||
bool GattClient::isCharacteristicDescriptorDiscoveryActive(
|
||||
const DiscoveredCharacteristic& characteristic
|
||||
) const
|
||||
{
|
||||
return impl->isCharacteristicDescriptorDiscoveryActive(characteristic);
|
||||
}
|
||||
|
||||
void GattClient::terminateCharacteristicDescriptorDiscovery(
|
||||
const DiscoveredCharacteristic& characteristic
|
||||
)
|
||||
{
|
||||
return impl->terminateCharacteristicDescriptorDiscovery(characteristic);
|
||||
}
|
||||
|
||||
ble_error_t GattClient::negotiateAttMtu(ble::connection_handle_t connection)
|
||||
{
|
||||
return impl->negotiateAttMtu(connection);
|
||||
}
|
||||
|
||||
void GattClient::onHVX(HVXCallback_t callback)
|
||||
{
|
||||
return impl->onHVX(callback);
|
||||
}
|
||||
|
||||
|
||||
void GattClient::onShutdown(const GattClientShutdownCallback_t& callback)
|
||||
{
|
||||
return impl->onShutdown(callback);
|
||||
}
|
||||
|
||||
GattClient::GattClientShutdownCallbackChain_t& GattClient::onShutdown()
|
||||
{
|
||||
return impl->onShutdown();
|
||||
}
|
||||
|
||||
GattClient::HVXCallbackChain_t& GattClient::onHVX()
|
||||
{
|
||||
return impl->onHVX();
|
||||
}
|
||||
|
||||
ble_error_t GattClient::reset(void)
|
||||
{
|
||||
return impl->reset();
|
||||
}
|
||||
|
||||
void GattClient::processReadResponse(const GattReadCallbackParams *params)
|
||||
{
|
||||
return impl->processReadResponse(params);
|
||||
}
|
||||
|
||||
void GattClient::processWriteResponse(const GattWriteCallbackParams *params)
|
||||
{
|
||||
return impl->processWriteResponse(params);
|
||||
}
|
||||
|
||||
void GattClient::processHVXEvent(const GattHVXCallbackParams *params)
|
||||
{
|
||||
return impl->processHVXEvent(params);
|
||||
}
|
||||
|
||||
} // namespace ble
|
||||
|
Loading…
Reference in New Issue