ndef

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: LGPL-3.0 Imports: 12 Imported by: 6

README

Go-ndef

Build Status codecov GoDoc

A Go implementation of the NFC Data Exchange Format (NDEF) specification, Version 1.0.

go-ndef allows to easily work with NDEF Messages and Records in Go, providing an easy way to parse bytes and generate bytes. The types submodules add support for some NFC Forum Record Types, such as Text or URI.

Documentation

go-ndef uses godoc for documentation and examples. You can read it at https://godoc.org/github.com/hsanjuan/go-ndef .

Documentation

Overview

Package ndef provides an implementation of the NFC Data Exchange Format (NDEF) specification: - NFCForum-TS-NDEF_1.0 - NFCForum-TS-RTD_1.0 It allows to parse byte slices into a structured Message type, as well as to turn an Message into a bytes again.

Index

Examples

Constants

View Source
const (
	Empty = byte(iota)
	NFCForumWellKnownType
	MediaType
	AbsoluteURI
	NFCForumExternalType
	Unknown
	Unchanged
	Reserved
)

Possible values for the TNF Field as defined in the specification.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Records []*Record
}

Message represents an NDEF Message, which is a collection of one or more NDEF Records.

Most common types of NDEF Messages (URI, Media) only have a single record. However, others, like Smart Posters, have multiple ones.

Example
// Here we create an NDEF Message of type "U" (URI).
payload := uri.New("https://github.com/hsanjuan/go-ndef")
ndefMessage := NewMessage(NFCForumWellKnownType, "U", "", payload)
fmt.Println(ndefMessage)
Output:

urn:nfc:wkt:U:https://github.com/hsanjuan/go-ndef

func NewAbsoluteURIMessage

func NewAbsoluteURIMessage(typeURI string, payload []byte) *Message

NewAbsoluteURIMessage returns a new Message with a single Record of AbsoluteURI type.

AbsoluteURI means that the type of the payload for this record is defined by an URI resource. It is not supposed to be used to describe an URI. For that, use NewURIRecord().

func NewExternalMessage

func NewExternalMessage(extType string, payload []byte) *Message

NewExternalMessage returns a new Message with a single Record of NFC Forum External type.

func NewMediaMessage

func NewMediaMessage(mimeType string, payload []byte) *Message

NewMediaMessage returns a new Message with a single Record of Media (RFC-2046) type.

mimeType is something like "text/json" or "image/jpeg".

func NewMessage

func NewMessage(tnf byte, rtype string, id string, payload RecordPayload) *Message

NewMessage returns a new Message initialized with a single Record with the TNF, Type, ID and Payload values.

func NewMessageFromRecords

func NewMessageFromRecords(records ...*Record) *Message

NewMessageFromRecords returns a new Message containing several NDEF Records. The MB and ME flags for the records are adjusted to create a valid message.

func NewSmartPosterMessage

func NewSmartPosterMessage(msgPayload *Message) *Message

NewSmartPosterMessage returns a new Message with a single Record of WellKnownType Sp (Smart Poster).

func NewTextMessage

func NewTextMessage(textVal, language string) *Message

NewTextMessage returns a new Message with a single Record of WellKnownType T[ext].

func NewURIMessage

func NewURIMessage(uriVal string) *Message

NewURIMessage returns a new Message with a single Record of WellKnownType U[RI].

func (*Message) Inspect

func (m *Message) Inspect() string

Inspect returns a string with information about the message and its records.

func (*Message) Marshal

func (m *Message) Marshal() ([]byte, error)

Marshal provides the byte slice representation of a Message, which is the concatenation of the Marshaling of each of its records.

Returns an error if something goes wrong.

func (*Message) String

func (m *Message) String() string

Returns the string representation of each of the records in the message.

func (*Message) Unmarshal

func (m *Message) Unmarshal(buf []byte) (rLen int, err error)

Unmarshal parses a byte slice into a Message. This is done by parsing all Records in the slice, until there are no more to parse.

Returns the number of bytes processed (message length), or an error if something looks wrong with the message or its records.

Example
ndefMessageBytes := []byte{0xd1, 0x01, 0x23, 0x54, 0x02, 0x65, 0x6e,
	0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
	0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x6f, 0x66,
	0x20, 0x54, 0x5b, 0x65, 0x78, 0x74, 0x5d, 0x20, 0x74, 0x79,
	0x70, 0x65}
ndefMessage := &Message{}                         // Create uninitialized message
_, err := ndefMessage.Unmarshal(ndefMessageBytes) // Parse bytes into it
if err != nil {                                   // Your bytes don't look good
	fmt.Println(err)
	return
}
fmt.Println(ndefMessage) // Print the contents of every record
Output:

urn:nfc:wkt:T:This is a message of T[ext] type

type Record

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

A Record is an NDEF Record. Multiple records can be part of a single NDEF Message.

func NewAbsoluteURIRecord

func NewAbsoluteURIRecord(typeURI string, payload []byte) *Record

NewAbsoluteURIRecord returns a new Record with a Payload of Absolute URI type.

AbsoluteURI means that the type of the payload for this record is defined by an URI resource. It is not supposed to be used to describe an URI. For that, use NewURIRecord().

func NewExternalRecord

func NewExternalRecord(extType string, payload []byte) *Record

NewExternalRecord returns a new Record with a Payload of NFC Forum external type.

func NewMediaRecord

func NewMediaRecord(mimeType string, payload []byte) *Record

NewMediaRecord returns a new Record with a Media type (per RFC-2046) as payload.

mimeType is something like "text/json" or "image/jpeg".

func NewRecord

