frame

package
v0.22.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HostToPn532 = 0xD4 // Commands from host to PN532
	Pn532ToHost = 0xD5 // Responses from PN532 to host
)

Frame direction constants - these indicate the direction of data flow

View Source
const (
	Preamble   = 0x00 // Frame preamble byte
	StartCode1 = 0x00 // Start code byte 1
	StartCode2 = 0xFF // Start code byte 2
	Postamble  = 0x00 // Frame postamble byte
)

Frame markers and control bytes

View Source
const (
	MaxFrameDataLength = 263 // Maximum data length in frame (PN532 spec)
	MinFrameLength     = 6   // Minimum frame length (preamble + startcode + len + lcs + tfi + dcs)
)

Frame size limits

View Source
const (
	SmallBufferSize  = 16  // ACK processing, small responses
	MediumBufferSize = 255 // Standard PN532 frame data
	LargeBufferSize  = 512 // Extended frames with overhead
	FrameBufferSize  = 270 // Complete frame with all overhead (262 + 8)
)

Size thresholds for buffer categories

Variables

View Source
var (
	AckFrame  = []byte{0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00}
	NackFrame = []byte{0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00}
)

ACK and NACK frames - these are used for flow control

Functions

func CalculateChecksum

func CalculateChecksum(data []byte) byte

CalculateChecksum computes the checksum for a data buffer This is a simple sum of all bytes in the provided data

func ExtractFrameData

func ExtractFrameData(buf []byte, off, frameLen int, tfiExpected byte) (data []byte, retry bool, err error)

ExtractFrameData extracts the data payload from a validated PN532 frame Returns the frame data, whether a retry is needed (NACK should be sent), and any error This consolidates the extractFrameData logic from UART and I2C transports

func GetBuffer

func GetBuffer(size int) []byte

GetBuffer acquires a buffer from the default pool

func GetFrameBuffer

func GetFrameBuffer() []byte

GetFrameBuffer gets a frame-sized buffer from the default pool

func GetSmallBuffer

func GetSmallBuffer(size int) []byte

GetSmallBuffer gets a small buffer from the default pool

func HandleErrorFrame

func HandleErrorFrame(buf []byte, off int) (data []byte, retry bool, err error)

HandleErrorFrame processes a PN532 error frame (TFI = 0x7F) Returns the error frame data and any processing error

func PutBuffer

func PutBuffer(buf []byte)

PutBuffer returns a buffer to the default pool

func ValidateFrameChecksum

func ValidateFrameChecksum(buf []byte, start, end int) bool

ValidateFrameChecksum validates the frame data checksum Returns true if checksum is invalid (requiring NACK), false if valid This consolidates the validateFrameChecksum logic from UART and I2C transports

func ValidateFrameLength

func ValidateFrameLength(
	buf []byte, off, totalLen int, operation, port string,
) (frameLen int, shouldRetry bool, err error)

ValidateFrameLength validates the frame length field and length checksum Returns the validated frame length and whether a retry is needed (NACK should be sent) This consolidates the validateFrameLength logic from UART and I2C transports

Types

type BufferPool

type BufferPool struct {
	// contains filtered or unexported fields
}

BufferPool manages reusable byte slices for different size categories This reduces allocations in the hot paths of frame processing

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool creates a new buffer pool with optimized allocations

func (*BufferPool) GetBuffer

func (p *BufferPool) GetBuffer(size int) []byte

GetBuffer acquires a buffer of appropriate size for the requested capacity Returns a buffer that is at least 'size' bytes long The returned buffer should be returned via PutBuffer when done

func (*BufferPool) GetFrameBuffer

func (p *BufferPool) GetFrameBuffer() []byte

GetFrameBuffer is a convenience function for getting frame-sized buffers This is the most common case in UART transport operations

func (*BufferPool) GetSmallBuffer

func (p *BufferPool) GetSmallBuffer(size int) []byte

GetSmallBuffer is a convenience function for getting small buffers Used for ACK processing and small command responses

func (*BufferPool) PutBuffer

func (p *BufferPool) PutBuffer(buf []byte)

PutBuffer returns a buffer to the pool for reuse The buffer must not be used after calling this function The buffer will be reset (length set to capacity) and cleared of sensitive data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL