• 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 <vector>
18 
19 #include <list>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include "bta_gatt_api.h"
23 
24 /* BTA GATTC implementation does not allow for multiple commands queuing. So one
25  * client making calls to BTA_GATTC_ReadCharacteristic, BTA_GATTC_ReadCharDescr,
26  * BTA_GATTC_WriteCharValue, BTA_GATTC_WriteCharDescr must wait for the callacks
27  * before scheduling next operation.
28  *
29  * Methods below can be used as replacement to BTA_GATTC_* in BTA app. They do
30  * queue the commands if another command is currently being executed.
31  *
32  * If you decide to use those methods in your app, make sure to not mix it with
33  * existing BTA_GATTC_* API.
34  */
35 class BtaGattQueue {
36  public:
37   static void Clean(uint16_t conn_id);
38   static void ReadCharacteristic(uint16_t conn_id, uint16_t handle,
39                                  GATT_READ_OP_CB cb, void* cb_data);
40   static void ReadDescriptor(uint16_t conn_id, uint16_t handle,
41                              GATT_READ_OP_CB cb, void* cb_data);
42   static void WriteCharacteristic(uint16_t conn_id, uint16_t handle,
43                                   std::vector<uint8_t> value,
44                                   tGATT_WRITE_TYPE write_type,
45                                   GATT_WRITE_OP_CB cb, void* cb_data);
46   static void WriteDescriptor(uint16_t conn_id, uint16_t handle,
47                               std::vector<uint8_t> value,
48                               tGATT_WRITE_TYPE write_type, GATT_WRITE_OP_CB cb,
49                               void* cb_data);
50 
51   /* Holds pending GATT operations */
52   struct gatt_operation {
53     uint8_t type;
54     uint16_t handle;
55     GATT_READ_OP_CB read_cb;
56     void* read_cb_data;
57     GATT_WRITE_OP_CB write_cb;
58     void* write_cb_data;
59 
60     /* write-specific fields */
61     tGATT_WRITE_TYPE write_type;
62     std::vector<uint8_t> value;
63   };
64 
65  private:
66   static void mark_as_not_executing(uint16_t conn_id);
67   static void gatt_execute_next_op(uint16_t conn_id);
68   static void gatt_read_op_finished(uint16_t conn_id, tGATT_STATUS status,
69                                     uint16_t handle, uint16_t len,
70                                     uint8_t* value, void* data);
71   static void gatt_write_op_finished(uint16_t conn_id, tGATT_STATUS status,
72                                      uint16_t handle, void* data);
73 
74   // maps connection id to operations waiting for execution
75   static std::unordered_map<uint16_t, std::list<gatt_operation>> gatt_op_queue;
76   // contain connection ids that currently execute operations
77   static std::unordered_set<uint16_t> gatt_op_queue_executing;
78 };