blob: d308246f4d775f85f5a6a71331e3d2ee5d275dde [file] [log] [blame]
Hugo Benichicfbf7412016-06-23 10:41:30 +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 android.net.metrics;
18
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010019import android.annotation.UnsupportedAppUsage;
Hugo Benichicfbf7412016-06-23 10:41:30 +090020import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * An event logged when the APF packet socket receives an RA packet.
25 * {@hide}
26 */
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090027public final class RaEvent implements IpConnectivityLog.Event {
Hugo Benichicfbf7412016-06-23 10:41:30 +090028
Hugo Benichi6ccd51a2016-07-04 09:22:30 +090029 public static final long NO_LIFETIME = -1L;
30
Hugo Benichicfbf7412016-06-23 10:41:30 +090031 // Lifetime in seconds of options found in a single RA packet.
32 // When an option is not set, the value of the associated field is -1;
33 public final long routerLifetime;
34 public final long prefixValidLifetime;
35 public final long prefixPreferredLifetime;
36 public final long routeInfoLifetime;
37 public final long rdnssLifetime;
38 public final long dnsslLifetime;
39
Hugo Benichicfbf7412016-06-23 10:41:30 +090040 public RaEvent(long routerLifetime, long prefixValidLifetime, long prefixPreferredLifetime,
41 long routeInfoLifetime, long rdnssLifetime, long dnsslLifetime) {
42 this.routerLifetime = routerLifetime;
43 this.prefixValidLifetime = prefixValidLifetime;
44 this.prefixPreferredLifetime = prefixPreferredLifetime;
45 this.routeInfoLifetime = routeInfoLifetime;
46 this.rdnssLifetime = rdnssLifetime;
47 this.dnsslLifetime = dnsslLifetime;
48 }
49
50 private RaEvent(Parcel in) {
51 routerLifetime = in.readLong();
52 prefixValidLifetime = in.readLong();
53 prefixPreferredLifetime = in.readLong();
54 routeInfoLifetime = in.readLong();
55 rdnssLifetime = in.readLong();
56 dnsslLifetime = in.readLong();
57 }
58
59 @Override
60 public void writeToParcel(Parcel out, int flags) {
61 out.writeLong(routerLifetime);
62 out.writeLong(prefixValidLifetime);
63 out.writeLong(prefixPreferredLifetime);
64 out.writeLong(routeInfoLifetime);
65 out.writeLong(rdnssLifetime);
66 out.writeLong(dnsslLifetime);
67 }
68
69 @Override
70 public int describeContents() {
71 return 0;
72 }
73
74 @Override
75 public String toString() {
76 return new StringBuilder("RaEvent(lifetimes: ")
77 .append(String.format("router=%ds, ", routerLifetime))
78 .append(String.format("prefix_valid=%ds, ", prefixValidLifetime))
79 .append(String.format("prefix_preferred=%ds, ", prefixPreferredLifetime))
80 .append(String.format("route_info=%ds, ", routeInfoLifetime))
81 .append(String.format("rdnss=%ds, ", rdnssLifetime))
82 .append(String.format("dnssl=%ds)", dnsslLifetime))
83 .toString();
84 }
85
86 public static final Parcelable.Creator<RaEvent> CREATOR = new Parcelable.Creator<RaEvent>() {
87 public RaEvent createFromParcel(Parcel in) {
88 return new RaEvent(in);
89 }
90
91 public RaEvent[] newArray(int size) {
92 return new RaEvent[size];
93 }
94 };
Hugo Benichi6ccd51a2016-07-04 09:22:30 +090095
Hugo Benichi6ccd51a2016-07-04 09:22:30 +090096 public static class Builder {
97
98 long routerLifetime = NO_LIFETIME;
99 long prefixValidLifetime = NO_LIFETIME;
100 long prefixPreferredLifetime = NO_LIFETIME;
101 long routeInfoLifetime = NO_LIFETIME;
102 long rdnssLifetime = NO_LIFETIME;
103 long dnsslLifetime = NO_LIFETIME;
104
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100105 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900106 public Builder() {
107 }
108
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100109 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900110 public RaEvent build() {
111 return new RaEvent(routerLifetime, prefixValidLifetime, prefixPreferredLifetime,
112 routeInfoLifetime, rdnssLifetime, dnsslLifetime);
113 }
114
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100115 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900116 public Builder updateRouterLifetime(long lifetime) {
117 routerLifetime = updateLifetime(routerLifetime, lifetime);
118 return this;
119 }
120
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100121 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900122 public Builder updatePrefixValidLifetime(long lifetime) {
123 prefixValidLifetime = updateLifetime(prefixValidLifetime, lifetime);
124 return this;
125 }
126
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100127 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900128 public Builder updatePrefixPreferredLifetime(long lifetime) {
129 prefixPreferredLifetime = updateLifetime(prefixPreferredLifetime, lifetime);
130 return this;
131 }
132
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100133 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900134 public Builder updateRouteInfoLifetime(long lifetime) {
135 routeInfoLifetime = updateLifetime(routeInfoLifetime, lifetime);
136 return this;
137 }
138
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100139 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900140 public Builder updateRdnssLifetime(long lifetime) {
141 rdnssLifetime = updateLifetime(rdnssLifetime, lifetime);
142 return this;
143 }
144
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100145 @UnsupportedAppUsage
Hugo Benichi6ccd51a2016-07-04 09:22:30 +0900146 public Builder updateDnsslLifetime(long lifetime) {
147 dnsslLifetime = updateLifetime(dnsslLifetime, lifetime);
148 return this;
149 }
150
151 private long updateLifetime(long currentLifetime, long newLifetime) {
152 if (currentLifetime == RaEvent.NO_LIFETIME) {
153 return newLifetime;
154 }
155 return Math.min(currentLifetime, newLifetime);
156 }
157 }
Hugo Benichicfbf7412016-06-23 10:41:30 +0900158}