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(uint8_t role,uint16_t role_handle,uint8_t UGT_feature,uint16_t UGT_feature_handle)39 void GmapClient::AddFromStorage(uint8_t role, uint16_t role_handle, uint8_t UGT_feature,
40 uint16_t UGT_feature_handle) {
41 role_ = role;
42 role_handle_ = role_handle;
43 UGT_feature_ = UGT_feature;
44 UGT_feature_handle_ = UGT_feature_handle;
45 }
46
DebugDump(std::stringstream & stream)47 void GmapClient::DebugDump(std::stringstream &stream) {
48 if (!IsGmapClientEnabled()) {
49 stream << "GmapClient not enabled\n";
50 return;
51 }
52 stream << "GmapClient device: " << addr_ << ", Role: " << role_ << ", ";
53 stream << "UGT Feature: " << UGT_feature_ << "\n";
54 }
55
IsGmapClientEnabled()56 bool GmapClient::IsGmapClientEnabled() {
57 bool flag = com::android::bluetooth::flags::leaudio_gmap_client();
58 bool system_prop = osi_property_get_bool("bluetooth.profile.gmap.enabled", false);
59
60 bool result = flag && system_prop && is_offloader_support_gmap_;
61 log::info("GmapClientEnabled={}, flag={}, system_prop={}, offloader_support={}", result, flag,
62 system_prop, GmapClient::is_offloader_support_gmap_);
63 return result;
64 }
65
UpdateGmapOffloaderSupport(bool value)66 void GmapClient::UpdateGmapOffloaderSupport(bool value) {
67 GmapClient::is_offloader_support_gmap_ = value;
68 }
69
parseAndSaveGmapRole(uint16_t len,const uint8_t * value)70 bool GmapClient::parseAndSaveGmapRole(uint16_t len, const uint8_t *value) {
71 if (len != GmapClient::kGmapRoleLen) {
72 log::error("device: {}, Wrong len of GMAP Role characteristic", addr_);
73 return false;
74 }
75
76 STREAM_TO_UINT8(role_, value);
77 log::info("GMAP device: {}, Role: {}", addr_, role_.to_string());
78 return true;
79 }
80
parseAndSaveUGTFeature(uint16_t len,const uint8_t * value)81 bool GmapClient::parseAndSaveUGTFeature(uint16_t len, const uint8_t *value) {
82 if (len != kGmapUGTFeatureLen) {
83 log::error("device: {}, Wrong len of GMAP UGT Feature characteristic", addr_);
84 return false;
85 }
86 STREAM_TO_UINT8(UGT_feature_, value);
87 log::info("GMAP device: {}, Feature: {}", addr_, UGT_feature_.to_string());
88 return true;
89 }
90
getRole() const91 std::bitset<8> GmapClient::getRole() const { return role_; }
92
getRoleHandle() const93 uint16_t GmapClient::getRoleHandle() const { return role_handle_; }
94
setRoleHandle(uint16_t handle)95 void GmapClient::setRoleHandle(uint16_t handle) { role_handle_ = handle; }
96
getUGTFeature() const97 std::bitset<8> GmapClient::getUGTFeature() const { return UGT_feature_; }
98
getUGTFeatureHandle() const99 uint16_t GmapClient::getUGTFeatureHandle() const { return UGT_feature_handle_; }
100
setUGTFeatureHandle(uint16_t handle)101 void GmapClient::setUGTFeatureHandle(uint16_t handle) { UGT_feature_handle_ = handle; }
102