Sat Mar 24 23:26:57 2007

Asterisk developer's documentation


callerid.h File Reference

CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata library. Caller ID names are currently 8 bit characters, propably ISO8859-1, depending on what your channel drivers handle. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define AST_LIN2X(a)   ((codec == AST_FORMAT_ALAW) ? (AST_LIN2A(a)) : (AST_LIN2MU(a)))
#define AST_PRES_ALLOWED   0x00
#define AST_PRES_ALLOWED_NETWORK_NUMBER   AST_PRES_NETWORK_NUMBER + AST_PRES_ALLOWED
#define AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN   AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_ALLOWED
#define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED   AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_ALLOWED
#define AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN   AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_ALLOWED
#define AST_PRES_NETWORK_NUMBER   0x03
#define AST_PRES_NUMBER_NOT_AVAILABLE   AST_PRES_NETWORK_NUMBER + AST_PRES_UNAVAILABLE
#define AST_PRES_NUMBER_TYPE   0x03
#define AST_PRES_PROHIB_NETWORK_NUMBER   AST_PRES_NETWORK_NUMBER + AST_PRES_RESTRICTED
#define AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN   AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_RESTRICTED
#define AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED   AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_RESTRICTED
#define AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN   AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_RESTRICTED
#define AST_PRES_RESERVED   0x60
#define AST_PRES_RESTRICTED   0x20
#define AST_PRES_RESTRICTION   0x60
#define AST_PRES_UNAVAILABLE   0x40
#define AST_PRES_USER_NUMBER_FAILED_SCREEN   0x02
#define AST_PRES_USER_NUMBER_PASSED_SCREEN   0x01
#define AST_PRES_USER_NUMBER_UNSCREENED   0x00
#define AST_XLAW(a)   ((codec == AST_FORMAT_ALAW) ? (AST_ALAW(a)) : (AST_MULAW(a)))
#define CID_PRIVATE_NAME   (1 << 0)
#define CID_PRIVATE_NUMBER   (1 << 1)
#define CID_SIG_BELL   1
#define CID_SIG_DTMF   3
#define CID_SIG_V23   2
#define CID_START_POLARITY   2
#define CID_START_RING   1
#define CID_START_USEHIST   3
#define CID_UNKNOWN_NAME   (1 << 2)
#define CID_UNKNOWN_NUMBER   (1 << 3)
#define MAX_CALLERID_SIZE   32000
#define PUT_AUDIO_SAMPLE(y)
#define PUT_BYTE(a)
#define PUT_CLID(byte)
#define PUT_CLID_BAUD(bit)
#define PUT_CLID_MARKMS

Typedefs

typedef callerid_state CIDSTATE

Functions

int ast_callerid_callwaiting_generate (unsigned char *buf, char *name, char *number, int codec)
 Generate Caller-ID spill but in a format suitable for Call Waiting(tm)'s Caller*ID(tm) See ast_callerid_generate() for other details.
int ast_callerid_generate (unsigned char *buf, char *name, char *number, int codec)
 Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format).
char * ast_callerid_merge (char *buf, int bufsiz, const char *name, const char *num, const char *unknown)
int ast_callerid_parse (char *instr, char **name, char **location)
 Destructively parse inbuf into name and location (or number) Parses callerid stream from inbuf and changes into useable form, outputed in name and location.
int ast_callerid_split (const char *src, char *name, int namelen, char *num, int numlen)
const char * ast_describe_caller_presentation (int data)
 Convert caller ID pres value to explanatory string.
int ast_gen_cas (unsigned char *outbuf, int sas, int len, int codec)
int ast_isphonenumber (char *n)
 Check if a string consists only of digits.
int ast_parse_caller_presentation (const char *data)
 Convert caller ID text code to value used in config file parsing.
void ast_shrink_phone_number (char *n)
 Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s...
int callerid_feed (struct callerid_state *cid, unsigned char *ubuf, int samples, int codec)
 Read samples into the state machine.
void callerid_free (struct callerid_state *cid)
 Free a callerID state.
int callerid_generate (unsigned char *buf, char *number, char *name, int flags, int callwaiting, int codec)
 Generates a CallerID FSK stream in ulaw format suitable for transmission.
void callerid_get (struct callerid_state *cid, char **number, char **name, int *flags)
 Extract info out of callerID state machine. Flags are listed above.
void callerid_get_dtmf (char *cidstring, char *number, int *flags)
static float callerid_getcarrier (float *cr, float *ci, int bit)
void callerid_init (void)
 CallerID Initialization.
callerid_statecallerid_new (int cid_signalling)
 Create a callerID state machine.
int vmwi_generate (unsigned char *buf, int active, int mdmf, int codec)
 Generate message waiting indicator (stutter tone).

Variables

float cid_di [4]
float cid_dr [4]
float clidsb


Detailed Description

CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata library. Caller ID names are currently 8 bit characters, propably ISO8859-1, depending on what your channel drivers handle.

Note:
Asterisk does not currently support SIP utf8 caller ID names or caller ID's.
See also

