Skip to content

Instantly share code, notes, and snippets.

@neaxi
Last active October 8, 2020 03:23
Show Gist options
  • Save neaxi/cbd68f9de00efb9ce1c5145d875de163 to your computer and use it in GitHub Desktop.
Save neaxi/cbd68f9de00efb9ce1c5145d875de163 to your computer and use it in GitHub Desktop.

Related to Raspberry Pi - Auto WiFi Hotspot Switch

Issue 01 - \r\n newlines and awk ORS

I've noticed the issue when examining /var/log/syslog and also the uap0 was bouncing back and forth from the ifconfig.

The issue

/usr/bin/autohotspot script checks for existing SSIDs in /etc/wpa_supplicant/wpa_supplicant.conf and starts the hotspot only if they are not connected.
Currently configured SSIDs are parsed out of the config using following line:
wpassid=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' ORS=',' | sed 's/\"/''/g' | sed 's/,$//')

The issue was awk -F'ssid=' '{ print $2 }' ORS=',' was comming up empty. So although the wlan0 was connected to the AP it still attempted to create a hotspot.

Solution

Remove carraige return char (^M, \r, ASCII 13, 0x0D) prior ORS concatenation.
awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' | sed 's/\r//g'| awk 'BEGIN{ORS=","} {print}' | sed 's/\"/''/g' | sed 's/,$//'

How the extra \r got there

When Raspberry's Wi-Fi is being set up in a headless fashion from a Windows computer the wpa_supplicant.conf is written with Windows newlines (\r\n) resulting in the configuration file with extra \r (Windows styled newlines). More info about the newlines is avaialble on Wiki.

Issue 02 - grep with special chars

Following code checks for SSID in output from the wlan0 scan:
if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1

My SSID contains -~ which grep interprets as an argument resulting in:

sudo iw dev wlan0 scan | grep "-~["
grep: invalid option -- '~'

Solution

if (echo "$ssidreply" | grep -F -- "$ssid") >/dev/null 2>&1
because:
-F, --fixed-strings: Interpret PATTERNS as fixed strings, not regular expressions.
-- specifies end of command options for many commands/shell built-ins, after which the remaining arguments are treated as positional arguments.

Other errors encountered along the way

Other errors I've encountered and haven't found a solution yet. They're not 100% related to the autohotspot script itself, but may be just manifested during its start due to some other system configuration. They're not necessarily a show stopper either and may appear from time to time but won't affect overall functionality.

Oct  7 12:29:01 hostname kernel: [  203.019215] ieee80211 phy0: brcmf_vif_set_mgmt_ie: vndr ie set error : -52
Oct  7 12:29:01 hostname kernel: [  203.019232] ieee80211 phy0: brcmf_cfg80211_scan: scan error (-52)
Oct  7 12:29:09 hostname dnsmasq[1460]: Too few arguments.
Oct  7 12:29:09 hostname dnsmasq[1251]: exiting on receipt of SIGTERM

 brcmfmac: brcmf_cfg80211_connect: failed to enable fw supplicant

Anybody wanted a Backtrace?

Active/Open issue - raspberrypi/linux#3318

