@@ -1547,121 +1547,106 @@ static void discover_next_instance(struct bt_conn *conn, uint8_t index)
15471547 }
15481548}
15491549
1550+ static void primary_discover_complete (struct bt_tbs_server_inst * server , struct bt_conn * conn )
1551+ {
1552+ if (IS_ENABLED (CONFIG_BT_TBS_CLIENT_GTBS )) {
1553+ server -> gtbs_found = server -> tbs_insts [GTBS_INDEX ].gtbs ;
1554+ BT_DBG ("Discover complete, found %u instances (GTBS%s found)" ,
1555+ server -> inst_cnt , server -> gtbs_found ? "" : " not" );
1556+ } else {
1557+ BT_DBG ("Discover complete, found %u instances" , server -> inst_cnt );
1558+ }
1559+
1560+ if (server -> inst_cnt != 0 ) {
1561+ discover_next_instance (conn , 0 );
1562+ } else if (IS_ENABLED (CONFIG_BT_TBS_CLIENT_GTBS ) && server -> gtbs_found ) {
1563+ discover_next_instance (conn , GTBS_INDEX );
1564+ } else {
1565+ server -> current_inst = NULL ;
1566+ if (tbs_client_cbs != NULL && tbs_client_cbs -> discover != NULL ) {
1567+ tbs_client_cbs -> discover (conn , 0 , 0 , false);
1568+ }
1569+ }
1570+ }
1571+
15501572/**
15511573 * @brief This will discover all characteristics on the server, retrieving the
15521574 * handles of the writeable characteristics and subscribing to all notify and
15531575 * indicate characteristics.
15541576 */
1555- static uint8_t primary_discover_func (struct bt_conn * conn ,
1556- const struct bt_gatt_attr * attr ,
1557- struct bt_gatt_discover_params * params )
1577+ static uint8_t primary_discover_tbs (struct bt_conn * conn , const struct bt_gatt_attr * attr ,
1578+ struct bt_gatt_discover_params * params )
15581579{
15591580 const uint8_t conn_index = bt_conn_index (conn );
15601581 struct bt_tbs_server_inst * srv_inst = & srv_insts [conn_index ];
1582+ const struct bt_gatt_service_val * prim_service ;
15611583
1562- /*
1563- * TODO: Since we know the ranges of each instance, we could do
1564- * discover of more than just prim_service->start and
1565- * prim_service->end_handle, so that we can detect multiple instances
1566- * faster
1567- */
1584+ if (attr == NULL ) {
1585+ primary_discover_complete (srv_inst , conn );
15681586
1569- if (attr == NULL || srv_inst -> inst_cnt == BT_TBS_INSTANCE_MAX_CNT ) {
1570- if (IS_ENABLED (CONFIG_BT_TBS_CLIENT_GTBS ) &&
1571- bt_uuid_cmp (params -> uuid , BT_UUID_GTBS ) == 0 ) {
1572- int err ;
1573- /* Didn't find GTBS; look for TBS */
1574- params -> uuid = tbs_uuid ;
1575- params -> start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE ;
1576-
1577- err = bt_gatt_discover (conn , params );
1578- if (err != 0 ) {
1579- BT_DBG ("Discover failed (err %d)" , err );
1580- srv_inst -> current_inst = NULL ;
1581- if (tbs_client_cbs != NULL &&
1582- tbs_client_cbs -> discover != NULL ) {
1583- tbs_client_cbs -> discover (conn , err , 0 ,
1584- false);
1585- }
1586- }
1587+ return BT_GATT_ITER_STOP ;
1588+ }
15871589
1588- return BT_GATT_ITER_STOP ;
1589- }
1590+ BT_DBG ("[ATTRIBUTE] handle 0x%04X" , attr -> handle );
15901591
1591- if (IS_ENABLED (CONFIG_BT_TBS_CLIENT_GTBS )) {
1592- srv_inst -> gtbs_found = srv_inst -> tbs_insts [GTBS_INDEX ].gtbs ;
1593- BT_DBG ("Discover complete, found %u instances "
1594- "(GTBS%s found)" ,
1595- srv_inst -> inst_cnt ,
1596- srv_inst -> gtbs_found ? "" : " not" );
1597- } else {
1598- BT_DBG ("Discover complete, found %u instances" ,
1599- srv_inst -> inst_cnt );
1600- }
1592+ prim_service = (struct bt_gatt_service_val * )attr -> user_data ;
16011593
1602- if (srv_inst -> inst_cnt != 0 ) {
1603- discover_next_instance (conn , 0 );
1604- } else if (IS_ENABLED (CONFIG_BT_TBS_CLIENT_GTBS ) &&
1605- srv_inst -> gtbs_found ) {
1606- discover_next_instance (conn , GTBS_INDEX );
1607- } else {
1608- srv_inst -> current_inst = NULL ;
1609- if (tbs_client_cbs != NULL &&
1610- tbs_client_cbs -> discover != NULL ) {
1611- tbs_client_cbs -> discover (conn , 0 ,
1612- srv_inst -> inst_cnt ,
1613- srv_inst -> gtbs_found );
1614- }
1615- }
1594+ srv_inst -> current_inst = & srv_inst -> tbs_insts [srv_inst -> inst_cnt ];
1595+ srv_inst -> current_inst -> index = srv_inst -> inst_cnt ;
1596+ srv_inst -> current_inst -> gtbs = false;
1597+ srv_inst -> current_inst -> start_handle = attr -> handle + 1 ;
1598+ srv_inst -> current_inst -> end_handle = prim_service -> end_handle ;
1599+
1600+ srv_inst -> inst_cnt ++ ;
1601+ if (srv_inst -> inst_cnt == CONFIG_BT_TBS_CLIENT_MAX_TBS_INSTANCES ) {
1602+ primary_discover_complete (srv_inst , conn );
16161603
16171604 return BT_GATT_ITER_STOP ;
16181605 }
16191606
1620- BT_DBG ("[ATTRIBUTE] handle 0x%04X" , attr -> handle );
1607+ return BT_GATT_ITER_CONTINUE ;
1608+ }
16211609
1622- if (params -> type == BT_GATT_DISCOVER_PRIMARY ) {
1610+ static uint8_t primary_discover_gtbs (struct bt_conn * conn , const struct bt_gatt_attr * attr ,
1611+ struct bt_gatt_discover_params * params )
1612+ {
1613+ const uint8_t conn_index = bt_conn_index (conn );
1614+ struct bt_tbs_server_inst * srv_inst = & srv_insts [conn_index ];
1615+ int err ;
1616+
1617+ if (attr != NULL ) {
16231618 const struct bt_gatt_service_val * prim_service ;
16241619
1620+ BT_DBG ("[ATTRIBUTE] handle 0x%04X" , attr -> handle );
1621+
16251622 prim_service = (struct bt_gatt_service_val * )attr -> user_data ;
1626- params -> start_handle = attr -> handle + 1 ;
16271623
1628- srv_inst -> current_inst = & srv_inst -> tbs_insts [srv_inst -> inst_cnt ];
1629- srv_inst -> current_inst -> index = srv_inst -> inst_cnt ;
1624+ /* GTBS is placed as the "last" instance */
1625+ srv_inst -> current_inst = & srv_inst -> tbs_insts [GTBS_INDEX ];
1626+ srv_inst -> current_inst -> index = GTBS_INDEX ;
1627+ srv_inst -> current_inst -> gtbs = true;
1628+ srv_inst -> current_inst -> start_handle = attr -> handle + 1 ;
1629+ srv_inst -> current_inst -> end_handle = prim_service -> end_handle ;
1630+ }
1631+
1632+ if (CONFIG_BT_TBS_CLIENT_MAX_TBS_INSTANCES == 0 ) {
1633+ primary_discover_complete (srv_inst , conn );
16301634
1631- if (IS_ENABLED (CONFIG_BT_TBS_CLIENT_GTBS ) &&
1632- bt_uuid_cmp (params -> uuid , BT_UUID_GTBS ) == 0 ) {
1633- int err ;
1634-
1635- /* GTBS is placed as the "last" instance */
1636- srv_inst -> current_inst = & srv_inst -> tbs_insts [GTBS_INDEX ];
1637- srv_inst -> current_inst -> index = GTBS_INDEX ;
1638- srv_inst -> current_inst -> gtbs = true;
1639- srv_inst -> current_inst -> start_handle = attr -> handle + 1 ;
1640- srv_inst -> current_inst -> end_handle = prim_service -> end_handle ;
1641-
1642- params -> uuid = tbs_uuid ;
1643- params -> start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE ;
1644-
1645- err = bt_gatt_discover (conn , params );
1646- if (err != 0 ) {
1647- BT_DBG ("Discover failed (err %d)" , err );
1648- srv_inst -> current_inst = NULL ;
1649- if (tbs_client_cbs != NULL &&
1650- tbs_client_cbs -> discover != NULL ) {
1651- tbs_client_cbs -> discover (conn , err , 0 ,
1652- false);
1653- }
1654- }
1635+ return BT_GATT_ITER_STOP ;
1636+ }
16551637
1656- return BT_GATT_ITER_STOP ;
1657- }
1638+ params -> uuid = tbs_uuid ;
1639+ params -> func = primary_discover_tbs ;
1640+ params -> start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE ;
16581641
1659- srv_inst -> current_inst -> start_handle = attr -> handle + 1 ;
1660- srv_inst -> current_inst -> end_handle = prim_service -> end_handle ;
1661- srv_inst -> inst_cnt ++ ;
1642+ err = bt_gatt_discover (conn , params );
1643+ if (err != 0 ) {
1644+ BT_DBG ("Discover failed (err %d)" , err );
1645+
1646+ primary_discover_complete (srv_inst , conn );
16621647 }
16631648
1664- return BT_GATT_ITER_CONTINUE ;
1649+ return BT_GATT_ITER_STOP ;
16651650}
16661651
16671652/****************************** PUBLIC API ******************************/
@@ -2327,10 +2312,11 @@ int bt_tbs_client_discover(struct bt_conn *conn, bool subscribe)
23272312 if (IS_ENABLED (CONFIG_BT_TBS_CLIENT_GTBS )) {
23282313 BT_DBG ("Discovering GTBS" );
23292314 srv_inst -> discover_params .uuid = gtbs_uuid ;
2315+ srv_inst -> discover_params .func = primary_discover_gtbs ;
23302316 } else {
23312317 srv_inst -> discover_params .uuid = tbs_uuid ;
2318+ srv_inst -> discover_params .func = primary_discover_tbs ;
23322319 }
2333- srv_inst -> discover_params .func = primary_discover_func ;
23342320 srv_inst -> discover_params .type = BT_GATT_DISCOVER_PRIMARY ;
23352321 srv_inst -> discover_params .start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE ;
23362322 srv_inst -> discover_params .end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE ;
0 commit comments