@@ -621,7 +621,7 @@ static uint8_t aics_discover_func(struct bt_conn *conn, const struct bt_gatt_att
621621 return BT_GATT_ITER_CONTINUE ;
622622}
623623
624- static void aics_client_reset (struct bt_aics * inst , struct bt_conn * conn )
624+ static void aics_client_reset (struct bt_aics * inst )
625625{
626626 inst -> cli .desc_writable = 0 ;
627627 inst -> cli .change_counter = 0 ;
@@ -635,12 +635,36 @@ static void aics_client_reset(struct bt_aics *inst, struct bt_conn *conn)
635635 inst -> cli .control_handle = 0 ;
636636 inst -> cli .desc_handle = 0 ;
637637
638- /* It's okay if these fail */
639- (void )bt_gatt_unsubscribe (conn , & inst -> cli .state_sub_params );
640- (void )bt_gatt_unsubscribe (conn , & inst -> cli .status_sub_params );
641- (void )bt_gatt_unsubscribe (conn , & inst -> cli .desc_sub_params );
638+ if (inst -> cli .conn != NULL ) {
639+ struct bt_conn * conn = inst -> cli .conn ;
640+
641+ /* It's okay if these fail. In case of disconnect, we can't
642+ * unsubscribe and they will just fail.
643+ * In case that we reset due to another call of the discover
644+ * function, we will unsubscribe (regardless of bonding state)
645+ * to accommodate the new discovery values.
646+ */
647+ (void )bt_gatt_unsubscribe (conn , & inst -> cli .state_sub_params );
648+ (void )bt_gatt_unsubscribe (conn , & inst -> cli .status_sub_params );
649+ (void )bt_gatt_unsubscribe (conn , & inst -> cli .desc_sub_params );
650+
651+ bt_conn_unref (conn );
652+ inst -> cli .conn = NULL ;
653+ }
654+ }
655+
656+ static void disconnected (struct bt_conn * conn , uint8_t reason )
657+ {
658+ for (size_t i = 0 ; i < ARRAY_SIZE (aics_insts ); i ++ ) {
659+ if (aics_insts [i ].cli .conn == conn ) {
660+ aics_client_reset (& aics_insts [i ]);
661+ }
662+ }
642663}
643664
665+ BT_CONN_CB_DEFINE (conn_callbacks ) = {
666+ .disconnected = disconnected ,
667+ };
644668
645669int bt_aics_discover (struct bt_conn * conn , struct bt_aics * inst ,
646670 const struct bt_aics_discover_param * param )
@@ -669,11 +693,11 @@ int bt_aics_discover(struct bt_conn *conn, struct bt_aics *inst,
669693 return - EBUSY ;
670694 }
671695
672- aics_client_reset (inst , conn );
696+ aics_client_reset (inst );
673697
674698 (void )memset (& inst -> cli .discover_params , 0 , sizeof (inst -> cli .discover_params ));
675699
676- inst -> cli .conn = conn ;
700+ inst -> cli .conn = bt_conn_ref ( conn ) ;
677701 inst -> cli .discover_params .start_handle = param -> start_handle ;
678702 inst -> cli .discover_params .end_handle = param -> end_handle ;
679703 inst -> cli .discover_params .type = BT_GATT_DISCOVER_CHARACTERISTIC ;
0 commit comments