jkprotocol

package
v0.0.0-...-a9e9222 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Pacakge jkprotocol implement jk protocol generate, parse and use.

Index

Constants

View Source
const (
	JK_P_HEADER_MARK_NAME     = "JK"
	JK_P_HEADER_MAIN_VERSION  = 0
	JK_P_HEADER_SLAVE_VERSION = 1
	JK_P_HEADER_LAST_VERSION  = 0
)

Header const define

View Source
const (
	JK_P_HEADER_TYPE_SEND     = 0
	JK_P_HEADER_TYPE_RESPONSE = 1
	JK_P_HEADER_TYPE_NOTIFY   = 2
)

Header main command type

View Source
const (
	JK_P_HEADER_SINGLE   = 0
	JK_P_HEADER_CONTINUE = 1
)
View Source
const (
	JK_P_CMD_DEVICE_CONNECT_QUERY         = 1
	JK_P_CMD_DEVICE_CONNECT_FILE_TRANSFER = 2
)

Header slave command depends on main command Device Connect

View Source
const (
	JK_PROTOCOL_C_REGISTER = 1 << iota
	JK_PROTOCOL_C_LEAVE
	JK_PROTOCOL_C_KEEPALIVE
)
View Source
const (
	JK_PROTOCOL_VERSION_1 = 1 << iota
	JK_PROTOCOL_VERSION_2
	JK_PROTOCOL_VERSION_3
	JK_PROTOCOL_VERSION_4
	JK_PROTOCOL_VERSION_5
)
View Source
const (
	JKProtoV4CryptNone = 0
	JKProtoV4CryptAES  = 1
)
View Source
const (
	JKP_V6_REGISTER_NAME  = "Register"
	JKP_V6_KEEPALIVE_NAME = "Keepalive"
	// transfer content to other program
	JKP_V6_TR_NAME = "TR"
)
View Source
const (
	JK_PROTOCOL_CMD_REGISTER = 1 << iota
	JK_PROTOCOL_CMD_NOTIFY   // 2
	JK_PROTOCOL_CMD_CONTROL  // 4
)
View Source
const (
	KF_CMD_QUERY = 1 << iota
	KF_CMD_CONTROL
	KF_CMD_NOTIFY
)
View Source
const (
	KF_SUBCMD_REGISTER  = 1
	KF_SUBCMD_KEEPALIVE = 2

	KF_SUBCMD_COMMAND = 0x100 // execute command
	KF_SUBCMD_FILE    = 0x101 // Get file info (bidirectional)

	KF_SUBCMD_OFFLINE = 0x500
)
View Source
const (
	JK_PROTOCOL_CMD_SAVEFILE = 1 << iota
)
View Source
const (
	JK_P_CMD_DEVICE_CONNECT = 1
)

Header main command

View Source
const (
	JK_P_HEADER_LENGTH = 20 // The header length is 20 bytes
)

Header const len define

View Source
const (
	V5_VERSION = 0.1
)

Variables

This section is empty.

Functions

func JKProtoV6Make

func JKProtoV6Make(cmd string, resp bool, id string, data interface{}) (string, error)

func JKProtoV6MakeKeepalive

func JKProtoV6MakeKeepalive(id string) (string, error)

func JKProtoV6MakeRegister

func JKProtoV6MakeRegister(id string, data interface{}) (string, error)

Types

type JKDeviceInfo

type JKDeviceInfo struct {
	Name  string
	Addr  string
	ID    int
	Model string
}

First query from other device contain information Response need.

func (*JKDeviceInfo) ToByte

func (di *JKDeviceInfo) ToByte() []byte

type JKFileTransfer

type JKFileTransfer struct {
	Addr string
	Port int
}

Used when need file transfer Use the addr and port to contact each other This will use tcp connect Response need.

func (*JKFileTransfer) ToByte

func (ft *JKFileTransfer) ToByte() []byte

type JKProtoInfo

type JKProtoInfo struct {
	Cmd string
	Seq uint32
	Id  string
}

type JKProtoUp

type JKProtoUp struct {
	PType int // proto type
	Proto *JKProtoV4

	Info JKProtoInfo
}

func JKProtoUpNew

