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>
2322LOG_MODULE_DECLARE (fp_fmdn , LOG_LEVEL_INF );
@@ -39,6 +38,7 @@ static bool fmdn_provisioned;
3938
4039static struct bt_conn * fp_conn ;
4140static struct bt_le_ext_adv * fp_adv_set ;
41+ static bool fp_adv_set_active ;
4242static bool fp_adv_rpa_rotation_suspended ;
4343static enum app_fp_adv_mode fp_adv_mode = APP_FP_ADV_MODE_OFF ;
4444static uint32_t fp_adv_request_bm ;
@@ -62,6 +62,35 @@ static void fp_adv_restart_work_handle(struct k_work *w);
6262
6363static 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+
6594static 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)
212280static 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
230298static 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}
0 commit comments