Skip to content

Commit

Permalink
feat(src): Allow listing devices of concrete types
Browse files Browse the repository at this point in the history
Currently, the "list" command returns all discovered devices. This
includes also devices without host services like printers. That is a
problem for some API clients (i.e. gvfs). Let's add an optional
argument to specify concrete type.

Fixes: #178
  • Loading branch information
ondrejholy committed Dec 12, 2023
1 parent 55fe3e6 commit 5402645
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 6 additions & 4 deletions man/wsdd.8
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ only. The following commands can be issued:
Clears the list of all discovered devices. Use the \fBprobe\fR command to
search for devices again. This command does not return any data and is only
available in discover mode.
.SS \fBlist\fR - List discovered devices
Returns a tab-separated list of discovered devices with the following information.
The possibly empty list of detected hosts is always terminated with a single
dot ('.') in an otherwise empty line. This command is only available in discover mode.
.SS \fBlist \fI[TYPE]\fR - List discovered devices
Returns a tab-separated list of discovered devices of the provided TYPE (e.g.
"pub:Computer") with the following information. If no type is provided, all
discovered devices are listed. The possibly empty list of detected hosts is
always terminated with a single dot ('.') in an otherwise empty line. This
command is only available in discover mode.
.TP
UUID
UUID of the discovered device. Note that a multi-homed device should appear
Expand Down
10 changes: 7 additions & 3 deletions src/wsdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ def extract_wsdp_props(self, root: ElementTree.Element, dialect: str) -> None:

def extract_host_props(self, root: ElementTree.Element) -> None:
types = root.findtext('wsdp:Types', '', namespaces)
self.props['types'] = types.split(' ')[0]
self.props['types'] = types.split(' ')
if types != PUB_COMPUTER:
return

Expand Down Expand Up @@ -1162,7 +1162,8 @@ def handle_command(self, line: str, write_stream: asyncio.StreamWriter) -> None:
logger.debug('clearing list of known devices')
WSDDiscoveredDevice.instances.clear()
elif command == 'list' and args.discovery:
write_stream.write(bytes(self.get_list_reply(), 'utf-8'))
wsd_type = command_args[0] if command_args else None
write_stream.write(bytes(self.get_list_reply(wsd_type), 'utf-8'))
elif command == 'quit':
write_stream.close()
elif command == 'start':
Expand All @@ -1175,9 +1176,12 @@ def handle_command(self, line: str, write_stream: asyncio.StreamWriter) -> None:
def get_clients_by_interface(self, interface: Optional[str]) -> List[WSDClient]:
return [c for c in WSDClient.instances if c.mch.address.interface.name == interface or not interface]

def get_list_reply(self) -> str:
def get_list_reply(self, wsd_type: Optional[str]) -> str:
retval = ''
for dev_uuid, dev in WSDDiscoveredDevice.instances.items():
if wsd_type and (('types' not in dev.props) or (wsd_type not in dev.props['types'])):
continue

addrs_str = []
for addrs in dev.addresses.items():
addrs_str.append(', '.join(['{}'.format(a) for a in addrs]))
Expand Down

0 comments on commit 5402645

Please sign in to comment.