live_dahdi 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/bin/sh
  2. LINUX_DIR=${LINUX_DIR:=.}
  3. TOOLS_DIR=${TOOLS_DIR:=../dahdi-tools}
  4. DYNAMIC_LOC="${DYNAMIC_LOC:-no}"
  5. DESTDIR=$PWD/live
  6. LIVE_DAHDI_RSYNC_DIR=${LIVE_DAHDI_RSYNC_DIR:-/tmp/dahdi}
  7. KVERS=${KVERS:-`uname -r`}
  8. XPP_SYNC=${XPP_SYNC:-auto}
  9. AST_SCRIPT=${AST_SCRIPT:-/etc/init.d/asterisk}
  10. # Use this file to pass options to modules:
  11. PERLLIBDIR=`perl -V:sitelib | cut -d "'" -f 2`
  12. EXTRA_MODS="crc_ccitt"
  13. # Manual list of modules. They will be loaded by insmod.
  14. # If reside in a subdir, add it explicitly.
  15. MODULES_LOAD="${MODULES_LOAD:-dahdi dahdi_echocan_mg2}"
  16. # this one *is* resolved recusively.
  17. # the only reason to set a different value is if you use hpec / oslec,
  18. # as Zaptel depends on them.
  19. REMOVE_MODULES="${REMOVE_MODULES:-dahdi}" # "dahdi oslec"?
  20. if [ -r $DESTDIR/live.conf ]; then . $DESTDIR/live.conf; fi
  21. MODULES_DIR="$DESTDIR/lib/modules/$KVERS/dahdi"
  22. if [ ! -d "$LINUX_DIR" ]; then
  23. echo >&2 "$0:dahdi-linux dir '$LINUX_DIR' does not exits. Aborting".
  24. exit 1
  25. fi
  26. set -e
  27. LINUX_DIR_FULL=`(cd $LINUX_DIR; pwd)`
  28. set_tools_dir() {
  29. if [ ! -d "$TOOLS_DIR" ]; then
  30. echo >&2 "$0:dahdi-tools dir '$TOOLS_DIR' does not exits. Aborting".
  31. exit 1
  32. fi
  33. TOOLS_DIR_FULL=`(cd $TOOLS_DIR; pwd)`
  34. }
  35. # Give priority to our installed binaries:
  36. PATH=$DESTDIR/sbin:$DESTDIR/usr/sbin:$PATH
  37. export PATH
  38. # TODO: If you already use PERL5DIR, please fix this part:
  39. PERL5LIB="$DESTDIR/$PERLLIBDIR"
  40. export PERL5LIB
  41. # used in xpp_fxloader:
  42. FIRMWARE_DIR="$DESTDIR/usr/share/dahdi"
  43. export FIRMWARE_DIR
  44. ASTRIBANK_TOOL="$DESTDIR/usr/sbin/astribank_tool"
  45. export ASTRIBANK_TOOL
  46. ASTRIBANK_HEXLOAD="$DESTDIR/usr/sbin/astribank_hexload"
  47. export ASTRIBANK_HEXLOAD
  48. # make sure Astribank initialization scripts are from our tree.
  49. xpp_ARGS="$xpp_ARGS initdir=$FIRMWARE_DIR"
  50. dahdi_ARGS="$dahdi_ARGS tools_rootdir=$DESTDIR"
  51. if [ "$DYNAMIC_LOC" = 'yes' ]; then
  52. MODULES_LOAD="$MODULES_LOAD dahdi_dynamic dahdi_dynamic_loc"
  53. fi
  54. # the same as xpp/utils/dahdi_drivers .
  55. # With the remote mode, I can't rely on files in the source directory.
  56. dahdi_drivers() {
  57. perl -MDahdi::Hardware -e '
  58. my @drivers = Dahdi::Hardware->drivers;
  59. print join(" ", @drivers);
  60. '
  61. }
  62. # Add modules for existing hardware on the system for the list of
  63. # modules to load.
  64. #
  65. # As module loading is manual with insmod, some manual fixes are needed.
  66. set_modules_to_load() {
  67. for mod in `dahdi_drivers`; do
  68. case "$mod" in
  69. xpp_usb)
  70. MODULES_LOAD="$MODULES_LOAD xpp/xpp xpp/xpd_fxs"
  71. MODULES_LOAD="$MODULES_LOAD xpp/xpd_fxo xpp/xpd_pri"
  72. if [ -r "$MODULES_DIR/xpp/xpd_bri.ko" ]; then
  73. MODULES_LOAD="$MODULES_LOAD xpp/xpd_bri"
  74. fi
  75. if [ -r "$MODULES_DIR/xpp/xpd_echo.ko" ]; then
  76. MODULES_LOAD="$MODULES_LOAD xpp/xpd_echo"
  77. fi
  78. MODULES_LOAD="$MODULES_LOAD xpp/xpp_usb"
  79. ;;
  80. wctdm24xxp | wcte12xp)
  81. # FIXME: better automation of the voicebus
  82. # dependency:
  83. MODULES_LOAD="$MODULES_LOAD voicebus/dahdi_voicebus $mod/$mod"
  84. EXTRA_MODS="$EXTRA_MODS firmware_class"
  85. ;;
  86. wct4xxp | wcte12xp | wctc4xp | wcb4xxp)
  87. MODULES_LOAD="$MODULES_LOAD $mod/$mod"
  88. ;;
  89. wanpipe)
  90. : # requires different handling
  91. ;;
  92. *)
  93. MODULES_LOAD="$MODULES_LOAD $mod"
  94. ;;
  95. esac
  96. done
  97. }
  98. # Initialize the Xorcom Astribank (xpp/) using perl utiliites:
  99. # intended to replace all the the three functions below if user has
  100. # installed the dahdi-perl utilities.
  101. xpp_startup() {
  102. # do nothing if there are no astribank devices:
  103. if ! grep -q connected /proc/xpp/xbuses 2>/dev/null; then return 0; fi
  104. echo "Waiting for Astribank devices to initialize:"
  105. "$FIRMWARE_DIR/waitfor_xpds" # Asusmes a recent dahdi-tools
  106. # overriding locales for the above two, as perl can be noisy
  107. # when locales are missing.
  108. # No register all the devices if they didn't auto-register:
  109. LC_ALL=C dahdi_registration on
  110. # this one could actually be run after dahdi_cfg:
  111. LC_ALL=C xpp_sync "$XPP_SYNC"
  112. }
  113. # recursively unload a module and its dependencies, if possible.
  114. # where's modprobe -r when you need it?
  115. # inputs: module to unload.
  116. # returns: the result from
  117. unload_module() {
  118. module="$1"
  119. line=`lsmod 2>/dev/null | grep "^$1 " || :`
  120. if [ "$line" = '' ]; then return; fi # module was not loaded
  121. set -- $line
  122. # $1: the original module, $2: size, $3: refcount, $4: deps list
  123. mods=`echo $4 | tr , ' '`
  124. ec_modules=""
  125. # xpp_usb keeps the xpds below busy if an xpp hardware is
  126. # connected. Hence must be removed before them:
  127. case "$module" in xpd_*) mods="xpp_usb $mods";; esac
  128. for mod in $mods; do
  129. case "$mod" in
  130. dahdi_echocan_*)
  131. ec_modules="$mod $ec_modules"
  132. ;;
  133. *)
  134. # run in a subshell, so it won't step over our vars:
  135. (unload_module $mod)
  136. ;;
  137. esac
  138. done
  139. # Now that all the other dependencies are unloaded, we can unload the
  140. # dahdi_echocan modules. The drivers that register spans may keep
  141. # references on the echocan modules before they are unloaded.
  142. for mod in $ec_modules; do
  143. (unload_module $mod)
  144. done
  145. rmmod $module
  146. }
  147. load_dynamic() {
  148. if [ "$DYNAMIC_LOC" != yes ]; then return; fi
  149. local conf_file="$DESTDIR/etc/dahdi/dynamic.conf"
  150. if [ ! -r "$conf_file" ]; then
  151. cat <<EOF >"$conf_file"
  152. dynamic=loc,1:0,5,0
  153. dynamic=loc,1:1,5,0
  154. EOF
  155. fi
  156. dahdi_cfg -c "$conf_file"
  157. }
  158. unload_dynamic() {
  159. if [ "$DYNAMIC_LOC" != yes ]; then return; fi
  160. local conf_file="$DESTDIR/etc/dahdi/dynamic.conf"
  161. if [ ! -r "$conf_file" ]; then return; fi
  162. dahdi_cfg -c "$conf_file" -s || :
  163. }
  164. genconf() {
  165. GENCONF_PARAMETERS=$DESTDIR/etc/dahdi/genconf_parameters \
  166. DAHDI_CONF_FILE=$DESTDIR/etc/dahdi/system.conf \
  167. DAHDI_MODS_FILE=$DESTDIR/etc/dahdi/modules \
  168. CHAN_DAHDI_CHANNELS_FILE=$DESTDIR/etc/asterisk/dahdi-channels.conf \
  169. dahdi_genconf
  170. if [ "$DYNAMIC_LOC" = yes ]; then
  171. cat "$DESTDIR/etc/dahdi/dynamic.conf" >>"$DESTDIR/etc/dahdi/system.conf"
  172. fi
  173. dahdi_cfg -c $DESTDIR/etc/dahdi/system.conf
  174. # TODO: fxotune, hpec
  175. }
  176. run_asterisk() {
  177. $AST_SCRIPT start
  178. }
  179. usage() {
  180. me=`basename $0`
  181. echo "$me: Run DAHDI in a test environment"
  182. echo 'Version: $Id$'
  183. echo ''
  184. echo "Usage: equivalent of:"
  185. echo "$me configure ./configure"
  186. echo "$me install make install"
  187. echo "$me config make config"
  188. echo "$me unload /etc/init.d/dahdi stop"
  189. echo "$me load /etc/init.d/dahdi start"
  190. echo "$me reload /etc/init.d/dahdi restart"
  191. echo "$me xpp-firm (Reset and load xpp firmware)"
  192. echo "$me genconf dahdi_genconf; dahdi_cfg"
  193. echo "$me asterisk /etc/init.d/asterisk start"
  194. echo "$me symlink_ast symlink dahdi-channels.conf"
  195. echo "$me rsync TARGET (copy filea to /tmp/live in host TARGET)"
  196. echo "$me exec COMMAND (Run COMMAND in 'live' environment)"
  197. echo ""
  198. echo "dahdi-linux: $LINUX_DIR"
  199. echo "dahdi-tools: $TOOLS_DIR"
  200. }
  201. case "$1" in
  202. configure)
  203. shift
  204. set_tools_dir
  205. cd "$TOOLS_DIR"; ./configure --with-dahdi="$LINUX_DIR_FULL" "$@"
  206. ;;
  207. install)
  208. shift
  209. set_tools_dir
  210. cd "$LINUX_DIR"; make install DESTDIR=$DESTDIR "$@"
  211. cd "$TOOLS_DIR_FULL"; make install DESTDIR=$DESTDIR DYNFS=yes "$@"
  212. ;;
  213. config)
  214. shift
  215. set_tools_dir
  216. cd "$TOOLS_DIR"; make config DESTDIR=$DESTDIR "$@"
  217. mkdir -p $DESTDIR/etc/asterisk
  218. cat >"$FIRMWARE_DIR/live-init.conf" <<EOF
  219. PATH="$PATH" # FIXME: add to existing \$PATH?
  220. export PATH
  221. dahdiconfdir="${DESTDIR}/etc/dahdi"
  222. export dahdiconfdir
  223. EOF
  224. ln -s ../../../etc/dahdi/xpp.conf "$DESTDIR/usr/share/dahdi/"
  225. ;;
  226. rsync)
  227. if [ $# -ne 2 ]; then
  228. echo >&2 "$0: Error: rsync requires a target parameter".
  229. exit 1
  230. fi
  231. # copy the script itself and the installed directory to the
  232. # target host:
  233. rsync -ai "$0" $DESTDIR "$2:$LIVE_DAHDI_RSYNC_DIR"
  234. ;;
  235. unload)
  236. # OK for Asterisk not to be running. TODO: a better test?
  237. $AST_SCRIPT stop || :
  238. unload_dynamic
  239. for mod in $REMOVE_MODULES; do
  240. unload_module $mod
  241. done
  242. ;;
  243. load)
  244. # TODO: Find a way to use modprobe.
  245. # Or implement a way to pass arguments to modules here (yuck)
  246. set_modules_to_load
  247. for mod in $EXTRA_MODS; do
  248. modprobe $mod || : # FIXME: Make this optional?
  249. done
  250. for module in $MODULES_LOAD; do
  251. eval module_args="\$`basename ${module}`_ARGS"
  252. insmod $MODULES_DIR/$module.ko $module_args
  253. done
  254. xpp_startup
  255. load_dynamic
  256. genconf
  257. # or find a way to reuse init.d start sequence.
  258. # TODO: A local copy of Asterisk, configured with dahdi_gnconf.
  259. # doable, but trickier.
  260. run_asterisk
  261. if [ "$LIVE_DAHDI_FREEPBXDB" = 'yes' ]; then
  262. GENCONF_PARAMETERS=$DESTDIR/etc/dahdi/genconf_parameters \
  263. dahdi_genconf freepbxdb
  264. fi
  265. ;;
  266. genconf)
  267. genconf
  268. ;;
  269. asterisk)
  270. run_asterisk
  271. ;;
  272. symlink_ast)
  273. ln -sf "$DESTDIR/etc/asterisk/dahdi-channels.conf" /etc/asterisk/
  274. ;;
  275. reload)
  276. $0 unload
  277. $0 load
  278. ;;
  279. exec)
  280. if [ $# -lt 2 ]; then
  281. # No command given: start a subshell in the environemnt
  282. # of the "live" system:
  283. echo >&2 "$0: Error: exec requires a command to run"
  284. exit 1
  285. fi
  286. # Command given: run it:
  287. shift
  288. "$@"
  289. ;;
  290. xpp-firm)
  291. # Still broken. Needs to be run several times.
  292. # set XPP_HOTPLUG_DISABLED=yes in /etc/dahdi/init.conf
  293. XPP_FIRMWARE_DIR=$FIRMWARE_DIR \
  294. "$FIRMWARE_DIR/xpp_fxloader" reset
  295. sleep 5
  296. XPP_FIRMWARE_DIR=$FIRMWARE_DIR \
  297. XPP_CONFIG="$DESTDIR/etc/dahdi/xpp.conf" \
  298. "$FIRMWARE_DIR/xpp_fxloader" load
  299. ;;
  300. help|'')
  301. usage
  302. ;;
  303. *)
  304. echo >&2 "$0: Error: incorrect command \"$1\". Aborting"
  305. usage
  306. exit 1
  307. esac