blob: 397af7ba299139e5b68b9631d5e44c83f45a2345 [file] [log] [blame]
Hugo Benichi50a84c62016-09-02 09:00:59 +09001/*
2 * Copyright (C) 2016 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
17package com.android.server.connectivity;
18
Hugo Benichi73fd4d12017-03-15 23:05:01 +090019import static android.net.NetworkCapabilities.MAX_TRANSPORT;
20import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH;
21import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
22import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET;
Hugo Benichi27a51172017-07-18 11:35:18 +090023import static android.net.NetworkCapabilities.TRANSPORT_LOWPAN;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090024import static android.net.NetworkCapabilities.TRANSPORT_VPN;
25import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
26import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090027
Hugo Benichi380a0632017-10-20 09:25:29 +090028import android.net.ConnectivityManager;
Hugo Benichi50a84c62016-09-02 09:00:59 +090029import android.net.ConnectivityMetricsEvent;
30import android.net.metrics.ApfProgramEvent;
31import android.net.metrics.ApfStats;
32import android.net.metrics.DefaultNetworkEvent;
33import android.net.metrics.DhcpClientEvent;
34import android.net.metrics.DhcpErrorEvent;
35import android.net.metrics.DnsEvent;
Hugo Benichi5eb90532017-03-23 18:38:22 +090036import android.net.metrics.ConnectStats;
Hugo Benichi50a84c62016-09-02 09:00:59 +090037import android.net.metrics.IpManagerEvent;
38import android.net.metrics.IpReachabilityEvent;
39import android.net.metrics.NetworkEvent;
40import android.net.metrics.RaEvent;
41import android.net.metrics.ValidationProbeEvent;
Hugo Benichi60c9f632017-09-05 13:34:48 +090042import android.net.metrics.WakeupStats;
Hugo Benichi50a84c62016-09-02 09:00:59 +090043import android.os.Parcelable;
Hugo Benichi73fd4d12017-03-15 23:05:01 +090044import android.util.SparseArray;
Hugo Benichi5eb90532017-03-23 18:38:22 +090045import android.util.SparseIntArray;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010046import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
Hugo Benichi5eb90532017-03-23 18:38:22 +090047import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent;
48import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog;
Hugo Benichi5eb90532017-03-23 18:38:22 +090049import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.Pair;
Hugo Benichi50a84c62016-09-02 09:00:59 +090050import java.io.IOException;
51import java.util.ArrayList;
52import java.util.List;
53
Hugo Benichi50a84c62016-09-02 09:00:59 +090054
55/** {@hide} */
56final public class IpConnectivityEventBuilder {
57 private IpConnectivityEventBuilder() {
58 }
59
Hugo Benichi0d4a3982016-11-25 11:24:22 +090060 public static byte[] serialize(int dropped, List<IpConnectivityEvent> events)
Hugo Benichi50a84c62016-09-02 09:00:59 +090061 throws IOException {
62 final IpConnectivityLog log = new IpConnectivityLog();
Hugo Benichi0d4a3982016-11-25 11:24:22 +090063 log.events = events.toArray(new IpConnectivityEvent[events.size()]);
Hugo Benichi50a84c62016-09-02 09:00:59 +090064 log.droppedEvents = dropped;
Hugo Benichid680d4c2016-10-13 13:16:16 +090065 if ((log.events.length > 0) || (dropped > 0)) {
66 // Only write version number if log has some information at all.
67 log.version = IpConnectivityMetrics.VERSION;
68 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090069 return IpConnectivityLog.toByteArray(log);
70 }
71
Hugo Benichi0d4a3982016-11-25 11:24:22 +090072 public static List<IpConnectivityEvent> toProto(List<ConnectivityMetricsEvent> eventsIn) {
Hugo Benichi50a84c62016-09-02 09:00:59 +090073 final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size());
74 for (ConnectivityMetricsEvent in : eventsIn) {
75 final IpConnectivityEvent out = toProto(in);
76 if (out == null) {
77 continue;
78 }
79 eventsOut.add(out);
80 }
Hugo Benichi0d4a3982016-11-25 11:24:22 +090081 return eventsOut;
Hugo Benichi50a84c62016-09-02 09:00:59 +090082 }
83
84 public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) {
Hugo Benichi5eb90532017-03-23 18:38:22 +090085 final IpConnectivityEvent out = buildEvent(ev.netId, ev.transports, ev.ifname);
86 out.timeMs = ev.timestamp;
Hugo Benichi50a84c62016-09-02 09:00:59 +090087 if (!setEvent(out, ev.data)) {
88 return null;
89 }
Hugo Benichi50a84c62016-09-02 09:00:59 +090090 return out;
91 }
92
Hugo Benichi5eb90532017-03-23 18:38:22 +090093 public static IpConnectivityEvent toProto(ConnectStats in) {
94 IpConnectivityLogClass.ConnectStatistics stats =
95 new IpConnectivityLogClass.ConnectStatistics();
96 stats.connectCount = in.connectCount;
97 stats.connectBlockingCount = in.connectBlockingCount;
98 stats.ipv6AddrCount = in.ipv6ConnectCount;
99 stats.latenciesMs = in.latencies.toArray();
100 stats.errnosCounters = toPairArray(in.errnos);
101 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
102 out.setConnectStatistics(stats);
103 return out;
104 }
105
106
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900107 public static IpConnectivityEvent toProto(DnsEvent in) {
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900108 IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch =
109 new IpConnectivityLogClass.DNSLookupBatch();
110 in.resize(in.eventCount);
111 dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
112 dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
113 dnsLookupBatch.latenciesMs = in.latenciesMs;
Hugo Benichi5eb90532017-03-23 18:38:22 +0900114 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900115 out.setDnsLookupBatch(dnsLookupBatch);
Hugo Benichi2a5cfb92017-03-22 22:21:44 +0900116 return out;
117 }
118
Hugo Benichi60c9f632017-09-05 13:34:48 +0900119 public static IpConnectivityEvent toProto(WakeupStats in) {
120 IpConnectivityLogClass.WakeupStats wakeupStats =
121 new IpConnectivityLogClass.WakeupStats();
122 in.updateDuration();
123 wakeupStats.durationSec = in.durationSec;
124 wakeupStats.totalWakeups = in.totalWakeups;
125 wakeupStats.rootWakeups = in.rootWakeups;
126 wakeupStats.systemWakeups = in.systemWakeups;
127 wakeupStats.nonApplicationWakeups = in.nonApplicationWakeups;
128 wakeupStats.applicationWakeups = in.applicationWakeups;
Hugo Benichi175b5742017-09-19 13:15:26 +0900129 wakeupStats.noUidWakeups = in.noUidWakeups;
Hugo Benichid921bce2017-10-12 21:33:40 +0900130 wakeupStats.l2UnicastCount = in.l2UnicastCount;
131 wakeupStats.l2MulticastCount = in.l2MulticastCount;
132 wakeupStats.l2BroadcastCount = in.l2BroadcastCount;
133 wakeupStats.ethertypeCounts = toPairArray(in.ethertypes);
134 wakeupStats.ipNextHeaderCounts = toPairArray(in.ipNextHeaders);
Hugo Benichi60c9f632017-09-05 13:34:48 +0900135 final IpConnectivityEvent out = buildEvent(0, 0, in.iface);
136 out.setWakeupStats(wakeupStats);
137 return out;
138 }
139
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900140 public static IpConnectivityEvent toProto(DefaultNetworkEvent in) {
141 IpConnectivityLogClass.DefaultNetworkEvent ev =
142 new IpConnectivityLogClass.DefaultNetworkEvent();
Hugo Benichi380a0632017-10-20 09:25:29 +0900143 ev.finalScore = in.finalScore;
144 ev.initialScore = in.initialScore;
145 ev.ipSupport = ipSupportOf(in);
146 ev.defaultNetworkDurationMs = in.durationMs;
147 ev.validationDurationMs = in.validatedMs;
148 ev.previousDefaultNetworkLinkLayer = transportsToLinkLayer(in.previousTransports);
149 final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null);
150 if (in.transports == 0) {
151 // Set link layer to NONE for events representing the absence of a default network.
152 out.linkLayer = IpConnectivityLogClass.NONE;
153 }
Hugo Benichi1193a9c2017-10-19 14:58:15 +0900154 out.setDefaultNetworkEvent(ev);
155 return out;
156 }
157
Hugo Benichi5eb90532017-03-23 18:38:22 +0900158 private static IpConnectivityEvent buildEvent(int netId, long transports, String ifname) {
159 final IpConnectivityEvent ev = new IpConnectivityEvent();
160 ev.networkId = netId;
161 ev.transports = transports;
162 if (ifname != null) {
163 ev.ifName = ifname;
164 }
165 inferLinkLayer(ev);
166 return ev;
167 }
168
Hugo Benichi50a84c62016-09-02 09:00:59 +0900169 private static boolean setEvent(IpConnectivityEvent out, Parcelable in) {
170 if (in instanceof DhcpErrorEvent) {
171 setDhcpErrorEvent(out, (DhcpErrorEvent) in);
172 return true;
173 }
174
175 if (in instanceof DhcpClientEvent) {
176 setDhcpClientEvent(out, (DhcpClientEvent) in);
177 return true;
178 }
179
Hugo Benichi50a84c62016-09-02 09:00:59 +0900180 if (in instanceof IpManagerEvent) {
181 setIpManagerEvent(out, (IpManagerEvent) in);
182 return true;
183 }
184
185 if (in instanceof IpReachabilityEvent) {
186 setIpReachabilityEvent(out, (IpReachabilityEvent) in);
187 return true;
188 }
189
Hugo Benichi50a84c62016-09-02 09:00:59 +0900190 if (in instanceof NetworkEvent) {
191 setNetworkEvent(out, (NetworkEvent) in);
192 return true;
193 }
194
195 if (in instanceof ValidationProbeEvent) {
196 setValidationProbeEvent(out, (ValidationProbeEvent) in);
197 return true;
198 }
199
200 if (in instanceof ApfProgramEvent) {
201 setApfProgramEvent(out, (ApfProgramEvent) in);
202 return true;
203 }
204
205 if (in instanceof ApfStats) {
206 setApfStats(out, (ApfStats) in);
207 return true;
208 }
209
210 if (in instanceof RaEvent) {
211 setRaEvent(out, (RaEvent) in);
212 return true;
213 }
214
215 return false;
216 }
217
218 private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100219 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100220 dhcpEvent.setErrorCode(in.errorCode);
221 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900222 }
223
224 private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100225 IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100226 dhcpEvent.setStateTransition(in.msg);
227 dhcpEvent.durationMs = in.durationMs;
228 out.setDhcpEvent(dhcpEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900229 }
230
Hugo Benichi50a84c62016-09-02 09:00:59 +0900231 private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900232 IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent =
233 new IpConnectivityLogClass.IpProvisioningEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100234 ipProvisioningEvent.eventType = in.eventType;
235 ipProvisioningEvent.latencyMs = (int) in.durationMs;
236 out.setIpProvisioningEvent(ipProvisioningEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900237 }
238
239 private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900240 IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent =
241 new IpConnectivityLogClass.IpReachabilityEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100242 ipReachabilityEvent.eventType = in.eventType;
243 out.setIpReachabilityEvent(ipReachabilityEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900244 }
245
Hugo Benichi50a84c62016-09-02 09:00:59 +0900246 private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900247 IpConnectivityLogClass.NetworkEvent networkEvent =
248 new IpConnectivityLogClass.NetworkEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100249 networkEvent.eventType = in.eventType;
250 networkEvent.latencyMs = (int) in.durationMs;
251 out.setNetworkEvent(networkEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900252 }
253
254 private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900255 IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent =
256 new IpConnectivityLogClass.ValidationProbeEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100257 validationProbeEvent.latencyMs = (int) in.durationMs;
258 validationProbeEvent.probeType = in.probeType;
259 validationProbeEvent.probeResult = in.returnCode;
260 out.setValidationProbeEvent(validationProbeEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900261 }
262
263 private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900264 IpConnectivityLogClass.ApfProgramEvent apfProgramEvent =
265 new IpConnectivityLogClass.ApfProgramEvent();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100266 apfProgramEvent.lifetime = in.lifetime;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900267 apfProgramEvent.effectiveLifetime = in.actualLifetime;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100268 apfProgramEvent.filteredRas = in.filteredRas;
269 apfProgramEvent.currentRas = in.currentRas;
270 apfProgramEvent.programLength = in.programLength;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900271 if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100272 apfProgramEvent.dropMulticast = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900273 }
274 if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100275 apfProgramEvent.hasIpv4Addr = true;
Hugo Benichi50a84c62016-09-02 09:00:59 +0900276 }
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100277 out.setApfProgramEvent(apfProgramEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900278 }
279
280 private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900281 IpConnectivityLogClass.ApfStatistics apfStatistics =
282 new IpConnectivityLogClass.ApfStatistics();
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100283 apfStatistics.durationMs = in.durationMs;
284 apfStatistics.receivedRas = in.receivedRas;
285 apfStatistics.matchingRas = in.matchingRas;
286 apfStatistics.droppedRas = in.droppedRas;
287 apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
288 apfStatistics.parseErrors = in.parseErrors;
289 apfStatistics.programUpdates = in.programUpdates;
Hugo Benichi22d9b2d2017-02-22 13:02:27 +0900290 apfStatistics.programUpdatesAll = in.programUpdatesAll;
291 apfStatistics.programUpdatesAllowingMulticast = in.programUpdatesAllowingMulticast;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100292 apfStatistics.maxProgramSize = in.maxProgramSize;
293 out.setApfStatistics(apfStatistics);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900294 }
295
296 private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
Tamas Berghammer383db5eb2016-06-22 15:21:38 +0100297 IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent();
298 raEvent.routerLifetime = in.routerLifetime;
299 raEvent.prefixValidLifetime = in.prefixValidLifetime;
300 raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
301 raEvent.routeInfoLifetime = in.routeInfoLifetime;
302 raEvent.rdnssLifetime = in.rdnssLifetime;
303 raEvent.dnsslLifetime = in.dnsslLifetime;
304 out.setRaEvent(raEvent);
Hugo Benichi50a84c62016-09-02 09:00:59 +0900305 }
306
307 private static int[] bytesToInts(byte[] in) {
308 final int[] out = new int[in.length];
309 for (int i = 0; i < in.length; i++) {
310 out[i] = in[i] & 0xFF;
311 }
312 return out;
313 }
314
Hugo Benichi5eb90532017-03-23 18:38:22 +0900315 private static Pair[] toPairArray(SparseIntArray counts) {
316 final int s = counts.size();
317 Pair[] pairs = new Pair[s];
318 for (int i = 0; i < s; i++) {
319 Pair p = new Pair();
320 p.key = counts.keyAt(i);
321 p.value = counts.valueAt(i);
322 pairs[i] = p;
323 }
324 return pairs;
325 }
326
Hugo Benichi50a84c62016-09-02 09:00:59 +0900327 private static int ipSupportOf(DefaultNetworkEvent in) {
Hugo Benichi380a0632017-10-20 09:25:29 +0900328 if (in.ipv4 && in.ipv6) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900329 return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
330 }
Hugo Benichi380a0632017-10-20 09:25:29 +0900331 if (in.ipv6) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900332 return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
333 }
Hugo Benichi380a0632017-10-20 09:25:29 +0900334 if (in.ipv4) {
Hugo Benichi50a84c62016-09-02 09:00:59 +0900335 return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
336 }
337 return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
338 }
339
340 private static boolean isBitSet(int flags, int bit) {
341 return (flags & (1 << bit)) != 0;
342 }
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900343
344 private static void inferLinkLayer(IpConnectivityEvent ev) {
345 int linkLayer = IpConnectivityLogClass.UNKNOWN;
346 if (ev.transports != 0) {
347 linkLayer = transportsToLinkLayer(ev.transports);
348 } else if (ev.ifName != null) {
349 linkLayer = ifnameToLinkLayer(ev.ifName);
350 }
351 if (linkLayer == IpConnectivityLogClass.UNKNOWN) {
352 return;
353 }
354 ev.linkLayer = linkLayer;
355 ev.ifName = "";
356 }
357
358 private static int transportsToLinkLayer(long transports) {
359 switch (Long.bitCount(transports)) {
360 case 0:
361 return IpConnectivityLogClass.UNKNOWN;
362 case 1:
363 int t = Long.numberOfTrailingZeros(transports);
364 return transportToLinkLayer(t);
365 default:
366 return IpConnectivityLogClass.MULTIPLE;
367 }
368 }
369
370 private static int transportToLinkLayer(int transport) {
371 if (0 <= transport && transport < TRANSPORT_LINKLAYER_MAP.length) {
372 return TRANSPORT_LINKLAYER_MAP[transport];
373 }
374 return IpConnectivityLogClass.UNKNOWN;
375 }
376
377 private static final int[] TRANSPORT_LINKLAYER_MAP = new int[MAX_TRANSPORT + 1];
378 static {
379 TRANSPORT_LINKLAYER_MAP[TRANSPORT_CELLULAR] = IpConnectivityLogClass.CELLULAR;
380 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI] = IpConnectivityLogClass.WIFI;
381 TRANSPORT_LINKLAYER_MAP[TRANSPORT_BLUETOOTH] = IpConnectivityLogClass.BLUETOOTH;
382 TRANSPORT_LINKLAYER_MAP[TRANSPORT_ETHERNET] = IpConnectivityLogClass.ETHERNET;
383 TRANSPORT_LINKLAYER_MAP[TRANSPORT_VPN] = IpConnectivityLogClass.UNKNOWN;
Hugo Benichi27a51172017-07-18 11:35:18 +0900384 TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI_AWARE] = IpConnectivityLogClass.WIFI_NAN;
385 TRANSPORT_LINKLAYER_MAP[TRANSPORT_LOWPAN] = IpConnectivityLogClass.LOWPAN;
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900386 };
387
388 private static int ifnameToLinkLayer(String ifname) {
389 // Do not try to catch all interface names with regexes, instead only catch patterns that
390 // are cheap to check, and otherwise fallback on postprocessing in aggregation layer.
Hugo Benichi27a51172017-07-18 11:35:18 +0900391 for (int i = 0; i < KNOWN_PREFIX; i++) {
392 String pattern = IFNAME_PREFIXES[i];
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900393 if (ifname.startsWith(pattern)) {
Hugo Benichi27a51172017-07-18 11:35:18 +0900394 return IFNAME_LINKLAYERS[i];
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900395 }
396 }
397 return IpConnectivityLogClass.UNKNOWN;
398 }
399
Hugo Benichi27a51172017-07-18 11:35:18 +0900400 private static final int KNOWN_PREFIX = 7;
401 private static final String[] IFNAME_PREFIXES = new String[KNOWN_PREFIX];
402 private static final int[] IFNAME_LINKLAYERS = new int[KNOWN_PREFIX];
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900403 static {
Hugo Benichi27a51172017-07-18 11:35:18 +0900404 // Ordered from most likely link layer to least likely.
405 IFNAME_PREFIXES[0] = "rmnet";
406 IFNAME_LINKLAYERS[0] = IpConnectivityLogClass.CELLULAR;
407
408 IFNAME_PREFIXES[1] = "wlan";
409 IFNAME_LINKLAYERS[1] = IpConnectivityLogClass.WIFI;
410
411 IFNAME_PREFIXES[2] = "bt-pan";
412 IFNAME_LINKLAYERS[2] = IpConnectivityLogClass.BLUETOOTH;
413
414 IFNAME_PREFIXES[3] = "p2p";
415 IFNAME_LINKLAYERS[3] = IpConnectivityLogClass.WIFI_P2P;
416
417 IFNAME_PREFIXES[4] = "aware";
418 IFNAME_LINKLAYERS[4] = IpConnectivityLogClass.WIFI_NAN;
419
420 IFNAME_PREFIXES[5] = "eth";
421 IFNAME_LINKLAYERS[5] = IpConnectivityLogClass.ETHERNET;
422
423 IFNAME_PREFIXES[6] = "wpan";
424 IFNAME_LINKLAYERS[6] = IpConnectivityLogClass.LOWPAN;
Hugo Benichi73fd4d12017-03-15 23:05:01 +0900425 }
Hugo Benichi50a84c62016-09-02 09:00:59 +0900426}