Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a show device tab #53

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions apps/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(self):
self.known_attributes = []
self.device = None
self.connected_peer = None
self.top_tab = 'scan'
self.top_tab = 'device'
self.monitor_rssi = False
self.connection_rssi = None

Expand Down Expand Up @@ -166,7 +166,8 @@ def make_completer():
'scan': None,
'services': None,
'attributes': None,
'log': None
'log': None,
'device': None
},
'filter': {
'address': None,
Expand Down Expand Up @@ -212,6 +213,7 @@ def make_completer():
self.scan_results_text = FormattedTextControl()
self.services_text = FormattedTextControl()
self.attributes_text = FormattedTextControl()
self.device_text = FormattedTextControl()
self.log_text = FormattedTextControl(get_cursor_position=lambda: Point(0, max(0, len(self.log_lines) - 1)))
self.log_height = Dimension(min=7, weight=4)
self.log_max_lines = 100
Expand All @@ -234,6 +236,10 @@ def make_completer():
Frame(Window(self.log_text, height=self.log_height), title='Log'),
filter=Condition(lambda: self.top_tab == 'log')
),
ConditionalContainer(
Frame(Window(self.device_text), title='Device'),
filter=Condition(lambda: self.top_tab == 'device')
),
Frame(Window(self.output, height=self.output_height)),
FormattedTextToolbar(text=self.get_status_bar_text, style='reverse'),
self.input_field
Expand Down Expand Up @@ -275,6 +281,7 @@ async def run_async(self, device_config, transport):
self.device = Device.with_hci('Bumble', 'F0:F1:F2:F3:F4:F5', hci_source, hci_sink)
self.device.listener = DeviceListener(self)
await self.device.power_on()
self.show_device(self.device)

# Run the UI
await self.ui.run_async()
Expand Down Expand Up @@ -356,7 +363,7 @@ def show_services(self, services):
self.services_text.text = lines
self.ui.invalidate()

async def show_attributes(self, attributes):
def show_attributes(self, attributes):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive by fix: this was the only show_* method that was async, but it doesn't need to be.

lines = []

for attribute in attributes:
Expand All @@ -365,6 +372,44 @@ async def show_attributes(self, attributes):
self.attributes_text.text = lines
self.ui.invalidate()

def show_device(self, device):
lines = []

lines.append(('ansicyan', 'Name: '))
lines.append(('', f'{device.name}\n'))
lines.append(('ansicyan', 'Public Address: '))
lines.append(('', f'{device.public_address}\n'))
lines.append(('ansicyan', 'Random Address: '))
lines.append(('', f'{device.random_address}\n'))
lines.append(('ansicyan', 'LE Enabled: '))
lines.append(('', f'{device.le_enabled}\n'))
lines.append(('ansicyan', 'Classic Enabled: '))
lines.append(('', f'{device.classic_enabled}\n'))
lines.append(('ansicyan', 'Classic SC Enabled: '))
lines.append(('', f'{device.classic_sc_enabled}\n'))
lines.append(('ansicyan', 'Classic SSP Enabled: '))
lines.append(('', f'{device.classic_ssp_enabled}\n'))
lines.append(('ansicyan', 'Classic Class: '))
lines.append(('', f'{device.class_of_device}\n'))
lines.append(('ansicyan', 'Discoverable: '))
lines.append(('', f'{device.discoverable}\n'))
lines.append(('ansicyan', 'Connectable: '))
lines.append(('', f'{device.connectable}\n'))
lines.append(('ansicyan', 'Advertising Data: '))
lines.append(('', f'{device.advertising_data}\n'))
lines.append(('ansicyan', 'Scan Response Data: '))
lines.append(('', f'{device.scan_response_data}\n'))
advertising_interval = (
device.advertising_interval_min
if device.advertising_interval_min == device.advertising_interval_max
else f"{device.advertising_interval_min} to {device.advertising_interval_max}"
)
lines.append(('ansicyan', 'Advertising Interval: '))
lines.append(('', f'{advertising_interval}\n'))

self.device_text.text = lines
self.ui.invalidate()

def append_to_output(self, line, invalidate=True):
if type(line) is str:
line = [('', line)]
Expand Down Expand Up @@ -413,7 +458,7 @@ async def discover_attributes(self):
attributes = await self.connected_peer.discover_attributes()
self.append_to_output(f'discovered {len(attributes)} attributes...')

await self.show_attributes(attributes)
self.show_attributes(attributes)

def find_characteristic(self, param):
parts = param.split('.')
Expand Down Expand Up @@ -576,7 +621,7 @@ async def do_advertise(self, params):

async def do_show(self, params):
if params:
if params[0] in {'scan', 'services', 'attributes', 'log'}:
if params[0] in {'scan', 'services', 'attributes', 'log', 'device'}:
self.top_tab = params[0]
self.ui.invalidate()

Expand Down