func JKProtoUpNew(t int, Id string) (*JKProtoUp, error)

func (*JKProtoUp) JKProtoUpCommon

func (pup *JKProtoUp) JKProtoUpCommon(cmd string) ([]byte, error)

Command of register,...

func (*JKProtoUp) JKProtoUpInit

func (pup *JKProtoUp) JKProtoUpInit(ack bool, length uint32, content []byte) ([]byte, error)

Return bytes Generate command with protocol v4

func (*JKProtoUp) JKProtoUpKeepalive

func (pup *JKProtoUp) JKProtoUpKeepalive() ([]byte, error)

func (*JKProtoUp) JKProtoUpParse

func (pup *JKProtoUp) JKProtoUpParse(data []byte) error

Parse bytes

func (*JKProtoUp) JKProtoUpRegister

func (pup *JKProtoUp) JKProtoUpRegister() ([]byte, error)

func (*JKProtoUp) JKProtoUpSeq

func (pup *JKProtoUp) JKProtoUpSeq() int

Get last command's sequence

func (*JKProtoUp) JKProtoUpSeqAdd

func (pup *JKProtoUp) JKProtoUpSeqAdd(v int32) error

Auto add sequence, or set sequence

type JKProtoV4

type JKProtoV4 struct {
	Header JKProtoV4Header
	Body   JKProtoV4Body
}

func JKProtoV4New

func JKProtoV4New() (*JKProtoV4, error)

func (*JKProtoV4) Debug

func (p *JKProtoV4) Debug()

func (*JKProtoV4) GenerateBody

func (p *JKProtoV4) GenerateBody(data string) (*JKProtoV4Body, error)

Set Body

func (*JKProtoV4) GenerateBodyByte

func (p *JKProtoV4) GenerateBodyByte(data []byte) (*JKProtoV4Body, error)

func (*JKProtoV4) GenerateHeader

func (p *JKProtoV4) GenerateHeader(crypt uint, ack bool, length uint32) (*JKProtoV4Header, error)

Set header

func (*JKProtoV4) Parse

func (p *JKProtoV4) Parse(data []byte) error

func (*JKProtoV4) ToByte

func (p *JKProtoV4) ToByte() ([]byte, error)

Generate to Bytes

type JKProtoV4Body

type JKProtoV4Body struct {
	Data []byte
}

type JKProtoV4Header

type JKProtoV4Header struct {
	Version uint
	Crypt   uint
	ACK     bool
	Length  uint32
}

type JKProtoV6

type JKProtoV6 struct {
	H JKProtoV6Header
	B interface{}
}

func JKProtoV6Parse

func JKProtoV6Parse(data string) (*JKProtoV6, error)

func (*JKProtoV6) JKProtoV6MakeCommon

func (p *JKProtoV6) JKProtoV6MakeCommon(cmd, id string, resp bool, data interface{}) (string, error)

func (*JKProtoV6) JKProtoV6MakeCommonResponse

func (p *JKProtoV6) JKProtoV6MakeCommonResponse(data interface{}) (string, error)

func (*JKProtoV6) JKProtoV6MakeKeepaliveResponse

func (p *JKProtoV6) JKProtoV6MakeKeepaliveResponse() (string, error)

func (*JKProtoV6) JKProtoV6MakeTR

func (p *JKProtoV6) JKProtoV6MakeTR(id string, resp bool, data interface{}) (string, error)

type JKProtoV6Header

type JKProtoV6Header struct {
	V           string
	C           string
	ID          string
	T           int64
	R           bool
	Seq         int64
	Transaction int64
}

type JKProtocol

type JKProtocol struct {
	// contains filtered or unexported fields
}

func NewJKProtocol

func NewJKProtocol() *JKProtocol

func ParseJKProtocol

func ParseJKProtocol(data string) *JKProtocol

Server parse files

func (*JKProtocol) Command

func (p *JKProtocol) Command() int

func (*JKProtocol) GenerateControlSaveFile

func (p *JKProtocol) GenerateControlSaveFile(filename string, data string) string

func (*JKProtocol) GenerateNotifySaveFile

func (p *JKProtocol) GenerateNotifySaveFile(filename string) string

