Implement submit message encoding
This commit is contained in:
@@ -2,12 +2,10 @@ package pdu
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/ascii85"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/warthog618/sms/encoding/gsm7"
|
"smpptester/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -67,55 +65,43 @@ type (
|
|||||||
SUBMIT_MULTI struct{}
|
SUBMIT_MULTI struct{}
|
||||||
SUBMIT_MULTI_RESP struct{}
|
SUBMIT_MULTI_RESP struct{}
|
||||||
)
|
)
|
||||||
|
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
|
||||||
|
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
|
||||||
|
|
||||||
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
return fmt.Errorf("cannot encode into nil buffer")
|
return fmt.Errorf("cannot encode into nil buffer")
|
||||||
}
|
}
|
||||||
// This won't do...
|
messageEncoder := p.GetEncoder()
|
||||||
// TODO: Implement your own encoders and shit
|
|
||||||
// ASCII is easy
|
|
||||||
// UCS2 should also be fairly easy, use uint16 or something
|
|
||||||
// GSM7 will not be easy
|
|
||||||
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
|
|
||||||
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
|
|
||||||
asciiEncoder := ascii85.NewEncoder(buf)
|
|
||||||
// TODO: Implement encodings bsed on p.data_coding
|
|
||||||
messageEncoder := gsm7.NewEncoder()
|
|
||||||
|
|
||||||
p.header.Encode(buf)
|
p.header.Encode(buf)
|
||||||
n, err := asciiEncoder.Write([]byte("OOO"))
|
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
|
||||||
if err != nil {
|
buf.WriteString(p.service_type)
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Println(n)
|
|
||||||
// asciiEncoder.Write([]byte(p.service_type))
|
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.source_addr_ton)
|
binary.Write(buf, binary.BigEndian, p.source_addr_ton)
|
||||||
binary.Write(buf, binary.BigEndian, p.source_addr_npi)
|
binary.Write(buf, binary.BigEndian, p.source_addr_npi)
|
||||||
asciiEncoder.Write([]byte(p.source_addr))
|
buf.WriteString(p.source_addr)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.dest_addr_ton)
|
binary.Write(buf, binary.BigEndian, p.dest_addr_ton)
|
||||||
binary.Write(buf, binary.BigEndian, p.dest_addr_npi)
|
binary.Write(buf, binary.BigEndian, p.dest_addr_npi)
|
||||||
asciiEncoder.Write([]byte(p.destination_addr))
|
buf.WriteString(p.destination_addr)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.esm_class)
|
binary.Write(buf, binary.BigEndian, p.esm_class)
|
||||||
binary.Write(buf, binary.BigEndian, p.protocol_id)
|
binary.Write(buf, binary.BigEndian, p.protocol_id)
|
||||||
binary.Write(buf, binary.BigEndian, p.priority_flag)
|
binary.Write(buf, binary.BigEndian, p.priority_flag)
|
||||||
asciiEncoder.Write([]byte(p.schedule_delivery_time))
|
buf.WriteString(p.schedule_delivery_time)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
asciiEncoder.Write([]byte(p.validity_period))
|
buf.WriteString(p.validity_period)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.registered_delivery)
|
binary.Write(buf, binary.BigEndian, p.registered_delivery)
|
||||||
binary.Write(buf, binary.BigEndian, p.replace_if_present)
|
binary.Write(buf, binary.BigEndian, p.replace_if_present)
|
||||||
binary.Write(buf, binary.BigEndian, p.data_coding)
|
binary.Write(buf, binary.BigEndian, p.data_coding)
|
||||||
binary.Write(buf, binary.BigEndian, p.sm_default_msg_id)
|
binary.Write(buf, binary.BigEndian, p.sm_default_msg_id)
|
||||||
binary.Write(buf, binary.BigEndian, p.sm_length)
|
binary.Write(buf, binary.BigEndian, p.sm_length)
|
||||||
encodedMsg, err := messageEncoder.Encode([]byte(p.short_message))
|
err := messageEncoder.Encode(&p.short_message, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buf.Write(encodedMsg)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
||||||
@@ -144,20 +130,24 @@ func (p *SUBMIT_SM) Size() int {
|
|||||||
size += 1 // data_coding
|
size += 1 // data_coding
|
||||||
size += 1 // sm_default_msg_id
|
size += 1 // sm_default_msg_id
|
||||||
size += 1 // sm_length
|
size += 1 // sm_length
|
||||||
// TODO: Handle encoding based on p.data_coding
|
size += p.GetEncoder().EncodesInto(&p.short_message)
|
||||||
switch p.data_coding {
|
|
||||||
case 0b00000000: // GSM7
|
|
||||||
size += (len(p.short_message)*7 + 8 - 1) / 8
|
|
||||||
case 0b00000001: // ASCII
|
|
||||||
size += len(p.short_message)
|
|
||||||
case 0b00000011: // LATIN1
|
|
||||||
size += len(p.short_message)
|
|
||||||
case 0b00001000: // UCS2
|
|
||||||
size += len(p.short_message) * 2
|
|
||||||
}
|
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) UpdateSize() {
|
func (p *SUBMIT_SM) UpdateSize() {
|
||||||
p.header.command_length = uint32(p.Size())
|
p.header.command_length = uint32(p.Size())
|
||||||
p.sm_length = byte(len(p.short_message))
|
p.sm_length = byte(len(p.short_message))
|
||||||
}
|
}
|
||||||
|
func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
|
||||||
|
switch p.data_coding {
|
||||||
|
case 0b00000000: // GSM7
|
||||||
|
return &encoding.GSM7Coder{}
|
||||||
|
case 0b00000001: // ASCII
|
||||||
|
return &encoding.ASCIICoder{}
|
||||||
|
// case 0b00000011: // LATIN1
|
||||||
|
// return &encoding.LATIN1Coder{}
|
||||||
|
case 0b00001000: // UCS2
|
||||||
|
return &encoding.UCS2Coder{}
|
||||||
|
default:
|
||||||
|
return &encoding.ASCIICoder{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -39,7 +39,7 @@ func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
|
|||||||
t.Fatalf("expected no error, got %v", err)
|
t.Fatalf("expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []byte{0,0,0,107,0,0,0,4,0,0,0,0,0,5,196,163,79,77,86,52,0,3,1,56,48,51,54,50,0,1,1,56,49,50,51,52,53,48,48,49,48,48,48,0,0,0,0,0,49,56,48,49,48,53,49,50,48,48,48,48,48,48,52,43,0,1,0,0,0,37,82,101,112,108,121,32,89,101,115,32,116,111,32,111,112,116,32,105,110,32,111,114,32,78,111,32,116,111,32,111,112,116,32,111,117,116,46}
|
expected := []byte{0, 0, 0, 107, 0, 0, 0, 4, 0, 0, 0, 0, 0, 5, 196, 163, 79, 77, 86, 52, 0, 3, 1, 56, 48, 51, 54, 50, 0, 1, 1, 56, 49, 50, 51, 52, 53, 48, 48, 49, 48, 48, 48, 0, 0, 0, 0, 0, 49, 56, 48, 49, 48, 53, 49, 50, 48, 48, 48, 48, 48, 48, 52, 43, 0, 1, 0, 1, 0, 37, 82, 101, 112, 108, 121, 32, 89, 101, 115, 32, 116, 111, 32, 111, 112, 116, 32, 105, 110, 32, 111, 114, 32, 78, 111, 32, 116, 111, 32, 111, 112, 116, 32, 111, 117, 116, 46}
|
||||||
if !bytes.Equal(buf.Bytes(), expected) {
|
if !bytes.Equal(buf.Bytes(), expected) {
|
||||||
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
|
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user