Skip to content

Commit

Permalink
pkg/core/method: define MethodCall.String method for debug
Browse files Browse the repository at this point in the history
  • Loading branch information
yogev-lb committed Jun 28, 2022
1 parent ad55b3b commit c62b435
Showing 1 changed file with 156 additions and 2 deletions.
158 changes: 156 additions & 2 deletions pkg/core/method/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"bytes"
"errors"

//"fmt"
//"log"

"github.com/open-source-firmware/go-tcg-storage/pkg/core/stream"
"github.com/open-source-firmware/go-tcg-storage/pkg/core/uid"
)
Expand Down Expand Up @@ -60,6 +63,8 @@ var (
)

type MethodCall struct {
iid uid.InvokingID
mid uid.MethodID
buf bytes.Buffer
// Used to verify detect programming errors
depth int
Expand All @@ -68,7 +73,7 @@ type MethodCall struct {

// Prepare a new method call
func NewMethodCall(iid uid.InvokingID, mid uid.MethodID, flags MethodFlag) *MethodCall {
m := &MethodCall{bytes.Buffer{}, 0, flags}
m := &MethodCall{iid, mid, bytes.Buffer{}, 0, flags}
m.buf.Write(stream.Token(stream.Call))
m.Bytes(iid[:])
m.Bytes(mid[:])
Expand All @@ -77,9 +82,158 @@ func NewMethodCall(iid uid.InvokingID, mid uid.MethodID, flags MethodFlag) *Meth
return m
}

// func (m *MethodCall) parseParams(params stream.List) {
// for i := 0; i < len(params); i++ {
// param := params[i]
// tok, ok1 := param.(stream.TokenType)
// if ok1 {
// if tok == stream.StartName {
// _, ok1 := params[i+1].([]byte) // name
// _, ok2 := params[i+2].(uint) // val2
// endToc, ok3 := params[i+3].(stream.TokenType)
// if ok1 && ok2 && ok3 && endToc == stream.EndName {
// //log.Printf("got tokenType %q on params[%d]. name: %s, index: %d", tok.String(), i, string(name), uint(val2))
// i += 3
// } else {
// _, ok1 := params[i+1].(uint) //val1
// _, ok2 := params[i+2].([]byte) //name
// endToc, ok3 := params[i+3].(stream.TokenType)
// if ok1 && ok2 && ok3 && endToc == stream.EndName {
// //log.Printf("got tokenType %q on params[%d]. index: %d, name: %s", tok.String(), i, uint(val1), string(name))
// i += 3
// } else {
// //log.Printf("got tokenType %q on params[%d]", tok.String(), i)
// }
// }
// }
// continue
// }
// lst, ok2 := param.(stream.List)
// if ok2 {
// //log.Printf("got list on params[%d]", i)
// m.parseParams(lst)
// continue
// }
// _, ok3 := param.(uint)
// if ok3 {
// //log.Printf("got uint on params[%d]", i)
// continue
// }
// _, ok4 := param.([]byte)
// if ok4 {
// //log.Printf("got []byte on params[%d]", i)
// continue
// }
// log.Printf("unknown value at params[%d], %v", i, param)
// }
// }

// func (m *MethodCall) String() string {
// var buff bytes.Buffer
// b, err := m.MarshalBinary()
// if err != nil {
// log.Printf("failed to marshal binary")
// return ""
// }

// req, err := stream.Decode(b)
// if err != nil {
// log.Printf("failed to decode m")
// return ""
// }
// if len(req) >= 6 {
// if tok, ok := req[0].(stream.TokenType); !ok || tok != stream.Call {
// log.Printf("expected first token to be Call")
// return ""
// }
// var uidStr uid.InvokingID
// var midstr uid.MethodID
// if iid, ok := req[1].([]byte); !ok {
// log.Printf("expected uid")
// return ""
// } else {
// copy(uidStr[:], iid[:8])
// }
// if mid, ok := req[2].([]byte); !ok {
// return ""
// } else {
// copy(midstr[:], mid[:8])
// }
// if params, ok := req[3].(stream.List); ok {
// m.parseParams(params)
// }

// // if tok, ok := req[4].(stream.TokenType); ok {
// // if lst, ok := req[5].(stream.List); ok {
// // log.Printf("got tokenType %q: %v", tok.String(), lst)
// // }
// // }
// if midstr == uid.MethodIDSMStartSession {
// reqBytes := req[3].(stream.List)
// hsn := reqBytes[0]
// spid := reqBytes[1].([]byte)
// readOnly := reqBytes[2].(uint)
// // for some reason readOnly is opposite - we set mc.Bool(!s.ReadOnly) so 0 means read-only -- go figure
// buff.WriteString(fmt.Sprintf("[MethodID: StartSession] hsn: %d, spid: %v, readOnly: %t", hsn, spid, readOnly == 0))
// } else if midstr == uid.OpalGet {
// buff.WriteString("[MethodID: Get]")
// } else if midstr == uid.OpalNext {
// buff.WriteString("[MethodID: Next]")
// } else if midstr == uid.OpalSet {
// reqBytes := req[3].(stream.List)
// _ = reqBytes[0].(stream.TokenType) //startList
// _ = reqBytes[1].(uint)
// lst := reqBytes[2].(stream.List)
// _ = lst[0].(stream.TokenType) //startName
// length := lst[1].(uint) //len
// name, ok := lst[2].([]byte) //data
// if !ok {
// buff.WriteString(fmt.Sprintf("[MethodID: Set] %v,", req[3]))
// }
// _ = lst[3].(stream.TokenType) //endName
// buff.WriteString(fmt.Sprintf("[MethodID: Set] len: %v, name: %v", length, string(name)))
// } else if midstr == uid.OpalAuthenticate {
// reqBytes := req[3].(stream.List)
// lockingAuthority := reqBytes[0].([]byte)
// _ = reqBytes[1].(stream.TokenType) //startName
// length := reqBytes[2].(uint)
// name := reqBytes[3].([]byte)
// _ = reqBytes[4].(stream.TokenType) //endName
// buff.WriteString(fmt.Sprintf("[MethodID: Authenticate] authority: %v, len: %d, name: %s", lockingAuthority, length, string(name)))
// } else if midstr == uid.OpalRevert {
// buff.WriteString("[MethodID: Revert]")
// } else if midstr == uid.MethodIDSMProperties {
// // var tp TPerProperties
// // parseTPerProperties(req[3].(stream.List), &tp)
// // buff.WriteString(fmt.Sprintf("[MethodID: Properties] tp: %v", tp))
// // reqBytes := req[3].(stream.List)
// // for i := 0; i < len(reqBytes); i++ {
// // tokenType := reqBytes[i].(stream.TokenType)
// // if tokenType == stream.StartName {
// // length := reqBytes[i+1].(uint)
// // name := reqBytes[i+2].(stream.List)
// // _ = reqBytes[i+3].(stream.TokenType)
// // buff.WriteString(fmt.Sprintf("[MethodID: Properties] tokenType: %v, length: %d, name: %s", stream.StartName, length, name))
// // i += 3
// // }
// } else {
// buff.WriteString(fmt.Sprintf("default to invoking-id: %s, method-id: %s. req: %+v", uidStr.String(), midstr.String(), req[3]))
// }
// return buff.String()
// }

// return fmt.Sprintf("unhandled method call - invoking-id: %s, method-id: %s. req: %+v", m.iid.String(), m.mid.String(), req)
// }

// Copy the current state of a method call into a new independent copy
func (m *MethodCall) Clone() *MethodCall {
mn := &MethodCall{bytes.Buffer{}, m.depth, m.flags}
mn := &MethodCall{
iid: m.iid,
mid: m.mid,
buf: bytes.Buffer{},
depth: m.depth,
flags: m.flags,
}
mn.buf.Write(m.buf.Bytes())
return mn
}
Expand Down

0 comments on commit c62b435

Please sign in to comment.