Browse Source

module: setup project structure, document and add utilities

pull/3/head
cn 2 years ago
parent
commit
2a0e220b8e
10 changed files with 723 additions and 82 deletions
  1. +2
    -0
      .gitignore
  2. +48
    -0
      README.md
  3. +175
    -0
      cmd/munin-miflora/main.go
  4. +125
    -0
      common/common.go
  5. +6
    -1
      go.mod
  6. +14
    -2
      go.sum
  7. +0
    -79
      main.go
  8. +212
    -0
      notes.md
  9. +129
    -0
      utils/legacy/scanner.go
  10. +12
    -0
      utils/reset.py

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
cmd/munin-miflora/munin-miflora
miflorad

+ 48
- 0
README.md View File

@@ -0,0 +1,48 @@
# miflorad

This project aims to produce tools written in Go for interfacing with Xiaomi Flora sensors for IoT use cases.

## Misc

If the Intel Wireless Bluetooth 8265 chip gets stuck ([source](https://bbs.archlinux.org/viewtopic.php?id=193813)):

```bash
# pip install pyusb
sudo python utils/reset.py
```

The output of `gatttool` listing all characteristics of a Xiaomi Flora sensor (firmware version 2.7.0):

```
$ gatttool -b C4:7C:8D:xx:xx:xx --characteristics
handle = 0x0002, char properties = 0x02, char value handle = 0x0003, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0004, char properties = 0x02, char value handle = 0x0005, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x0006, char properties = 0x0a, char value handle = 0x0007, uuid = 00002a02-0000-1000-8000-00805f9b34fb
handle = 0x0008, char properties = 0x02, char value handle = 0x0009, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000d, char properties = 0x22, char value handle = 0x000e, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x0011, char properties = 0x1a, char value handle = 0x0012, uuid = 00000001-0000-1000-8000-00805f9b34fb
handle = 0x0014, char properties = 0x02, char value handle = 0x0015, uuid = 00000002-0000-1000-8000-00805f9b34fb
handle = 0x0016, char properties = 0x12, char value handle = 0x0017, uuid = 00000004-0000-1000-8000-00805f9b34fb
handle = 0x0018, char properties = 0x08, char value handle = 0x0019, uuid = 00000007-0000-1000-8000-00805f9b34fb
handle = 0x001a, char properties = 0x08, char value handle = 0x001b, uuid = 00000010-0000-1000-8000-00805f9b34fb
handle = 0x001c, char properties = 0x0a, char value handle = 0x001d, uuid = 00000013-0000-1000-8000-00805f9b34fb
handle = 0x001e, char properties = 0x02, char value handle = 0x001f, uuid = 00000014-0000-1000-8000-00805f9b34fb
handle = 0x0020, char properties = 0x10, char value handle = 0x0021, uuid = 00001001-0000-1000-8000-00805f9b34fb
handle = 0x0024, char properties = 0x0a, char value handle = 0x0025, uuid = 8082caa8-41a6-4021-91c6-56f9b954cc34
handle = 0x0026, char properties = 0x0a, char value handle = 0x0027, uuid = 724249f0-5ec3-4b5f-8804-42345af08651
handle = 0x0028, char properties = 0x02, char value handle = 0x0029, uuid = 6c53db25-47a1-45fe-a022-7c92fb334fd4
handle = 0x002a, char properties = 0x0a, char value handle = 0x002b, uuid = 9d84b9a3-000c-49d8-9183-855b673fda31
handle = 0x002c, char properties = 0x0e, char value handle = 0x002d, uuid = 457871e8-d516-4ca1-9116-57d0b17b9cb2
handle = 0x002e, char properties = 0x12, char value handle = 0x002f, uuid = 5f78df94-798c-46f5-990a-b3eb6a065c88
handle = 0x0032, char properties = 0x0a, char value handle = 0x0033, uuid = 00001a00-0000-1000-8000-00805f9b34fb
handle = 0x0034, char properties = 0x1a, char value handle = 0x0035, uuid = 00001a01-0000-1000-8000-00805f9b34fb
handle = 0x0037, char properties = 0x02, char value handle = 0x0038, uuid = 00001a02-0000-1000-8000-00805f9b34fb
handle = 0x003b, char properties = 0x02, char value handle = 0x003c, uuid = 00001a11-0000-1000-8000-00805f9b34fb
handle = 0x003d, char properties = 0x1a, char value handle = 0x003e, uuid = 00001a10-0000-1000-8000-00805f9b34fb
handle = 0x0040, char properties = 0x02, char value handle = 0x0041, uuid = 00001a12-0000-1000-8000-00805f9b34fb
```

## Related Work

- [https://wiki.hackerspace.pl/projects:xiaomi-flora](https://wiki.hackerspace.pl/projects:xiaomi-flora) (very nice teardown)
- [https://github.com/open-homeautomation/miflora](https://github.com/open-homeautomation/miflora) (python library)

+ 175
- 0
cmd/munin-miflora/main.go View File

@@ -0,0 +1,175 @@
package main

import (
"flag"
"fmt"
"os"
"regexp"
"strings"
"time"

"miflorad/common"

"github.com/currantlabs/gatt"
"github.com/currantlabs/gatt/examples/option"
)

const discoveryTimeout = 4 * time.Second
const connectionTimeout = 4 * time.Second

type DiscoveryResult struct {
p gatt.Peripheral
a *gatt.Advertisement
rssi int
}

var discoveryDone = make(chan DiscoveryResult)
var connectionDone = make(chan struct{})

func onStateChanged(device gatt.Device, state gatt.State) {
fmt.Fprintln(os.Stderr, "State:", state)
switch state {
case gatt.StatePoweredOn:
fmt.Fprintln(os.Stderr, "Scanning...")
device.Scan([]gatt.UUID{}, false)
return
default:
device.StopScanning()
}
}

func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
id := strings.ToUpper(flag.Args()[1])
if strings.ToUpper(p.ID()) != id {
return
}

// Stop scanning once we've got the peripheral we're looking for.
p.Device().StopScanning()

discoveryDone <- DiscoveryResult{p, a, rssi}
}

func onPeriphConnected(p gatt.Peripheral, err error) {
fmt.Fprintln(os.Stderr, "Connected")

// Note: can hang due when device has terminated the connection on it's own already
// defer p.Device().CancelConnection(p)

if err := p.SetMTU(500); err != nil {
fmt.Fprintf(os.Stderr, "Failed to set MTU, err: %s\n", err)
}

// Discover services and characteristics
{
_, err := p.DiscoverServices([]gatt.UUID{common.MifloraServiceUUID})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to discover services, err: %s\n", err)
return
}
}
for _, service := range p.Services() {
_, err := p.DiscoverCharacteristics(nil, service)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to discover characteristics, err: %s\n", err)
return
}
}

prefix := flag.Args()[0]

regexNonAlphaNumeric, err4 := regexp.Compile("[^a-z0-9]+")
if err4 != nil {
fmt.Fprintf(os.Stderr, "Failed to compile regex, err: %s\n", err4)
}
id := regexNonAlphaNumeric.ReplaceAllString(strings.ToLower(flag.Args()[1]), "")

metaData, err := common.MifloraRequestVersionBattery(p)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to request version battery, err: %s\n", err)
return
}

fmt.Fprintf(os.Stderr, "Firmware version: %s\n", metaData.FirmwareVersion)

fmt.Fprintf(os.Stdout, "%s.miflora.%s.battery_level %d %d\n", prefix, id, metaData.BatteryLevel, time.Now().Unix())
fmt.Fprintf(os.Stdout, "%s.miflora.%s.firmware_version %s %d\n", prefix, id, metaData.NumericFirmwareVersion(), time.Now().Unix())

// for the newer models a magic number must be written before we can read the current data
if metaData.FirmwareVersion >= "2.6.6" {
err2 := common.MifloraRequestModeChange(p)
if err2 != nil {
fmt.Fprintf(os.Stderr, "Failed to request mode change, err: %s\n", err2)
return
}
}

sensorData, err3 := common.MifloraRequstSensorData(p)
if err3 != nil {
fmt.Fprintf(os.Stderr, "Failed to request sensor data, err: %s\n", err3)
return
}
fmt.Fprintf(os.Stdout, "%s.miflora.%s.temperature %.1f %d\n", prefix, id, sensorData.Temperature, time.Now().Unix())
fmt.Fprintf(os.Stdout, "%s.miflora.%s.brightness %d %d\n", prefix, id, sensorData.Brightness, time.Now().Unix())
fmt.Fprintf(os.Stdout, "%s.miflora.%s.moisture %d %d\n", prefix, id, sensorData.Moisture, time.Now().Unix())
fmt.Fprintf(os.Stdout, "%s.miflora.%s.conductivity %d %d\n", prefix, id, sensorData.Conductivity, time.Now().Unix())
}

