Bluetooth: Mesh: Add callback for received beacons

This adds callbacks for Secure and Private Network Beacons.
SNB callbacks are called after `secure_beacon_authenticate` ends
with success, and Private Beacon callback after Private Beacon
payload is decrypted succsessfully.

Signed-off-by: Krzysztof Kopyściński <krzysztof.kopyscinski@codecoup.pl>
This commit is contained in:
Krzysztof Kopyściński 2023-04-18 14:13:06 +02:00 committed by Carles Cufí
commit 0a55ec8d52
3 changed files with 101 additions and 0 deletions

View file

@ -762,6 +762,73 @@ struct bt_mesh_friend_cb {
static const STRUCT_SECTION_ITERABLE(bt_mesh_friend_cb, \
_CONCAT(bt_mesh_friend_cb_, \
_name))
#if defined(CONFIG_BT_TESTING)
struct bt_mesh_snb {
/** Flags */
uint8_t flags;
/** Network ID */
uint64_t net_id;
/** IV Index */
uint32_t iv_idx;
/** Authentication Value */
uint64_t auth_val;
};
#if defined(CONFIG_BT_MESH_V1d1)
struct bt_mesh_prb {
/** Random */
uint8_t random[13];
/** Flags */
uint8_t flags;
/** IV Index */
uint32_t iv_idx;
/** Authentication tag */
uint64_t auth_tag;
};
#endif
/** Beacon callback functions. */
struct bt_mesh_beacon_cb {
/** @brief Secure Network Beacon received.
*
* This callback notifies the application that Secure Network Beacon
* was received.
*
* @param snb Structure describing received Secure Network Beacon
*/
void (*snb_received)(const struct bt_mesh_snb *snb);
#if defined(CONFIG_BT_MESH_V1d1)
/** @brief Private Beacon received.
*
* This callback notifies the application that Private Beacon
* was received and successfully decrypted.
*
* @param prb Structure describing received Private Beacon
*/
void (*priv_received)(const struct bt_mesh_prb *prb);
#endif
};
/**
* @brief Register a callback structure for beacon events.
*
* Registers a callback structure that will be called whenever beacon advertisement
* is received.
*
* @param _name Name of callback structure.
*/
#define BT_MESH_BEACON_CB_DEFINE(_name) \
static const STRUCT_SECTION_ITERABLE(bt_mesh_beacon_cb, \
_CONCAT(bt_mesh_beacon_cb_, \
_name))
#endif
/** @brief Terminate Friendship.
*

View file

@ -17,6 +17,10 @@
ITERABLE_SECTION_ROM(bt_mesh_app_key_cb, 4)
ITERABLE_SECTION_ROM(bt_mesh_hb_cb, 4)
#if defined(CONFIG_BT_TESTING)
ITERABLE_SECTION_ROM(bt_mesh_beacon_cb, 4)
#endif
#endif
#if defined(CONFIG_BT_MESH_FRIEND)

View file

@ -501,6 +501,21 @@ static bool secure_beacon_authenticate(struct bt_mesh_subnet *sub, void *cb_data
for (int i = 0; i < ARRAY_SIZE(sub->keys); i++) {
if (sub->keys[i].valid && auth_match(&sub->keys[i], params)) {
params->new_key = (i > 0);
#if defined(CONFIG_BT_TESTING)
struct bt_mesh_snb beacon_info;
beacon_info.flags = params->flags;
memcpy(&beacon_info.net_id, params->net_id, 8);
beacon_info.iv_idx = params->iv_index;
memcpy(&beacon_info.auth_val, params->auth, 8);
STRUCT_SECTION_FOREACH(bt_mesh_beacon_cb, cb) {
if (cb->snb_received) {
cb->snb_received(&beacon_info);
}
}
#endif
return true;
}
}
@ -526,6 +541,21 @@ static bool priv_beacon_decrypt(struct bt_mesh_subnet *sub, void *cb_data)
params->new_key = (i > 0);
params->flags = out[0];
params->iv_index = sys_get_be32(&out[1]);
#if defined(CONFIG_BT_TESTING)
struct bt_mesh_prb beacon_info;
memcpy(beacon_info.random, params->random, 13);
beacon_info.flags = params->flags;
beacon_info.iv_idx = params->iv_index;
memcpy(&beacon_info.auth_tag, params->auth, 8);
STRUCT_SECTION_FOREACH(bt_mesh_beacon_cb, cb) {
if (cb->priv_received) {
cb->priv_received(&beacon_info);
}
}
#endif
return true;
}
}