• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 queues "Read Multiple Variable Length Characteristic Values".
48    * Remote must support this method when it supports EATT.
49    * Returns true when remote supports EATT and operation was successfully queued.
50    * Returns false when remote doesn't support EATT and operation was not scheduled.
51    */
52   static bool ReadMultiCharacteristic(tCONN_ID conn_id, tBTA_GATTC_MULTI& p_read_multi,
53                                       GATT_READ_MULTI_OP_CB cb, void* cb_data);
54 
55   /* Holds pending GATT operations */
56   struct gatt_operation {
57     uint8_t type;
58     uint16_t handle;
59     tBTA_GATTC_MULTI handles;
60     GATT_READ_OP_CB read_cb;
61     GATT_READ_MULTI_OP_CB read_multi_cb;
62     void* read_cb_data;
63     GATT_WRITE_OP_CB write_cb;
64     void* write_cb_data;
65     GATT_CONFIGURE_MTU_OP_CB mtu_cb;
66     void* mtu_cb_data;
67 
68     /* write-specific fields */
69     tGATT_WRITE_TYPE write_type;
70     std::vector<uint8_t> value;
71   };
72 
73 private:
74   static void mark_as_not_executing(tCONN_ID conn_id);
75   static void gatt_execute_next_op(tCONN_ID conn_id);
76   static void gatt_read_op_finished(tCONN_ID conn_id, tGATT_STATUS status, uint16_t handle,
77                                     uint16_t len, uint8_t* value, void* data);
78   static void gatt_write_op_finished(tCONN_ID conn_id, tGATT_STATUS status, uint16_t handle,
79                                      uint16_t len, const uint8_t* value, void* data);
80   static void gatt_configure_mtu_op_finished(tCONN_ID conn_id, tGATT_STATUS status, void* data);
81   static void gatt_read_multi_op_finished(tCONN_ID conn_id, tGATT_STATUS status,
82                                           tBTA_GATTC_MULTI& handle, uint16_t len, uint8_t* value,
83                                           void* data);
84   // maps connection id to operations waiting for execution
85   static std::unordered_map<tCONN_ID, std::list<gatt_operation>> gatt_op_queue;
86   // contain connection ids that currently execute operations
87   static std::unordered_set<tCONN_ID> gatt_op_queue_executing;
88 };
89