1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/proximity_auth/metrics.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/metrics/sparse_histogram.h"
10 #include "base/sys_byteorder.h"
12 namespace proximity_auth
{
17 // Converts the 4-byte prefix of an MD5 hash into a int32 value.
18 int32
DigestToInt32(const base::MD5Digest
& digest
) {
19 // First, copy to a uint32, since byte swapping and endianness conversions
20 // expect unsigned integers.
21 uint32 unsigned_value
;
22 DCHECK_GE(arraysize(digest
.a
), sizeof(unsigned_value
));
23 memcpy(&unsigned_value
, digest
.a
, sizeof(unsigned_value
));
24 unsigned_value
= base::ByteSwap(base::HostToNet32(unsigned_value
));
26 // Then copy the resulting bit pattern to an int32 to match the datatype that
29 memcpy(&value
, &unsigned_value
, sizeof(value
));
33 // Returns a hash of the given |name|, encoded as a 32-bit signed integer.
34 int32
HashDeviceModelName(const std::string
& name
) {
35 base::MD5Digest digest
;
36 base::MD5Sum(name
.c_str(), name
.size(), &digest
);
37 return DigestToInt32(digest
);
42 const char kUnknownDeviceModel
[] = "Unknown";
43 const int kUnknownProximityValue
= 127;
45 void RecordAuthProximityRollingRssi(int rolling_rssi
) {
46 if (rolling_rssi
!= kUnknownProximityValue
)
47 rolling_rssi
= std::min(50, std::max(-100, rolling_rssi
));
49 UMA_HISTOGRAM_SPARSE_SLOWLY("EasyUnlock.AuthProximity.RollingRssi",
53 void RecordAuthProximityTransmitPowerDelta(int transmit_power_delta
) {
54 if (transmit_power_delta
!= kUnknownProximityValue
)
55 transmit_power_delta
= std::min(50, std::max(-100, transmit_power_delta
));
57 UMA_HISTOGRAM_SPARSE_SLOWLY("EasyUnlock.AuthProximity.TransmitPowerDelta",
58 transmit_power_delta
);
61 void RecordAuthProximityTimeSinceLastZeroRssi(
62 base::TimeDelta time_since_last_zero_rssi
) {
63 UMA_HISTOGRAM_TIMES("EasyUnlock.AuthProximity.TimeSinceLastZeroRssi",
64 time_since_last_zero_rssi
);
67 void RecordAuthProximityRemoteDeviceModelHash(const std::string
& device_model
) {
68 UMA_HISTOGRAM_SPARSE_SLOWLY("EasyUnlock.AuthProximity.RemoteDeviceModelHash",
69 HashDeviceModelName(device_model
));
72 } // namespace metrics
73 } // namespace proximity_auth