コード例 #1
0
ファイル: commands.go プロジェクト: evandertino/at
// Encode converts the query string into bytes according to the
// specified encoding.
func (u *Ussd) Encode(enc Encoding) ([]byte, error) {
	switch enc {
	case Encodings.Gsm7Bit:
		return pdu.Encode7Bit(u.String()), nil
	case Encodings.UCS2:
		return pdu.EncodeUcs2(u.String()), nil
	default:
		return nil, ErrUnknownEncoding
	}
}
コード例 #2
0
ファイル: sms.go プロジェクト: evandertino/at
// PDU serializes the message into octets ready to be transferred.
// Returns the number of TPDU bytes in the produced PDU.
// Complies with 3GPP TS 23.040.
func (s *Message) PDU() (int, []byte, error) {
	var buf bytes.Buffer
	if len(s.ServiceCenterAddress) < 1 {
		buf.WriteByte(0x00) // SMSC info length
	} else {
		_, octets, err := s.ServiceCenterAddress.PDU()
		if err != nil {
			return 0, nil, err
		}
		buf.WriteByte(byte(len(octets)))
		buf.Write(octets)
	}

	switch s.Type {
	case MessageTypes.Deliver:
		var sms smsDeliver
		sms.MessageTypeIndicator = byte(s.Type)
		sms.MoreMessagesToSend = s.MoreMessagesToSend
		sms.LoopPrevention = s.LoopPrevention
		sms.ReplyPath = s.ReplyPathExists
		sms.UserDataHeaderIndicator = s.UserDataStartsWithHeader
		sms.StatusReportIndication = s.StatusReportIndication

		addrLen, addr, err := s.Address.PDU()
		if err != nil {
			return 0, nil, err
		}
		var addrBuf bytes.Buffer
		addrBuf.WriteByte(byte(addrLen))
		addrBuf.Write(addr)
		sms.OriginatingAddress = addrBuf.Bytes()

		sms.ProtocolIdentifier = 0x00 // Short Message Type 0
		sms.DataCodingScheme = byte(s.Encoding)
		sms.ServiceCentreTimestamp = s.ServiceCenterTime.PDU()

		var userData []byte
		if s.Encoding == Encodings.Gsm7Bit {
			userData = pdu.Encode7Bit(s.Text)
			sms.UserDataLength = byte(len(s.Text))
		} else if s.Encoding == Encodings.UCS2 {
			userData = pdu.EncodeUcs2(s.Text)
			sms.UserDataLength = byte(len(userData))
		} else {
			return 0, nil, ErrUnknownEncoding
		}

		sms.UserDataLength = byte(len(userData))
		sms.UserData = userData
		n, err := buf.Write(sms.Bytes())
		if err != nil {
			return 0, nil, err
		}
		return n, buf.Bytes(), nil
	case MessageTypes.Submit:
		var sms smsSubmit
		sms.MessageTypeIndicator = byte(s.Type)
		sms.RejectDuplicates = s.RejectDuplicates
		sms.ValidityPeriodFormat = byte(s.VPFormat)
		sms.ReplyPath = s.ReplyPathExists
		sms.UserDataHeaderIndicator = s.UserDataStartsWithHeader
		sms.StatusReportRequest = s.StatusReportRequest
		sms.MessageReference = s.MessageReference

		addrLen, addr, err := s.Address.PDU()
		if err != nil {
			return 0, nil, err
		}
		var addrBuf bytes.Buffer
		addrBuf.WriteByte(byte(addrLen))
		addrBuf.Write(addr)
		sms.DestinationAddress = addrBuf.Bytes()

		sms.ProtocolIdentifier = 0x00 // Short Message Type 0
		sms.DataCodingScheme = byte(s.Encoding)

		switch s.VPFormat {
		case ValidityPeriodFormats.Relative:
			sms.ValidityPeriod = byte(s.VP.Octet())
		case ValidityPeriodFormats.Absolute, ValidityPeriodFormats.Enhanced:
			return 0, nil, ErrNonRelative
		}

		var userData []byte
		if s.Encoding == Encodings.Gsm7Bit {
			userData = pdu.Encode7Bit(s.Text)
			sms.UserDataLength = byte(len(s.Text))
		} else if s.Encoding == Encodings.UCS2 {
			userData = pdu.EncodeUcs2(s.Text)
			sms.UserDataLength = byte(len(userData))
		} else {
			return 0, nil, ErrUnknownEncoding
		}

		sms.UserData = userData
		n, err := buf.Write(sms.Bytes())
		if err != nil {
			return 0, nil, err
		}
		return n, buf.Bytes(), nil
	default:
		return 0, nil, ErrUnknownMessageType
	}
}