Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

callerid.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * CallerID (and other GR30) Generation support 
00005  * 
00006  * Copyright (C) 1999, Mark Spencer
00007  *
00008  * Mark Spencer <markster@linux-support.net>
00009  *
00010  * This program is free software, distributed under the terms of
00011  * the GNU General Public License.
00012  *
00013  * Includes code and algorithms from the Zapata library.
00014  *
00015  */
00016 
00017 #ifndef _CALLERID_H
00018 #define _CALLERID_H
00019 
00020 #define MAX_CALLERID_SIZE 32000
00021 
00022 #define CID_PRIVATE_NAME      (1 << 0)
00023 #define CID_PRIVATE_NUMBER    (1 << 1)
00024 #define CID_UNKNOWN_NAME      (1 << 2)
00025 #define CID_UNKNOWN_NUMBER    (1 << 3)
00026 
00027 #define AST_LIN2X(a) ((codec == AST_FORMAT_ALAW) ? (AST_LIN2A(a)) : (AST_LIN2MU(a)))
00028 #define AST_XLAW(a) ((codec == AST_FORMAT_ALAW) ? (AST_ALAW(a)) : (AST_MULAW(a)))
00029 
00030 
00031 struct callerid_state;
00032 typedef struct callerid_state CIDSTATE;
00033 
00034 //! CallerID Initialization
00035 /*!
00036  * Initializes the callerid system.  Mostly stuff for inverse FFT
00037  */
00038 extern void callerid_init(void);
00039 
00040 //! Generates a CallerID FSK stream in ulaw format suitable for transmission.
00041 /*!
00042  * \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.
00043  * \param number Use NULL for no number or "P" for "private"
00044  * \param name name to be used
00045  * \param callwaiting callwaiting flag
00046  * \param codec -- either AST_FORMAT_ULAW or AST_FORMAT_ALAW
00047  * This function creates a stream of callerid (a callerid spill) data in ulaw format. It returns the size
00048  * (in bytes) of the data (if it returns a size of 0, there is probably an error)
00049 */
00050 extern int callerid_generate(unsigned char *buf, char *number, char *name, int flags, int callwaiting, int codec);
00051 
00052 //! Create a callerID state machine
00053 /*!
00054  * This function returns a malloc'd instance of the callerid_state data structure.
00055  * Returns a pointer to a malloc'd callerid_state structure, or NULL on error.
00056  */
00057 extern struct callerid_state *callerid_new(void);
00058 
00059 //! Read samples into the state machine.
00060 /*!
00061  * \param cid Which state machine to act upon
00062  * \param buffer containing your samples
00063  * \param samples number of samples contained within the buffer.
00064  * \param codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00065  *
00066  * Send received audio to the Caller*ID demodulator.
00067  * Returns -1 on error, 0 for "needs more samples", 
00068  * and 1 if the CallerID spill reception is complete.
00069  */
00070 extern int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int samples, int codec);
00071 
00072 //! Extract info out of callerID state machine.  Flags are listed above
00073 /*!
00074  * \param cid Callerid state machine to act upon
00075  * \param number Pass the address of a pointer-to-char (will contain the phone number)
00076  * \param name Pass the address of a pointer-to-char (will contain the name)
00077  * \param flags Pass the address of an int variable(will contain the various callerid flags)
00078  *
00079  * This function extracts a callerid string out of a callerid_state state machine.
00080  * If no number is found, *number will be set to NULL.  Likewise for the name.
00081  * Flags can contain any of the following:
00082  * 
00083  * Returns nothing.
00084  */
00085 void callerid_get(struct callerid_state *cid, char **number, char **name, int *flags);
00086 
00087 //! Free a callerID state
00088 /*!
00089  * \param cid This is the callerid_state state machine to free
00090  * This function frees callerid_state cid.
00091  */
00092 extern void callerid_free(struct callerid_state *cid);
00093 
00094 //! Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format)
00095 /*!
00096  * \param buf buffer for output samples. See callerid_generate() for details regarding buffer.
00097  * \param astcid Asterisk format callerid string, taken from the callerid field of asterisk.
00098  * \param codec Asterisk codec (either AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00099  *
00100  * Acts like callerid_generate except uses an asterisk format callerid string.
00101  */
00102 extern int ast_callerid_generate(unsigned char *buf, char *astcid, int codec);
00103 
00104 //! Generate message waiting indicator 
00105 extern int vmwi_generate(unsigned char *buf, int active, int mdmf, int codec);
00106 
00107 //! Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format) but in a format suitable for Call Waiting(tm)'s Caller*ID(tm)
00108 /*!
00109  * See ast_callerid_generate for other details
00110  */
00111 extern int ast_callerid_callwaiting_generate(unsigned char *buf, char *astcid, int codec);
00112 
00113 //! Destructively parse inbuf into name and location (or number)
00114 /*!
00115  * \param inbuf buffer of callerid stream (in audio form) to be parsed. Warning, data in buffer is changed.
00116  * \param name address of a pointer-to-char for the name value of the stream.
00117  * \param location address of a pointer-to-char for the phone number value of the stream.
00118  * Parses callerid stream from inbuf and changes into useable form, outputed in name and location.
00119  * Returns 0 on success, -1 on failure.
00120  */
00121 extern int ast_callerid_parse(char *instr, char **name, char **location);
00122 
00123 //! Generate a CAS (CPE Alert Signal) tone for 'n' samples
00124 /*!
00125  * \param outbuf Allocated buffer for data.  Must be at least 2400 bytes unless no SAS is desired
00126  * \param sas Non-zero if CAS should be preceeded by SAS
00127  * \param len How many samples to generate.
00128  * \param codec Which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
00129  * Returns -1 on error (if len is less than 2400), 0 on success.
00130  */
00131 extern int ast_gen_cas(unsigned char *outbuf, int sas, int len, int codec);
00132 
00133 //! Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s...
00134 /*!
00135  * \param n The number to be stripped/shrunk
00136  * Returns nothing important
00137  */
00138 extern void ast_shrink_phone_number(char *n);
00139 
00140 //! Check if a string consists only of digits.  Returns non-zero if so
00141 /*!
00142  * \param n number to be checked.
00143  * Returns 0 if n is a number, 1 if it's not.
00144  */
00145 extern int ast_isphonenumber(char *n);
00146 
00147 
00148 /*
00149  * Caller*ID and other GR-30 compatible generation
00150  * routines (used by ADSI for example)
00151  */
00152 
00153 extern float cid_dr[4];
00154 extern float cid_di[4];
00155 extern float clidsb;
00156 
00157 static inline float callerid_getcarrier(float *cr, float *ci, int bit)
00158 {
00159    /* Move along.  There's nothing to see here... */
00160    float t;
00161    t = *cr * cid_dr[bit] - *ci * cid_di[bit];
00162    *ci = *cr * cid_di[bit] + *ci * cid_dr[bit];
00163    *cr = t;
00164    
00165    t = 2.0 - (*cr * *cr + *ci * *ci);
00166    *cr *= t;
00167    *ci *= t;
00168    return *cr;
00169 }  
00170 
00171 #define PUT_BYTE(a) do { \
00172    *(buf++) = (a); \
00173    bytes++; \
00174 } while(0)
00175 
00176 #define PUT_AUDIO_SAMPLE(y) do { \
00177    int index = (short)(rint(8192.0 * (y))); \
00178    *(buf++) = AST_LIN2X(index); \
00179    bytes++; \
00180 } while(0)
00181    
00182 #define PUT_CLID_MARKMS do { \
00183    int x; \
00184    for (x=0;x<8;x++) \
00185       PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, 1)); \
00186 } while(0)
00187 
00188 #define PUT_CLID_BAUD(bit) do { \
00189    while(scont < clidsb) { \
00190       PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, bit)); \
00191       scont += 1.0; \
00192    } \
00193    scont -= clidsb; \
00194 } while(0)
00195 
00196 
00197 #define PUT_CLID(byte) do { \
00198    int z; \
00199    unsigned char b = (byte); \
00200    PUT_CLID_BAUD(0);    /* Start bit */ \
00201    for (z=0;z<8;z++) { \
00202       PUT_CLID_BAUD(b & 1); \
00203       b >>= 1; \
00204    } \
00205    PUT_CLID_BAUD(1); /* Stop bit */ \
00206 } while(0); 
00207 
00208 
00209 #endif

Generated on Fri Feb 27 12:19:41 2004 for Asterisk by doxygen 1.3.5