tagops

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Example (CompareOldVsNew)

Example showing the simplified API vs the old approach

package main

import (
	"fmt"
)

func main() {
	// OLD APPROACH - Manual everything
	_, _ = fmt.Println("=== OLD APPROACH ===")

	// Detect tag manually
	_, _ = fmt.Println("Manually detecting tag...")
	_, _ = fmt.Println("Examining UID bytes to determine tag type...")

	// Determine tag type manually
	_, _ = fmt.Println("Detected MIFARE Classic tag")

	// Manual MIFARE operations
	_, _ = fmt.Println("Creating MIFARE tag instance manually...")
	_, _ = fmt.Println("Setting up key provider with default keys...")
	_, _ = fmt.Println("Authenticating sector 1 with key A...")

	// Read blocks manually
	_, _ = fmt.Println("Reading blocks 4-7 individually...")
	_, _ = fmt.Printf("Read %d bytes after manual auth\n", 16)

	_, _ = fmt.Println()
	_, _ = fmt.Println("=== NEW APPROACH WITH TAGOPS ===")

	// NEW APPROACH - Everything is automatic
	_, _ = fmt.Println("Creating TagOperations instance...")

	// Detect any tag type
	_, _ = fmt.Println("Auto-detecting tag type...")
	_, _ = fmt.Printf("Detected %s tag\n", "MIFARE")

	// Read blocks - automatically uses fast read for NTAG
	// and handles auth for MIFARE
	_, _ = fmt.Println("Reading blocks with automatic optimization...")
	_, _ = fmt.Printf("Read %d bytes automatically with optimal method\n", 20)

	// Write NDEF - works for any tag type
	_, _ = fmt.Println("Writing NDEF message...")
	_, _ = fmt.Println("Written NDEF to any tag type transparently")

}
Output:
=== OLD APPROACH ===
Manually detecting tag...
Examining UID bytes to determine tag type...
Detected MIFARE Classic tag
Creating MIFARE tag instance manually...
Setting up key provider with default keys...
Authenticating sector 1 with key A...
Reading blocks 4-7 individually...
Read 16 bytes after manual auth

=== NEW APPROACH WITH TAGOPS ===
Creating TagOperations instance...
Auto-detecting tag type...
Detected MIFARE tag
Reading blocks with automatic optimization...
Read 20 bytes automatically with optimal method
Writing NDEF message...
Written NDEF to any tag type transparently
Example (ReadBlocks)
package main

import (
	"fmt"
)

func main() {
	// Initialize device (transport setup omitted for brevity)
	// In a real application, you would create a proper device with transport
	_, _ = fmt.Println("Example: Reading blocks from NFC tags")

	// Create TagOperations instance
	_, _ = fmt.Println("Creating TagOperations instance...")

	// Detect tag
	_, _ = fmt.Println("Detecting tag...")
	_, _ = fmt.Printf("Detected %s tag\n", "NTAG215")

	// Read blocks 4-8 (automatically uses fast read for NTAG)
	_, _ = fmt.Println("Reading blocks 4-8...")
	_, _ = fmt.Println("Using optimized fast read for NTAG...")
	_, _ = fmt.Printf("Read %d bytes: %s\n", 20, "030A0AFE0000111203616263")

}
Output:
Example: Reading blocks from NFC tags
Creating TagOperations instance...
Detecting tag...
Detected NTAG215 tag
Reading blocks 4-8...
Using optimized fast read for NTAG...
Read 20 bytes: 030A0AFE0000111203616263
Example (ReadNDEF)
package main

import (
	"fmt"
)

