utils_ap.py 6.75 KB
import os
import utils_config ## incluse ssh command execution method

# Global variables
path_module_b43 = "/lib/modules/2.6.36/kernel/drivers/net/wireless"
path_module_80211 = "/lib/modules/2.6.36/kernel/net/mac80211"
path_firm_b43_blockack = "/lib/firmware/b43.video/firmware.blockack"

# Get ap data from peers file
#	- Input: Path to peers file and wired ip where AP is located
#	- Output: peer[0] (wlan0 ip - 192.168.98.1), peer[1] (mac address), peer[2] (wired ip - 192.168.97.1)
def search_ap_data(peers_file, ap_addr):
	with open(peers_file) as peers_file_opened:
		for peer in peers_file_opened:
			if ap_addr in peer:
				peer = peer.split()
				return peer[0], peer[1], peer[2]

	return "EMPTY", "EMPTY", "EMPTY"


# Reload all the modules needed by the ap
#	- Input: ap_addr
def reload_ap(ap_addr):
	print "Reload AP modules using wired ip -> %s" % ap_addr

	hostapd_pids = utils_config.exec_command_ssh(ap_addr, "pgrep -x hostapd")
	if hostapd_pids != "ERROR":
		for pid in hostapd_pids:
			pid = int(pid)
			utils_config.exec_command_ssh(ap_addr, ("kill -SIGTERM %d" % pid))
			print "Process hostapd (pid = %d) killed" % pid

	print "Reconfiguring Interface ..."
	utils_config.exec_command_ssh(ap_addr, "rmmod ath9k && sleep 0.5")
	utils_config.exec_command_ssh(ap_addr, "rmmod mac80211")
	utils_config.exec_command_ssh(ap_addr, "rmmod b43")

	utils_config.exec_command_ssh(ap_addr, "rm /lib/firmware/b43")
	utils_config.exec_command_ssh(ap_addr, ("rm %s/b43/b43.ko" % path_module_b43))
	utils_config.exec_command_ssh(ap_addr, ("rm %s/b43" % path_module_b43))
	utils_config.exec_command_ssh(ap_addr, ("rm %s/mac80211.ko" % path_module_80211))

	utils_config.exec_command_ssh(ap_addr, ("ln -s %s/mac80211.video.ko %s/mac80211.ko" % (path_module_80211, path_module_80211)))
	utils_config.exec_command_ssh(ap_addr, "modprobe mac80211")
	utils_config.exec_command_ssh(ap_addr, ("ln -s %s/b43.video %s/b43" % (path_module_b43, path_module_b43)))

# Enable the required modules according to the selected mechanism
#	- Input: AP address, mechanism and rate. In addition b_size or retries if mechanism is BLOCK_ACK or UR
def set_mechanism(ap_addr, mechanism, rate, b_size, retries):
	print "Mechanism %s selected for the AP. Loading modules ..." % mechanism

	if mechanism == "BLOCK_ACK":
		set_block_ack(ap_addr, rate, b_size)
	elif mechanism == "DMS":
		set_dms()
	elif mechanism == "UR":
		set_ur(ap_addr, rate, retries)
	elif mechanism == "LEGACY":
		set_legacy(ap_addr, rate)
		
# Check if all the configuration is correctly set
#	- Input: AP address
def check_ap(ap_addr, wlan0_ip):
	print "Checking AP ..."
	if utils_config.exec_command_ssh(ap_addr, "pgrep -x hostapd") == "ERROR":
		return "hostapd"
	elif utils_config.exec_command_ssh(ap_addr, "modprobe -l | grep mac80211.ko") == "ERROR":
		return "mac80211"
	elif utils_config.exec_command_ssh(ap_addr, "modprobe -l | grep b43.ko") == "ERROR":
		return "b43"
	else:
		ap_status = utils_config.check_ping(wlan0_ip)
		if ap_status != "ok":
			return "ERROR"
		else:
			return "ok"

# Load modules in order to enable BLOCK_ACK mechanism
#	- Input: AP wired address, rate and b_size
def set_block_ack(ap_addr, rate, b_size):
	utils_config.exec_command("cp %s/%s/ucode5.back-%s.fw %s/ucode5.fw" % (path_firm_b43_blockack, rate, b_size, path_firm_b43_blockack))
	# DMA
	#utils_config.exec_command_ssh(ap_addr, ("cp %s/%s/ucode5.back-%s.fw.dma %s/ucode5.fw" % (path_firm_b43_blockack, rate, b_size, path_firm_b43_blockack)))
	utils_config.exec_command_ssh(ap_addr, "cp %s/%s/ucode5.back-%s.fw %s/ucode5.fw" % (path_firm_b43_blockack, rate, b_size, path_firm_b43_blockack))
	# END DMA

	utils_config.exec_command_ssh(ap_addr, "ln -s %s /lib/firmware/b43" % path_firm_b43_blockack)
	utils_config.exec_command_ssh(ap_addr, "ln -s %s/b43/b43.rate-adaptation.ko %s/b43/b43.ko" % (path_module_b43, path_module_b43))
	#utils_config.exec_command_ssh(ap_addr, "ln -s %s/b43/b43.blockack.ko %s/b43/b43.ko" % (path_module_b43, path_module_b43))
#PABLO
# ATTENTION: frate=1 implies the transmission rate is fixed, so no rate controller
	utils_config.exec_command_ssh(ap_addr, "modprobe b43 qos=1 aseq=1 frate=0 bsize=%s brate=%s && sleep 0.5" % (b_size, rate))


# Load modules in order to enable DMS mechanism
#	- Input: AP wired address and rate
def set_dms(ap_addr, rate):
	utils_config.exec_command_ssh(ap_addr, "ln -s /lib/firmware/b43.video/firmware.dms /lib/firmware/b43")
	utils_config.exec_command_ssh(ap_addr, "ln -s %s/b43/b43.dms.ko %s/b43/b43.ko" % (path_module_b43, path_module_b43))
	utils_config.exec_command_ssh(ap_addr, "modprobe b43 qos=0 brate=%s && sleep 0.5" % rate)


# Load modules in order to enable UR mechanism
#	- Input: AP wired address, rate and retries
def set_ur(ap_addr, rate, retries):
	utils_config.exec_command_ssh(ap_addr, "ln -s /lib/firmware/b43.video/firmware.ur /lib/firmware/b43")
	utils_config.exec_command_ssh(ap_addr, "rm /lib/firmware/b43.video/firmware.ur/ucode5.fw")
	utils_config.exec_command_ssh(ap_addr, "ln -s /lib/firmware/b43.video/firmware.ur/ucode5.ur-%s.fw /lib/firmware/b43/ucode5.fw" % retries)
	utils_config.exec_command_ssh(ap_addr, "ln -s %s/b43/b43.ur.ko %s/b43/b43.ko" % (path_module_b43, path_module_b43))
	utils_config.exec_command_ssh(ap_addr, "modprobe b43 qos=1 brate=%s && sleep 0.5" % rate)


# Load modules in order to enable LEGACY mechanism
#	- Input: AP wired address and rate
def set_legacy(ap_addr, rate):
	utils_config.exec_command_ssh(ap_addr, "ln -s /lib/firmware/b43.video/firmware.dms /lib/firmware/b43")
	utils_config.exec_command_ssh(ap_addr, "ln -s %s/b43/b43.dms.ko %s/b43/b43.ko" % (path_module_b43, path_module_b43))
	utils_config.exec_command_ssh(ap_addr, "modprobe b43 qos=0 brate=%s && sleep 0.5" % rate)

# Starts AP with the given configuration and set up wlan0
#	- Input: Input: AP address, rate, channel and power that we want to stablish in the AP
def start_ap(ap_addr, wlan0_ip, rate, channel, power):
	print "Starting AP on %s with rate = %s, power = %s " % (wlan0_ip, rate, power)
	utils_config.exec_command_ssh(ap_addr, "ifconfig wlan0 %s up" % wlan0_ip)
	utils_config.exec_command_ssh(ap_addr, "iw reg set JP")

	#print "Running hostapd and setting his configuration and log file"
	utils_config.exec_command_ssh(ap_addr, ("cp $PWD/hostapd-minimal-R %s-%s.conf $PWD/hostapd-minimal.conf" % (rate, channel)))
	utils_config.exec_command_ssh(ap_addr, "$PWD/hostapd -ddd $PWD/hostapd-minimal.conf > $PWD/hostap.log &")
	# utils_config.exec_command_ssh(ap_addr, "hostapd -B /root/hostapd-minimal.conf")

	#print "Configuring Rate and TX Power"
	utils_config.exec_command_ssh(ap_addr, ("iwconfig wlan0 rate %s fixed txpower %s fixed" % (rate, power)))

	#print "Configuring multicast route"
	utils_config.exec_command_ssh(ap_addr, "route add -host 224.0.0.1 wlan0")

	#print "Setting arp table"
	#utils_config.exec_command_ssh(ap_addr, "$PWD/setarp.sh $PATH_AP")