wz_user.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. #!/bin/sh
  2. LOG_NAME=/opt/wz_mini/log/wz_user
  3. if [[ -e $LOG_NAME.log || -L $LOG_NAME.log ]] ; then
  4. i=0
  5. while [[ -e $LOG_NAME.log.$i || -L $LOG_NAME.log.$i ]] ; do
  6. let i++
  7. done
  8. mv $LOG_NAME.log $LOG_NAME.log.$i
  9. LOG_NAME=$LOG_NAME
  10. fi
  11. touch -- "$LOG_NAME".log
  12. exec 1> $LOG_NAME.log 2>&1
  13. set -x
  14. echo "welcome to wz_user.sh"
  15. echo "PID $$"
  16. if [[ -e /tmp/dbgflag ]];then
  17. echo "debug mode, disabled"
  18. exit 0
  19. fi
  20. export WZMINI_CFG=/opt/wz_mini/wz_mini.conf
  21. [ -f $WZMINI_CFG ] && source $WZMINI_CFG
  22. hostname_set() {
  23. echo "set hostname"
  24. hostname $CUSTOM_HOSTNAME
  25. }
  26. first_run_check() {
  27. if [ -e /opt/wz_mini/tmp/.wz_user_firstrun ]; then
  28. echo "wz_user.sh already run once, exit."
  29. exit 0
  30. fi
  31. }
  32. store_mac() {
  33. echo "store original mac"
  34. cat /sys/class/net/wlan0/address | tr '[:lower:]' '[:upper:]' > /opt/wz_mini/tmp/wlan0_mac
  35. }
  36. wait_wlan() {
  37. ##Check if the driver has been loaded for the onboard wlan0, store the MAC.
  38. while true
  39. do
  40. if ifconfig wlan0 | grep "inet addr"; then
  41. store_mac
  42. break
  43. elif ifconfig wlan0 | grep "inet addr" && [[ "$ENABLE_USB_ETH" == "true" || "$ENABLE_USB_DIRECT" == "true" ]]; then
  44. store_mac
  45. break
  46. fi
  47. echo " wlan0 not ready yet..."
  48. sleep 5
  49. done
  50. }
  51. rename_interface() {
  52. ##Fool iCamera by renaming the hardline interface to wlan0
  53. echo "renaming interfaces"
  54. ifconfig $1 down
  55. ifconfig wlan0 down
  56. /opt/wz_mini/bin/busybox ip link set wlan0 name wlanold
  57. /opt/wz_mini/bin/busybox ip addr flush dev wlanold
  58. /opt/wz_mini/bin/busybox ip link set $1 name wlan0
  59. eth_wlan_up
  60. }
  61. rename_interface_and_setup_bonding() {
  62. ##Fool iCamera by renaming the hardline interface to wlan0
  63. ## $1 Bonding Interface, $2 Primary Interface, $3 Secondary Interface
  64. bonding_interface=$1
  65. primary_interface=$2
  66. secondary_interface=$3
  67. echo "renaming interfaces"
  68. # Bring all interfaces down
  69. ifconfig $bonding_interface down
  70. ifconfig $primary_interface down
  71. ifconfig $secondary_interface down
  72. # Have to bring bonding interface up to be able to bond our slaves.
  73. /opt/wz_mini/bin/busybox ip link set $bonding_interface up
  74. # Rename the real wlan0 interface if needed/used
  75. if [[ "$primary_interface" == "wlan0" ]]; then
  76. /opt/wz_mini/bin/busybox ip link set $primary_interface name wlanold
  77. /opt/wz_mini/bin/busybox ip addr flush dev wlanold
  78. primary_interface="wlanold"
  79. # Because we just changed the name of the primary interface, we need to
  80. # tell the bonding driver about the name change as well.
  81. echo "$primary_interface" > /sys/devices/virtual/net/$bonding_interface/bonding/primary
  82. fi
  83. if [[ "$secondary_interface" == "wlan0" ]]; then
  84. /opt/wz_mini/bin/busybox ip link set $secondary_interface name wlanold
  85. /opt/wz_mini/bin/busybox ip addr flush dev wlanold
  86. secondary_interface="wlanold"
  87. fi
  88. # Enslave the Ethernet and Original Wifi interfaces
  89. # under the bonding interface.
  90. /opt/wz_mini/tmp/.bin/ifenslave $bonding_interface $primary_interface $secondary_interface
  91. # Have to bring bonding interface down to be rename the interface
  92. /opt/wz_mini/bin/busybox ip link set $bonding_interface down
  93. # Name the bonding interface to be the "new" wlan0 interface
  94. /opt/wz_mini/bin/busybox ip link set $bonding_interface name wlan0
  95. # Bring the newly renamed wlan0 (actually the bond interface) back up
  96. eth_wlan_up
  97. }
  98. eth_wlan_up() {
  99. ##Run DHCP client, and bind mount our fake wpa_cli.sh to fool iCamera
  100. ifconfig wlan0 up
  101. pkill udhcpc
  102. udhcpc -i wlan0 -x hostname:$CUSTOM_HOSTNAME -p /var/run/udhcpc.pid -b
  103. # If running with Interface Bonding enabled, kill any existing
  104. # wpa_supplicant that might be running and spawn our own instead
  105. if [[ "$BONDING_ENABLED" == "true" ]] && ([[ "$ENABLE_USB_ETH" == "true" ]] || [[ "$ENABLE_USB_DIRECT" == "true" ]]); then
  106. /opt/wz_mini/bin/busybox killall wpa_supplicant
  107. wpa_supplicant -D nl80211 -i wlanold -c /tmp/wpa_supplicant.conf -B -s
  108. fi
  109. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  110. mount -o bind /opt/wz_mini/bin/wpa_cli.sh /system/bin/wpa_cli
  111. else
  112. mount -o bind /opt/wz_mini/bin/wpa_cli.sh /bin/wpa_cli
  113. fi
  114. break
  115. }
  116. wpa_check() {
  117. #Check if wpa_supplicant has been created by iCamera
  118. if [ -e /tmp/wpa_supplicant.conf ]; then
  119. echo "wpa_supplicant.conf ready"
  120. wlanold_check $1
  121. else
  122. echo "wpa_supplicant.conf not ready, wait some time for creation."
  123. COUNT=0
  124. ATTEMPTS=15
  125. until [[ -e /tmp/wpa_supplicant.conf ]] || [[ $COUNT -eq $ATTEMPTS ]]; do
  126. echo -e "$(( COUNT++ ))... \c"
  127. sleep 5
  128. done
  129. [[ $COUNT -eq $ATTEMPTS ]] && echo "time exceeded waiting for iCamera, continue potentially broken condition without network." && wlanold_check $1
  130. fi
  131. }
  132. wlanold_check() {
  133. #Have we renamed interfaces yet?
  134. if [ -d /sys/class/net/wlanold ]; then
  135. echo "wlanold exist"
  136. eth_wlan_up
  137. else
  138. echo "wlanold doesn't exist"
  139. if [[ "$BONDING_ENABLED" == "true" ]] && ([[ "$ENABLE_USB_ETH" == "true" ]] || [[ "$ENABLE_USB_DIRECT" == "true" ]]); then
  140. rename_interface_and_setup_bonding bond0 "$BONDING_PRIMARY_INTERFACE" "$BONDING_SECONDARY_INTERFACE"
  141. else
  142. rename_interface $1
  143. fi
  144. fi
  145. }
  146. netloop() {
  147. ##While loop for check
  148. while true
  149. do
  150. wpa_check $1
  151. echo "wlan0 not ready yet..."
  152. sleep 5
  153. done
  154. }
  155. swap_enable() {
  156. if [ -e /opt/wz_mini/swap ]; then
  157. echo "Swap file exists"
  158. if cat /proc/swaps | grep "mini" ; then
  159. echo "Swap is already enabled"
  160. else
  161. echo "Enable swap"
  162. swapon /opt/wz_mini/swap
  163. fi
  164. else
  165. echo "Swap file missing!"
  166. fi
  167. }
  168. dmesg_log() {
  169. DMESG_LOG=/opt/wz_mini/log/dmesg
  170. if [[ -e $DMESG_LOG.log || -L $DMESG_LOG.log ]] ; then
  171. i=0
  172. while [[ -e $DMESG_LOG.log.$i || -L $DMESG_LOG.log.$i ]] ; do
  173. let i++
  174. done
  175. mv $DMESG_LOG.log $DMESG_LOG.log.$i
  176. DMESG_LOG=$DMESG_LOG
  177. fi
  178. touch -- "$DMESG_LOG".log
  179. dmesg > $DMESG_LOG.log 2>&1
  180. }
  181. trim_logs() {
  182. echo "Deleting logs older than 5 boots..."
  183. find /opt/wz_mini/log -name '*log*' | while read file; do
  184. [ "${file#/opt/wz_mini/log/*log.}" -gt 5 ] && rm -v "$file"
  185. done
  186. }
  187. first_run_check
  188. wait_wlan
  189. #Set module dir depending on platform
  190. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  191. KMOD_PATH="/opt/wz_mini/lib/modules/3.10.14"
  192. else
  193. KMOD_PATH="/opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__"
  194. fi
  195. swap_enable
  196. if [[ "$ENABLE_IPV6" == "true" ]]; then
  197. echo "ipv6 enabled"
  198. else
  199. echo "ipv6 disabled"
  200. sysctl -w net.ipv6.conf.all.disable_ipv6=1
  201. fi
  202. if [[ "$ENABLE_NFSv4" == "true" ]]; then
  203. insmod $KMOD_PATH/kernel/lib/oid_registry.ko
  204. insmod $KMOD_PATH/kernel/net/dns_resolver/dns_resolver.ko
  205. insmod $KMOD_PATH/kernel/fs/nfs/nfsv4.ko
  206. insmod $KMOD_PATH/kernel/net/sunrpc/auth_gss/auth_rpcgss.ko
  207. echo nfsv4 enabled
  208. else
  209. echo nfsv4 disabled
  210. fi
  211. if [[ "$ENABLE_IPTABLES" == "true" ]]; then
  212. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  213. echo "T20 has iptables built in"
  214. else
  215. insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/netfilter/x_tables.ko
  216. insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ip_tables.ko
  217. insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/ipt_REJECT.ko
  218. insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_filter.ko
  219. insmod /lib/modules/3.10.14__isvp_swan_1.0__/kernel/net/ipv4/netfilter/iptable_mangle.ko
  220. echo "iptables ipv4 enabled"
  221. fi
  222. if [[ "$ENABLE_IPV6" == "true" ]]; then
  223. insmod $KMOD_PATH/kernel/net/ipv6/netfilter/ip6_tables.ko
  224. insmod $KMOD_PATH/kernel/net/ipv6/netfilter/ip6t_REJECT.ko
  225. insmod $KMOD_PATH/kernel/net/ipv6/netfilter/ip6table_filter.ko
  226. insmod $KMOD_PATH/kernel/net/ipv6/netfilter/ip6table_mangle.ko
  227. echo "iptables ipv6 enabled"
  228. fi
  229. else
  230. echo "iptables disabled"
  231. fi
  232. if [[ "$ENABLE_USB_ETH" == "true" ]]; then
  233. insmod $KMOD_PATH/kernel/drivers/net/usb/usbnet.ko
  234. # Auto-Detect an Ethernet Driver and load it
  235. if [[ "$ENABLE_USB_ETH_MODULE_AUTODETECT" == "true" ]]; then
  236. for DEVICE in `lsusb | awk '{print $6}'| tr '[:upper:]' '[:lower:]'`; do
  237. case $DEVICE in
  238. '077b:2226' | '0846:1040' | '2001:1a00' | '0b95:1720' | '07b8:420a' |\
  239. '08dd:90ff' | '0557:2009' | '0411:003d' | '0411:006e' | '6189:182d' |\
  240. '0df6:0056' | '07aa:0017' | '1189:0893' | '1631:6200' | '04f1:3008' |\
  241. '17ef:7203' | '0b95:772b' | '0b95:7720' | '0b95:1780' | '0789:0160' |\
  242. '13b1:0018' | '1557:7720' | '07d1:3c05' | '2001:3c05' | '2001:1a02' |\
  243. '1737:0039' | '04bb:0930' | '050d:5055' | '05ac:1402' | '0b95:772a' |\
  244. '14ea:ab11' | '0db0:a877' | '0b95:7e2b' | '0b95:172a' | '066b:20f9')
  245. echo "Loading ASIX Ethernet driver..."
  246. modprobe asix
  247. ;;
  248. '0b95:1790' | '0b95:178a' | '0df6:0072')
  249. echo "Loading AX88179 Gigabit Ethernet driver..."
  250. modprobe ax88179_178a
  251. ;;
  252. '1004:61aa' | '046d:c11f' | '1410:b001' | '1410:9010' | '413c:8195' |\
  253. '413c:8196' | '413c:819b' | '16d5:650a' | '12d1:14ac' | '0bda:8152')
  254. echo "Loading USB CDC Ethernet driver..."
  255. modprobe cdc_ether
  256. ;;
  257. '0bda:8152')
  258. echo "Loading Realtek RTL8152 driver..."
  259. modprobe r8152
  260. ;;
  261. esac
  262. done
  263. fi
  264. # Manually load any other Ethernet Drivers if asked for
  265. if [[ "$ENABLE_USB_ETH_MODULE_MANUAL" != "" ]]; then
  266. for i in $(echo "$ENABLE_USB_ETH_MODULES" | tr "," "\n")
  267. do
  268. insmod $KMOD_PATH/kernel/drivers/net/usb/$i.ko
  269. done
  270. fi
  271. if [[ "$BONDING_ENABLED" == "true" ]]; then
  272. if [[ "$BONDING_LINK_MONITORING_FREQ_MS" == "" ]]; then
  273. "$BONDING_LINK_MONITORING_FREQ_MS" = "100"
  274. fi
  275. if [[ "$BONDING_DOWN_DELAY_MS" == "" ]]; then
  276. "$BONDING_DOWN_DELAY_MS" = "5000"
  277. fi
  278. if [[ "$BONDING_UP_DELAY_MS" == "" ]]; then
  279. "$BONDING_UP_DELAY_MS" = "5000"
  280. fi
  281. if [[ "$BONDING_PRIMARY_INTERFACE" == "" ]]; then
  282. "$BONDING_PRIMARY_INTERFACE" = "eth0"
  283. fi
  284. if [[ "$BONDING_SECONDARY_INTERFACE" == "" ]]; then
  285. "$BONDING_SECONDARY_INTERFACE" = "wlan0"
  286. fi
  287. # Insert the bonding driver into the kernel
  288. insmod $KMOD_PATH/kernel/drivers/net/bonding/bonding.ko mode=active-backup miimon="$BONDING_LINK_MONITORING_FREQ_MS" downdelay="$BONDING_DOWN_DELAY_MS" updelay="$BONDING_UP_DELAY_MS" primary="$BONDING_PRIMARY_INTERFACE"
  289. fi
  290. swap_enable
  291. netloop eth0
  292. else
  293. echo "usb ethernet disabled"
  294. fi
  295. if [[ "$ENABLE_USB_DIRECT" == "true" ]]; then
  296. HOST_MACADDR=$(echo "$CUSTOM_HOSTNAME"|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
  297. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  298. echo connect > /sys/devices/platform/jz-dwc2/dwc2/udc/dwc2/soft_connect
  299. sleep 1
  300. devmem 0x10000040 32 0x0b800096
  301. sleep 1
  302. devmem 0x13500000 32 0x001100cc
  303. else
  304. #Set dwc2 ID_PIN driver memory
  305. devmem 0x13500000 32 0x001100cc
  306. devmem 0x10000040 32 0x0b000096
  307. #wipe the bits to set the ID_PIN, only for the V3.
  308. devmem 0x10000040 32 0x0b000FFF
  309. fi
  310. insmod $KMOD_PATH/kernel/drivers/usb/gadget/libcomposite.ko
  311. if [ -f /opt/wz_mini/tmp/.T31 ]; then
  312. insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/usb/gadget/u_ether.ko
  313. insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/usb/gadget/usb_f_ncm.ko
  314. fi
  315. insmod $KMOD_PATH/kernel/drivers/usb/gadget/g_ncm.ko iManufacturer=wz_mini_ncm host_addr="$HOST_MACADDR" dev_addr="$USB_DIRECT_MAC_ADDR"
  316. sleep 1
  317. if [[ "$BONDING_ENABLED" == "true" ]]; then
  318. if [[ "$BONDING_LINK_MONITORING_FREQ_MS" == "" ]]; then
  319. "$BONDING_LINK_MONITORING_FREQ_MS" = "100"
  320. fi
  321. if [[ "$BONDING_DOWN_DELAY_MS" == "" ]]; then
  322. "$BONDING_DOWN_DELAY_MS" = "5000"
  323. fi
  324. if [[ "$BONDING_UP_DELAY_MS" == "" ]]; then
  325. "$BONDING_UP_DELAY_MS" = "5000"
  326. fi
  327. if [[ "$BONDING_PRIMARY_INTERFACE" == "" ]]; then
  328. "$BONDING_PRIMARY_INTERFACE" = "usb0"
  329. fi
  330. if [[ "$BONDING_SECONDARY_INTERFACE" == "" ]]; then
  331. "$BONDING_SECONDARY_INTERFACE" = "wlan0"
  332. fi
  333. # Insert the bonding driver into the kernel
  334. insmod $KMOD_PATH/kernel/drivers/net/bonding/bonding.ko mode=active-backup miimon="$BONDING_LINK_MONITORING_FREQ_MS" downdelay="$BONDING_DOWN_DELAY_MS" updelay="$BONDING_UP_DELAY_MS" primary="$BONDING_PRIMARY_INTERFACE"
  335. fi
  336. swap_enable
  337. #loop begin
  338. while true
  339. do
  340. wpa_check usb0
  341. echo "wlan0 not ready yet..."
  342. sleep 1
  343. done
  344. else
  345. echo "usb direct disabled"
  346. fi
  347. if [[ "$ENABLE_USB_RNDIS" == "true" ]]; then
  348. insmod $KMOD_PATH/kernel/drivers/net/usb/usbnet.ko
  349. insmod $KMOD_PATH/kernel/drivers/net/usb/cdc_ether.ko
  350. insmod $KMOD_PATH/kernel/drivers/net/usb/rndis_host.ko
  351. sleep 1
  352. swap_enable
  353. #loop begin
  354. while true
  355. do
  356. wpa_check usb0
  357. echo "wlan0 not ready yet..."
  358. sleep 1
  359. done
  360. else
  361. echo "usb direct disabled"
  362. fi
  363. if [[ "$ENABLE_WIREGUARD" == "true" ]]; then
  364. insmod $KMOD_PATH/kernel/net/ipv4/tunnel4.ko
  365. insmod $KMOD_PATH/kernel/net/ipv4/ip_tunnel.ko
  366. insmod $KMOD_PATH/kernel/net/wireguard/wireguard.ko
  367. if [[ "$WIREGUARD_IPV4" != "" ]]; then
  368. if [ -d /opt/wz_mini/etc/wireguard ]; then
  369. echo "wireguard dir exists"
  370. else
  371. mkdir -p /opt/wz_mini/etc/wireguard
  372. fi
  373. if [ ! -f /opt/wz_mini/etc/wireguard/privatekey ]; then
  374. (umask 277 && /opt/wz_mini/bin/wg genkey | /opt/wz_mini/bin/busybox tee /opt/wz_mini/etc/wireguard/privatekey | /opt/wz_mini/bin/wg pubkey > /opt/wz_mini/etc/wireguard/publickey)
  375. fi
  376. /opt/wz_mini/bin/busybox ip link add dev wg0 type wireguard
  377. /opt/wz_mini/bin/busybox ip address add dev wg0 "$WIREGUARD_IPV4"
  378. /opt/wz_mini/bin/wg set wg0 private-key /opt/wz_mini/etc/wireguard/privatekey
  379. /opt/wz_mini/bin/busybox ip link set wg0 up
  380. fi
  381. if [[ "$WIREGUARD_PEER_PUBLIC_KEY" != "" ]] && [[ "$WIREGUARD_PEER_ALLOWED_IPS" != "" ]] && [[ "$WIREGUARD_PEER_ENDPOINT" != "" ]] && [[ "$WIREGUARD_PEER_KEEP_ALIVE" != "" ]]; then
  382. /opt/wz_mini/bin/wg set wg0 peer "$WIREGUARD_PEER_PUBLIC_KEY" allowed-ips "$WIREGUARD_PEER_ALLOWED_IPS" endpoint "$WIREGUARD_PEER_ENDPOINT" persistent-keepalive "$WIREGUARD_PEER_KEEP_ALIVE"
  383. /opt/wz_mini/bin/busybox ip route add "$WIREGUARD_PEER_ALLOWED_IPS" dev wg0
  384. fi
  385. else
  386. echo "wireguard disabled"
  387. fi
  388. if [[ "$ENABLE_CIFS" == "true" ]]; then
  389. insmod $KMOD_PATH/kernel/fs/cifs/cifs.ko
  390. else
  391. echo "cifs disabled"
  392. fi
  393. if [[ "$ENABLE_USB_STORAGE" == "true" ]]; then
  394. insmod $KMOD_PATH/kernel/drivers/scsi/scsi_mod.ko
  395. insmod $KMOD_PATH/kernel/drivers/scsi/sd_mod.ko
  396. insmod $KMOD_PATH/kernel/drivers/usb/storage/usb-storage.ko
  397. else
  398. echo "usb_storage disabled"
  399. fi
  400. if [[ "$ENABLE_EXT4" == "true" ]]; then
  401. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  402. insmod $KMOD_PATH/kernel/lib/crc16.ko
  403. fi
  404. insmod $KMOD_PATH/kernel/fs/jbd2/jbd2.ko
  405. insmod $KMOD_PATH/kernel/fs/mbcache.ko
  406. insmod $KMOD_PATH/kernel/fs/ext4/ext4.ko
  407. else
  408. echo "ext4 disabled"
  409. fi
  410. if [[ "$DISABLE_FW_UPGRADE" == "true" ]]; then
  411. mkdir /tmp/Upgrade
  412. mount -t tmpfs -o size=1,nr_inodes=1 none /tmp/Upgrade
  413. echo -e "127.0.0.1 localhost \n127.0.0.1 wyze-upgrade-service.wyzecam.com" > /opt/wz_mini/tmp/.storage/hosts
  414. mount --bind /opt/wz_mini/tmp/.storage/hosts /etc/hosts
  415. else
  416. mkdir /tmp/Upgrade
  417. /opt/wz_mini/bin/busybox inotifyd /opt/wz_mini/usr/bin/watch_up.sh /tmp:n &
  418. fi
  419. if [[ "$REMOTE_SPOTLIGHT" == "true" ]]; then
  420. /opt/wz_mini/bin/socat pty,link=/dev/ttyUSB0,raw tcp:"$REMOTE_SPOTLIGHT_HOST":9000 &
  421. echo "remote accessory enabled"
  422. else
  423. echo "remote accessory disabled"
  424. fi
  425. if [[ "$ENABLE_MP4_WRITE" == "true" ]]; then
  426. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  427. echo "mp4_write is not supported on T20"
  428. else
  429. /opt/wz_mini/bin/cmd mp4write on
  430. echo "mp4_write enabled"
  431. fi
  432. else
  433. echo "mp4 write disabled"
  434. fi
  435. if [[ "$RTSP_HI_RES_ENABLED" == "true" ]]; then
  436. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  437. HI_VIDEO_DEV="/dev/video6"
  438. else
  439. HI_VIDEO_DEV="/dev/video1"
  440. fi
  441. swap_enable
  442. if [[ "$RTSP_PASSWORD" = "" ]]; then
  443. RTSP_PASSWORD=$(cat /opt/wz_mini/tmp/wlan0_mac)
  444. fi
  445. /opt/wz_mini/bin/cmd video on
  446. if [[ "$RTSP_HI_RES_ENABLE_AUDIO" == "true" ]]; then
  447. /opt/wz_mini/bin/cmd audio on
  448. AUDIO_CH="-C 1"
  449. AUDIO_FMT="-a S16_LE"
  450. DEVICE1="$HI_VIDEO_DEV,hw:Loopback,0"
  451. else
  452. DEVICE1="$HI_VIDEO_DEV"
  453. echo "rtsp audio disabled"
  454. fi
  455. if [[ "$RTSP_HI_RES_ENC_PARAMETER" != "" ]]; then
  456. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  457. if [[ $RTSP_HI_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  458. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:0:4:$RTSP_HI_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  459. sleep 5
  460. else
  461. echo "Invalid encoder value"
  462. fi
  463. else
  464. if [[ $RTSP_HI_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  465. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:44:4:$RTSP_HI_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  466. sleep 5
  467. else
  468. echo "Invalid encoder value"
  469. fi
  470. fi
  471. fi
  472. if [[ "$RTSP_HI_RES_MAX_BITRATE" != "" ]]; then
  473. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  474. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:28:4:$RTSP_HI_RES_MAX_BITRATE" > /dev/null 2>&1 &
  475. sleep 5
  476. else
  477. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:52:4:$RTSP_HI_RES_MAX_BITRATE" > /dev/null 2>&1 &
  478. sleep 5
  479. fi
  480. fi
  481. if [[ "$RTSP_HI_RES_TARGET_BITRATE" != "" ]]; then
  482. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  483. echo "not supported on T20"
  484. else
  485. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:48:4:$RTSP_HI_RES_TARGET_BITRATE" > /dev/null 2>&1 &
  486. sleep 5
  487. fi
  488. fi
  489. if [[ "$RTSP_HI_RES_FPS" != "" ]]; then
  490. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  491. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:8:4:$RTSP_HI_RES_FPS" > /dev/null 2>&1 &
  492. sleep 5
  493. else
  494. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:80:4:$RTSP_HI_RES_FPS" > /dev/null 2>&1 &
  495. sleep 5
  496. fi
  497. fi
  498. else
  499. echo "rtsp disabled"
  500. fi
  501. if [[ "$RTSP_LOW_RES_ENABLED" == "true" ]]; then
  502. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  503. LOW_VIDEO_DEV="/dev/video7"
  504. else
  505. LOW_VIDEO_DEV="/dev/video2"
  506. fi
  507. swap_enable
  508. /opt/wz_mini/bin/cmd video on1
  509. if [[ "$RTSP_PASSWORD" = "" ]]; then
  510. RTSP_PASSWORD=$(cat /opt/wz_mini/tmp/wlan0_mac)
  511. fi
  512. if [[ "$RTSP_LOW_RES_ENABLE_AUDIO" == "true" ]]; then
  513. /opt/wz_mini/bin/cmd audio on1
  514. AUDIO_CH="-C 1"
  515. AUDIO_FMT="-a S16_LE"
  516. DEVICE2="$LOW_VIDEO_DEV,hw:Loopback,1"
  517. else
  518. DEVICE2="$LOW_VIDEO_DEV"
  519. echo "rtsp audio disabled"
  520. fi
  521. if [[ "$RTSP_LOW_RES_ENC_PARAMETER" != "" ]]; then
  522. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  523. if [[ $RTSP_LOW_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  524. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:0:4:$RTSP_LOW_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  525. sleep 5
  526. else
  527. echo "Invalid encoder value"
  528. fi
  529. else
  530. if [[ $RTSP_LOW_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  531. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:44:4:$RTSP_LOW_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  532. else
  533. echo "Invalid encoder value"
  534. fi
  535. fi
  536. fi
  537. if [[ "$RTSP_LOW_RES_MAX_BITRATE" != "" ]]; then
  538. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  539. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:28:4:$RTSP_LOW_RES_MAX_BITRATE" > /dev/null 2>&1 &
  540. sleep 5
  541. else
  542. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:52:4:$RTSP_LOW_RES_MAX_BITRATE" > /dev/null 2>&1 &
  543. fi
  544. fi
  545. if [[ "$RTSP_LOW_RES_TARGET_BITRATE" != "" ]]; then
  546. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  547. echo "not supported on T20"
  548. else
  549. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:48:4:$RTSP_LOW_RES_TARGET_BITRATE" > /dev/null 2>&1 &
  550. fi
  551. fi
  552. if [[ "$RTSP_LOW_RES_FPS" != "" ]]; then
  553. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  554. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:8:4:$RTSP_LOW_RES_FPS" > /dev/null 2>&1 &
  555. sleep 5
  556. else
  557. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:80:4:$RTSP_LOW_RES_FPS" > /dev/null 2>&1 &
  558. fi
  559. fi
  560. else
  561. echo "rtsp disabled"
  562. fi
  563. if [[ "$RTSP_LOW_RES_ENABLED" == "true" ]] || [[ "$RTSP_HI_RES_ENABLED" == "true" ]]; then
  564. echo "delay RTSP for iCamera"
  565. #This delay is required. Sometimes, if you start the rtsp server too soon, live view will break on the app.
  566. sleep 5
  567. LD_LIBRARY_PATH=/opt/wz_mini/lib /opt/wz_mini/bin/v4l2rtspserver $AUDIO_CH $AUDIO_FMT -F0 -U "$RTSP_LOGIN":"$RTSP_PASSWORD" -P "$RTSP_PORT" $DEVICE1 $DEVICE2 &
  568. fi
  569. if ([[ "$RTSP_LOW_RES_ENABLED" == "true" ]] || [[ "$RTSP_HI_RES_ENABLED" == "true" ]]) && [[ "$RTMP_STREAM_ENABLED" == "true" ]] && ([[ "$RTSP_LOW_RES_ENABLE_AUDIO" == "true" ]] || [[ "$RTSP_HI_RES_ENABLE_AUDIO" == "true" ]]); then
  570. if [[ "$RTMP_STREAM_DISABLE_AUDIO" == "true" ]]; then
  571. RMTP_AUDIO="no_audio"
  572. fi
  573. echo "delay RTMP server"
  574. #Follow the delay from the RTSP server
  575. sleep 5
  576. /opt/wz_mini/bin/rtmp-stream.sh "$RMTP_STREAM_SERVICE" "$RTMP_AUDIO"
  577. fi
  578. if [[ "$NIGHT_DROP_DISABLE" == "true" ]]; then
  579. touch /opt/wz_mini/tmp/.nd
  580. fi
  581. if [[ "$ENABLE_ATBM603X_DRIVER" == "true" ]]; then
  582. #Reduce dmesg log spam by driver
  583. echo "LOG_ERR=OFF LOG_WARN=ON LOG_LMAC=ON LOG_SCAN=OFF" > /sys/module/atbm603x_wifi_sdio/atbmfs/atbm_printk_mask
  584. fi
  585. hostname_set
  586. touch /opt/wz_mini/tmp/.wz_user_firstrun
  587. pkill -f dumpload #Kill dumpload so it won't waste cpu or ram gathering cores and uploading them when something crashes
  588. sysctl -w kernel.core_pattern='|/bin/false'
  589. dmesg_log
  590. trim_logs
  591. sync;echo 3 > /proc/sys/vm/drop_caches
  592. if [ -f "$CUSTOM_SCRIPT_PATH" ]; then
  593. echo "starting custom script"
  594. $CUSTOM_SCRIPT_PATH &
  595. else
  596. echo "custom script not found"
  597. fi
  598. echo "wz_user.sh done" > /dev/kmsg