#!/usr/bin/python """ Portfinder script for Debian Linux Tries to detect the port to which a Sun SPOT is connected. Prints a comma separated listed of ports on stderr. """ import sys, commands, glob def _run(cmd): status, out = commands.getstatusoutput(cmd) if status != 0: raise ValueError("%r (%r)" % (cmd, status)) return [line.strip() for line in out.splitlines()] def halFindByProperty(key, string, parent=None): cmd = "hal-find-by-property --key %s --string '%s'" % (key, string) if parent: cmd += " --key info.parent --string '%s'" % parent return _run(cmd) def halGetProperty(udi, key): cmd = "hal-get-property --udi '%s' --key '%s'" % (udi, key) return _run(cmd)[0] def portsViaHal(): ports = [] print "Using Hardware Abstraction Layer (HAL) to probe Sun SPOTS..." for spot in halFindByProperty(key="info.product", string="Sun SPOT"): # find CDC_ACM driven devices with SPOT parents for acmdriven in halFindByProperty(key="info.linux.driver", string="cdc_acm", parent=spot): # find the serial port that the CDC_ACM driver offers try: serialports = halFindByProperty(key="info.product", string="Serial Port", parent=acmdriven) except ValueError: #print "No serial ports for %s" % acmdriven continue for serialportdevice in serialports: # extract the serial port serialport = halGetProperty(udi=serialportdevice, key='serial.device') spotid = halGetProperty(udi=spot, key='usb_device.serial') ports.append("%s (%s)" % (serialport, spotid)) return ports def portsViaDev(): pattern = "/dev/ttyACM*" print """ WARNING: Your Linux installation does not include the 'hal-device' application. The spotfinder utility will treat all devices matching the pattern: %s as Sun SPOT devices. If no Sun SPOT device is connected but another device matching this pattern is available, that device will be selected automatically. """ % pattern return glob.glob(pattern) try: ports = portsViaHal() except ValueError: ports = portsViaDev() print >>sys.stderr, ", ".join(ports)