System information

We’re not done writing this extension yet, but let’s pause for a moment and see where
we’re at so far.
When a sales agent sits down at a desk, he logs in by dialing hash (#) plus his own
extension number. In this case we have allowed the 1101 through 1105 extensions to
log in with our pattern match of _#110[1-5]. You could just as easily make this less
restrictive by using _#11XX (allowing 1100 through 1199). This extension uses
func_odbc to perform a lookup with the HOTDESK_INFO() dialplan function. This custom
function (which we will define in the func_odbc.conf file) performs an SQL statement
and returns whatever is retrieved from the database.
We would define the new function HOTDESK_INFO() in func_odbc.conf like so:
[INFO]
prefix=HOTDESK
dsn=asterisk
readsql=SELECT ${ARG1} FROM ast_hotdesk WHERE extension = '${ARG2}'
That’s a lot of stuff in just a few lines. Let’s quickly cover them before we move on.
First of all, the prefix is optional. If you don’t configure the prefix, then Asterisk adds
ODBCto the name of the function (in this case, INFO), which means this function would
become ODBC_INFO(). This is not very descriptive of what the function is doing, so it
can be helpful to assign a prefix that helps to relate your ODBC functions to the tasks
they are performing. We chose HOTDESK, which means that this custom function will be
named HOTDESK_INFO().
The dsn attribute tells Asterisk which connection to use from res_odbc.conf. Since sev-
eral database connections could be configured in res_odbc.conf, we specify which one
to use here. In Figure 16-1, we show the relationship between the various file config-
urations and how they reference down the chain to connect to the database.
The func_odbc.conf.sample file in the Asterisk source contains addi-
tional information about how to handle multiple databases and control
the reading and writing of information to different DSN connections.
Specifically, the readhandle, writehandle, readsql, and writesql argu-
ments will provide you with great flexibility for database integration and
control.
Finally, we define our SQL statement with the readsql attribute. Dialplan functions
have two different formats that they can be called with: one for retrieving information,
and one for setting information. The readsql attribute is used when we call the HOT
DESK_INFO() function with the retrieve format (we could execute a separate SQL state-
ment with the writesql attribute; we’ll discuss the format for that attribute a little bit
later in this chapter).
Reading values from this function would take this format in the dialplan:
exten => s,n,Set(RETURNED_VALUE=${HOTDESK_INFO(status,1101)})
Getting Funky with func_odbc: Hot-Desking | 359