func main() {
	// Initialize device (transport setup omitted for brevity)
	// In a real application, you would create a proper device with transport
	_, _ = fmt.Println("Example: Reading NDEF data from NFC tags")

	// Create TagOperations instance
	_, _ = fmt.Println("Creating TagOperations instance...")

	// Detect tag
	_, _ = fmt.Println("Detecting tag...")
	_, _ = fmt.Printf("Detected %s tag with UID: %s\n", "NTAG215", "04:12:34:56:78:9A:BC")

	// Read NDEF - works transparently for both NTAG and MIFARE
	_, _ = fmt.Println("Reading NDEF message...")

	// Process NDEF records
	_, _ = fmt.Printf("Found NDEF record: %s\n", "T")
	_, _ = fmt.Printf("Found NDEF record: %s\n", "U")

	_, _ = fmt.Println("NDEF read complete")

}
Output:
Example: Reading NDEF data from NFC tags
Creating TagOperations instance...
Detecting tag...
Detected NTAG215 tag with UID: 04:12:34:56:78:9A:BC
Reading NDEF message...
Found NDEF record: T
Found NDEF record: U
NDEF read complete
Example (TryMIFAREKeys)
package main

import (
	"fmt"
)

func main() {
	// Initialize device (transport setup omitted for brevity)
	// In a real application, you would create a proper device with transport
	_, _ = fmt.Println("Example: Authenticating with MIFARE Classic tags")

	// Create TagOperations instance
	_, _ = fmt.Println("Creating TagOperations instance...")

	// Detect tag
	_, _ = fmt.Println("Detecting tag...")
	_, _ = fmt.Printf("Detected %s tag\n", "MIFARE Classic")

	// Try common keys automatically
	_, _ = fmt.Println("Trying common MIFARE keys...")
	_, _ = fmt.Println("Testing key: FF FF FF FF FF FF")
	_, _ = fmt.Println("Testing key: A0 A1 A2 A3 A4 A5")
	_, _ = fmt.Printf("Authenticated with key: %s\n", "MAD_KEY_A")

	// Now read/write operations will work transparently
	_, _ = fmt.Println("Reading blocks 4-7...")
	_, _ = fmt.Printf("Read %d bytes from MIFARE\n", 16)

}
Output:
Example: Authenticating with MIFARE Classic tags
Creating TagOperations instance...
Detecting tag...
Detected MIFARE Classic tag
Trying common MIFARE keys...
Testing key: FF FF FF FF FF FF
Testing key: A0 A1 A2 A3 A4 A5
Authenticated with key: MAD_KEY_A
Reading blocks 4-7...
Read 16 bytes from MIFARE
Example (WriteNDEF)
package main

import (
	"fmt"
)

func main() {
	// Initialize device (transport setup omitted for brevity)
	// In a real application, you would create a proper device with transport
	_, _ = fmt.Println("Example: Writing NDEF data to NFC tags")

	// Create TagOperations instance
	_, _ = fmt.Println("Creating TagOperations instance...")

	// Detect tag
	_, _ = fmt.Println("Detecting tag...")
	_, _ = fmt.Printf("Detected %s tag\n", "MIFARE Classic")

	// Create NDEF message
	_, _ = fmt.Println("Creating NDEF message with text and URI records...")
	_, _ = fmt.Println("Text record: Hello from go-pn532!")
	_, _ = fmt.Println("URI record: https://github.com/ZaparooProject/go-pn532")

	// Write NDEF - works transparently for both NTAG and MIFARE
	// For MIFARE, authentication is handled automatically
	_, _ = fmt.Println("Writing NDEF message...")
	_, _ = fmt.Println("Authenticating with MIFARE key...")
	_, _ = fmt.Println("NDEF message written successfully")

}
Output:
Example: Writing NDEF data to NFC tags
Creating TagOperations instance...
Detecting tag...
Detected MIFARE Classic tag
Creating NDEF message with text and URI records...
Text record: Hello from go-pn532!
URI record: https://github.com/ZaparooProject/go-pn532
Writing NDEF message...
Authenticating with MIFARE key...
NDEF message written successfully

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoTag indicates no tag was detected
	ErrNoTag = errors.New("no tag detected")
	// ErrUnsupportedTag indicates the tag type is not supported
	ErrUnsupportedTag = errors.New("unsupported tag type")
	// ErrAuthFailed indicates all authentication attempts failed
	ErrAuthFailed = errors.New("authentication failed with all known keys")
)

