mdb

package
v0.230528.1 Latest Latest
Warning

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

Go to latest
Published: May 28, 2023 License: CC0-1.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDelayAfterReset  = 500 * time.Millisecond
	DefaultDelayBeforeReset = 0
	DefaultDelayIdle        = 700 * time.Millisecond
	DefaultDelayNext        = 200 * time.Millisecond
	DefaultDelayOffline     = 10 * time.Second
	DefaultIdleThreshold    = 30 * time.Second
)
View Source
const (
	DefaultBusResetKeep  = 200 * time.Millisecond
	DefaultBusResetSleep = 500 * time.Millisecond
)
View Source
const ErrCodeNone int32 = -1
View Source
const MockContextKey = "test/mdb-mock"
View Source
const MockTimeout = 5 * time.Second
View Source
const (
	PacketMaxLength = 40
)

Variables

View Source
var (
	ErrStateInvalid = fmt.Errorf("CRITICAL code error state=invalid")
	ErrStateError   = fmt.Errorf("state=error")
)
View Source
var (
	ErrNak        = fmt.Errorf("MDB NAK")
	ErrBusy       = fmt.Errorf("MDB busy")
	ErrTimeoutMDB = fmt.Errorf("MDB timeout")
)
View Source
var (
	ErrPacketOverflow = errors.New("mdb: operation larger than max packet size")
	ErrPacketReadonly = errors.New("mdb: packet is readonly")

	PacketEmpty = &Packet{readonly: true}
	PacketAck   = MustPacketFromHex("00", true)
	PacketNak   = MustPacketFromHex("ff", true)
	PacketRet   = MustPacketFromHex("aa", true)
)

Functions

func IsResponseTimeout

func IsResponseTimeout(e error) bool

func NewMockBus

func NewMockBus(t testing.TB) (*Bus, *MockUart)

Types

type Bus

type Bus struct {
	Error func(error)
	Log   *log2.Log
	// contains filtered or unexported fields
}

func NewBus

func NewBus(u Uarter, log *log2.Log, errfun func(error)) *Bus

func (*Bus) Reset

func (b *Bus) Reset(keep, sleep time.Duration) error

func (*Bus) ResetDefault

func (b *Bus) ResetDefault() error

func (*Bus) Tx

func (b *Bus) Tx(request Packet, response *Packet) error

type Device

type Device struct {
	LastOk  *atomic_clock.Clock // last successful tx(), 0 at init, monotonic
	LastOff *atomic_clock.Clock // last change from online to offline (MDB timeout), 0=online

	Log     *log2.Log
	Address uint8

	ByteOrder   binary.ByteOrder
	PacketReset Packet
	PacketSetup Packet
	PacketPoll  Packet
	DoReset     engine.Doer
	DoInit      engine.Doer // likely Seq starting with DoReset

	DelayAfterReset  time.Duration
	DelayBeforeReset time.Duration
	DelayIdle        time.Duration
	DelayNext        time.Duration
	DelayOffline     time.Duration
	IdleThreshold    time.Duration

	SetupResponse Packet
	// contains filtered or unexported fields
}

func (*Device) ErrorCode

func (dev *Device) ErrorCode() int32

func (*Device) Init

func (dev *Device) Init(bus *Bus, addr uint8, name string, byteOrder binary.ByteOrder)

func (*Device) Keepalive

func (dev *Device) Keepalive(interval time.Duration, stopch <-chan struct{})

Keep particular devices "hot" to reduce useless POLL time.

func (*Device) Locked_TxKnown

func (dev *Device) Locked_TxKnown(request Packet, response *Packet) error

Please make sure it is called under cmdLk or don't use it.

func (*Device) Name

func (dev *Device) Name() string

func (*Device) NewFunLoop

func (dev *Device) NewFunLoop(tag string, fun PollFunc, timeout time.Duration) engine.Doer

Call `fun` until `timeout` or it returns stop=true or error.

func (*Device) NewPollLoop

func (dev *Device) NewPollLoop(tag string, request Packet, timeout time.Duration, fun PollRequestFunc) engine.Doer

Send `request` packets until `timeout` or `fun` returns stop=true or error.

func (*Device) Ready

func (dev *Device) Ready() bool

func (*Device) Reset

func (dev *Device) Reset() error

func (*Device) Rst added in v0.230424.2

func (dev *Device) Rst() (err error)

func (*Device) SetError

func (dev *Device) SetError(e error)

func (*Device) SetOnline

func (dev *Device) SetOnline()

func (*Device) SetReady

func (dev *Device) SetReady()

func (*Device) SetState

func (dev *Device) SetState(new DeviceState)

func (*Device) State

func (dev *Device) State() DeviceState

func (*Device) TeleError

func (dev *Device) TeleError(e error)

func (*Device) Tx added in v0.230424.2

func (dev *Device) Tx(request Packet, response *Packet) error

func (*Device) TxCustom

func (dev *Device) TxCustom(request Packet, response *Packet, opt TxOpt) error

func (*Device) TxKnown

func (dev *Device) TxKnown(request Packet, response *Packet) error

Command is known to be supported, MDB timeout means remote is offline. RESET if appropriate.

