2014-03-23 02:51:22 +01:00
|
|
|
// Host descriptor data structures.
|
|
|
|
|
2014-03-02 20:21:23 +01:00
|
|
|
package embd
|
2014-02-27 01:29:38 +01:00
|
|
|
|
2014-03-23 01:55:32 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-04-11 10:29:58 +02:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2014-03-23 01:55:32 +01:00
|
|
|
)
|
2014-02-27 01:29:38 +01:00
|
|
|
|
2014-03-23 02:51:22 +01:00
|
|
|
// Descriptor represents a host descriptor.
|
2014-02-27 01:29:38 +01:00
|
|
|
type Descriptor struct {
|
2014-03-23 02:02:24 +01:00
|
|
|
GPIODriver func() GPIODriver
|
|
|
|
I2CDriver func() I2CDriver
|
|
|
|
LEDDriver func() LEDDriver
|
2014-04-27 21:48:14 +02:00
|
|
|
SPIDriver func() SPIDriver
|
2014-02-27 01:29:38 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 02:51:22 +01:00
|
|
|
// The Describer type is a Descriptor provider.
|
2014-02-27 01:29:38 +01:00
|
|
|
type Describer func(rev int) *Descriptor
|
|
|
|
|
2014-03-23 02:51:22 +01:00
|
|
|
// Describers is a global list of registered host Describers.
|
2014-03-23 22:20:24 +01:00
|
|
|
var describers = make(map[Host]Describer)
|
|
|
|
|
2014-03-23 22:31:58 +01:00
|
|
|
// 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-23 22:20:24 +01:00
|
|
|
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 10:29:58 +02:00
|
|
|
|
|
|
|
glog.V(1).Infof("embd: host %v is registered", host)
|
2014-03-23 22:20:24 +01:00
|
|
|
}
|
2014-02-27 01:29:38 +01:00
|
|
|
|
2014-03-23 22:31:58 +01:00
|
|
|
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 02:51:22 +01:00
|
|
|
// DescribeHost returns the detected host descriptor.
|
2014-03-23 22:31:58 +01:00
|
|
|
// Can be overriden by calling SetHost though.
|
2014-03-02 20:21:23 +01:00
|
|
|
func DescribeHost() (*Descriptor, error) {
|
2014-03-23 22:31:58 +01:00
|
|
|
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-02-27 01:29:38 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 22:20:24 +01:00
|
|
|
describer, ok := describers[host]
|
2014-02-27 01:29:38 +01:00
|
|
|
if !ok {
|
2014-03-02 15:09:30 +01:00
|
|
|
return nil, fmt.Errorf("host: invalid host %q", host)
|
2014-02-27 01:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return describer(rev), nil
|
|
|
|
}
|
2014-03-23 01:55:32 +01:00
|
|
|
|
2014-03-23 02:51:22 +01:00
|
|
|
// ErrFeatureNotSupported is returned when the host does not support a
|
|
|
|
// particular feature.
|
2014-03-23 02:11:51 +01:00
|
|
|
var ErrFeatureNotSupported = errors.New("embd: requested feature is not supported")
|
2014-03-23 02:51:22 +01:00
|
|
|
|
|
|
|
// ErrFeatureNotImplemented is returned when a particular feature is supported
|
|
|
|
// by the host but not implemented yet.
|
2014-03-23 02:11:51 +01:00
|
|
|
var ErrFeatureNotImplemented = errors.New("embd: requested feature is not implemented")
|