From a72801f9b5411d245df44863bd0f626084b5ef51 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Fri, 15 Feb 2019 13:41:39 +0100 Subject: [PATCH] new: ble.show shows device names if available for at least one of the devices --- modules/ble/ble_show.go | 49 +++++++++++++++++++++++++++++++---------- network/ble_device.go | 11 +++++++-- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/modules/ble/ble_show.go b/modules/ble/ble_show.go index 8025cf81..5ce7e26b 100644 --- a/modules/ble/ble_show.go +++ b/modules/ble/ble_show.go @@ -19,7 +19,7 @@ var ( blePresentInterval = time.Duration(30) * time.Second ) -func (mod *BLERecon) getRow(dev *network.BLEDevice) []string { +func (mod *BLERecon) getRow(dev *network.BLEDevice, withName bool) []string { rssi := network.ColorRSSI(dev.RSSI) address := network.NormalizeMac(dev.Device.ID()) vendor := tui.Dim(ops.Ternary(dev.Vendor == "", dev.Advertisement.Company, dev.Vendor).(string)) @@ -34,13 +34,25 @@ func (mod *BLERecon) getRow(dev *network.BLEDevice) []string { address = tui.Dim(address) } - return []string{ - rssi, - address, - vendor, - dev.Advertisement.Flags.String(), - isConnectable, - lastSeen, + if withName { + return []string{ + rssi, + address, + dev.Name(), + vendor, + dev.Advertisement.Flags.String(), + isConnectable, + lastSeen, + } + } else { + return []string{ + rssi, + address, + vendor, + dev.Advertisement.Flags.String(), + isConnectable, + lastSeen, + } } } @@ -97,15 +109,20 @@ func (mod *BLERecon) doSelection() (err error, devices []*network.BLEDevice) { return } -func (mod *BLERecon) colNames() []string { +func (mod *BLERecon) colNames(withName bool) []string { colNames := []string{"RSSI", "MAC", "Vendor", "Flags", "Connect", "Seen"} + seenIdx := 5 + if withName { + colNames = []string{"RSSI", "MAC", "Name", "Vendor", "Flags", "Connect", "Seen"} + seenIdx = 6 + } switch mod.selector.SortField { case "rssi": colNames[0] += " " + mod.selector.SortSymbol case "mac": colNames[1] += " " + mod.selector.SortSymbol case "seen": - colNames[5] += " " + mod.selector.SortSymbol + colNames[seenIdx] += " " + mod.selector.SortSymbol } return colNames } @@ -116,13 +133,21 @@ func (mod *BLERecon) Show() error { return err } + hasName := false + for _, dev := range devices { + if dev.Name() != "" { + hasName = true + break + } + } + rows := make([][]string, 0) for _, dev := range devices { - rows = append(rows, mod.getRow(dev)) + rows = append(rows, mod.getRow(dev, hasName)) } if len(rows) > 0 { - tui.Table(os.Stdout, mod.colNames(), rows) + tui.Table(os.Stdout, mod.colNames(hasName), rows) mod.Session.Refresh() } diff --git a/network/ble_device.go b/network/ble_device.go index 814f7098..b1ef5380 100644 --- a/network/ble_device.go +++ b/network/ble_device.go @@ -40,14 +40,21 @@ func NewBLEDevice(p gatt.Peripheral, a *gatt.Advertisement, rssi int) *BLEDevice } } +func (d *BLEDevice) Name() string { + name := d.Device.Name() + if name == "" && d.Advertisement != nil { + name = d.Advertisement.LocalName + } + return name +} + func (d *BLEDevice) MarshalJSON() ([]byte, error) { doc := bleDeviceJSON{ LastSeen: d.LastSeen, - Name: d.Device.Name(), + Name: d.Name(), MAC: d.Device.ID(), Vendor: d.Vendor, RSSI: d.RSSI, } - return json.Marshal(doc) }