acr122u

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: MIT Imports: 3 Imported by: 13

README

acr122u

Build status Go Report Card GoDoc License MIT

This is a Go package for the ACR122U USB NFC Reader

Requirements

Under macOS pcsc-lite can be installed using homebrew: brew install pcsc-lite

Installation

go get -u github.com/peterhellberg/acr122u

Usage

Minimal example
package main

import (
	"fmt"

	"github.com/peterhellberg/acr122u"
)

func main() {
	ctx, err := acr122u.EstablishContext()
	if err != nil {
		panic(err)
	}

	ctx.ServeFunc(func(c acr122u.Card) {
		fmt.Printf("%x\n", c.UID())
	})
}
Using a struct that implements the acr122u.Handler interface
package main

import (
	"log"
	"os"

	"github.com/peterhellberg/acr122u"
)

func main() {
	ctx, err := acr122u.EstablishContext()
	if err != nil {
		panic(err)
	}

	h := &handler{log.New(os.Stdout, "", 0)}

	ctx.Serve(h)
}

type handler struct {
	acr122u.Logger
}

func (h *handler) ServeCard(c acr122u.Card) {
	h.Printf("%x\n", c.UID())
}
NATS

NATS is my favorite messaging system, so let’s see how we can use it with this package:

Publish each UID to a NATS subject
package main

import (
	nats "github.com/nats-io/go-nats"
	acr122u "github.com/peterhellberg/acr122u"
)

func main() {
	nc, err := nats.Connect(nats.DefaultURL)
	if err != nil {
		panic(err)
	}

	ctx, err := acr122u.EstablishContext()
	if err != nil {
		panic(err)
	}

	ctx.ServeFunc(func(c acr122u.Card) {
		nc.Publish("acr122u", c.UID())
	})
}
Subscribe to the NATS subject
package main

import (
	"fmt"
	"runtime"

	nats "github.com/nats-io/go-nats"
)

func main() {
	nc, err := nats.Connect(nats.DefaultURL)
	if err != nil {
		panic(err)
	}

	nc.Subscribe("acr122u", func(m *nats.Msg) {
		fmt.Printf("Received UID: %x\n", m.Data)
	})

	runtime.Goexit()
}

License (MIT)

Copyright (c) 2018-2023 Peter Hellberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package acr122u is a library for the ACR122U USB NFC Reader

Requirements

ACR122U USB NFC Reader https://www.acs.com.hk/en/products/3/acr122u-usb-nfc-reader

Middleware to access a smart card using SCard API (PC/SC) https://pcsclite.apdu.fr

Under macOS pcsc-lite can be installed using homebrew: brew install pcsc-lite

The Go bindings to the PC/SC API https://github.com/ebfe/scard

Installation

You can install the acr122u package using go get

go get -u github.com/peterhellberg/acr122u

Usage

A minimal usage example

package main

import (
	"fmt"

	"github.com/peterhellberg/acr122u"
)

func main() {
	ctx, err := acr122u.EstablishContext()
	if err != nil {
		panic(err)
	}

	ctx.ServeFunc(func(c acr122u.Card) {
		fmt.Printf("%x\n", c.UID())
	})
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrOperationFailed is returned when the response code is 0x63 0x00
	ErrOperationFailed = Error{Message: "operation failed"}
)

Functions

This section is empty.

Types

type Card

type Card interface {
	// Reader returns the name of the reader used
	Reader() string

	// Status returns the card status
	Status() (Status, error)

	// UID returns the UID for the card
	UID() []byte
}

Card represents a ACR122U card

type Context

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

Context for ACR122U readers

func EstablishContext

func EstablishContext(options ...Option) (*Context, error)

EstablishContext creates a ACR122U context

func (*Context) Readers

func (ctx *Context) Readers() []string

Readers returns a list of readers

func (*Context) Release

func (ctx *Context) Release() error

Release should be called when the context is not needed anymore

func (*Context) Serve

func (ctx *Context) Serve(h Handler) error

Serve cards being swiped using the provided Handler

func (*Context) ServeFunc

func (ctx *Context) ServeFunc(hf HandlerFunc) error

ServeFunc uses the provided HandlerFunc as a Handler

type Error

type Error struct {
	Message string
}

Error is tye error type returned by the acr122u package

func (Error) Error

func (e Error) Error() string

Error returns the error message

type Handler

type Handler interface {
	ServeCard(Card)
}

Handler is the interface that handles each card when present in the field.

type HandlerFunc

type HandlerFunc func(Card)

HandlerFunc is the function signature for handling a card

func (HandlerFunc) ServeCard

func (hf HandlerFunc) ServeCard(c Card)

ServeCard makes HandlerFunc implement the Handler interface

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger interface that is implemented by *log.Logger

type Option

type Option func(*Context)

Option is the function type used to configure the context

func WithProtocol

func WithProtocol(p Protocol) Option

WithProtocol accepts Undefined (0x0), T0 (0x1), T1 (0x2) or Any (T0|T1)

func WithShareMode

func WithShareMode(sm ShareMode) Option

WithShareMode accepts Exclusive (0x1) or Shared mode (0x2)

type Protocol

type Protocol uint32

Protocol is the protocol type

var (
	ProtocolUndefined Protocol = 0x0
	ProtocolT0        Protocol = 0x1
	ProtocolT1        Protocol = 0x2
	ProtocolAny                = ProtocolT0 | ProtocolT1
)

Protocols

type ShareMode

type ShareMode uint32

ShareMode is the share mode type

var (
	ShareExclusive ShareMode = 0x1
	ShareShared    ShareMode = 0x2
)

Share modes

type Status

type Status struct {
	Reader         string
	State          uint32
	ActiveProtocol uint32
	Atr            []byte
}

Status contains the status of a card

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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