blob: 45b2a531a2aa5ecd92287d6c81f3414a26169e44 [file] [log] [blame]
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fidl
const FidlWireFormatMagicNumberInitial = 0x01
const FidlV2WireFormatFlagMask = 0x02
// MarshalerContext stores flags to control marshaling/unmarshaling.
// This is currently empty. We keep it around to ease the implementation of
// context-dependent behavior for future migrations.
type MarshalerContext struct {
UseV2WireFormat bool
func (ctx MarshalerContext) isV2WireFormatDecodingEnabled() bool {
return ctx.UseV2WireFormat
// NewCtx returns the default context.
// During migrations, this controls the default write path.
func NewCtx() MarshalerContext {
return MarshalerContext{UseV2WireFormat: true}
// MessageHeader represents a transactional message header.
type MessageHeader struct {
_ struct{} `fidl:"s" fidl_size_v2:"16" fidl_alignment_v2:"8"`
Txid uint32 `fidl:"0" fidl_offset_v2:"0" fidl_bounds:""`
Flags [3]uint8 `fidl:"4" fidl_offset_v2:"4" fidl_bounds:""`
Magic uint8 `fidl:"7" fidl_offset_v2:"7" fidl_bounds:""`
Ordinal uint64 `fidl:"8" fidl_offset_v2:"8" fidl_bounds:""`
func (msg *MessageHeader) IsSupportedVersion() bool {
return msg.Magic == FidlWireFormatMagicNumberInitial
// NewCtx creates a new MarshalerContext for unmarshaling based on msg.Flags.
// During migrations, this controls dynamic behavior in the read path.
func (msg *MessageHeader) NewCtx() MarshalerContext {
return MarshalerContext{UseV2WireFormat: true}
var mMessageHeader = MustCreateMarshaler(MessageHeader{})
var MessageHeaderSize = 16
func (msg *MessageHeader) Marshaler() Marshaler {
return mMessageHeader
// NewHeader create a new MessageHeader for marshaling with the correct flags
// set based on the MarshalerContext. It is the caller's responsibilty to set
// the transaction ID and method ordinal
func (ctx MarshalerContext) NewHeader() MessageHeader {
var flagByte0 uint8
if ctx.UseV2WireFormat {
flagByte0 = FidlV2WireFormatFlagMask
return MessageHeader{
Flags: [3]uint8{flagByte0, 0, 0},
Magic: FidlWireFormatMagicNumberInitial,