1 /*
2 * Copyright 2023 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/dm/bta_dm_gatt_client.h"
18
19 #include <base/functional/bind.h>
20 #include <base/functional/callback.h>
21
22 #include <cstdint>
23 #include <string>
24 #include <vector>
25
26 #include "bta/include/bta_gatt_api.h"
27 #include "common/strings.h"
28 #include "main/shim/dumpsys.h"
29 #include "stack/btm/btm_int_types.h"
30 #include "types/bluetooth/uuid.h"
31 #include "types/raw_address.h"
32
33 namespace {
34 TimestampedStringCircularBuffer gatt_history_{50};
35 constexpr char kTimeFormatString[] = "%Y-%m-%d %H:%M:%S";
36
37 constexpr unsigned MillisPerSecond = 1000;
EpochMillisToString(uint64_t time_ms)38 std::string EpochMillisToString(uint64_t time_ms) {
39 time_t time_sec = time_ms / MillisPerSecond;
40 struct tm tm;
41 localtime_r(&time_sec, &tm);
42 std::string s = bluetooth::common::StringFormatTime(kTimeFormatString, tm);
43 return std::format("{}.{:03}", s, time_ms % MillisPerSecond);
44 }
45 } // namespace
46
47 gatt_interface_t default_gatt_interface = {
48 .BTA_GATTC_CancelOpen =
__anona56446520202() 49 [](tGATT_IF client_if, const RawAddress& remote_bda, bool is_direct) {
50 gatt_history_.Push(std::format("{:<32s} bd_addr:{} client_if:{} is_direct:{:c}",
51 "GATTC_CancelOpen", remote_bda, client_if,
52 is_direct ? 'T' : 'F'));
53 BTA_GATTC_CancelOpen(client_if, remote_bda, is_direct);
54 },
55 .BTA_GATTC_Refresh =
__anona56446520302() 56 [](const RawAddress& remote_bda) {
57 gatt_history_.Push(
58 std::format("{:<32s} bd_addr:{}", "GATTC_Refresh", remote_bda));
59 BTA_GATTC_Refresh(remote_bda);
60 },
61 .BTA_GATTC_GetGattDb =
62 [](tCONN_ID conn_id, uint16_t start_handle, uint16_t end_handle,
__anona56446520402() 63 btgatt_db_element_t** db, int* count) {
64 gatt_history_.Push(std::format("{:<32s} conn_id:{} start_handle:{} end:handle:{}",
65 "GATTC_GetGattDb", conn_id, start_handle,
66 end_handle));
67 BTA_GATTC_GetGattDb(conn_id, start_handle, end_handle, db, count);
68 },
69 .BTA_GATTC_AppRegister =
70 [](const std::string& name, tBTA_GATTC_CBACK* p_client_cb,
__anona56446520502() 71 BtaAppRegisterCallback cb, bool eatt_support) {
72 gatt_history_.Push(std::format("{:<32s} eatt_support:{:c}", "GATTC_AppRegister",
73 eatt_support ? 'T' : 'F'));
74 BTA_GATTC_AppRegister(name, p_client_cb, cb, eatt_support);
75 },
76 .BTA_GATTC_Close =
__anona56446520602() 77 [](tCONN_ID conn_id) {
78 gatt_history_.Push(std::format("{:<32s} conn_id:{}", "GATTC_Close", conn_id));
79 BTA_GATTC_Close(conn_id);
80 },
81 .BTA_GATTC_ServiceSearchRequest =
__anona56446520702() 82 [](tCONN_ID conn_id, const bluetooth::Uuid* p_srvc_uuid) {
83 gatt_history_.Push(
84 std::format("{:<32s} conn_id:{}", "GATTC_ServiceSearchRequest", conn_id));
85 if (p_srvc_uuid) {
86 BTA_GATTC_ServiceSearchRequest(conn_id, *p_srvc_uuid);
87 } else {
88 BTA_GATTC_ServiceSearchAllRequest(conn_id);
89 }
90 },
91 .BTA_GATTC_Open =
92 [](tGATT_IF client_if, const RawAddress& remote_bda,
__anona56446520802() 93 tBTM_BLE_CONN_TYPE connection_type, bool opportunistic, uint16_t preferred_mtu) {
94 gatt_history_.Push(std::format(
95 "{:<32s} bd_addr:{} client_if:{} type:0x{:x} opportunistic:{:c}",
96 "GATTC_Open", remote_bda, client_if, connection_type,
97 opportunistic ? 'T' : 'F'));
98 BTA_GATTC_Open(client_if, remote_bda, BLE_ADDR_PUBLIC, connection_type,
99 BT_TRANSPORT_LE, opportunistic, LE_PHY_1M, preferred_mtu);
100 },
101 };
102
103 gatt_interface_t* gatt_interface = &default_gatt_interface;
104
get_gatt_interface()105 gatt_interface_t& get_gatt_interface() { return *gatt_interface; }
106
gatt_history_callback(const std::string & entry)107 void gatt_history_callback(const std::string& entry) { gatt_history_.Push(entry); }
108
109 #define DUMPSYS_TAG "shim::legacy::bta::dm"
DumpsysBtaDmGattClient(int fd)110 void DumpsysBtaDmGattClient(int fd) {
111 auto gatt_history = gatt_history_.Pull();
112 LOG_DUMPSYS(fd, " last %zu gatt history entries", gatt_history.size());
113 for (const auto& it : gatt_history) {
114 LOG_DUMPSYS(fd, " %s %s", EpochMillisToString(it.timestamp).c_str(), it.entry.c_str());
115 }
116 }
117 #undef DUMPSYS_TAG
118
119 namespace bluetooth::testing {
120
PullCopyOfGattHistory()121 std::vector<bluetooth::common::TimestampedEntry<std::string>> PullCopyOfGattHistory() {
122 return gatt_history_.Pull();
123 }
124
125 } // namespace bluetooth::testing
126