Definition in file callerid.h.


Define Documentation

#define AST_LIN2X  )     ((codec == AST_FORMAT_ALAW) ? (AST_LIN2A(a)) : (AST_LIN2MU(a)))
 

Definition at line 50 of file callerid.h.

Referenced by gen_tone(), and gen_tones().

#define AST_PRES_ALLOWED   0x00
 

Definition at line 248 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_ALLOWED_NETWORK_NUMBER   AST_PRES_NETWORK_NUMBER + AST_PRES_ALLOWED
 

Definition at line 262 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN   AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_ALLOWED
 

Definition at line 259 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED   AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_ALLOWED
 

Definition at line 253 of file callerid.h.

Referenced by build_rpid(), and cb_events().

#define AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN   AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_ALLOWED
 

Definition at line 256 of file callerid.h.

Referenced by build_rpid(), cb_events(), and check_access().

#define AST_PRES_NETWORK_NUMBER   0x03
 

Definition at line 245 of file callerid.h.

Referenced by misdn_call().

#define AST_PRES_NUMBER_NOT_AVAILABLE   AST_PRES_NETWORK_NUMBER + AST_PRES_UNAVAILABLE
 

Definition at line 277 of file callerid.h.

Referenced by build_rpid(), check_access(), and iax2_call().

#define AST_PRES_NUMBER_TYPE   0x03
 

Definition at line 241 of file callerid.h.

#define AST_PRES_PROHIB_NETWORK_NUMBER   AST_PRES_NETWORK_NUMBER + AST_PRES_RESTRICTED
 

Definition at line 274 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN   AST_PRES_USER_NUMBER_FAILED_SCREEN + AST_PRES_RESTRICTED
 

Definition at line 271 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED   AST_PRES_USER_NUMBER_UNSCREENED + AST_PRES_RESTRICTED
 

Definition at line 265 of file callerid.h.

Referenced by build_rpid(), and get_rpid_num().

#define AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN   AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_RESTRICTED
 

Definition at line 268 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_RESERVED   0x60
 

Definition at line 251 of file callerid.h.

#define AST_PRES_RESTRICTED   0x20
 

Definition at line 249 of file callerid.h.

Referenced by misdn_call().

#define AST_PRES_RESTRICTION   0x60
 

Definition at line 247 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_UNAVAILABLE   0x40
 

Definition at line 250 of file callerid.h.

Referenced by misdn_call().

#define AST_PRES_USER_NUMBER_FAILED_SCREEN   0x02
 

Definition at line 244 of file callerid.h.

Referenced by misdn_call().

#define AST_PRES_USER_NUMBER_PASSED_SCREEN   0x01
 

Definition at line 243 of file callerid.h.

Referenced by misdn_call().

#define AST_PRES_USER_NUMBER_UNSCREENED   0x00
 

Definition at line 242 of file callerid.h.

Referenced by misdn_call().

#define AST_XLAW  )     ((codec == AST_FORMAT_ALAW) ? (AST_ALAW(a)) : (AST_MULAW(a)))
 

Definition at line 51 of file callerid.h.

Referenced by callerid_feed().

#define CID_PRIVATE_NAME   (1 << 0)
 

Definition at line 36 of file callerid.h.

#define CID_PRIVATE_NUMBER   (1 << 1)
 

Definition at line 37 of file callerid.h.

Referenced by callerid_genmsg(), callerid_get(), and callerid_get_dtmf().

#define CID_SIG_BELL   1
 

Definition at line 41 of file callerid.h.

Referenced by get_callerid_ast().

#define CID_SIG_DTMF   3
 

Definition at line 43 of file callerid.h.

#define CID_SIG_V23   2
 

Definition at line 42 of file callerid.h.

Referenced by get_callerid_ast().

#define CID_START_POLARITY   2
 

Definition at line 46 of file callerid.h.

Referenced by handle_init_event().

#define CID_START_RING   1
 

Definition at line 45 of file callerid.h.

#define CID_START_USEHIST   3
 

Definition at line 47 of file callerid.h.

Referenced by ss_thread().

#define CID_UNKNOWN_NAME   (1 << 2)
 

Definition at line 38 of file callerid.h.

Referenced by callerid_genmsg(), callerid_get(), and callerid_new().

#define CID_UNKNOWN_NUMBER   (1 << 3)
 

Definition at line 39 of file callerid.h.

Referenced by callerid_genmsg(), callerid_get(), callerid_get_dtmf(), and callerid_new().

#define MAX_CALLERID_SIZE   32000
 

Definition at line 34 of file callerid.h.

Referenced by send_cwcidspill(), and zt_call().

#define PUT_AUDIO_SAMPLE  ) 
 

Value:

do { \
   int index = (short)(rint(8192.0 * (y))); \
   *(buf++) = AST_LIN2X(index); \
   bytes++; \
} while(0)

Definition at line 207 of file callerid.h.

#define PUT_BYTE  ) 
 

Value:

