The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2003-2012 Broadcom Corporation |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 4 | * |
| 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 27 | #include <string.h> |
Myles Watson | f355ef5 | 2016-11-09 13:04:33 -0800 | [diff] [blame] | 28 | |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 29 | #include "bt_common.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 30 | #include "bta_gatts_int.h" |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 31 | #include "bta_sys.h" |
| 32 | #include "utl.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 33 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 34 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 35 | * |
| 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 Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 43 | uint8_t bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB* p_cb, uint8_t rcb_idx) { |
| 44 | uint8_t i; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 45 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 46 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 51 | } |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 52 | } |
| 53 | return BTA_GATTS_INVALID_APP; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 57 | * |
| 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 Pawlowski | ee9a11f | 2017-09-25 18:47:54 -0700 | [diff] [blame] | 65 | tBTA_GATTS_RCB* bta_gatts_find_app_rcb_by_app_if(tGATT_IF server_if) { |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 66 | uint8_t i; |
| 67 | tBTA_GATTS_RCB* p_reg; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 68 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 69 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 77 | * |
| 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 Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 82 | * Returns index of the control block, or BTA_GATTS_INVALID_APP if |
Myles Watson | 1baaae3 | 2016-11-09 14:25:23 -0800 | [diff] [blame] | 83 | * failed. |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 84 | * |
| 85 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 86 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 87 | uint8_t bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB* p_cb, |
Jakub Pawlowski | ee9a11f | 2017-09-25 18:47:54 -0700 | [diff] [blame] | 88 | tGATT_IF server_if) { |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 89 | uint8_t i; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 90 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 91 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 95 | } |
| 96 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 97 | * |
| 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 Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 105 | tBTA_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 Pawlowski | 2647452 | 2017-10-04 12:22:10 -0700 | [diff] [blame] | 108 | VLOG(1) << __func__ << ": service_id=" << +service_id; |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 109 | 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 Pawlowski | 2647452 | 2017-10-04 12:22:10 -0700 | [diff] [blame] | 111 | VLOG(1) << __func__ << ": found service cb index=" << +i; |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 112 | return &p_cb->srvc_cb[i]; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 113 | } |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 114 | } |
| 115 | return NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 116 | } |
| 117 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 118 | * |
| 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 Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 126 | tBTA_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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 129 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 130 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 144 | } |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 145 | } |
| 146 | return NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 147 | } |