func NewRecord(tnf byte, typ string, id string, payload RecordPayload) *Record

NewRecord returns a single-chunked record with the given options. Use a generic.Payload if you want to use a custom byte-slice for payload.

func NewSmartPosterRecord

func NewSmartPosterRecord(msg *Message) *Record

NewSmartPosterRecord creates a new Record representing a Smart Poster. The Payload of a Smart Poster is an NDEF Message.

func NewTextRecord

func NewTextRecord(textVal, language string) *Record

NewTextRecord returns a new Record with a Payload of Text [Well-Known] Type.

func NewURIRecord

func NewURIRecord(uriVal string) *Record

NewURIRecord returns a new Record with a Payload of URI [Well-Known] Type.

func (*Record) Empty

func (r *Record) Empty() bool

Empty returns true if this record has no chunks.

func (*Record) ID

func (r *Record) ID() string

ID returns the declared record ID for this record.

func (*Record) Inspect

func (r *Record) Inspect() string

Inspect provides a string with information about this record. For a String representation of the contents use String().

func (*Record) MB

func (r *Record) MB() bool

MB returns the value of the MessageBegin bit of the first chunk of this record, signaling that this is the first record in an NDEF Message. a NDEF Message.

func (*Record) ME

func (r *Record) ME() bool

ME returns the value of the MessageEnd bit of the last chunk of this Record, signaling that this is the last record in an NDEF Message.

func (*Record) Marshal

func (r *Record) Marshal() ([]byte, error)

Marshal returns the byte representation of a Record. It does this by producing a single record chunk.

Note that if the original Record was unmarshaled from many chunks, the recovery is not possible anymore.

func (*Record) Payload

func (r *Record) Payload() (RecordPayload, error)

Payload returns the RecordPayload for this record. It will use one of the supported types, or otherwise a generic.Payload.

func (*Record) SetMB

func (r *Record) SetMB(b bool)

SetMB sets the MessageBegin bit of the first chunk of this Record.

func (*Record) SetME

func (r *Record) SetME(b bool)

SetME sets the MessageEnd bit of the last chunk of this record.

func (*Record) String

func (r *Record) String() string

String a string representation of the payload of the record, prefixed by the URN of the resource.

Note that not all NDEF Payloads are supported, and that custom types/payloads are considered not printable. In those cases, a generic RecordPayload is used and an explanatory message is returned instead. See submodules under "types/" for a list of supported types.

func (*Record) TNF

func (r *Record) TNF() byte

TNF returns the Type Name Format (3 bits) associated to this Record.

func (*Record) Type

func (r *Record) Type() string

Type returns the declared Type for this record.

func (*Record) Unmarshal

func (r *Record) Unmarshal(buf []byte) (rLen int, err error)

Unmarshal parses a byte slice into a Record struct (the slice can have extra bytes which are ignored). The Record is always reset before parsing.

It does this by parsing every record chunk until a chunk with the CF flag cleared is read is read.

Returns how many bytes were parsed from the slice (record length) or an error if something went wrong.

type RecordPayload

type RecordPayload interface {
	// Returns a string representation of the Payload
	String() string
	// Provides serialization for the Payload
	Marshal() []byte
	// Provides de-serialization for the Payload
	Unmarshal(buf []byte)
	// Returns a string indetifying the type of this payload
	Type() string
	// Returns the length of the Payload (serialized)
	Len() int
}

The RecordPayload interface should be implemented by supported NDEF Record types. It ensures that we have a way to interpret payloads into printable information and to produce NDEF Record payloads for a given type.

type SmartPosterPayload

type SmartPosterPayload struct {
	Message *Message
}

SmartPosterPayload represents the Payload of a Smart Poster, which is an NDEF Message with one or multiple records.

func NewSmartPosterPayload

func NewSmartPosterPayload(msg *Message) *SmartPosterPayload

NewSmartPosterPayload returns a new Smart Poster payload.

func (*SmartPosterPayload) Len

func (sp *SmartPosterPayload) Len() int

Len returns the length of this payload in bytes.

func (*SmartPosterPayload) Marshal

func (sp *SmartPosterPayload) Marshal() []byte

Marshal returns the bytes representing the payload of a Smart Poster. The payload is the contained NDEF Message.

func (*SmartPosterPayload) String

func (sp *SmartPosterPayload) String() string

String returns the contents of the message contained in the Smart Poster.

func (*SmartPosterPayload) Type

func (sp *SmartPosterPayload) Type() string

Type returns the URN for the Smart Poster type

func (*SmartPosterPayload) Unmarshal

func (sp *SmartPosterPayload) Unmarshal(buf []byte)

Unmarshal parses the SmartPosterPayload from a Smart Poster.

Directories

Path Synopsis
types
absoluteuri
Package absoluteuri provides an implementation for NDEF Payloads of Absolute URI type.
Package absoluteuri provides an implementation for NDEF Payloads of Absolute URI type.
ext
Package ext provides an implementation for NDEF Payloads of NFC Forum External Type.
Package ext provides an implementation for NDEF Payloads of NFC Forum External Type.
generic
Package generic provides a generic implementation for NDEF Payloads which are either custom or not supported yet.
Package generic provides a generic implementation for NDEF Payloads which are either custom or not supported yet.
media
Package media provides an implementation for NDEF Payloads for media types.
Package media provides an implementation for NDEF Payloads for media types.
wkt/text
Package text provides support for NDEF Payloads of Text type.
Package text provides support for NDEF Payloads of Text type.
wkt/uri
Package uri provides support for NDEF Payloads of URI type.
Package uri provides support for NDEF Payloads of URI type.

Jump to

Keyboard shortcuts

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