func onPeriphDisconnected(p gatt.Peripheral, err error) {
fmt.Fprintln(os.Stderr, "Disconnected")
close(connectionDone)
}

func main() {
flag.Parse()
if len(flag.Args()) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [options] prefix peripheral-id\n", os.Args[0])
os.Exit(1)
}

device, err := gatt.NewDevice(option.DefaultClientOptions...)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open device, err: %s\n", err)
os.Exit(1)
}

// Register discovery handler
device.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered))

device.Init(onStateChanged)

var discoveryResult DiscoveryResult

select {
case discoveryResult = <-discoveryDone:
fmt.Fprintln(os.Stderr, "Discovery done")
case <-time.After(discoveryTimeout):
fmt.Fprintln(os.Stderr, "Discovery timed out")
device.StopScanning()
device.Stop()
os.Exit(1)
}

fmt.Fprintf(os.Stderr, "Discovered peripheral ID:%s, NAME:(%s), RSSI:%d\n", discoveryResult.p.ID(), discoveryResult.p.Name(), discoveryResult.rssi)

// Register connection handlers
device.Handle(
gatt.PeripheralConnected(onPeriphConnected),
gatt.PeripheralDisconnected(onPeriphDisconnected),
)

device.Connect(discoveryResult.p)

select {
case <-connectionDone:
fmt.Fprintln(os.Stderr, "Connection done")
case <-time.After(connectionTimeout):
fmt.Fprintln(os.Stderr, "Connection timed out")
// TODO: can hang due when device has terminated the connection on it's own already
// device.CancelConnection(discoveryResult.p)
os.Exit(1)
}

// Note: calls CancelConnetcion() and thus suffers the same problem, kernel will cleanup after our process finishes
// device.Stop()
}

+ 125
- 0
common/common.go View File

@@ -0,0 +1,125 @@
package common

import (
"encoding/binary"
"errors"
"math"
"strconv"
"strings"

"github.com/currantlabs/gatt"
)

type VersionBatteryResponse struct {
FirmwareVersion string // as "x.y.z"
BatteryLevel uint8 // in percent 0-100
}

type SensorDataResponse struct {
Temperature float64 // in degree C
Brightness uint32 // in lux
Moisture uint8 // in percent 0-100
Conductivity uint16 // in µS/cm
}

func MifloraGetModeChangeData() []byte {
return []byte{0xa0, 0x1f}
}

var MifloraServiceUUID = gatt.MustParseUUID("00001204-0000-1000-8000-00805f9b34fb")
var MifloraCharModeChangeUUID = gatt.MustParseUUID("00001a00-0000-1000-8000-00805f9b34fb")
var MifloraCharReadSensorDataUUID = gatt.MustParseUUID("00001a01-0000-1000-8000-00805f9b34fb")
var MifloraCharVersionBatteryUUID = gatt.MustParseUUID("00001a02-0000-1000-8000-00805f9b34fb")

func FindServiceByUUID(services []*gatt.Service, u gatt.UUID) *gatt.Service {
for _, service := range services {
if service.UUID().Equal(u) {
return service
}
}
return nil
}

func FindCharacteristicByUUID(characteristics []*gatt.Characteristic, u gatt.UUID) *gatt.Characteristic {
for _, characteristic := range characteristics {
if characteristic.UUID().Equal(u) {
return characteristic
}
}
return nil
}

func MifloraRequestVersionBattery(p gatt.Peripheral) (VersionBatteryResponse, error) {
mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID)
if mifloraService == nil {
return VersionBatteryResponse{}, errors.New("Failed to get the miflora service")
}

mifloraVersionBatteryChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharVersionBatteryUUID)
if mifloraVersionBatteryChar == nil {
return VersionBatteryResponse{}, errors.New("Failed to get the version battery characteristic")
}

bytes, err := p.ReadCharacteristic(mifloraVersionBatteryChar)
if err != nil {
return VersionBatteryResponse{}, err
}