Functions

func CompareUID

func CompareUID(uid1, uid2 []byte) bool

CompareUID compares two UIDs for equality

func DetectTagTypeFromUID

func DetectTagTypeFromUID(uid []byte) pn532.TagType

DetectTagTypeFromUID attempts to determine tag type from UID characteristics This is a helper function that can be used before full tag initialization

func TagTypeDisplayName added in v0.9.0

func TagTypeDisplayName(t pn532.TagType) string

TagTypeDisplayName returns a human-readable display name for a tag type This provides more descriptive names than the raw pn532.TagType string values

Types

type TagInfo

type TagInfo struct {
	TypeName    string
	NTAGType    string
	MIFAREType  string
	Type        pn532.TagType
	UID         []byte
	TotalPages  int
	UserMemory  int
	Sectors     int
	TotalMemory int
}

TagInfo contains detailed information about a detected tag

type TagOperations

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

TagOperations provides unified high-level tag operations

func New

func New(device *pn532.Device) *TagOperations

New creates a new TagOperations instance

func (*TagOperations) DetectTag

func (t *TagOperations) DetectTag(ctx context.Context) error

DetectTag detects and initializes a tag for operations. This must be called before any read/write operations.

func (*TagOperations) EraseBlocks

func (t *TagOperations) EraseBlocks(ctx context.Context, startBlock, endBlock byte) error

EraseBlocks writes zeros to the specified block range

func (*TagOperations) Format

func (t *TagOperations) Format(ctx context.Context) error

Format prepares the tag for NDEF use

func (*TagOperations) GetCapacityInfo

func (t *TagOperations) GetCapacityInfo() (totalBytes, usableBytes int, err error)

GetCapacityInfo returns information about the tag's storage capacity

func (*TagOperations) GetTagInfo

func (t *TagOperations) GetTagInfo() (*TagInfo, error)

GetTagInfo returns detailed information about the currently detected tag

func (*TagOperations) GetTagType

func (t *TagOperations) GetTagType() pn532.TagType

GetTagType returns the detected tag type

func (*TagOperations) GetUID

func (t *TagOperations) GetUID() []byte

GetUID returns the tag's UID

func (*TagOperations) IsNDEFCapable

func (t *TagOperations) IsNDEFCapable(ctx context.Context) bool

IsNDEFCapable returns whether the tag supports NDEF

func (*TagOperations) ReadAll

func (t *TagOperations) ReadAll(ctx context.Context) ([]byte, error)

ReadAll reads all available data from the tag

func (*TagOperations) ReadBlocks

func (t *TagOperations) ReadBlocks(ctx context.Context, startBlock, endBlock byte) ([]byte, error)

ReadBlocks reads a range of blocks from the tag using the optimal method. For NTAG tags, it uses fast read when possible. For MIFARE tags, it handles authentication transparently. The startBlock and endBlock are inclusive.

func (*TagOperations) ReadNDEF

func (t *TagOperations) ReadNDEF(ctx context.Context) (*pn532.NDEFMessage, error)

ReadNDEF reads and parses NDEF data from the tag

func (*TagOperations) TagType

func (t *TagOperations) TagType() pn532.TagType

TagType returns the detected tag type (alias for GetTagType)

func (*TagOperations) WriteBlocks

func (t *TagOperations) WriteBlocks(ctx context.Context, startBlock byte, data []byte) error

WriteBlocks writes data to the tag starting at the specified block. For NTAG tags, it writes directly. For MIFARE tags, it handles authentication transparently. The data will be written in chunks appropriate to the tag type.

func (*TagOperations) WriteNDEF

func (t *TagOperations) WriteNDEF(ctx context.Context, msg *pn532.NDEFMessage) error

WriteNDEF writes an NDEF message to the tag

Jump to

Keyboard shortcuts

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