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