diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2005-04-29 18:12:48 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2005-04-29 18:12:48 +0000 |
commit | 2947852ee491953d81acb6d0822bc45da5a36c41 (patch) | |
tree | 2970cc2e7911793804881a30715b6d850bd4dfd9 /gpsd.hotplug | |
parent | 15ef9420ea6112b537c20b42d33aabf10a1fba80 (diff) | |
download | gpsd-2947852ee491953d81acb6d0822bc45da5a36c41.tar.gz |
Refactored, we're probably going to put most of this code
in a library module soon.
Diffstat (limited to 'gpsd.hotplug')
-rwxr-xr-x | gpsd.hotplug | 82 |
1 files changed, 40 insertions, 42 deletions
diff --git a/gpsd.hotplug b/gpsd.hotplug index 27876875..7b0635cd 100755 --- a/gpsd.hotplug +++ b/gpsd.hotplug @@ -1,7 +1,8 @@ #!/usr/bin/python # Hotplug script for gpsd by Eric S. Raymond, March 2005 # This script is part of the gpsd distribution: see http://gpsd.berlios.de -# Can be called like "gpsd.hotplug [+-]/dev/ttyUSB0" for test purposes. +# Can be called like "gpsd.hotplug [add|remove] /dev/ttyUSB0" for test +# purposes. import sys, os, syslog, glob, socket CONTROL_SOCKET = "/var/run/gpsd.sock" @@ -23,56 +24,53 @@ def gpsd_control_connect(): else: return sockfile -def wake_up_gpsd(devpath, action): - subnodes = glob.glob("/sys" + devpath + "/*") - subnodes = map(os.path.basename, subnodes) - subnodes = filter(lambda s: s.startswith("ttyUSB"), subnodes) - if len(subnodes) == 0: - syslog.syslog("no ttyUSB device under " + devpath) - return - elif len(subnodes) > 1: - syslog.syslog("too many ttyUSB devices under " + devpath) - return - else: - tty = "/dev/" + subnodes[0] - syslog.syslog(tty + " has gone active") - - # Next get a connection to gpsd; start one if not already running - connect = gpsd_connect() - if connect: - syslog.syslog("reached a running gpsd") - elif action == 'add': - syslog.syslog("attempting to launch gpsd") - os.system("gpsd -F " + CONTROL_SOCKET) - connect = gpsd_control_connect() - if not connect: - return - - # We've got a live connection to the gpsd control socket. No - # need to parse response, because gpsd will lock on to the - # device if it's really a GPS and ignore it if it's not. - if action == 'add': - connect.write("+%s\r\n" % tty) - elif action == 'remove': - connect.write("-%s\r\n" % tty) - connect.close() - return +def gpsd_control(action, argument): + "Pass a command to gpsd; start the daemon if not already running." + connect = gpsd_control_connect() + if connect: + syslog.syslog("reached a running gpsd") + elif action == 'add': + syslog.syslog("attempting to launch gpsd") + os.system("gpsd -F " + CONTROL_SOCKET) + connect = gpsd_control_connect() + if not connect: + return None + # We've got a live connection to the gpsd control socket. No + # need to parse the response, because gpsd will lock on to the + # device if it's really a GPS and ignore it if it's not. + if action == 'add': + connect.write("+%s\r\n" % argument) + elif action == 'remove': + connect.write("-%s\r\n" % argument) + connect.close() + return action def hotplug(action, devpath): - syslog.openlog('gpsdplug', 0, syslog.LOG_DAEMON) - syslog.syslog("gpsdplug begins with action: %s" % action) + syslog.openlog('gpsd.hotplug', 0, syslog.LOG_DAEMON) + syslog.syslog("gpsd.hotplug begins with action: %s" % action) if not devpath: syslog.syslog("No device") else: - wake_up_gpsd(devpath, action) + subnodes = glob.glob("/sys" + devpath + "/*") + subnodes = map(os.path.basename, subnodes) + subnodes = filter(lambda s: s.startswith("ttyUSB"), subnodes) + if len(subnodes) == 0: + syslog.syslog("no ttyUSB device under " + devpath) + return + elif len(subnodes) > 1: + syslog.syslog("too many ttyUSB devices under " + devpath) + return + else: + tty = "/dev/" + subnodes[0] + syslog.syslog(tty + " has gone active") - syslog.syslog("gpsdplug ends") + gpsd_control(action, tty) + return + syslog.syslog("gpsd.hotplug ends") syslog.closelog() if __name__ == '__main__': if len(sys.argv) == 1: # Called as hotplug script hotplug(os.getenv("ACTION"), os.getenv("DEVPATH")) else: # Called by hand for testing - fp = gpsd_control_connect() - fp.write(sys.argv[1]+"\n") - fp.close() + gpsd_control(sys.argv[1], sys.argv[2]) |