new: ble.show shows device names if available for at least one of the devices

This commit is contained in:
evilsocket 2019-02-15 13:41:39 +01:00
commit a72801f9b5
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
2 changed files with 46 additions and 14 deletions

View file

@ -19,7 +19,7 @@ var (
blePresentInterval = time.Duration(30) * time.Second 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) rssi := network.ColorRSSI(dev.RSSI)
address := network.NormalizeMac(dev.Device.ID()) address := network.NormalizeMac(dev.Device.ID())
vendor := tui.Dim(ops.Ternary(dev.Vendor == "", dev.Advertisement.Company, dev.Vendor).(string)) 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) address = tui.Dim(address)
} }
return []string{ if withName {
rssi, return []string{
address, rssi,
vendor, address,
dev.Advertisement.Flags.String(), dev.Name(),
isConnectable, vendor,
lastSeen, 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 return
} }
func (mod *BLERecon) colNames() []string { func (mod *BLERecon) colNames(withName bool) []string {
colNames := []string{"RSSI", "MAC", "Vendor", "Flags", "Connect", "Seen"} 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 { switch mod.selector.SortField {
case "rssi": case "rssi":
colNames[0] += " " + mod.selector.SortSymbol colNames[0] += " " + mod.selector.SortSymbol
case "mac": case "mac":
colNames[1] += " " + mod.selector.SortSymbol colNames[1] += " " + mod.selector.SortSymbol
case "seen": case "seen":
colNames[5] += " " + mod.selector.SortSymbol colNames[seenIdx] += " " + mod.selector.SortSymbol
} }
return colNames return colNames
} }
@ -116,13 +133,21 @@ func (mod *BLERecon) Show() error {
return err return err
} }
hasName := false
for _, dev := range devices {
if dev.Name() != "" {
hasName = true
break
}
}
rows := make([][]string, 0) rows := make([][]string, 0)
for _, dev := range devices { for _, dev := range devices {
rows = append(rows, mod.getRow(dev)) rows = append(rows, mod.getRow(dev, hasName))
} }
if len(rows) > 0 { if len(rows) > 0 {
tui.Table(os.Stdout, mod.colNames(), rows) tui.Table(os.Stdout, mod.colNames(hasName), rows)
mod.Session.Refresh() mod.Session.Refresh()
} }

View file

@ -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) { func (d *BLEDevice) MarshalJSON() ([]byte, error) {
doc := bleDeviceJSON{ doc := bleDeviceJSON{
LastSeen: d.LastSeen, LastSeen: d.LastSeen,
Name: d.Device.Name(), Name: d.Name(),
MAC: d.Device.ID(), MAC: d.Device.ID(),
Vendor: d.Vendor, Vendor: d.Vendor,
RSSI: d.RSSI, RSSI: d.RSSI,
} }
return json.Marshal(doc) return json.Marshal(doc)
} }