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 #include "bta/le_audio/gmap_client.h"
18 
19 #include <base/functional/bind.h>
20 #include <base/functional/callback.h>
21 #include <base/strings/string_number_conversions.h>
22 #include <bluetooth/log.h>
23 #include <com_android_bluetooth_flags.h>
24 #include <stdio.h>
25 
26 #include <bitset>
27 #include <cstdint>
28 #include <sstream>
29 
30 #include "bta_gatt_queue.h"
31 #include "osi/include/properties.h"
32 #include "stack/include/bt_types.h"
33 #include "types/raw_address.h"
34 
35 using namespace bluetooth;
36 using bluetooth::le_audio::GmapClient;
37 bool GmapClient::is_offloader_support_gmap_ = false;
38 
AddFromStorage(const RawAddress & addr,const uint8_t role,const uint16_t role_handle,const uint8_t UGT_feature,const uint16_t UGT_feature_handle)39 void GmapClient::AddFromStorage(const RawAddress &addr, const uint8_t role,
40                                 const uint16_t role_handle, const uint8_t UGT_feature,
41                                 const uint16_t UGT_feature_handle) {
42   addr_ = addr;
43   role_ = role;
44   role_handle_ = role_handle;
45   UGT_feature_ = UGT_feature;
46   UGT_feature_handle_ = UGT_feature_handle;
47 }
48 
DebugDump(std::stringstream & stream)49 void GmapClient::DebugDump(std::stringstream &stream) {
50   if (!IsGmapClientEnabled()) {
51     stream << "GmapClient not enabled\n";
52     return;
53   }
54   stream << "GmapClient device: " << addr_ << ", Role: " << role_ << ", ";
55   stream << "UGT Feature: " << UGT_feature_ << "\n";
56 }
57 
IsGmapClientEnabled()58 bool GmapClient::IsGmapClientEnabled() {
59   bool flag = com::android::bluetooth::flags::leaudio_gmap_client();
60   bool system_prop = osi_property_get_bool("bluetooth.profile.gmap.enabled", false);
61 
62   bool result = flag && system_prop && is_offloader_support_gmap_;
63   log::info("GmapClientEnabled={}, flag={}, system_prop={}, offloader_support={}", result,
64             system_prop, flag, GmapClient::is_offloader_support_gmap_);
65   return result;
66 }
67 
UpdateGmapOffloaderSupport(bool value)68 void GmapClient::UpdateGmapOffloaderSupport(bool value) {
69   GmapClient::is_offloader_support_gmap_ = value;
70 }
71 
parseAndSaveGmapRole(uint16_t len,const uint8_t * value)72 bool GmapClient::parseAndSaveGmapRole(uint16_t len, const uint8_t *value) {
73   if (len != GmapClient::kGmapRoleLen) {
74     log::error("device: {}, Wrong len of GMAP Role characteristic", addr_);
75     return false;
76   }
77 
78   STREAM_TO_UINT8(role_, value);
79   log::info("GMAP device: {}, Role: {}", addr_, role_.to_string());
80   return true;
81 }
82 
parseAndSaveUGTFeature(uint16_t len,const uint8_t * value)83 bool GmapClient::parseAndSaveUGTFeature(uint16_t len, const uint8_t *value) {
84   if (len != kGmapUGTFeatureLen) {
85     log::error("device: {}, Wrong len of GMAP UGT Feature characteristic", addr_);
86     return false;
87   }
88   STREAM_TO_UINT8(UGT_feature_, value);
89   log::info("GMAP device: {}, Feature: {}", addr_, UGT_feature_.to_string());
90   return true;
91 }
92 
getRole()93 std::bitset<8> GmapClient::getRole() { return role_; }
94 
getRoleHandle()95 uint16_t GmapClient::getRoleHandle() { return role_handle_; }
96 
setRoleHandle(uint16_t handle)97 void GmapClient::setRoleHandle(uint16_t handle) { role_handle_ = handle; }
98 
getUGTFeature()99 std::bitset<8> GmapClient::getUGTFeature() { return UGT_feature_; }
100 
getUGTFeatureHandle()101 uint16_t GmapClient::getUGTFeatureHandle() { return UGT_feature_handle_; }
102 
setUGTFeatureHandle(uint16_t handle)103 void GmapClient::setUGTFeatureHandle(uint16_t handle) { UGT_feature_handle_ = handle; }
104