Commit e78417b5 authored by Michał Narajowski's avatar Michał Narajowski Committed by Kevin
Browse files

Add option to pass HCI options when creating a device (#28)

This can be useful when we need to use specific HCI device ID.
parent c9d4987c
......@@ -37,7 +37,7 @@ type Device struct {
}
// NewDevice returns a BLE device.
func NewDevice(opts ...Option) (*Device, error) {
func NewDevice(opts ...ble.Option) (*Device, error) {
err := initXpcIDs()
if err != nil {
return nil, err
......@@ -61,7 +61,7 @@ func NewDevice(opts ...Option) (*Device, error) {
}
// Option sets the options specified.
func (d *Device) Option(opts ...Option) error {
func (d *Device) Option(opts ...ble.Option) error {
var err error
for _, opt := range opts {
err = opt(d)
......
package darwin
// An Option is a configuration function, which configures the device.
type Option func(*Device) error
// OptPeripheralRole configures the device to perform Peripheral tasks.
func OptPeripheralRole() Option {
return func(d *Device) error {
d.role = 1
return nil
}
import (
"errors"
"time"
"github.com/go-ble/ble/linux/hci/cmd"
)
// SetPeripheralRole configures the device to perform Peripheral tasks.
func (d *Device) SetPeripheralRole() error {
d.role = 1
return nil
}
// SetCentralRole configures the device to perform Central tasks.
func (d *Device) SetCentralRole() error {
d.role = 0
return nil
}
// SetDeviceID sets HCI device ID.
func (d *Device) SetDeviceID(id int) error {
return errors.New("Not supported")
}
// SetDialerTimeout sets dialing timeout for Dialer.
func (d *Device) SetDialerTimeout(dur time.Duration) error {
return errors.New("Not supported")
}
// SetListenerTimeout sets dialing timeout for Listener.
func (d *Device) SetListenerTimeout(dur time.Duration) error {
return errors.New("Not supported")
}
// OptCentralRole configures the device to perform Central tasks.
func OptCentralRole() Option {
return func(d *Device) error {
d.role = 0
return nil
}
// SetConnParams overrides default connection parameters.
func (d *Device) SetConnParams(param cmd.LECreateConnection) error {
return errors.New("Not supported")
}
package main
import (
"github.com/go-ble/ble"
"github.com/go-ble/ble/linux"
"github.com/go-ble/ble/linux/hci"
"github.com/go-ble/ble/linux/hci/cmd"
"github.com/pkg/errors"
)
......@@ -31,7 +31,7 @@ func updateLinuxParam(d *linux.Device) error {
return errors.Wrap(err, "can't set scan param")
}
if err := d.HCI.Option(hci.OptConnParams(
if err := d.HCI.Option(ble.OptConnParams(
cmd.LECreateConnection{
LEScanInterval: 0x0004, // 0x0004 - 0x4000; N * 0.625 msec
LEScanWindow: 0x0004, // 0x0004 - 0x4000; N * 0.625 msec
......
......@@ -6,6 +6,6 @@ import (
)
// DefaultDevice ...
func DefaultDevice() (d ble.Device, err error) {
return darwin.NewDevice()
func DefaultDevice(opts ...ble.Option) (d ble.Device, err error) {
return darwin.NewDevice(opts...)
}
......@@ -6,6 +6,6 @@ import (
)
// DefaultDevice ...
func DefaultDevice() (d ble.Device, err error) {
return linux.NewDevice()
func DefaultDevice(opts ...ble.Option) (d ble.Device, err error) {
return linux.NewDevice(opts...)
}
package dev
import "github.com/go-ble/ble"
import (
"github.com/go-ble/ble"
)
// NewDevice ...
func NewDevice(impl string) (d ble.Device, err error) {
return DefaultDevice()
func NewDevice(impl string, opts ...ble.Option) (d ble.Device, err error) {
return DefaultDevice(opts...)
}
......@@ -13,17 +13,17 @@ import (
)
// NewDevice returns the default HCI device.
func NewDevice() (*Device, error) {
return NewDeviceWithName("Gopher")
func NewDevice(opts ...ble.Option) (*Device, error) {
return NewDeviceWithName("Gopher", opts...)
}
// NewDeviceWithName returns the default HCI device.
func NewDeviceWithName(name string) (*Device, error) {
return NewDeviceWithNameAndHandler(name, nil)
func NewDeviceWithName(name string, opts ...ble.Option) (*Device, error) {
return NewDeviceWithNameAndHandler(name, nil, opts...)
}
func NewDeviceWithNameAndHandler(name string, handler ble.NotifyHandler) (*Device, error) {
dev, err := hci.NewHCI()
func NewDeviceWithNameAndHandler(name string, handler ble.NotifyHandler, opts ...ble.Option) (*Device, error) {
dev, err := hci.NewHCI(opts...)
if err != nil {
return nil, errors.Wrap(err, "can't create hci")
}
......
......@@ -35,7 +35,7 @@ type pkt struct {
}
// NewHCI returns a hci device.
func NewHCI(opts ...Option) (*HCI, error) {
func NewHCI(opts ...ble.Option) (*HCI, error) {
h := &HCI{
id: -1,
......@@ -171,7 +171,7 @@ func (h *HCI) Error() error {
}
// Option sets the options specified.
func (h *HCI) Option(opts ...Option) error {
func (h *HCI) Option(opts ...ble.Option) error {
var err error
for _, opt := range opts {
err = opt(h)
......
package hci
import (
"errors"
"time"
"github.com/go-ble/ble/linux/hci/cmd"
)
// An Option is a configuration function, which configures the device.
type Option func(*HCI) error
// SetDeviceID sets HCI device ID.
func (h *HCI) SetDeviceID(id int) error {
h.id = id
return nil
}
// SetDialerTimeout sets dialing timeout for Dialer.
func (h *HCI) SetDialerTimeout(d time.Duration) error {
h.dialerTmo = d
return nil
}
// OptDeviceID sets HCI device ID.
func OptDeviceID(id int) Option {
return func(h *HCI) error {
h.id = id
return nil
}
// SetListenerTimeout sets dialing timeout for Listener.
func (h *HCI) SetListenerTimeout(d time.Duration) error {
h.listenerTmo = d
return nil
}
// OptDialerTimeout sets dialing timeout for Dialer.
func OptDialerTimeout(d time.Duration) Option {
return func(h *HCI) error {
h.dialerTmo = d
return nil
}
// SetConnParams overrides default connection parameters.
func (h *HCI) SetConnParams(param cmd.LECreateConnection) error {
h.params.connParams = param
return nil
}
// OptListenerTimeout sets dialing timeout for Listener.
func OptListenerTimeout(d time.Duration) Option {
return func(h *HCI) error {
h.listenerTmo = d
return nil
}
// SetPeripheralRole is not supported
func (h *HCI) SetPeripheralRole() error {
return errors.New("Not supported")
}
// OptConnParams overrides default connection parameters.
func OptConnParams(param cmd.LECreateConnection) Option {
return func(h *HCI) error {
h.params.connParams = param
return nil
}
// SetCentralRole is not supported
func (h *HCI) SetCentralRole() error {
return errors.New("Not supported")
}
package ble
import (
"time"
"github.com/go-ble/ble/linux/hci/cmd"
)
// DeviceOption is an interface which the device should implement to allow using configuration options
type DeviceOption interface {
SetDeviceID(int) error
SetDialerTimeout(time.Duration) error
SetListenerTimeout(time.Duration) error
SetConnParams(cmd.LECreateConnection) error
SetPeripheralRole() error
SetCentralRole() error
}
// An Option is a configuration function, which configures the device.
type Option func(DeviceOption) error
// OptDeviceID sets HCI device ID.
func OptDeviceID(id int) Option {
return func(opt DeviceOption) error {
opt.SetDeviceID(id)
return nil
}
}
// OptDialerTimeout sets dialing timeout for Dialer.
func OptDialerTimeout(d time.Duration) Option {
return func(opt DeviceOption) error {
opt.SetDialerTimeout(d)
return nil
}
}
// OptListenerTimeout sets dialing timeout for Listener.
func OptListenerTimeout(d time.Duration) Option {
return func(opt DeviceOption) error {
opt.SetListenerTimeout(d)
return nil
}
}
// OptConnParams overrides default connection parameters.
func OptConnParams(param cmd.LECreateConnection) Option {
return func(opt DeviceOption) error {
opt.SetConnParams(param)
return nil
}
}
// OptPeripheralRole configures the device to perform Peripheral tasks.
func OptPeripheralRole() Option {
return func(opt DeviceOption) error {
opt.SetPeripheralRole()
return nil
}
}
// OptCentralRole configures the device to perform Central tasks.
func OptCentralRole() Option {
return func(opt DeviceOption) error {
opt.SetCentralRole()
return nil
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment