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 ¶
- Constants
- type Message
- func NewAbsoluteURIMessage(typeURI string, payload []byte) *Message
- func NewExternalMessage(extType string, payload []byte) *Message
- func NewMediaMessage(mimeType string, payload []byte) *Message
- func NewMessage(tnf byte, rtype string, id string, payload RecordPayload) *Message
- func NewMessageFromRecords(records ...*Record) *Message
- func NewSmartPosterMessage(msgPayload *Message) *Message
- func NewTextMessage(textVal, language string) *Message
- func NewURIMessage(uriVal string) *Message
- type Record
- func NewAbsoluteURIRecord(typeURI string, payload []byte) *Record
- func NewExternalRecord(extType string, payload []byte) *Record
- func NewMediaRecord(mimeType string, payload []byte) *Record
- func NewRecord(tnf byte, typ string, id string, payload RecordPayload) *Record
- func NewSmartPosterRecord(msg *Message) *Record
- func NewTextRecord(textVal, language string) *Record
- func NewURIRecord(uriVal string) *Record
- func (r *Record) Empty() bool
- func (r *Record) ID() string
- func (r *Record) Inspect() string
- func (r *Record) MB() bool
- func (r *Record) ME() bool
- func (r *Record) Marshal() ([]byte, error)
- func (r *Record) Payload() (RecordPayload, error)
- func (r *Record) SetMB(b bool)
- func (r *Record) SetME(b bool)
- func (r *Record) String() string
- func (r *Record) TNF() byte
- func (r *Record) Type() string
- func (r *Record) Unmarshal(buf []byte) (rLen int, err error)
- type RecordPayload
- type SmartPosterPayload
Examples ¶
Constants ¶
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 ¶
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 ¶
NewExternalMessage returns a new Message with a single Record of NFC Forum External type.
func NewMediaMessage ¶
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 ¶
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 ¶
NewSmartPosterMessage returns a new Message with a single Record of WellKnownType Sp (Smart Poster).
func NewTextMessage ¶
NewTextMessage returns a new Message with a single Record of WellKnownType T[ext].
func NewURIMessage ¶
NewURIMessage returns a new Message with a single Record of WellKnownType U[RI].
func (*Message) Inspect ¶
Inspect returns a string with information about the message and its records.
func (*Message) Marshal ¶
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) Unmarshal ¶
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 ¶
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 ¶
NewExternalRecord returns a new Record with a Payload of NFC Forum external type.
func NewMediaRecord ¶
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 ¶
NewSmartPosterRecord creates a new Record representing a Smart Poster. The Payload of a Smart Poster is an NDEF Message.
func NewTextRecord ¶
NewTextRecord returns a new Record with a Payload of Text [Well-Known] Type.
func NewURIRecord ¶
NewURIRecord returns a new Record with a Payload of URI [Well-Known] Type.
func (*Record) Inspect ¶
Inspect provides a string with information about this record. For a String representation of the contents use String().
func (*Record) MB ¶
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 ¶
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 ¶
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) 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) Unmarshal ¶
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.
Source Files ¶
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. |