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(tCONN_ID conn_id);
39   static void ReadCharacteristic(tCONN_ID conn_id, uint16_t handle, GATT_READ_OP_CB cb,
40                                  void* cb_data);
41   static void ReadDescriptor(tCONN_ID conn_id, uint16_t handle, GATT_READ_OP_CB cb, void* cb_data);
42   static void WriteCharacteristic(tCONN_ID conn_id, uint16_t handle, std::vector<uint8_t> value,
43                                   tGATT_WRITE_TYPE write_type, GATT_WRITE_OP_CB cb, void* cb_data);
44   static void WriteDescriptor(tCONN_ID conn_id, uint16_t handle, std::vector<uint8_t> value,
45                               tGATT_WRITE_TYPE write_type, GATT_WRITE_OP_CB cb, void* cb_data);
46   static void ConfigureMtu(tCONN_ID conn_id, uint16_t mtu);
47   /* This method uses "Read Multiple Variable Length Characteristic Values".
48    * If EATT is not enabled on remote, it would send multiple regular Characteristic Reads, and
49    * concatenate their values into Length Value Tuple List
50    */
51   static void ReadMultiCharacteristic(tCONN_ID conn_id, tBTA_GATTC_MULTI& p_read_multi,
52                                       GATT_READ_MULTI_OP_CB cb, void* cb_data);
53 
54   /* Holds pending GATT operations */
55   struct gatt_operation {
56     uint8_t type;
57     uint16_t handle;
58     tBTA_GATTC_MULTI handles;
59     GATT_READ_OP_CB read_cb;
60     GATT_READ_MULTI_OP_CB read_multi_cb;
61     void* read_cb_data;
62     GATT_WRITE_OP_CB write_cb;
63     void* write_cb_data;
64     GATT_CONFIGURE_MTU_OP_CB mtu_cb;
65     void* mtu_cb_data;
66 
67     /* write-specific fields */
68     tGATT_WRITE_TYPE write_type;
69     std::vector<uint8_t> value;
70   };
71 
72 private:
73   static void mark_as_not_executing(tCONN_ID conn_id);
74   static void gatt_execute_next_op(tCONN_ID conn_id);
75   static void gatt_read_op_finished(tCONN_ID conn_id, tGATT_STATUS status, uint16_t handle,
76                                     uint16_t len, uint8_t* value, void* data);
77   static void gatt_write_op_finished(tCONN_ID conn_id, tGATT_STATUS status, uint16_t handle,
78                                      uint16_t len, const uint8_t* value, void* data);
79   static void gatt_configure_mtu_op_finished(tCONN_ID conn_id, tGATT_STATUS status, void* data);
80   static void gatt_read_multi_op_finished(tCONN_ID conn_id, tGATT_STATUS status,
81                                           tBTA_GATTC_MULTI& handle, uint16_t len, uint8_t* value,
82                                           void* data);
83   static void gatt_read_multi_op_simulate(tCONN_ID conn_id, tGATT_STATUS status, uint16_t handle,
84                                           uint16_t len, uint8_t* value, void* data_read);
85   // maps connection id to operations waiting for execution
86   static std::unordered_map<tCONN_ID, std::list<gatt_operation>> gatt_op_queue;
87   // contain connection ids that currently execute operations
88   static std::unordered_set<tCONN_ID> gatt_op_queue_executing;
89 };
90