Oct  7 21:34:50 hostname kernel: [ 1493.074495] ieee80211 phy0: brcmf_update_bss_info: wl dtim_assoc failed (-52)
Oct  7 21:34:50 hostname kernel: [ 1493.074623] ------------[ cut here ]------------
Oct  7 21:34:50 hostname kernel: [ 1493.074941] WARNING: CPU: 0 PID: 1565 at net/wireless/sme.c:756 __cfg80211_connect_result+0x43c/0x4e8 [cfg80211]
Oct  7 21:34:50 hostname kernel: [ 1493.074952] Modules linked in: xt_state xt_conntrack nft_chain_nat xt_MASQUERADE nf_nat nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 cmac bnep hci_uart btbcm bluetooth ecdh_generic ecc nft_counter xt_DSCP xt_tcpudp nft_compat nf_tables nfnetlink 8021q garp stp llc brcmfmac sg brcmutil uas sha256_generic libsha256 cfg80211 raspberrypi_hwmon rfkill bcm2835_codec(C) bcm2835_v4l2(C) bcm2835_isp(C) v4l2_mem2mem bcm2835_mmal_vchiq(C) videobuf2_vmalloc videobuf2_dma_contig videobuf2_memops snd_bcm2835(C) videobuf2_v4l2 snd_pcm videobuf2_common i2c_bcm2835 snd_timer videodev snd mc vc_sm_cma(C) uio_pdrv_genirq uio fixed i2c_dev ip_tables x_tables ipv6
Oct  7 21:34:50 hostname kernel: [ 1493.075216] CPU: 0 PID: 1565 Comm: kworker/u8:0 Tainted: G         C        5.4.69-v7+ #1344
Oct  7 21:34:50 hostname kernel: [ 1493.075221] Hardware name: BCM2835
Oct  7 21:34:50 hostname kernel: [ 1493.075476] Workqueue: cfg80211 cfg80211_event_work [cfg80211]
Oct  7 21:34:50 hostname kernel: [ 1493.075484] Backtrace:
Oct  7 21:34:50 hostname kernel: [ 1493.075507] [<8010d514>] (dump_backtrace) from [<8010d82c>] (show_stack+0x20/0x24)
Oct  7 21:34:50 hostname kernel: [ 1493.075518]  r6:b315e000 r5:00000000 r4:80e97b48 r3:33554f4f
Oct  7 21:34:50 hostname kernel: [ 1493.075533] [<8010d80c>] (show_stack) from [<809090c4>] (dump_stack+0xe0/0x124)
Oct  7 21:34:50 hostname kernel: [ 1493.075549] [<80908fe4>] (dump_stack) from [<80120168>] (__warn+0xec/0x104)
Oct  7 21:34:50 hostname kernel: [ 1493.075563]  r8:000002f4 r7:00000009 r6:7f2c0168 r5:00000000 r4:00000000 r3:33554f4f
Oct  7 21:34:50 hostname kernel: [ 1493.075577] [<8012007c>] (__warn) from [<80120238>] (warn_slowpath_fmt+0xb8/0xc0)
Oct  7 21:34:50 hostname kernel: [ 1493.075590]  r9:7f2c0168 r8:000002f4 r7:7f294d08 r6:00000009 r5:00000000 r4:80e04f88
Oct  7 21:34:50 hostname kernel: [ 1493.075829] [<80120184>] (warn_slowpath_fmt) from [<7f294d08>] (__cfg80211_connect_result+0x43c/0x4e8 [cfg80211])
Oct  7 21:34:50 hostname kernel: [ 1493.075842]  r9:00000122 r8:b315fe54 r7:80e04f88 r6:00000000 r5:b0862a0c r4:af660004
Oct  7 21:34:50 hostname kernel: [ 1493.076304] [<7f2948cc>] (__cfg80211_connect_result [cfg80211]) from [<7f260b68>] (cfg80211_process_wdev_events+0x104/0x160 [cfg80211])
Oct  7 21:34:50 hostname kernel: [ 1493.076317]  r8:af660090 r7:af660028 r6:af660098 r5:af660004 r4:b0862a00
Oct  7 21:34:50 hostname kernel: [ 1493.076783] [<7f260a64>] (cfg80211_process_wdev_events [cfg80211]) from [<7f260c04>] (cfg80211_process_rdev_events+0x40/0x98 [cfg80211])
Oct  7 21:34:50 hostname kernel: [ 1493.076796]  r10:00000000 r9:00000100 r8:00000000 r7:b2ad1100 r6:b3044400 r5:ae9f4460
Oct  7 21:34:50 hostname kernel: [ 1493.076802]  r4:af660004
Oct  7 21:34:50 hostname kernel: [ 1493.077263] [<7f260bc4>] (cfg80211_process_rdev_events [cfg80211]) from [<7f25a1b8>] (cfg80211_event_work+0x24/0x2c [cfg80211])
Oct  7 21:34:50 hostname kernel: [ 1493.077270]  r5:af533080 r4:ae9f40e4
Oct  7 21:34:50 hostname kernel: [ 1493.077512] [<7f25a194>] (cfg80211_event_work [cfg80211]) from [<8013c548>] (process_one_work+0x17c/0x4b4)
Oct  7 21:34:50 hostname kernel: [ 1493.077520]  r4:ae9f40e4 r3:7f25a194
Oct  7 21:34:50 hostname kernel: [ 1493.077535] [<8013c3cc>] (process_one_work) from [<8013cdb0>] (worker_thread+0x54/0x5b4)
Oct  7 21:34:50 hostname kernel: [ 1493.077548]  r10:af533080 r9:b315e038 r8:80e03d00 r7:b3044418 r6:00000088 r5:af533094
Oct  7 21:34:50 hostname kernel: [ 1493.077554]  r4:b3044400
Oct  7 21:34:50 hostname kernel: [ 1493.077568] [<8013cd5c>] (worker_thread) from [<801431c0>] (kthread+0x13c/0x168)
Oct  7 21:34:50 hostname kernel: [ 1493.077582]  r10:a92d30dc r9:b0a53e74 r8:8013cd5c r7:af533080 r6:00000000 r5:a44fbd40
Oct  7 21:34:50 hostname kernel: [ 1493.077587]  r4:a92d30c0
Oct  7 21:34:50 hostname kernel: [ 1493.077600] [<80143084>] (kthread) from [<801010ac>] (ret_from_fork+0x14/0x28)
Oct  7 21:34:50 hostname kernel: [ 1493.077607] Exception stack(0xb315ffb0 to 0xb315fff8)
Oct  7 21:34:50 hostname kernel: [ 1493.077617] ffa0:                                     00000000 00000000 00000000 00000000
Oct  7 21:34:50 hostname kernel: [ 1493.077629] ffc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
Oct  7 21:34:50 hostname kernel: [ 1493.077639] ffe0: 00000000 00000000 00000000 00000000 00000013 00000000
Oct  7 21:34:50 hostname kernel: [ 1493.077652]  r10:00000000 r9:00000000 r8:00000000 r7:00000000 r6:00000000 r5:80143084
Oct  7 21:34:50 hostname kernel: [ 1493.077659]  r4:a44fbd40 r3:80104648
Oct  7 21:34:50 hostname kernel: [ 1493.077667] ---[ end trace fbdc9d8762cd7987 ]---

