You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.6 KiB
Go

1 year ago
package common
import (
"bytes"
"encoding/binary"
"git.cheetah.cat/cheetah/opentetraflex-go/common/tmkind"
1 year ago
)
type TerminalMessageResponse struct {
Length uint16
Kind tmkind.TetraFlexTerminalMessageKinds
Handler uint32
data bytes.Buffer
payload []byte
}
type HandlerResponse struct {
Success bool
Error error
TMR *TerminalMessageResponse
}
1 year ago
func NewTerminalMessageResponse(length uint16, kind tmkind.TetraFlexTerminalMessageKinds, handler uint32, payload []byte) (tm *TerminalMessageResponse, err error) {
tm = &TerminalMessageResponse{
Length: length,
Kind: kind,
Handler: handler,
data: *bytes.NewBuffer(payload),
payload: payload,
}
return tm, nil
}
func (tmr *TerminalMessageResponse) Bytes() []byte {
return tmr.payload
}
func (tmr *TerminalMessageResponse) readUInt32() (result uint32, err error) {
temp := make([]byte, 4)
if _, err = tmr.data.Read(temp); err != nil {
return result, err
}
result = binary.LittleEndian.Uint32(temp)
return result, nil
}
func (tmr *TerminalMessageResponse) readUInt16() (result uint16, err error) {
temp := make([]byte, 2)
if _, err = tmr.data.Read(temp); err != nil {
return result, err
}
result = binary.LittleEndian.Uint16(temp)
return result, nil
}
func (tmr *TerminalMessageResponse) readByte() (result byte, err error) {
result, err = tmr.data.ReadByte()
return result, err
}
func (tmr *TerminalMessageResponse) skipByte() (err error) {
_, err = tmr.data.ReadByte()
return err
}
func (tmr *TerminalMessageResponse) skipBytes(count int) (err error) {
for i := 0; i < count; i++ {
_, err = tmr.data.ReadByte()
if err != nil {
return err
}
}
return nil
}
func (tmr *TerminalMessageResponse) readAddress() (result TetraFlexAddress, err error) {
kindByte, err := tmr.readByte()
if err != nil {
return result, err
}
result.Kind = TetraFlexAddressKind(kindByte)
if err = tmr.skipByte(); err != nil {
return result, err
}
if err = tmr.skipByte(); err != nil {
return result, err
}
if err = tmr.skipByte(); err != nil {
return result, err
}
result.TSI.SSI, err = tmr.readUInt32()
if err != nil {
return result, err
}
result.TSI.MCC, err = tmr.readUInt16()
if err != nil {
return result, err
}
result.TSI.MCC, err = tmr.readUInt16()
if err != nil {
return result, err
}
textLength, err := tmr.readByte()
if err != nil {
return result, err
}
if err = tmr.skipByte(); err != nil {
return result, err
}
if err = tmr.skipByte(); err != nil {
return result, err
}
if err = tmr.skipByte(); err != nil {
return result, err
}
textBytes := make([]byte, textLength)
if _, err = tmr.data.Read(textBytes); err != nil {
return result, err
}
for i := textLength; i < 24; i++ {
if err = tmr.skipByte(); err != nil {
return result, err
}
}
return result, nil
}
func (tmr *TerminalMessageResponse) readString(count int) (result string, err error) {
textBytes := make([]byte, count)
if _, err = tmr.data.Read(textBytes); err != nil {
return result, err
}
return string(textBytes), nil
}
1 year ago
func (tmr *TerminalMessageResponse) AsAuthenticationChallenge() (authChallenge AuthenticationChallenge, err error) {
authChallenge.RandomSeed = make([]byte, 10)
if _, err = tmr.data.Read(authChallenge.RandomSeed); err != nil {
return authChallenge, err
}
tmr.data.ReadByte()
tmr.data.ReadByte()
authChallenge.RandomChallenge = make([]byte, 10)
if _, err = tmr.data.Read(authChallenge.RandomChallenge); err != nil {
return authChallenge, err
}
flagByte, err := tmr.data.ReadByte()
if err != nil {
return authChallenge, err
}
authChallenge.PinRequested = flagByte&1 != 0
authChallenge.SecurityKeyRequested = flagByte&2 != 0
authChallenge.Response = make([]byte, 4)
if _, err = tmr.data.Read(authChallenge.Response); err != nil {
return authChallenge, err
}
return authChallenge, nil
}
func (tmr *TerminalMessageResponse) AsRegistrationInfo() (regInfo RegistrationInfo, err error) {
if regInfo.ConnectionState, err = tmr.readUInt32(); err != nil {
return regInfo, err
}
if regInfo.RemoteVersion, err = tmr.readUInt32(); err != nil {
return regInfo, err
}
if regInfo.Address, err = tmr.readAddress(); err != nil {
return regInfo, err
}
if regInfo.MyNumber, err = tmr.readUInt32(); err != nil {
return regInfo, err
}
if regInfo.MyShortNumber, err = tmr.readUInt32(); err != nil {
return regInfo, err
}
if err = tmr.skipBytes(4); err != nil {
return regInfo, err
}
if regInfo.CallCount, err = tmr.readByte(); err != nil {
return regInfo, err
}
if regInfo.StreamCount, err = tmr.readByte(); err != nil {
return regInfo, err
}
if err = tmr.skipBytes(2); err != nil {
return regInfo, err
}
if regInfo.MaxVersion, err = tmr.readUInt32(); err != nil {
return regInfo, err
}
if regInfo.RoamingRef, err = tmr.readUInt32(); err != nil {
return regInfo, err
}
if regInfo.Description, err = tmr.readString(64); err != nil {
return regInfo, err
}
if err != nil {
return regInfo, err
}
1 year ago
if regInfo.RemoteVersion > 20120702 {
port1, err := tmr.readUInt16()
if err != nil {
return regInfo, err
}
port2, err := tmr.readUInt16()
if err != nil {
return regInfo, err
}
regInfo.RTPPorts = []uint16{port1, port2}
}
return regInfo, nil
}
func (tmr *TerminalMessageResponse) AsKeepAliveChallenge() (keepAliveChallenge KeepAliveChallenge, err error) {
if keepAliveChallenge.Value1, err = tmr.readUInt16(); err != nil {
return keepAliveChallenge, err
}
if keepAliveChallenge.Value2, err = tmr.readUInt16(); err != nil {
return keepAliveChallenge, err
}
if keepAliveChallenge.Value3, err = tmr.readUInt32(); err != nil {
return keepAliveChallenge, err
}
return keepAliveChallenge, nil
}