do { \
   *(buf++) = (a); \
   bytes++; \
} while(0)

Definition at line 202 of file callerid.h.

#define PUT_CLID byte   ) 
 

Definition at line 228 of file callerid.h.

Referenced by adsi_generate(), callerid_generate(), vmwi_generate(), and zt_sendtext().

#define PUT_CLID_BAUD bit   ) 
 

Definition at line 219 of file callerid.h.

#define PUT_CLID_MARKMS
 

Value:

do { \
   int x; \
   for (x=0;x<8;x++) \
      PUT_AUDIO_SAMPLE(callerid_getcarrier(&cr, &ci, 1)); \
} while(0)

Definition at line 213 of file callerid.h.

Referenced by adsi_generate(), callerid_generate(), vmwi_generate(), and zt_sendtext().


Typedef Documentation

typedef struct callerid_state CIDSTATE
 

Definition at line 55 of file callerid.h.


Function Documentation

int ast_callerid_callwaiting_generate unsigned char *  buf,
char *  name,
char *  number,
int  codec
 

Generate Caller-ID spill but in a format suitable for Call Waiting(tm)'s Caller*ID(tm) See ast_callerid_generate() for other details.

Definition at line 698 of file callerid.c.

References __ast_callerid_generate().

Referenced by send_cwcidspill().

00699 {
00700    return __ast_callerid_generate(buf, name, number, 1, codec);
00701 }

int ast_callerid_generate unsigned char *  buf,
char *  name,
char *  number,
int  codec
 

Generate Caller-ID spill from the "callerid" field of asterisk (in e-mail address like format).

Parameters:
buf buffer for output samples. See callerid_generate() for details regarding buffer.
name Caller-ID Name
number Caller-ID Number
codec Asterisk codec (either AST_FORMAT_ALAW or AST_FORMAT_ULAW)
Acts like callerid_generate except uses an asterisk format callerid string.

Definition at line 693 of file callerid.c.

References __ast_callerid_generate().

Referenced by zt_call().

00694 {
00695    return __ast_callerid_generate(buf, name, number, 0, codec);
00696 }

char* ast_callerid_merge char *  buf,
int  bufsiz,
const char *  name,
const char *  num,
const char *  unknown
 

Definition at line 703 of file callerid.c.

Referenced by _sip_show_peer(), iax2_show_peer(), leave_voicemail(), prep_email_sub_vars(), and sip_show_user().

00704 {
00705    if (!unknown)
00706       unknown = "<unknown>";
00707    if (name && num)
00708       snprintf(buf, bufsiz, "\"%s\" <%s>", name, num);
00709    else if (name) 
00710       ast_copy_string(buf, name, bufsiz);
00711    else if (num)
00712       ast_copy_string(buf, num, bufsiz);
00713    else
00714       ast_copy_string(buf, unknown, bufsiz);
00715    return buf;
00716 }

int ast_callerid_parse char *  instr,
char **  name,
char **  location
 

Destructively parse inbuf into name and location (or number) Parses callerid stream from inbuf and changes into useable form, outputed in name and location.

Returns:
returns -1 on failure, otherwise 0

Definition at line 637 of file callerid.c.

References ast_isphonenumber(), ast_shrink_phone_number(), and ast_strlen_zero().

Referenced by action_originate(), advanced_options(), ast_callerid_split(), ast_osp_lookup(), ast_osp_validate(), ast_privacy_check(), ast_privacy_set(), handle_setcallerid(), misdn_new(), play_message_callerid(), rpt_call(), setrdnis_exec(), and write_metadata().

00638 {
00639    char *ns, *ne;
00640    char *ls, *le;
00641    char tmp[256];
00642    /* Try for "name" <location> format or 
00643       name <location> format */
00644    if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
00645       /* Found the location */
00646       *le = '\0';
00647       *ls = '\0';
00648       *location = ls + 1;
00649       if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) {
00650          /* Get name out of quotes */
00651          *ns = '\0';
00652          *ne = '\0';
00653          *name = ns + 1;
00654          return 0;
00655       } else {
00656          /* Just trim off any trailing spaces */
00657          *name = instr;
00658          while(!ast_strlen_zero(instr) && (instr[strlen(instr) - 1] < 33))
00659             instr[strlen(instr) - 1] = '\0';
00660          /* And leading spaces */
00661          *name = ast_skip_blanks(*name);
00662          return 0;
00663       }
00664    } else {
00665       ast_copy_string(tmp, instr, sizeof(tmp));
00666       ast_shrink_phone_number(tmp);
00667       if (ast_isphonenumber(tmp)) {
00668          /* Assume it's just a location */
00669          *name = NULL;
00670          *location = instr;
00671       } else {
00672          /* Assume it's just a name.  Make sure it's not quoted though */
00673          *name = instr;
00674          while(*(*name) && ((*(*name) < 33) || (*(*name) == '\"'))) (*name)++;
00675          ne = *name + strlen(*name) - 1;
00676          while((ne > *name) && ((*ne < 33) || (*ne == '\"'))) { *ne = '\0'; ne--; }
00677          *location = NULL;
00678       }
00679       return 0;
00680    }
00681    return -1;
00682 }

