Extension States

Extension states are another important concept in Asterisk. Extension states are what SIP devices subscribe to for presence information. (SIP presence is discussed in more detail in the section called “SIP Presence”). The state of an extension is determined by checking the state of one or more devices. The list of devices that map to extension states is defined in the Asterisk dialplan, /etc/asterisk/extensions.conf, using a special hint directive. Figure 14.2, “Extension state mappings” shows the mapping between devices, device states, and extension states.

Figure 14.2. Extension state mappings

Extension state mappings

Hints

To define an extension state hint in the dialplan, the keyword hint is used in place of a priority. Here is a simple example dialplan that relates to Figure 14.2, “Extension state mappings”:

[default]

exten => 1234,hint,SIP/phoneA&SIP/phoneB&SIP/phoneC

exten => 5555,hint,DAHDI/1

exten => 31337,hint,MeetMe:31337

Typically, hints are simply defined along with the rest of the extension. This next example adds simple extension entries for what would happen if each of these extensions were called:

[default]

exten => 1234,hint,SIP/phoneA&SIP/phoneB&SIP/phoneC
exten => 1234,1,Dial(SIP/phoneA&SIP/phoneB&SIP/phoneC)

exten => 5555,hint,DAHDI/1
exten => 5555,1,Dial(DAHDI/1)

exten => 31337,hint,MeetMe:31337
exten => 31337,1,MeetMe(31337,dM)

In our example we’ve made a direct correlation between the hint’s extension number and the extension number being dialed, although there is no requirement that that be the case.

Checking Extension States

The easiest way to check the current state of an extension is at the Asterisk CLI. The core show hints command will show you all currently configured hints. Consider the following hint definition:

[phones]

exten => 7001,hint,SIP/0004F2060EB4

When core show hints is executed at the Asterisk CLI, the following output is presented when the device is currently in use:

*CLI> core show hints

    -= Registered Asterisk Dial Plan Hints =-
           7001@phones   : SIP/0004F2060EB4    State:InUse    Watchers  0
----------------
- 1 hints registered

In addition to showing you the state of the extension, the output of core show hints also provides a count of watchers. A watcher is something in Asterisk that has subscribed to receive updates on the state of this extension. If a SIP phone subscribes to the state of an extension, the watcher count will be increased.

Extension state can also be retrieved with a dialplan function, EXTENSION_STATE(). This function operates similarly to the DEVICE_STATE() function described in the preceding section. The following example shows an extension that will print the current state of another extension to the Asterisk console:

exten => 7013,1,Answer()
    same => n,Verbose(3,The state of 7001@phones is ${EXTENSION_STATE(7001@phones)})
    same => n,Hangup()

When this extension is called, this is the verbose message that shows up on the Asterisk console:

    -- The state of 7001@phones is INUSE

The following list includes the possible values that may be returned back from the EXTENSION_STATE() function:

  • UNKNOWN

  • NOT_INUSE

  • INUSE

  • BUSY

  • UNAVAILABLE

  • RINGING

  • RINGINUSE

  • HOLDINUSE

  • ONHOLD