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.IntRange; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.Size; 23 import android.annotation.SystemApi; 24 import android.media.tv.tuner.TunerVersionChecker; 25 26 /** 27 * Filter configuration for a IP filter. 28 * 29 * @hide 30 */ 31 @SystemApi 32 public final class IpFilterConfiguration extends FilterConfiguration { 33 /** 34 * Undefined filter type. 35 */ 36 public static final int INVALID_IP_FILTER_CONTEXT_ID = 37 android.hardware.tv.tuner.V1_1.Constants.Constant.INVALID_IP_FILTER_CONTEXT_ID; 38 39 private final byte[] mSrcIpAddress; 40 private final byte[] mDstIpAddress; 41 private final int mSrcPort; 42 private final int mDstPort; 43 private final boolean mPassthrough; 44 private final int mIpFilterContextId; 45 IpFilterConfiguration(Settings settings, byte[] srcAddr, byte[] dstAddr, int srcPort, int dstPort, boolean passthrough, int ipCid)46 private IpFilterConfiguration(Settings settings, byte[] srcAddr, byte[] dstAddr, int srcPort, 47 int dstPort, boolean passthrough, int ipCid) { 48 super(settings); 49 mSrcIpAddress = srcAddr; 50 mDstIpAddress = dstAddr; 51 mSrcPort = srcPort; 52 mDstPort = dstPort; 53 mPassthrough = passthrough; 54 mIpFilterContextId = ipCid; 55 } 56 57 @Override getType()58 public int getType() { 59 return Filter.TYPE_IP; 60 } 61 62 /** 63 * Gets source IP address. 64 */ 65 @Size(min = 4, max = 16) 66 @NonNull getSrcIpAddress()67 public byte[] getSrcIpAddress() { 68 return mSrcIpAddress; 69 } 70 /** 71 * Gets destination IP address. 72 */ 73 @Size(min = 4, max = 16) 74 @NonNull getDstIpAddress()75 public byte[] getDstIpAddress() { 76 return mDstIpAddress; 77 } 78 /** 79 * Gets source port. 80 */ getSrcPort()81 public int getSrcPort() { 82 return mSrcPort; 83 } 84 /** 85 * Gets destination port. 86 */ getDstPort()87 public int getDstPort() { 88 return mDstPort; 89 } 90 /** 91 * Checks whether the filter is passthrough. 92 * 93 * @return {@code true} if the data from IP subtype go to next filter directly; 94 * {@code false} otherwise. 95 */ isPassthrough()96 public boolean isPassthrough() { 97 return mPassthrough; 98 } 99 /** 100 * Gets the ip filter context id. Default value is {@link #INVALID_IP_FILTER_CONTEXT_ID}. 101 * 102 * <p>This API is only supported by Tuner HAL 1.1 or higher. Unsupported version would return 103 * default value. Use {@link TunerVersionChecker#getTunerVersion()} to check the version. 104 */ 105 @IntRange(from = 0, to = 0xefff) getIpFilterContextId()106 public int getIpFilterContextId() { 107 return mIpFilterContextId; 108 } 109 110 /** 111 * Creates a builder for {@link IpFilterConfiguration}. 112 */ 113 @NonNull builder()114 public static Builder builder() { 115 return new Builder(); 116 } 117 118 /** 119 * Builder for {@link IpFilterConfiguration}. 120 */ 121 public static final class Builder { 122 private byte[] mSrcIpAddress = {0, 0, 0, 0}; 123 private byte[] mDstIpAddress = {0, 0, 0, 0}; 124 private int mSrcPort = 0; 125 private int mDstPort = 0; 126 private boolean mPassthrough = false; 127 private Settings mSettings; 128 private int mIpCid = INVALID_IP_FILTER_CONTEXT_ID; 129 Builder()130 private Builder() { 131 } 132 133 /** 134 * Sets source IP address. 135 * 136 * <p>Default value is 0.0.0.0, an invalid IP address. 137 */ 138 @NonNull setSrcIpAddress(@onNull byte[] srcIpAddress)139 public Builder setSrcIpAddress(@NonNull byte[] srcIpAddress) { 140 mSrcIpAddress = srcIpAddress; 141 return this; 142 } 143 /** 144 * Sets destination IP address. 145 * 146 * <p>Default value is 0.0.0.0, an invalid IP address. 147 */ 148 @NonNull setDstIpAddress(@onNull byte[] dstIpAddress)149 public Builder setDstIpAddress(@NonNull byte[] dstIpAddress) { 150 mDstIpAddress = dstIpAddress; 151 return this; 152 } 153 /** 154 * Sets source port. 155 * 156 * <p>Default value is 0. 157 */ 158 @NonNull setSrcPort(int srcPort)159 public Builder setSrcPort(int srcPort) { 160 mSrcPort = srcPort; 161 return this; 162 } 163 /** 164 * Sets destination port. 165 * 166 * <p>Default value is 0. 167 */ 168 @NonNull setDstPort(int dstPort)169 public Builder setDstPort(int dstPort) { 170 mDstPort = dstPort; 171 return this; 172 } 173 /** 174 * Sets passthrough. 175 * 176 * <p>Default value is {@code false}. 177 */ 178 @NonNull setPassthrough(boolean passthrough)179 public Builder setPassthrough(boolean passthrough) { 180 mPassthrough = passthrough; 181 return this; 182 } 183 184 /** 185 * Sets filter settings. 186 */ 187 @NonNull setSettings(@ullable Settings settings)188 public Builder setSettings(@Nullable Settings settings) { 189 mSettings = settings; 190 return this; 191 } 192 193 /** 194 * Sets the ip filter context id. Default value is {@link #INVALID_IP_FILTER_CONTEXT_ID}. 195 * 196 * <p>This API is only supported by Tuner HAL 1.1 or higher. Unsupported version would cause 197 * no-op. Use {@link TunerVersionChecker#getTunerVersion()} to check the version. 198 */ 199 @NonNull setIpFilterContextId(int ipContextId)200 public Builder setIpFilterContextId(int ipContextId) { 201 if (TunerVersionChecker.checkHigherOrEqualVersionTo( 202 TunerVersionChecker.TUNER_VERSION_1_1, "setIpFilterContextId")) { 203 mIpCid = ipContextId; 204 } 205 return this; 206 } 207 208 /** 209 * Builds a {@link IpFilterConfiguration} object. 210 */ 211 @NonNull build()212 public IpFilterConfiguration build() { 213 int ipAddrLength = mSrcIpAddress.length; 214 if (ipAddrLength != mDstIpAddress.length || (ipAddrLength != 4 && ipAddrLength != 16)) { 215 throw new IllegalArgumentException( 216 "The lengths of src and dst IP address must be 4 or 16 and must be the same." 217 + "srcLength=" + ipAddrLength + ", dstLength=" + mDstIpAddress.length); 218 } 219 return new IpFilterConfiguration(mSettings, mSrcIpAddress, mDstIpAddress, mSrcPort, 220 mDstPort, mPassthrough, mIpCid); 221 } 222 } 223 } 224