Debian Codegraph @

modules/ble/ble_recon_events.go

raw symbols SCIP
1 // +build !windows
2
3 package ble
4
5 import (
6 "github.com/bettercap/gatt"
7 )
8
9 func (mod *BLERecon) onStateChanged(dev gatt.Device, s gatt.State) {
10 mod.Debug("state changed to %v", s)
11
12 switch s {
13 case gatt.StatePoweredOn:
14 if mod.currDevice == nil {
15 mod.Debug("starting discovery ...")
16 dev.Scan([]gatt.UUID{}, true)
17 } else {
18 mod.Debug("current device was not cleaned: %v", mod.currDevice)
19 }
20 case gatt.StatePoweredOff:
21 mod.Debug("resetting device instance")
22 mod.gattDevice.StopScanning()
23 mod.setCurrentDevice(nil)
24 mod.gattDevice = nil
25
26 default:
27 mod.Warning("unexpected state: %v", s)
28 }
29 }
30
31 func (mod *BLERecon) onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
32 mod.Session.BLE.AddIfNew(p.ID(), p, a, rssi)
33 }
34
35 func (mod *BLERecon) onPeriphDisconnected(p gatt.Peripheral, err error) {
36 mod.Session.Events.Add("ble.device.disconnected", mod.currDevice)
37 mod.setCurrentDevice(nil)
38 if mod.Running() {
39 mod.Debug("device disconnected, restoring discovery.")
40 mod.gattDevice.Scan([]gatt.UUID{}, true)
41 }
42 }
43
44 func (mod *BLERecon) onPeriphConnected(p gatt.Peripheral, err error) {
45 if err != nil {
46 mod.Warning("connected to %s but with error: %s", p.ID(), err)
47 return
48 } else if mod.currDevice == nil {
49 mod.Warning("connected to %s but after the timeout :(", p.ID())
50 return
51 }
52
53 mod.connected = true
54
55 defer func(per gatt.Peripheral) {
56 mod.Debug("disconnecting from %s ...", per.ID())
57 per.Device().CancelConnection(per)
58 mod.setCurrentDevice(nil)
59 }(p)
60
61 mod.Session.Events.Add("ble.device.connected", mod.currDevice)
62
63 if err := p.SetMTU(500); err != nil {
64 mod.Warning("failed to set MTU: %s", err)
65 }
66
67 mod.Debug("connected, enumerating all the things for %s!", p.ID())
68 services, err := p.DiscoverServices(nil)
69 // https://github.com/bettercap/bettercap/issues/498
70 if err != nil && err.Error() != "success" {
71 mod.Error("error discovering services: %s", err)
72 return
73 }
74
75 mod.showServices(p, services)
76 }