Sat Aug 6 00:39:23 2011

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 #define CID_START_DTMF_NOALERT 4
00062 
00063 
00064 #define AST_LIN2X(a) ((codec == AST_FORMAT_ALAW) ? (AST_LIN2A(a)) : (AST_LIN2MU(a)))
00065 #define AST_XLAW(a) ((codec == AST_FORMAT_ALAW) ? (AST_ALAW(a)) : (AST_MULAW(a)))
00066 
00067 
00068 struct callerid_state;
00069 typedef struct callerid_state CIDSTATE;
00070 
00071 /*! \brief CallerID Initialization
00072  * \par
00073  * Initializes the callerid system.  Mostly stuff for inverse FFT
00074  */
00075 void callerid_init(void);
00076 
00077 /*! \brief Generates a CallerID FSK stream in ulaw format suitable for transmission.
00078  * \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.
00079  * \param number Use NULL for no number or "P" for "private"
00080  * \param name name to be used
00081  * \param flags passed flags
00082  * \param callwaiting callwaiting flag
00083  * \param codec -- either AST_FORMAT_ULAW or AST_FORMAT_ALAW
00084  * This function creates a stream of callerid (a callerid spill) data in ulaw format.
00085  * \return It returns the size
00086  * (in bytes) of the data (if it returns a size of 0, there is probably an error)
00087 */
00088 int callerid_generate(unsigned char *buf, const char *number, const char *name, int flags, int callwaiting, int codec);
00089 
00090 /*! \brief Create a callerID state machine
00091  * \param cid_signalling Type of signalling in use
00092  *
00093  * This function returns a malloc'd instance of the callerid_state data structure.
00094  * \return Returns a pointer to a malloc'd callerid_state structure, or NULL on error.
00095  */
00096 struct callerid_state *callerid_new(int cid_signalling);
00097 
00098 /*! \brief Read samples into the state machine.
00099  * \param cid Which state machine to act upon
00100  * \param ubuf containing your samples
00101  * \param samples number of samples contained within the buffer.
00102  * \param codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00103  *
00104  * Send received audio to the Caller*ID demodulator.
00105  * \return Returns -1 on error, 0 for "needs more samples",
00106  * and 1 if the CallerID spill reception is complete.
00107  */
00108 int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int samples, int codec);
00109 
00110 /*! \brief Read samples into the state machine.
00111  * \param cid Which state machine to act upon
00112  * \param ubuf containing your samples
00113  * \param samples number of samples contained within the buffer.
00114  * \param codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00115  *
00116  * Send received audio to the Caller*ID demodulator (for japanese style lines).
00117  * \return Returns -1 on error, 0 for "needs more samples",
00118  * and 1 if the CallerID spill reception is complete.
00119  */
00120 int callerid_feed_jp(struct callerid_state *cid, unsigned char *ubuf, int samples, int codec);
00121 
00122 /*! \brief Extract info out of callerID state machine.  Flags are listed above
00123  * \param cid Callerid state machine to act upon
00124  * \param number Pass the address of a pointer-to-char (will contain the phone number)
00125  * \param name Pass the address of a pointer-to-char (will contain the name)
00126  * \param flags Pass the address of an int variable(will contain the various callerid flags)
00127  *
00128  * This function extracts a callerid string out of a callerid_state state machine.
00129  * If no number is found, *number will be set to NULL.  Likewise for the name.
00130  * Flags can contain any of the following:
00131  *
00132  * \return Returns nothing.
00133  */
00134 void callerid_get(struct callerid_state *cid, char **number, char **name, int *flags);
00135 
00136 /*! Get and parse DTMF-based callerid  */
00137 /*!
00138  * \param cidstring The actual transmitted string.
00139  * \param number The cid number is returned here.
00140  * \param flags The cid flags are returned here.
00141  * This function parses DTMF callerid.
00142  */
00143 void callerid_get_dtmf(char *cidstring, char *number, int *flags);
00144 
00145 /*! \brief Free a callerID state
00146  * \param cid This is the callerid_state state machine to free
00147  * This function frees callerid_state cid.
00148  */
00149 void callerid_free(struct callerid_state *cid);
00150 
00151 /*! \brief Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format)
00152  * \param buf buffer for output samples. See callerid_generate() for details regarding buffer.
00153  * \param name Caller-ID Name
00154  * \param number Caller-ID Number
00155  * \param codec Asterisk codec (either AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00156  *
00157  * Acts like callerid_generate except uses an asterisk format callerid string.
00158  */
00159 int ast_callerid_generate(unsigned char *buf, const char *name, const char *number, int codec);
00160 
00161 /*! \brief Generate message waiting indicator  (stutter tone) */
00162 int ast_callerid_vmwi_generate(unsigned char *buf, int active, int mdmf, int codec);
00163 
00164 /*! \brief Generate Caller-ID spill but in a format suitable for Call Waiting(tm)'s Caller*ID(tm)
00165  * See ast_callerid_generate() for other details
00166  */
00167 int ast_callerid_callwaiting_generate(unsigned char *buf, const char *name, const char *number, int codec);
00168 
00169 /*! \brief Destructively parse inbuf into name and location (or number)
00170  * Parses callerid stream from inbuf and changes into useable form, outputed in name and location.
00171  * \param instr buffer of callerid stream (in audio form) to be parsed. Warning, data in buffer is changed.
00172  * \param name address of a pointer-to-char for the name value of the stream.
00173  * \param location address of a pointer-to-char for the phone number value of the stream.
00174  * \return Returns 0 on success, -1 on failure.
00175  */
00176 int ast_callerid_parse(char *instr, char **name, char **location);
00177 
00178 /*! Generate a CAS (CPE Alert Signal) tone for 'n' samples */
00179 /*!
00180  * \param outbuf Allocated buffer for data.  Must be at least 2400 bytes unless no SAS is desired
00181  * \param sas Non-zero if CAS should be preceeded by SAS
00182  * \param len How many samples to generate.
00183  * \param codec Which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00184  * \return Returns -1 on error (if len is less than 2400), 0 on success.
00185  */
00186 int ast_gen_cas(unsigned char *outbuf, int sas, int len, int codec);
00187 
00188 /*! \brief Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s... */
00189 /*!
00190  * \param n The number to be stripped/shrunk
00191  * \return Returns nothing important
00192  */
00193 void ast_shrink_phone_number(char *n);
00194 
00195 /*! \brief Check if a string consists only of digits and + \#
00196     \param n number to be checked.
00197     \return Returns 0 if n is a number, 1 if it's not.
00198  */
00199 int ast_isphonenumber(const char *n);
00200 
00201 /*! \brief Check if a string consists only of digits and and + \# ( ) - .
00202    (meaning it can be cleaned with ast_shrink_phone_number)
00203     \param exten The extension (or URI) to be checked.
00204     \return Returns 0 if n is a number, 1 if it's not.
00205  */
00206 int ast_is_shrinkable_phonenumber(const char *exten);
00207 
00208 int ast_callerid_split(const char *src, char *name, int namelen, char *num, int numlen);
00209 
00210 char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown);
00211 
00212 /*
00213  * Caller*ID and other GR-30 compatible generation
00214  * routines (used by ADSI for example)
00215  */
00216 
00217 extern float cid_dr[4];
00218 extern float cid_di[4];
00219 extern float clidsb;
00220 
00221 static inline float callerid_getcarrier(float *cr, float *ci, int bit)
00222 {
00223    /* Move along.  There's nothing to see here... */
00224    float t;
00225    t = *cr * cid_dr[bit] - *ci * cid_di[bit];
00226    *ci = *cr * cid_di[bit] + *ci * cid_dr[bit];
00227    *cr = t;
00228 
00229    t = 2.0 - (*cr * *cr + *ci * *ci);
00230    *cr *= t;
00231    *ci *= t;
00232    return *cr;
00233 }
00234 
00235 #define PUT_BYTE(a) do { \
00236    *(buf++) = (a); \
00237    bytes++; \
00238 } while(0)
00239 
00240 #define PUT_AUDIO_SAMPLE(y) do { \
00241    int index = (short)(rint(8192.0 * (y))); \
00242    *(buf++) = AST_LIN2X(index); \
00243    bytes++; \
00244 } while(0)
00245 
00246 #define PUT_CLID_MARKMS do { \
00247    int x; \
00248    for (x=0;x<8;x++) \
00249       PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, 1)); \
00250 } while(0)
00251 
00252 #define PUT_CLID_BAUD(bit) do { \
00253    while(scont < clidsb) { \
00254       PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, bit)); \
00255       scont += 1.0; \
00256    } \
00257    scont -= clidsb; \
00258 } while(0)
00259 
00260 
00261 #define PUT_CLID(byte) do { \
00262    int z; \
00263    unsigned char b = (byte); \
00264    PUT_CLID_BAUD(0);    /* Start bit */ \
00265    for (z=0;z<8;z++) { \
00266       PUT_CLID_BAUD(b & 1); \
00267       b >>= 1; \
00268    } \
00269    PUT_CLID_BAUD(1); /* Stop bit */ \
00270 } while(0)
00271 
00272 /* Various defines and bits for handling PRI- and SS7-type restriction */
00273 
00274 #define AST_PRES_NUMBER_TYPE           0x03
00275 #define AST_PRES_USER_NUMBER_UNSCREENED         0x00
00276 #define AST_PRES_USER_NUMBER_PASSED_SCREEN      0x01
00277 #define AST_PRES_USER_NUMBER_FAILED_SCREEN      0x02
00278 #define AST_PRES_NETWORK_NUMBER           0x03
00279 
00280 #define AST_PRES_RESTRICTION           0x60
00281 #define AST_PRES_ALLOWED            0x00
00282 #define AST_PRES_RESTRICTED            0x20
00283 #define AST_PRES_UNAVAILABLE           0x40
00284 #define AST_PRES_RESERVED           0x60
00285 
00286 #define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED \
00287    AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_ALLOWED
00288 
00289 #define AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN \
00290    AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_ALLOWED
00291 
00292 #define AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN \
00293    AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_ALLOWED
00294 
00295 #define AST_PRES_ALLOWED_NETWORK_NUMBER   \
00296    AST_PRES_NETWORK_NUMBER + AST_PRES_ALLOWED
00297 
00298 #define AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED \
00299    AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_RESTRICTED
00300 
00301 #define AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN \
00302    AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_RESTRICTED
00303 
00304 #define AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN \
00305    AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_RESTRICTED
00306 
00307 #define AST_PRES_PROHIB_NETWORK_NUMBER \
00308    AST_PRES_NETWORK_NUMBER + AST_PRES_RESTRICTED
00309 
00310 #define AST_PRES_NUMBER_NOT_AVAILABLE \
00311    AST_PRES_NETWORK_NUMBER + AST_PRES_UNAVAILABLE
00312 
00313 int ast_parse_caller_presentation(const char *data);
00314 const char *ast_describe_caller_presentation(int data);
00315 
00316 /*! \page Def_CallerPres Caller ID Presentation
00317 
00318    Caller ID presentation values are used to set properties to a
00319    caller ID in PSTN networks, and as RPID value in SIP transactions.
00320 
00321    The following values are available to use:
00322    \arg \b Defined value, text string in config file, explanation
00323 
00324    \arg \b AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "allowed_not_screened", Presentation Allowed, Not Screened,
00325    \arg \b AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", Presentation Allowed, Passed Screen,
00326    \arg \b AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", Presentation Allowed, Failed Screen,
00327    \arg \b AST_PRES_ALLOWED_NETWORK_NUMBER, "allowed", Presentation Allowed, Network Number,
00328    \arg \b AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "prohib_not_screened", Presentation Prohibited, Not Screened,
00329    \arg \b AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", Presentation Prohibited, Passed Screen,
00330    \arg \b AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", Presentation Prohibited, Failed Screen,
00331    \arg \b AST_PRES_PROHIB_NETWORK_NUMBER, "prohib", Presentation Prohibited, Network Number,
00332 
00333    \par References
00334    \arg \ref callerid.h Definitions
00335    \arg \ref callerid.c Functions
00336    \arg \ref CID Caller ID names and numbers
00337 */
00338 
00339 
00340 #endif /* _ASTERISK_CALLERID_H */

Generated on Sat Aug 6 00:39:23 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7