Package org.nfctools.ndef.wkt.handover.records

Examples of org.nfctools.ndef.wkt.handover.records.HandoverRequestRecord


public class HandoverRequestRecordDecoder implements WellKnownRecordPayloadDecoder {

  @Override
  public WellKnownRecord decodePayload(byte[] payload, NdefMessageDecoder messageDecoder) {

    HandoverRequestRecord handoverRequestRecord = new HandoverRequestRecord();

    byte minorVersion = (byte)(payload[0] & 0x0F);
    byte majorVersion = (byte)((payload[0] >> 4) & 0x0F);

    handoverRequestRecord.setMinorVersion(minorVersion);
    handoverRequestRecord.setMajorVersion(majorVersion);

    List<Record> records = messageDecoder.decodeToRecords(payload, 1, payload.length - 1);

    if (records.isEmpty()) {
      throw new IllegalArgumentException(
          "Expected collision resolution record and at least one alternative carrier");
    }

    for (int i = 0; i < records.size(); i++) {
      Record record = records.get(i);
      if (record instanceof CollisionResolutionRecord) {
        handoverRequestRecord.setCollisionResolution((CollisionResolutionRecord)record);
      }
      else if (record instanceof AlternativeCarrierRecord)
        handoverRequestRecord.add((AlternativeCarrierRecord)records.get(i));
      // An implementation SHALL silently ignore and SHALL NOT raise an error
      // if it encounters other unknown record types.
    }

    if (handoverRequestRecord.getAlternativeCarriers().size() == 0)
      throw new IllegalArgumentException("Expected at least one alternative carrier");

    return handoverRequestRecord;
  }
View Full Code Here


  @Test
  public void testBluetoothHandoverRequest10() throws Exception {
    List<Record> records = decoder.decodeToRecords(NfcUtils.convertASCIIToBin(BLUETOOTH_HANDOVER_REQUEST_MESSAGE));

    HandoverRequestRecord handoverRequestRecord = (HandoverRequestRecord)records.get(0);

    assertEquals(1, handoverRequestRecord.getMajorVersion());
    assertEquals(0, handoverRequestRecord.getMinorVersion());
    assertNull(handoverRequestRecord.getCollisionResolution());

    assertEquals(1, handoverRequestRecord.getAlternativeCarriers().size());

    AlternativeCarrierRecord alternativeCarrierRecord = handoverRequestRecord.getAlternativeCarriers().get(0);
    assertEquals(CarrierPowerState.Active, alternativeCarrierRecord.getCarrierPowerState());
    assertEquals("0", alternativeCarrierRecord.getCarrierDataReference());
    assertEquals(0, alternativeCarrierRecord.getAuxiliaryDataReferences().size());

    BinaryMimeRecord binaryMimeRecord = (BinaryMimeRecord)records.get(1);
View Full Code Here

    byte[] message = getResource("/handover/1.2/BluetoothHandoverRequestMessage.bin");

    List<Record> records = decoder.decodeToRecords(message);
    assertEquals(2, records.size());

    HandoverRequestRecord handoverRequestRecord = (HandoverRequestRecord)records.get(0);

    assertEquals(1, handoverRequestRecord.getMajorVersion());
    assertEquals(2, handoverRequestRecord.getMinorVersion());

    CollisionResolutionRecord collisionResolution = handoverRequestRecord.getCollisionResolution();
    assertEquals((0x01 << 8) | 0x02, collisionResolution.getRandomNumber());

    List<AlternativeCarrierRecord> alternativeCarriers = handoverRequestRecord.getAlternativeCarriers();
    assertEquals(1, alternativeCarriers.size());

    AlternativeCarrierRecord alternativeCarrierRecord = alternativeCarriers.get(0);
    assertEquals("0", alternativeCarrierRecord.getCarrierDataReference());
    assertFalse(alternativeCarrierRecord.hasAuxiliaryDataReferences());
View Full Code Here

public class HandoverRequestRecordEncoder implements WellKnownRecordPayloadEncoder {

  @Override
  public byte[] encodePayload(WellKnownRecord wellKnownRecord, NdefMessageEncoder messageEncoder) {

    HandoverRequestRecord handoverRequestRecord = (HandoverRequestRecord)wellKnownRecord;

    ByteArrayOutputStream payload = new ByteArrayOutputStream();

    // major version, minor version
    payload.write((handoverRequestRecord.getMajorVersion() << 4) | handoverRequestRecord.getMinorVersion());

    if (!handoverRequestRecord.hasCollisionResolution()) {
      throw new NdefEncoderException("Expected collision resolution", handoverRequestRecord);
    }

    // implementation note: write alternative carriers and and collision resolution together
    if (!handoverRequestRecord.hasAlternativeCarriers()) {
      // At least a single alternative carrier MUST be specified by the Handover Requester.
      throw new NdefEncoderException("Expected at least one alternative carrier", handoverRequestRecord);
    }
    List<Record> records = new ArrayList<Record>();

    // a collision resolution record
    records.add(handoverRequestRecord.getCollisionResolution());

    // n alternative carrier records
    records.addAll(handoverRequestRecord.getAlternativeCarriers());

    messageEncoder.encode(records, payload);

    return payload.toByteArray();
  }
View Full Code Here

TOP

Related Classes of org.nfctools.ndef.wkt.handover.records.HandoverRequestRecord

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.