Added Reading of Json with Protocol Definition
Some checks failed
Build / Build (push) Has been cancelled
Some checks failed
Build / Build (push) Has been cancelled
This commit is contained in:
parent
a743e940c0
commit
4b991729ea
47
proto/proto.go
Normal file
47
proto/proto.go
Normal file
@ -0,0 +1,47 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type HexByte byte
|
||||
|
||||
type ProtoComplete struct {
|
||||
Header ProtocolHeader `json:"protocol"`
|
||||
Protocols map[string][]ProtoMessage `json:"protocols"`
|
||||
}
|
||||
|
||||
type ProtocolHeader struct {
|
||||
StartByte HexByte `json:"start_byte"`
|
||||
MessageLength int `json:"message_length"`
|
||||
MaxPayload int `json:"max_payload"`
|
||||
Checksum string `json:"checksum"`
|
||||
}
|
||||
|
||||
type ProtoMessage struct {
|
||||
Name string `json:"name"`
|
||||
ID HexByte `json:"id"`
|
||||
Payload []ProtoMessagePayload `json:"payload,omitempty"` // optional falls kein payload
|
||||
}
|
||||
|
||||
type ProtoMessagePayload struct {
|
||||
Name string `json:"name"`
|
||||
DataType string `json:"type"`
|
||||
ArrayLength int `json:"array,omitempty"` // optional falls kein array
|
||||
}
|
||||
|
||||
func (h *HexByte) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil { // turn into go string
|
||||
return err
|
||||
}
|
||||
|
||||
i, err := strconv.ParseUint(s, 0, 8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*h = HexByte(i)
|
||||
return nil
|
||||
}
|
||||
23
proto/proto_test.go
Normal file
23
proto/proto_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadJson(t *testing.T) {
|
||||
con, err := os.ReadFile("../testdata/prot1.json")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read file %v", err)
|
||||
}
|
||||
|
||||
pc := &ProtoComplete{}
|
||||
|
||||
json.Unmarshal(con, pc)
|
||||
|
||||
log.Printf("%v", pc)
|
||||
|
||||
t.Fail()
|
||||
}
|
||||
138
testdata/prot1.json
vendored
138
testdata/prot1.json
vendored
@ -5,58 +5,88 @@
|
||||
"max_payload": 255,
|
||||
"checksum": "xor"
|
||||
},
|
||||
"messages_esp_to_pc": [
|
||||
{
|
||||
"name": "Clients",
|
||||
"id": "0xE1",
|
||||
"payload": [
|
||||
{ "name": "clientCount", "type": "uint8_t" },
|
||||
{ "name": "clientAvaiableBitMask", "type": "uint32_t" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Status",
|
||||
"id": "0xE2",
|
||||
"payload": [
|
||||
{ "name": "clientId", "type": "uint8_t" },
|
||||
{ "name": "mac", "type": "uint8_t", "array": 6 }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Pong",
|
||||
"id": "0xD1",
|
||||
"payload": [
|
||||
{ "name": "clientId", "type": "uint8_t" },
|
||||
{ "name": "ping", "type": "uint32_t" }
|
||||
]
|
||||
}
|
||||
],
|
||||
"messages_pc_to_esp": [
|
||||
{
|
||||
"name": "RequestPing",
|
||||
"id": "0xE1",
|
||||
"payload": [
|
||||
{ "name": "clientId", "type": "uint8_t" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "RequestStatus",
|
||||
"id": "0xE2",
|
||||
"payload": [
|
||||
{ "name": "clientId", "type": "uint8_t" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PrepareFirmwareUpdate",
|
||||
"id": "0xF1"
|
||||
},
|
||||
{
|
||||
"name": "FirmwareUpdateLine",
|
||||
"id": "0xF2",
|
||||
"payload": [
|
||||
{ "name": "data", "type": "uint8_t", "array": 240 }
|
||||
]
|
||||
}
|
||||
]
|
||||
"protocols": {
|
||||
"messages_esp_to_pc": [
|
||||
{
|
||||
"name": "Clients",
|
||||
"id": "0xE1",
|
||||
"payload": [
|
||||
{
|
||||
"name": "clientCount",
|
||||
"type": "uint8_t"
|
||||
},
|
||||
{
|
||||
"name": "clientAvaiableBitMask",
|
||||
"type": "uint32_t"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Status",
|
||||
"id": "0xE2",
|
||||
"payload": [
|
||||
{
|
||||
"name": "clientId",
|
||||
"type": "uint8_t"
|
||||
},
|
||||
{
|
||||
"name": "mac",
|
||||
"type": "uint8_t",
|
||||
"array": 6
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Pong",
|
||||
"id": "0xD1",
|
||||
"payload": [
|
||||
{
|
||||
"name": "clientId",
|
||||
"type": "uint8_t"
|
||||
},
|
||||
{
|
||||
"name": "ping",
|
||||
"type": "uint32_t"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"messages_pc_to_esp": [
|
||||
{
|
||||
"name": "RequestPing",
|
||||
"id": "0xE1",
|
||||
"payload": [
|
||||
{
|
||||
"name": "clientId",
|
||||
"type": "uint8_t"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "RequestStatus",
|
||||
"id": "0xE2",
|
||||
"payload": [
|
||||
{
|
||||
"name": "clientId",
|
||||
"type": "uint8_t"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PrepareFirmwareUpdate",
|
||||
"id": "0xF1"
|
||||
},
|
||||
{
|
||||
"name": "FirmwareUpdateLine",
|
||||
"id": "0xF2",
|
||||
"payload": [
|
||||
{
|
||||
"name": "data",
|
||||
"type": "uint8_t",
|
||||
"array": 240
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user