5.8. Connecting Your Asterisk PBX to Analog Phone Lines

You're running a small shop with fewer than 10 analog phone lines. You're not quite ready to give up your nice reliable analog phone service, but you do want to set up an Asterisk server for your local PBX, and to integrate some VoIP services. Your first job is connecting Asterisk to your analog lines—how do you do this?

First, follow the previous recipes to install and test Asterisk's basic functions. In this recipe, we'll route incoming and outgoing calls through Asterisk. Incoming calls will be routed to our existing extension 250, which is probably not how you want to set up your system permanently, but it's fine for testing. Later in this chapter, we'll set up a proper digital receptionist.

Let's assume you have three analog phone lines. You'll need an Asterisk server, and the Digium TDM400P analog interface PCI card with three FXO ports. You'll also need to load the appropriate Zaptel driver, which for this card is the wctdm kernel module.

Install the TDM400P in your Asterisk server. Then, you'll edit /etc/zaptel.conf and /etc/asterisk/zapata.conf. First, make a backup copy of the original /etc/zaptel.conf:

	# mv zaptel.conf zaptel.conf-old

Then, make a new zaptel.conf file with these lines in it. Use your own country code—you'll find a complete list in the zonedata.c file in the Zaptel source tree:

	;zaptel.conf
	loadzone = us
	defaultzone=us
	fxsks=1,2,3

Now, load the wctdm module and verify that it loaded:

	# modprobe wctdm
	# lsmod
	Module     Size Used by
	wctdm     34880 0

To ensure that the Zaptel module loads automatically at boot, go back to the Zaptel source directory and install the configuration and startup files:

	# cd /usr/src/zaptel-1.4.3
	# make config

The next file to edit is /etc/asterisk/zapata.conf. Back up the original:

	# mv zapata.conf zapata.conf.old

Then, enter these lines in a new empty zapata.conf:

	## zapata.conf
	[channels]
	context=pstn-test-in
	signalling=fxs_ks
	language=en
	usecallerid=yes
	echocancel=yes
	transfer=yes
	immediate=no
	group=1
	channel => 1-3

Now, add the line TRUNK=Zap/g1 to the [globals] section of /etc/asterisk/extensions.conf.

Then, create a new [pstn-test-in] context in /etc/asterisk/extensions.conf. This example routes all incoming calls to the existing extension 250:

	[pstn-test-in]
	;incoming calls go to ext. 250
	exten => s,1,Dial(SIP/250,30)
	exten => s,n,Voicemail(250)
	exten => s,n,Hangup

Now, create an [outbound] context so your local users can dial out:

	[outbound]
	ignorepat => 9
	exten => _9NXXXXXX,1,Dial(TRUNK/${EXTEN:1})
	exten => _91NXXNXXXXXX,1,Dial(TRUNK/${EXTEN:1})
	exten => 911,1,Dial(TRUNK/911)
	exten => 9911,1,Dial(TRUNK/911)

Add the [pstn-test-in] context to the [default] context:

	include => pstn-test-in

Add the [outbound] context to the [local-users] context.

	include => outbound

Load the new configurations:

	asterisk1*CLI> dialplan reload

Now, give it a test drive. You should be able to make calls in the usual way: dial 9 for an outside line, then dial your normal 7-digit local numbers or 10-digit long-distance numbers. This is normal for the U.S., at any rate; you can adapt this as you need for different calling areas.

ignorepat (ignore pattern) means keep playing a dial tone after dialing whatever number or numbers you specify.

In zapata.conf, we lumped all three channels into a single hunt group, group 1. This means that callers will always be routed to the first available line.

All the Zaptel modules are loaded when you use the default configuration files. This doesn't hurt anything, but you can configure your system to load only the module you need. On CentOS (and Fedora and Red Hat), comment out all the unnecessary modules in /etc/sysconfig/zapte (on Debian, it's /etc/default/zaptel).

A fundamental security measure is to never include an outbound context in any inbound context because you don't want to provide toll calling services to the world.

If you're trying to make sense of this FXS/FXO stuff, you're noticing that the TDM400P has three FXO modules, but the configurations specify FXS signaling. Think of it this way: it accepts and translates FXO signaling on incoming calls, but has to transmit FXS signaling.

Office users are usually accustomed to dialing 9 for an outside line. With Asterisk, it's not necessary, so you don't have to set it up this way. In the example, 911 is programmed to work both ways, so users don't have to remember which is which. This line shows how to configure dialing out without pressing 9 first:

	exten => _NXXXXXX,1,Dial(TRUNK/${EXTEN})

911 service can be a problem with VoIP. If your Asterisk server is down, you won't be able to call 911. Some fallbacks to consider are keeping an analog line or two independently of your Asterisk server, or having your server in a location where you can quickly unplug an analog line from the server and connect it to a telephone. Because faxing over VoIP is still a big pain, keeping an ordinary analog fax machine with an attached telephone would solve two problems.