@@ -71,7 +71,7 @@ static void disconnected_cb(struct bt_conn *conn, uint8_t reason)
7171 conns [conn_index ] = NULL ;
7272}
7373
74- static struct bt_conn_cb conn_callbacks = {
74+ BT_CONN_CB_DEFINE ( conn_callbacks ) = {
7575 .connected = connected_cb ,
7676 .disconnected = disconnected_cb
7777};
@@ -156,6 +156,22 @@ static bool csip_set_coordinator_oap_cb(const struct bt_csip_set_coordinator_set
156156 return true;
157157}
158158
159+ static bool csip_found (struct bt_data * data , void * user_data );
160+ static void csip_set_coordinator_scan_recv (const struct bt_le_scan_recv_info * info ,
161+ struct net_buf_simple * ad )
162+ {
163+ /* We're only interested in connectable events */
164+ if (info -> adv_props & BT_GAP_ADV_PROP_CONNECTABLE ) {
165+ if (cur_inst != NULL ) {
166+ bt_data_parse (ad , csip_found , (void * )info -> addr );
167+ }
168+ }
169+ }
170+
171+ static struct bt_le_scan_cb csip_set_coordinator_scan_callbacks = {
172+ .recv = csip_set_coordinator_scan_recv
173+ };
174+
159175static bool csip_found (struct bt_data * data , void * user_data )
160176{
161177 if (bt_csip_set_coordinator_is_set_member (cur_inst -> info .set_sirk , data )) {
@@ -182,6 +198,8 @@ static bool csip_found(struct bt_data *data, void *user_data)
182198
183199 (void )k_work_cancel_delayable (& discover_members_timer );
184200
201+ bt_le_scan_cb_unregister (& csip_set_coordinator_scan_callbacks );
202+
185203 err = bt_le_scan_stop ();
186204 if (err != 0 ) {
187205 shell_error (ctx_shell ,
@@ -197,62 +215,37 @@ static bool csip_found(struct bt_data *data, void *user_data)
197215 return true;
198216}
199217
200- static void csip_set_coordinator_scan_recv (const struct bt_le_scan_recv_info * info ,
201- struct net_buf_simple * ad )
202- {
203- /* We're only interested in connectable events */
204- if (info -> adv_props & BT_GAP_ADV_PROP_CONNECTABLE ) {
205- if (cur_inst != NULL ) {
206- bt_data_parse (ad , csip_found , (void * )info -> addr );
207- }
208- }
209- }
210-
211- static struct bt_le_scan_cb csip_set_coordinator_scan_callbacks = {
212- .recv = csip_set_coordinator_scan_recv
213- };
214-
215218static void discover_members_timer_handler (struct k_work * work )
216219{
217220 int err ;
218221
219222 shell_error (ctx_shell , "Could not find all members (%u / %u)" ,
220223 members_found , cur_inst -> info .set_size );
221224
225+ bt_le_scan_cb_unregister (& csip_set_coordinator_scan_callbacks );
226+
222227 err = bt_le_scan_stop ();
223228 if (err != 0 ) {
224229 shell_error (ctx_shell , "Failed to stop scan: %d" , err );
225230 }
226231}
227232
228- static int cmd_csip_set_coordinator_init (const struct shell * sh , size_t argc ,
229- char * argv [])
230- {
231- static bool initialized ;
232-
233- if (initialized ) {
234- return - EALREADY ;
235- }
236-
237- k_work_init_delayable (& discover_members_timer ,
238- discover_members_timer_handler );
239- bt_le_scan_cb_register (& csip_set_coordinator_scan_callbacks );
240- bt_csip_set_coordinator_register_cb (& cbs );
241- bt_conn_cb_register (& conn_callbacks );
242-
243- initialized = true;
244-
245- return 0 ;
246- }
247-
248233static int cmd_csip_set_coordinator_discover (const struct shell * sh ,
249234 size_t argc , char * argv [])
250235{
251236 char addr [BT_ADDR_LE_STR_LEN ];
237+ static bool initialized ;
252238 long member_index = 0 ;
253239 struct bt_conn * conn ;
254240 int err ;
255241
242+ if (!initialized ) {
243+ k_work_init_delayable (& discover_members_timer ,
244+ discover_members_timer_handler );
245+ bt_csip_set_coordinator_register_cb (& cbs );
246+ initialized = true;
247+ }
248+
256249 if (argc > 1 ) {
257250 member_index = strtol (argv [1 ], NULL , 0 );
258251
@@ -317,6 +310,8 @@ static int cmd_csip_set_coordinator_discover_members(const struct shell *sh,
317310 return err ;
318311 }
319312
313+ bt_le_scan_cb_register (& csip_set_coordinator_scan_callbacks );
314+
320315 err = bt_le_scan_start (BT_LE_SCAN_ACTIVE , NULL );
321316 if (err != 0 ) {
322317 shell_error (sh , "Could not start scan: %d" , err );
@@ -489,9 +484,6 @@ static int cmd_csip_set_coordinator(const struct shell *sh, size_t argc,
489484}
490485
491486SHELL_STATIC_SUBCMD_SET_CREATE (csip_set_coordinator_cmds ,
492- SHELL_CMD_ARG (init , NULL ,
493- "Initialize csip_set_coordinator" ,
494- cmd_csip_set_coordinator_init , 1 , 1 ),
495487 SHELL_CMD_ARG (discover , NULL ,
496488 "Run discover for CSIS on peer device [member_index]" ,
497489 cmd_csip_set_coordinator_discover , 1 , 1 ),
0 commit comments