int ast_callerid_split const char *  src,
char *  name,
int  namelen,
char *  num,
int  numlen
 

Definition at line 718 of file callerid.c.

References ast_callerid_parse(), ast_shrink_phone_number(), ast_strdupa, and n.

Referenced by apply_outgoing(), build_device(), build_gateway(), build_peer(), build_user(), callerid_write(), load_module(), monitor_handle_notowned(), setcallerid_exec(), and vpb_new().

00719 {
00720    char *tmp;
00721    char *l = NULL, *n = NULL;
00722    tmp = ast_strdupa(buf);
00723    if (!tmp) {
00724       name[0] = '\0';
00725       num[0] = '\0';
00726       return -1;
00727    }
00728    ast_callerid_parse(tmp, &n, &l);
00729    if (n)
00730       ast_copy_string(name, n, namelen);
00731    else
00732       name[0] = '\0';
00733    if (l) {
00734       ast_shrink_phone_number(l);
00735       ast_copy_string(num, l, numlen);
00736    } else
00737       num[0] = '\0';
00738    return 0;
00739 }

const char* ast_describe_caller_presentation int  data  ) 
 

Convert caller ID pres value to explanatory string.

Parameters:
data value (see callerid.h AST_PRES_ )
Returns:
string for human presentation

Definition at line 778 of file callerid.c.

References pres_types.

Referenced by _sip_show_peer(), ast_set_callerid(), and sip_show_user().

00779 {
00780    int i;
00781 
00782    for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
00783       if (pres_types[i].val == data)
00784          return pres_types[i].description;
00785    }
00786 
00787    return "unknown";
00788 }

int ast_gen_cas unsigned char *  outbuf,
int  sas,
int  len,
int  codec
 

Parameters:
outbuf Allocated buffer for data. Must be at least 2400 bytes unless no SAS is desired
sas Non-zero if CAS should be preceeded by SAS
len How many samples to generate.
codec Which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
Returns:
Returns -1 on error (if len is less than 2400), 0 on success.

Definition at line 233 of file callerid.c.

References casdi1, casdi2, casdr1, casdr2, gen_tone(), gen_tones(), callerid_state::pos, sasdi, and sasdr.

Referenced by __adsi_transmit_messages(), and zt_callwait().

00234 {
00235    int pos = 0;
00236    int saslen=2400;
00237    float cr1 = 1.0;
00238    float ci1 = 0.0;
00239    float cr2 = 1.0;
00240    float ci2 = 0.0;
00241    if (sendsas) {
00242       if (len < saslen)
00243          return -1;
00244       gen_tone(outbuf, saslen, codec, sasdr, sasdi, &cr1, &ci1);
00245       len -= saslen;
00246       pos += saslen;
00247       cr2 = cr1;
00248       ci2 = ci1;
00249    }
00250    gen_tones(outbuf + pos, len, codec, casdr1, casdi1, casdr2, casdi2, &cr1, &ci1, &cr2, &ci2);
00251    return 0;
00252 }

int ast_isphonenumber char *  n  ) 
 

Check if a string consists only of digits.

Returns:
1 if string is valid AST phone number

0 if not

Definition at line 623 of file callerid.c.

References ast_strlen_zero().

Referenced by ast_callerid_parse(), ast_osp_lookup(), and ast_osp_validate().

00624 {
00625    int x;
00626    if (ast_strlen_zero(n))
00627       return 0;
00628    for (x=0;n[x];x++)
00629       if (!strchr("0123456789*#+", n[x]))
00630          return 0;
00631    return 1;
00632 }

int ast_parse_caller_presentation const char *  data  ) 
 

Convert caller ID text code to value used in config file parsing.

Parameters:
data text string
Returns:
value AST_PRES_ from callerid.h

Definition at line 762 of file callerid.c.

References name, and pres_types.

Referenced by build_peer(), build_user(), and setcallerid_pres_exec().

00763 {
00764    int i;
00765 
00766    for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
00767       if (!strcasecmp(pres_types[i].name, data))
00768          return pres_types[i].val;
00769    }
00770 
00771    return -1;
00772 }

void ast_shrink_phone_number char *  n  ) 
 

Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s...

Parameters:
n The number to be stripped/shrunk
Returns:
Returns nothing important

Definition at line 589 of file callerid.c.

Referenced by action_originate(), ast_callerid_parse(), ast_callerid_split(), ast_osp_lookup(), ast_osp_validate(), ast_privacy_check(), ast_privacy_set(), check_access(), check_user_full(), dial_exec_full(), get_callerid_ast(), handle_setcallerid(), pbx_load_module(), setrdnis_exec(), and write_metadata().

