Skip to content

Commit

Permalink
Wi-Fi regulatory country change implementation. Issue #48.
Browse files Browse the repository at this point in the history
  • Loading branch information
martignoni committed Jul 11, 2018
1 parent b2627f0 commit 5f20daf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bin/changewifisettings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ FILE=${DIR%/*}/.wifisettings
CONFIGFILE="/etc/hostapd/hostapd.conf"
# New values taken from $FILE.
NEWCHANNEL="$(grep '^channel\b' $FILE | cut -d= -f2)"
NEWCOUNTRY="$(grep '^country\b' $FILE | cut -d= -f2)"
NEWPASSWORD="$(grep '^password\b' $FILE | cut -d= -f2)"
NEWSSID="$(grep '^ssid\b' $FILE | cut -d= -f2)"
PASSWORDPROTECTED="$(grep '^passwordprotected\b' $FILE | cut -d= -f2)"
# Valid country codes. See https://www.iso.org/iso-3166-country-codes.html.
ALLOWEDCOUNTRIES="AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP) KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK) ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW"
#
# Actions.
#
Expand All @@ -41,9 +44,19 @@ PASSWORDPROTECTED="$(grep '^passwordprotected\b' $FILE | cut -d= -f2)"
# New password is now valid; set it in config file.
sed -i "/^wpa_passphrase=/c\wpa_passphrase=$NEWPASSWORD" "$CONFIGFILE"
#
# Country setting.
# Validate new country. Replace it with 'CH' if invalid.
[[ $ALLOWEDCOUNTRIES =~ $NEWCOUNTRY ]] || NEWCOUNTRY="CH"
# New channel is now valid; set it in config file.
sed -i "/^country_code=/c\country_code=$NEWCOUNTRY" "$CONFIGFILE"
#
# Channel setting.
# Validate new channel. Replace it with 11 if invalid.
[[ $NEWCHANNEL =~ ^[1-9]|1[0-3]$ ]] || NEWCHANNEL="11"
# Channel 12 and 13 aren't valid in Canada and US.
if [[ $NEWCOUNTRY =~ ^(CA|US)$ ]] && [[ $NEWCHANNEL =~ ^1[23]$ ]]
NEWCHANNEL="11"
fi
# New channel is now valid; set it in config file.
sed -i "/^channel=/c\channel=$NEWCHANNEL" "$CONFIGFILE"
#
Expand Down
12 changes: 12 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ public function definition() {

$mform = $this->_form;

// SSID setting.
$mform->addElement('text', 'wifissid', get_string('wifissid', 'tool_moodlebox'));
$mform->addRule('wifissid', get_string('required'), 'required', null, 'client');
$mform->setType('wifissid', PARAM_RAW_TRIMMED);
$mform->setDefault('wifissid', $currentwifissid);

// Channel setting.
if ($currentwificountry == 'US' or $currentwificountry == 'CA') {
$wifichannelrange = range(1, 11);
} else {
Expand All @@ -251,11 +253,20 @@ public function definition() {
$mform->setType('wifichannel', PARAM_INT);
$mform->setDefault('wifichannel', $currentwifichannel);

// Regulatory country setting.
$mform->addElement('select', 'wificountry', get_string('wificountry', 'tool_moodlebox'),
get_string_manager()->get_list_of_countries(true));
$mform->addRule('wificountry', get_string('required'), 'required', null, 'client');
$mform->setType('wificountry', PARAM_RAW);
$mform->setDefault('wificountry', $currentwificountry);

// Password protection setting.
$mform->addElement('checkbox', 'wifipasswordon', get_string('wifipasswordon', 'tool_moodlebox'),
' ' . get_string('wifipasswordonhelp', 'tool_moodlebox'));
$mform->setDefault('wifipasswordon', ($currentwifipassword == null) ? 0 : 1);
$mform->setType('wifipasswordon', PARAM_INT);

// Password setting.
$mform->addElement('text', 'wifipassword', get_string('wifipassword', 'tool_moodlebox'));
$mform->disabledIf('wifipassword', 'wifipasswordon');
$mform->setType('wifipassword', PARAM_RAW_TRIMMED);
Expand Down Expand Up @@ -429,6 +440,7 @@ public function definition() {
$data->wifissid = implode(unpack("H*", $data->wifissid));
file_put_contents($wifipasswordtriggerfilename,
"channel=" . $data->wifichannel . "\n" .
"country=" . $data->wificountry . "\n" .
"password=" . $data->wifipassword . "\n" .
"ssid=" . $data->wifissid . "\n" .
"passwordprotected=" . $data->wifipasswordon . "\n");
Expand Down

0 comments on commit 5f20daf

Please sign in to comment.