bta_gatt_queue.h 3.1 KB

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