From 6436c86e0c53d9fbd83d883f76cbd8a267b6428e Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 7 Feb 2022 20:50:53 +0200 Subject: Implement initial PoC for Kodi --- m4p/message.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 m4p/message.go (limited to 'm4p/message.go') diff --git a/m4p/message.go b/m4p/message.go new file mode 100644 index 0000000..34a9383 --- /dev/null +++ b/m4p/message.go @@ -0,0 +1,91 @@ +package m4p + +import ( + "bytes" + "encoding/json" +) + +type MessageType string + +// MessageType enums. +const ( + Magic4PCAdMessage MessageType = "magic4pc_ad" + SubSensorMessage MessageType = "sub_sensor" + RemoteUpdateMessage MessageType = "remote_update" + InputMessage MessageType = "input" + KeepAliveMessage MessageType = "keepalive" +) + +// Message format sent over the wire. +type Message struct { + Type MessageType `json:"t"` + Version int `json:"version"` + *DeviceInfo + *Register + *RemoteUpdate + *Input +} + +// NewMessage initializes a message with the type and protocol version. +func NewMessage(typ MessageType) Message { + return Message{ + Type: typ, + Version: protocolVersion, + } +} + +// DeviceInfo represents a magic4pc server. +type DeviceInfo struct { + Model string `json:"model"` + IPAddr string `json:"-"` + Port int `json:"port"` + MAC string `json:"mac"` +} + +// Register payload for registering a new client on the server. +type Register struct { + UpdateFrequency int `json:"updateFreq"` + Filter []string `json:"filter"` +} + +// Input event (key pressed). +type Input struct { + Parameters struct { + KeyCode int `json:"keyCode"` + IsDown bool `json:"isDown"` + } `json:"parameters"` +} + +// RemoteUpdate event, sensor data from the magic remote. +type RemoteUpdate struct { + // filters []string + Payload []byte `json:"payload"` +} + +// type ( +// ReturnValue uint8 +// DeviceID uint8 +// Coordinates struct{ X, Y int32 } +// Gyroscope struct{ X, Y, Z float32 } +// Acceleration struct{ X, Y, Z float32 } +// Quaternion struct{ Q0, Q1, Q2, Q3 float32 } +// ) + +// func (ru RemoteUpdate) Coordinates() Coordinates { +// return Coordinates{} +// } + +// func (ru RemoteUpdate) Acceleration() Acceleration { +// return Acceleration{} +// } + +func decode(b []byte) (Message, error) { + var m Message + dec := json.NewDecoder(bytes.NewReader(b)) + dec.DisallowUnknownFields() + if err := dec.Decode(&m); err != nil { + return Message{}, err + } + + return m, nil +} -- cgit v1.2.3