freedombone-utils-wifi 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Wifi functions
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. WIFI_CHANNEL=2
  30. WIFI_INTERFACE=wlan0
  31. WIFI_TYPE='wpa2-psk'
  32. WIFI_SSID=
  33. WIFI_PASSPHRASE=
  34. WIFI_HOTSPOT='no'
  35. WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
  36. # repo for atheros AR9271 wifi driver
  37. ATHEROS_WIFI_REPO="https://github.com/qca/open-ath9k-htc-firmware.git"
  38. function wifi_is_running {
  39. wifi_state=$(wpa_cli status)
  40. if [[ "$wifi_state" == *"COMPLETED"* ]]; then
  41. echo "1"
  42. else
  43. echo "0"
  44. fi
  45. }
  46. function wifi_static_network_interface {
  47. NETWORK_IS_STATIC=0
  48. read_config_param "NETWORK_IS_STATIC"
  49. if [ ${NETWORK_IS_STATIC} -eq 0 ]; then
  50. echo '#this line must always be here' >> /etc/network/interfaces
  51. echo 'iface default inet dhcp' >> /etc/network/interfaces
  52. else
  53. read_config_param "LOCAL_NETWORK_STATIC_IP_ADDRESS"
  54. read_config_param "ROUTER_IP_ADDRESS"
  55. echo '#static address' >> /etc/network/interfaces
  56. echo 'iface default inet static' >> /etc/network/interfaces
  57. echo " address ${LOCAL_NETWORK_STATIC_IP_ADDRESS}" >> /etc/network/interfaces
  58. echo ' netmask 255.255.255.0' >> /etc/network/interfaces
  59. echo " gateway ${ROUTER_IP_ADDRESS}" >> /etc/network/interfaces
  60. fi
  61. }
  62. function setup_wifi_atheros {
  63. if [[ $(running_as_root) == "0" ]]; then
  64. return
  65. fi
  66. atheros_drivers_file=drivers/ath9k_htc_driver_bbb.tar.gz
  67. if [ ! -f $atheros_drivers_file ]; then
  68. if [ ! -f ~/freedombone/$atheros_drivers_file ]; then
  69. return
  70. else
  71. atheros_drivers_file=~/freedombone/$atheros_drivers_file
  72. fi
  73. else
  74. atheros_drivers_file=$(pwd)/$atheros_drivers_file
  75. fi
  76. if [ ! -d /lib/firmware ]; then
  77. mkdir /lib/firmware
  78. if [ ! -d /lib/firmware ]; then
  79. return
  80. fi
  81. fi
  82. if [ -f /lib/firmware/htc_9271.fw ]; then
  83. return
  84. fi
  85. curr_dir=$(pwd)
  86. cd /lib/firmware
  87. tar -xzvf $atheros_drivers_file
  88. reset_usb_devices
  89. cd $curr_dir
  90. update_wifi_adaptors
  91. if [ $IFACE ]; then
  92. wpa_action ${IFACE} stop
  93. wpa_cli -i ${IFACE} terminate
  94. ifconfig $IFACE up
  95. fi
  96. }
  97. function setup_wifi {
  98. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  99. return
  100. fi
  101. if [ ! $WIFI_SSID ]; then
  102. return
  103. fi
  104. if [ ${#WIFI_SSID} -lt 2 ]; then
  105. return
  106. fi
  107. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  108. return
  109. fi
  110. HOTSPOT='no'
  111. if [[ $WIFI_HOTSPOT != 'no' ]]; then
  112. HOTSPOT='yes'
  113. fi
  114. if [ -f $WIFI_NETWORKS_FILE ]; then
  115. ${PROJECT_NAME}-wifi --networks $WIFI_NETWORKS_FILE
  116. mark_completed $FUNCNAME
  117. return
  118. fi
  119. if [[ $WIFI_TYPE != 'none' ]]; then
  120. if [ ! $WIFI_PASSPHRASE ]; then
  121. echo $'No wifi passphrase was given'
  122. return
  123. fi
  124. if [ ${#WIFI_PASSPHRASE} -lt 2 ]; then
  125. echo $'Wifi passphrase was too short'
  126. return
  127. fi
  128. ${PROJECT_NAME}-wifi -s $WIFI_SSID -t $WIFI_TYPE -p $WIFI_PASSPHRASE --hotspot $HOTSPOT --networks $WIFI_NETWORKS_FILE
  129. else
  130. ${PROJECT_NAME}-wifi -s $WIFI_SSID -t $WIFI_TYPE --hotspot $HOTSPOT --networks $WIFI_NETWORKS_FILE
  131. fi
  132. mark_completed $FUNCNAME
  133. }
  134. # ath9k_htc driver
  135. function install_atheros_wifi {
  136. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  137. return
  138. fi
  139. if [ $INSTALLING_ON_BBB != "yes" ]; then
  140. return
  141. fi
  142. if [[ $ENABLE_BATMAN != "yes" ]]; then
  143. return
  144. fi
  145. if [ -d $INSTALL_DIR/open-ath9k-htc-firmware ]; then
  146. return
  147. fi
  148. # have drivers already been installed ?
  149. if [ -f /lib/firmware/htc_9271.fw ]; then
  150. return
  151. fi
  152. apt-get -yq install build-essential cmake git m4 texinfo
  153. if [ ! -d $INSTALL_DIR ]; then
  154. mkdir -p $INSTALL_DIR
  155. fi
  156. cd $INSTALL_DIR
  157. if [ ! -d $INSTALL_DIR/open-ath9k-htc-firmware ]; then
  158. function_check git_clone
  159. git_clone $ATHEROS_WIFI_REPO $INSTALL_DIR/open-ath9k-htc-firmware
  160. if [ ! "$?" = "0" ]; then
  161. rm -rf $INSTALL_DIR/open-ath9k-htc-firmware
  162. exit 74283
  163. fi
  164. fi
  165. cd $INSTALL_DIR/open-ath9k-htc-firmware
  166. git checkout 1.4.0
  167. make toolchain
  168. if [ ! "$?" = "0" ]; then
  169. rm -rf $INSTALL_DIR/open-ath9k-htc-firmware
  170. exit 24820
  171. fi
  172. make firmware
  173. if [ ! "$?" = "0" ]; then
  174. rm -rf $INSTALL_DIR/open-ath9k-htc-firmware
  175. exit 63412
  176. fi
  177. cp target_firmware/*.fw /lib/firmware/
  178. if [ ! "$?" = "0" ]; then
  179. exit 74681
  180. fi
  181. mark_completed $FUNCNAME
  182. }
  183. function update_wifi_adaptors {
  184. IFACE=
  185. IFACE_SECONDARY=
  186. for i in $(seq 10 -1 0); do
  187. if grep -q "wlan${i}" /proc/net/dev; then
  188. if [ ! $IFACE ]; then
  189. IFACE="wlan${i}"
  190. else
  191. IFACE_SECONDARY="wlan${i}"
  192. return
  193. fi
  194. fi
  195. done
  196. }
  197. function wifi_get_psk {
  198. ssid=$1
  199. passphrase=$2
  200. psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
  201. echo $psk
  202. }
  203. function hotspot_off {
  204. if [ ! -f /etc/hostapd/hostapd.conf ]; then
  205. return
  206. fi
  207. systemctl stop hostapd
  208. rm /etc/hostapd/hostapd.conf
  209. if [ -f /etc/network/interfaces_original ]; then
  210. cp /etc/network/interfaces_original /etc/network/interfaces
  211. else
  212. echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
  213. echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
  214. echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
  215. fi
  216. wpa_action ${WIFI_INTERFACE} stop
  217. wpa_cli -i ${WIFI_INTERFACE} terminate
  218. systemctl restart network-manager
  219. }
  220. function hotspot_on {
  221. if [ ! -f /etc/default/hostapd ]; then
  222. echo $'/etc/default/hostapd was not found'
  223. exit 67241
  224. fi
  225. if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
  226. echo $'Wifi hotspot passphrase is too short'
  227. exit 25719
  228. fi
  229. sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
  230. echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf
  231. echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf
  232. echo '' >> /etc/hostapd/hostapd.conf
  233. echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf
  234. echo 'bridge=br0' >> /etc/hostapd/hostapd.conf
  235. echo '' >> /etc/hostapd/hostapd.conf
  236. echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
  237. echo "country_code=UK" >> /etc/hostapd/hostapd.conf
  238. echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf
  239. echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
  240. echo 'channel=6' >> /etc/hostapd/hostapd.conf
  241. echo 'wpa=2' >> /etc/hostapd/hostapd.conf
  242. echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf
  243. echo '' >> /etc/hostapd/hostapd.conf
  244. echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf
  245. echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
  246. echo '' >> /etc/hostapd/hostapd.conf
  247. echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf
  248. echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf
  249. echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf
  250. echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
  251. echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
  252. echo '' >> /etc/hostapd/hostapd.conf
  253. echo '## Shared Key Authentication ##'
  254. echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
  255. echo '' >> /etc/hostapd/hostapd.conf
  256. echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
  257. echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
  258. if [ ! -f /etc/network/interfaces_original ]; then
  259. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  260. cp /etc/network/interfaces /etc/network/interfaces_original
  261. fi
  262. fi
  263. echo '# wifi enabled' > /etc/network/interfaces
  264. echo 'auto lo br0' >> /etc/network/interfaces
  265. echo 'iface lo inet loopback' >> /etc/network/interfaces
  266. echo '' >> /etc/network/interfaces
  267. echo "# wireless $WIFI_INTERFACE" >> /etc/network/interfaces
  268. echo "allow-hotplug $WIFI_INTERFACE" >> /etc/network/interfaces
  269. echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces
  270. echo '' >> /etc/network/interfaces
  271. echo '# eth0 connected to the ISP router' >> /etc/network/interfaces
  272. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  273. echo 'iface eth0 inet manual' >> /etc/network/interfaces
  274. echo '' >> /etc/network/interfaces
  275. echo '# Setup bridge' >> /etc/network/interfaces
  276. echo 'iface br0 inet static' >> /etc/network/interfaces
  277. echo " bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces
  278. systemctl restart network-manager
  279. ifconfig $WIFI_INTERFACE up
  280. systemctl restart hostapd
  281. }
  282. function wifi_store_original_network_settings {
  283. if [ ! -f /etc/network/interfaces_original ]; then
  284. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  285. cp /etc/network/interfaces /etc/network/interfaces_original
  286. fi
  287. fi
  288. }
  289. function wifi_original_network_settings {
  290. remove_config_param "NETWORK_IS_STATIC"
  291. if [ -f /etc/network/interfaces_original ]; then
  292. cp /etc/network/interfaces_original /etc/network/interfaces
  293. fi
  294. }
  295. function wifi_wpa2_psk {
  296. ssid=$1
  297. passphrase=$2
  298. wifi_store_original_network_settings
  299. echo '# wifi enabled' > /etc/network/interfaces
  300. echo 'auto lo' >> /etc/network/interfaces
  301. echo 'iface lo inet loopback' >> /etc/network/interfaces
  302. echo '' >> /etc/network/interfaces
  303. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  304. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
  305. echo '' >> /etc/network/interfaces
  306. echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
  307. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
  308. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
  309. echo '' >> /etc/network/interfaces
  310. wifi_static_network_interface
  311. wpa_passphrase "$ssid" "$passphrase" > $WIFI_CONFIG
  312. systemctl restart network-manager
  313. ifconfig ${WIFI_INTERFACE} up
  314. }
  315. function wifi_none {
  316. ssid=$1
  317. wifi_store_original_network_settings
  318. echo '# wifi enabled' > /etc/network/interfaces
  319. echo 'auto lo' >> /etc/network/interfaces
  320. echo 'iface lo inet loopback' >> /etc/network/interfaces
  321. echo '' >> /etc/network/interfaces
  322. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  323. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
  324. echo '' >> /etc/network/interfaces
  325. echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
  326. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
  327. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
  328. echo '' >> /etc/network/interfaces
  329. wifi_static_network_interface
  330. echo 'update_config=1' > $WIFI_CONFIG
  331. echo 'eapol_version=1' >> $WIFI_CONFIG
  332. echo '' >> $WIFI_CONFIG
  333. echo 'network={' >> $WIFI_CONFIG
  334. if [[ "${ssid}" != $'any' && "${ssid}" != $'all' && "${ssid}" != $'open' ]]; then
  335. echo " ssid=\"${ssid}\"" >> $WIFI_CONFIG
  336. fi
  337. echo ' key_mgmt=NONE' >> $WIFI_CONFIG
  338. echo '}' >> $WIFI_CONFIG
  339. systemctl restart network-manager
  340. ifconfig ${WIFI_INTERFACE} up
  341. }
  342. function networks_from_file {
  343. if [ ! -f $WIFI_NETWORKS_FILE ]; then
  344. exit 4
  345. fi
  346. if [[ $(config_param_exists "WIFI_INTERFACE") == "0" ]]; then
  347. exit 5
  348. fi
  349. read_config_param "WIFI_INTERFACE"
  350. wifi_store_original_network_settings
  351. echo '# wifi enabled' > /etc/network/interfaces
  352. echo 'auto lo' >> /etc/network/interfaces
  353. echo 'iface lo inet loopback' >> /etc/network/interfaces
  354. echo '' >> /etc/network/interfaces
  355. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  356. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
  357. echo '' >> /etc/network/interfaces
  358. echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
  359. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
  360. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
  361. echo '' >> /etc/network/interfaces
  362. wifi_static_network_interface
  363. # remove wpa_supplicant.conf if it exists
  364. if [ -f $WIFI_CONFIG ]; then
  365. rm -f $WIFI_CONFIG
  366. fi
  367. echo 'update_config=1' > $WIFI_CONFIG
  368. echo 'eapol_version=1' >> $WIFI_CONFIG
  369. echo '' >> $WIFI_CONFIG
  370. ctr=0
  371. while read -r line
  372. do
  373. if [ ${#line} -gt 1 ]; then
  374. if [[ "$line" != '#'* ]]; then
  375. if [ $ctr -eq 0 ]; then
  376. WIFI_SSID="$line"
  377. fi
  378. if [ $ctr -eq 1 ]; then
  379. WIFI_TYPE="$line"
  380. if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
  381. echo 'network={' >> $WIFI_CONFIG
  382. if [[ "${WIFI_SSID}" != $'any' && "${WIFI_SSID}" != $'all' && "${WIFI_SSID}" != $'open' ]]; then
  383. echo " ssid=\"${WIFI_SSID}\"" >> $WIFI_CONFIG
  384. fi
  385. echo ' key_mgmt=NONE' >> $WIFI_CONFIG
  386. echo '}' >> $WIFI_CONFIG
  387. ctr=0
  388. continue
  389. fi
  390. fi
  391. if [ $ctr -eq 2 ]; then
  392. WIFI_PASSPHRASE="$line"
  393. wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> $WIFI_CONFIG
  394. ctr=0
  395. continue
  396. fi
  397. ctr=$((ctr + 1))
  398. fi
  399. fi
  400. done < $WIFI_NETWORKS_FILE
  401. chattr -i /etc/resolv.conf
  402. systemctl restart network-manager
  403. ifconfig ${WIFI_INTERFACE} up
  404. }
  405. function wifi_networks_file_header {
  406. echo $'# Add wifi networks as follows:' > $WIFI_NETWORKS_FILE
  407. echo '#' >> $WIFI_NETWORKS_FILE
  408. echo $'# MySSID' >> $WIFI_NETWORKS_FILE
  409. echo $'# wpa2-psk' >> $WIFI_NETWORKS_FILE
  410. echo $'# myWifiPassphrase' >> $WIFI_NETWORKS_FILE
  411. echo '#' >> $WIFI_NETWORKS_FILE
  412. echo $'# AnotherSSID' >> $WIFI_NETWORKS_FILE
  413. echo $'# none' >> $WIFI_NETWORKS_FILE
  414. echo '#' >> $WIFI_NETWORKS_FILE
  415. }
  416. function create_networks_interactive {
  417. remove_config_param "WIFI_INTERFACE"
  418. update_wifi_adaptors
  419. if [ ! $IFACE ]; then
  420. # Don't try to configure wifi if there are no adaptors
  421. return
  422. fi
  423. if [ -f $WIFI_NETWORKS_FILE ]; then
  424. rm $WIFI_NETWORKS_FILE
  425. fi
  426. # By default connect to any open wifi
  427. WIFI_SSID=''
  428. WIFI_TYPE=$'open'
  429. wifi_ctr=0
  430. wifi_networks_done=
  431. while [ ! $wifi_networks_done ]
  432. do
  433. data=$(tempfile 2>/dev/null)
  434. trap "rm -f $data" 0 1 2 5 15
  435. dialog --backtitle $"Freedombone Configuration" \
  436. --title $"Wifi Settings ${wifi_ctr}" \
  437. --form $"\nTo use this system via wifi (eg. USB dongle) enter the details below, otherwise just select Ok:" 13 65 4 \
  438. $"SSID (can be 'any'):" 1 1 "$WIFI_SSID" 1 24 30 30 \
  439. $"Type (open/wpa2-psk):" 2 1 "$WIFI_TYPE" 2 24 10 10 \
  440. $"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 24 50 50 \
  441. 2> $data
  442. sel=$?
  443. case $sel in
  444. 1) return;;
  445. 255) return;;
  446. esac
  447. WIFI_SSID=$(cat $data | sed -n 1p)
  448. WIFI_TYPE=$(cat $data | sed -n 2p)
  449. WIFI_PASSPHRASE=$(cat $data | sed -n 3p)
  450. # if these fields are empty then there are no more wifi networks
  451. if [ ${#WIFI_SSID} -lt 2 ]; then
  452. wifi_networks_done='yes'
  453. continue
  454. fi
  455. if [ ${#WIFI_TYPE} -lt 2 ]; then
  456. wifi_networks_done='yes'
  457. continue
  458. fi
  459. if [ ! -f $WIFI_NETWORKS_FILE ]; then
  460. wifi_networks_file_header
  461. fi
  462. # update the wifi networks file
  463. echo '' >> $WIFI_NETWORKS_FILE
  464. echo "$WIFI_SSID" >> $WIFI_NETWORKS_FILE
  465. echo "$WIFI_TYPE" >> $WIFI_NETWORKS_FILE
  466. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  467. echo "$WIFI_PASSPHRASE" >> $WIFI_NETWORKS_FILE
  468. fi
  469. if [ ${#WIFI_SSID} -gt 1 ]; then
  470. if [ ${#WIFI_TYPE} -gt 1 ]; then
  471. if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
  472. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  473. return
  474. else
  475. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  476. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  477. return
  478. fi
  479. fi
  480. fi
  481. fi
  482. # clear values
  483. WIFI_SSID=
  484. WIFI_PASSPHRASE=
  485. wifi_ctr=$((wifi_ctr + 1))
  486. done
  487. }
  488. function disable_wifi {
  489. if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
  490. hotspot_off
  491. echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
  492. echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
  493. echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
  494. remove_config_param "WIFI_INTERFACE"
  495. wpa_action ${WIFI_INTERFACE} stop
  496. wpa_cli -i ${WIFI_INTERFACE} terminate
  497. systemctl restart network-manager
  498. else
  499. networks_from_file
  500. fi
  501. }
  502. function count_wlan {
  503. # counts the number of wlan devices
  504. ctr=0
  505. for i in $(seq 0 1 10); do
  506. if grep -q "wlan${i}" /proc/net/dev; then
  507. ctr=$((ctr + 1))
  508. fi
  509. done
  510. echo $ctr
  511. }
  512. function wifi_exists {
  513. if grep -q "wlan" /proc/net/dev; then
  514. echo "1"
  515. else
  516. echo "0"
  517. fi
  518. }
  519. function remove_wifi_startup_script {
  520. remove_config_param "WIFI_INTERFACE"
  521. systemd_file=/etc/systemd/system/wifistart.service
  522. if [ -f $systemd_file ]; then
  523. systemctl stop wifistart
  524. systemctl disable wifistart
  525. rm $systemd_file
  526. fi
  527. if [ -f $HOME/${PROJECT_NAME}-wifi.cfg ]; then
  528. rm $HOME/${PROJECT_NAME}-wifi.cfg
  529. fi
  530. }
  531. function create_wifi_startup_script {
  532. systemd_file=/etc/systemd/system/wifistart.service
  533. echo '[Unit]' > $systemd_file
  534. echo 'Description=WifiStartup (Start wifi networking)' >> $systemd_file
  535. echo 'After=syslog.target' >> $systemd_file
  536. echo 'After=network.target' >> $systemd_file
  537. echo 'After=remote-fs.target' >> $systemd_file
  538. echo '' >> $systemd_file
  539. echo '[Service]' >> $systemd_file
  540. echo 'Type=simple' >> $systemd_file
  541. echo 'User=root' >> $systemd_file
  542. echo 'Group=root' >> $systemd_file
  543. echo 'WorkingDirectory=/root' >> $systemd_file
  544. echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-wifi --wait 5 2> /dev/null" >> $systemd_file
  545. echo '' >> $systemd_file
  546. echo '[Install]' >> $systemd_file
  547. echo 'WantedBy=multi-user.target' >> $systemd_file
  548. systemctl enable wifistart
  549. systemctl daemon-reload
  550. }
  551. function remove_wifi_startup_script {
  552. systemd_file=/etc/systemd/system/wifistart.service
  553. if [ -f $systemd_file ]; then
  554. systemctl disable wifistart
  555. systemctl daemon-reload
  556. rm $systemd_file
  557. fi
  558. }
  559. # NOTE: deliberately no exit 0