Documentation
¶
Index ¶
- Constants
- Variables
- func CombineMsgID(month, day, hour, minute, second, gateID, sequenceID uint64) uint64
- func ConnectRespResultString(r uint8) string
- func GenConnectAuth(account string, password string, timestampStr string) []byte
- func GenConnectRespAuthISMG(statusBytes []byte, reqAuth string, password string) []byte
- func GenConnectTimestamp(nowFunc func() time.Time) (string, uint32)
- func MsgID2String(u uint64) string
- func MsgIDString2Uint64(s string) uint64
- func ParseSignature(content string) string
- func RemoveSign(sourceContent string) (newContent string, signature string)
- func SplitMsgID(msgID uint64) (month, day, hour, minute, second, gateID, sequenceID uint64)
- func TimeStamp2Str(t uint32) string
- func Utf8ToUcs2(in string) (string, error)
- func Utf8ToUcs2Back(in string) string
- func Utf8ToUcs2Pooled(in string) (s string)
- func WriteHeader(h Header, buf *packet.Writer)
- func WriteHeaderNoLength(h Header, buf *packet.Writer)
- type CommandID
- type FuncWithError
- type Header
- type SubPduDeliveryContent
Constants ¶
const ( // VersionUnknown represents an unknown CMPP version. VersionUnknown = 0 // Version20 represents CMPP version 2.0. Version20 = 0x20 // Version30 represents CMPP version 3.0. Version30 = 0x30 )
const ( // HeaderLength defines the fixed length of the CMPP PDU header. HeaderLength = 12 // PacketTotalLengthBytes defines the number of bytes used for the TotalLength field in the header. PacketTotalLengthBytes = 4 )
const ( ASCII = uint8(0) LATIN = uint8(1) UCS2 = uint8(8) GBK = uint8(15) // UCS2RemoveSign 有些通道,下游供应商会自动填充签名,此时发送时需要将 content 中的签名去掉 // 此编码同 UCS2,但对于运营商会自动加签名的通道,此编码可以让运营商不再自动加签名 UCS2RemoveSign = uint8(25) )
const ConnectTSFormat = "0102150405"
ConnectTSFormat defines the timestamp format (MMDDHHMMSS) used in CMPP Connect PDU.
const ( // MinCMPPPduLength defines the minimum length of a CMPP PDU, which is the header length. MinCMPPPduLength = HeaderLength // cmpp PduCMPP 最小的长度 )
Variables ¶
var ( // ErrIllegalHeaderLength indicates that the provided data length is less than the required header length. ErrIllegalHeaderLength = errors.New("cmpp2 header length is invalid") // ErrInvalidPudLength indicates that the PDU length specified in the header is invalid or inconsistent. ErrInvalidPudLength = errors.New("invalid pdu length") )
var ( ErrInvalidUtf8Rune = errors.New("not valid utf8 runes") ErrUnsupportedDataCoding = errors.New("unsupported data coding") )
var Now = time.Now
Now returns the current time. It's a variable for easy mocking in tests.
Functions ¶
func CombineMsgID ¶
CombineMsgID generates a 64-bit MsgID based on the provided time components, gateway ID, and sequence ID.
func ConnectRespResultString ¶
func GenConnectAuth ¶
GenConnectAuth generates the AuthenticatorSource field for the CMPP CONNECT PDU. It calculates the MD5 hash of (SourceAddr + 9 bytes of zeros + password + Timestamp).
func GenConnectRespAuthISMG ¶
GenConnectRespAuthISMG generates the AuthenticatorISMG field for the CMPP CONNECT_RESP PDU. It calculates the MD5 hash of (Status + AuthenticatorSource + password).
func GenConnectTimestamp ¶
GenConnectTimestamp generates the timestamp string and uint32 value for CMPP Connect PDU. It uses the format defined by ConnectTSFormat.
func MsgID2String ¶
MsgID2String converts a 64-bit MsgID to its string representation (MMDDHHMMSSGGGGGQQQQQ).
func MsgIDString2Uint64 ¶
MsgIDString2Uint64 converts a MsgID string representation (MMDDHHMMSSGGGGGQQQQQ) back to its 64-bit integer form.
func RemoveSign ¶
RemoveSign 移除发送内容中的签名。 - 成功移除时,signature不为空,返回去除签名后的content和signature; - 不符合签名规则,返回原内容
func SplitMsgID ¶
SplitMsgID extracts the time components, gateway ID, and sequence ID from a 64-bit MsgID.
func TimeStamp2Str ¶
TimeStamp2Str converts a timestamp (MMDDHHMMSS format uint32) to a 10-byte string. It pads with leading zeros if necessary.
func Utf8ToUcs2 ¶
func Utf8ToUcs2Back ¶
func Utf8ToUcs2Pooled ¶
func WriteHeader ¶
WriteHeader encodes and writes a Header to a packet.Writer, including the TotalLength.
func WriteHeaderNoLength ¶
WriteHeaderNoLength encodes and writes a Header to a packet.Writer, excluding the TotalLength. This is typically used when the total length needs to be calculated and written later.
Types ¶
type CommandID ¶
type CommandID uint32
CommandID represents the command identifier for CMPP PDUs.
const ( // CommandRequestNone is a placeholder for no request command. CommandRequestNone CommandID = 0x00000000 + iota // CommandConnect represents the CMPP_CONNECT request. CommandConnect // 请求连接 // CommandTerminate represents the CMPP_TERMINATE request. CommandTerminate // 终止连接 // CommandSubmit represents the CMPP_SUBMIT request. CommandSubmit // 提交短信 // CommandDeliver represents the CMPP_DELIVER request (used for MO messages). CommandDeliver // 短信下发 // CommandQuery represents the CMPP_QUERY request. CommandQuery // 发送短信状态查询 // CommandCancel represents the CMPP_CANCEL request. CommandCancel // 删除短信 // CommandActiveTest represents the CMPP_ACTIVE_TEST request. CommandActiveTest // 激活测试 // CommandFwd represents the CMPP_FWD request (message forwarding). CommandFwd // 消息前转 // CommandMtRoute represents the CMPP_MT_ROUTE request. CommandMtRoute // MT 路由请求 // CommandMoRoute represents the CMPP_MO_ROUTE request. CommandMoRoute // MO 路由请求 // CommandGetRoute represents the CMPP_GET_ROUTE request. CommandGetRoute // 获取路由请求 // CommandMtRouteUpdate represents the CMPP_MT_ROUTE_UPDATE request. CommandMtRouteUpdate // MT 路由更新 // CommandMoRouteUpdate represents the CMPP_MO_ROUTE_UPDATE request. CommandMoRouteUpdate // MO 路由更新 // CommandPushMtRouteUpdate represents the CMPP_PUSH_MT_ROUTE_UPDATE request. CommandPushMtRouteUpdate // MT 路由更新 // CommandPushMoRouteUpdate represents the CMPP_PUSH_MO_ROUTE_UPDATE request. CommandPushMoRouteUpdate // MO 路由更新 )
CMPP Command IDs for requests.
const ( // CommandResponseNone is a placeholder for no response command. CommandResponseNone CommandID = 0x80000000 + iota // CommandConnectResp represents the CMPP_CONNECT_RESP response. CommandConnectResp // 请求连接应答 // CommandTerminateResp represents the CMPP_TERMINATE_RESP response. CommandTerminateResp // 终止连接应答 // CommandSubmitResp represents the CMPP_SUBMIT_RESP response. CommandSubmitResp // 提交短信应答 // CommandDeliverResp represents the CMPP_DELIVER_RESP response. CommandDeliverResp // 短信下发应答 // CommandQueryResp represents the CMPP_QUERY_RESP response. CommandQueryResp // 发送短信状态查询应答 // CommandCancelResp represents the CMPP_CANCEL_RESP response. CommandCancelResp // 删除短信应答 // CommandActiveTestResp represents the CMPP_ACTIVE_TEST_RESP response. CommandActiveTestResp // 激活测试应答 // CommandFwdResp represents the CMPP_FWD_RESP response. CommandFwdResp // 消息前转应答 // CommandMtRouteResp represents the CMPP_MT_ROUTE_RESP response. CommandMtRouteResp // MT 路由请求应答 // CommandMoRouteResp represents the CMPP_MO_ROUTE_RESP response. CommandMoRouteResp // MO 路由请求应答 // CommandGetRouteResp represents the CMPP_GET_ROUTE_RESP response. CommandGetRouteResp // 获取路由请求应答 // CommandMtRouteUpdateResp represents the CMPP_MT_ROUTE_UPDATE_RESP response. CommandMtRouteUpdateResp // MT 路由更新应答 // CommandMoRouteUpdateResp represents the CMPP_MO_ROUTE_UPDATE_RESP response. CommandMoRouteUpdateResp // MO 路由更新应答 // CommandPushMtRouteUpdateResp represents the CMPP_PUSH_MT_ROUTE_UPDATE_RESP response. CommandPushMtRouteUpdateResp // MT 路由更新应答 // CommandPushMoRouteUpdateResp represents the CMPP_PUSH_MO_ROUTE_UPDATE_RESP response. CommandPushMoRouteUpdateResp // MO 路由更新应答 )
CMPP Command IDs for responses.
func (CommandID) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for CommandID.
func (*CommandID) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for CommandID.
type FuncWithError ¶
type FuncWithError func() error
type Header ¶
type Header struct {
// TotalLength is the total length of the PDU in bytes, including the header.
TotalLength uint32
// CommandID identifies the type of the CMPP command or response.
CommandID CommandID
// SequenceID is the sequence number of the message, used for matching requests and responses.
SequenceID uint32
}
Header represents the common header structure for all CMPP PDUs.
func NewHeaderFromBytes ¶
NewHeaderFromBytes decodes a Header from a byte slice.
func NewHeaderFromReader ¶
NewHeaderFromReader decodes a Header from an io.Reader.
func PeekHeader ¶
PeekHeader reads the header bytes from a byte slice without consuming them and decodes them into a Header struct.
func ReadHeader ¶
ReadHeader reads and decodes a Header from a packet.Reader.
type SubPduDeliveryContent ¶
type SubPduDeliveryContent struct {
// MsgID is the message identifier generated by the ISMG when the SP submitted the message (CMPP_SUBMIT).
// 8 bytes.
MsgID uint64
// Stat is the status of the sent message (7 bytes).
// Values are similar to SMPP stat field definitions:
// DELIVRD: Message is delivered to destination
// EXPIRED: Message validity period has expired
// DELETED: Message has been deleted.
// ACCEPTD: Message is in accepted state (i.e., manually read by customer service)
// UNKNOWN: Message is in invalid state
// REJECTD: Message is in a rejected state
Stat string
// SubmitTime is the time the message was submitted to the downstream gateway (10 bytes, YYMMDDHHMM format).
SubmitTime string
// DoneTime is the time the status report was received from the downstream gateway (10 bytes, YYMMDDHHMM format).
DoneTime string
// DestTerminalID is the destination terminal MSISDN (21 bytes).
// This is the target terminal from the SP's CMPP_SUBMIT message.
DestTerminalID string
// SMSCSequence is the sequence number from the SMSC's status report message body (4 bytes).
SMSCSequence uint32
}
SubPduDeliveryContent represents the content structure of a CMPP Deliver PDU when it's a status report.
func (*SubPduDeliveryContent) IDecode ¶
func (s *SubPduDeliveryContent) IDecode(data []byte) (err error)
IDecode decodes the byte slice into a SubPduDeliveryContent.
func (*SubPduDeliveryContent) IEncode ¶
func (s *SubPduDeliveryContent) IEncode() ([]byte, error)
IEncode encodes the SubPduDeliveryContent into a byte slice.