1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <cstdint> 18 #include <list> 19 #include <unordered_map> 20 #include <unordered_set> 21 #include <vector> 22 23 #include "bta/include/bta_gatt_api.h" 24 25 /* BTA GATTC implementation does not allow for multiple commands queuing. So one 26 * client making calls to BTA_GATTC_ReadCharacteristic, BTA_GATTC_ReadCharDescr, 27 * BTA_GATTC_WriteCharValue, BTA_GATTC_WriteCharDescr must wait for the callacks 28 * before scheduling next operation. 29 * 30 * Methods below can be used as replacement to BTA_GATTC_* in BTA app. They do 31 * queue the commands if another command is currently being executed. 32 * 33 * If you decide to use those methods in your app, make sure to not mix it with 34 * existing BTA_GATTC_* API. 35 */ 36 class BtaGattQueue { 37 public: 38 static void Clean(uint16_t conn_id); 39 static void ReadCharacteristic(uint16_t conn_id, uint16_t handle, 40 GATT_READ_OP_CB cb, void* cb_data); 41 static void ReadDescriptor(uint16_t conn_id, uint16_t handle, 42 GATT_READ_OP_CB cb, void* cb_data); 43 static void WriteCharacteristic(uint16_t conn_id, uint16_t handle, 44 std::vector<uint8_t> value, 45 tGATT_WRITE_TYPE write_type, 46 GATT_WRITE_OP_CB cb, void* cb_data); 47 static void WriteDescriptor(uint16_t conn_id, uint16_t handle, 48 std::vector<uint8_t> value, 49 tGATT_WRITE_TYPE write_type, GATT_WRITE_OP_CB cb, 50 void* cb_data); 51 static void ConfigureMtu(uint16_t conn_id, uint16_t mtu); 52 static void ReadMultiCharacteristic(uint16_t conn_id, 53 tBTA_GATTC_MULTI& p_read_multi, 54 bool variable_len, 55 GATT_READ_MULTI_OP_CB cb, void* cb_data); 56 57 /* Holds pending GATT operations */ 58 struct gatt_operation { 59 uint8_t type; 60 uint16_t handle; 61 tBTA_GATTC_MULTI handles; 62 bool variable_len; /* whether read multi variable len is used*/ 63 GATT_READ_OP_CB read_cb; 64 GATT_READ_MULTI_OP_CB read_multi_cb; 65 void* read_cb_data; 66 GATT_WRITE_OP_CB write_cb; 67 void* write_cb_data; 68 GATT_CONFIGURE_MTU_OP_CB mtu_cb; 69 void* mtu_cb_data; 70 71 /* write-specific fields */ 72 tGATT_WRITE_TYPE write_type; 73 std::vector<uint8_t> value; 74 }; 75 76 private: 77 static void mark_as_not_executing(uint16_t conn_id); 78 static void gatt_execute_next_op(uint16_t conn_id); 79 static void gatt_read_op_finished(uint16_t conn_id, tGATT_STATUS status, 80 uint16_t handle, uint16_t len, 81 uint8_t* value, void* data); 82 static void gatt_write_op_finished(uint16_t conn_id, tGATT_STATUS status, 83 uint16_t handle, uint16_t len, 84 const uint8_t* value, void* data); 85 static void gatt_configure_mtu_op_finished(uint16_t conn_id, 86 tGATT_STATUS status, void* data); 87 static void gatt_read_multi_op_finished(uint16_t conn_id, tGATT_STATUS status, 88 tBTA_GATTC_MULTI& handle, 89 uint16_t len, uint8_t* value, 90 void* data); 91 // maps connection id to operations waiting for execution 92 static std::unordered_map<uint16_t, std::list<gatt_operation>> gatt_op_queue; 93 // contain connection ids that currently execute operations 94 static std::unordered_set<uint16_t> gatt_op_queue_executing; 95 }; 96