1# Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import re 7import socket 8import time 9 10from autotest_lib.client.bin import test 11from autotest_lib.client.common_lib import error, utils 12 13 14class hardware_GPS(test.test): 15 version = 1 16 17 def run_once(self): 18 # Default gpsd port. Can be changed in /etc/init/gpsd.conf. 19 gpsd_port = 2947 20 match = False 21 gpsd_started = False 22 23 gpsd_status = utils.system_output('initctl status gpsd') 24 if not 'start/running' in gpsd_status: 25 utils.system('initctl start gpsd') 26 gpsd_started = True 27 for _ in range(10): 28 try: 29 c = socket.create_connection(('localhost', gpsd_port)) 30 except socket.error: 31 time.sleep(1) 32 continue 33 c.close() 34 break 35 36 gpspipe = utils.system_output( 37 'gpspipe -r -n 10 localhost:%d' % gpsd_port, timeout=60) 38 logging.debug(gpspipe) 39 for line in gpspipe.split('\n'): 40 line = line.strip() 41 42 # For now - just look for any GPS sentence in the output. 43 match = re.search( 44 r'^\$GP(BOD|BWC|GGA|GLL|GSA|GSV|HDT|R00|RMA|RMB|' + 45 r'RMC|RTE|STN|TRF|VBW|VTG|WPL|XTE|ZDA)', 46 line) 47 48 if match: 49 break 50 51 if gpsd_started: 52 # If it was us who started it - shut it back down. 53 utils.system('initctl stop gpsd') 54 55 if not match: 56 raise error.TestFail('Unable to find GPS device') 57