public byte[] marshall() throws Exception {
    out = new ByteArrayOutputStream();
    out.reset();
    out.write(PduUtilities.makeByteArrayFromInt(getCmd_len(), 4));
    out.write(PduUtilities.makeByteArrayFromInt(getCmd_id(), 4));
    out.write(PduUtilities.makeByteArrayFromInt(getCmd_status(), 4));
    out.write(PduUtilities.makeByteArrayFromInt(getSeq_no(), 4));

    out.write(PduUtilities.stringToNullTerminatedByteArray(service_type));
    out.write(PduUtilities.makeByteArrayFromInt(source_addr_ton, 1));
    out.write(PduUtilities.makeByteArrayFromInt(source_addr_npi, 1));
    out.write(PduUtilities.stringToNullTerminatedByteArray(source_addr));
    out.write(PduUtilities.makeByteArrayFromInt(dest_addr_ton, 1));
    out.write(PduUtilities.makeByteArrayFromInt(dest_addr_npi, 1));
    out.write(PduUtilities.stringToNullTerminatedByteArray(destination_addr));
    out.write(PduUtilities.makeByteArrayFromInt(esm_class, 1));
    out.write(PduUtilities.makeByteArrayFromInt(registered_delivery_flag, 1));
    out.write(PduUtilities.makeByteArrayFromInt(data_coding, 1));

    for (Iterator<Tlv> it = optionalsByTag.values().iterator(); it.hasNext(); ) {
      Tlv opt = it.next();
      out.write(PduUtilities.makeByteArrayFromInt(opt.getTag(), 2));
      out.write(PduUtilities.makeByteArrayFromInt(opt.getLen(), 2));
      out.write(opt.getValue());
    }

    byte[] response = out.toByteArray();
    int l = response.length;
    response = PduUtilities.setPduLength(response, l);
    return response;
  }
Beispiel #2
0
  private static int tlvEncodedLength(Tlv tlv) {
    int length;
    switch (tlv.getType()) {
      case RESOURCE_VALUE:
      case RESOURCE_INSTANCE:
        length = tlv.getValue().length;
        break;
      default:
        length = 0;
        for (Tlv child : tlv.getChildren()) {
          int subLength = tlvEncodedLength(child);
          length += tlvEncodedSize(child, subLength);
        }
    }

    return length;
  }
Beispiel #3
0
  private static int tlvEncodedSize(Tlv tlv, int length) {
    int size = 1 /* HEADER */;
    size += (tlv.getIdentifier() < MAX_LENGTH_8BIT) ? 1 : 2; /* 8 bits or 16 bits identifiers */

    if (length < 8) {
      size += 0;
    } else if (length < MAX_LENGTH_8BIT) {
      size += 1;
    } else if (length < MAX_LENGTH_16BIT) {
      size += 2;
    } else if (length < MAX_LENGTH_24BIT) {
      size += 3;
    } else {
      throw new IllegalArgumentException("length should fit in max 24bits");
    }

    size += length;
    return size;
  }
Beispiel #4
0
  private static void encode(Tlv tlv, ByteBuffer b) {
    int length;
    length = tlvEncodedLength(tlv);
    int typeByte;

    switch (tlv.getType()) {
      case OBJECT_INSTANCE:
        typeByte = 0b00_000000;
        break;
      case RESOURCE_INSTANCE:
        typeByte = 0b01_000000;
        break;
      case MULTIPLE_RESOURCE:
        typeByte = 0b10_000000;
        break;
      case RESOURCE_VALUE:
        // encode the value
        typeByte = 0b11_000000;
        break;
      default:
        throw new IllegalArgumentException("unknown TLV type : '" + tlv.getType() + "'");
    }

    // encode identifier length
    typeByte |= (tlv.getIdentifier() < MAX_LENGTH_8BIT) ? 0b00_0000 : 0b10_0000;

    // type of length
    if (length < 8) {
      typeByte |= length;
    } else if (length < MAX_LENGTH_8BIT) {
      typeByte |= 0b0000_1000;
    } else if (length < MAX_LENGTH_16BIT) {
      typeByte |= 0b0001_0000;
    } else {
      typeByte |= 0b0001_1000;
    }

    // fill the buffer
    b.put((byte) typeByte);
    if (tlv.getIdentifier() < MAX_LENGTH_8BIT) {
      b.put((byte) tlv.getIdentifier());
    } else {
      b.putShort((short) tlv.getIdentifier());
    }

    // write length

    if (length >= 8) {
      if (length < MAX_LENGTH_8BIT) {
        b.put((byte) length);
      } else if (length < MAX_LENGTH_16BIT) {
        b.putShort((short) length);
      } else {
        int msb = (length & 0xFF_00_00) >> 16;
        b.put((byte) msb);
        b.putShort((short) (length & 0xFF_FF));
        typeByte |= 0b0001_1000;
      }
    }

    switch (tlv.getType()) {
      case RESOURCE_VALUE:
      case RESOURCE_INSTANCE:
        b.put(tlv.getValue());
        break;
      default:
        for (Tlv child : tlv.getChildren()) {
          encode(child, b);
        }
        break;
    }
  }
 public void setOptionnal(Tlv opt) {
   optionalsByTag.put(new Short(opt.getTag()), opt);
 }