1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 21:00:05 +01:00
embd/descriptor.go

84 lines
2.0 KiB
Go
Raw Normal View History

2014-03-23 07:21:22 +05:30
// Host descriptor data structures.
2014-03-03 00:51:23 +05:30
package embd
import (
"errors"
"fmt"
2014-04-11 13:59:58 +05:30
"github.com/golang/glog"
)
2014-03-23 07:21:22 +05:30
// Descriptor represents a host descriptor.
type Descriptor struct {
GPIODriver func() GPIODriver
I2CDriver func() I2CDriver
LEDDriver func() LEDDriver
SPIDriver func() SPIDriver
}
2014-03-23 07:21:22 +05:30
// The Describer type is a Descriptor provider.
type Describer func(rev int) *Descriptor
2014-03-23 07:21:22 +05:30
// Describers is a global list of registered host Describers.
2014-03-24 02:50:24 +05:30
var describers = make(map[Host]Describer)
// Register makes a host describer available by the provided host key.
// If Register is called twice with the same host or if describer is nil,
// it panics.
2014-03-24 02:50:24 +05:30
func Register(host Host, describer Describer) {
if describer == nil {
panic("embd: describer is nil")
}
if _, dup := describers[host]; dup {
panic("embd: describer already registered")
}
describers[host] = describer
2014-04-11 13:59:58 +05:30
glog.V(1).Infof("embd: host %v is registered", host)
2014-03-24 02:50:24 +05:30
}
var hostOverride Host
var hostRevOverride int
var hostOverriden bool
// SetHost overrides the host and revision no.
func SetHost(host Host, rev int) {
hostOverride = host
hostRevOverride = rev
hostOverriden = true
}
2014-03-23 07:21:22 +05:30
// DescribeHost returns the detected host descriptor.
// Can be overriden by calling SetHost though.
2014-03-03 00:51:23 +05:30
func DescribeHost() (*Descriptor, error) {
var host Host
var rev int
if hostOverriden {
host, rev = hostOverride, hostRevOverride
} else {
var err error
host, rev, err = DetectHost()
if err != nil {
return nil, err
}
}
2014-03-24 02:50:24 +05:30
describer, ok := describers[host]
if !ok {
2014-03-02 19:39:30 +05:30
return nil, fmt.Errorf("host: invalid host %q", host)
}
return describer(rev), nil
}
2014-03-23 07:21:22 +05:30
// ErrFeatureNotSupported is returned when the host does not support a
// particular feature.
2014-03-23 06:41:51 +05:30
var ErrFeatureNotSupported = errors.New("embd: requested feature is not supported")
2014-03-23 07:21:22 +05:30
// ErrFeatureNotImplemented is returned when a particular feature is supported
// by the host but not implemented yet.
2014-03-23 06:41:51 +05:30
var ErrFeatureNotImplemented = errors.New("embd: requested feature is not implemented")