def init_mavlink(profile, wlans): cfg = getattr(settings, '%s_mavlink' % (profile,)) cmd_rx = ('%s -p %d -u %d -K %s -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_rx'), cfg.stream_rx, cfg.port_rx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.fec_k, cfg.fec_n)).split() + wlans cmd_tx = ('%s -p %d -u %d -K %s -B %d -G %s -S %d -L %d -M %d -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_tx'), cfg.stream_tx, cfg.port_tx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.bandwidth, "short" if cfg.short_gi else "long", cfg.stbc, cfg.ldpc, cfg.mcs_index, cfg.fec_k, cfg.fec_n)).split() + wlans if connect_re.match(cfg.peer): m = connect_re.match(cfg.peer) connect = m.group('addr'), int(m.group('port')) listen = None log.msg('Connect telem stream %d(RX), %d(TX) to %s:%d' % (cfg.stream_rx, cfg.stream_tx, connect[0], connect[1])) elif listen_re.match(cfg.peer): m = listen_re.match(cfg.peer) listen = m.group('addr'), int(m.group('port')) connect = None log.msg('Listen for telem stream %d(RX), %d(TX) on %s:%d' % (cfg.stream_rx, cfg.stream_tx, listen[0], listen[1])) else: raise Exception('Unsupport peer address: %s' % (cfg.peer,)) # The first argument is not None only if we initiate mavlink connection p_in = UDPProxyProtocol(connect, agg_max_size=settings.common.radio_mtu, agg_timeout=settings.common.mavlink_agg_timeout, inject_rssi=cfg.inject_rssi) p_tx_l = [UDPProxyProtocol(('127.0.0.1', cfg.port_tx + i)) for i, _ in enumerate(wlans)] p_rx = UDPProxyProtocol() p_rx.peer = p_in sockets = [ reactor.listenUDP(listen[1] if listen else 0, p_in), reactor.listenUDP(cfg.port_rx, p_rx) ] sockets += [ reactor.listenUDP(0, p_tx) for p_tx in p_tx_l ] log.msg('Telem RX: %s' % (' '.join(cmd_rx),)) log.msg('Telem TX: %s' % (' '.join(cmd_tx),)) ant_f = AntennaFactory(p_in, p_tx_l) if cfg.stats_port: reactor.listenTCP(cfg.stats_port, ant_f) dl = [RXProtocol(ant_f, cmd_rx, 'telem rx').start(), TXProtocol(cmd_tx, 'telem tx').start()] def _cleanup(x): for s in sockets: s.stopListening() return x return defer.gatherResults(dl, consumeErrors=True).addBoth(_cleanup)\ .addErrback(lambda f: f.trap(defer.FirstError) and f.value.subFailure)
def init_mavlink(profile, wlans): cfg = getattr(settings, '%s_mavlink' % (profile, )) cmd_rx = ('%s -p %d -u %d -K %s' % \ (os.path.join(settings.path.bin_dir, 'wfb_rx'), cfg.stream_rx, cfg.port_rx, os.path.join(settings.path.conf_dir, cfg.keypair))).split() + wlans cmd_tx = ('%s -p %d -u %d -K %s -B %d -G %s -S %d -M %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_tx'), cfg.stream_tx, cfg.port_tx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.bandwidth, "short" if cfg.short_gi else "long", cfg.stbc, cfg.mcs_index)).split() + wlans if cfg.listen: connect = None listen = cfg.listen else: connect = ('127.0.0.1', cfg.connect) listen = 0 p_in = UDPProxyProtocol(connect, agg_max_size=settings.common.radio_mtu, agg_timeout=settings.common.mavlink_agg_timeout, inject_rssi=cfg.inject_rssi) p_tx_l = [ UDPProxyProtocol(('127.0.0.1', cfg.port_tx + i)) for i, _ in enumerate(wlans) ] p_rx = UDPProxyProtocol() p_rx.peer = p_in sockets = [ reactor.listenUDP(listen, p_in), reactor.listenUDP(cfg.port_rx, p_rx) ] sockets += [reactor.listenUDP(0, p_tx) for p_tx in p_tx_l] log.msg('Telem RX: %s' % (' '.join(cmd_rx), )) log.msg('Telem TX: %s' % (' '.join(cmd_tx), )) ant_f = AntennaFactory(p_in, p_tx_l) if cfg.stats_port: reactor.listenTCP(cfg.stats_port, ant_f) dl = [ RXProtocol(ant_f, cmd_rx, 'telem rx').start(), TXProtocol(cmd_tx, 'telem tx').start() ] def _cleanup(x): for s in sockets: s.stopListening() return x return defer.gatherResults(dl, consumeErrors=True).addBoth(_cleanup)\ .addErrback(lambda f: f.trap(defer.FirstError) and f.value.subFailure)
def init_tunnel(profile, wlans): cfg = getattr(settings, '%s_tunnel' % (profile, )) cmd_rx = ('%s -p %d -u %d -K %s -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_rx'), cfg.stream_rx, cfg.port_rx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.fec_k, cfg.fec_n)).split() + wlans cmd_tx = ('%s -p %d -u %d -K %s -B %d -G %s -S %d -L %d -M %d -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_tx'), cfg.stream_tx, cfg.port_tx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.bandwidth, "short" if cfg.short_gi else "long", cfg.stbc, cfg.ldpc, cfg.mcs_index, cfg.fec_k, cfg.fec_n)).split() + wlans p_in = TUNTAPProtocol() p_tx_l = [ UDPProxyProtocol(('127.0.0.1', cfg.port_tx + i)) for i, _ in enumerate(wlans) ] p_rx = UDPProxyProtocol() p_rx.peer = p_in tun_ep = TUNTAPTransport(reactor, p_in, cfg.ifname, cfg.ifaddr, mtu=settings.common.radio_mtu) sockets = [reactor.listenUDP(cfg.port_rx, p_rx)] sockets += [reactor.listenUDP(0, p_tx) for p_tx in p_tx_l] log.msg('Tunnel RX: %s' % (' '.join(cmd_rx), )) log.msg('Tunnel TX: %s' % (' '.join(cmd_tx), )) ant_f = AntennaFactory(p_in, p_tx_l) if cfg.stats_port: reactor.listenTCP(cfg.stats_port, ant_f) dl = [ RXProtocol(ant_f, cmd_rx, 'tunnel rx').start(), TXProtocol(cmd_tx, 'tunnel tx').start() ] def _cleanup(x): tun_ep.loseConnection() for s in sockets: s.stopListening() return x return defer.gatherResults(dl, consumeErrors=True).addBoth(_cleanup)\ .addErrback(lambda f: f.trap(defer.FirstError) and f.value.subFailure)