iface validation failed: err=-16

The error went away once I've installed Autohostpot via provided script.

Oct  7 07:57:32 hostname kernel: [   43.449838] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:32 hostname avahi-daemon[389]: Interface uap0.IPv4 no longer relevant for mDNS.
Oct  7 07:57:32 hostname avahi-daemon[389]: Leaving mDNS multicast group on interface uap0.IPv4 with address 192.168.69.1.
Oct  7 07:57:32 hostname avahi-daemon[389]: Withdrawing address record for 192.168.69.1 on uap0.
Oct  7 07:57:32 hostname kernel: [   43.967518] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:32 hostname kernel: [   44.068743] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.169689] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.270538] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.371530] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.472545] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.573604] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.674692] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.775710] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname kernel: [   44.876716] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:33 hostname avahi-daemon[389]: Joining mDNS multicast group on interface uap0.IPv4 with address 192.168.69.1.
Oct  7 07:57:33 hostname avahi-daemon[389]: New relevant interface uap0.IPv4 for mDNS.
Oct  7 07:57:33 hostname avahi-daemon[389]: Registering new address record for 192.168.69.1 on uap0.IPv4.
Oct  7 07:57:36 hostname systemd[1]: systemd-hostnamed.service: Succeeded.
Oct  7 07:57:39 hostname kernel: [   50.714787] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname avahi-daemon[389]: Interface uap0.IPv4 no longer relevant for mDNS.
Oct  7 07:57:40 hostname avahi-daemon[389]: Leaving mDNS multicast group on interface uap0.IPv4 with address 192.168.69.1.
Oct  7 07:57:40 hostname avahi-daemon[389]: Withdrawing address record for 192.168.69.1 on uap0.
Oct  7 07:57:40 hostname kernel: [   51.237551] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   51.338683] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   51.439706] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   51.540720] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   51.641764] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   51.742792] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   51.843802] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   51.944810] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:40 hostname kernel: [   52.045897] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:41 hostname kernel: [   52.146948] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:41 hostname avahi-daemon[389]: Joining mDNS multicast group on interface uap0.IPv4 with address 192.168.69.1.
Oct  7 07:57:41 hostname avahi-daemon[389]: New relevant interface uap0.IPv4 for mDNS.
Oct  7 07:57:41 hostname avahi-daemon[389]: Registering new address record for 192.168.69.1 on uap0.IPv4.
Oct  7 07:57:51 hostname kernel: [   62.994379] ieee80211 phy0: brcmf_cfg80211_change_iface: iface validation failed: err=-16
Oct  7 07:57:52 hostname avahi-daemon[389]: Interface uap0.IPv4 no longer relevant for mDNS.
Oct  7 07:57:52 hostname avahi-daemon[389]: Leaving mDNS multicast group on interface uap0.IPv4 with address 192.168.69.1.
Oct  7 07:57:52 hostname avahi-daemon[389]: Withdrawing address record for 192.168.69.1 on uap0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment