Mon Mar 31 07:37:57 2008

Asterisk developer's documentation


callerid.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief CallerID (and other GR30) management and generation
00021  * Includes code and algorithms from the Zapata library.
00022  *
00023  */
00024 
00025 /*!
00026  * \page CID Caller ID names and numbers
00027  *
00028  * Caller ID names are currently 8 bit characters, propably
00029  * ISO8859-1, depending on what your channel drivers handle.
00030  *
00031  * IAX2 and SIP caller ID names are UTF8
00032  * On ISDN Caller ID names are 7 bit, Almost ASCII
00033  * (See http://www.zytrax.com/tech/ia5.html )
00034  *
00035  * \note Asterisk does not currently support SIP utf8 caller ID names or caller ID's.
00036  *
00037  * \par See also
00038  *    \arg \ref callerid.c
00039  *    \arg \ref callerid.h
00040  * \arg \ref Def_CallerPres
00041  */
00042 
00043 #ifndef _ASTERISK_CALLERID_H
00044 #define _ASTERISK_CALLERID_H
00045 
00046 #define MAX_CALLERID_SIZE 32000
00047 
00048 #define CID_PRIVATE_NAME      (1 << 0)
00049 #define CID_PRIVATE_NUMBER    (1 << 1)
00050 #define CID_UNKNOWN_NAME      (1 << 2)
00051 #define CID_UNKNOWN_NUMBER    (1 << 3)
00052 
00053 #define CID_SIG_BELL 1
00054 #define CID_SIG_V23  2
00055 #define CID_SIG_DTMF 3
00056 #define CID_SIG_V23_JP  4
00057 #define CID_SIG_SMDI 5
00058 
00059 #define CID_START_RING  1
00060 #define CID_START_POLARITY 2
00061 
00062 
00063 #define AST_LIN2X(a) ((codec == AST_FORMAT_ALAW) ? (AST_LIN2A(a)) : (AST_LIN2MU(a)))
00064 #define AST_XLAW(a) ((codec == AST_FORMAT_ALAW) ? (AST_ALAW(a)) : (AST_MULAW(a)))
00065 
00066 
00067 struct callerid_state;
00068 typedef struct callerid_state CIDSTATE;
00069 
00070 /*! \brief CallerID Initialization
00071  * \par
00072  * Initializes the callerid system.  Mostly stuff for inverse FFT
00073  */
00074 void callerid_init(void);
00075 
00076 /*! \brief Generates a CallerID FSK stream in ulaw format suitable for transmission.
00077  * \param buf Buffer to use. If "buf" is supplied, it will use that buffer instead of allocating its own.  "buf" must be at least 32000 bytes in size of you want to be sure you don't have an overrun.
00078  * \param number Use NULL for no number or "P" for "private"
00079  * \param name name to be used
00080  * \param flags passed flags
00081  * \param callwaiting callwaiting flag
00082  * \param codec -- either AST_FORMAT_ULAW or AST_FORMAT_ALAW
00083  * This function creates a stream of callerid (a callerid spill) data in ulaw format.
00084  * \return It returns the size
00085  * (in bytes) of the data (if it returns a size of 0, there is probably an error)
00086 */
00087 int callerid_generate(unsigned char *buf, const char *number, const char *name, int flags, int callwaiting, int codec);
00088 
00089 /*! \brief Create a callerID state machine
00090  * \param cid_signalling Type of signalling in use
00091  *
00092  * This function returns a malloc'd instance of the callerid_state data structure.
00093  * \return Returns a pointer to a malloc'd callerid_state structure, or NULL on error.
00094  */
00095 struct callerid_state *callerid_new(int cid_signalling);
00096 
00097 /*! \brief Read samples into the state machine.
00098  * \param cid Which state machine to act upon
00099  * \param ubuf containing your samples
00100  * \param samples number of samples contained within the buffer.
00101  * \param codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00102  *
00103  * Send received audio to the Caller*ID demodulator.
00104  * \return Returns -1 on error, 0 for "needs more samples",
00105  * and 1 if the CallerID spill reception is complete.
00106  */
00107 int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int samples, int codec);
00108 
00109 /*! \brief Read samples into the state machine.
00110  * \param cid Which state machine to act upon
00111  * \param ubuf containing your samples
00112  * \param samples number of samples contained within the buffer.
00113  * \param codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00114  *
00115  * Send received audio to the Caller*ID demodulator (for japanese style lines).
00116  * \return Returns -1 on error, 0 for "needs more samples",
00117  * and 1 if the CallerID spill reception is complete.
00118  */
00119 int callerid_feed_jp(struct callerid_state *cid, unsigned char *ubuf, int samples, int codec);
00120 
00121 /*! \brief Extract info out of callerID state machine.  Flags are listed above
00122  * \param cid Callerid state machine to act upon
00123  * \param number Pass the address of a pointer-to-char (will contain the phone number)
00124  * \param name Pass the address of a pointer-to-char (will contain the name)
00125  * \param flags Pass the address of an int variable(will contain the various callerid flags)
00126  *
00127  * This function extracts a callerid string out of a callerid_state state machine.
00128  * If no number is found, *number will be set to NULL.  Likewise for the name.
00129  * Flags can contain any of the following:
00130  *
00131  * \return Returns nothing.
00132  */
00133 void callerid_get(struct callerid_state *cid, char **number, char **name, int *flags);
00134 
00135 /*! Get and parse DTMF-based callerid  */
00136 /*!
00137  * \param cidstring The actual transmitted string.
00138  * \param number The cid number is returned here.
00139  * \param flags The cid flags are returned here.
00140  * This function parses DTMF callerid.
00141  */
00142 void callerid_get_dtmf(char *cidstring, char *number, int *flags);
00143 
00144 /*! \brief Free a callerID state
00145  * \param cid This is the callerid_state state machine to free
00146  * This function frees callerid_state cid.
00147  */
00148 void callerid_free(struct callerid_state *cid);
00149 
00150 /*! \brief Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format)
00151  * \param buf buffer for output samples. See callerid_generate() for details regarding buffer.
00152  * \param name Caller-ID Name
00153  * \param number Caller-ID Number
00154  * \param codec Asterisk codec (either AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00155  *
00156  * Acts like callerid_generate except uses an asterisk format callerid string.
00157  */
00158 int ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int codec);
00159 
00160 /*! \brief Generate message waiting indicator  (stutter tone) */
00161 int vmwi_generate(unsigned char *buf, int active, int mdmf, int codec);
00162 
00163 /*! \brief Generate Caller-ID spill but in a format suitable for Call Waiting(tm)'s Caller*ID(tm)
00164  * See ast_callerid_generate() for other details
00165  */
00166 int ast_callerid_callwaiting_generate(unsigned char *buf, const char *name, const char *number, int codec);
00167 
00168 /*! \brief Destructively parse inbuf into name and location (or number)
00169  * Parses callerid stream from inbuf and changes into useable form, outputed in name and location.
00170  * \param instr buffer of callerid stream (in audio form) to be parsed. Warning, data in buffer is changed.
00171  * \param name address of a pointer-to-char for the name value of the stream.
00172  * \param location address of a pointer-to-char for the phone number value of the stream.
00173  * \return Returns 0 on success, -1 on failure.
00174  */
00175 int ast_callerid_parse(char *instr, char **name, char **location);
00176 
00177 /*! Generate a CAS (CPE Alert Signal) tone for 'n' samples */
00178 /*!
00179  * \param outbuf Allocated buffer for data.  Must be at least 2400 bytes unless no SAS is desired
00180  * \param sas Non-zero if CAS should be preceeded by SAS
00181  * \param len How many samples to generate.
00182  * \param codec Which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00183  * \return Returns -1 on error (if len is less than 2400), 0 on success.
00184  */
00185 int ast_gen_cas(unsigned char *outbuf, int sas, int len, int codec);
00186 
00187 /*! \brief Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s... */
00188 /*!
00189  * \param n The number to be stripped/shrunk
00190  * \return Returns nothing important
00191  */
00192 void ast_shrink_phone_number(char *n);
00193 
00194 /*! \brief Check if a string consists only of digits and + \#
00195     \param n number to be checked.
00196     \return Returns 0 if n is a number, 1 if it's not.
00197  */
00198 int ast_isphonenumber(const char *n);
00199 
00200 /*! \brief Check if a string consists only of digits and and + \# ( ) - .
00201    (meaning it can be cleaned with ast_shrink_phone_number)
00202     \param exten The extension (or URI) to be checked.
00203     \return Returns 0 if n is a number, 1 if it's not.
00204  */
00205 int ast_is_shrinkable_phonenumber(const char *exten);
00206 
00207 int ast_callerid_split(const char *src, char *name, int namelen, char *num, int numlen);
00208 
00209 char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown);
00210 
00211 /*
00212  * Caller*ID and other GR-30 compatible generation
00213  * routines (used by ADSI for example)
00214  */
00215 
00216 extern float cid_dr[4];
00217 extern float cid_di[4];
00218 extern float clidsb;
00219 
00220 static inline float callerid_getcarrier(float *cr, float *ci, int bit)
00221 {
00222    /* Move along.  There's nothing to see here... */
00223    float t;
00224    t = *cr * cid_dr[bit] - *ci * cid_di[bit];
00225    *ci = *cr * cid_di[bit] + *ci * cid_dr[bit];
00226    *cr = t;
00227 
00228    t = 2.0 - (*cr * *cr + *ci * *ci);
00229    *cr *= t;
00230    *ci *= t;
00231    return *cr;
00232 }
00233 
00234 #define PUT_BYTE(a) do { \
00235    *(buf++) = (a); \
00236    bytes++; \
00237 } while(0)
00238 
00239 #define PUT_AUDIO_SAMPLE(y) do { \
00240    int index = (short)(rint(8192.0 * (y))); \
00241    *(buf++) = AST_LIN2X(index); \
00242    bytes++; \
00243 } while(0)
00244 
00245 #define PUT_CLID_MARKMS do { \
00246    int x; \
00247    for (x=0;x<8;x++) \
00248       PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, 1)); \
00249 } while(0)
00250 
00251 #define PUT_CLID_BAUD(bit) do { \
00252    while(scont < clidsb) { \
00253       PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, bit)); \
00254       scont += 1.0; \
00255    } \
00256    scont -= clidsb; \
00257 } while(0)
00258 
00259 
00260 #define PUT_CLID(byte) do { \
00261    int z; \
00262    unsigned char b = (byte); \
00263    PUT_CLID_BAUD(0);    /* Start bit */ \
00264    for (z=0;z<8;z++) { \
00265       PUT_CLID_BAUD(b & 1); \
00266       b >>= 1; \
00267    } \
00268    PUT_CLID_BAUD(1); /* Stop bit */ \
00269 } while(0)
00270 
00271 /* Various defines and bits for handling PRI- and SS7-type restriction */
00272 
00273 #define AST_PRES_NUMBER_TYPE           0x03
00274 #define AST_PRES_USER_NUMBER_UNSCREENED         0x00
00275 #define AST_PRES_USER_NUMBER_PASSED_SCREEN      0x01
00276 #define AST_PRES_USER_NUMBER_FAILED_SCREEN      0x02
00277 #define AST_PRES_NETWORK_NUMBER           0x03
00278 
00279 #define AST_PRES_RESTRICTION           0x60
00280 #define AST_PRES_ALLOWED            0x00
00281 #define AST_PRES_RESTRICTED            0x20
00282 #define AST_PRES_UNAVAILABLE           0x40
00283 #define AST_PRES_RESERVED           0x60
00284 
00285 #define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED \
00286    AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_ALLOWED
00287 
00288 #define AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN \
00289    AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_ALLOWED
00290 
00291 #define AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN \
00292    AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_ALLOWED
00293 
00294 #define AST_PRES_ALLOWED_NETWORK_NUMBER   \
00295    AST_PRES_NETWORK_NUMBER + AST_PRES_ALLOWED
00296 
00297 #define AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED \
00298    AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_RESTRICTED
00299 
00300 #define AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN \
00301    AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_RESTRICTED
00302 
00303 #define AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN \
00304    AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_RESTRICTED
00305 
00306 #define AST_PRES_PROHIB_NETWORK_NUMBER \
00307    AST_PRES_NETWORK_NUMBER + AST_PRES_RESTRICTED
00308 
00309 #define AST_PRES_NUMBER_NOT_AVAILABLE \
00310    AST_PRES_NETWORK_NUMBER + AST_PRES_UNAVAILABLE
00311 
00312 int ast_parse_caller_presentation(const char *data);
00313 const char *ast_describe_caller_presentation(int data);
00314 
00315 /*! \page Def_CallerPres Caller ID Presentation
00316 
00317    Caller ID presentation values are used to set properties to a
00318    caller ID in PSTN networks, and as RPID value in SIP transactions.
00319 
00320    The following values are available to use:
00321    \arg \b Defined value, text string in config file, explanation
00322 
00323    \arg \b AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "allowed_not_screened", Presentation Allowed, Not Screened,
00324    \arg \b AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", Presentation Allowed, Passed Screen,
00325    \arg \b AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", Presentation Allowed, Failed Screen,
00326    \arg \b AST_PRES_ALLOWED_NETWORK_NUMBER, "allowed", Presentation Allowed, Network Number,
00327    \arg \b AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "prohib_not_screened", Presentation Prohibited, Not Screened,
00328    \arg \b AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", Presentation Prohibited, Passed Screen,
00329    \arg \b AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", Presentation Prohibited, Failed Screen,
00330    \arg \b AST_PRES_PROHIB_NETWORK_NUMBER, "prohib", Presentation Prohibited, Network Number,
00331 
00332    \par References
00333    \arg \ref callerid.h Definitions
00334    \arg \ref callerid.c Functions
00335    \arg \ref CID Caller ID names and numbers
00336 */
00337 
00338 
00339 #endif /* _ASTERISK_CALLERID_H */

Generated on Mon Mar 31 07:37:57 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1