Asterisk has an internal system that reports states for an extension. By using the dialplan priority -1, also called a hint, a connection can be made from an extension to one or many devices. The state of the extension now depends on the combined state of the devices.
The device state is basically based on the current calls. If the devicestate engine can find a call from or to the device, it's in use.
Some channel drivers implement a callback function for a better level of reporting device states. The SIP channel has a complicated system for this, which is improved by adding call limits to the configuration.
Functions that want to check the status of an extension register themself as a watcher. Watchers in this system can subscribe either to all extensions or just a specific extensions.
For non-device related states, there's an API called devicestate providers. This is an extendible system for delivering state information from outside sources or functions within Asterisk. Currently we have providers for app_meetme.c - the conference bridge - and call parking (metermaids).
There are manly three subscribers to extension states within Asterisk:
The CLI command "show hints" show last known state
The changes are queued and processed by a separate thread. This thread calls the watchers subscribing to status changes for the object. For manager, this results in events. For SIP, NOTIFY requests.
extension. If an extension changes state it checks the hint devices. If there is a hint, the callbacks into device states are checked. The aggregated state is set for the hint and reported back.