📄 pdu.java
字号:
package org.ajwcc.pduUtils.gsm3040;
import java.util.*;
import org.ajwcc.pduUtils.gsm3040.ie.*;
//PduUtils Library - A Java library for generating GSM 3040 Protocol Data Units (PDUs)
//
//Copyright (C) 2008, Ateneo Java Wireless Competency Center/Blueblade Technologies, Philippines.
//PduUtils is distributed under the terms of the Apache License version 2.0
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
public abstract class Pdu
{
// PDU class
// this class holds directly usable data only
// - all lengths are ints
// - dates and strings are already decoded
// - byte[] for binary data that can interpreted later
// an object of this type is created via a PduParser
// or created raw, has its field set and supplied to a PduGenerator
// ==================================================
// SMSC INFO
// ==================================================
private int smscInfoLength;
private int smscAddressType;
private String smscAddress;
public int getSmscInfoLength()
{
return smscInfoLength;
}
public void setSmscInfoLength(int smscInfoLength)
{
this.smscInfoLength = smscInfoLength;
}
public void setSmscAddressType(int smscAddressType)
{
this.smscAddressType = PduUtils.createAddressType(smscAddressType);
}
public int getSmscAddressType()
{
return smscAddressType;
}
public void setSmscAddress(String smscAddress)
{
if (smscAddress.equals(""))
{
smscAddress = null;
smscAddressType = 0;
smscInfoLength = 0;
return;
}
// strip the + since it is not needed
if (smscAddress.startsWith("+"))
{
this.smscAddress = smscAddress.substring(1);
}
else
{
this.smscAddress = smscAddress;
}
// pass original address with the +, if any
// to determine if international format or not
setSmscAddressType(PduUtils.getAddressTypeFor(smscAddress));
}
public String getSmscAddress()
{
return smscAddress;
}
// ==================================================
// FIRST OCTET
// ==================================================
private int firstOctet = 0;
public int getFirstOctet()
{
return firstOctet;
}
public void setFirstOctet(int value)
{
firstOctet = value;
}
protected void setFirstOctetField(int fieldName, int fieldValue, int[] allowedValues)
{
for (int value : allowedValues)
{
if (value == fieldValue)
{
// clear the bits for this field
firstOctet &= fieldName;
// copy the new bits on to it
firstOctet |= fieldValue;
return;
}
}
throw new RuntimeException("Invalid value for fieldName.");
}
protected int getFirstOctetField(int fieldName)
{
return firstOctet & ~fieldName;
}
// ==================================================
// FIRST OCTET UTILITIES
// ==================================================
protected void checkTpMti(int allowedType)
{
int tpMti = getTpMti();
if (tpMti != allowedType) { throw new RuntimeException("Invalid message type : " + getTpMti()); }
}
protected void checkTpMti(int[] allowedTypes)
{
int tpMti = getTpMti();
for (int type : allowedTypes)
{
if (tpMti == type) { return; }
}
throw new RuntimeException("Invalid message type : " + getTpMti());
}
public void setTpMti(int value)
{
setFirstOctetField(PduUtils.TP_MTI_MASK, value, new int[] { PduUtils.TP_MTI_SMS_DELIVER, PduUtils.TP_MTI_SMS_STATUS_REPORT, PduUtils.TP_MTI_SMS_SUBMIT });
}
public int getTpMti()
{
return getFirstOctetField(PduUtils.TP_MTI_MASK);
}
public void setTpUdhi(int value)
{
setFirstOctetField(PduUtils.TP_UDHI_MASK, value, new int[] { PduUtils.TP_UDHI_NO_UDH, PduUtils.TP_UDHI_WITH_UDH });
}
public boolean hasTpUdhi()
{
return getFirstOctetField(PduUtils.TP_UDHI_MASK) == PduUtils.TP_UDHI_WITH_UDH;
}
// ==================================================
// PROTOCOL IDENTIFIER
// ==================================================
// usually just 0x00 for regular SMS
private int protocolIdentifier = 0x00;
public void setProtocolIdentifier(int protocolIdentifier)
{
this.protocolIdentifier = protocolIdentifier;
}
public int getProtocolIdentifier()
{
return protocolIdentifier;
}
// ==================================================
// DATA CODING SCHEME
// ==================================================
// usually just 0x00 for default GSM alphabet, phase 2
private int dataCodingScheme = 0x00;
public void setDataCodingScheme(int encoding)
{
switch (encoding & ~PduUtils.DCS_ENCODING_MASK)
{
case PduUtils.DCS_ENCODING_7BIT:
case PduUtils.DCS_ENCODING_8BIT:
case PduUtils.DCS_ENCODING_UCS2:
break;
default:
throw new RuntimeException("Invalid encoding value: " + PduUtils.byteToPdu(encoding));
}
dataCodingScheme = encoding;
}
public int getDataCodingScheme()
{
return dataCodingScheme;
}
// ==================================================
// TYPE-OF-ADDRESS
// ==================================================
private int addressType;
public int getAddressType()
{
return addressType;
}
public void setAddressType(int addressType)
{
// insure last bit is always set
this.addressType = PduUtils.createAddressType(addressType);
}
// ==================================================
// ADDRESS
// ==================================================
// swapped BCD-format for numbers
// 7-bit GSM string for alphanumeric
private String address;
public void setAddress(String address)
{
// eliminate the + since this does not do anything
if (address.startsWith("+"))
{
this.address = address.substring(1);
}
else
{
this.address = address;
}
// pass original address with the +, if any
// to determine if international format or not
setAddressType(PduUtils.getAddressTypeFor(address));
}
public String getAddress()
{
return address;
}
// ==================================================
// USER DATA SECTION
// ==================================================
// this is still needs to be stored since it does not always represent
// length in octets, for 7-bit encoding this is length in SEPTETS
// NOTE: udData.length may not equal udLength if 7-bit encoding is used
private int udLength;
private byte[] udData;
public int getUDLength()
{
return udLength;
}
public void setUDLength(int udLength)
{
this.udLength = udLength;
}
public byte[] getUDData()
{
return udData;
}
// NOTE: udData DOES NOT include the octet with the length
public void setUDData(byte[] udData)
{
this.udData = udData;
}
// ==================================================
// USER DATA HEADER
// ==================================================
// all methods accessing UDH specific methods require the UDHI to be set
// or else an exception will result
private static final int UDH_CHECK_MODE_ADD_IF_NONE = 0;
private static final int UDH_CHECK_MODE_EXCEPTION_IF_NONE = 1;
private static final int UDH_CHECK_MODE_IGNORE_IF_NONE = 2;
private void checkForUDHI(int udhCheckMode)
{
if (!hasTpUdhi())
{
switch (udhCheckMode)
{
case UDH_CHECK_MODE_EXCEPTION_IF_NONE:
throw new IllegalStateException("PDU does not have a UDHI in the first octet");
case UDH_CHECK_MODE_ADD_IF_NONE:
setTpUdhi(PduUtils.TP_UDHI_WITH_UDH);
break;
case UDH_CHECK_MODE_IGNORE_IF_NONE:
break;
default:
throw new RuntimeException("Invalid UDH check mode");
}
}
}
public int getTotalUDHLength()
{
int udhLength = getUDHLength();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -