commit 87977ab7987693283c48be19c9b17446357406e1 Author: jimZAH Date: Sun Apr 21 21:49:42 2024 +0100 start of proto diff --git a/main.go b/main.go new file mode 100644 index 0000000..13fdc06 --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + + "github.com/statsharev2/proto" +) + +func main() { + client := proto.Init("1234567894321", 234) + raw := client.Connect() + fmt.Println(raw) +} diff --git a/proto/statshare.go b/proto/statshare.go new file mode 100644 index 0000000..4e3c139 --- /dev/null +++ b/proto/statshare.go @@ -0,0 +1,55 @@ +package proto + +import ( + "bytes" + "encoding/binary" + "log" + "strconv" +) + +func Init(mpan string, clientID clientID) *Session { + n, err := strconv.ParseUint(mpan, 10, 64) + if err != nil { + log.Fatal("error with provided mpan") + } + + session := new(Session) + session.Header.Version = VERSION + session.Header.ClientID = clientID + session.Mpan = n + + return session +} + +/* connecting to server */ +func (s *Session) Connect() []byte { + if s.State != NOT_CONNECTED { + return nil + } + + s.Header.Flag = AUTH_REQUEST + s.Header.DataLen = 64 + + Request := Request{ + Akey_0: s.Mpan, + } + + buff := make([]byte, 0, 16) + buffer := bytes.NewBuffer(buff) + if err := binary.Write(buffer, binary.BigEndian, s.Header); err != nil { + log.Fatal(err) + } + + if err := binary.Write(buffer, binary.BigEndian, Request); err != nil { + log.Fatal(err) + } + + s.State = SENT_AUTH + return buffer.Bytes() +} + +func (s *Session) SendData() { + if s.State != CONNECTED { + return + } +} diff --git a/proto/types.go b/proto/types.go new file mode 100644 index 0000000..9557bff --- /dev/null +++ b/proto/types.go @@ -0,0 +1,128 @@ +package proto + +type ( + clientID uint16 + flag uint8 + state uint8 + version uint8 +) + +/* +0 - AUTH REQUEST +1 - AUTH RESPONSE +2 - CLIENT DATA +254 - ACK +255 - NAK +*/ +const ( + AUTH_REQUEST flag = iota + AUTH_RESPONSE + CLIENT_DATA + ACK flag = 254 + NAK flag = 255 +) + +const ( + NOT_CONNECTED state = iota + SENT_AUTH + AUTH_TOT + AUTHENTICATED + CONNECTED + SENT_DATA + RECEIVED_ACK + RECEIVED_NAK +) + +const ( + VERSION version = 1 +) + +/* +=================================================== +| VERSION 1 byte | FLAG 1 byte | CLIENT ID 2 byte | +=================================================== +| LEN OF DATA 4 bytes | +=================================================== +*/ +type Header struct { + Version version + Flag flag + ClientID clientID + DataLen uint32 +} + +/* +AUTH DATA (REQUEST) +=================================================== +| AUTH KEY 0 4 bytes | +=================================================== +*/ + +type Request struct { + Akey_0 uint64 +} + +/* +AUTH DATA (RESPONSE) +=================================================== +| CLIENT KEY 0 4 bytes | +=================================================== +=================================================== +| CLIENT KEY 1 4 bytes | +=================================================== +=================================================== +| CLIENT KEY 2 4 bytes | +=================================================== +=================================================== +| CLIENT KEY 3 4 bytes | +=================================================== +*/ +type Response struct { + CKey_0 uint32 + Ckey_1 uint32 + Ckey_2 uint32 + Ckey_3 uint32 +} + +/* +CLIENT DATA +=================================================== +| CLIENT KEY 0 4 bytes | +=================================================== +=================================================== +| CLIENT KEY 1 4 bytes | +=================================================== +=================================================== +| CLIENT KEY 2 4 bytes | +=================================================== +=================================================== +| CLIENT KEY 3 4 bytes | +=================================================== +=================================================== +| NOW 4 bytes | +=================================================== +=================================================== +| DAY 4 bytes | +=================================================== +=================================================== +| EXPORT 4 bytes | +=================================================== +=================================================== +| GAS 4 bytes | +=================================================== +*/ +type Energy struct { + CKey Response + Now float32 + Day float32 + Export float32 + Gas float32 +} + +type Session struct { + CKey_p1 uint64 + Ckey_p2 uint64 + Header Header + Mpan uint64 + State state +}