+ def printConfigList(self, confList):
+ printDebugMsg("== printConfigList ==");
+ for (key, entry) in confList.items():
+ printDebugMsg("%s = %s"%(key , str(entry.value)));
+
+ printDebugMsg("== printConfigList end ==");
+
+ def loadHostapConfig(self):
+ global apModeConfig
+ fd = -1
+ if access("/etc/hostapd.conf", R_OK) is True:
+ printDebugMsg("open /etc/hostapd.conf")
+ fd = open("/etc/hostapd.conf", "r")
+ elif access("/etc/hostapd.conf.linuxap.back", R_OK) is True:
+ printDebugMsg("open /etc/hostapd.conf.linuxap.back")
+ fd = open("/etc/hostapd.conf.linuxap.back", "r")
+ if fd == -1:
+ printDebugMsg("can not open hostapd.conf")
+ return -1
+
+ for line in fd.readlines():
+ line = line.strip()
+
+ if (len(line) == 0) or (line.find('=') == -1):
+ continue
+
+ data = line.split('=', 1)
+ if len(data) != 2:
+ continue
+
+ key = data[0].strip()
+ value = data[1].strip()
+
+ if key == "#wep_key0":
+ self.hostapdConf["wep_key0"].value = value
+ apModeConfig.wep.value = False
+
+ elif key == "wep_key0":
+ self.hostapdConf["wep_key0"].value = value
+ apModeConfig.wep.value = True
+
+ elif key.startswith('#'):
+ continue
+
+ elif key == "channel" :
+ if int(value) not in range(14):
+ self.hostapdConf[key].value = 1
+ else:
+ self.hostapdConf[key].value = int(value)
+
+ elif key in ["beacon_int", "rts_threshold", "fragm_threshold", "wpa_group_rekey"]:
+ self.hostapdConf[key].value = int(value)
+
+ elif key in self.hostapdConf.keys():
+ self.hostapdConf[key].value = value
+
+ fd.close()
+ self.printConfigList(self.hostapdConf)
+
+ return 0
+
+ def writeHostapdConfig(self):
+ global apModeConfig
+ global ORIG_HOSTAPD_CONF
+ self.printConfigList(self.hostapdConf)
+ if access(ORIG_HOSTAPD_CONF, R_OK) is not True:
+ self.msg = "can not access file. (%s)" % ORIG_HOSTAPD_CONF
+ printDebugMsg(self.msg)
+ return -1
+
+ orig_conf = open(ORIG_HOSTAPD_CONF, "r")
+ if orig_conf == -1:
+ print "can't open file. (%s)" % ORIG_HOSTAPD_CONF
+
+ new_conf = open(HOSTAPD_CONF, "w")
+ if new_conf == -1:
+ print "can't open file. (%s)" % HOSTAPD_CONF
+
+ isEncryptOn = apModeConfig.encrypt.value is True
+ isEncryptWEP = apModeConfig.method.value == "0"
+ isEncryptWPA = not isEncryptWEP
+
+ for r_line in orig_conf.readlines():
+ line = r_line.strip()
+ if len(line) < 2:
+ new_conf.write(r_line)
+ continue
+
+ fix_line = None
+# for encrypt line
+ if line.find("wep_default_key=") != -1 : # is wepLine
+ if isEncryptOn and isEncryptWEP :
+ fix_line = "wep_default_key=%s\n" % self.hostapdConf["wep_default_key"].value
+
+ elif line.find("wep_key0=") != -1 : # is WepKeyLine
+ if isEncryptOn:
+ if isEncryptWEP :
+ fix_line = "wep_key0=%s\n" % self.hostapdConf["wep_key0"].value
+ else:
+ fix_line = "#wep_key0=%s\n" % self.hostapdConf["wep_key0"].value
+
+ else:
+ fix_line = "#wep_key0=%s\n" % self.hostapdConf["wep_key0"].value
+
+ elif line.find("wpa=") != -1 : # is wpaLine
+ if isEncryptOn and isEncryptWPA :
+ fix_line = "wpa=%s\n" % apModeConfig.method.value
+##
+ elif line.startswith("#ssid"):
+ pass
+
+ else:
+ for (key , entry) in self.hostapdConf.items():
+ value = str(entry.value)
+ pos = line.find(key+'=')
+ if ( (pos != -1) and (pos < 2) ) and len(value)!=0 :
+ fix_line = "%s=%s\n" % (key, value)
+ break
+
+# if fix_line is not None:
+# print "r_line : ", r_line,
+# print "fix_li : ", fix_line
+
+ if fix_line is not None:
+ new_conf.write(fix_line)
+ else:
+ new_conf.write(r_line)
+
+ orig_conf.close()
+ new_conf.close()
+ return 0
+