Before commenting read Forum rules
Don't comment the topic if you have a new question.
You can create a new topic selecting correct category from Gurux Forum and then create a new topic selecting "New Topic" from the top left.
Before commenting read Forum rules
Don't comment the topic if you have a new question.
You can create a new topic selecting correct category from Gurux Forum and then create a new topic selecting "New Topic" from the top left.
Execute Gurux.SMS.Read() generate System.ArgumentOutOfRangeException
Reasons:
1) incorrect calculation of length Service Center number
int sz = 2 * ServCenterlen - 1;
correct: int sz = 2 * (ServCenterlen - 1);
2) message type is not taken into account when parsing
fix code:
public static void Encode(string data, GXSMSMessage msg)
{
if (data.Length < 1)
{
throw new ArgumentOutOfRangeException("Invalid data.");
}
int index = 0;
// Service Center Number length
int ServCenterlen = GetInteger(data, index);
index += 2;
//Type-of-address of the SMSC.
int SMSCType = GetInteger(data, index);
index += 2;
//Get Service Center number
if (ServCenterlen > 0)
{
int sz = 2 * (ServCenterlen - 1);
if (data.Length < index + sz)
{
throw new ArgumentOutOfRangeException("Invalid data.");
}
//string serviceCenterNumber = data.Substring(index, sz);
string serviceCenterNumber = GetString(data, index, sz, true);
if (SMSCType == 0x91) //If international format.
{
serviceCenterNumber = "+" + serviceCenterNumber;
}
msg.ServiceCenterNumber = serviceCenterNumber;
index += sz;
}
//First octet of the SMS-DELIVER PDU
int SMSDeliver = GetInteger(data, index);
index += 2;
MessageTypeIndicator mti = (MessageTypeIndicator)(SMSDeliver & 0x03);
if (mti == MessageTypeIndicator.SmsSubmit)
{
//Skip TP-MR (Message Reference)
index += 2;
}
//Length of the sender number
int SenderLen = GetInteger(data, index);
index += 2;
SenderLen += SenderLen % 2;
//Type-of-address of the sender number.
int SenderType = GetInteger(data, index);
index += 2;
//Get Sender number
string phoneNumber = GetString(data, index, SenderLen, true);
if (SenderType == 0x91) //If international format.
{
phoneNumber = "+" + phoneNumber;
}
msg.PhoneNumber = phoneNumber;
index += SenderLen;
//TP-PID. Protocol identifier.
int ProtocolID = GetInteger(data, index);
index += 2;
//TP-DCS Data coding scheme
msg.CodeType = (MessageCodeType)GetInteger(data, index);
index += 2;
//VP (Validity Period)
if (mti==MessageTypeIndicator.SmsDeliver || (SMSDeliver & 0x18) == 0x18)
{
int year = GetInteger(data, index);
index += 2;
if (year != 0xAA)
{
year += 2000;
int month = GetInteger(data, index);
index += 2;
int day = GetInteger(data, index);
index += 2;
int hour = GetInteger(data, index);
index += 2;
int minute = GetInteger(data, index);
index += 2;
int second = GetInteger(data, index);
index += 2;
int timezone = GetInteger(data, index);
index += 2;
//TODO: lisää time zone.
//DateTime dt = new DateTime(year, month, day, hour, minute, second);
}
}
else if ((SMSDeliver & 0x18) == 0x10)
{
//Skip the length of the validity period, counted from when the SMS - SUBMIT is received by the SC
index += 2;
}
//TP-UDL. User data length, length of message.
// The TP-DCS field indicated 7-bit data, so the length here is the number of septets (10).
// If the TP-DCS field were set to indicate 8-bit data or Unicode, the length would be the number of octets (9).
int DataLen = GetInteger(data, index);
index += 2;
string buff = GetString(data, index, 2 * DataLen, false); //data.Substring(index, DataLen);
index += DataLen;
//TP-UD. Message 8-bit octets representing 7-bit data.
//When DataCodingScheme = 0, PDU code is coded from 7bit charactor (see GSM 03.38).
if (msg.CodeType == MessageCodeType.Bits7)
{
msg.Data = Decode7Bit(buff);
}
// When DataCodingScheme = 4, PDU code is coded using 8 bits codec...
else if (msg.CodeType == MessageCodeType.Bits8)
{
msg.Data = Decode8Bit(buff);
}
// When DataCodingScheme = 8, PDU code is coded from Unicode charactor (see GSM 03.38).
else if (msg.CodeType == MessageCodeType.Unicode)
{
msg.Data = DecodeUnicode(buff);
}
else
{
throw new ArgumentOutOfRangeException("Invalid data coding scheme");
}
}
/// <summary>
/// The TP-Message-Type-Indicator is a 2-bit field, located within bits no 0 and 1 of the first octet of
/// SMS-DELIVER, SMS-SUBMIT, SMS-STATUS-REPORT or SMS-COMMAND
/// </summary>
public enum MessageTypeIndicator : byte
{
SmsDeliver = 0x00,
SmsSubmit = 0x01,
SmsStatusReport = 0x02,
Reserved = 0x03,
}