00590 {
00591    int x,y=0;
00592    int bracketed=0;
00593    for (x=0;n[x];x++) {
00594       switch(n[x]) {
00595       case '[':
00596          bracketed++;
00597          n[y++] = n[x];
00598          break;
00599       case ']':
00600          bracketed--;
00601          n[y++] = n[x];
00602          break;
00603       case '-':
00604          if (bracketed)
00605             n[y++] = n[x];
00606          break;
00607       case '.':
00608          if (!n[x+1])
00609             n[y++] = n[x];
00610          break;
00611       default:
00612          if (!strchr("( )", n[x]))
00613             n[y++] = n[x];
00614       }
00615    }
00616    n[y] = '\0';
00617 }

int callerid_feed struct callerid_state cid,
unsigned char *  ubuf,
int  samples,
int  codec
 

Read samples into the state machine.

Parameters:
cid Which state machine to act upon
ubuf containing your samples
samples number of samples contained within the buffer.
codec which codec (AST_FORMAT_ALAW or AST_FORMAT_ULAW)
Send received audio to the Caller*ID demodulator.
Returns:
Returns -1 on error, 0 for "needs more samples", and 1 if the CallerID spill reception is complete.

Definition at line 254 of file callerid.c.

References ast_log(), ast_strlen_zero(), AST_XLAW, callerid_state::cksum, fsk_serie(), callerid_state::fskd, callerid_state::len, LOG_ERROR, LOG_NOTICE, LOG_WARNING, malloc, callerid_state::name, callerid_state::number, callerid_state::oldlen, callerid_state::oldstuff, callerid_state::pos, callerid_state::rawdata, callerid_state::sawflag, and callerid_state::type.

Referenced by get_callerid_ast(), and ss_thread().

00255 {
00256    int mylen = len;
00257    int olen;
00258    int b = 'X';
00259    int res;
00260    int x;
00261    short *buf = malloc(2 * len + cid->oldlen);
00262    short *obuf = buf;
00263    if (!buf) {
00264       ast_log(LOG_WARNING, "Out of memory\n");
00265       return -1;
00266    }
00267    memset(buf, 0, 2 * len + cid->oldlen);
00268    memcpy(buf, cid->oldstuff, cid->oldlen);
00269    mylen += cid->oldlen/2;
00270    for (x=0;x<len;x++) 
00271       buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
00272    while(mylen >= 160) {
00273       olen = mylen;
00274       res = fsk_serie(&cid->fskd, buf, &mylen, &b);
00275       if (mylen < 0) {
00276          ast_log(LOG_ERROR, "fsk_serie made mylen < 0 (%d)\n", mylen);
00277          return -1;
00278       }
00279       buf += (olen - mylen);
00280       if (res < 0) {
00281          ast_log(LOG_NOTICE, "fsk_serie failed\n");
00282          return -1;
00283       }
00284       if (res == 1) {
00285          /* Ignore invalid bytes */
00286          if (b > 0xff)
00287             continue;
00288          switch(cid->sawflag) {
00289          case 0: /* Look for flag */
00290             if (b == 'U')
00291                cid->sawflag = 2;
00292             break;
00293          case 2: /* Get lead-in */
00294             if ((b == 0x04) || (b == 0x80)) {
00295                cid->type = b;
00296                cid->sawflag = 3;
00297                cid->cksum = b;
00298             }
00299             break;
00300          case 3:  /* Get length */
00301             /* Not a lead in.  We're ready  */
00302             cid->sawflag = 4;
00303             cid->len = b;
00304             cid->pos = 0;
00305             cid->cksum += b;
00306             break;
00307          case 4: /* Retrieve message */
00308             if (cid->pos >= 128) {
00309                ast_log(LOG_WARNING, "Caller ID too long???\n");
00310                return -1;
00311             }
00312             cid->rawdata[cid->pos++] = b;
00313             cid->len--;
00314             cid->cksum += b;
00315             if (!cid->len) {
00316                cid->rawdata[cid->pos] = '\0';
00317                cid->sawflag = 5;
00318             }
00319             break;
00320          case 5: /* Check checksum */
00321             if (b != (256 - (cid->cksum & 0xff))) {
00322                ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
00323                /* Try again */
00324                cid->sawflag = 0;
00325                break;
00326             }
00327       
00328             cid->number[0] = '\0';
00329             cid->name[0] = '\0';
00330             /* If we get this far we're fine.  */
00331             if (cid->type == 0x80) {
00332                /* MDMF */
00333                /* Go through each element and process */
00334                for (x=0;x< cid->pos;) {
00335                   switch(cid->rawdata[x++]) {
00336                   case 1:
00337                      /* Date */
00338                      break;
00339                   case 2: /* Number */
00340                   case 3: /* Number (for Zebble) */
00341                   case 4: /* Number */
00342                      res = cid->rawdata[x];
00343                      if (res > 32) {
00344                         ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
00345                         res = 32; 
00346                      }
00347                      if (ast_strlen_zero(cid->number)) {
00348                         memcpy(cid->number, cid->rawdata + x + 1, res);
00349                         /* Null terminate */
00350                         cid->number[res] = '\0';
00351                      }
00352                      break;
00353                   case 6: /* Stentor Call Qualifier (ie. Long Distance call) */
00354                      break;
00355                   case 7: /* Name */
00356                   case 8: /* Name */
00357                      res = cid->rawdata[x];
00358                      if (res > 32) {
00359                         ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
00360                         res = 32; 
00361                      }
00362                      memcpy(cid->name, cid->rawdata + x + 1, res);
00363                      cid->name[res] = '\0';
00364                      break;
00365                   case 17: /* UK: Call type, 1=Voice Call, 2=Ringback when free, 129=Message waiting  */
00366                   case 19: /* UK: Network message system status (Number of messages waiting) */
00367                   case 22: /* Something French */
00368                      break;
00369                   default:
00370                      ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]);
00371                   }
00372                   x += cid->rawdata[x];
00373                   x++;
00374                }
00375             } else {
00376                /* SDMF */
00377                ast_copy_string(cid->number, cid->rawdata + 8, sizeof(cid->number));
00378             }
00379             /* Update flags */
00380             cid->flags = 0;
00381             if (!strcmp(cid->number, "P")) {
00382                strcpy(cid->number, "");
00383                cid->flags |= CID_PRIVATE_NUMBER;
00384             } else if (!strcmp(cid->number, "O") || ast_strlen_zero(cid->number)) {
00385                strcpy(cid->number, "");
00386                cid->flags |= CID_UNKNOWN_NUMBER;
00387             }
00388             if (!strcmp(cid->name, "P")) {
00389                strcpy(cid->name, "");
00390                cid->flags |= CID_PRIVATE_NAME;
00391             } else if (!strcmp(cid->name, "O") || ast_strlen_zero(cid->name)) {
00392                strcpy(cid->name, "");
00393                cid->flags |= CID_UNKNOWN_NAME;
00394             }
00395             return 1;
00396             break;
00397          default:
00398             ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
00399          }
00400       }
00401    }
00402    if (mylen) {
00403       memcpy(cid->oldstuff, buf, mylen * 2);
00404       cid->oldlen = mylen * 2;
00405    } else
00406       cid->oldlen = 0;
00407    free(obuf);
00408    return 0;
00409 }

