blob: b17b4ba77cfb9980b59ad756c3aa1c7aaa8ef3f1 [file] [log] [blame]
markchienf87ebdc2019-12-07 22:02:28 +08001/*
2 * Copyright (C) 2019 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 */
16package android.net.util;
17
Hungming Chend0216992020-05-08 17:50:53 +080018import android.net.TetherStatsParcel;
markchienf053e4b2020-03-16 21:49:48 +080019import android.net.TetheringRequestParcel;
20
Hungming Chend0216992020-05-08 17:50:53 +080021import androidx.annotation.NonNull;
22
markchienf87ebdc2019-12-07 22:02:28 +080023import java.io.FileDescriptor;
24import java.net.SocketException;
markchienf053e4b2020-03-16 21:49:48 +080025import java.util.Objects;
markchienf87ebdc2019-12-07 22:02:28 +080026
27/**
Hungming Chend0216992020-05-08 17:50:53 +080028 * The classes and the methods for tethering utilization.
markchienf87ebdc2019-12-07 22:02:28 +080029 *
30 * {@hide}
31 */
32public class TetheringUtils {
markchienf87ebdc2019-12-07 22:02:28 +080033 /**
Hungming Chend0216992020-05-08 17:50:53 +080034 * The object which records offload Tx/Rx forwarded bytes/packets.
35 * TODO: Replace the inner class ForwardedStats of class OffloadHardwareInterface with
36 * this class as well.
37 */
38 public static class ForwardedStats {
39 public final long rxBytes;
40 public final long rxPackets;
41 public final long txBytes;
42 public final long txPackets;
43
44 public ForwardedStats() {
45 rxBytes = 0;
46 rxPackets = 0;
47 txBytes = 0;
48 txPackets = 0;
49 }
50
51 public ForwardedStats(long rxBytes, long txBytes) {
52 this.rxBytes = rxBytes;
53 this.rxPackets = 0;
54 this.txBytes = txBytes;
55 this.txPackets = 0;
56 }
57
58 public ForwardedStats(long rxBytes, long rxPackets, long txBytes, long txPackets) {
59 this.rxBytes = rxBytes;
60 this.rxPackets = rxPackets;
61 this.txBytes = txBytes;
62 this.txPackets = txPackets;
63 }
64
65 public ForwardedStats(@NonNull TetherStatsParcel tetherStats) {
66 rxBytes = tetherStats.rxBytes;
67 rxPackets = tetherStats.rxPackets;
68 txBytes = tetherStats.txBytes;
69 txPackets = tetherStats.txPackets;
70 }
71
72 public ForwardedStats(@NonNull ForwardedStats other) {
73 rxBytes = other.rxBytes;
74 rxPackets = other.rxPackets;
75 txBytes = other.txBytes;
76 txPackets = other.txPackets;
77 }
78
79 /** Add Tx/Rx bytes/packets and return the result as a new object. */
80 @NonNull
81 public ForwardedStats add(@NonNull ForwardedStats other) {
82 return new ForwardedStats(rxBytes + other.rxBytes, rxPackets + other.rxPackets,
83 txBytes + other.txBytes, txPackets + other.txPackets);
84 }
85
86 /** Subtract Tx/Rx bytes/packets and return the result as a new object. */
87 @NonNull
88 public ForwardedStats subtract(@NonNull ForwardedStats other) {
89 // TODO: Perhaps throw an exception if any negative difference value just in case.
90 final long rxBytesDiff = Math.max(rxBytes - other.rxBytes, 0);
91 final long rxPacketsDiff = Math.max(rxPackets - other.rxPackets, 0);
92 final long txBytesDiff = Math.max(txBytes - other.txBytes, 0);
93 final long txPacketsDiff = Math.max(txPackets - other.txPackets, 0);
94 return new ForwardedStats(rxBytesDiff, rxPacketsDiff, txBytesDiff, txPacketsDiff);
95 }
96
97 /** Returns the string representation of this object. */
98 @NonNull
99 public String toString() {
100 return String.format("ForwardedStats(rxb: %d, rxp: %d, txb: %d, txp: %d)", rxBytes,
101 rxPackets, txBytes, txPackets);
102 }
103 }
104
105 /**
markchienf87ebdc2019-12-07 22:02:28 +0800106 * Configures a socket for receiving ICMPv6 router solicitations and sending advertisements.
107 * @param fd the socket's {@link FileDescriptor}.
108 * @param ifIndex the interface index.
109 */
110 public static native void setupRaSocket(FileDescriptor fd, int ifIndex)
111 throws SocketException;
markchien6cf0e552019-12-06 15:24:53 +0800112
113 /**
114 * Read s as an unsigned 16-bit integer.
115 */
116 public static int uint16(short s) {
117 return s & 0xffff;
118 }
markchienf053e4b2020-03-16 21:49:48 +0800119
120 /** Check whether two TetheringRequestParcels are the same. */
121 public static boolean isTetheringRequestEquals(final TetheringRequestParcel request,
122 final TetheringRequestParcel otherRequest) {
123 if (request == otherRequest) return true;
124
125 return request != null && otherRequest != null
126 && request.tetheringType == otherRequest.tetheringType
127 && Objects.equals(request.localIPv4Address, otherRequest.localIPv4Address)
128 && Objects.equals(request.staticClientAddress, otherRequest.staticClientAddress)
129 && request.exemptFromEntitlementCheck == otherRequest.exemptFromEntitlementCheck
130 && request.showProvisioningUi == otherRequest.showProvisioningUi;
131 }
markchienf87ebdc2019-12-07 22:02:28 +0800132}