Skip to content

Commit

Permalink
debuglink support for logreplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
solipsis committed Feb 15, 2019
1 parent 2702280 commit f568ce0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
12 changes: 6 additions & 6 deletions cmd/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var replayCmd = &cobra.Command{
lm := keepkey.LogMsg{}
json.Unmarshal([]byte(single), &lm)

rec := keepkey.Record{Messages: []keepkey.LogMsg{lm}}
keepkey.Replay(kk, rec)
replay := keepkey.Replay{Messages: []keepkey.LogMsg{lm}}
replay.Play(kk)
return
}

Expand All @@ -50,14 +50,14 @@ var replayCmd = &cobra.Command{
log.Fatal(err)
}

r := keepkey.Record{}
json.Unmarshal(buf, &r)
replay := keepkey.Replay{}
json.Unmarshal(buf, &replay)

if len(r.Messages) == 0 {
if len(replay.Messages) == 0 {
log.Fatal("Unable to parse messages, Validate that they are in valid JSON format")
}

keepkey.Replay(kk, r)
replay.Play(kk)
}

},
Expand Down
27 changes: 22 additions & 5 deletions pkg/keepkey/logreplay.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"reflect"
"strings"

"github.com/solipsis/go-keepkey/pkg/kkProto"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)

// Record blalahoeushtouthoesaut
type Record struct {
// Replay is a collection of log messages to play against the device
type Replay struct {
Messages []LogMsg `json:"messages"`
}

// LogMsg is a message sent to or from the device conforming to the keepkey log spec
type LogMsg struct {
Type string `json:"message_type"`
Interface string `json:"interface"` // TODO: should probably make this an enum
FromDevice bool `json:"from_device"`
Msg *json.RawMessage `json:"message"`
}

// Replay plays a list of messages back to the device
//
func Replay(kk *Keepkey, r Record) {
func (r *Replay) Play(kk *Keepkey) {

for _, msg := range r.Messages {

Expand All @@ -45,11 +48,25 @@ func Replay(kk *Keepkey, r Record) {
log.Fatal(err)
}

err = kk.SendRaw(proto)
// determine interface to send over (standard vs. debug)
debug := strings.Contains(strings.ToLower(msg.Interface), "debug")
var transportIface io.ReadWriteCloser
if debug {
transportIface = kk.transport.debug
} else {
transportIface = kk.transport.conn
}

err = kk.SendRaw(proto, transportIface)
if err != nil {
log.Fatal(err)
}

// If we send a debug button press then there is no response from device
if debug && msg.Type == "DebugLinkDecision" {
continue
}

_, err = kk.ReceiveRaw()
if err != nil {
log.Fatal(err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/keepkey/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keepkey
import (
"encoding/binary"
"fmt"
"io"
"reflect"
"strings"

Expand All @@ -12,7 +13,8 @@ import (

// SendRaw sends a message to the device without waiting for a response
// This is useful for recreating previous exchanges with the device
func (kk *Keepkey) SendRaw(req proto.Message) error {
// The debug flag specifies wether to send over the standard or debug interface
func (kk *Keepkey) SendRaw(req proto.Message, transportIface io.Writer) error {
kk.log("Sending payload to device:\n%s:\n%s", kkProto.Name(kkProto.Type(req)), pretty(req))

// Construct message payload to chunk up
Expand Down Expand Up @@ -40,9 +42,8 @@ func (kk *Keepkey) SendRaw(req proto.Message) error {
copy(chunk[1+len(payload):], make([]byte, 63-len(payload)))
payload = nil
}
// send over to the device
// TODO: add ability for debuglikn
if _, err := kk.transport.conn.Write(chunk); err != nil {

if _, err := transportIface.Write(chunk); err != nil {
return err
}
}
Expand Down

0 comments on commit f568ce0

Please sign in to comment.