void callerid_free struct callerid_state cid  ) 
 

Free a callerID state.

Parameters:
cid This is the callerid_state state machine to free This function frees callerid_state cid.

Definition at line 411 of file callerid.c.

References free.

Referenced by get_callerid_ast(), and ss_thread().

00412 {
00413    free(cid);
00414 }

int callerid_generate unsigned char *  buf,
char *  number,
char *  name,
int  flags,
int  callwaiting,
int  codec
 

Generates a CallerID FSK stream in ulaw format suitable for transmission.

Parameters:
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.
number Use NULL for no number or "P" for "private"
name name to be used
flags passed flags
callwaiting callwaiting flag
codec -- either AST_FORMAT_ULAW or AST_FORMAT_ALAW This function creates a stream of callerid (a callerid spill) data in ulaw format.
Returns:
It returns the size (in bytes) of the data (if it returns a size of 0, there is probably an error)

Definition at line 547 of file callerid.c.

References callerid_genmsg(), PUT_BYTE, PUT_CLID, and PUT_CLID_MARKMS.

Referenced by __ast_callerid_generate().

00548 {
00549    int bytes=0;
00550    int x, sum;
00551    int len;
00552    /* Initial carriers (real/imaginary) */
00553    float cr = 1.0;
00554    float ci = 0.0;
00555    float scont = 0.0;
00556    char msg[256];
00557    len = callerid_genmsg(msg, sizeof(msg), number, name, flags);
00558    if (!callwaiting) {
00559       /* Wait a half a second */
00560       for (x=0;x<4000;x++)
00561          PUT_BYTE(0x7f);
00562       /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
00563       for (x=0;x<30;x++)
00564          PUT_CLID(0x55);
00565    }
00566    /* Send 150ms of callerid marks */
00567    for (x=0;x<150;x++)
00568       PUT_CLID_MARKMS;
00569    /* Send 0x80 indicating MDMF format */
00570    PUT_CLID(0x80);
00571    /* Put length of whole message */
00572    PUT_CLID(len);
00573    sum = 0x80 + strlen(msg);
00574    /* Put each character of message and update checksum */
00575    for (x=0;x<len; x++) {
00576       PUT_CLID(msg[x]);
00577       sum += msg[x];
00578    }
00579    /* Send 2's compliment of sum */
00580    PUT_CLID(256 - (sum & 255));
00581 
00582    /* Send 50 more ms of marks */
00583    for (x=0;x<50;x++)
00584       PUT_CLID_MARKMS;
00585    
00586    return bytes;
00587 }

void callerid_get struct callerid_state cid,
char **  number,
char **  name,
int *  flags
 

Extract info out of callerID state machine. Flags are listed above.

