1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <bluetooth/log.h>
20 #include <bta_gatt_api.h>
21 #include <hardware/bluetooth.h>
22 
23 #include <bitset>
24 #include <unordered_map>
25 
26 namespace bluetooth::le_audio {
27 struct GmapCharacteristic {
28   bluetooth::Uuid uuid_;
29   uint16_t attribute_handle_;
30 };
31 
32 /**
33  * As there is only 1 Gaming Audio Server, so here uses static function
34  */
35 class GmapServer {
36 public:
37   static void DebugDump(int fd);
38 
39   /**
40    * UGG, UGT, and BGR devices supporting GMAP shall implement the GATT Server role
41    * and instantiate only one GMA Server.
42    * BGS-only devices are not required by GMAP to implement the GATT Server role.
43    * @return true if GMAP Server is enabled
44    */
45   static bool IsGmapServerEnabled();
46 
47   static void UpdateGmapOffloaderSupport(bool value);
48 
49   static void Initialize(std::bitset<8> role, std::bitset<8> UGG_feature);
50 
51   static void Initialize(std::bitset<8> UGG_feature);
52 
53   static std::bitset<8> GetRole();
54 
55   static uint16_t GetRoleHandle();
56 
57   static std::bitset<8> GetUGGFeature();
58 
59   static uint16_t GetUGGFeatureHandle();
60 
61   /**
62    * This function is used only for testing
63    */
64   static void GattsCallback(tBTA_GATTS_EVT event, tBTA_GATTS *p_data);
65 
66   /**
67    * This function is used only for testing
68    * @return an unordered_map whose key is attribute_handle_ and value is GmapCharacteristic
69    */
70   static std::unordered_map<uint16_t, GmapCharacteristic> &GetCharacteristics();
71 
72   constexpr static uint16_t kGmapRoleLen = 1;
73   constexpr static uint16_t kGmapUGGFeatureLen = 1;
74 
75 private:
76   static void OnGattConnect(tBTA_GATTS *p_data);
77 
78   static void OnGattDisconnect(tBTA_GATTS *p_data);
79 
80   static void OnGattServerRegister(tBTA_GATTS *p_data);
81 
82   static void OnServiceAdded(tGATT_STATUS status, int server_if,
83                              std::vector<btgatt_db_element_t> services);
84 
85   static void OnReadCharacteristic(tBTA_GATTS *p_data);
86 
87   static bool is_offloader_support_gmap_;
88   static uint16_t server_if_;
89   static std::unordered_map<uint16_t, GmapCharacteristic> characteristics_;
90   static std::bitset<8> role_;
91   static std::bitset<8> UGG_feature_;
92 };
93 }  // namespace bluetooth::le_audio
94