forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbluetooth_adapter_mac_metrics.mm
187 lines (171 loc) · 8.25 KB
/
bluetooth_adapter_mac_metrics.mm
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/bluetooth_adapter_mac_metrics.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>
#include "base/mac/mac_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
namespace {
MacOSBluetoothOperationsResult GetMacOSOperationResultFromNSError(
NSError* error) {
if (!error)
return MacOSBluetoothOperationsResult::NO_ERROR;
NSString* error_domain = [error domain];
NSInteger error_code = [error code];
if ([error_domain isEqualToString:CBErrorDomain]) {
switch (error_code) {
case CBErrorUnknown:
return MacOSBluetoothOperationsResult::CBERROR_UNKNOWN;
case CBErrorInvalidParameters:
return MacOSBluetoothOperationsResult::CBERROR_INVALID_PARAMETERS;
case CBErrorInvalidHandle:
return MacOSBluetoothOperationsResult::CBERROR_INVALID_HANDLE;
case CBErrorNotConnected:
return MacOSBluetoothOperationsResult::CBERROR_NOT_CONNECTED;
case CBErrorOutOfSpace:
return MacOSBluetoothOperationsResult::CBERROR_OUT_OF_SPACE;
case CBErrorOperationCancelled:
return MacOSBluetoothOperationsResult::CBERROR_OPERATION_CANCELLED;
case CBErrorConnectionTimeout:
return MacOSBluetoothOperationsResult::CBERROR_CONNECTION_TIMEOUT;
case CBErrorPeripheralDisconnected:
return MacOSBluetoothOperationsResult::CBERROR_PERIPHERAL_DISCONNECTED;
case CBErrorUUIDNotAllowed:
return MacOSBluetoothOperationsResult::CBERROR_UUID_NOT_ALLOWED;
case CBErrorAlreadyAdvertising:
return MacOSBluetoothOperationsResult::CBERROR_ALREADY_ADVERTISING;
case CBErrorConnectionFailed:
if (base::mac::IsAtLeastOS10_13()) {
return MacOSBluetoothOperationsResult::CBERROR_CONNECTION_FAILED;
} else {
// For macOS 10.12 or before, the value CBErrorMaxConnection has the
// same value than CBErrorConnectionFailed and the same description
// than CBErrorConnectionLimitReached.
return MacOSBluetoothOperationsResult::
CBERROR_CONNECTION_LIMIT_REACHED;
}
case CBErrorConnectionLimitReached:
return MacOSBluetoothOperationsResult::CBERROR_CONNECTION_LIMIT_REACHED;
case CBErrorUnknownDevice:
return MacOSBluetoothOperationsResult::CBERROR_UNKNOWN_DEVICE;
default:
NOTREACHED();
}
return MacOSBluetoothOperationsResult::CBATT_ERROR_UNKNOWN_ERROR_CODE;
} else if ([error_domain isEqualToString:CBATTErrorDomain]) {
switch (static_cast<CBATTError>(error_code)) {
case CBATTErrorSuccess:
return MacOSBluetoothOperationsResult::CBATT_ERROR_SUCCESS;
case CBATTErrorInvalidHandle:
return MacOSBluetoothOperationsResult::CBATT_ERROR_INVALID_HANDLE;
case CBATTErrorReadNotPermitted:
return MacOSBluetoothOperationsResult::CBATT_ERROR_READ_NOT_PERMITTED;
case CBATTErrorWriteNotPermitted:
return MacOSBluetoothOperationsResult::CBATT_ERROR_WRITE_NOT_PERMITTED;
case CBATTErrorInvalidPdu:
return MacOSBluetoothOperationsResult::CBATT_ERROR_INVALID_PDU;
case CBATTErrorInsufficientAuthentication:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_INSUFFICIENT_AUTHENTICATION;
case CBATTErrorRequestNotSupported:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_REQUEST_NOT_SUPPORTED;
case CBATTErrorInvalidOffset:
return MacOSBluetoothOperationsResult::CBATT_ERROR_INVALID_OFFSET;
case CBATTErrorInsufficientAuthorization:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_INSUFFICIENT_AUTHORIZATION;
case CBATTErrorPrepareQueueFull:
return MacOSBluetoothOperationsResult::CBATT_ERROR_PREPARE_QUEUE_FULL;
case CBATTErrorAttributeNotFound:
return MacOSBluetoothOperationsResult::CBATT_ERROR_ATTRIBUTE_NOT_FOUND;
case CBATTErrorAttributeNotLong:
return MacOSBluetoothOperationsResult::CBATT_ERROR_ATTRIBUTE_NOT_LONG;
case CBATTErrorInsufficientEncryptionKeySize:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE;
case CBATTErrorInvalidAttributeValueLength:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH;
case CBATTErrorUnlikelyError:
return MacOSBluetoothOperationsResult::CBATT_ERROR_UNLIKELY_ERROR;
case CBATTErrorInsufficientEncryption:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_INSUFFICIENT_ENCRYPTION;
case CBATTErrorUnsupportedGroupType:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_UNSUPPORTED_GROUP_TYPE;
case CBATTErrorInsufficientResources:
return MacOSBluetoothOperationsResult::
CBATT_ERROR_INSUFFICIENT_RESOURCES;
}
return MacOSBluetoothOperationsResult::CBERROR_UNKNOWN_ERROR_CODE;
}
// TODO(crbug.com/755667): Needs to create an histogram to record unknown
// error domains.
return MacOSBluetoothOperationsResult::UNKNOWN_ERROR_DOMAIN;
}
} // namespace
void RecordDidFailToConnectPeripheralResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse(
"Bluetooth.MacOS.Errors.DidFailToConnectToPeripheral",
static_cast<int>(histogram_macos_error));
}
void RecordDidDisconnectPeripheralResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidDisconnectPeripheral",
static_cast<int>(histogram_macos_error));
}
void RecordDidDiscoverPrimaryServicesResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidDiscoverPrimaryServices",
static_cast<int>(histogram_macos_error));
}
void RecordDidDiscoverCharacteristicsResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidDiscoverCharacteristics",
static_cast<int>(histogram_macos_error));
}
void RecordDidUpdateValueResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidUpdateValue",
static_cast<int>(histogram_macos_error));
}
void RecordDidWriteValueResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidWriteValue",
static_cast<int>(histogram_macos_error));
}
void RecordDidUpdateNotificationStateResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidUpdateNotificationState",
static_cast<int>(histogram_macos_error));
}
void RecordDidDiscoverDescriptorsResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidDiscoverDescriptors",
static_cast<int>(histogram_macos_error));
}
void RecordDidUpdateValueForDescriptorResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidUpdateValueForDescriptor",
static_cast<int>(histogram_macos_error));
}
void RecordDidWriteValueForDescriptorResult(NSError* error) {
MacOSBluetoothOperationsResult histogram_macos_error =
GetMacOSOperationResultFromNSError(error);
base::UmaHistogramSparse("Bluetooth.MacOS.Errors.DidWriteValueForDescriptor",
static_cast<int>(histogram_macos_error));
}