Parameters:
cid Callerid state machine to act upon
number Pass the address of a pointer-to-char (will contain the phone number)
name Pass the address of a pointer-to-char (will contain the name)
flags Pass the address of an int variable(will contain the various callerid flags)
This function extracts a callerid string out of a callerid_state state machine. If no number is found, *number will be set to NULL. Likewise for the name. Flags can contain any of the following:

Returns:
Returns nothing.

Definition at line 159 of file callerid.c.

References CID_PRIVATE_NUMBER, CID_UNKNOWN_NAME, CID_UNKNOWN_NUMBER, callerid_state::flags, callerid_state::name, and callerid_state::number.

Referenced by get_callerid_ast(), and ss_thread().

00160 {
00161    *flags = cid->flags;
00162    if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NUMBER))
00163       *name = NULL;
00164    else
00165       *name = cid->name;
00166    if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER))
00167       *number = NULL;
00168    else
00169       *number = cid->number;
00170 }

void callerid_get_dtmf char *  cidstring,
char *  number,
int *  flags
 

Parameters:
cidstring The actual transmitted string.
number The cid number is returned here.
flags The cid flags are returned here. This function parses DTMF callerid.

Definition at line 172 of file callerid.c.

References ast_log(), CID_PRIVATE_NUMBER, CID_UNKNOWN_NUMBER, and LOG_DEBUG.

00173 {
00174    int i;
00175    int code;
00176 
00177    /* "Clear" the number-buffer. */
00178    number[0] = 0;
00179 
00180    if (strlen(cidstring) < 2) {
00181       ast_log(LOG_DEBUG, "No cid detected\n");
00182       *flags = CID_UNKNOWN_NUMBER;
00183       return;
00184    }
00185    
00186    /* Detect protocol and special types */
00187    if (cidstring[0] == 'B') {
00188       /* Handle special codes */
00189       code = atoi(&cidstring[1]);
00190       if (code == 0)
00191          *flags = CID_UNKNOWN_NUMBER;
00192       else if (code == 10) 
00193          *flags = CID_PRIVATE_NUMBER;
00194       else
00195          ast_log(LOG_DEBUG, "Unknown DTMF code %d\n", code);
00196    } else if (cidstring[0] == 'D' && cidstring[2] == '#') {
00197       /* .DK special code */
00198       if (cidstring[1] == '1')
00199          *flags = CID_PRIVATE_NUMBER;
00200       if (cidstring[1] == '2' || cidstring[1] == '3')
00201          *flags = CID_UNKNOWN_NUMBER;
00202    } else if (cidstring[0] == 'D' || cidstring[0] == 'A') {
00203       /* "Standard" callerid */
00204       for (i = 1; i < strlen(cidstring); i++ ) {
00205          if (cidstring[i] == 'C' || cidstring[i] == '#')
00206             break;
00207          if (isdigit(cidstring[i]))
00208             number[i-1] = cidstring[i];
00209          else
00210             ast_log(LOG_DEBUG, "Unknown CID digit '%c'\n",
00211                cidstring[i]);
00212       }
00213       number[i-1] = 0;
00214    } else if (isdigit(cidstring[0])) {
00215       /* It begins with a digit, so we parse it as a number and hope
00216        * for the best */
00217       ast_log(LOG_WARNING, "Couldn't detect start-character. CID "
00218          "parsing might be unreliable\n");
00219       for (i = 0; i < strlen(cidstring); i++) {
00220          if (isdigit(cidstring[i]))
00221                                 number[i] = cidstring[i];
00222          else
00223             break;
00224       }
00225       number[i] = 0;
00226    } else {
00227       ast_log(LOG_DEBUG, "Unknown CID protocol, start digit '%c'\n", 
00228          cidstring[0]);
00229       *flags = CID_UNKNOWN_NUMBER;
00230    }
00231 }

static float callerid_getcarrier float *  cr,
float *  ci,
int  bit
[inline, static]
 

Definition at line 188 of file callerid.h.

References cid_di, cid_dr, and t.

00189 {
00190    /* Move along.  There's nothing to see here... */
00191    float t;
00192    t = *cr * cid_dr[bit] - *ci * cid_di[bit];
00193    *ci = *cr * cid_di[bit] + *ci * cid_dr[bit];
00194    *cr = t;
00195    
00196    t = 2.0 - (*cr * *cr + *ci * *ci);
00197    *cr *= t;
00198    *ci *= t;
00199    return *cr;
00200 }  

void callerid_init void   ) 
 

CallerID Initialization.

Initializes the callerid system. Mostly stuff for inverse FFT

Definition at line 113 of file callerid.c.

References CALLERID_MARK, CALLERID_SPACE, CAS_FREQ1, CAS_FREQ2, casdi1, casdi2, casdr1, casdr2, cid_di, cid_dr, SAS_FREQ, sasdi, and sasdr.

