1 /* 2 * Copyright 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 */ 16 17 package android.media.tv.tuner.filter; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.Size; 22 import android.annotation.SystemApi; 23 24 /** 25 * Filter configuration for a IP filter. 26 * 27 * @hide 28 */ 29 @SystemApi 30 public final class IpFilterConfiguration extends FilterConfiguration { 31 private final byte[] mSrcIpAddress; 32 private final byte[] mDstIpAddress; 33 private final int mSrcPort; 34 private final int mDstPort; 35 private final boolean mPassthrough; 36 IpFilterConfiguration(Settings settings, byte[] srcAddr, byte[] dstAddr, int srcPort, int dstPort, boolean passthrough)37 private IpFilterConfiguration(Settings settings, byte[] srcAddr, byte[] dstAddr, int srcPort, 38 int dstPort, boolean passthrough) { 39 super(settings); 40 mSrcIpAddress = srcAddr; 41 mDstIpAddress = dstAddr; 42 mSrcPort = srcPort; 43 mDstPort = dstPort; 44 mPassthrough = passthrough; 45 } 46 47 @Override getType()48 public int getType() { 49 return Filter.TYPE_IP; 50 } 51 52 /** 53 * Gets source IP address. 54 */ 55 @Size(min = 4, max = 16) 56 @NonNull getSrcIpAddress()57 public byte[] getSrcIpAddress() { 58 return mSrcIpAddress; 59 } 60 /** 61 * Gets destination IP address. 62 */ 63 @Size(min = 4, max = 16) 64 @NonNull getDstIpAddress()65 public byte[] getDstIpAddress() { 66 return mDstIpAddress; 67 } 68 /** 69 * Gets source port. 70 */ getSrcPort()71 public int getSrcPort() { 72 return mSrcPort; 73 } 74 /** 75 * Gets destination port. 76 */ getDstPort()77 public int getDstPort() { 78 return mDstPort; 79 } 80 /** 81 * Checks whether the filter is passthrough. 82 * 83 * @return {@code true} if the data from IP subtype go to next filter directly; 84 * {@code false} otherwise. 85 */ isPassthrough()86 public boolean isPassthrough() { 87 return mPassthrough; 88 } 89 90 /** 91 * Creates a builder for {@link IpFilterConfiguration}. 92 */ 93 @NonNull builder()94 public static Builder builder() { 95 return new Builder(); 96 } 97 98 /** 99 * Builder for {@link IpFilterConfiguration}. 100 */ 101 public static final class Builder { 102 private byte[] mSrcIpAddress = {0, 0, 0, 0}; 103 private byte[] mDstIpAddress = {0, 0, 0, 0}; 104 private int mSrcPort = 0; 105 private int mDstPort = 0; 106 private boolean mPassthrough = false; 107 private Settings mSettings; 108 Builder()109 private Builder() { 110 } 111 112 /** 113 * Sets source IP address. 114 * 115 * <p>Default value is 0.0.0.0, an invalid IP address. 116 */ 117 @NonNull setSrcIpAddress(@onNull byte[] srcIpAddress)118 public Builder setSrcIpAddress(@NonNull byte[] srcIpAddress) { 119 mSrcIpAddress = srcIpAddress; 120 return this; 121 } 122 /** 123 * Sets destination IP address. 124 * 125 * <p>Default value is 0.0.0.0, an invalid IP address. 126 */ 127 @NonNull setDstIpAddress(@onNull byte[] dstIpAddress)128 public Builder setDstIpAddress(@NonNull byte[] dstIpAddress) { 129 mDstIpAddress = dstIpAddress; 130 return this; 131 } 132 /** 133 * Sets source port. 134 * 135 * <p>Default value is 0. 136 */ 137 @NonNull setSrcPort(int srcPort)138 public Builder setSrcPort(int srcPort) { 139 mSrcPort = srcPort; 140 return this; 141 } 142 /** 143 * Sets destination port. 144 * 145 * <p>Default value is 0. 146 */ 147 @NonNull setDstPort(int dstPort)148 public Builder setDstPort(int dstPort) { 149 mDstPort = dstPort; 150 return this; 151 } 152 /** 153 * Sets passthrough. 154 * 155 * <p>Default value is {@code false}. 156 */ 157 @NonNull setPassthrough(boolean passthrough)158 public Builder setPassthrough(boolean passthrough) { 159 mPassthrough = passthrough; 160 return this; 161 } 162 163 /** 164 * Sets filter settings. 165 */ 166 @NonNull setSettings(@ullable Settings settings)167 public Builder setSettings(@Nullable Settings settings) { 168 mSettings = settings; 169 return this; 170 } 171 172 /** 173 * Builds a {@link IpFilterConfiguration} object. 174 */ 175 @NonNull build()176 public IpFilterConfiguration build() { 177 int ipAddrLength = mSrcIpAddress.length; 178 if (ipAddrLength != mDstIpAddress.length || (ipAddrLength != 4 && ipAddrLength != 16)) { 179 throw new IllegalArgumentException( 180 "The lengths of src and dst IP address must be 4 or 16 and must be the same." 181 + "srcLength=" + ipAddrLength + ", dstLength=" + mDstIpAddress.length); 182 } 183 return new IpFilterConfiguration( 184 mSettings, mSrcIpAddress, mDstIpAddress, mSrcPort, mDstPort, mPassthrough); 185 } 186 } 187 } 188