Documentation
¶
Index ¶
- Constants
- Variables
- func BuildDataFrame(tfi byte, data []byte) []byte
- func CalculateChecksum(data []byte) byte
- func CalculateDataChecksum(tfi byte, data []byte) byte
- func CalculateLengthChecksum(length byte) byte
- func ExtractFrameData(buf []byte, off, frameLen int, tfiExpected byte) (data []byte, retry bool, err error)
- func ExtractFrameDataPooled(buf []byte, off, frameLen int, tfiExpected byte) (data []byte, retry bool, err error)
- func FindFrameStart(buf []byte, totalLen int, startMarker byte) (offset int, shouldRetry bool)
- func GetBuffer(size int) []byte
- func GetFrameBuffer() []byte
- func GetSmallBuffer(size int) []byte
- func HandleErrorFrame(buf []byte, off int) (data []byte, retry bool, err error)
- func PutBuffer(buf []byte)
- func ValidateChecksum(data []byte) bool
- func ValidateFrameChecksum(buf []byte, start, end int) bool
- func ValidateFrameLength(buf []byte, off, totalLen int, operation, port string) (frameLen int, shouldRetry bool, err error)
- type BufferPool
Constants ¶
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
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
const ( MaxFrameDataLength = 263 // Maximum data length in frame (PN532 spec) MinFrameLength = 6 // Minimum frame length (preamble + startcode + len + lcs + tfi + dcs) )
Frame size limits
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 ¶
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 ¶
BuildDataFrame constructs a PN532 data frame with proper framing Returns the complete frame ready for transmission
func CalculateChecksum ¶
CalculateChecksum computes the checksum for a data buffer This is a simple sum of all bytes in the provided data
func CalculateDataChecksum ¶
CalculateDataChecksum computes the checksum for frame data (TFI + data bytes) and returns the two's complement (for inclusion in frame)
func CalculateLengthChecksum ¶
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 ¶
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 GetFrameBuffer ¶
func GetFrameBuffer() []byte
GetFrameBuffer gets a frame-sized buffer from the default pool
func GetSmallBuffer ¶
GetSmallBuffer gets a small buffer from the default pool
func HandleErrorFrame ¶
HandleErrorFrame processes a PN532 error frame (TFI = 0x7F) Returns the error frame data and any processing error
func ValidateChecksum ¶
ValidateChecksum verifies that the provided data has a valid checksum Returns true if checksum is invalid (requiring NACK), false if valid
func ValidateFrameChecksum ¶
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