public override void Decode() { string tail = body; byte[] bt; service_type = Common.HexToString(Common.GetCString(ref tail)); bt = Common.ConvertHexStringToByteArray(tail.Substring(0, 4)); tail = tail.Remove(0, 4); source_addr_ton = bt[0]; source_addr_npi = bt[1]; source_addr = Common.HexToString(Common.GetCString(ref tail)); bt = Common.ConvertHexStringToByteArray(tail.Substring(0, 4)); tail = tail.Remove(0, 4); dest_addr_ton = bt[0]; dest_addr_npi = bt[1]; destination_address = Common.HexToString(Common.GetCString(ref tail)); bt = Common.ConvertHexStringToByteArray(tail.Substring(0, 6)); tail = tail.Remove(0, 6); esm_class = bt[0]; protocol_id = bt[1]; priority_flag = bt[2]; schedule_delivery_time = Common.HexToString(Common.GetCString(ref tail)); validity_period = Common.HexToString(Common.GetCString(ref tail)); bt = Common.ConvertHexStringToByteArray(tail.Substring(0, 10)); tail = tail.Remove(0, 10); registered_delivery = bt[0].ToString(); replace_if_present_flag = bt[1]; data_coding = bt[2]; sm_default_msg_id = bt[3]; sm_length = bt[4]; if (sm_length != 0) { switch (data_coding) { case 8: // UNICODE body_hex = tail.Substring(0, sm_length * 2); if (body_hex.StartsWith("050003")) { multipart_header = body_hex.Substring(0, 12); short_message = Encoding.BigEndianUnicode.GetString(Common.ConvertHexStringToByteArray(tail.Substring(12, sm_length * 2 - 12))); } else { if (esm_class != 4) { short_message = Encoding.BigEndianUnicode.GetString(Common.ConvertHexStringToByteArray(body_hex)); } else { short_message = Encoding.Unicode.GetString(Common.ConvertHexStringToByteArray(body_hex)); } } break; case 3: // Latin 1 (ISO-8859-1) case 1: // ASCII case 0: body_hex = tail.Substring(0, sm_length * 2); if (is_8bit) { short_message = Common.Convert8bitTo7bit(body_hex); } else { if (body_hex.StartsWith("050003")) { multipart_header = body_hex.Substring(0, 12); short_message = Common.HexToString(tail.Substring(12, sm_length * 2 - 12)); } else { short_message = Common.HexToString(body_hex); //short_message = Encoding.Unicode.GetString(Common.ConvertHexStringToByteArray(body_hex)); } } break; default: body_hex = tail.Substring(0, sm_length * 2); short_message = body_hex; break; } } else { base.LoadTLV(tail); //message_payload if (tlv.ContainsKey("0424")) { switch (data_coding) { // UNICODE case 8: body_hex = tlv["0424"].ToString(); short_message = Encoding.BigEndianUnicode.GetString( Common.ConvertHexStringToByteArray(body_hex) ); break; //ASCII case 1: case 0: body_hex = tlv["0424"].ToString(); short_message = Common.HexToString(body_hex); break; default: body_hex = tlv["0424"].ToString(); short_message = Common.HexToString(body_hex); break; } } } if (short_message == null) { short_message = String.Empty; } // example from SMSinform - megafon // id:32C84E82 sub:001 dlvrd:001 submit date:0709111120 done date:0709111120 stat:DELIVRD' // Delivery report if (esm_class == 4) { int i_status; var reg = Regex.Match(short_message, @"id:\s*([a-zA-Z\-0-9]+)", RegexOptions.IgnoreCase); if (reg.Groups.Count == 2) { reference_from = reg.Groups[1].Value; } if (reference_from == String.Empty) { reference_from = ""; } i_status = short_message.IndexOf("stat:", StringComparison.CurrentCultureIgnoreCase); if (i_status == -1) { //gateEvents.LogEvent(LogEvent.Level.Error, "Error decoding delivery report: no reference; " + short_message); delivery_status = "DELIVR"; return; } //delivery_status = short_message.Substring(i_status + 5, i_p - i_status - 5, 7); // in common SMSC returns 7 symbols , but some just 6 // so let for first 6 symbols try { delivery_status = short_message.Substring(i_status + 5, 6); } catch { delivery_status = String.Empty; gateEvents.LogEvent(LogEvent.Level.Error, "Delivery error code new " + short_message); } if (delivery_status == String.Empty) { delivery_status = "DELIVR"; } } }
public void MessageHandler(object mess) { var data = new List <string>(); int PDULength = 0; last_pdu_time = DateTime.Now; try { _partial_pdu += mess.ToString(); PDULength = int.Parse(_partial_pdu.Substring(0, 8), System.Globalization.NumberStyles.HexNumber); if (PDULength > 1500) { _partial_pdu = ""; return; } while (PDULength != 0) { data.Add(_partial_pdu.Substring(0, PDULength * 2)); _partial_pdu = _partial_pdu.Substring(PDULength * 2); PDULength = _partial_pdu != "" ? int.Parse(_partial_pdu.Substring(0, 8), System.Globalization.NumberStyles.HexNumber) : 0; } } catch (CommandLengthException) { Events.LogEvent(LogEvent.Level.Error, "Command length exception"); } catch (Exception e1) { Events.LogEvent(LogEvent.Level.Error, channel_name + " - " + e1 + ":" + PDULength); _partial_pdu = ""; return; } foreach (string pdu in data) { if (string.IsNullOrEmpty(pdu) || pdu.Length < 8) { continue; } try { PduHandler(pdu); } catch (Exception e1) { Events.LogEvent(LogEvent.Level.Error, channel_name + " - " + e1 + ":" + pdu); } } }