1 /* 2 * Copyright (C) 2017 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.location.cts.asn1.supl2.ulp_components; 18 19 /* 20 */ 21 22 23 // 24 // 25 import android.location.cts.asn1.base.Asn1Choice; 26 import android.location.cts.asn1.base.Asn1Null; 27 import android.location.cts.asn1.base.Asn1Object; 28 import android.location.cts.asn1.base.Asn1OctetString; 29 import android.location.cts.asn1.base.Asn1Tag; 30 import android.location.cts.asn1.base.BitStream; 31 import android.location.cts.asn1.base.BitStreamReader; 32 import android.location.cts.asn1.base.ChoiceComponent; 33 import com.google.common.collect.ImmutableList; 34 import java.nio.ByteBuffer; 35 import java.util.Collection; 36 import java.util.HashMap; 37 import java.util.Map; 38 import javax.annotation.Nullable; 39 40 41 /** 42 */ 43 public class IPAddress extends Asn1Choice { 44 // 45 46 private static final Asn1Tag TAG_IPAddress 47 = Asn1Tag.fromClassAndNumber(-1, -1); 48 49 private static final Map<Asn1Tag, Select> tagToSelection = new HashMap<>(); 50 51 private boolean extension; 52 private ChoiceComponent selection; 53 private Asn1Object element; 54 55 static { 56 for (Select select : Select.values()) { 57 for (Asn1Tag tag : select.getPossibleFirstTags()) { 58 Select select0; 59 if ((select0 = tagToSelection.put(tag, select)) != null) { 60 throw new IllegalStateException( 61 "IPAddress: " + tag + " maps to both " + select0 + " and " + select); 62 } 63 } 64 } 65 } 66 IPAddress()67 public IPAddress() { 68 super(); 69 } 70 71 @Override 72 @Nullable getTag()73 protected Asn1Tag getTag() { 74 return TAG_IPAddress; 75 } 76 77 @Override isTagImplicit()78 protected boolean isTagImplicit() { 79 return true; 80 } 81 getPossibleFirstTags()82 public static Collection<Asn1Tag> getPossibleFirstTags() { 83 if (TAG_IPAddress != null) { 84 return ImmutableList.of(TAG_IPAddress); 85 } else { 86 return tagToSelection.keySet(); 87 } 88 } 89 90 /** 91 * Creates a new IPAddress from encoded stream. 92 */ fromPerUnaligned(byte[] encodedBytes)93 public static IPAddress fromPerUnaligned(byte[] encodedBytes) { 94 IPAddress result = new IPAddress(); 95 result.decodePerUnaligned(new BitStreamReader(encodedBytes)); 96 return result; 97 } 98 99 /** 100 * Creates a new IPAddress from encoded stream. 101 */ fromPerAligned(byte[] encodedBytes)102 public static IPAddress fromPerAligned(byte[] encodedBytes) { 103 IPAddress result = new IPAddress(); 104 result.decodePerAligned(new BitStreamReader(encodedBytes)); 105 return result; 106 } 107 108 109 hasExtensionValue()110 @Override protected boolean hasExtensionValue() { 111 return extension; 112 } 113 getSelectionOrdinal()114 @Override protected Integer getSelectionOrdinal() { 115 return selection.ordinal(); 116 } 117 118 @Nullable 119 @Override getSelectedComponent()120 protected ChoiceComponent getSelectedComponent() { 121 return selection; 122 } 123 getOptionCount()124 @Override protected int getOptionCount() { 125 if (hasExtensionValue()) { 126 return Extend.values().length; 127 } 128 return Select.values().length; 129 } 130 createAndSetValue(boolean isExtensionValue, int ordinal)131 protected Asn1Object createAndSetValue(boolean isExtensionValue, 132 int ordinal) { 133 extension = isExtensionValue; 134 if (isExtensionValue) { 135 selection = Extend.values()[ordinal]; 136 } else { 137 selection = Select.values()[ordinal]; 138 } 139 element = selection.createElement(); 140 return element; 141 } 142 createAndSetValue(Asn1Tag tag)143 @Override protected ChoiceComponent createAndSetValue(Asn1Tag tag) { 144 Select select = tagToSelection.get(tag); 145 if (select == null) { 146 throw new IllegalArgumentException("Unknown selection tag: " + tag); 147 } 148 element = select.createElement(); 149 selection = select; 150 extension = false; 151 return select; 152 } 153 isExtensible()154 @Override protected boolean isExtensible() { 155 return false; 156 } 157 getValue()158 @Override protected Asn1Object getValue() { 159 return element; 160 } 161 162 163 private static enum Select implements ChoiceComponent { 164 165 $Ipv4Address(Asn1Tag.fromClassAndNumber(2, 0), 166 true) { 167 @Override createElement()168 public Asn1Object createElement() { 169 return new IPAddress.ipv4AddressType(); 170 } 171 172 @Override getPossibleFirstTags()173 Collection<Asn1Tag> getPossibleFirstTags() { 174 return tag == null ? IPAddress.ipv4AddressType.getPossibleFirstTags() : ImmutableList.of(tag); 175 } 176 177 @Override elementIndentedString(Asn1Object element, String indent)178 String elementIndentedString(Asn1Object element, String indent) { 179 return toString() + " : " + element.toIndentedString(indent); 180 } 181 }, 182 183 $Ipv6Address(Asn1Tag.fromClassAndNumber(2, 1), 184 true) { 185 @Override createElement()186 public Asn1Object createElement() { 187 return new IPAddress.ipv6AddressType(); 188 } 189 190 @Override getPossibleFirstTags()191 Collection<Asn1Tag> getPossibleFirstTags() { 192 return tag == null ? IPAddress.ipv6AddressType.getPossibleFirstTags() : ImmutableList.of(tag); 193 } 194 195 @Override elementIndentedString(Asn1Object element, String indent)196 String elementIndentedString(Asn1Object element, String indent) { 197 return toString() + " : " + element.toIndentedString(indent); 198 } 199 }, 200 201 ; 202 203 @Nullable final Asn1Tag tag; 204 final boolean isImplicitTagging; 205 Select(@ullable Asn1Tag tag, boolean isImplicitTagging)206 Select(@Nullable Asn1Tag tag, boolean isImplicitTagging) { 207 this.tag = tag; 208 this.isImplicitTagging = isImplicitTagging; 209 } 210 211 @Override createElement()212 public Asn1Object createElement() { 213 throw new IllegalStateException("Select template error"); 214 } 215 216 @Override 217 @Nullable getTag()218 public Asn1Tag getTag() { 219 return tag; 220 } 221 222 @Override isImplicitTagging()223 public boolean isImplicitTagging() { 224 return isImplicitTagging; 225 } 226 getPossibleFirstTags()227 abstract Collection<Asn1Tag> getPossibleFirstTags(); 228 elementIndentedString(Asn1Object element, String indent)229 abstract String elementIndentedString(Asn1Object element, String indent); 230 } 231 232 /* 233 */ 234 235 236 // 237 238 /** 239 */ 240 public static class ipv4AddressType extends Asn1OctetString { 241 // 242 243 private static final Asn1Tag TAG_ipv4AddressType 244 = Asn1Tag.fromClassAndNumber(-1, -1); 245 ipv4AddressType()246 public ipv4AddressType() { 247 super(); 248 setMinSize(4); 249 setMaxSize(4); 250 251 } 252 253 @Override 254 @Nullable getTag()255 protected Asn1Tag getTag() { 256 return TAG_ipv4AddressType; 257 } 258 259 @Override isTagImplicit()260 protected boolean isTagImplicit() { 261 return true; 262 } 263 getPossibleFirstTags()264 public static Collection<Asn1Tag> getPossibleFirstTags() { 265 if (TAG_ipv4AddressType != null) { 266 return ImmutableList.of(TAG_ipv4AddressType); 267 } else { 268 return Asn1OctetString.getPossibleFirstTags(); 269 } 270 } 271 272 /** 273 * Creates a new ipv4AddressType from encoded stream. 274 */ fromPerUnaligned(byte[] encodedBytes)275 public static ipv4AddressType fromPerUnaligned(byte[] encodedBytes) { 276 ipv4AddressType result = new ipv4AddressType(); 277 result.decodePerUnaligned(new BitStreamReader(encodedBytes)); 278 return result; 279 } 280 281 /** 282 * Creates a new ipv4AddressType from encoded stream. 283 */ fromPerAligned(byte[] encodedBytes)284 public static ipv4AddressType fromPerAligned(byte[] encodedBytes) { 285 ipv4AddressType result = new ipv4AddressType(); 286 result.decodePerAligned(new BitStreamReader(encodedBytes)); 287 return result; 288 } 289 encodePerUnaligned()290 @Override public Iterable<BitStream> encodePerUnaligned() { 291 return super.encodePerUnaligned(); 292 } 293 encodePerAligned()294 @Override public Iterable<BitStream> encodePerAligned() { 295 return super.encodePerAligned(); 296 } 297 decodePerUnaligned(BitStreamReader reader)298 @Override public void decodePerUnaligned(BitStreamReader reader) { 299 super.decodePerUnaligned(reader); 300 } 301 decodePerAligned(BitStreamReader reader)302 @Override public void decodePerAligned(BitStreamReader reader) { 303 super.decodePerAligned(reader); 304 } 305 getTypeName()306 @Override protected String getTypeName() { 307 return "ipv4AddressType"; 308 } 309 } 310 311 isIpv4Address()312 public boolean isIpv4Address() { 313 return !hasExtensionValue() && Select.$Ipv4Address == selection; 314 } 315 316 /** 317 * @throws {@code IllegalStateException} if {@code !isIpv4Address}. 318 */ 319 @SuppressWarnings("unchecked") getIpv4Address()320 public IPAddress.ipv4AddressType getIpv4Address() { 321 if (!isIpv4Address()) { 322 throw new IllegalStateException("IPAddress value not a Ipv4Address"); 323 } 324 return (IPAddress.ipv4AddressType) element; 325 } 326 setIpv4Address(IPAddress.ipv4AddressType selected)327 public void setIpv4Address(IPAddress.ipv4AddressType selected) { 328 selection = Select.$Ipv4Address; 329 extension = false; 330 element = selected; 331 } 332 setIpv4AddressToNewInstance()333 public IPAddress.ipv4AddressType setIpv4AddressToNewInstance() { 334 IPAddress.ipv4AddressType element = new IPAddress.ipv4AddressType(); 335 setIpv4Address(element); 336 return element; 337 } 338 339 /* 340 */ 341 342 343 // 344 345 /** 346 */ 347 public static class ipv6AddressType extends Asn1OctetString { 348 // 349 350 private static final Asn1Tag TAG_ipv6AddressType 351 = Asn1Tag.fromClassAndNumber(-1, -1); 352 ipv6AddressType()353 public ipv6AddressType() { 354 super(); 355 setMinSize(16); 356 setMaxSize(16); 357 358 } 359 360 @Override 361 @Nullable getTag()362 protected Asn1Tag getTag() { 363 return TAG_ipv6AddressType; 364 } 365 366 @Override isTagImplicit()367 protected boolean isTagImplicit() { 368 return true; 369 } 370 getPossibleFirstTags()371 public static Collection<Asn1Tag> getPossibleFirstTags() { 372 if (TAG_ipv6AddressType != null) { 373 return ImmutableList.of(TAG_ipv6AddressType); 374 } else { 375 return Asn1OctetString.getPossibleFirstTags(); 376 } 377 } 378 379 /** 380 * Creates a new ipv6AddressType from encoded stream. 381 */ fromPerUnaligned(byte[] encodedBytes)382 public static ipv6AddressType fromPerUnaligned(byte[] encodedBytes) { 383 ipv6AddressType result = new ipv6AddressType(); 384 result.decodePerUnaligned(new BitStreamReader(encodedBytes)); 385 return result; 386 } 387 388 /** 389 * Creates a new ipv6AddressType from encoded stream. 390 */ fromPerAligned(byte[] encodedBytes)391 public static ipv6AddressType fromPerAligned(byte[] encodedBytes) { 392 ipv6AddressType result = new ipv6AddressType(); 393 result.decodePerAligned(new BitStreamReader(encodedBytes)); 394 return result; 395 } 396 encodePerUnaligned()397 @Override public Iterable<BitStream> encodePerUnaligned() { 398 return super.encodePerUnaligned(); 399 } 400 encodePerAligned()401 @Override public Iterable<BitStream> encodePerAligned() { 402 return super.encodePerAligned(); 403 } 404 decodePerUnaligned(BitStreamReader reader)405 @Override public void decodePerUnaligned(BitStreamReader reader) { 406 super.decodePerUnaligned(reader); 407 } 408 decodePerAligned(BitStreamReader reader)409 @Override public void decodePerAligned(BitStreamReader reader) { 410 super.decodePerAligned(reader); 411 } 412 getTypeName()413 @Override protected String getTypeName() { 414 return "ipv6AddressType"; 415 } 416 } 417 418 isIpv6Address()419 public boolean isIpv6Address() { 420 return !hasExtensionValue() && Select.$Ipv6Address == selection; 421 } 422 423 /** 424 * @throws {@code IllegalStateException} if {@code !isIpv6Address}. 425 */ 426 @SuppressWarnings("unchecked") getIpv6Address()427 public IPAddress.ipv6AddressType getIpv6Address() { 428 if (!isIpv6Address()) { 429 throw new IllegalStateException("IPAddress value not a Ipv6Address"); 430 } 431 return (IPAddress.ipv6AddressType) element; 432 } 433 setIpv6Address(IPAddress.ipv6AddressType selected)434 public void setIpv6Address(IPAddress.ipv6AddressType selected) { 435 selection = Select.$Ipv6Address; 436 extension = false; 437 element = selected; 438 } 439 setIpv6AddressToNewInstance()440 public IPAddress.ipv6AddressType setIpv6AddressToNewInstance() { 441 IPAddress.ipv6AddressType element = new IPAddress.ipv6AddressType(); 442 setIpv6Address(element); 443 return element; 444 } 445 446 447 private static enum Extend implements ChoiceComponent { 448 449 ; 450 @Nullable private final Asn1Tag tag; 451 private final boolean isImplicitTagging; 452 Extend(@ullable Asn1Tag tag, boolean isImplicitTagging)453 Extend(@Nullable Asn1Tag tag, boolean isImplicitTagging) { 454 this.tag = tag; 455 this.isImplicitTagging = isImplicitTagging; 456 } 457 createElement()458 public Asn1Object createElement() { 459 throw new IllegalStateException("Extend template error"); 460 } 461 462 @Override 463 @Nullable getTag()464 public Asn1Tag getTag() { 465 return tag; 466 } 467 468 @Override isImplicitTagging()469 public boolean isImplicitTagging() { 470 return isImplicitTagging; 471 } 472 elementIndentedString(Asn1Object element, String indent)473 String elementIndentedString(Asn1Object element, String indent) { 474 throw new IllegalStateException("Extend template error"); 475 } 476 } 477 478 encodePerUnaligned()479 @Override public Iterable<BitStream> encodePerUnaligned() { 480 return super.encodePerUnaligned(); 481 } 482 encodePerAligned()483 @Override public Iterable<BitStream> encodePerAligned() { 484 return super.encodePerAligned(); 485 } 486 decodePerUnaligned(BitStreamReader reader)487 @Override public void decodePerUnaligned(BitStreamReader reader) { 488 super.decodePerUnaligned(reader); 489 } 490 decodePerAligned(BitStreamReader reader)491 @Override public void decodePerAligned(BitStreamReader reader) { 492 super.decodePerAligned(reader); 493 } 494 toString()495 @Override public String toString() { 496 return toIndentedString(""); 497 } 498 elementIndentedString(String indent)499 private String elementIndentedString(String indent) { 500 if (element == null) { 501 return "null;\n"; 502 } 503 if (extension) { 504 return Extend.values()[selection.ordinal()] 505 .elementIndentedString(element, indent + " "); 506 } else { 507 return Select.values()[selection.ordinal()] 508 .elementIndentedString(element, indent + " "); 509 } 510 } 511 toIndentedString(String indent)512 public String toIndentedString(String indent) { 513 return "IPAddress = " + elementIndentedString(indent) + indent + ";\n"; 514 } 515 } 516