return VersionBatteryResponse{string(bytes[2:]), uint8(bytes[0])}, nil
}

func MifloraRequestModeChange(p gatt.Peripheral) error {
mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID)
if mifloraService == nil {
return errors.New("Failed to get the miflora service")
}

mifloraModeChangeChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharModeChangeUUID)
if mifloraModeChangeChar == nil {
return errors.New("Failed to discover the mode change characteristic")
}

err := p.WriteCharacteristic(mifloraModeChangeChar, MifloraGetModeChangeData(), false)
if err != nil {
return err
}

return nil
}

func MifloraRequstSensorData(p gatt.Peripheral) (SensorDataResponse, error) {
mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID)
if mifloraService == nil {
return SensorDataResponse{}, errors.New("Failed to get the miflora service")
}

mifloraSensorDataChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharReadSensorDataUUID)
if mifloraSensorDataChar == nil {
return SensorDataResponse{}, errors.New("Failed to discover the sensor data characteristic")
}

bytes, err := p.ReadCharacteristic(mifloraSensorDataChar)
if err != nil {
return SensorDataResponse{}, err
}

return SensorDataResponse{
Temperature: float64(binary.LittleEndian.Uint16(bytes[0:2])) / 10.0,
Brightness: binary.LittleEndian.Uint32(bytes[3:7]),
Moisture: uint8(bytes[7]),
Conductivity: binary.LittleEndian.Uint16(bytes[8:10]),
}, nil
}

func (res VersionBatteryResponse) NumericFirmwareVersion() int {
// turns "2.3.4" into 20304
version := 0
parts := strings.Split(res.FirmwareVersion, ".")
for i, part := range parts {
partNumber, err := strconv.Atoi(part)
if err != nil {
version += int(math.Pow10((len(parts)-(i+1))*2)) * partNumber
}
}
return version
}

+ 6
- 1
go.mod View File

@@ -2,10 +2,15 @@ module miflorad

require (
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142 // indirect
github.com/currantlabs/gatt v0.0.0-20161006170101-f949eac78f4e
github.com/fatih/structs v1.1.0 // indirect
github.com/godbus/dbus v0.0.0-20181031085051-66d97ae // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab // indirect
github.com/muka/ble v0.0.0-20180314094923-5613a57406d1 // indirect
github.com/muka/go-bluetooth v0.0.0-20181012115104-31d8f53bf9a1
github.com/pkg/errors v0.8.0 // indirect
github.com/sirupsen/logrus v1.2.0
github.com/sirupsen/logrus v1.2.0 // indirect
)

+ 14
- 2
go.sum View File

@@ -1,23 +1,35 @@
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142 h1:3jFq2xL4ZajGK4aZY8jz+DAF0FHjI51BXjjSwCzS1Dk=
github.com/coreos/go-systemd v0.0.0-20181031085051-9002847aa142/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/currantlabs/gatt v0.0.0-20161006170101-f949eac78f4e h1:qu1wqkuctiqRtgZu8kNMtFxQ7/xXuOxSJZ2kYoOxFM0=
github.com/currantlabs/gatt v0.0.0-20161006170101-f949eac78f4e/go.mod h1:GCdlaU9vOYeye8wQtSZNyZ4j5PhmnJ2HUqhRZO0KoZI=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/godbus/dbus v0.0.0-20181031085051-66d97ae h1:NTs1uIj/Ru/QlpKwd9C9dnv/5zblvCXH7Dbn2oi3p98=
github.com/godbus/dbus v0.0.0-20181031085051-66d97ae/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab h1:n8cgpHzJ5+EDyDri2s/GC7a9+qK3/YEGnBsd0uS/8PY=
github.com/mgutz/logxi v0.0.0-20161027140823-aebf8a7d67ab/go.mod h1:y1pL58r5z2VvAjeG1VLGc8zOQgSOzbKN7kMHPvFXJ+8=
github.com/muka/ble v0.0.0-20180314094923-5613a57406d1 h1:DrMDOYCxMG7TUMjhBWnRsgPfpPlz0sFL11ukl+2+CZU=
github.com/muka/ble v0.0.0-20180314094923-5613a57406d1/go.mod h1:1lEPQDLh8Z86+REeQMqID8UpXCSdHF0fvU0qZj3b1eA=
github.com/muka/go-bluetooth v0.0.0-20181012115104-31d8f53bf9a1 h1:VKhCDuxZ3+Bg+Plf7+CxgGRlNEdwHDKUqHoZLzer8Ms=
github.com/muka/go-bluetooth v0.0.0-20181012115104-31d8f53bf9a1/go.mod h1:brKFFAJeW2mWp1W5D/GJhwbn3IpNR69jIm1qGWZcl50=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=