00114 {
00115    cid_dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
00116    cid_di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
00117    cid_dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0);
00118    cid_di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0);
00119    sasdr = cos(SAS_FREQ * 2.0 * M_PI / 8000.0);
00120    sasdi = sin(SAS_FREQ * 2.0 * M_PI / 8000.0);
00121    casdr1 = cos(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
00122    casdi1 = sin(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
00123    casdr2 = cos(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
00124    casdi2 = sin(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
00125 }

struct callerid_state* callerid_new int  cid_signalling  ) 
 

Create a callerID state machine.

Parameters:
cid_signalling Type of signalling in use
This function returns a malloc'd instance of the callerid_state data structure.
Returns:
Returns a pointer to a malloc'd callerid_state structure, or NULL on error.

Definition at line 127 of file callerid.c.

References ast_log(), CID_UNKNOWN_NAME, CID_UNKNOWN_NUMBER, LOG_WARNING, and malloc.

Referenced by get_callerid_ast(), and ss_thread().

00128 {
00129    struct callerid_state *cid;
00130    cid = malloc(sizeof(struct callerid_state));
00131    if (cid) {
00132       memset(cid, 0, sizeof(struct callerid_state));
00133       cid->fskd.spb = 7;      /* 1200 baud */
00134       cid->fskd.hdlc = 0;     /* Async */
00135       cid->fskd.nbit = 8;     /* 8 bits */
00136       cid->fskd.nstop = 1; /* 1 stop bit */
00137       cid->fskd.paridad = 0;  /* No parity */
00138       cid->fskd.bw=1;         /* Filter 800 Hz */
00139       if (cid_signalling == 2) { /* v23 signalling */
00140          cid->fskd.f_mark_idx =  4; /* 1300 Hz */
00141          cid->fskd.f_space_idx = 5; /* 2100 Hz */
00142       } else { /* Bell 202 signalling as default */ 
00143          cid->fskd.f_mark_idx =  2; /* 1200 Hz */
00144          cid->fskd.f_space_idx = 3; /* 2200 Hz */
00145       }
00146       cid->fskd.pcola = 0;    /* No clue */
00147       cid->fskd.cont = 0;        /* Digital PLL reset */
00148       cid->fskd.x0 = 0.0;
00149       cid->fskd.state = 0;
00150       memset(cid->name, 0, sizeof(cid->name));
00151       memset(cid->number, 0, sizeof(cid->number));
00152       cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
00153       cid->pos = 0;
00154    } else
00155       ast_log(LOG_WARNING, "Out of memory\n");
00156    return cid;
00157 }

int vmwi_generate unsigned char *  buf,
int  active,
int  mdmf,
int  codec
 

Generate message waiting indicator (stutter tone).

Definition at line 485 of file callerid.c.

References PUT_BYTE, PUT_CLID, and PUT_CLID_MARKMS.

00486 {
00487    unsigned char msg[256];
00488    int len=0;
00489    int sum;
00490    int x;
00491    int bytes = 0;
00492    float cr = 1.0;
00493    float ci = 0.0;
00494    float scont = 0.0;
00495    if (mdmf) {
00496       /* MDMF Message waiting */
00497       msg[len++] = 0x82;
00498       /* Length is 3 */
00499       msg[len++] = 3;
00500       /* IE is "Message Waiting Parameter" */
00501       msg[len++] = 0xb;
00502       /* Length of IE is one */
00503       msg[len++] = 1;
00504       /* Active or not */
00505       if (active)
00506          msg[len++] = 0xff;
00507       else
00508          msg[len++] = 0x00;
00509    } else {
00510       /* SDMF Message waiting */
00511       msg[len++] = 0x6;
00512       /* Length is 3 */
00513       msg[len++] = 3;
00514       if (active) {
00515          msg[len++] = 0x42;
00516          msg[len++] = 0x42;
00517          msg[len++] = 0x42;
00518       } else {
00519          msg[len++] = 0x6f;
00520          msg[len++] = 0x6f;
00521          msg[len++] = 0x6f;
00522       }
00523    }
00524    sum = 0;
00525    for (x=0;x<len;x++)
00526       sum += msg[x];
00527    sum = (256 - (sum & 255));
00528    msg[len++] = sum;
00529    /* Wait a half a second */
00530    for (x=0;x<4000;x++)
00531       PUT_BYTE(0x7f);
00532    /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
00533    for (x=0;x<30;x++)
00534       PUT_CLID(0x55);
00535    /* Send 170ms of callerid marks */
00536    for (x=0;x<170;x++)
00537       PUT_CLID_MARKMS;
00538    for (x=0;x<len;x++) {
00539       PUT_CLID(msg[x]);
00540    }
00541    /* Send 50 more ms of marks */
00542    for (x=0;x<50;x++)
00543       PUT_CLID_MARKMS;
00544    return bytes;
00545 }


Variable Documentation

float cid_di[4]
 

Definition at line 62 of file callerid.c.

Referenced by callerid_getcarrier(), and callerid_init().

float cid_dr[4]
 

Definition at line 62 of file callerid.c.

Referenced by callerid_getcarrier(), and callerid_init().

float clidsb
 

Definition at line 63 of file callerid.c.


Generated on Sat Mar 24 23:26:58 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.6