func (*JKProtocol) GenerateRegister

func (p *JKProtocol) GenerateRegister(id string) string

func (*JKProtocol) GenerateResponseFail

func (p *JKProtocol) GenerateResponseFail() string

func (*JKProtocol) GenerateResponseOK

func (p *JKProtocol) GenerateResponseOK() string

func (*JKProtocol) ID

func (p *JKProtocol) ID() string

func (*JKProtocol) ParseFilename

func (p *JKProtocol) ParseFilename() string

func (*JKProtocol) ParseFilenameData

func (p *JKProtocol) ParseFilenameData() (string, string)

func (*JKProtocol) SubCommand

func (p *JKProtocol) SubCommand() int

type JKProtocolHead

type JKProtocolHead struct {
	// contains filtered or unexported fields
}

type JKProtocolWrap

type JKProtocolWrap struct {
	Type    int // protocol type
	CmdType int
	Base    V5Base
}

func NewJKProtocolWrap

func NewJKProtocolWrap(ptype int) (*JKProtocolWrap, error)

func (*JKProtocolWrap) Keepalive

func (wrap *JKProtocolWrap) Keepalive(data string) (string, error)

func (*JKProtocolWrap) KeepaliveResponse

func (wrap *JKProtocolWrap) KeepaliveResponse(data string) (string, error)

func (*JKProtocolWrap) Leave

func (wrap *JKProtocolWrap) Leave(data string) (string, error)

func (*JKProtocolWrap) LeaveResponse

func (wrap *JKProtocolWrap) LeaveResponse(data string) (string, error)

func (*JKProtocolWrap) Parse

func (wrap *JKProtocolWrap) Parse(data string) (string, error)

Set cmd type for which command Give common response of the sender

func (*JKProtocolWrap) Register

func (wrap *JKProtocolWrap) Register(data string) (string, error)

func (*JKProtocolWrap) RegisterResponse

func (wrap *JKProtocolWrap) RegisterResponse(data string) (string, error)

type JK_P_DataWithHeader

type JK_P_DataWithHeader struct {
	// contains filtered or unexported fields
}

func JKFromBytes

func JKFromBytes(buf []byte) (*JK_P_DataWithHeader, error)

Parse buf out to DataWithHeader

func (*JK_P_DataWithHeader) Data

func (header *JK_P_DataWithHeader) Data() []byte

Get data from dataWithHeader

func (*JK_P_DataWithHeader) FromBytes

func (header *JK_P_DataWithHeader) FromBytes(buf []byte) error

func (*JK_P_DataWithHeader) GetData

func (h *JK_P_DataWithHeader) GetData() []byte

Get the data

func (*JK_P_DataWithHeader) Header

func (header *JK_P_DataWithHeader) Header() *JK_P_Header

Get header from dataWithHeader

func (*JK_P_DataWithHeader) SetData

func (h *JK_P_DataWithHeader) SetData(data []byte)

Change the data

func (*JK_P_DataWithHeader) ToByte

func (h *JK_P_DataWithHeader) ToByte() []byte

type JK_P_Header

type JK_P_Header struct {
	// contains filtered or unexported fields
}

func JKGenearteHeaderSimpleNotify

func JKGenearteHeaderSimpleNotify(seq, mCmd, sCmd int) *JK_P_Header

func JKGenerateHeaderSimple

func JKGenerateHeaderSimple(seq, mCmd, sCmd int) *JK_P_Header

default version to use

func JKGenerateHeaderSimpleResponse

func JKGenerateHeaderSimpleResponse(seq, mCmd, sCmd int) *JK_P_Header

func JKGenerateProtocolHeader

func JKGenerateProtocolHeader(cmdType, cont, seq int, mCmd, sCmd int, length int) *JK_P_Header

GenerateProtocolHeader with args

func (*JK_P_Header) GetContinued

func (h *JK_P_Header) GetContinued() int

func (*JK_P_Header) GetLength

func (h *JK_P_Header) GetLength() int

func (*JK_P_Header) GetMainCommand

func (h *JK_P_Header) GetMainCommand() int

func (*JK_P_Header) GetSequence

func (h *JK_P_Header) GetSequence() int

