def process(): killer = GracefulKiller() dial_tone_enabled = not "--disable-dial-tone" in sys.argv # Make sure pppd isn't running with open(os.devnull, 'wb') as devnull: subprocess.call(["sudo", "killall", "pppd"], stderr=devnull) BAUD_SPEED = 56000 device_and_speed, internet_connected = None, False ## Startup checks, make sure that we don't do anything until ## we have a modem and internet connection while True: logger.info("Detecting connection and modem...") internet_connected = check_internet_connection() device_and_speed = detect_device_and_speed() if internet_connected and device_and_speed: logger.info("Internet connected and device found!") break elif not internet_connected: logger.warn("Unable to detect an internet connection. Waiting...") elif not device_and_speed: logger.warn("Unable to find a modem device. Waiting...") time.sleep(5) modem = Modem(device_and_speed[0], device_and_speed[1], BAUD_SPEED, dial_tone_enabled) dreamcast_ip = autoconfigure_ppp(modem.device_name, modem.device_speed) # Get a port forwarding object, now that we know the DC IP. port_forwarding = PortForwarding(dreamcast_ip, logger) mode = "LISTENING" modem.connect() if dial_tone_enabled: modem.start_dial_tone() time_digit_heard = None dcnow = DreamcastNowService() while True: if killer.kill_now: break now = datetime.now() if mode == "LISTENING": modem.update() char = modem._serial.read(1).strip() if not char: continue if ord(char) == 16: #DLE character try: char = modem._serial.read(1) digit = int(char) logger.info("Heard: %s", digit) mode = "ANSWERING" modem.stop_dial_tone() time_digit_heard = now except (TypeError, ValueError): pass elif mode == "ANSWERING": if (now - time_digit_heard).total_seconds() > 8.0: time_digit_heard = None modem.answer() modem.disconnect() mode = "CONNECTED" elif mode == "CONNECTED": dcnow.go_online(dreamcast_ip) port_forwarding.forward_all() # We start watching /var/log/messages for the hang up message for line in sh.tail("-f", "/var/log/messages", "-n", "1", _iter=True): if "Modem hangup" in line: logger.info( "Detected modem hang up, going back to listening") time.sleep(5) # Give the hangup some time break port_forwarding.delete_all() dcnow.go_offline() mode = "LISTENING" modem = Modem(device_and_speed[0], device_and_speed[1], BAUD_SPEED, dial_tone_enabled) modem.connect() if dial_tone_enabled: modem.start_dial_tone() return 0
def process(): killer = GracefulKiller() dial_tone_enabled = not "--disable-dial-tone" in sys.argv # Make sure pppd isn't running with open(os.devnull, 'wb') as devnull: subprocess.call(["sudo", "killall", "pppd"], stderr=devnull) BAUD_SPEED = 57600 device_and_speed, internet_connected = None, False ## Startup checks, make sure that we don't do anything until ## we have a modem and internet connection while True: logger.info("Detecting connection and modem...") internet_connected = check_internet_connection() device_and_speed = detect_device_and_speed() if internet_connected and device_and_speed: logger.info("Internet connected and device found!") break elif not internet_connected: logger.warn("Unable to detect an internet connection. Waiting...") elif not device_and_speed: logger.warn("Unable to find a modem device. Waiting...") time.sleep(5) modem = Modem(device_and_speed[0], device_and_speed[1], BAUD_SPEED, dial_tone_enabled) dreamcast_ip = autoconfigure_ppp(modem.device_name, modem.device_speed) mode = "LISTENING" modem.connect() if dial_tone_enabled: modem.start_dial_tone() time_digit_heard = None dcnow = DreamcastNowService() while True: if killer.kill_now: break now = datetime.now() if mode == "LISTENING": modem.update() char = modem._serial.read(1).strip() if not char: continue if ord(char) == 16: #DLE character try: char = modem._serial.read(1) digit = int(char) logger.info("Heard: %s", digit) mode = "ANSWERING" modem.stop_dial_tone() time_digit_heard = now except (TypeError, ValueError): pass elif mode == "ANSWERING": if (now - time_digit_heard).total_seconds() > 8.0: time_digit_heard = None modem.answer() modem.disconnect() mode = "CONNECTED" elif mode == "CONNECTED": dcnow.go_online(dreamcast_ip) # We start watching /var/log/messages for the hang up message for line in sh.tail("-f", "/var/log/messages", "-n", "1", _iter=True): if "Modem hangup" in line: logger.info("Detected modem hang up, going back to listening") time.sleep(5) # Give the hangup some time break dcnow.go_offline() mode = "LISTENING" modem = Modem(device_and_speed[0], device_and_speed[1], BAUD_SPEED, dial_tone_enabled) modem.connect() if dial_tone_enabled: modem.start_dial_tone() return 0
def process(): killer = GracefulKiller() dial_tone_enabled = "--disable-dial-tone" not in sys.argv # Initial cleanup: old processes and config files with open(os.devnull, 'wb') as devnull: # use sh -c to delay eval instead of shell=True; globbing would be expanded with the current user otherwise subprocess.call([ "sudo", "sh", "-c", "rm /etc/ppp/peers/dreamcast* /var/run/ppp-dreamcast*.pid" ], stderr=devnull) # Make sure pppd isn't running with open(os.devnull, 'wb') as devnull: subprocess.call(["sudo", "killall", "pppd"], stderr=devnull) devices, internet_connected = None, False # Startup checks, make sure that we don't do anything until # we have a modem and internet connection while True: logger.info("Detecting connection and modem...") internet_connected = check_internet_connection() devices = detect_devices_and_speed() if internet_connected and devices: logger.info("Internet connected and devices found!") break elif not internet_connected: logger.warn("Unable to detect an internet connection. Waiting...") elif not devices: logger.warn("Unable to find a modem device. Waiting...") time.sleep(5) autoconfigure_ppp(devices) count = 0 for device in devices: count += 1 if count < len(devices): # we're not on the last device, fork! if os.fork() == 0: # parent process continue modem = Modem(device['device'], device['speed'], dial_tone_enabled) # if len(devices) == 1: # Get a port forwarding object, now that we know the DC IP. # port_forwarding = PortForwarding(dreamcast_ip, logger) # Disabled until we can figure out a faster way of doing this.. it takes a minute # on my router which is way too long to wait for the DreamPi to boot # port_forwarding.forward_all() mode = "LISTENING" modem.connect() if dial_tone_enabled: modem.start_dial_tone() time_digit_heard = None # TODO pass device identifier to let dcnow distinguish different dreamcasts ? dcnow = DreamcastNowService() while True: if killer.kill_now: break now = datetime.now() if mode == "LISTENING": modem.update() char = modem._serial.read(1).strip() if not char: continue if ord(char) == 16: # DLE character try: char = modem._serial.read(1) digit = int(char) logger.info("Heard: %s", digit) mode = "ANSWERING" modem.stop_dial_tone() time_digit_heard = now except (TypeError, ValueError): pass elif mode == "ANSWERING": if (now - time_digit_heard).total_seconds() > 8.0: time_digit_heard = None modem.answer(device['name']) modem.disconnect() pid = find_pppd_pid(device['name']) device['pid'] = pid mode = "CONNECTED" elif mode == "CONNECTED": # TODO pass device identifier to let dcnow distinguish different dreamcasts ? dcnow.go_online(device['dreamcast_ip']) # We start watching /var/log/messages for the hang up message pidstring = "pppd[{}]".format(device['pid']) for line in sh.tail("-f", "/var/log/messages", "-n", "1", _iter=True): # ignore lines that do not come from our pppd pid if pidstring not in line: continue if "Modem hangup" in line: logger.info( "Detected modem hang up, going back to listening") time.sleep(5) # Give the hangup some time break dcnow.go_offline() mode = "LISTENING" modem = Modem(device['device'], device['speed'], dial_tone_enabled) modem.connect() if dial_tone_enabled: modem.start_dial_tone() # if len(devices) == 1: # Temporarily disabled, see above # port_forwarding.delete_all() return 0