Skip to content

Commit a5fff6a

Browse files
kapi-nojukkar
authored andcommitted
samples: bluetooth: fast_pair: locator_tag: remove ui dep in fp adv
Removed the UI module dependency in the Fast Pair advertising module that is part of the Fast Pair Locator Tag sample. Ref: NCSDK-30487 Signed-off-by: Kamil Piszczek <[email protected]>
1 parent 27e0a2a commit a5fff6a

File tree

3 files changed

+127
-12
lines changed

3 files changed

+127
-12
lines changed

samples/bluetooth/fast_pair/locator_tag/include/app_fp_adv.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ int app_fp_adv_id_set(uint8_t id);
108108
*/
109109
uint8_t app_fp_adv_id_get(void);
110110

111+
/** Fast Pair advertising information callback descriptor. */
112+
struct app_fp_adv_info_cb {
113+
/** Notify that the advertising state has changed.
114+
*
115+
* This information callback is used to track the state of the Fast Pair advertising set.
116+
* The current state can be used to signal the application mode in the user interface
117+
* (for example, on LEDs).
118+
*
119+
* @param enable True: Fast Pair advertising is enabled.
120+
* False: Fast Pair advertising is disabled.
121+
*/
122+
void (*state_changed)(bool enabled);
123+
};
124+
125+
/** Register Fast Pair advertising information callbacks.
126+
*
127+
* This function registers an instance of information callbacks. The registered instance needs to
128+
* persist in the memory after this function exits, as it is used directly without the copy
129+
* operation. It is possible to register only one instance of callbacks with this API.
130+
*
131+
* This function can only be called before enabling Fast Pair with the @ref app_fp_adv_enable
132+
* API.
133+
*
134+
* This function must be called in the cooperative thread context.
135+
*
136+
* @param cb Callback struct.
137+
*
138+
* @return Zero on success or negative error code otherwise
139+
*/
140+
int app_fp_adv_info_cb_register(const struct app_fp_adv_info_cb *cb);
141+
111142
/** Initialize the Fast Pair advertising module.
112143
*
113144
* @return 0 if the operation was successful. Otherwise, a (negative) error code is returned.

samples/bluetooth/fast_pair/locator_tag/src/fp_adv.c

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <bluetooth/services/fast_pair/fmdn.h>
1818

1919
#include "app_fp_adv.h"
20-
#include "app_ui.h"
2120

2221
#include <zephyr/logging/log.h>
2322
LOG_MODULE_DECLARE(fp_fmdn, LOG_LEVEL_INF);
@@ -39,6 +38,7 @@ static bool fmdn_provisioned;
3938

4039
static struct bt_conn *fp_conn;
4140
static struct bt_le_ext_adv *fp_adv_set;
41+
static bool fp_adv_set_active;
4242
static bool fp_adv_rpa_rotation_suspended;
4343
static enum app_fp_adv_mode fp_adv_mode = APP_FP_ADV_MODE_OFF;
4444
static uint32_t fp_adv_request_bm;
@@ -62,6 +62,35 @@ static void fp_adv_restart_work_handle(struct k_work *w);
6262

6363
static K_WORK_DEFINE(fp_adv_restart_work, fp_adv_restart_work_handle);
6464

65+
/* Reference to the Fast Pair advertising information callback structure. */
66+
static const struct app_fp_adv_info_cb *fast_pair_adv_info_cb;
67+
68+
static void fp_adv_state_changed_notify(bool enabled)
69+
{
70+
if (fast_pair_adv_info_cb && fast_pair_adv_info_cb->state_changed) {
71+
fast_pair_adv_info_cb->state_changed(enabled);
72+
}
73+
}
74+
75+
int app_fp_adv_info_cb_register(const struct app_fp_adv_info_cb *cb)
76+
{
77+
if (app_fp_adv_is_ready()) {
78+
return -EACCES;
79+
}
80+
81+
if (!cb) {
82+
return -EINVAL;
83+
}
84+
85+
if (fast_pair_adv_info_cb) {
86+
return -EALREADY;
87+
}
88+
89+
fast_pair_adv_info_cb = cb;
90+
91+
return 0;
92+
}
93+
6594
static uint16_t fp_adv_rpa_timeout_calculate(void)
6695
{
6796
int err;
@@ -178,18 +207,58 @@ static int fp_adv_payload_set(bool rpa_rotated, bool new_session)
178207
return 0;
179208
}
180209