+ 0
- 79
main.go View File

@@ -1,79 +0,0 @@
//shows how to watch for new devices and list them
package main

import (
"os"

log "github.com/sirupsen/logrus"
"github.com/muka/go-bluetooth/api"
"github.com/muka/go-bluetooth/emitter"
"github.com/muka/go-bluetooth/linux"
)

const logLevel = log.DebugLevel
const adapterID = "hci0"

func main() {

log.SetLevel(logLevel)

//clean up connection on exit
defer api.Exit()

log.Debugf("Reset bluetooth device")
a := linux.NewBtMgmt(adapterID)
err := a.Reset()
if err != nil {
log.Error(err)
os.Exit(1)
}

devices, err := api.GetDevices()
if err != nil {
log.Error(err)
os.Exit(1)
}

log.Infof("Cached devices:")
for _, dev := range devices {
showDeviceInfo(&dev)
}

log.Infof("Discovered devices:")
err = discoverDevices(adapterID)
if err != nil {
log.Error(err)
os.Exit(1)
}

select {}
}

func discoverDevices(adapterID string) error {

err := api.StartDiscovery()
if err != nil {
return err
}

log.Debugf("Started discovery")
err = api.On("discovery", emitter.NewCallback(func(ev emitter.Event) {
discoveryEvent := ev.GetData().(api.DiscoveredDeviceEvent)
dev := discoveryEvent.Device
showDeviceInfo(dev)
}))

return err
}

func showDeviceInfo(dev *api.Device) {
if dev == nil {
return
}
props, err := dev.GetProperties()
if err != nil {
log.Errorf("%s: Failed to get properties: %s", dev.Path, err.Error())
return
}
log.Infof("name=%s addr=%s rssi=%d", props.Name, props.Address, props.RSSI)
}

+ 212
- 0
notes.md View File

@@ -0,0 +1,212 @@
INFO[0036] name=Flower care addr=C4:7C:8D:66:D5:27 rssi=-43

list-attributes C4:7C:8D:66:D5:27
connect C4:7C:8D:66:D5:27
select-attribute /org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0002/char0003
select-attribute 00002a00-0000-1000-8000-00805f9b34fb
read 0003

hcitool dev
https://bbs.archlinux.org/viewtopic.php?id=193813
modprobe -r btusb; modprobe btusb

08:38:52::$ gatttool --device=C4:7C:8D:66:D5:27 --char-read -a 0x03
Characteristic value/descriptor: 46 6c 6f 77 65 72 20 63 61 72 65


08:53:32::$ sudo hcitool leinfo C4:7C:8D:66:D5:27
Requesting information ...
Handle: 3585 (0x0e01)
LMP Version: 4.0 (0x6) LMP Subversion: 0x706
Manufacturer: RivieraWaves S.A.S (96)
Features: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00