func (*Device) TxMaybe

func (dev *Device) TxMaybe(request Packet, response *Packet) error

Remote may ignore command with MDB timeout. state=Offline -> RESET state.Ok() required

func (*Device) TxReadSetup added in v0.230424.2

func (dev *Device) TxReadSetup() error

func (*Device) TxSetup

func (dev *Device) TxSetup() error

func (*Device) ValidateErrorCode

func (dev *Device) ValidateErrorCode() error

func (*Device) ValidateOnline

func (dev *Device) ValidateOnline() error

func (*Device) XXX_FIXME_SetAllDelays

func (dev *Device) XXX_FIXME_SetAllDelays(d time.Duration)

Used by tests to avoid waiting.

type DeviceState

type DeviceState uint32
const (
	DeviceInvalid DeviceState = iota // new, not usable
	DeviceInited                     // after Init(), usable for Probe
	DeviceOnline                     // Probe success, calibration may be required
	DeviceReady                      // "ready for useful work", with RESET, configure, calibration done
	DeviceError                      // responds but doesn't work well
	DeviceOffline                    // does not respond
)

func (DeviceState) Ok

func (s DeviceState) Ok() bool

func (DeviceState) Online

func (s DeviceState) Online() bool

func (DeviceState) String

func (i DeviceState) String() string

type FeatureNotSupported

type FeatureNotSupported string

func (FeatureNotSupported) Error

func (fns FeatureNotSupported) Error() string

type InvalidChecksum

type InvalidChecksum struct {
	Received byte
	Actual   byte
}

func (InvalidChecksum) Error

func (ic InvalidChecksum) Error() string

type MockR

type MockR [2]string

func (MockR) String

func (m MockR) String() string

type MockUart

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

func MockFromContext

func MockFromContext(ctx context.Context) *MockUart

sorry for this ugly convolution working around import cycle on a time budget

func NewMockUart

func NewMockUart(t testing.TB) *MockUart

func (*MockUart) Break

func (mu *MockUart) Break(d, sleep time.Duration) error

func (*MockUart) Close

func (mu *MockUart) Close() error

func (*MockUart) Expect

func (mu *MockUart) Expect(rrs []MockR)

usage: m, mock:= NewTestMdber(t) defer mock.Close() go use_mdb(m) mock.Expect(...) go use_mdb(m) mock.Expect(...) wait use_mdb() to finish to catch all possible errors

func (*MockUart) ExpectMap

func (mu *MockUart) ExpectMap(rrs map[string]string)

func (*MockUart) Open

func (mu *MockUart) Open(path string) error

func (*MockUart) Tx

func (mu *MockUart) Tx(request, response []byte) (n int, err error)

type Packet

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

func MustPacketFromBytes

func MustPacketFromBytes(b []byte, readonly bool) Packet

func MustPacketFromHex

func MustPacketFromHex(s string, readonly bool) Packet

func PacketFromBytes

func PacketFromBytes(b []byte, readonly bool) (Packet, error)

func PacketFromHex

func PacketFromHex(s string, readonly bool) (Packet, error)

func (*Packet) Bytes

func (p *Packet) Bytes() []byte

func (*Packet) Equal

func (p *Packet) Equal(p2 *Packet) bool

func (*Packet) Format

func (p *Packet) Format() string

func (*Packet) Len

func (p *Packet) Len() int

func (*Packet) String added in v0.230424.2

func (p *Packet) String() string

func (*Packet) TestHex

func (p *Packet) TestHex(t testing.TB, expect string)

Without checksum

func (*Packet) Wire

func (p *Packet) Wire(ffDance bool) []byte

func (*Packet) Write

func (sp *Packet) Write(p []byte) (n int, err error)

type PollDelay

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

"Idle mode" polling, runs forever until receive on `stopch`. Switches between fast/idle delays. Used by bill/coin devices.

func (*PollDelay) Delay

func (pd *PollDelay) Delay(dev *Device, active bool, err bool, stopch <-chan struct{}) bool

type PollFunc

type PollFunc func() (stop bool, err error)

type PollRequestFunc

type PollRequestFunc func(Packet) (stop bool, err error)

type TxOpt

type TxOpt struct {
	TimeoutOffline bool
	RequireOK      bool
	NoReset        bool
	ResetError     bool
	ResetOffline   bool
}

type Uarter

type Uarter interface {
	Break(d, sleep time.Duration) error
	Close() error
	Open(options string) error
	Tx(request, response []byte) (int, error)
}

Directories

Path Synopsis
Package bill incapsulates work with bill validators.
Package bill incapsulates work with bill validators.
Separate package to for hardware/mdb related config structure.
Separate package to for hardware/mdb related config structure.
Package evend incapsulates common parts of MDB protocol for eVend machine devices like conveyor, hopper, cup dispenser, elevator, etc.
Package evend incapsulates common parts of MDB protocol for eVend machine devices like conveyor, hopper, cup dispenser, elevator, etc.
config
Separate package to for hardware/evend related config structure.
Separate package to for hardware/evend related config structure.

Jump to

Keyboard shortcuts

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