wz_user.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. # Should we load a specific Ethernet driver, or try to auto-detect one
  235. if [[ "$ENABLE_USB_ETH_MODULE" != "" ]]; then
  236. insmod $KMOD_PATH/kernel/drivers/net/usb/$ENABLE_USB_ETH_MODULE.ko
  237. else
  238. # Auto-Detect an Ethernet Driver and load it
  239. for DEVICE in `lsusb | awk '{print $6}'| tr '[:upper:]' '[:lower:]'`; do
  240. case $DEVICE in
  241. '077b:2226' | '0846:1040' | '2001:1a00' | '0b95:1720' | '07b8:420a' |\
  242. '08dd:90ff' | '0557:2009' | '0411:003d' | '0411:006e' | '6189:182d' |\
  243. '0df6:0056' | '07aa:0017' | '1189:0893' | '1631:6200' | '04f1:3008' |\
  244. '17ef:7203' | '0b95:772b' | '0b95:7720' | '0b95:1780' | '0789:0160' |\
  245. '13b1:0018' | '1557:7720' | '07d1:3c05' | '2001:3c05' | '2001:1a02' |\
  246. '1737:0039' | '04bb:0930' | '050d:5055' | '05ac:1402' | '0b95:772a' |\
  247. '14ea:ab11' | '0db0:a877' | '0b95:7e2b' | '0b95:172a' | '066b:20f9')
  248. echo "Loading ASIX Ethernet driver..."
  249. modprobe asix
  250. ;;
  251. '0b95:1790' | '0b95:178a' | '0df6:0072')
  252. echo "Loading AX88179 Gigabit Ethernet driver..."
  253. modprobe ax88179_178a
  254. ;;
  255. '1004:61aa' | '046d:c11f' | '1410:b001' | '1410:9010' | '413c:8195' |\
  256. '413c:8196' | '413c:819b' | '16d5:650a' | '12d1:14ac' | '0bda:8152')
  257. echo "Loading USB CDC Ethernet driver..."
  258. modprobe cdc_ether
  259. ;;
  260. '0bda:8152')
  261. echo "Loading Realtek RTL8152 driver..."
  262. modprobe r8152
  263. ;;
  264. esac
  265. done
  266. fi
  267. if [[ "$BONDING_ENABLED" == "true" ]]; then
  268. if [[ "$BONDING_LINK_MONITORING_FREQ_MS" == "" ]]; then
  269. "$BONDING_LINK_MONITORING_FREQ_MS" = "100"
  270. fi
  271. if [[ "$BONDING_DOWN_DELAY_MS" == "" ]]; then
  272. "$BONDING_DOWN_DELAY_MS" = "5000"
  273. fi
  274. if [[ "$BONDING_UP_DELAY_MS" == "" ]]; then
  275. "$BONDING_UP_DELAY_MS" = "5000"
  276. fi
  277. if [[ "$BONDING_PRIMARY_INTERFACE" == "" ]]; then
  278. "$BONDING_PRIMARY_INTERFACE" = "eth0"
  279. fi
  280. if [[ "$BONDING_SECONDARY_INTERFACE" == "" ]]; then
  281. "$BONDING_SECONDARY_INTERFACE" = "wlan0"
  282. fi
  283. # Insert the bonding driver into the kernel
  284. 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"
  285. fi
  286. swap_enable
  287. netloop eth0
  288. else
  289. echo "usb ethernet disabled"
  290. fi
  291. if [[ "$ENABLE_USB_DIRECT" == "true" ]]; then
  292. HOST_MACADDR=$(echo "$CUSTOM_HOSTNAME"|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
  293. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  294. echo connect > /sys/devices/platform/jz-dwc2/dwc2/udc/dwc2/soft_connect
  295. sleep 1
  296. devmem 0x10000040 32 0x0b800096
  297. sleep 1
  298. devmem 0x13500000 32 0x001100cc
  299. else
  300. #Set dwc2 ID_PIN driver memory
  301. devmem 0x13500000 32 0x001100cc
  302. devmem 0x10000040 32 0x0b000096
  303. #wipe the bits to set the ID_PIN, only for the V3.
  304. devmem 0x10000040 32 0x0b000FFF
  305. fi
  306. insmod $KMOD_PATH/kernel/drivers/usb/gadget/libcomposite.ko
  307. if [ -f /opt/wz_mini/tmp/.T31 ]; then
  308. insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/usb/gadget/u_ether.ko
  309. insmod /opt/wz_mini/lib/modules/3.10.14__isvp_swan_1.0__/kernel/drivers/usb/gadget/usb_f_ncm.ko
  310. fi
  311. insmod $KMOD_PATH/kernel/drivers/usb/gadget/g_ncm.ko iManufacturer=wz_mini_ncm host_addr="$HOST_MACADDR" dev_addr="$USB_DIRECT_MAC_ADDR"
  312. sleep 1
  313. if [[ "$BONDING_ENABLED" == "true" ]]; then
  314. if [[ "$BONDING_LINK_MONITORING_FREQ_MS" == "" ]]; then
  315. "$BONDING_LINK_MONITORING_FREQ_MS" = "100"
  316. fi
  317. if [[ "$BONDING_DOWN_DELAY_MS" == "" ]]; then
  318. "$BONDING_DOWN_DELAY_MS" = "5000"
  319. fi
  320. if [[ "$BONDING_UP_DELAY_MS" == "" ]]; then
  321. "$BONDING_UP_DELAY_MS" = "5000"
  322. fi
  323. if [[ "$BONDING_PRIMARY_INTERFACE" == "" ]]; then
  324. "$BONDING_PRIMARY_INTERFACE" = "usb0"
  325. fi
  326. if [[ "$BONDING_SECONDARY_INTERFACE" == "" ]]; then
  327. "$BONDING_SECONDARY_INTERFACE" = "wlan0"
  328. fi
  329. # Insert the bonding driver into the kernel
  330. 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"
  331. fi
  332. swap_enable
  333. #loop begin
  334. while true
  335. do
  336. wpa_check usb0
  337. echo "wlan0 not ready yet..."
  338. sleep 1
  339. done
  340. else
  341. echo "usb direct disabled"
  342. fi
  343. if [[ "$ENABLE_USB_RNDIS" == "true" ]]; then
  344. insmod $KMOD_PATH/kernel/drivers/net/usb/usbnet.ko
  345. insmod $KMOD_PATH/kernel/drivers/net/usb/cdc_ether.ko
  346. insmod $KMOD_PATH/kernel/drivers/net/usb/rndis_host.ko
  347. sleep 1
  348. swap_enable
  349. #loop begin
  350. while true
  351. do
  352. wpa_check usb0
  353. echo "wlan0 not ready yet..."
  354. sleep 1
  355. done
  356. else
  357. echo "usb direct disabled"
  358. fi
  359. if [[ "$ENABLE_WIREGUARD" == "true" ]]; then
  360. insmod $KMOD_PATH/kernel/net/ipv4/tunnel4.ko
  361. insmod $KMOD_PATH/kernel/net/ipv4/ip_tunnel.ko
  362. insmod $KMOD_PATH/kernel/net/wireguard/wireguard.ko
  363. if [[ "$WIREGUARD_IPV4" != "" ]]; then
  364. if [ -d /opt/wz_mini/etc/wireguard ]; then
  365. echo "wireguard dir exists"
  366. else
  367. mkdir -p /opt/wz_mini/etc/wireguard
  368. fi
  369. if [ ! -f /opt/wz_mini/etc/wireguard/privatekey ]; then
  370. (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)
  371. fi
  372. /opt/wz_mini/bin/busybox ip link add dev wg0 type wireguard
  373. /opt/wz_mini/bin/busybox ip address add dev wg0 "$WIREGUARD_IPV4"
  374. /opt/wz_mini/bin/wg set wg0 private-key /opt/wz_mini/etc/wireguard/privatekey
  375. /opt/wz_mini/bin/busybox ip link set wg0 up
  376. fi
  377. if [[ "$WIREGUARD_PEER_PUBLIC_KEY" != "" ]] && [[ "$WIREGUARD_PEER_ALLOWED_IPS" != "" ]] && [[ "$WIREGUARD_PEER_ENDPOINT" != "" ]] && [[ "$WIREGUARD_PEER_KEEP_ALIVE" != "" ]]; then
  378. /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"
  379. /opt/wz_mini/bin/busybox ip route add "$WIREGUARD_PEER_ALLOWED_IPS" dev wg0
  380. fi
  381. else
  382. echo "wireguard disabled"
  383. fi
  384. if [[ "$ENABLE_CIFS" == "true" ]]; then
  385. insmod $KMOD_PATH/kernel/fs/cifs/cifs.ko
  386. else
  387. echo "cifs disabled"
  388. fi
  389. if [[ "$ENABLE_USB_STORAGE" == "true" ]]; then
  390. insmod $KMOD_PATH/kernel/drivers/scsi/scsi_mod.ko
  391. insmod $KMOD_PATH/kernel/drivers/scsi/sd_mod.ko
  392. insmod $KMOD_PATH/kernel/drivers/usb/storage/usb-storage.ko
  393. else
  394. echo "usb_storage disabled"
  395. fi
  396. if [[ "$ENABLE_EXT4" == "true" ]]; then
  397. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  398. insmod $KMOD_PATH/kernel/lib/crc16.ko
  399. fi
  400. insmod $KMOD_PATH/kernel/fs/jbd2/jbd2.ko
  401. insmod $KMOD_PATH/kernel/fs/mbcache.ko
  402. insmod $KMOD_PATH/kernel/fs/ext4/ext4.ko
  403. else
  404. echo "ext4 disabled"
  405. fi
  406. if [[ "$DISABLE_FW_UPGRADE" == "true" ]]; then
  407. mkdir /tmp/Upgrade
  408. mount -t tmpfs -o size=1,nr_inodes=1 none /tmp/Upgrade
  409. echo -e "127.0.0.1 localhost \n127.0.0.1 wyze-upgrade-service.wyzecam.com" > /opt/wz_mini/tmp/.storage/hosts
  410. mount --bind /opt/wz_mini/tmp/.storage/hosts /etc/hosts
  411. else
  412. mkdir /tmp/Upgrade
  413. /opt/wz_mini/bin/busybox inotifyd /opt/wz_mini/usr/bin/watch_up.sh /tmp:n &
  414. fi
  415. if [[ "$REMOTE_SPOTLIGHT" == "true" ]]; then
  416. /opt/wz_mini/bin/socat pty,link=/dev/ttyUSB0,raw tcp:"$REMOTE_SPOTLIGHT_HOST":9000 &
  417. echo "remote accessory enabled"
  418. else
  419. echo "remote accessory disabled"
  420. fi
  421. if [[ "$ENABLE_MP4_WRITE" == "true" ]]; then
  422. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  423. echo "mp4_write is not supported on T20"
  424. else
  425. /opt/wz_mini/bin/cmd mp4write on
  426. echo "mp4_write enabled"
  427. fi
  428. else
  429. echo "mp4 write disabled"
  430. fi
  431. if [[ "$RTSP_HI_RES_ENABLED" == "true" ]]; then
  432. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  433. HI_VIDEO_DEV="/dev/video6"
  434. else
  435. HI_VIDEO_DEV="/dev/video1"
  436. fi
  437. swap_enable
  438. if [[ "$RTSP_PASSWORD" = "" ]]; then
  439. RTSP_PASSWORD=$(cat /opt/wz_mini/tmp/wlan0_mac)
  440. fi
  441. /opt/wz_mini/bin/cmd video on
  442. if [[ "$RTSP_HI_RES_ENABLE_AUDIO" == "true" ]]; then
  443. /opt/wz_mini/bin/cmd audio on
  444. AUDIO_CH="-C 1"
  445. AUDIO_FMT="-a S16_LE"
  446. DEVICE1="$HI_VIDEO_DEV,hw:Loopback,0"
  447. else
  448. DEVICE1="$HI_VIDEO_DEV"
  449. echo "rtsp audio disabled"
  450. fi
  451. if [[ "$RTSP_HI_RES_ENC_PARAMETER" != "" ]]; then
  452. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  453. if [[ $RTSP_HI_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  454. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:0:4:$RTSP_HI_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  455. sleep 5
  456. else
  457. echo "Invalid encoder value"
  458. fi
  459. else
  460. if [[ $RTSP_HI_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  461. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:44:4:$RTSP_HI_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  462. sleep 5
  463. else
  464. echo "Invalid encoder value"
  465. fi
  466. fi
  467. fi
  468. if [[ "$RTSP_HI_RES_MAX_BITRATE" != "" ]]; then
  469. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  470. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:28:4:$RTSP_HI_RES_MAX_BITRATE" > /dev/null 2>&1 &
  471. sleep 5
  472. else
  473. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:52:4:$RTSP_HI_RES_MAX_BITRATE" > /dev/null 2>&1 &
  474. sleep 5
  475. fi
  476. fi
  477. if [[ "$RTSP_HI_RES_TARGET_BITRATE" != "" ]]; then
  478. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  479. echo "not supported on T20"
  480. else
  481. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:48:4:$RTSP_HI_RES_TARGET_BITRATE" > /dev/null 2>&1 &
  482. sleep 5
  483. fi
  484. fi
  485. if [[ "$RTSP_HI_RES_FPS" != "" ]]; then
  486. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  487. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:8:4:$RTSP_HI_RES_FPS" > /dev/null 2>&1 &
  488. sleep 5
  489. else
  490. watch -n30 -t "/system/bin/impdbg --enc_rc_s 0:80:4:$RTSP_HI_RES_FPS" > /dev/null 2>&1 &
  491. sleep 5
  492. fi
  493. fi
  494. else
  495. echo "rtsp disabled"
  496. fi
  497. if [[ "$RTSP_LOW_RES_ENABLED" == "true" ]]; then
  498. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  499. LOW_VIDEO_DEV="/dev/video7"
  500. else
  501. LOW_VIDEO_DEV="/dev/video2"
  502. fi
  503. swap_enable
  504. /opt/wz_mini/bin/cmd video on1
  505. if [[ "$RTSP_PASSWORD" = "" ]]; then
  506. RTSP_PASSWORD=$(cat /opt/wz_mini/tmp/wlan0_mac)
  507. fi
  508. if [[ "$RTSP_LOW_RES_ENABLE_AUDIO" == "true" ]]; then
  509. /opt/wz_mini/bin/cmd audio on1
  510. AUDIO_CH="-C 1"
  511. AUDIO_FMT="-a S16_LE"
  512. DEVICE2="$LOW_VIDEO_DEV,hw:Loopback,1"
  513. else
  514. DEVICE2="$LOW_VIDEO_DEV"
  515. echo "rtsp audio disabled"
  516. fi
  517. if [[ "$RTSP_LOW_RES_ENC_PARAMETER" != "" ]]; then
  518. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  519. if [[ $RTSP_LOW_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  520. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:0:4:$RTSP_LOW_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  521. sleep 5
  522. else
  523. echo "Invalid encoder value"
  524. fi
  525. else
  526. if [[ $RTSP_LOW_RES_ENC_PARAMETER =~ "^[0|1|2|4|8]$" ]]; then
  527. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:44:4:$RTSP_LOW_RES_ENC_PARAMETER" > /dev/null 2>&1 &
  528. else
  529. echo "Invalid encoder value"
  530. fi
  531. fi
  532. fi
  533. if [[ "$RTSP_LOW_RES_MAX_BITRATE" != "" ]]; then
  534. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  535. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:28:4:$RTSP_LOW_RES_MAX_BITRATE" > /dev/null 2>&1 &
  536. sleep 5
  537. else
  538. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:52:4:$RTSP_LOW_RES_MAX_BITRATE" > /dev/null 2>&1 &
  539. fi
  540. fi
  541. if [[ "$RTSP_LOW_RES_TARGET_BITRATE" != "" ]]; then
  542. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  543. echo "not supported on T20"
  544. else
  545. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:48:4:$RTSP_LOW_RES_TARGET_BITRATE" > /dev/null 2>&1 &
  546. fi
  547. fi
  548. if [[ "$RTSP_LOW_RES_FPS" != "" ]]; then
  549. if [ -f /opt/wz_mini/tmp/.T20 ]; then
  550. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:8:4:$RTSP_LOW_RES_FPS" > /dev/null 2>&1 &
  551. sleep 5
  552. else
  553. watch -n30 -t "/system/bin/impdbg --enc_rc_s 1:80:4:$RTSP_LOW_RES_FPS" > /dev/null 2>&1 &
  554. fi
  555. fi
  556. else
  557. echo "rtsp disabled"
  558. fi
  559. if [[ "$RTSP_LOW_RES_ENABLED" == "true" ]] || [[ "$RTSP_HI_RES_ENABLED" == "true" ]]; then
  560. echo "delay RTSP for iCamera"
  561. #This delay is required. Sometimes, if you start the rtsp server too soon, live view will break on the app.
  562. sleep 5
  563. 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 &
  564. fi
  565. 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
  566. if [[ "$RTMP_STREAM_DISABLE_AUDIO" == "true" ]]; then
  567. RMTP_AUDIO="no_audio"
  568. fi
  569. echo "delay RTMP server"
  570. #Follow the delay from the RTSP server
  571. sleep 5
  572. /opt/wz_mini/bin/rtmp-stream.sh "$RMTP_STREAM_SERVICE" "$RTMP_AUDIO"
  573. fi
  574. if [[ "$NIGHT_DROP_DISABLE" == "true" ]]; then
  575. touch /opt/wz_mini/tmp/.nd
  576. fi
  577. if [[ "$ENABLE_ATBM603X_DRIVER" == "true" ]]; then
  578. #Reduce dmesg log spam by driver
  579. echo "LOG_ERR=OFF LOG_WARN=ON LOG_LMAC=ON LOG_SCAN=OFF" > /sys/module/atbm603x_wifi_sdio/atbmfs/atbm_printk_mask
  580. fi
  581. hostname_set
  582. touch /opt/wz_mini/tmp/.wz_user_firstrun
  583. pkill -f dumpload #Kill dumpload so it won't waste cpu or ram gathering cores and uploading them when something crashes
  584. sysctl -w kernel.core_pattern='|/bin/false'
  585. dmesg_log
  586. trim_logs
  587. sync;echo 3 > /proc/sys/vm/drop_caches
  588. if [ -f "$CUSTOM_SCRIPT_PATH" ]; then
  589. echo "starting custom script"
  590. $CUSTOM_SCRIPT_PATH &
  591. else
  592. echo "custom script not found"
  593. fi
  594. echo "wz_user.sh done" > /dev/kmsg