From 060fa23f4d2c0ebcedfee4c36e80cba518953c54 Mon Sep 17 00:00:00 2001 From: Florian Thienel Date: Sat, 25 Sep 2021 14:43:27 +0200 Subject: [PATCH] make simple text messages encodable --- sds/sds.go | 31 +++++++++++++++++++++++++++++-- sds/sds_test.go | 16 ++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/sds/sds.go b/sds/sds.go index 89ba8d0..ef11e04 100644 --- a/sds/sds.go +++ b/sds/sds.go @@ -360,7 +360,7 @@ func ParseSDSTransfer(bytes []byte) (SDSTransfer, error) { } // NewTextMessageTransfer returns a new SDS-TRANSFER PDU for text messaging with the given parameters -func NewTextMessageTransfer(messageReference MessageReference, immediate bool, deliveryReport DeliveryReportRequest, text string) SDSTransfer { +func NewTextMessageTransfer(messageReference MessageReference, immediate bool, deliveryReport DeliveryReportRequest, encoding TextEncoding, text string) SDSTransfer { var protocol ProtocolIdentifier if immediate { protocol = ImmediateTextMessaging @@ -374,7 +374,7 @@ func NewTextMessageTransfer(messageReference MessageReference, immediate bool, d DeliveryReportRequest: deliveryReport, UserData: TextSDU{ TextHeader: TextHeader{ - Encoding: ISO8859_1, + Encoding: encoding, }, Text: text, }, @@ -750,6 +750,7 @@ type ExternalSubscriberNumberDigit byte // its only 4 bits per digit /* Simple Text Messaging related types and functions */ +// ParseSimpleTextMessage parses a simple text message PDU func ParseSimpleTextMessage(bytes []byte) (SimpleTextMessage, error) { if len(bytes) < 2 { return SimpleTextMessage{}, fmt.Errorf("simple text message PDU too short: %d", len(bytes)) @@ -768,6 +769,22 @@ func ParseSimpleTextMessage(bytes []byte) (SimpleTextMessage, error) { return result, nil } +// NewSimpleTextMessage returns a new simple text message PDU according to the given parameters +func NewSimpleTextMessage(immediate bool, encoding TextEncoding, text string) SimpleTextMessage { + var protocol ProtocolIdentifier + if immediate { + protocol = ImmediateTextMessaging + } else { + protocol = TextMessaging + } + + return SimpleTextMessage{ + protocol: protocol, + Encoding: encoding, + Text: text, + } +} + // SimpleTextMessage represents the data of a simple text messaging PDU, according to [AI] 29.5.2.3 type SimpleTextMessage struct { protocol ProtocolIdentifier @@ -780,6 +797,16 @@ func (m SimpleTextMessage) Immediate() bool { return m.protocol == SimpleImmediateTextMessaging } +// Encode this simple text message +func (m SimpleTextMessage) Encode(bytes []byte, bits int) ([]byte, int) { + bytes, bits = m.protocol.Encode(bytes, bits) + bytes = append(bytes, byte(m.Encoding)) + bits += 8 + bytes, bits = AppendEncodedPayloadText(bytes, bits, m.Text, m.Encoding) + + return bytes, bits +} + /* Text messaging related types and functions */ // ParseTextSDU parses the user data of a text message. diff --git a/sds/sds_test.go b/sds/sds_test.go index 9581ab5..56eaf6c 100644 --- a/sds/sds_test.go +++ b/sds/sds_test.go @@ -32,10 +32,10 @@ func TestParseMessage(t *testing.T) { }, { desc: "simple text message", - header: "+CTSDSR: 12,1234567,0,2345678,0,224", + header: "+CTSDSR: 12,1234567,0,2345678,0,104", pdu: "0201746573746D657373616765", expected: IncomingMessage{ - Header: Header{AIService: SDSTLService, Source: "1234567", Destination: "2345678", PDUBits: 224}, + Header: Header{AIService: SDSTLService, Source: "1234567", Destination: "2345678", PDUBits: 104}, Payload: SimpleTextMessage{ protocol: SimpleTextMessaging, Encoding: ISO8859_1, @@ -493,6 +493,18 @@ func TestEncode(t *testing.T) { expectedBytes: []byte{0x82, 0x06, 0xC9, 0x81, 0x44, 0x5A, 0x8F, 0x74, 0x65, 0x73, 0x74, 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65}, expectedBits: 144, }, + { + desc: "simple text message", + values: []Encoder{ + SimpleTextMessage{ + protocol: SimpleTextMessaging, + Encoding: ISO8859_1, + Text: "testmessage", + }, + }, + expectedBytes: []byte{0x02, 0x01, 0x74, 0x65, 0x73, 0x74, 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65}, + expectedBits: 104, + }, } for _, tc := range tt { t.Run(tc.desc, func(t *testing.T) {