hybridgroup.gobot/platforms/bleclient/helpers_test.go

120 lines
2.8 KiB
Go
Raw Normal View History

2024-02-12 00:01:24 +08:00
package bleclient
import (
"fmt"
"time"
"tinygo.org/x/bluetooth"
)
type btTestAdapter struct {
deviceAddress string
rssi int16
scanDelay time.Duration
payload *btTestPayload
simulateEnableErr bool
simulateScanErr bool
simulateStopScanErr bool
simulateConnectErr bool
}
func (bta *btTestAdapter) Enable() error {
if bta.simulateEnableErr {
return fmt.Errorf("adapter enable error")
}
return nil
}
func (bta *btTestAdapter) Scan(callback func(*bluetooth.Adapter, bluetooth.ScanResult)) error {
if bta.simulateScanErr {
return fmt.Errorf("adapter scan error")
}
devAddr, err := bluetooth.ParseMAC(bta.deviceAddress)
if err != nil {
// normally this error should not happen in test
return err
}
time.Sleep(bta.scanDelay)
a := bluetooth.Address{MACAddress: bluetooth.MACAddress{MAC: devAddr}}
r := bluetooth.ScanResult{Address: a, RSSI: bta.rssi, AdvertisementPayload: bta.payload}
callback(nil, r)
return nil
}
func (bta *btTestAdapter) StopScan() error {
if bta.simulateStopScanErr {
return fmt.Errorf("adapter stop scan error")
}
return nil
}
2024-11-05 16:22:08 +08:00
func (bta *btTestAdapter) Connect(addr bluetooth.Address, _ bluetooth.ConnectionParams) (bluetooth.Device, error) {
2024-02-12 00:01:24 +08:00
if bta.simulateConnectErr {
2024-11-05 16:22:08 +08:00
return bluetooth.Device{}, fmt.Errorf("adapter connect error")
2024-02-12 00:01:24 +08:00
}
2024-11-05 16:22:08 +08:00
return bluetooth.Device{Address: addr}, nil
2024-02-12 00:01:24 +08:00
}
type btTestPayload struct {
name string
}
func (ptp *btTestPayload) LocalName() string { return ptp.name }
func (*btTestPayload) HasServiceUUID(bluetooth.UUID) bool { return true }
func (*btTestPayload) Bytes() []byte { return nil }
2024-11-05 16:22:08 +08:00
func (*btTestPayload) ManufacturerData() []bluetooth.ManufacturerDataElement { return nil }
func (*btTestPayload) ServiceData() []bluetooth.ServiceDataElement { return nil }
2024-02-12 00:01:24 +08:00
type btTestDevice struct {
simulateDiscoverServicesErr bool
simulateDisconnectErr bool
}
2024-11-05 16:22:08 +08:00
func (btd btTestDevice) DiscoverServices(_ []bluetooth.UUID) ([]bluetooth.DeviceService, error) {
2024-02-12 00:01:24 +08:00
if btd.simulateDiscoverServicesErr {
return nil, fmt.Errorf("device discover services error")
}
// for this test we can not return any []bluetooth.DeviceService
return nil, nil
}
2024-11-05 16:22:08 +08:00
func (btd btTestDevice) Disconnect() error {
2024-02-12 00:01:24 +08:00
if btd.simulateDisconnectErr {
return fmt.Errorf("device disconnect error")
}
return nil
}
type btTestChara struct {
readData []byte
writtenData []byte
notificationFunc func(buf []byte)
}
func (btc *btTestChara) Read(data []byte) (int, error) {
copy(data, btc.readData)
return len(btc.readData), nil
}
func (btc *btTestChara) WriteWithoutResponse(data []byte) (int, error) {
btc.writtenData = append(btc.writtenData, data...)
return len(data), nil
}
func (btc *btTestChara) EnableNotifications(callback func(buf []byte)) error {
btc.notificationFunc = callback
return nil
}