blob: 173d4f3058d1cdabed986504d1eb396fdb056381 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2003-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 * This file contains the GATT client utility function.
22 *
23 ******************************************************************************/
24
25#include "bt_target.h"
26
The Android Open Source Project5738f832012-12-12 16:00:35 -080027#include <string.h>
Myles Watsonf355ef52016-11-09 13:04:33 -080028
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070029#include "bt_common.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include "bta_gatts_int.h"
Myles Watsoncd1fd072016-11-09 13:17:43 -080031#include "bta_sys.h"
32#include "utl.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080033
The Android Open Source Project5738f832012-12-12 16:00:35 -080034/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080035 *
36 * Function bta_gatts_alloc_srvc_cb
37 *
38 * Description allocate a service control block.
39 *
40 * Returns pointer to the control block, or otherwise NULL when failed.
41 *
42 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -080043uint8_t bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB* p_cb, uint8_t rcb_idx) {
44 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -080045
Myles Watsoncd1fd072016-11-09 13:17:43 -080046 for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i++) {
47 if (!p_cb->srvc_cb[i].in_use) {
48 p_cb->srvc_cb[i].in_use = true;
49 p_cb->srvc_cb[i].rcb_idx = rcb_idx;
50 return i;
The Android Open Source Project5738f832012-12-12 16:00:35 -080051 }
Myles Watsoncd1fd072016-11-09 13:17:43 -080052 }
53 return BTA_GATTS_INVALID_APP;
The Android Open Source Project5738f832012-12-12 16:00:35 -080054}
55
56/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080057 *
58 * Function bta_gatts_find_app_rcb_by_app_if
59 *
60 * Description find the index of the application control block by app ID.
61 *
62 * Returns pointer to the control block if success, otherwise NULL
63 *
64 ******************************************************************************/
Jakub Pawlowskiee9a11f2017-09-25 18:47:54 -070065tBTA_GATTS_RCB* bta_gatts_find_app_rcb_by_app_if(tGATT_IF server_if) {
Myles Watsoncd1fd072016-11-09 13:17:43 -080066 uint8_t i;
67 tBTA_GATTS_RCB* p_reg;
The Android Open Source Project5738f832012-12-12 16:00:35 -080068
Myles Watsoncd1fd072016-11-09 13:17:43 -080069 for (i = 0, p_reg = bta_gatts_cb.rcb; i < BTA_GATTS_MAX_APP_NUM;
70 i++, p_reg++) {
71 if (p_reg->in_use && p_reg->gatt_if == server_if) return p_reg;
72 }
73 return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -080074}
75
76/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080077 *
78 * Function bta_gatts_find_app_rcb_idx_by_app_if
79 *
80 * Description find the index of the application control block by app ID.
81 *
Myles Watsoncd1fd072016-11-09 13:17:43 -080082 * Returns index of the control block, or BTA_GATTS_INVALID_APP if
Myles Watson1baaae32016-11-09 14:25:23 -080083 * failed.
Myles Watson8af480e2016-11-09 10:40:23 -080084 *
85 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080086
Myles Watsoncd1fd072016-11-09 13:17:43 -080087uint8_t bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB* p_cb,
Jakub Pawlowskiee9a11f2017-09-25 18:47:54 -070088 tGATT_IF server_if) {
Myles Watsoncd1fd072016-11-09 13:17:43 -080089 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -080090
Myles Watsoncd1fd072016-11-09 13:17:43 -080091 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
92 if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == server_if) return i;
93 }
94 return BTA_GATTS_INVALID_APP;
The Android Open Source Project5738f832012-12-12 16:00:35 -080095}
96/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080097 *
98 * Function bta_gatts_find_srvc_cb_by_srvc_id
99 *
100 * Description find the service control block by service ID.
101 *
102 * Returns pointer to the rcb.
103 *
104 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800105tBTA_GATTS_SRVC_CB* bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB* p_cb,
106 uint16_t service_id) {
107 uint8_t i;
Jakub Pawlowski26474522017-10-04 12:22:10 -0700108 VLOG(1) << __func__ << ": service_id=" << +service_id;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800109 for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i++) {
110 if (p_cb->srvc_cb[i].in_use && p_cb->srvc_cb[i].service_id == service_id) {
Jakub Pawlowski26474522017-10-04 12:22:10 -0700111 VLOG(1) << __func__ << ": found service cb index=" << +i;
Myles Watsoncd1fd072016-11-09 13:17:43 -0800112 return &p_cb->srvc_cb[i];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800113 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800114 }
115 return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800116}
117/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800118 *
119 * Function bta_gatts_find_srvc_cb_by_attr_id
120 *
121 * Description find the service control block by attribute ID.
122 *
123 * Returns pointer to the rcb.
124 *
125 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800126tBTA_GATTS_SRVC_CB* bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB* p_cb,
127 uint16_t attr_id) {
128 uint8_t i;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129
Myles Watsoncd1fd072016-11-09 13:17:43 -0800130 for (i = 0; i < (BTA_GATTS_MAX_SRVC_NUM); i++) {
131 if (/* middle service */
132 (i < (BTA_GATTS_MAX_SRVC_NUM - 1) && p_cb->srvc_cb[i].in_use &&
133 p_cb->srvc_cb[i + 1].in_use &&
134 attr_id >= p_cb->srvc_cb[i].service_id &&
135 attr_id < p_cb->srvc_cb[i + 1].service_id) ||
136 /* last active service */
137 (i < (BTA_GATTS_MAX_SRVC_NUM - 1) && p_cb->srvc_cb[i].in_use &&
138 !p_cb->srvc_cb[i + 1].in_use &&
139 attr_id >= p_cb->srvc_cb[i].service_id) ||
140 /* last service incb */
141 (i == (BTA_GATTS_MAX_SRVC_NUM - 1) &&
142 attr_id >= p_cb->srvc_cb[i].service_id)) {
143 return &p_cb->srvc_cb[i];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800144 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800145 }
146 return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800147}