Skip to content

Commit

Permalink
Refactor code (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachhh authored Nov 2, 2020
1 parent c8e20f1 commit 5e77364
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 111 deletions.
5 changes: 2 additions & 3 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
var currentAuth int32

var auths = [][2]string{
{"976831", "83c6c9"},
{"498160", "ce35cd"},
{"087051", "1d29a1"},
{"863463", "e2e79e"},
}

const (
Expand All @@ -29,7 +29,6 @@ func nextAuth() Auth {
}
}

// TestBindingSMSC test binding connection with SMSC
func TestBindingSMSC(t *testing.T) {
// valid
connection, err := ConnectAsTransceiver(NonTLSDialer, nextAuth())
Expand Down
106 changes: 53 additions & 53 deletions data/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ const (

// Interface_Version
SMPP_V33 int8 = int8(-0x33)
SMPP_V34 byte = byte(0x34)
SMPP_V34 = byte(0x34)

// Address_TON
GSM_TON_UNKNOWN byte = byte(0x00)
GSM_TON_INTERNATIONAL byte = byte(0x01)
GSM_TON_NATIONAL byte = byte(0x02)
GSM_TON_NETWORK byte = byte(0x03)
GSM_TON_SUBSCRIBER byte = byte(0x04)
GSM_TON_ALPHANUMERIC byte = byte(0x05)
GSM_TON_ABBREVIATED byte = byte(0x06)
GSM_TON_RESERVED_EXTN byte = byte(0x07)
GSM_TON_UNKNOWN = byte(0x00)
GSM_TON_INTERNATIONAL = byte(0x01)
GSM_TON_NATIONAL = byte(0x02)
GSM_TON_NETWORK = byte(0x03)
GSM_TON_SUBSCRIBER = byte(0x04)
GSM_TON_ALPHANUMERIC = byte(0x05)
GSM_TON_ABBREVIATED = byte(0x06)
GSM_TON_RESERVED_EXTN = byte(0x07)

// Address_NPI
GSM_NPI_UNKNOWN byte = byte(0x00)
GSM_NPI_E164 byte = byte(0x01)
GSM_NPI_ISDN byte = GSM_NPI_E164
GSM_NPI_X121 byte = byte(0x03)
GSM_NPI_TELEX byte = byte(0x04)
GSM_NPI_LAND_MOBILE byte = byte(0x06)
GSM_NPI_NATIONAL byte = byte(0x08)
GSM_NPI_PRIVATE byte = byte(0x09)
GSM_NPI_ERMES byte = byte(0x0A)
GSM_NPI_INTERNET byte = byte(0x0E)
GSM_NPI_WAP_CLIENT_ID byte = byte(0x12)
GSM_NPI_RESERVED_EXTN byte = byte(0x0F)
GSM_NPI_UNKNOWN = byte(0x00)
GSM_NPI_E164 = byte(0x01)
GSM_NPI_ISDN = GSM_NPI_E164
GSM_NPI_X121 = byte(0x03)
GSM_NPI_TELEX = byte(0x04)
GSM_NPI_LAND_MOBILE = byte(0x06)
GSM_NPI_NATIONAL = byte(0x08)
GSM_NPI_PRIVATE = byte(0x09)
GSM_NPI_ERMES = byte(0x0A)
GSM_NPI_INTERNET = byte(0x0E)
GSM_NPI_WAP_CLIENT_ID = byte(0x12)
GSM_NPI_RESERVED_EXTN = byte(0x0F)

// Service_Type
SERVICE_NULL string = ""
Expand All @@ -73,11 +73,11 @@ const (
SERVICE_WAP string = "WAP"
SERVICE_USSD string = "USSD"

SMPP_PROTOCOL byte = byte(1)
SMPPP_PROTOCOL byte = byte(2)
SM_SERVICE_MOBILE_TERMINATED byte = byte(0)
SM_SERVICE_MOBILE_ORIGINATED byte = byte(1)
SM_SERVICE_MOBILE_TRANSCEIVER byte = byte(2)
SMPP_PROTOCOL = byte(1)
SMPPP_PROTOCOL = byte(2)
SM_SERVICE_MOBILE_TERMINATED = byte(0)
SM_SERVICE_MOBILE_ORIGINATED = byte(1)
SM_SERVICE_MOBILE_TRANSCEIVER = byte(2)

// State of message at SMSC
SM_STATE_EN_ROUTE = 1 // default state for messages in transit
Expand Down Expand Up @@ -226,20 +226,20 @@ const (

// Registered delivery
// SMSC Delivery Receipt (bits 1 & 0)
SM_SMSC_RECEIPT_MASK byte = 0x03
SM_SMSC_RECEIPT_NOT_REQUESTED byte = 0x00
SM_SMSC_RECEIPT_REQUESTED byte = 0x01
SM_SMSC_RECEIPT_ON_FAILURE byte = 0x02
SM_SMSC_RECEIPT_MASK = byte(0x03)
SM_SMSC_RECEIPT_NOT_REQUESTED = byte(0x00)
SM_SMSC_RECEIPT_REQUESTED = byte(0x01)
SM_SMSC_RECEIPT_ON_FAILURE = byte(0x02)
// SME originated acknowledgement (bits 3 & 2)
SM_SME_ACK_MASK byte = 0x0c
SM_SME_ACK_NOT_REQUESTED byte = 0x00
SM_SME_ACK_DELIVERY_REQUESTED byte = 0x04
SM_SME_ACK_MANUAL_REQUESTED byte = 0x08
SM_SME_ACK_BOTH_REQUESTED byte = 0x0c
SM_SME_ACK_MASK = byte(0x0c)
SM_SME_ACK_NOT_REQUESTED = byte(0x00)
SM_SME_ACK_DELIVERY_REQUESTED = byte(0x04)
SM_SME_ACK_MANUAL_REQUESTED = byte(0x08)
SM_SME_ACK_BOTH_REQUESTED = byte(0x0c)
// Intermediate notification (bit 5)
SM_NOTIF_MASK byte = 0x010
SM_NOTIF_NOT_REQUESTED byte = 0x000
SM_NOTIF_REQUESTED byte = 0x010
SM_NOTIF_MASK = byte(0x010)
SM_NOTIF_NOT_REQUESTED = byte(0x000)
SM_NOTIF_REQUESTED = byte(0x010)

// Replace if Present flag
SM_NOREPLACE = 0
Expand Down Expand Up @@ -318,8 +318,8 @@ const (
OPT_PAR_MSG_PAYLOAD_MAX = 1500

// User Data Header
UDH_CONCAT_MSG_8_BIT_REF byte = 0x00
UDH_CONCAT_MSG_16_BIT_REF byte = 0x08
UDH_CONCAT_MSG_8_BIT_REF = byte(0x00)
UDH_CONCAT_MSG_16_BIT_REF = byte(0x08)

/**
* @deprecated As of version 1.3 of the library there are defined
Expand All @@ -341,18 +341,18 @@ const (
DFLT_ERR byte = 0
DFLT_SCHEDULE string = ""
DFLT_VALIDITY string = ""
DFLT_REG_DELIVERY byte = SM_SMSC_RECEIPT_NOT_REQUESTED | SM_SME_ACK_NOT_REQUESTED | SM_NOTIF_NOT_REQUESTED
DFLT_DFLTMSGID byte = 0
DFLT_MSG_LEN byte = 0
DFLT_ESM_CLASS byte = 0
DFLT_DATA_CODING byte = 0
DFLT_PROTOCOLID byte = 0
DFLT_PRIORITY_FLAG byte = 0
DFTL_REPLACE_IFP byte = 0
DFLT_REG_DELIVERY = SM_SMSC_RECEIPT_NOT_REQUESTED | SM_SME_ACK_NOT_REQUESTED | SM_NOTIF_NOT_REQUESTED
DFLT_DFLTMSGID = byte(0)
DFLT_MSG_LEN = byte(0)
DFLT_ESM_CLASS = byte(0)
DFLT_DATA_CODING = byte(0)
DFLT_PROTOCOLID = byte(0)
DFLT_PRIORITY_FLAG = byte(0)
DFTL_REPLACE_IFP = byte(0)
DFLT_DL_NAME string = ""
DFLT_GSM_TON byte = GSM_TON_UNKNOWN
DFLT_GSM_NPI byte = GSM_NPI_UNKNOWN
DFLT_DEST_FLAG byte = 0 // not set
DFLT_GSM_TON = GSM_TON_UNKNOWN
DFLT_GSM_NPI = GSM_NPI_UNKNOWN
DFLT_DEST_FLAG = byte(0) // not set
MAX_PDU_LEN = 64 << 10

PDU_HEADER_SIZE = 16 // 4 integers
Expand All @@ -375,8 +375,8 @@ const (
)

var (
// ErrNotImpplSplitterInterface indicates that encoding does not support Splitter interface
ErrNotImpplSplitterInterface = fmt.Errorf("Encoding not implementing Splitter interface")
// ErrNotImplSplitterInterface indicates that encoding does not support Splitter interface
ErrNotImplSplitterInterface = fmt.Errorf("Encoding not implementing Splitter interface")
)

var defaultTon atomic.Value
Expand Down
2 changes: 1 addition & 1 deletion errors/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ var (
ErrShortMessageLengthTooLarge error = &SmppErr{err: fmt.Sprintf("Encoded short message data exceeds size of %d", data.SM_MSG_LEN), serialVersionUID: 78237205927624}

// ErrUDHTooLong UDH-L is larger than total length of short message data
ErrUDHTooLong error = fmt.Errorf("User Data Header is too long for PDU short message")
ErrUDHTooLong = fmt.Errorf("User Data Header is too long for PDU short message")
)
32 changes: 31 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -74,6 +75,7 @@ func sendingAndReceiveSMS(wg *sync.WaitGroup) {
}

func handlePDU() func(pdu.PDU, bool) {
concatenated := map[uint8][]string{}
return func(p pdu.PDU, responded bool) {
switch pd := p.(type) {
case *pdu.SubmitSMResp:
Expand All @@ -90,7 +92,26 @@ func handlePDU() func(pdu.PDU, bool) {

case *pdu.DeliverSM:
fmt.Printf("DeliverSM:%+v\n", pd)
fmt.Println(pd.Message.GetMessage())
log.Println(pd.Message.GetMessage())
// region concatenated sms (sample code)
message, err := pd.Message.GetMessage()
if err != nil {
log.Fatal(err)
}
totalParts, sequence, reference, found := pd.Message.UDH().GetConcatInfo()
if found {
if _, ok := concatenated[reference]; !ok {
concatenated[reference] = make([]string, totalParts)
}
concatenated[reference][sequence-1] = message
}
if !found {
log.Println(message)
} else if parts, ok := concatenated[reference]; ok && isConcatenatedDone(parts, totalParts) {
log.Println(strings.Join(parts, ""))
delete(concatenated, reference)
}
// endregion
}
}
}
Expand Down Expand Up @@ -118,3 +139,12 @@ func newSubmitSM() *pdu.SubmitSM {

return submitSM
}

func isConcatenatedDone(parts []string, total byte) bool {
for _, part := range parts {
if part != "" {
total--
}
}
return total == 0
}
5 changes: 5 additions & 0 deletions pdu/Address.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ func (c Address) Npi() byte {
func (c Address) Address() string {
return c.address
}

// String implement stringer interface
func (c Address) String() string {
return c.address
}
13 changes: 6 additions & 7 deletions pdu/ShortMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ func (c *ShortMessage) GetMessageWithEncoding(enc data.Encoding) (st string, err

f, t := 0, len(c.messageData)

if c.udHeader.UDHL() > 0 {
f = c.udHeader.UDHL() + 1
if f >= t {
err = errors.ErrUDHTooLong
return
}
// skip if UDL is present
f = c.udHeader.UDHL()
if f >= t {
err = errors.ErrUDHTooLong
return
}

st, err = enc.Decode(c.messageData[f:t])
Expand Down Expand Up @@ -150,7 +149,7 @@ func (c *ShortMessage) Split() (multiSM []*ShortMessage, err error) {
// message: we don't really care
messageData: seg,
withoutDataCoding: c.withoutDataCoding,
udHeader: UDH{NewIEConcatMessage(len(segments), i+1, int(ref))},
udHeader: UDH{NewIEConcatMessage(uint8(len(segments)), uint8(i+1), uint8(ref))},
})
}

Expand Down
4 changes: 2 additions & 2 deletions pdu/SubmitSM_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestSubmitSM(t *testing.T) {
v.DestAddr.SetTon(79)
v.DestAddr.SetNpi(80)

v.EsmClass = 77
v.EsmClass = 77 ^ data.SM_UDH_GSM
v.ProtocolID = 99
v.PriorityFlag = 61
v.RegisteredDelivery = 83
Expand All @@ -37,7 +37,7 @@ func TestSubmitSM(t *testing.T) {

validate(t,
v,
"0000005d00000004000000000000000d616263001c1d416c69636572004f50426f62004d633d00005300080030006e006700681eaf0020006e00670068006900ea006e00670020006e0067006800691ec5006e00670020006e00671ea3",
"0000005d00000004000000000000000d616263001c1d416c69636572004f50426f62000d633d00005300080030006e006700681eaf0020006e00670068006900ea006e00670020006e0067006800691ec5006e00670020006e00671ea3",
data.SUBMIT_SM,
)
}
Loading

0 comments on commit 5e77364

Please sign in to comment.