181-
static int bt_stack_advertising_update(void)
210+
static int bt_stack_advertising_start(void)
182211
{
183212
int err;
184213
struct bt_le_ext_adv_start_param ext_adv_start_param = {0};
185214

186215
__ASSERT(fp_adv_set, "Fast Pair: invalid state of the advertising set");
216+
217+
if (fp_adv_set_active) {
218+
return 0;
219+
}
220+
221+
err = bt_le_ext_adv_start(fp_adv_set, &ext_adv_start_param);
222+
if (err) {
223+
LOG_ERR("Fast Pair: bt_le_ext_adv_start returned error: %d", err);
224+
return err;
225+
}
226+
227+
fp_adv_set_active = true;
228+
229+
return 0;
230+
}
231+
232+
static int bt_stack_advertising_stop(void)
233+
{
234+
int err;
235+
236+
__ASSERT(fp_adv_set, "Fast Pair: invalid state of the advertising set");
237+
238+
if (!fp_adv_set_active) {
239+
return 0;
240+
}
241+
242+
err = bt_le_ext_adv_stop(fp_adv_set);
243+
if (err) {
244+
LOG_ERR("Fast Pair: cannot stop advertising (err: %d)", err);
245+
return err;
246+
}
247+
248+
fp_adv_set_active = false;
249+
250+
return 0;
251+
}
252+
253+
static int bt_stack_advertising_update(void)
254+
{
255+
int err;
256+
187257
__ASSERT(!fp_conn, "Fast Pair: invalid connection state");
188258

189259
if (fp_adv_mode == APP_FP_ADV_MODE_OFF) {
190-
err = bt_le_ext_adv_stop(fp_adv_set);
260+
err = bt_stack_advertising_stop();
191261
if (err) {
192-
LOG_ERR("Fast Pair: cannot stop advertising (err: %d)", err);
193262
return err;
194263
}
195264
} else {
@@ -199,9 +268,8 @@ static int bt_stack_advertising_update(void)
199268
return err;
200269
}
201270

202-
err = bt_le_ext_adv_start(fp_adv_set, &ext_adv_start_param);
271+
err = bt_stack_advertising_start();
203272
if (err) {
204-
LOG_ERR("Fast Pair: bt_le_ext_adv_start returned error: %d", err);
205273
return err;
206274
}
207275
}
@@ -212,26 +280,27 @@ static int bt_stack_advertising_update(void)
212280
static void fp_advertising_update(void)
213281
{
214282
int err;
283+
bool fp_adv_set_was_active = fp_adv_set_active;
215284

216285
err = bt_stack_advertising_update();
217286
if (!err) {
218-
app_ui_state_change_indicate(APP_UI_STATE_FP_ADV,
219-
(fp_adv_mode != APP_FP_ADV_MODE_OFF));
220-
221287
LOG_INF("Fast Pair: advertising in the %s mode",
222288
fp_adv_mode_description[fp_adv_mode]);
223289
} else {
224-
app_ui_state_change_indicate(APP_UI_STATE_FP_ADV, false);
225-
226290
LOG_ERR("Fast Pair: advertising failed to start (err %d)", err);
227291
}
292+
293+
if (fp_adv_set_was_active != fp_adv_set_active) {
294+
fp_adv_state_changed_notify(fp_adv_set_active);
295+
}
228296
}
229297

230298
static void fp_adv_connected(struct bt_le_ext_adv *adv, struct bt_le_ext_adv_connected_info *info)
231299
{
232300
__ASSERT_NO_MSG(!fp_conn);
233301

234-
app_ui_state_change_indicate(APP_UI_STATE_FP_ADV, false);
302+
fp_adv_set_active = false;
303+
fp_adv_state_changed_notify(fp_adv_set_active);
235304

236305
fp_conn = info->conn;
237306
}

samples/bluetooth/fast_pair/locator_tag/src/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,15 @@ static struct bt_fast_pair_fmdn_info_cb fmdn_info_cb = {
422422
.provisioning_state_changed = fmdn_provisioning_state_changed,
423423
};
424424

425+
static void fp_adv_state_changed(bool enabled)
426+
{
427+
app_ui_state_change_indicate(APP_UI_STATE_FP_ADV, enabled);
428+
}
429+
430+
static const struct app_fp_adv_info_cb fp_adv_info_cb = {
431+
.state_changed = fp_adv_state_changed,
432+
};
433+
425434
static int fast_pair_prepare(void)
426435
{
427436
int err;
@@ -438,6 +447,12 @@ static int fast_pair_prepare(void)
438447
return err;
439448
}
440449

450+
err = app_fp_adv_info_cb_register(&fp_adv_info_cb);
451+
if (err) {
452+
LOG_ERR("Fast Pair: app_fp_adv_info_cb_register failed (err %d)", err);
453+
return err;
454+
}
455+
441456
return 0;
442457
}
443458

0 commit comments

Comments
 (0)