Attempting to connect to C4:7C:8D:66:D5:27
[CHG] Device C4:7C:8D:66:D5:27 Connected: yes
Connection successful
[NEW] Primary Service
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service000c
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service000c/char000d
00002a05-0000-1000-8000-00805f9b34fb
Service Changed
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service000c/char000d/desc000f
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010
0000fe95-0000-1000-8000-00805f9b34fb
Xiaomi Inc.
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char0011
00000001-0000-1000-8000-00805f9b34fb
SDP
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char0011/desc0013
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char0014
00000002-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char0016
00000004-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char0018
00000007-0000-1000-8000-00805f9b34fb
ATT
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char001a
00000010-0000-1000-8000-00805f9b34fb
UPNP
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char001c
00000013-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char001e
00000014-0000-1000-8000-00805f9b34fb
Hardcopy Data Channel
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char0020
00001001-0000-1000-8000-00805f9b34fb
Browse Group Descriptor Service Class
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0010/char0020/desc0022
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023
0000fef5-0000-1000-8000-00805f9b34fb
Dialog Semiconductor GmbH
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023/char0024
8082caa8-41a6-4021-91c6-56f9b954cc34
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023/char0026
724249f0-5ec3-4b5f-8804-42345af08651
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023/char0028
6c53db25-47a1-45fe-a022-7c92fb334fd4
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023/char002a
9d84b9a3-000c-49d8-9183-855b673fda31
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023/char002c
457871e8-d516-4ca1-9116-57d0b17b9cb2
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023/char002e
5f78df94-798c-46f5-990a-b3eb6a065c88
Vendor specific
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0023/char002e/desc0030
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0031
00001204-0000-1000-8000-00805f9b34fb
Generic Telephony
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0031/char0032
00001a00-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0031/char0034
00001a01-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0031/char0034/desc0036
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0031/char0037
00001a02-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0031/char0037/desc0039
00001a02-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Primary Service
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service003a
00001206-0000-1000-8000-00805f9b34fb
UPNP IP Service
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service003a/char003b
00001a11-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service003a/char003d
00001a10-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service003a/char003d/desc003f
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Characteristic
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service003a/char0040
00001a12-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Descriptor
/org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service003a/char0040/desc0042
00001a12-0000-1000-8000-00805f9b34fb
Unknown
[CHG] Device C4:7C:8D:66:D5:27 ServicesResolved: yes
[CHG] Device C4:7C:8D:66:D5:27 ServicesResolved: no
[CHG] Device C4:7C:8D:66:D5:27 Connected: no

https://github.com/open-homeautomation/miflora/blob/master/miflora/miflora_poller.py#L11
https://github.com/golang/go/wiki/Modules
https://github.com/currantlabs/gatt/blob/master/examples/explorer.go

$ gatttool -b C4:7C:8D:66:D5:27 --characteristics
handle = 0x0002, char properties = 0x02, char value handle = 0x0003, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0004, char properties = 0x02, char value handle = 0x0005, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x0006, char properties = 0x0a, char value handle = 0x0007, uuid = 00002a02-0000-1000-8000-00805f9b34fb
handle = 0x0008, char properties = 0x02, char value handle = 0x0009, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000d, char properties = 0x22, char value handle = 0x000e, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x0011, char properties = 0x1a, char value handle = 0x0012, uuid = 00000001-0000-1000-8000-00805f9b34fb
handle = 0x0014, char properties = 0x02, char value handle = 0x0015, uuid = 00000002-0000-1000-8000-00805f9b34fb
handle = 0x0016, char properties = 0x12, char value handle = 0x0017, uuid = 00000004-0000-1000-8000-00805f9b34fb
handle = 0x0018, char properties = 0x08, char value handle = 0x0019, uuid = 00000007-0000-1000-8000-00805f9b34fb
handle = 0x001a, char properties = 0x08, char value handle = 0x001b, uuid = 00000010-0000-1000-8000-00805f9b34fb
handle = 0x001c, char properties = 0x0a, char value handle = 0x001d, uuid = 00000013-0000-1000-8000-00805f9b34fb
handle = 0x001e, char properties = 0x02, char value handle = 0x001f, uuid = 00000014-0000-1000-8000-00805f9b34fb
handle = 0x0020, char properties = 0x10, char value handle = 0x0021, uuid = 00001001-0000-1000-8000-00805f9b34fb
handle = 0x0024, char properties = 0x0a, char value handle = 0x0025, uuid = 8082caa8-41a6-4021-91c6-56f9b954cc34
handle = 0x0026, char properties = 0x0a, char value handle = 0x0027, uuid = 724249f0-5ec3-4b5f-8804-42345af08651
handle = 0x0028, char properties = 0x02, char value handle = 0x0029, uuid = 6c53db25-47a1-45fe-a022-7c92fb334fd4
handle = 0x002a, char properties = 0x0a, char value handle = 0x002b, uuid = 9d84b9a3-000c-49d8-9183-855b673fda31
handle = 0x002c, char properties = 0x0e, char value handle = 0x002d, uuid = 457871e8-d516-4ca1-9116-57d0b17b9cb2
handle = 0x002e, char properties = 0x12, char value handle = 0x002f, uuid = 5f78df94-798c-46f5-990a-b3eb6a065c88
handle = 0x0032, char properties = 0x0a, char value handle = 0x0033, uuid = 00001a00-0000-1000-8000-00805f9b34fb
handle = 0x0034, char properties = 0x1a, char value handle = 0x0035, uuid = 00001a01-0000-1000-8000-00805f9b34fb
handle = 0x0037, char properties = 0x02, char value handle = 0x0038, uuid = 00001a02-0000-1000-8000-00805f9b34fb
handle = 0x003b, char properties = 0x02, char value handle = 0x003c, uuid = 00001a11-0000-1000-8000-00805f9b34fb
handle = 0x003d, char properties = 0x1a, char value handle = 0x003e, uuid = 00001a10-0000-1000-8000-00805f9b34fb
handle = 0x0040, char properties = 0x02, char value handle = 0x0041, uuid = 00001a12-0000-1000-8000-00805f9b34fb

