frame

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: LGPL-3.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 BuildDataFrame

func BuildDataFrame(tfi byte, data []byte) []byte

BuildDataFrame constructs a PN532 data frame with proper framing Returns the complete frame ready for transmission

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 CalculateDataChecksum

func CalculateDataChecksum(tfi byte, data []byte) byte

CalculateDataChecksum computes the checksum for frame data (TFI + data bytes) and returns the two's complement (for inclusion in frame)

func CalculateLengthChecksum

func CalculateLengthChecksum(length byte) byte

CalculateLengthChecksum computes the length checksum (LCS) The LCS is the two's complement of the length

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 ExtractFrameDataPooled

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

ExtractFrameDataPooled is a pooled version of ExtractFrameData that returns the buffer when done Caller must call PutBuffer on the returned data when finished This version provides maximum performance by avoiding the final copy

func FindFrameStart

func FindFrameStart(buf []byte, totalLen int, startMarker byte) (offset int, shouldRetry bool)

FindFrameStart locates the start of a PN532 frame in the buffer Returns the offset where the frame starts, or -1 if not found shouldRetry indicates if more data should be read

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 ValidateChecksum

func ValidateChecksum(data []byte) bool

ValidateChecksum verifies that the provided data has a valid checksum Returns true if checksum is invalid (requiring NACK), false if valid

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