Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nimble/host/include/host/ble_att.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ struct os_mbuf;
/**Insufficient Resources to complete the request. */
#define BLE_ATT_ERR_INSUFFICIENT_RES 0x11

/** The server requests the client to rediscover the database. */
#define BLE_ATT_ERR_DB_OUT_OF_SYNC 0x12

/**Requested value is not allowed. */
#define BLE_ATT_ERR_VALUE_NOT_ALLOWED 0x13

Expand Down
13 changes: 13 additions & 0 deletions nimble/host/include/host/ble_gatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ struct ble_hs_cfg;

/** @} */

/** Client supported features size. For now only 3 bits in first octet are defined */
#define BLE_GATT_CHR_CLI_SUP_FEAT_SZ 1

/*** @client. */
/** Represents a GATT error. */
struct ble_gatt_error {
Expand Down Expand Up @@ -1253,6 +1256,16 @@ int ble_gatts_peer_cl_sup_feat_get(uint16_t conn_handle, uint8_t *out_supported_
int ble_gatts_read_cccd(uint16_t conn_handle, uint16_t chr_val_handle,
uint8_t *cccd_value);

/**
* Calculates Database Hash characteristic value based on service definitions
* in the GATT database.
*
* @return 0 on success;
* BLE_HS_EUNKNOWN if initializing CMAC session
* or computing CMAC tag value fails.
*
*/
int ble_gatts_calculate_db_hash(void);
#ifdef __cplusplus
}
#endif
Expand Down
221 changes: 218 additions & 3 deletions nimble/host/include/host/ble_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <inttypes.h>
#include "nimble/ble.h"
#include "host/ble_gatt.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -40,14 +41,19 @@ extern "C" {
* @{
*/
/** Object type: Our security material. */
#define BLE_STORE_OBJ_TYPE_OUR_SEC 1
#define BLE_STORE_OBJ_TYPE_OUR_SEC 1

/** Object type: Peer security material. */
#define BLE_STORE_OBJ_TYPE_PEER_SEC 2
#define BLE_STORE_OBJ_TYPE_PEER_SEC 2

/** Object type: Client Characteristic Configuration Descriptor. */
#define BLE_STORE_OBJ_TYPE_CCCD 3
#define BLE_STORE_OBJ_TYPE_CCCD 3

/** Object type: Peer last known database hash. */
#define BLE_STORE_OBJ_TYPE_DB_HASH 4

/** Object type: Peer Client Supported Features. */
#define BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT 5
/** @} */

/**
Expand Down Expand Up @@ -154,6 +160,61 @@ struct ble_store_value_cccd {
unsigned value_changed:1;
};

/**
* Used as key for lookups of stored database hash.
* This struct corresponds to the BLE_STORE_OBJ_TYPE_DB_HASH store object
* type.
*/
struct ble_store_key_db_hash {
/**
* Key by peer identity address;
* peer_addr=BLE_ADDR_NONE means don't key off peer.
*/
ble_addr_t peer_addr;

/** Number of results to skip; 0 means retrieve the first match. */
uint8_t idx;
};

/**
* Represents a stored database hash of a peer. This struct
* corresponds to the BLE_STORE_OBJ_TYPE_DB_HASH store object type.
*/
struct ble_store_value_db_hash {
/** The peer address associated with the stored database hash. */
ble_addr_t peer_addr;
/** Database hash; can be used to determine whether the peer
* is change aware/unaware. */
const uint8_t *db_hash;
};

/**
* Used as a key for lookups of stored client supported features of specific
* peer. This struct corresponds to the BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT
* store object type.
*/
struct ble_store_key_cl_sup_feat {
/**
* Key by peer identity address;
* peer_addr=BLE_ADDR_NONE means don't key off peer.
*/
ble_addr_t peer_addr;

/** Number of results to skip; 0 means retrieve the first match. */
uint8_t idx;
};

/**
* Represents a stored client supported features of specific peer. This struct
* corresponds to the BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT store object type.
*/
struct ble_store_value_cl_sup_feat {
/** The peer address associated with the stored supported features. */
ble_addr_t peer_addr;
/** Client supported features of a specific peer. */
uint8_t peer_cl_sup_feat[BLE_GATT_CHR_CLI_SUP_FEAT_SZ];
};

/**
* Used as a key for store lookups. This union must be accompanied by an
* object type code to indicate which field is valid.
Expand All @@ -163,6 +224,10 @@ union ble_store_key {
struct ble_store_key_sec sec;
/** Key for Client Characteristic Configuration Descriptor store lookups. */
struct ble_store_key_cccd cccd;
/** Key for database hash store lookups */
struct ble_store_key_db_hash db_hash;
/** Key for Peer Client Supported Features store lookpus. */
struct ble_store_key_cl_sup_feat feat;
};

/**
Expand All @@ -174,6 +239,10 @@ union ble_store_value {
struct ble_store_value_sec sec;
/** Stored Client Characteristic Configuration Descriptor. */
struct ble_store_value_cccd cccd;
/** Stored database hash. */
struct ble_store_value_db_hash db_hash;
/** Stored Client Supported Features. */
struct ble_store_value_cl_sup_feat feat;
};

/** Represents an event associated with the BLE Store. */
Expand Down Expand Up @@ -556,6 +625,132 @@ int ble_store_write_cccd(const struct ble_store_value_cccd *value);
*/
int ble_store_delete_cccd(const struct ble_store_key_cccd *key);

