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