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 53 /* Holds pending GATT operations */ 54 struct gatt_operation { 55 uint8_t type; 56 uint16_t handle; 57 GATT_READ_OP_CB read_cb; 58 void* read_cb_data; 59 GATT_WRITE_OP_CB write_cb; 60 void* write_cb_data; 61 GATT_CONFIGURE_MTU_OP_CB mtu_cb; 62 void* mtu_cb_data; 63 64 /* write-specific fields */ 65 tGATT_WRITE_TYPE write_type; 66 std::vector<uint8_t> value; 67 }; 68 69 private: 70 static void mark_as_not_executing(uint16_t conn_id); 71 static void gatt_execute_next_op(uint16_t conn_id); 72 static void gatt_read_op_finished(uint16_t conn_id, tGATT_STATUS status, 73 uint16_t handle, uint16_t len, 74 uint8_t* value, void* data); 75 static void gatt_write_op_finished(uint16_t conn_id, tGATT_STATUS status, 76 uint16_t handle, void* data); 77 static void gatt_configure_mtu_op_finished(uint16_t conn_id, 78 tGATT_STATUS status, void* data); 79 80 // maps connection id to operations waiting for execution 81 static std::unordered_map<uint16_t, std::list<gatt_operation>> gatt_op_queue; 82 // contain connection ids that currently execute operations 83 static std::unordered_set<uint16_t> gatt_op_queue_executing; 84 }; 85