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