Service: 0000120400001000800000805f9b34fb
Characteristic 00001a0000001000800000805f9b34fb
properties read write
value 0000 | "\x00\x00"
Characteristic 00001a0100001000800000805f9b34fb
properties read write notify
value aabbccddeeff99887766000000000000 | "\xaa\xbb\xcc\xdd\xee\xff\x99\x88wf\x00\x00\x00\x00\x00\x00"
Descriptor 2902 (Client Characteristic Configuration)
value 0000 | "\x00\x00"
Characteristic 00001a0200001000800000805f9b34fb
properties read
value 6415322e372e30 | "d\x152.7.0"
Descriptor 00001a0200001000800000805f9b34fb
value 6415322e372e30 | "d\x152.7.0"

+ 129
- 0
utils/legacy/scanner.go View File

@@ -0,0 +1,129 @@
//shows how to watch for new devices and list them
package legacy

import (
"os"

"github.com/muka/go-bluetooth/api"
"github.com/muka/go-bluetooth/emitter"
log "github.com/sirupsen/logrus"
)

const adapterID = "hci0"

// func main() {
// manager, err := api.NewManager()
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// err = manager.RefreshState()
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// var address = "C4:7C:8D:66:D5:27"
//
// dev, err := api.GetDeviceByAddress(address)
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// if dev == nil {
// log.Infof("No device found!")
// os.Exit(1)
// }
//
// dev.GetAllServicesAndUUID()
//
// props, err := dev.GetProperties()
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// log.Infof("name=%s addr=%s rssi=%d", props.Name, props.Address, props.RSSI)
//
// err = dev.Connect()
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// if !dev.IsConnected() {
// log.Infof("Device not connected!")
// os.Exit(1)
// }
//
// y, err := dev.GetCharByUUID("00002a00-0000-1000-8000-00805f9b34fb")
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// log.Infof(y.Path)
//
// err = dev.Disconnect()
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
// }

// func main() {
// log.SetLevel(log.DebugLevel)
//
// // clean up connection on exit
// defer api.Exit()
//
// manager, err := api.NewManager()
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// err = manager.RefreshState()
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
//
// boo, err := api.AdapterExists(adapterID)
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
// log.Debugf("AdapterExists: %b", boo)
//
// err = api.StartDiscoveryOn(adapterID)
// if err != nil {
// log.Error(err)
// os.Exit(1)
// }
// log.Debugf("Started discovery")
//
// err = api.On("discovery", emitter.NewCallback(func(ev emitter.Event) {
// discoveryEvent := ev.GetData().(api.DiscoveredDeviceEvent)
// dev := discoveryEvent.Device
// handleDevice(dev)
// }))
//
// select {}
// }
//
// func handleDevice(dev *api.Device) {
// if dev == nil {
// return
// }
//
// props, err := dev.GetProperties()
// if err != nil {
// log.Error(err)
// return
// }
//
// log.Infof("name=%s addr=%s rssi=%d", props.Name, props.Address, props.RSSI)
// }

+ 12
- 0
utils/reset.py View File

@@ -0,0 +1,12 @@
#!/usr/bin/python

# This script allows hard-resetting the Intel Wireless Bluetooth 8265 chip
# (ID 8087:0a2b) built into newer Thinkpads which tends to get stuck as other
# people noticed before: https://bbs.archlinux.org/viewtopic.php?id=193813

# Setup: pip install pyusb
# Usage: sudo python reset.py

from usb.core import find as finddev
dev = finddev(idVendor=0x8087, idProduct=0x0a2b)
dev.reset()

Loading…
Cancel
Save