start of proto

This commit is contained in:
jimZAH 2024-04-21 21:49:42 +01:00
commit 87977ab798
3 changed files with 196 additions and 0 deletions

13
main.go Normal file
View File

@ -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)
}

55
proto/statshare.go Normal file
View File

@ -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
}
}

128
proto/types.go Normal file
View File

@ -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
}