func (*JK_P_Header) GetSlaveCommand

func (h *JK_P_Header) GetSlaveCommand() int

func (*JK_P_Header) GetType

func (h *JK_P_Header) GetType() int

func (*JK_P_Header) GetVersions

func (h *JK_P_Header) GetVersions() string

func (*JK_P_Header) SetContinued

func (h *JK_P_Header) SetContinued(c int) error

func (*JK_P_Header) SetData

func (h *JK_P_Header) SetData(data []byte) *JK_P_DataWithHeader

Return a new pointer to the header with data and header

func (*JK_P_Header) SetLength

func (h *JK_P_Header) SetLength(length int) error

func (*JK_P_Header) SetMainCommand

func (h *JK_P_Header) SetMainCommand(mc int) error

func (*JK_P_Header) SetSequence

func (h *JK_P_Header) SetSequence(seq int) error

func (*JK_P_Header) SetSlaveCommand

func (h *JK_P_Header) SetSlaveCommand(sc int) error

func (*JK_P_Header) SetType

func (h *JK_P_Header) SetType(t int) error

func (*JK_P_Header) SetVersions

func (h *JK_P_Header) SetVersions(m, s, l int) error

func (*JK_P_Header) ToBytes

func (h *JK_P_Header) ToBytes() []byte

Convert between header struct and []bytes

type KFProtocol

type KFProtocol struct {
	Header KFProtocolHeader
	Body   KFProtocolBody
}

func KFProtocolParse

func KFProtocolParse(data []byte) (*KFProtocol, error)

func KFProtocolParseText

func KFProtocolParseText(data []byte) (*KFProtocol, error)

func NewKFProtocol

func NewKFProtocol() *KFProtocol

func (*KFProtocol) GenerateData

func (p *KFProtocol) GenerateData(dir bool) ([]byte, error)

func (*KFProtocol) GenerateDataText

func (p *KFProtocol) GenerateDataText(dir bool) ([]byte, error)

func (*KFProtocol) Init

func (p *KFProtocol) Init()

func (*KFProtocol) SetCmd

func (p *KFProtocol) SetCmd(cmd, subcmd uint, id []byte)

func (*KFProtocol) SetData

func (p *KFProtocol) SetData(data []byte)

func (*KFProtocol) SetResponseCode

func (p *KFProtocol) SetResponseCode(result bool, code int) error

type KFProtocolBody

type KFProtocolBody struct {
	Data []byte
}

type KFProtocolHeader

type KFProtocolHeader struct {
	Cmd         uint
	SubCmd      uint
	Direction   uint8 // direction for 0 send, 1 response
	Version     uint
	Transaction uint64
	Sequence    uint16
	Length      uint16
	Sign        [16]byte
	Ret         bool
	Code        int
	Id          [16]byte
	IdStr       string
	// contains filtered or unexported fields
}

type V5Base

type V5Base struct {
	PreHeader V5PreHeader `json:preheader`
	Header    V5Header
	Body      V5Body
}

func (*V5Base) GenCmd

func (base *V5Base) GenCmd(cmd, subcmd string, resp bool, data string) (string, error)

func (*V5Base) Keepalive

func (base *V5Base) Keepalive(data string) (string, error)

func (*V5Base) KeepaliveResponse

func (base *V5Base) KeepaliveResponse(data string) (string, error)

func (*V5Base) Leave

func (base *V5Base) Leave(data string) (string, error)

func (*V5Base) LeaveResponse

func (base *V5Base) LeaveResponse(data string) (string, error)

func (*V5Base) Parse

func (base *V5Base) Parse(data string) (interface{}, int, error)

func (*V5Base) Register

func (base *V5Base) Register(data string) (string, error)

func (*V5Base) RegisterReponse

func (base *V5Base) RegisterReponse(data string) (string, error)

type V5Body

type V5Body struct {
	Data interface{}
}

type V5Header

type V5Header struct {
	Cmd         string
	SubCmd      string
	Id          string
	Transaction string // it must be uniqueue
	Resp        bool
}

type V5PreHeader

type V5PreHeader struct {
	Version float32 `json:version`
	Crypto  int     // 0: no crypto , 1 aes
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL