Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 19 | import static android.net.NetworkCapabilities.MAX_TRANSPORT; |
| 20 | import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH; |
| 21 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
| 22 | import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; |
Hugo Benichi | 27a5117 | 2017-07-18 11:35:18 +0900 | [diff] [blame^] | 23 | import static android.net.NetworkCapabilities.TRANSPORT_LOWPAN; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 24 | import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
| 25 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
| 26 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 27 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 28 | import android.net.ConnectivityMetricsEvent; |
| 29 | import android.net.metrics.ApfProgramEvent; |
| 30 | import android.net.metrics.ApfStats; |
| 31 | import android.net.metrics.DefaultNetworkEvent; |
| 32 | import android.net.metrics.DhcpClientEvent; |
| 33 | import android.net.metrics.DhcpErrorEvent; |
| 34 | import android.net.metrics.DnsEvent; |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 35 | import android.net.metrics.ConnectStats; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 36 | import android.net.metrics.IpManagerEvent; |
| 37 | import android.net.metrics.IpReachabilityEvent; |
| 38 | import android.net.metrics.NetworkEvent; |
| 39 | import android.net.metrics.RaEvent; |
| 40 | import android.net.metrics.ValidationProbeEvent; |
| 41 | import android.os.Parcelable; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 42 | import android.util.SparseArray; |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 43 | import android.util.SparseIntArray; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 44 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass; |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 45 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent; |
| 46 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityLog; |
| 47 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.NetworkId; |
| 48 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.Pair; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 49 | import java.io.IOException; |
| 50 | import java.util.ArrayList; |
| 51 | import java.util.List; |
| 52 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 53 | |
| 54 | /** {@hide} */ |
| 55 | final public class IpConnectivityEventBuilder { |
| 56 | private IpConnectivityEventBuilder() { |
| 57 | } |
| 58 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 59 | public static byte[] serialize(int dropped, List<IpConnectivityEvent> events) |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 60 | throws IOException { |
| 61 | final IpConnectivityLog log = new IpConnectivityLog(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 62 | log.events = events.toArray(new IpConnectivityEvent[events.size()]); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 63 | log.droppedEvents = dropped; |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 64 | if ((log.events.length > 0) || (dropped > 0)) { |
| 65 | // Only write version number if log has some information at all. |
| 66 | log.version = IpConnectivityMetrics.VERSION; |
| 67 | } |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 68 | return IpConnectivityLog.toByteArray(log); |
| 69 | } |
| 70 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 71 | public static List<IpConnectivityEvent> toProto(List<ConnectivityMetricsEvent> eventsIn) { |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 72 | final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size()); |
| 73 | for (ConnectivityMetricsEvent in : eventsIn) { |
| 74 | final IpConnectivityEvent out = toProto(in); |
| 75 | if (out == null) { |
| 76 | continue; |
| 77 | } |
| 78 | eventsOut.add(out); |
| 79 | } |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 80 | return eventsOut; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 84 | final IpConnectivityEvent out = buildEvent(ev.netId, ev.transports, ev.ifname); |
| 85 | out.timeMs = ev.timestamp; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 86 | if (!setEvent(out, ev.data)) { |
| 87 | return null; |
| 88 | } |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 89 | return out; |
| 90 | } |
| 91 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 92 | public static IpConnectivityEvent toProto(ConnectStats in) { |
| 93 | IpConnectivityLogClass.ConnectStatistics stats = |
| 94 | new IpConnectivityLogClass.ConnectStatistics(); |
| 95 | stats.connectCount = in.connectCount; |
| 96 | stats.connectBlockingCount = in.connectBlockingCount; |
| 97 | stats.ipv6AddrCount = in.ipv6ConnectCount; |
| 98 | stats.latenciesMs = in.latencies.toArray(); |
| 99 | stats.errnosCounters = toPairArray(in.errnos); |
| 100 | final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null); |
| 101 | out.setConnectStatistics(stats); |
| 102 | return out; |
| 103 | } |
| 104 | |
| 105 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 106 | public static IpConnectivityEvent toProto(DnsEvent in) { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 107 | IpConnectivityLogClass.DNSLookupBatch dnsLookupBatch = |
| 108 | new IpConnectivityLogClass.DNSLookupBatch(); |
| 109 | in.resize(in.eventCount); |
| 110 | dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes); |
| 111 | dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes); |
| 112 | dnsLookupBatch.latenciesMs = in.latenciesMs; |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 113 | final IpConnectivityEvent out = buildEvent(in.netId, in.transports, null); |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 114 | out.setDnsLookupBatch(dnsLookupBatch); |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 115 | return out; |
| 116 | } |
| 117 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 118 | private static IpConnectivityEvent buildEvent(int netId, long transports, String ifname) { |
| 119 | final IpConnectivityEvent ev = new IpConnectivityEvent(); |
| 120 | ev.networkId = netId; |
| 121 | ev.transports = transports; |
| 122 | if (ifname != null) { |
| 123 | ev.ifName = ifname; |
| 124 | } |
| 125 | inferLinkLayer(ev); |
| 126 | return ev; |
| 127 | } |
| 128 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 129 | private static boolean setEvent(IpConnectivityEvent out, Parcelable in) { |
| 130 | if (in instanceof DhcpErrorEvent) { |
| 131 | setDhcpErrorEvent(out, (DhcpErrorEvent) in); |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | if (in instanceof DhcpClientEvent) { |
| 136 | setDhcpClientEvent(out, (DhcpClientEvent) in); |
| 137 | return true; |
| 138 | } |
| 139 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 140 | if (in instanceof IpManagerEvent) { |
| 141 | setIpManagerEvent(out, (IpManagerEvent) in); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | if (in instanceof IpReachabilityEvent) { |
| 146 | setIpReachabilityEvent(out, (IpReachabilityEvent) in); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | if (in instanceof DefaultNetworkEvent) { |
| 151 | setDefaultNetworkEvent(out, (DefaultNetworkEvent) in); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | if (in instanceof NetworkEvent) { |
| 156 | setNetworkEvent(out, (NetworkEvent) in); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | if (in instanceof ValidationProbeEvent) { |
| 161 | setValidationProbeEvent(out, (ValidationProbeEvent) in); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | if (in instanceof ApfProgramEvent) { |
| 166 | setApfProgramEvent(out, (ApfProgramEvent) in); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | if (in instanceof ApfStats) { |
| 171 | setApfStats(out, (ApfStats) in); |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | if (in instanceof RaEvent) { |
| 176 | setRaEvent(out, (RaEvent) in); |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 184 | IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 185 | dhcpEvent.setErrorCode(in.errorCode); |
| 186 | out.setDhcpEvent(dhcpEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 190 | IpConnectivityLogClass.DHCPEvent dhcpEvent = new IpConnectivityLogClass.DHCPEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 191 | dhcpEvent.setStateTransition(in.msg); |
| 192 | dhcpEvent.durationMs = in.durationMs; |
| 193 | out.setDhcpEvent(dhcpEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 194 | } |
| 195 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 196 | private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) { |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 197 | IpConnectivityLogClass.IpProvisioningEvent ipProvisioningEvent = |
| 198 | new IpConnectivityLogClass.IpProvisioningEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 199 | ipProvisioningEvent.eventType = in.eventType; |
| 200 | ipProvisioningEvent.latencyMs = (int) in.durationMs; |
| 201 | out.setIpProvisioningEvent(ipProvisioningEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) { |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 205 | IpConnectivityLogClass.IpReachabilityEvent ipReachabilityEvent = |
| 206 | new IpConnectivityLogClass.IpReachabilityEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 207 | ipReachabilityEvent.eventType = in.eventType; |
| 208 | out.setIpReachabilityEvent(ipReachabilityEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) { |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 212 | IpConnectivityLogClass.DefaultNetworkEvent defaultNetworkEvent = |
| 213 | new IpConnectivityLogClass.DefaultNetworkEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 214 | defaultNetworkEvent.networkId = netIdOf(in.netId); |
| 215 | defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId); |
| 216 | defaultNetworkEvent.transportTypes = in.transportTypes; |
| 217 | defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in); |
| 218 | out.setDefaultNetworkEvent(defaultNetworkEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) { |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 222 | IpConnectivityLogClass.NetworkEvent networkEvent = |
| 223 | new IpConnectivityLogClass.NetworkEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 224 | networkEvent.networkId = netIdOf(in.netId); |
| 225 | networkEvent.eventType = in.eventType; |
| 226 | networkEvent.latencyMs = (int) in.durationMs; |
| 227 | out.setNetworkEvent(networkEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) { |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 231 | IpConnectivityLogClass.ValidationProbeEvent validationProbeEvent = |
| 232 | new IpConnectivityLogClass.ValidationProbeEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 233 | validationProbeEvent.latencyMs = (int) in.durationMs; |
| 234 | validationProbeEvent.probeType = in.probeType; |
| 235 | validationProbeEvent.probeResult = in.returnCode; |
| 236 | out.setValidationProbeEvent(validationProbeEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) { |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 240 | IpConnectivityLogClass.ApfProgramEvent apfProgramEvent = |
| 241 | new IpConnectivityLogClass.ApfProgramEvent(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 242 | apfProgramEvent.lifetime = in.lifetime; |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 243 | apfProgramEvent.effectiveLifetime = in.actualLifetime; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 244 | apfProgramEvent.filteredRas = in.filteredRas; |
| 245 | apfProgramEvent.currentRas = in.currentRas; |
| 246 | apfProgramEvent.programLength = in.programLength; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 247 | if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 248 | apfProgramEvent.dropMulticast = true; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 249 | } |
| 250 | if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 251 | apfProgramEvent.hasIpv4Addr = true; |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 252 | } |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 253 | out.setApfProgramEvent(apfProgramEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | private static void setApfStats(IpConnectivityEvent out, ApfStats in) { |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 257 | IpConnectivityLogClass.ApfStatistics apfStatistics = |
| 258 | new IpConnectivityLogClass.ApfStatistics(); |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 259 | apfStatistics.durationMs = in.durationMs; |
| 260 | apfStatistics.receivedRas = in.receivedRas; |
| 261 | apfStatistics.matchingRas = in.matchingRas; |
| 262 | apfStatistics.droppedRas = in.droppedRas; |
| 263 | apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas; |
| 264 | apfStatistics.parseErrors = in.parseErrors; |
| 265 | apfStatistics.programUpdates = in.programUpdates; |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 266 | apfStatistics.programUpdatesAll = in.programUpdatesAll; |
| 267 | apfStatistics.programUpdatesAllowingMulticast = in.programUpdatesAllowingMulticast; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 268 | apfStatistics.maxProgramSize = in.maxProgramSize; |
| 269 | out.setApfStatistics(apfStatistics); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private static void setRaEvent(IpConnectivityEvent out, RaEvent in) { |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 273 | IpConnectivityLogClass.RaEvent raEvent = new IpConnectivityLogClass.RaEvent(); |
| 274 | raEvent.routerLifetime = in.routerLifetime; |
| 275 | raEvent.prefixValidLifetime = in.prefixValidLifetime; |
| 276 | raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime; |
| 277 | raEvent.routeInfoLifetime = in.routeInfoLifetime; |
| 278 | raEvent.rdnssLifetime = in.rdnssLifetime; |
| 279 | raEvent.dnsslLifetime = in.dnsslLifetime; |
| 280 | out.setRaEvent(raEvent); |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | private static int[] bytesToInts(byte[] in) { |
| 284 | final int[] out = new int[in.length]; |
| 285 | for (int i = 0; i < in.length; i++) { |
| 286 | out[i] = in[i] & 0xFF; |
| 287 | } |
| 288 | return out; |
| 289 | } |
| 290 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 291 | private static Pair[] toPairArray(SparseIntArray counts) { |
| 292 | final int s = counts.size(); |
| 293 | Pair[] pairs = new Pair[s]; |
| 294 | for (int i = 0; i < s; i++) { |
| 295 | Pair p = new Pair(); |
| 296 | p.key = counts.keyAt(i); |
| 297 | p.value = counts.valueAt(i); |
| 298 | pairs[i] = p; |
| 299 | } |
| 300 | return pairs; |
| 301 | } |
| 302 | |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 303 | private static NetworkId netIdOf(int netid) { |
| 304 | final NetworkId ni = new NetworkId(); |
| 305 | ni.networkId = netid; |
| 306 | return ni; |
| 307 | } |
| 308 | |
| 309 | private static int ipSupportOf(DefaultNetworkEvent in) { |
| 310 | if (in.prevIPv4 && in.prevIPv6) { |
| 311 | return IpConnectivityLogClass.DefaultNetworkEvent.DUAL; |
| 312 | } |
| 313 | if (in.prevIPv6) { |
| 314 | return IpConnectivityLogClass.DefaultNetworkEvent.IPV6; |
| 315 | } |
| 316 | if (in.prevIPv4) { |
| 317 | return IpConnectivityLogClass.DefaultNetworkEvent.IPV4; |
| 318 | } |
| 319 | return IpConnectivityLogClass.DefaultNetworkEvent.NONE; |
| 320 | } |
| 321 | |
| 322 | private static boolean isBitSet(int flags, int bit) { |
| 323 | return (flags & (1 << bit)) != 0; |
| 324 | } |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 325 | |
| 326 | private static void inferLinkLayer(IpConnectivityEvent ev) { |
| 327 | int linkLayer = IpConnectivityLogClass.UNKNOWN; |
| 328 | if (ev.transports != 0) { |
| 329 | linkLayer = transportsToLinkLayer(ev.transports); |
| 330 | } else if (ev.ifName != null) { |
| 331 | linkLayer = ifnameToLinkLayer(ev.ifName); |
| 332 | } |
| 333 | if (linkLayer == IpConnectivityLogClass.UNKNOWN) { |
| 334 | return; |
| 335 | } |
| 336 | ev.linkLayer = linkLayer; |
| 337 | ev.ifName = ""; |
| 338 | } |
| 339 | |
| 340 | private static int transportsToLinkLayer(long transports) { |
| 341 | switch (Long.bitCount(transports)) { |
| 342 | case 0: |
| 343 | return IpConnectivityLogClass.UNKNOWN; |
| 344 | case 1: |
| 345 | int t = Long.numberOfTrailingZeros(transports); |
| 346 | return transportToLinkLayer(t); |
| 347 | default: |
| 348 | return IpConnectivityLogClass.MULTIPLE; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | private static int transportToLinkLayer(int transport) { |
| 353 | if (0 <= transport && transport < TRANSPORT_LINKLAYER_MAP.length) { |
| 354 | return TRANSPORT_LINKLAYER_MAP[transport]; |
| 355 | } |
| 356 | return IpConnectivityLogClass.UNKNOWN; |
| 357 | } |
| 358 | |
| 359 | private static final int[] TRANSPORT_LINKLAYER_MAP = new int[MAX_TRANSPORT + 1]; |
| 360 | static { |
| 361 | TRANSPORT_LINKLAYER_MAP[TRANSPORT_CELLULAR] = IpConnectivityLogClass.CELLULAR; |
| 362 | TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI] = IpConnectivityLogClass.WIFI; |
| 363 | TRANSPORT_LINKLAYER_MAP[TRANSPORT_BLUETOOTH] = IpConnectivityLogClass.BLUETOOTH; |
| 364 | TRANSPORT_LINKLAYER_MAP[TRANSPORT_ETHERNET] = IpConnectivityLogClass.ETHERNET; |
| 365 | TRANSPORT_LINKLAYER_MAP[TRANSPORT_VPN] = IpConnectivityLogClass.UNKNOWN; |
Hugo Benichi | 27a5117 | 2017-07-18 11:35:18 +0900 | [diff] [blame^] | 366 | TRANSPORT_LINKLAYER_MAP[TRANSPORT_WIFI_AWARE] = IpConnectivityLogClass.WIFI_NAN; |
| 367 | TRANSPORT_LINKLAYER_MAP[TRANSPORT_LOWPAN] = IpConnectivityLogClass.LOWPAN; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | private static int ifnameToLinkLayer(String ifname) { |
| 371 | // Do not try to catch all interface names with regexes, instead only catch patterns that |
| 372 | // are cheap to check, and otherwise fallback on postprocessing in aggregation layer. |
Hugo Benichi | 27a5117 | 2017-07-18 11:35:18 +0900 | [diff] [blame^] | 373 | for (int i = 0; i < KNOWN_PREFIX; i++) { |
| 374 | String pattern = IFNAME_PREFIXES[i]; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 375 | if (ifname.startsWith(pattern)) { |
Hugo Benichi | 27a5117 | 2017-07-18 11:35:18 +0900 | [diff] [blame^] | 376 | return IFNAME_LINKLAYERS[i]; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | return IpConnectivityLogClass.UNKNOWN; |
| 380 | } |
| 381 | |
Hugo Benichi | 27a5117 | 2017-07-18 11:35:18 +0900 | [diff] [blame^] | 382 | private static final int KNOWN_PREFIX = 7; |
| 383 | private static final String[] IFNAME_PREFIXES = new String[KNOWN_PREFIX]; |
| 384 | private static final int[] IFNAME_LINKLAYERS = new int[KNOWN_PREFIX]; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 385 | static { |
Hugo Benichi | 27a5117 | 2017-07-18 11:35:18 +0900 | [diff] [blame^] | 386 | // Ordered from most likely link layer to least likely. |
| 387 | IFNAME_PREFIXES[0] = "rmnet"; |
| 388 | IFNAME_LINKLAYERS[0] = IpConnectivityLogClass.CELLULAR; |
| 389 | |
| 390 | IFNAME_PREFIXES[1] = "wlan"; |
| 391 | IFNAME_LINKLAYERS[1] = IpConnectivityLogClass.WIFI; |
| 392 | |
| 393 | IFNAME_PREFIXES[2] = "bt-pan"; |
| 394 | IFNAME_LINKLAYERS[2] = IpConnectivityLogClass.BLUETOOTH; |
| 395 | |
| 396 | IFNAME_PREFIXES[3] = "p2p"; |
| 397 | IFNAME_LINKLAYERS[3] = IpConnectivityLogClass.WIFI_P2P; |
| 398 | |
| 399 | IFNAME_PREFIXES[4] = "aware"; |
| 400 | IFNAME_LINKLAYERS[4] = IpConnectivityLogClass.WIFI_NAN; |
| 401 | |
| 402 | IFNAME_PREFIXES[5] = "eth"; |
| 403 | IFNAME_LINKLAYERS[5] = IpConnectivityLogClass.ETHERNET; |
| 404 | |
| 405 | IFNAME_PREFIXES[6] = "wpan"; |
| 406 | IFNAME_LINKLAYERS[6] = IpConnectivityLogClass.LOWPAN; |
Hugo Benichi | 73fd4d1 | 2017-03-15 23:05:01 +0900 | [diff] [blame] | 407 | } |
Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 408 | } |