summaryrefslogtreecommitdiff
path: root/network/t38modem/rc.t38modem
blob: 158debc2ef25896b174140486304ab1484065e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/sh

#############################
# READ T38MODEM CONFIG FILE #
#############################

# Get the configuration information from /etc/rc.d/rc.t38modem.conf:
. /etc/rc.d/rc.t38modem.conf

###########
# LOGGING #
###########

# If possible, log events in /var/log/messages:
if [ -f /var/run/syslogd.pid -a -x /usr/bin/logger ]; then
  LOGGER=/usr/bin/logger
else # output to stdout/stderr:
  LOGGER=/bin/cat
fi

MAXMODEMS=6

# Function to return PID of modem instance:
modem_pid() {
  PID=$(ps -C t38modem -o pid,cmd | grep ${1} | grep -v grep | awk '{print $1}')
  echo "$PID"
}

# Function to start modem interface:
modem_up() {
  i=0
  while [ $i -lt $MAXMODEMS ]; do
    if [ "${MODEMNAME[$i]}" = "${1}" ]; then
      PID=$(modem_pid "${1}")
      if [ -n "$PID" ]; then
        echo "Modem "${1}" already up..."
      else
        echo "Starting t38modem on ${1}..."
        # Build PTTY name
        PTTY=${PTTY[$i]}
        if [ -z "${PTTY}" ]; then
          PTTY="+/dev/${MODEMNAME[$i]}"
        fi
        # Start t38modem
        nohup \
          /usr/bin/t38modem \
          --no-h323 \
          --sip-t38-udptl-redundancy ${T38_REDUNDANCY[$i]} \
          --sip-listen udp\$*:${LISTEN_PORT[$i]} \
          --sip-register ${SIP_ACCOUNT[$i]}@${SIP_SERVER[$i]},${SIP_PASSWORD[$i]} \
          --ptty ${PTTY} \
          --force-fax-mode \
          --route "modem:.*=sip:<dn>@${SIP_SERVER[$i]}" \
          --route "sip:.*=modem:<dn>" \
          > /dev/null 2>&1 &
      fi
      break
    fi
    i=$(($i+1))
  done
}

# Function to stop modem interface:
modem_down() {
  PID=$(modem_pid "${1}")
  if [ -n "$PID" ]; then
    echo "Stopping t38modem for modem ${1}..."
    kill $PID
  fi
}

# Function to report status on modem interface:
modem_status() {
  PID=$(modem_pid "${1}")
  echo -n "Modem ${1}: "
  if [ -n "$PID" ]; then
    echo "up"
  else
    echo "down"
  fi
}

# Function to bring modems up:
start() {
  for i in ${MODEMNAME[@]} ; do
    modem_up $i
  done
}

# Function to take modems down:
stop() {
  for i in ${MODEMNAME[@]} ; do
    modem_down $i
  done
}

# Function to query modem states:
status() {
  for i in ${MODEMNAME[@]} ; do
    modem_status $i
  done
}

############
### MAIN ###
############

case "$1" in
'start')
  start
  ;;
'stop')
  stop
  ;;
'restart')
  stop
  sleep 5
  start
  ;;
'status')
  status
  ;;
*_start)
  MODEM=$(echo $1 | /bin/cut -d '_' -f 1)
  modem_up $MODEM
  ;;
*_stop)
  MODEM=$(echo $1 | /bin/cut -d '_' -f 1)
  modem_down $MODEM
  ;;
*_restart)
  MODEM=$(echo $1 | /bin/cut -d '_' -f 1)
  modem_down $MODEM
  sleep 5
  modem_up $MODEM
  ;;
*_status)
  MODEM=$(echo $1 | /bin/cut -d '_' -f 1)
  modem_status $MODEM
  ;;
*_up)
  MODEM=$(echo $1 | /bin/cut -d '_' -f 1)
  modem_up $MODEM
  ;;
*_down)
  MODEM=$(echo $1 | /bin/cut -d '_' -f 1)
  modem_down $MODEM
  ;;
*)
  echo "usage: $0 start|stop|restart|status"
esac