summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/DppMetrics.java
blob: ef26b708a28a290c45c1e0e300e3b79afebd5bce (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (C) 2019 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.
 */

package com.android.server.wifi;

import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_AUTHENTICATION;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_BUSY;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_CONFIGURATION;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_GENERIC;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_INVALID_NETWORK;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_INVALID_URI;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_NOT_COMPATIBLE;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_NOT_SUPPORTED;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_TIMEOUT;
import static android.net.wifi.EasyConnectStatusCallback.EASY_CONNECT_EVENT_SUCCESS_CONFIGURATION_SENT;

import android.net.wifi.EasyConnectStatusCallback;
import android.util.SparseIntArray;

import com.android.internal.annotations.VisibleForTesting;
import com.android.server.wifi.nano.WifiMetricsProto;
import com.android.server.wifi.util.IntHistogram;

import java.io.PrintWriter;

/**
 * Provides metrics for Wi-Fi Easy Connect (DPP). Metrics include number of initiator requests,
 * number of successes, failures and time completion histogram.
 */
public class DppMetrics {
    private final WifiMetricsProto.WifiDppLog mWifiDppLogProto = new WifiMetricsProto.WifiDppLog();

    // Easy-Connect (DPP) Metrics
    // Histogram for DPP operation time. Indicates the following 5 buckets (in seconds):
    //   < 1
    //   [1, 10)
    //   [10, 25)
    //   [25, 39)
    //   >= 39  - which means timeout.
    @VisibleForTesting
    public static final int[] DPP_OPERATION_TIME = {1, 10, 25, 39};
    private IntHistogram mHistogramDppOperationTime = new IntHistogram(DPP_OPERATION_TIME);

    // Failure codes
    private SparseIntArray mHistogramDppFailureCode = new SparseIntArray();

    // Configurator success codes
    private SparseIntArray mHistogramDppConfiguratorSuccessCode = new SparseIntArray();

    private final Object mLock = new Object();

    /**
     * Update DPP Configurator-Initiator requests
     */
    public void updateDppConfiguratorInitiatorRequests() {
        synchronized (mLock) {
            mWifiDppLogProto.numDppConfiguratorInitiatorRequests++;
        }
    }

    /**
     * Update DPP Enrollee-Initiator requests
     */
    public void updateDppEnrolleeInitiatorRequests() {
        synchronized (mLock) {
            mWifiDppLogProto.numDppEnrolleeInitiatorRequests++;
        }
    }

    /**
     * Update DPP Enrollee success counter
     */
    public void updateDppEnrolleeSuccess() {
        synchronized (mLock) {
            mWifiDppLogProto.numDppEnrolleeSuccess++;
        }
    }

    /**
     * Update DPP Configurator success counter
     */
    public void updateDppConfiguratorSuccess(
            @EasyConnectStatusCallback.EasyConnectSuccessStatusCode int code) {
        synchronized (mLock) {
            switch (code) {
                case EASY_CONNECT_EVENT_SUCCESS_CONFIGURATION_SENT:
                    mHistogramDppConfiguratorSuccessCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_SUCCESS_CONFIGURATION_SENT,
                            mHistogramDppConfiguratorSuccessCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_SUCCESS_CONFIGURATION_SENT) + 1);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * Update DPP failure counters
     */
    public void updateDppFailure(@EasyConnectStatusCallback.EasyConnectFailureStatusCode int code) {
        synchronized (mLock) {
            switch (code) {
                case EASY_CONNECT_EVENT_FAILURE_INVALID_URI:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_INVALID_URI,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_INVALID_URI) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_AUTHENTICATION:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_AUTHENTICATION,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_AUTHENTICATION) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_NOT_COMPATIBLE:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_NOT_COMPATIBLE,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_NOT_COMPATIBLE) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_CONFIGURATION:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_CONFIGURATION,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_CONFIGURATION) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_BUSY:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_BUSY,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_BUSY) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_TIMEOUT:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_TIMEOUT,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_TIMEOUT) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_GENERIC:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_GENERIC,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_GENERIC) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_NOT_SUPPORTED:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_NOT_SUPPORTED,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_NOT_SUPPORTED) + 1);
                    break;
                case EASY_CONNECT_EVENT_FAILURE_INVALID_NETWORK:
                    mHistogramDppFailureCode.put(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_INVALID_NETWORK,
                            mHistogramDppFailureCode.get(WifiMetricsProto.WifiDppLog
                                    .EASY_CONNECT_EVENT_FAILURE_INVALID_NETWORK) + 1);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * Update DPP operation time
     *
     * @param timeMs Time it took to complete the operation, in milliseconds
     */
    public void updateDppOperationTime(int timeMs) {
        synchronized (mLock) {
            mHistogramDppOperationTime.increment(timeMs / 1000);
        }
    }

    /**
     * Dump all DPP metrics
     *
     * @param pw PrintWriter handle
     */
    public void dump(PrintWriter pw) {
        synchronized (mLock) {
            pw.println("---Easy Connect/DPP metrics---");
            pw.println("mWifiDppLogProto.numDppConfiguratorInitiatorRequests="
                    + mWifiDppLogProto.numDppConfiguratorInitiatorRequests);
            pw.println("mWifiDppLogProto.numDppEnrolleeInitiatorRequests="
                    + mWifiDppLogProto.numDppEnrolleeInitiatorRequests);
            pw.println("mWifiDppLogProto.numDppEnrolleeSuccess="
                    + mWifiDppLogProto.numDppEnrolleeSuccess);

            if (mHistogramDppFailureCode.size() > 0) {
                pw.println("mHistogramDppFailureCode=");
                pw.println(mHistogramDppFailureCode);
            }

            if (mHistogramDppConfiguratorSuccessCode.size() > 0) {
                pw.println("mHistogramDppConfiguratorSuccessCode=");
                pw.println(mHistogramDppConfiguratorSuccessCode);
            }

            if (mHistogramDppOperationTime.numNonEmptyBuckets() > 0) {
                pw.println("mHistogramDppOperationTime=");
                pw.println(mHistogramDppOperationTime);
            }
            pw.println("---End of Easy Connect/DPP metrics---");
        }
    }

    /**
     * Clear all DPP metrics
     */
    public void clear() {
        synchronized (mLock) {
            mWifiDppLogProto.numDppConfiguratorInitiatorRequests = 0;
            mWifiDppLogProto.numDppEnrolleeInitiatorRequests = 0;
            mWifiDppLogProto.numDppEnrolleeSuccess = 0;
            mHistogramDppFailureCode.clear();
            mHistogramDppOperationTime.clear();
            mHistogramDppConfiguratorSuccessCode.clear();
        }
    }

    private WifiMetricsProto.WifiDppLog.DppFailureStatusHistogramBucket[] consolidateDppFailure(
            SparseIntArray data) {
        WifiMetricsProto.WifiDppLog.DppFailureStatusHistogramBucket[]
                dppFailureStatusHistogramBuckets =
                new WifiMetricsProto.WifiDppLog.DppFailureStatusHistogramBucket[data.size()];

        for (int i = 0; i < data.size(); i++) {
            dppFailureStatusHistogramBuckets[i] =
                    new WifiMetricsProto.WifiDppLog.DppFailureStatusHistogramBucket();
            dppFailureStatusHistogramBuckets[i].dppStatusType = data.keyAt(i);
            dppFailureStatusHistogramBuckets[i].count = data.valueAt(i);
        }

        return dppFailureStatusHistogramBuckets;
    }

    private WifiMetricsProto.WifiDppLog.DppConfiguratorSuccessStatusHistogramBucket[]
            consolidateDppSuccess(
            SparseIntArray data) {
        WifiMetricsProto.WifiDppLog.DppConfiguratorSuccessStatusHistogramBucket[]
                dppConfiguratorSuccessStatusHistogramBuckets =
                new WifiMetricsProto.WifiDppLog
                        .DppConfiguratorSuccessStatusHistogramBucket[data.size()];

        for (int i = 0; i < data.size(); i++) {
            dppConfiguratorSuccessStatusHistogramBuckets[i] =
                    new WifiMetricsProto.WifiDppLog.DppConfiguratorSuccessStatusHistogramBucket();
            dppConfiguratorSuccessStatusHistogramBuckets[i].dppStatusType = data.keyAt(i);
            dppConfiguratorSuccessStatusHistogramBuckets[i].count = data.valueAt(i);
        }

        return dppConfiguratorSuccessStatusHistogramBuckets;
    }

    /**
     * Consolidate all metrics into the proto.
     */
    public WifiMetricsProto.WifiDppLog consolidateProto() {
        WifiMetricsProto.WifiDppLog log = new WifiMetricsProto.WifiDppLog();
        synchronized (mLock) {
            log.numDppConfiguratorInitiatorRequests =
                    mWifiDppLogProto.numDppConfiguratorInitiatorRequests;
            log.numDppEnrolleeInitiatorRequests = mWifiDppLogProto.numDppEnrolleeInitiatorRequests;
            log.numDppEnrolleeSuccess = mWifiDppLogProto.numDppEnrolleeSuccess;
            log.dppFailureCode = consolidateDppFailure(mHistogramDppFailureCode);
            log.dppConfiguratorSuccessCode =
                    consolidateDppSuccess(mHistogramDppConfiguratorSuccessCode);
            log.dppOperationTime = mHistogramDppOperationTime.toProto();
        }
        return log;
    }
}