/**
* @brief Writes a database hash to a storage.
*
* This function writes database hash value to a storage based on the
* provided value.
*
* @param value A pointer to a `ble_store_value_db_hash`
* structure representing the database hash
* to be written to a storage.
*
* @return 0 if the database hash was successfully
* written to a storage;
* Non-zero on error.
*/
int ble_store_write_db_hash(const struct ble_store_value_db_hash *value);

/**
* @brief Reads a database hash from a storage.
*
* This function reads a database hash from a storage based on the provided key
* and stores the retrieved value in the specified output structure.
*
* @param key A pointer to a `ble_store_key_db_hash`
* structure representing the key to identify
* the database hash to be read.
* @param out_value A pointer to a `ble_store_value_db_hash`
* structure to store the database hash value
* read from a storage.
*
* @return 0 if the database hash was successfully read
* and stored in the `out_value` structure;
* Non-zero on error.
*/
int ble_store_read_db_hash(const struct ble_store_key_db_hash *key,
struct ble_store_value_db_hash *out_value);

/**
* @brief Deletes a database hash from a storage.
*
* This function deletes a database hash from a storage based on the provided
* key.
*
* @param key A pointer to a `ble_store_key_db_hash`
* structure identifying the database hash to
* be deleted from a storage.
*
* @return 0 if the database hash was successfully deleted
* from a storage;
* Non-zero on error.
*/
int ble_store_delete_db_hash(const struct ble_store_value_db_hash *key);

/**
* @brief Generates a storage key for a database hash entry from its value.
*
* This function generates a storage key for a database hash entry based on
* the provided database hash value.
*
* @param out_key A pointer to a `ble_store_key_db_hash`
* structure where the generated key will be
* stored.
* @param value A pointer to a `ble_store_value_db_hash`
* structure containing the security material
* value from which the key will be generated.
*/
void ble_store_key_from_value_db_hash(struct ble_store_key_db_hash *out_key,
const struct ble_store_value_db_hash *value);

/**
* @brief Reads Client Supported Features value from a storage
*
* This function reads client supported features value from a storage based
* on the provied key and stores the retrieved value in the specified output
* structure.
*
* @param key A pointer to a 'ble_store_key_cl_sup_feat'
* struct representing the key to identify
* Client Supported Features value to be read.
* @param out_value A pointer to a 'ble_store_value_cl_sup_feat'
* struct to store the Client Supported
* Features value read from a storage
*
* @return 0 if the Client Supported Features values was
* successfully read and stored in the
* 'out_value' structure;
* Non-zero on error
*/
int
ble_store_read_peer_cl_sup_feat(const struct ble_store_key_cl_sup_feat *key,
struct ble_store_value_cl_sup_feat *out_value);

/**
* @brief Writes a Client Supported Features value to a storage.
*
* This function writes a Client Supported Features value to a storage based on
* the provided value
*
* @param value A pointer to a 'ble_store_value_cl_sup_feat'
* structure representing the Client Supported
* Features value to be written to a storage.
*
* @return 0 if the value was successfully written to
* a storage;
* Non-zero on error.
*/
int
ble_store_write_peer_cl_sup_feat(const struct ble_store_value_cl_sup_feat
*value);

/**
* @brief Deletes a Client Supported Features value from a storage.
*
* This function deletes a Client Supported Features value from a storage based
* on the provided key.
*
* @param key A pointer to a 'ble_store_key_cl_sup_feat'
* structure identifying the Client Supported
* Features value to be deleted from
* a storage.
*
* @return 0 if the Client Supported Features value was
* successfully written to a storage;
* Non-zero on error.
*/
int ble_store_delete_peer_cl_sup_feat(const struct ble_store_key_cl_sup_feat
*key);

/**
* @brief Generates a storage key for a security material entry from its value.
Expand Down Expand Up @@ -587,6 +782,26 @@ void ble_store_key_from_value_sec(struct ble_store_key_sec *out_key,
void ble_store_key_from_value_cccd(struct ble_store_key_cccd *out_key,
const struct ble_store_value_cccd *value);

/**
* @brief Generates a storage key for a Client Supported Features entry from
* its value
*
* This function generates a storage key for a Client Supported Features value
* entry based on the provided value.
*
* @param out_key A pointer to a 'ble_store_key_cl_sup_feat'
* structure where the generated key will be
* stored.
* @param value A pointer to a 'ble_store_value_cl_sup_feat'
* structure containing the Client Supported
* Features value from which the key will be
* generated.
*/
void
ble_store_key_from_value_peer_cl_sup_feat(struct ble_store_key_cl_sup_feat
*out_key,
const struct ble_store_value_cl_sup_feat
*value);

/**
* @brief Generates a storage key from a value based on the object type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct ble_hs_cfg;
#define BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16 0x2a05
#define BLE_SVC_GATT_CHR_SERVER_SUPPORTED_FEAT_UUID16 0x2b3a
#define BLE_SVC_GATT_CHR_CLIENT_SUPPORTED_FEAT_UUID16 0x2b29
#define BLE_SVC_GATT_CHR_DATABASE_HASH_UUID16 0x2b2a

uint8_t ble_svc_gatt_get_local_cl_supported_feat(void);
void ble_svc_gatt_changed(uint16_t start_handle, uint16_t end_handle);
Expand Down
Loading
Loading