1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "metrics_state.h"
#include <frameworks/proto_logging/stats/enums/bluetooth/hci/enums.pb.h>
#include <frameworks/proto_logging/stats/enums/bluetooth/le/enums.pb.h>
#include <chrono>
#include <climits>
#include <memory>
#include <unordered_map>
#include <utility>
#include "common/strings.h"
#include "hci/address.h"
#include "metrics/utils.h"
#include "os/log.h"
#include "os/metrics.h"
namespace bluetooth {
namespace metrics {
using android::bluetooth::le::LeConnectionOriginType;
using android::bluetooth::le::LeConnectionState;
using android::bluetooth::le::LeConnectionType;
// const static ClockTimePoint kInvalidTimePoint{};
/*
* This is the device level metrics state, which will be modified based on
* incoming state events.
*
*/
void LEConnectionMetricState::AddStateChangedEvent(
LeConnectionOriginType origin_type,
LeConnectionType connection_type,
LeConnectionState transaction_state,
std::vector<std::pair<os::ArgumentType, int>> argument_list) {
LOG_INFO(
"LEConnectionMetricState: Origin Type: %s, Connection Type: %s, Transaction State: "
"%s",
common::ToHexString(origin_type).c_str(),
common::ToHexString(connection_type).c_str(),
common::ToHexString(transaction_state).c_str());
ClockTimePoint current_timestamp = std::chrono::high_resolution_clock::now();
state = transaction_state;
// Assign the origin of the connection
if (connection_origin_type == LeConnectionOriginType::ORIGIN_UNSPECIFIED) {
connection_origin_type = origin_type;
}
if (input_connection_type == LeConnectionType::CONNECTION_TYPE_UNSPECIFIED) {
input_connection_type = connection_type;
}
if (start_timepoint == kInvalidTimePoint) {
start_timepoint = current_timestamp;
}
end_timepoint = current_timestamp;
switch (state) {
case LeConnectionState::STATE_LE_ACL_START: {
int connection_type_cid = GetArgumentTypeFromList(argument_list, os::ArgumentType::L2CAP_CID);
if (connection_type_cid != -1) {
LeConnectionType connection_type = GetLeConnectionTypeFromCID(connection_type_cid);
if (connection_type != LeConnectionType::CONNECTION_TYPE_UNSPECIFIED) {
LOG_INFO("LEConnectionMetricsRemoteDevice: Populating the connection type\n");
input_connection_type = connection_type;
}
}
break;
}
case LeConnectionState::STATE_LE_ACL_END: {
int acl_status_code_from_args =
GetArgumentTypeFromList(argument_list, os::ArgumentType::ACL_STATUS_CODE);
acl_status_code = static_cast<android::bluetooth::hci::StatusEnum>(acl_status_code_from_args);
acl_state = LeAclConnectionState::LE_ACL_SUCCESS;
if (acl_status_code != android::bluetooth::hci::StatusEnum::STATUS_SUCCESS) {
acl_state = LeAclConnectionState::LE_ACL_FAILED;
}
break;
}
case LeConnectionState::STATE_LE_ACL_TIMEOUT: {
int acl_status_code_from_args =
GetArgumentTypeFromList(argument_list, os::ArgumentType::ACL_STATUS_CODE);
acl_status_code = static_cast<android::bluetooth::hci::StatusEnum>(acl_status_code_from_args);
acl_state = LeAclConnectionState::LE_ACL_FAILED;
break;
}
case LeConnectionState::STATE_LE_ACL_CANCEL: {
acl_state = LeAclConnectionState::LE_ACL_FAILED;
is_cancelled = true;
break;
}
[[fallthrough]];
default: {
// do nothing
}
}
}
bool LEConnectionMetricState::IsEnded() {
return acl_state == LeAclConnectionState::LE_ACL_SUCCESS ||
acl_state == LeAclConnectionState::LE_ACL_FAILED;
}
bool LEConnectionMetricState::IsStarted() {
return state == LeConnectionState::STATE_LE_ACL_START;
}
bool LEConnectionMetricState::IsCancelled() {
return is_cancelled;
}
// Initialize the LEConnectionMetricsRemoteDevice
LEConnectionMetricsRemoteDevice::LEConnectionMetricsRemoteDevice() {
metrics_logger_module = new MetricsLoggerModule();
}
LEConnectionMetricsRemoteDevice::LEConnectionMetricsRemoteDevice(
BaseMetricsLoggerModule* baseMetricsLoggerModule) {
metrics_logger_module = baseMetricsLoggerModule;
}
// Uploading the session
void LEConnectionMetricsRemoteDevice::UploadLEConnectionSession(const hci::Address& address) {
auto it = opened_devices.find(address);
if (it != opened_devices.end()) {
os::LEConnectionSessionOptions session_options;
session_options.acl_connection_state = it->second->acl_state;
session_options.origin_type = it->second->connection_origin_type;
session_options.transaction_type = it->second->input_connection_type;
session_options.latency = bluetooth::metrics::get_timedelta_nanos(
it->second->start_timepoint, it->second->end_timepoint);
session_options.remote_address = address;
session_options.status = it->second->acl_status_code;
// TODO: keep the acl latency the same as the overall latency for now
// When more events are added, we will an overall latency
session_options.acl_latency = session_options.latency;
session_options.is_cancelled = it->second->is_cancelled;
metrics_logger_module->LogMetricBluetoothLESession(session_options);
opened_devices.erase(it);
}
}
// Implementation of metrics per remote device
void LEConnectionMetricsRemoteDevice::AddStateChangedEvent(
const hci::Address& address,
LeConnectionOriginType origin_type,
LeConnectionType connection_type,
LeConnectionState transaction_state,
std::vector<std::pair<os::ArgumentType, int>> argument_list) {
LOG_INFO(
"LEConnectionMetricsRemoteDevice: Transaction State %s, Connection Type %s, Origin Type %s\n",
common::ToHexString(transaction_state).c_str(),
common::ToHexString(connection_type).c_str(),
common::ToHexString(origin_type).c_str());
if (address.IsEmpty()) {
LOG_INFO(
"LEConnectionMetricsRemoteDevice: Empty Address Cancellation %s, %s, %s\n",
common::ToHexString(transaction_state).c_str(),
common::ToHexString(connection_type).c_str(),
common::ToHexString(transaction_state).c_str());
for (auto& device_metric : device_metrics) {
if (device_metric->IsStarted() &&
transaction_state == LeConnectionState::STATE_LE_ACL_CANCEL) {
LOG_INFO("LEConnectionMetricsRemoteDevice: Cancellation Begin");
// cancel the connection
device_metric->AddStateChangedEvent(
origin_type, connection_type, transaction_state, argument_list);
continue;
}
if (device_metric->IsCancelled() &&
transaction_state == LeConnectionState::STATE_LE_ACL_END) {
LOG_INFO("LEConnectionMetricsRemoteDevice: Session is now complete after cancellation");
// complete the connection
device_metric->AddStateChangedEvent(
origin_type, connection_type, transaction_state, argument_list);
UploadLEConnectionSession(address);
continue;
}
}
return;
}
auto it = opened_devices.find(address);
if (it == opened_devices.end()) {
device_metrics.push_back(std::make_unique<LEConnectionMetricState>(address));
it = opened_devices.insert(std::begin(opened_devices), {address, device_metrics.back().get()});
}
it->second->AddStateChangedEvent(origin_type, connection_type, transaction_state, argument_list);
// Connection is finished
if (it->second->IsEnded()) {
UploadLEConnectionSession(address);
}
}
// MetricsLoggerModule class
void MetricsLoggerModule::LogMetricBluetoothLESession(
os::LEConnectionSessionOptions session_options) {
os::LogMetricBluetoothLEConnection(session_options);
}
// Instance of Metrics Collector for LEConnectionMetricsRemoteDeviceImpl
LEConnectionMetricsRemoteDevice* MetricsCollector::le_connection_metrics_remote_device =
new LEConnectionMetricsRemoteDevice();
LEConnectionMetricsRemoteDevice* MetricsCollector::GetLEConnectionMetricsCollector() {
return MetricsCollector::le_connection_metrics_remote_device;
}
} // namespace metrics
} // namespace bluetooth
|