Skip to content

Instantly share code, notes, and snippets.

@kasparsd
Last active July 12, 2022 23:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kasparsd/1cb3919a238bc94b93ccc72324cab517 to your computer and use it in GitHub Desktop.
Save kasparsd/1cb3919a238bc94b93ccc72324cab517 to your computer and use it in GitHub Desktop.
Zemismart Zigbee Gateway TYZS4, see https://paulbanks.org/projects/lidl-zigbee/
#!/bin/sh
#########################################
#Function: update tuya_gateway app
#Usage: ./app_upgrade.sh
#Author: hxh
#Company: tuya
#Version: 3.0
#########################################
ENG_MODE_FILE=/tmp/eng_mode
#clear fold
clear() {
if [ -z "$1" ];then
echo "please input the upgrade file path"
exit 1
fi
rm -rf $1
}
#stop engineer app and script
stop() {
ps | grep $app_shell | grep -v grep | awk '{print $1}' | xargs kill -9 > /dev/null 2>&1
ps | grep $app_name | grep -v grep | awk '{print $1}' | xargs kill -9 > /dev/null 2>&1
ps | grep $log_shell | grep -v grep | awk '{print $1}' | xargs kill -9 > /dev/null 2>&1
}
#start ncp check
start_ncp_check() {
ncp_update_file=/tmp/upgrade/NcpUpgrade.ota
if [ -f "$ncp_ver65_flag" ];then
echo "ncp is ver65, don't update."
return 0
fi
if [ -f "$ncp_update_file" ];then
mv $ncp_update_file $tuya_fold/$ncp_backup65_file
if [ "$?" -ne 0 ];then
echo "mv ncp file error."
return 1
fi
echo "mv $ncp_update_file success."
cp -f $tuya_fold/$ncp_backup65_file /tmp/ota-files/NcpUpgrade.ota
if [ "$?" -eq 0 ];then
echo "cp $tuya_fold/$ncp_backup65_file success."
echo 1 >> /tuya/coo_upgdate65
return 0
else
rm -f $tuya_fold/$ncp_backup65_file
rm -f /tmp/ota-files/NcpUpgrade.ota
echo "cp $tuya_fold/$ncp_backup65_file error."
return 1
fi
else
echo "$ncp_update_file is non-existent"
return 0
fi
}
#update normal files
update_normal() {
if [ ! -d $user_path ];then
echo "$user_path is not exist"
if [ "$current_path" = "$app_fold1" ];then
target=$app_fold2
elif [ "$current_path" = "$app_fold2" ];then
target=$app_fold1
else
echo "Don't support update dir."
exit 1
fi
else
echo "$user_path is exist"
if [ "$user_path" = "$app_fold1" ];then
target=$app_fold2
elif [ "$user_path" = "$app_fold2" ];then
target=$app_fold1
else
echo "Don't support update dir."
exit 1
fi
fi
if [ ! -d $target ];then
echo "create dir: $target"
mkdir $target
fi
echo "target dir is: $target"
echo "update dir is: $this_path/tuya_user1"
cd $this_path
# cp tuya_start.sh /tuya/
# cp udhcpc.script /tuya/
# cp tuyamtd /tuya/
# cp def.cfg /tuya/
cp -f tuyadropbear ssh_monitor.sh tuya_start.sh tuya_net_start.sh udhcpc.script tuyamtd def.cfg json_parser.sh start_record.sh /tuya/
if [ "$?" -gt 1 ];then
echo "update_normal cp shell.sh error"
exit 1
fi
cd $this_path/tuya_user1
if [ "$?" -eq 1 ];then
echo "$this_path is not exist"
exit 1
fi
chmod +x *.sh $app_name
chmod +x ./engineer/*.sh ./engineer/$app_name > /dev/null 2>&1
cp -rf * $target
if [ "$?" -gt 1 ];then
echo "update_normal cp err [target]"
exit 1
fi
if [ ! -d engineer ];then
cp -rf $current_path/engineer $target
else
cp -f $current_path/engineer/$db_name $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$db_bak_name $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$zigbee_host_cfg $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$zigbee_host_cfg1 $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$zigbee_host_ias $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$eng_sub_dev_config $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$eng_cloud_config $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$eng_app_config $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$zigbee_host_flag $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$db_enckey $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$linkage_db_name $target/engineer > /dev/null 2>&1
cp -f $current_path/engineer/$linkage_db_bak_name $target/engineer > /dev/null 2>&1
fi
cp -f $current_path/$db_name $target
if [ "$?" -gt 1 ];then
echo "update_normal cp $db_name error"
exit 1
fi
cp -f $current_path/$db_bak_name $target
if [ "$?" -gt 1 ];then
echo "update_normal cp $db_bak_name error"
exit 1
fi
cp -f $current_path/$db_enckey $target
if [ "$?" -gt 1 ];then
echo "update_normal cp $db_enckey error"
exit 1
fi
cp -f $current_path/$zigbee_host_cfg $target
if [ "$?" -gt 1 ];then
echo "update_normal cp $zigbee_host_cfg error"
exit 1
fi
cp -f $current_path/$zigbee_host_ias $target
if [ "$?" -gt 1 ];then
echo "update_normal cp $zigbee_host_ias error"
exit 1
fi
cp -f $current_path/$zigbee_host_cfg1 $target
if [ "$?" -gt 1 ];then
echo "update_normal cp $zigbee_host_cfg1 error"
exit 1
fi
cp -f $current_path/$zigbee_host_flag $target
if [ "$?" -gt 1 ];then
echo "update_normal cp $zigbee_host_flag error"
exit 1
fi
start_ncp_check
if [ "$?" -eq 0 ];then
echo "tuya_start_dir=$target" > /tuya/start.conf
echo "update host success, will updating ncp"
echo "target dir is: $target"
if [ -f "$ncp_ver65_flag" ];then
echo "don't update ver65..."
else
if [ -f "/tmp/ota-files/NcpUpgrade.ota" ];then
$target/debugtool -n
else
echo "no ncp file"
fi
fi
else
echo "update fail."
fi
}
#start normal app and script
start_normal() {
$start_shell &
$ssh_monitor_shell &
}
#check space
check_space(){
if [ "$current_path" = "$app_fold1" ];then
clear_target=$app_fold2
elif [ "$current_path" = "$app_fold2" ];then
clear_target=$app_fold1
else
echo "Check space don't support update dir."
exit 1
fi
if [ -d $clear_target ];then
echo "Will clear $clear_target/* data."
rm -rf $clear_target/*
fi
total_block_size=`df $tuya_fold | grep / | awk -F '[ %]+' '{print $5}'`
echo "Space left on device:$total_block_size %."
if [ "$total_block_size" -gt 70 ];then
rm -rf /tuya/log_dir/*
total_block_size=`df $tuya_fold | grep / | awk -F '[ %]+' '{print $5}'`
if [ "$total_block_size" -gt 70 ];then
echo "No space left on device.Space:$total_block_size %."
exit 1
fi
fi
}
#main entry
main() {
#stop
[ "$upgrade_param" = "engineer" ] && echo "1" > $ENG_MODE_FILE
echo "start to update normal"
check_space
update_normal
start_normal
clear $this_path
}
#preprocess
if [ -z "$1" ];then
echo "please input the upgrade param"
exit 1
fi
app_name=tyZ3Gw
if [ -z "$app_name" ];then
echo "app_name is null"
clear $this_path
exit 1
fi
app_shell=app_detect.sh
if [ -z "$app_shell" ];then
echo "app_shell is null"
clear $this_path
exit 1
fi
log_shell=log_detect.sh
if [ -z "$log_shell" ];then
echo "log_shell is null"
clear $this_path
exit 1
fi
upgrade_param=$1
this_path=$(cd `dirname $0`;pwd)
app_fold1="/tuya/tuya_user1"
app_fold2="/tuya/tuya_user2"
tuya_fold="/tuya"
db_name="tuya_user.db"
db_bak_name="tuya_user.db_bak"
db_enckey="tuya_enckey.db"
zigbee_host_cfg="devices.txt"
zigbee_host_ias="iaszone.txt"
zigbee_host_cfg1="devices1.txt"
zigbee_host_flag="devflag.ini"
eng_sub_dev_config="sub_dev.config"
eng_cloud_config="cloud.config"
eng_app_config="app.config"
linkage_db_name="scene_linkage.db"
linkage_db_bak_name="scene_linkage.db_bak"
user_path=`cat /tuya/start.conf | grep tuya_start_dir | cut -d "=" -f 2`
start_shell_name=tuya_start.sh
start_shell=/tuya/tuya_start.sh
ssh_monitor_shell=/tuya/ssh_monitor.sh
start_children_shell=./tuya_start_children.sh
ncp_backup65_file="NcpUpgrade_backup65.ota"
ncp_ver65_flag="/tmp/ncp_ver65_1_0_9_flag"
current_path=$user_path
echo "current_path is $current_path"
echo "user_path is $user_path"
echo "this_path is $this_path"
echo "upgrade_param is $upgrade_param"
echo "app_name is $app_name"
echo "app_shell is $app_shell"
echo "log_shell is $log_shell"
main
echo "upgrade software success"
exit 0
cat def.cfg
{
"user_cfg_file":"/tuya/config/user.cfg",
"platform":"RTL8196E",
"product_key":"keyytmXXXXXXXXXXX",
"mode_id":"RTL8196E_TY01",
"storage_dir":"./",
"log_dir":"/tuya/log_dir/",
"tmp_dir":"/tmp/",
"bin_dir":"/tuya/",
"backup_dir":"/tuya/",
"net_led":"LED0",
"status_led":"LED1",
"reset_key":"KEY0",
"wan_interface":"eth1",
"wifi_interface":"wifi0",
"serial_port":"/dev/ttyS1",
"is_cts":true,
"is_sync_systime":true,
"is_hardward":true
}
#!/bin/sh
ifconfig lo 127.0.0.1
hostname Gateway
mount -t proc proc /proc
mount -t ramfs ramfs /var
mount -t sysfs sysfs /sys
mount -t tmpfs -o size=64k,mode=0755 tmpfs /dev
#mdev -s
/bin/mknod.sh
#wyj modify
mount -t jffs2 /dev/mtdblock4 /tuya
mkdir /var/tmp
mkdir /var/web
mkdir /var/log
mkdir /var/run
mkdir /var/lock
mkdir /var/system
mkdir /var/dnrd
mkdir /var/lib
mkdir /var/lib/misc
#GPIOB4 GPIOB5 init
#/tuya/gpio_ctl.sh set GPIOB4 out
#/tuya/gpio_ctl.sh write GPIOB4 1
#/tuya/gpio_ctl.sh set GPIOB5 out
#/tuya/gpio_ctl.sh write GPIOB5 0
# Start system script
/tuya/tuya_net_start.sh
#init.sh gw wan
# modify dst-cache setting
echo "2048" > /proc/sys/net/ipv4/route/max_size
echo "180" > /proc/sys/net/ipv4/route/gc_thresh
echo 20 > /proc/sys/net/ipv4/route/gc_elasticity
echo "4096" > /proc/sys/net/netfilter/nf_conntrack_max
echo "600" > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established
echo "20" > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_time_wait
echo "20" > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_close
echo "90" > /proc/sys/net/ipv4/netfilter/ip_conntrack_udp_timeout
echo "120" > /proc/sys/net/ipv4/netfilter/ip_conntrack_udp_timeout_stream
echo "90" > /proc/sys/net/ipv4/netfilter/ip_conntrack_generic_timeout
echo "32" > /proc/sys/net/netfilter/nf_conntrack_expect_max
stty -F /dev/ttyS1 115200
stty -F /dev/ttyS1 -echo
ifconfig eth1 192.168.1.254
udhcpc -i eth1 -s /bin/udhcpc.script -p /var/run/udhcpc0.pid &
dropbear -P /var/run/dropbear.1.pid -p 22 -K 300
/tuya/tuya_start.sh &
#!/bin/sh
ethn="eth1"
net_addr=$(/tuya/tuyamtd read master_mac)
ifconfig $ethn hw ether $net_addr
echo "Set power start"
echo read 0xb8000038 16 > /proc/rtl865x/memory
echo write 0xb8000038 0x1794a104 > /proc/rtl865x/memory
echo "Set power end"
rm -rf /tuya/ssh
/tuya/ssh_monitor.sh &
#!/bin/sh
#Usage: sh tuya_start.sh UserAppRunDir
#=======================================================================
DEFULT_APP_RUN_DIR=/tuya
TY_START_CHILDREN_SHELL=tuya_start_children.sh
def_jsonvalue_NULL="defaultValue"
#echo "Tuya Gateway Application Normal Srart $0 UserAppRunDir:${1} JsonFile Path:${2}"
echo "Tuya Gateway Application Normal Srart $0 UserAppRunDir:${1}"
#set app run dir
app_run_dir=$DEFULT_APP_RUN_DIR
if [ -d "$1" ];then
app_run_dir=${1%*/}
echo "set run_dir:${app_run_dir}"
elif [ -n "$TY_ENV_APP_RUN_DIR" ];then
app_run_dir=$TY_ENV_APP_RUN_DIR
echo "find old TY_ENV_APP_RUN_DIR:${app_run_dir}"
else
echo "set defult run_dir:${DEFULT_APP_RUN_DIR}"
fi
export TY_ENV_APP_RUN_DIR=$app_run_dir
LD_LIBRARY_PATH=$app_run_dir:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
echo "TY_ENV_APP_RUN_DIR=${app_run_dir}"
UserAppRunDir=$app_run_dir
JSON_PARSER_SH=${UserAppRunDir}/json_parser.sh
#load platform configure file
DEFULT_PLATFORM_CFG_FILE=${app_run_dir}/def.cfg
user_cfg_file=$DEFULT_PLATFORM_CFG_FILE
if [ -f "$DEFULT_PLATFORM_CFG_FILE" ];then
#get user cfg file path
jsonkey_USER_CFG_FILE="user_cfg_file"
jsonvalue_USER_CFG_FILE=`sh ${JSON_PARSER_SH} ${DEFULT_PLATFORM_CFG_FILE} ${jsonkey_USER_CFG_FILE} | sed 's/\"//g'`
if [ "$jsonvalue_USER_CFG_FILE" = "$def_jsonvalue_NULL" ] || [ ! -f "$jsonvalue_USER_CFG_FILE" ] ;then
echo "get user cfg file error, load defult cfg file"
user_cfg_file=$DEFULT_PLATFORM_CFG_FILE
else
echo "get user cfg file success."
user_cfg_file=$jsonvalue_USER_CFG_FILE
fi
else
echo "defult cfg does not exist."
exit 0
fi
echo "load platform configure file:${user_cfg_file}"
export TY_ENV_USER_CFG_FILE=$user_cfg_file
#sh $TY_PLATFORM_CFG_PARSER_SHELL $app_run_dir $user_cfg_file
JsonFile=$user_cfg_file
#tmp_dir
jsonkey_USER_TMP_DIR="tmp_dir"
def_jsonvalue_USER_TMP_DIR="/tmp"
jsonvalue_USER_TMP_DIR=`sh ${JSON_PARSER_SH} ${JsonFile} ${jsonkey_USER_TMP_DIR} | sed 's/\"//g'`
if [ "$jsonvalue_USER_TMP_DIR" == "$def_jsonvalue_NULL" ];then
jsonvalue_USER_TMP_DIR=$def_jsonvalue_USER_TMP_DIR
fi
export TY_ENV_USER_TMP_DIR=${jsonvalue_USER_TMP_DIR%*/}
#platform
jsonkey_PLATFORM="platform"
def_jsonvalue_PLATFORM="RTL8196E"
jsonvalue_PLATFORM=`sh ${JSON_PARSER_SH} ${JsonFile} ${jsonkey_PLATFORM} | sed 's/\"//g'`
if [ "$jsonvalue_PLATFORM" == "$def_jsonvalue_NULL" ];then
jsonvalue_PLATFORM=$def_jsonvalue_PLATFORM
fi
export TY_ENV_PLATFORM=${jsonvalue_PLATFORM}
#wan_interface
jsonkey_WAN_IF_NAME="wan_interface"
def_jsonvalue_WAN_IF_NAME="eth1"
jsonvalue_WAN_IF_NAME=`sh ${JSON_PARSER_SH} ${JsonFile} ${jsonkey_WAN_IF_NAME} | sed 's/\"//g'`
if [ "$jsonvalue_WAN_IF_NAME" == "$def_jsonvalue_NULL" ];then
jsonvalue_WAN_IF_NAME=$def_jsonvalue_WAN_IF_NAME
fi
export TY_ENV_WAN_IF_NAME=$jsonvalue_WAN_IF_NAME
app_fold1=${app_run_dir}/tuya_user1
app_fold2=${app_run_dir}/tuya_user2
user_path=${app_fold1}
#restart dhcp
killall -9 udhcpc
echo 4 > /proc/sys/net/ipv4/tcp_syn_retries
killall udhcpc
udhcpc -i ${TY_ENV_WAN_IF_NAME} -s ${app_run_dir}/udhcpc.script -p /var/run/udhcpc0.pid & ##需要修改
#create user_tmp dir
if [ ! -d "$TY_ENV_USER_TMP_DIR" ]; then
mkdir -p "$TY_ENV_USER_TMP_DIR"
fi
default() {
echo "Into default funtion"
user_path=$app_fold1
if [ ! -d $user_path ];then
echo "Error: no run dir:${user_path}"
user_path=$app_fold2
if [ ! -d $user_path ];then
echo "Error: no run dir:${user_path}"
exit 0
else
echo "tuya_start_dir=${user_path}" > ${app_run_dir}/start.conf
fi
else
echo "tuya_start_dir=${user_path}" > ${app_run_dir}/start.conf
fi
}
cd $app_run_dir
if [ ! -r "$app_run_dir" ]; then
echo "dir:${app_run_dir} error"
exit -1
fi
if [ ! -w "$app_run_dir" ]; then
echo "dir:${app_run_dir} read only!"
else
if [ -s start.conf ];then
echo "start.conf is exist"
user_path=`cat start.conf | grep tuya_start_dir | cut -d "=" -f 2`
else
echo "start.conf is not exist"
user_path=/tytest123
fi
if [ ! -d $user_path ];then
echo "$user_path is not exist"
default
else
if [ "$user_path" != "$app_fold1" ] && [ "$user_path" != "$app_fold2" ];then
echo "$user_path error."
default
fi
fi
fi
echo "current run dir:$user_path"
cd $user_path
./$TY_START_CHILDREN_SHELL $app_run_dir $user_cfg_file &
#!/bin/sh
[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
RESOLV_CONF="/var/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
HK_CERT_FILE="/tuya/hk_cert_flag"
case "$1" in
deconfig)
if [ ! -f "$HK_CERT_FILE" ]; then
echo "Normal mode."
ifconfig $interface 192.168.1.254
route add -net default gw 192.168.1.1 dev eth1
else
echo "Homekit cert mode."
ifconfig eth1 0.0.0.0
/tuya/avahi-autoipd --no-drop-root -t /tuya/avahi/avahi-autoipd.action eth1 &
fi
;;
renew|bound)
ifconfig $interface $ip $BROADCAST $NETMASK
if [ -n "$router" ] ; then
echo "deleting routers"
while route del default gw 0.0.0.0 dev $interface ; do
:
done
for i in $router ; do
route add default gw $i dev $interface
done
fi
echo -n > $RESOLV_CONF
[ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
for i in $dns ; do
echo adding dns $i
echo nameserver $i >> $RESOLV_CONF
done
;;
esac
grep "114.114.114.114" $RESOLV_CONF
if [ $? -ne 0 ];then
echo "nameserver 114.114.114.114" >> $RESOLV_CONF
echo "nameserver 8.8.8.8" >> $RESOLV_CONF
fi
exit 0
#!/bin/sh
[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
RESOLV_CONF="/var/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
case "$1" in
deconfig)
# ifconfig $interface 192.168.1.254
# route add -net default gw 192.168.1.1 dev eth1
#ifconfig eth1 0.0.0.0
/tuya/avahi-autoipd --no-drop-root -t /tuya/avahi/avahi-autoipd.action eth1 &
;;
renew|bound)
ifconfig $interface $ip $BROADCAST $NETMASK
if [ -n "$router" ] ; then
echo "deleting routers"
while route del default gw 0.0.0.0 dev $interface ; do
:
done
for i in $router ; do
route add default gw $i dev $interface
done
fi
echo -n > $RESOLV_CONF
[ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
for i in $dns ; do
echo adding dns $i
echo nameserver $i >> $RESOLV_CONF
done
;;
esac
grep "114.114.114.114" $RESOLV_CONF
if [ $? -ne 0 ];then
echo "nameserver 114.114.114.114" >> $RESOLV_CONF
echo "nameserver 8.8.8.8" >> $RESOLV_CONF
fi
exit 0
@kasparsd
Copy link
Author

Note that product_key has been obfuscated on purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment