ser2net.init 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2017 Michael Heimpold
  3. START=75
  4. STOP=10
  5. USE_PROCD=1
  6. PROG=/usr/sbin/ser2net
  7. STATICCFGFILE="/etc/ser2net.conf"
  8. DYNAMICCFGFILE="/tmp/ser2net.conf"
  9. list_cb_append() {
  10. local var="$2"
  11. local value="$1"
  12. local sep="${3:- }"
  13. eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
  14. }
  15. append_bool() {
  16. local var="$1"
  17. local key="$2"
  18. local val="$3"
  19. local uc="$4"
  20. local s=""
  21. [ "$uc" -eq 1 ] && key=`echo "$key" | tr '[a-z]' '[A-Z]'`
  22. [ "$val" -eq 0 ] && s="-"
  23. append "$var" "$s$key"
  24. }
  25. ser2net_default() {
  26. local cfg="$1"
  27. local key val
  28. for key in speed baudrate databits stopbits parity chardelay_scale chardelay_min; do
  29. config_get val "$cfg" "$key"
  30. [ -n "$val" ] || continue
  31. case "$key" in
  32. baudrate) key="speed" ;;
  33. hangup_when_done) ;;
  34. telnet_brk_on_sync) ;;
  35. deassert_CTS_DCD_DSR_on_connect) ;;
  36. *) key=`echo "$key" | tr '_' '-'`
  37. esac
  38. echo "DEFAULT:$key:$val"
  39. done
  40. for key in chardelay deassert_CTS_DCD_DSR_on_connect hangup_when_done kickolduser \
  41. local nobreak remctl rtscts telnet_brk_on_sync xonxoff; do
  42. config_get_bool val "$cfg" "$key"
  43. [ -n "$val" ] || continue
  44. [ "$val" -eq 0 ] && val="false" || val="true"
  45. echo "DEFAULT:$key:$val"
  46. done
  47. echo
  48. }
  49. ser2net_controlport() {
  50. local cfg="$1"
  51. local enabled host port
  52. config_get_bool enabled "$cfg" enabled 0
  53. [ "$enabled" -eq 0 ] && return 0
  54. config_get host "$cfg" host
  55. config_get port "$cfg" port
  56. echo -e "CONTROLPORT:${host:+$host,}$port\n"
  57. }
  58. ser2net_led() {
  59. local cfg="$1"
  60. local driver device state duration
  61. config_get driver "$cfg" driver sysfs
  62. config_get device "$cfg" device
  63. config_get state "$cfg" state 1
  64. config_get duration "$cfg" duration 20
  65. echo -e "LED:$cfg:$driver:device=$device state=$state duration=$duration\n"
  66. }
  67. ser2net_proxy() {
  68. local cfg="$1"
  69. local enabled port protocol timeout device baudrate databits parity stopbits
  70. local led_tx led_rx key boolval options
  71. config_get_bool enabled "$cfg" enabled 0
  72. [ "$enabled" -eq 0 ] && return 0
  73. config_get port "$cfg" port
  74. [ "$port" -le 0 -o "$port" -gt 65535 ] && return 1
  75. config_get protocol "$cfg" protocol
  76. case "$protocol" in
  77. raw|rawlp|telnet|off) ;;
  78. *) return 1
  79. esac
  80. config_get timeout "$cfg" timeout 0
  81. config_get device "$cfg" device
  82. [ -z "$device" ] && return 1
  83. config_get baudrate "$cfg" baudrate
  84. [ -n "$baudrate" ] && append options "$baudrate"
  85. config_get databits "$cfg" databits
  86. if [ -n "$databits" ]; then
  87. [ "$databits" -lt 5 -o "$databits" -gt 8 ] && return 1
  88. append options "${databits}DATABITS"
  89. fi
  90. config_get parity "$cfg" parity
  91. parity=`echo "$parity" | tr '[a-z]' '[A-Z]'`
  92. case "$parity" in
  93. EVEN|ODD|NONE|MARK|SPACE) append options "$parity" ;;
  94. "") ;;
  95. *) return 1
  96. esac
  97. config_get stopbits "$cfg" stopbits
  98. case "$stopbits" in
  99. 1) append options "${stopbits}STOPBIT" ;;
  100. 2) append options "${stopbits}STOPBITS" ;;
  101. "") ;;
  102. *) return 1
  103. esac
  104. config_get led_tx "$cfg" led_tx
  105. [ -n "$led_tx" ] && append options "led-tx=$led_tx"
  106. config_get led_rx "$cfg" led_rx
  107. [ -n "$led_rx" ] && append options "led-rx=$led_rx"
  108. for key in rtscts local xonxoff nobreak hangup_when_done; do
  109. config_get_bool boolval "$cfg" "$key"
  110. [ -n "$boolval" ] || continue
  111. append_bool options "$key" "$boolval" 1
  112. done
  113. for key in chardelay telnet_brk_on_sync kickolduser remctl; do
  114. config_get_bool boolval "$cfg" "$key"
  115. [ -n "$boolval" ] || continue
  116. append_bool options "$key" "$boolval" 0
  117. done
  118. config_list_foreach "$cfg" options list_cb_append options
  119. if [ "`echo "$device" | sed 's/://g'`" != "$device" ]; then
  120. echo "DEVICE:$cfg:$device"
  121. device="$cfg"
  122. fi
  123. echo -e "$port:$protocol:$timeout:$device:$options\n"
  124. }
  125. start_service() {
  126. local enabled
  127. config_load ser2net
  128. config_get_bool enabled global enabled 0
  129. [ "$enabled" -gt 0 ] || return 0
  130. cat "$STATICCFGFILE" - 2>/dev/null <<-EOF > "$DYNAMICCFGFILE"
  131. #
  132. # Following part is auto-generated from UCI settings in /etc/config/ser2net
  133. #
  134. EOF
  135. config_foreach ser2net_controlport controlport >> "$DYNAMICCFGFILE"
  136. config_foreach ser2net_default default >> "$DYNAMICCFGFILE"
  137. config_foreach ser2net_led led >> "$DYNAMICCFGFILE"
  138. config_foreach ser2net_proxy proxy >> "$DYNAMICCFGFILE"
  139. procd_open_instance
  140. procd_set_param command "$PROG" -n -c "$DYNAMICCFGFILE"
  141. procd_close_instance
  142. }