Skip to content

Commit 7476d7c

Browse files
bluetooth: mesh: update le pair responder model with BT_APP_PASSKEY
BT_FIXED_PASSKEY is depracated so wee need to aling the le pair responder model with the new Kconfig BT_APP_PASSKEY usage. Depracate bt_mesh_le_pair_resp_passkey_invalidate, it not needed anymore. Adding bt_mesh_le_pair_resp_passkey_get to be able to retreive the passkey set randomly or by app. Signed-off-by: alperen sener <[email protected]>
1 parent 97c8465 commit 7476d7c

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

include/bluetooth/mesh/vnd/le_pair_resp.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ extern "C" {
2323
#define BT_MESH_VENDOR_COMPANY_ID_LE_PAIR_RESP 0x0059
2424
#define BT_MESH_MODEL_ID_LE_PAIR_RESP 0x000E
2525

26+
#if defined(CONFIG_BT_FIXED_PASSKEY)
27+
#define INVALID_PASSKEY BT_PASSKEY_INVALID
28+
#else
29+
#define INVALID_PASSKEY BT_PASSKEY_RAND
30+
#endif
31+
2632
/** @def BT_MESH_MODEL_LE_PAIR_RESP
2733
*
2834
* @brief LE Pairing Responder model entry.
@@ -35,19 +41,31 @@ extern "C" {
3541
/* @brief Invalidate previously used passkey.
3642
*
3743
* A user must call this function when a pairing request completes regardless of the status.
44+
*
45+
* @deprecated Use bt_mesh_le_pair_resp_passkey_set(INVALID_PASSKEY) instead.
3846
*/
39-
void bt_mesh_le_pair_resp_passkey_invalidate(void);
47+
__deprecated void bt_mesh_le_pair_resp_passkey_invalidate(void);
4048

4149
/* @brief Set own passkey instead of using randomly generating passkeys.
4250
*
4351
* By default, passkeys will be randomly generated on every new request. This function allows to use
4452
* pre-defined passkey instead.
4553
*
46-
* @params passkey Passkey to use for the pairing, or @ref BT_PASSKEY_INVALID to use randomly
54+
* @params passkey Passkey to use for the pairing, or @ref INVALID_PASSKEY to use randomly
4755
* generated passkey again.
4856
*/
4957
void bt_mesh_le_pair_resp_passkey_set(uint32_t passkey);
5058

59+
/** @brief Get own passkey.
60+
*
61+
* This function will return the passkey set by the @ref bt_mesh_le_pair_resp_passkey_set function or it will return
62+
* a randomly generated passkey by the le pair responder reset message and the passkey is invalidated immediately.
63+
* @ref INVALID_PASSKEY is returned otherwise.
64+
*
65+
* @return Own passkey, or INVALID_PASSKEY if the passkey is not set or randomly generated.
66+
*/
67+
const uint32_t bt_mesh_le_pair_resp_passkey_get(void);
68+
5169
/** @cond INTERNAL_HIDDEN */
5270

5371
extern const struct bt_mesh_model_op _bt_mesh_le_pair_resp_op[];

subsys/bluetooth/mesh/vnd/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ config BT_MESH_LE_PAIR_RESP
8888
bool "LE Pairing Responder model"
8989
select BT_MESH_VENDOR_MODELS
9090
select EXPERIMENTAL
91-
depends on BT_FIXED_PASSKEY
91+
depends on BT_FIXED_PASSKEY || BT_APP_PASSKEY
9292
depends on BT_SMP_SC_ONLY
9393
help
9494
Enable the LE Pairing Responder model. The LE Pairing Responder model is used to generate

subsys/bluetooth/mesh/vnd/le_pair_resp.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,38 @@ LOG_MODULE_REGISTER(bt_mesh_le_pair_resp);
2424
#define STATUS_PASSKEY_SET 0x00
2525
#define STATUS_PASSKEY_NOT_SET 0x01
2626

27-
static uint32_t predefined_passkey = BT_PASSKEY_INVALID;
27+
static uint32_t predefined_passkey = INVALID_PASSKEY;
2828

2929
static int handle_reset(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
3030
struct net_buf_simple *buf)
3131
{
3232
uint32_t passkey;
3333
uint8_t status = STATUS_PASSKEY_SET;
34-
int err;
3534

3635
if (buf->len != 0) {
3736
return -EINVAL;
3837
}
3938

4039
BT_MESH_MODEL_BUF_DEFINE(rsp, BT_MESH_LE_PAIR_OP, 5);
4140

42-
if (predefined_passkey != BT_PASSKEY_INVALID) {
41+
if (predefined_passkey != INVALID_PASSKEY) {
4342
passkey = predefined_passkey;
4443
} else {
4544
passkey = sys_rand32_get() % 1000000;
45+
#if !defined(CONFIG_BT_FIXED_PASSKEY)
46+
/* Overwrite the predefined passkey with the randomly generated passkey.
47+
So the le pair responder can use the randomly generated passkey for the next pairing request.*/
48+
predefined_passkey = passkey;
49+
#endif
4650
}
4751

48-
err = bt_passkey_set(passkey);
52+
#if defined(CONFIG_BT_FIXED_PASSKEY)
53+
int err = bt_passkey_set(passkey);
4954
if (err) {
5055
LOG_ERR("Unable to set passkey (err: %d)", err);
5156
status = STATUS_PASSKEY_NOT_SET;
5257
}
58+
#endif /* CONFIG_BT_FIXED_PASSKEY */
5359

5460
bt_mesh_model_msg_init(&rsp, BT_MESH_LE_PAIR_OP);
5561
net_buf_simple_add_u8(&rsp, BT_MESH_LE_PAIR_OP_STATUS);
@@ -86,7 +92,7 @@ const struct bt_mesh_model_op _bt_mesh_le_pair_resp_op[] = {
8692

8793
static int bt_mesh_le_pair_resp_init(const struct bt_mesh_model *model)
8894
{
89-
bt_mesh_le_pair_resp_passkey_invalidate();
95+
bt_mesh_le_pair_resp_passkey_set(INVALID_PASSKEY);
9096

9197
return 0;
9298
}
@@ -95,7 +101,7 @@ static void bt_mesh_le_pair_resp_reset(const struct bt_mesh_model *model)
95101
{
96102
int err;
97103

98-
bt_mesh_le_pair_resp_passkey_invalidate();
104+
bt_mesh_le_pair_resp_passkey_set(INVALID_PASSKEY);
99105

100106
for (uint8_t id = 0; id < CONFIG_BT_ID_MAX; id++) {
101107
err = bt_unpair(id, NULL);
@@ -112,10 +118,22 @@ const struct bt_mesh_model_cb _bt_mesh_le_pair_resp_cb = {
112118

113119
void bt_mesh_le_pair_resp_passkey_invalidate(void)
114120
{
115-
(void)bt_passkey_set(BT_PASSKEY_INVALID);
121+
#if defined(CONFIG_BT_FIXED_PASSKEY)
122+
(void)bt_passkey_set(INVALID_PASSKEY);
123+
#endif /* CONFIG_BT_FIXED_PASSKEY */
124+
#if defined(CONFIG_BT_APP_PASSKEY)
125+
predefined_passkey = INVALID_PASSKEY;
126+
#endif /* CONFIG_BT_APP_PASSKEY */
116127
}
117128

118129
void bt_mesh_le_pair_resp_passkey_set(uint32_t passkey)
119130
{
120131
predefined_passkey = passkey;
121132
}
133+
134+
const uint32_t bt_mesh_le_pair_resp_passkey_get(void)
135+
{
136+
uint32_t passkey = predefined_passkey;
137+
predefined_passkey = INVALID_PASSKEY;
138+
return passkey;
139+
}

0 commit comments

Comments
 (0)