Sat Nov 1 06:28:50 2008

Asterisk developer's documentation


callerid.h File Reference

CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata library. 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_SMDI   5
#define CID_SIG_V23   2
#define CID_SIG_V23_JP   4
#define CID_START_POLARITY   2
#define CID_START_RING   1
#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, const char *name, const 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, const char *name, const 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_is_shrinkable_phonenumber (const char *exten)
 Check if a string consists only of digits and and + # ( ) - . (meaning it can be cleaned with ast_shrink_phone_number).
int ast_isphonenumber (const char *n)
 Check if a string consists only of digits and + #.
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.
int callerid_feed_jp (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, const char *number, const 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.

Definition in file callerid.h.


Define Documentation

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

Definition at line 63 of file callerid.h.

Referenced by gen_tone(), and gen_tones().

#define AST_PRES_ALLOWED   0x00

Definition at line 280 of file callerid.h.

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

#define AST_PRES_ALLOWED_NETWORK_NUMBER   AST_PRES_NETWORK_NUMBER + AST_PRES_ALLOWED

Definition at line 294 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 291 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 285 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN   AST_PRES_USER_NUMBER_PASSED_SCREEN + AST_PRES_ALLOWED

Definition at line 288 of file callerid.h.

Referenced by build_rpid(), and check_access().

#define AST_PRES_NETWORK_NUMBER   0x03

Definition at line 277 of file callerid.h.

Referenced by cb_events(), and update_config().

#define AST_PRES_NUMBER_NOT_AVAILABLE   AST_PRES_NETWORK_NUMBER + AST_PRES_UNAVAILABLE

Definition at line 309 of file callerid.h.

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

#define AST_PRES_NUMBER_TYPE   0x03

Definition at line 273 of file callerid.h.

#define AST_PRES_PROHIB_NETWORK_NUMBER   AST_PRES_NETWORK_NUMBER + AST_PRES_RESTRICTED

Definition at line 306 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 303 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 297 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 300 of file callerid.h.

Referenced by build_rpid().

#define AST_PRES_RESERVED   0x60

Definition at line 283 of file callerid.h.

#define AST_PRES_RESTRICTED   0x20

Definition at line 281 of file callerid.h.

Referenced by cb_events(), and update_config().

#define AST_PRES_RESTRICTION   0x60

Definition at line 279 of file callerid.h.

Referenced by build_rpid(), and initreqprep().

#define AST_PRES_UNAVAILABLE   0x40

Definition at line 282 of file callerid.h.

Referenced by cb_events(), privacy_exec(), and update_config().

#define AST_PRES_USER_NUMBER_FAILED_SCREEN   0x02

Definition at line 276 of file callerid.h.

Referenced by cb_events(), and update_config().

#define AST_PRES_USER_NUMBER_PASSED_SCREEN   0x01

Definition at line 275 of file callerid.h.

Referenced by cb_events(), and update_config().

#define AST_PRES_USER_NUMBER_UNSCREENED   0x00

Definition at line 274 of file callerid.h.

Referenced by cb_events(), and update_config().

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

Definition at line 64 of file callerid.h.

Referenced by callerid_feed(), and callerid_feed_jp().

#define CID_PRIVATE_NAME   (1 << 0)

Definition at line 48 of file callerid.h.

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

#define CID_PRIVATE_NUMBER   (1 << 1)

Definition at line 49 of file callerid.h.

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

#define CID_SIG_BELL   1

Definition at line 53 of file callerid.h.

Referenced by dahdi_chan_conf_default(), and process_dahdi().

#define CID_SIG_DTMF   3

Definition at line 55 of file callerid.h.

Referenced by process_dahdi(), and ss_thread().

#define CID_SIG_SMDI   5

Definition at line 57 of file callerid.h.

Referenced by process_dahdi(), and ss_thread().

#define CID_SIG_V23   2

Definition at line 54 of file callerid.h.

Referenced by process_dahdi(), and ss_thread().

#define CID_SIG_V23_JP   4

Definition at line 56 of file callerid.h.

Referenced by process_dahdi(), and ss_thread().

#define CID_START_POLARITY   2

Definition at line 60 of file callerid.h.

Referenced by handle_init_event(), process_dahdi(), and ss_thread().

#define CID_START_RING   1

Definition at line 59 of file callerid.h.

Referenced by dahdi_chan_conf_default(), and process_dahdi().

#define CID_UNKNOWN_NAME   (1 << 2)

Definition at line 50 of file callerid.h.

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

#define CID_UNKNOWN_NUMBER   (1 << 3)

Definition at line 51 of file callerid.h.

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

#define MAX_CALLERID_SIZE   32000

Definition at line 46 of file callerid.h.

Referenced by dahdi_call(), and send_cwcidspill().

#define PUT_AUDIO_SAMPLE (  ) 

Value:

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

Definition at line 239 of file callerid.h.

#define PUT_BYTE (  ) 

Value:

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

Definition at line 234 of file callerid.h.

#define PUT_CLID ( byte   ) 

Definition at line 260 of file callerid.h.

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

#define PUT_CLID_BAUD ( bit   ) 

Definition at line 251 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 245 of file callerid.h.

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


Typedef Documentation

typedef struct callerid_state CIDSTATE

Definition at line 68 of file callerid.h.


Function Documentation

int ast_callerid_callwaiting_generate ( unsigned char *  buf,
const char *  name,
const 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 1022 of file callerid.c.

References __ast_callerid_generate().

Referenced by send_cwcidspill().

01023 {
01024    return __ast_callerid_generate(buf, name, number, 1, codec);
01025 }

int ast_callerid_generate ( unsigned char *  buf,
const char *  name,
const 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 1017 of file callerid.c.

References __ast_callerid_generate().

Referenced by dahdi_call().

01018 {
01019    return __ast_callerid_generate(buf, name, number, 0, codec);
01020 }

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

Definition at line 1027 of file callerid.c.

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

01028 {
01029    if (!unknown)
01030       unknown = "<unknown>";
01031    if (name && num)
01032       snprintf(buf, bufsiz, "\"%s\" <%s>", name, num);
01033    else if (name) 
01034       ast_copy_string(buf, name, bufsiz);
01035    else if (num)
01036       ast_copy_string(buf, num, bufsiz);
01037    else
01038       ast_copy_string(buf, unknown, bufsiz);
01039    return buf;
01040 }

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:
always returns 0, as the code always returns something.
Note:
XXX 'name' is not parsed consistently e.g. we have input location name " foo bar " <123> 123 ' foo bar ' (with spaces around) " foo bar " NULL 'foo bar' (without spaces around) " foo bar <123>" 123 '" foo bar' The parsing of leading and trailing space/quotes should be more consistent.

Definition at line 970 of file callerid.c.

References ast_isphonenumber(), and ast_shrink_phone_number().

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

00971 {
00972    char *ns, *ne, *ls, *le;
00973 
00974    /* Try "name" <location> format or name <location> format */
00975    if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
00976       *ls = *le = '\0'; /* location found, trim off the brackets */
00977       *location = ls + 1;  /* and this is the result */
00978       if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
00979          *ns = *ne = '\0'; /* trim off the quotes */
00980          *name = ns + 1;      /* and this is the name */
00981       } else { /* no quotes, trim off leading and trailing spaces */
00982          *name = ast_skip_blanks(instr);
00983          ast_trim_blanks(*name);
00984       }
00985    } else { /* no valid brackets */
00986       char tmp[256];
00987 
00988       ast_copy_string(tmp, instr, sizeof(tmp));
00989       ast_shrink_phone_number(tmp);
00990       if (ast_isphonenumber(tmp)) { /* Assume it's just a location */
00991          *name = NULL;
00992          strcpy(instr, tmp); /* safe, because tmp will always be the same size or smaller than instr */
00993          *location = instr;
00994       } else { /* Assume it's just a name. */
00995          *location = NULL;
00996          if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
00997             *ns = *ne = '\0'; /* trim off the quotes */
00998             *name = ns + 1;      /* and this is the name */
00999          } else { /* no quotes, trim off leading and trailing spaces */
01000             *name = ast_skip_blanks(instr);
01001             ast_trim_blanks(*name);
01002          }
01003       }
01004    }
01005    return 0;
01006 }

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

Definition at line 1042 of file callerid.c.

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

Referenced by apply_outgoing(), build_device(), build_gateway(), build_peer(), build_user(), callerid_read(), callerid_write(), disa_exec(), load_module(), process_dahdi(), setcallerid_exec(), store_callerid(), and update_common_options().

01043 {
01044    char *tmp;
01045    char *l = NULL, *n = NULL;
01046 
01047    tmp = ast_strdupa(buf);
01048    ast_callerid_parse(tmp, &n, &l);
01049    if (n)
01050       ast_copy_string(name, n, namelen);
01051    else
01052       name[0] = '\0';
01053    if (l) {
01054       ast_shrink_phone_number(l);
01055       ast_copy_string(num, l, numlen);
01056    } else
01057       num[0] = '\0';
01058    return 0;
01059 }

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 1099 of file callerid.c.

References pres_types.

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

01100 {
01101    int i;
01102 
01103    for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
01104       if (pres_types[i].val == data)
01105          return pres_types[i].description;
01106    }
01107 
01108    return "unknown";
01109 }

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 235 of file callerid.c.

References gen_tone(), gen_tones(), and callerid_state::pos.

Referenced by __adsi_transmit_messages(), and dahdi_callwait().

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

int ast_is_shrinkable_phonenumber ( const char *  exten  ) 

Check if a string consists only of digits and and + # ( ) - . (meaning it can be cleaned with ast_shrink_phone_number).

Returns:
1 if string is valid AST shrinkable phone number

0 if not

Definition at line 955 of file callerid.c.

References ast_is_valid_string().

Referenced by check_user_full().

00956 {
00957    return ast_is_valid_string(exten, "0123456789*#+()-.");
00958 }

int ast_isphonenumber ( const char *  n  ) 

Check if a string consists only of digits and + #.

Returns:
1 if string is valid AST phone number

0 if not

Definition at line 945 of file callerid.c.

References ast_is_valid_string().

Referenced by ast_callerid_parse().

00946 {
00947    return ast_is_valid_string(n, "0123456789*#+");
00948 }

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 1083 of file callerid.c.

References name, and pres_types.

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

01084 {
01085    int i;
01086 
01087    for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
01088       if (!strcasecmp(pres_types[i].name, data))
01089          return pres_types[i].val;
01090    }
01091 
01092    return -1;
01093 }

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 893 of file callerid.c.

Referenced by action_originate(), ast_callerid_parse(), ast_callerid_split(), ast_privacy_check(), ast_privacy_set(), check_access(), check_user_full(), handle_setcallerid(), pbx_load_config(), and write_metadata().

00894 {
00895    int x, y=0;
00896    int bracketed = 0;
00897 
00898    for (x=0; n[x]; x++) {
00899       switch(n[x]) {
00900       case '[':
00901          bracketed++;
00902          n[y++] = n[x];
00903          break;
00904       case ']':
00905          bracketed--;
00906          n[y++] = n[x];
00907          break;
00908       case '-':
00909          if (bracketed)
00910             n[y++] = n[x];
00911          break;
00912       case '.':
00913          if (!n[x+1])
00914             n[y++] = n[x];
00915          break;
00916       default:
00917          if (!strchr("( )", n[x]))
00918             n[y++] = n[x];
00919       }
00920    }
00921    n[y] = '\0';
00922 }

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 540 of file callerid.c.

References ast_calloc, ast_log(), ast_strlen_zero(), AST_XLAW, CID_PRIVATE_NAME, CID_PRIVATE_NUMBER, CID_UNKNOWN_NAME, CID_UNKNOWN_NUMBER, callerid_state::cksum, callerid_state::flags, free, fsk_serie(), callerid_state::fskd, callerid_state::len, LOG_ERROR, LOG_NOTICE, LOG_WARNING, 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 ss_thread().

00541 {
00542    int mylen = len;
00543    int olen;
00544    int b = 'X';
00545    int res;
00546    int x;
00547    short *buf;
00548    short *obuf;
00549 
00550    if (!(buf = ast_calloc(1, 2 * len + cid->oldlen))) {
00551       return -1;
00552    }
00553 
00554    obuf = buf;
00555    memcpy(buf, cid->oldstuff, cid->oldlen);
00556    mylen += cid->oldlen/2;
00557 
00558    for (x=0;x<len;x++) 
00559       buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
00560    while(mylen >= 160) {
00561       olen = mylen;
00562       res = fsk_serie(&cid->fskd, buf, &mylen, &b);
00563       if (mylen < 0) {
00564          ast_log(LOG_ERROR, "No start bit found in fsk data.\n");
00565          free(obuf);
00566          return -1;
00567       }
00568       buf += (olen - mylen);
00569       if (res < 0) {
00570          ast_log(LOG_NOTICE, "fsk_serie failed\n");
00571          free(obuf);
00572          return -1;
00573       }
00574       if (res == 1) {
00575          /* Ignore invalid bytes */
00576          if (b > 0xff)
00577             continue;
00578          switch(cid->sawflag) {
00579          case 0: /* Look for flag */
00580             if (b == 'U')
00581                cid->sawflag = 2;
00582             break;
00583          case 2: /* Get lead-in */
00584             if ((b == 0x04) || (b == 0x80)) {
00585                cid->type = b;
00586                cid->sawflag = 3;
00587                cid->cksum = b;
00588             }
00589             break;
00590          case 3:  /* Get length */
00591             /* Not a lead in.  We're ready  */
00592             cid->sawflag = 4;
00593             cid->len = b;
00594             cid->pos = 0;
00595             cid->cksum += b;
00596             break;
00597          case 4: /* Retrieve message */
00598             if (cid->pos >= 128) {
00599                ast_log(LOG_WARNING, "Caller ID too long???\n");
00600                free(obuf);
00601                return -1;
00602             }
00603             cid->rawdata[cid->pos++] = b;
00604             cid->len--;
00605             cid->cksum += b;
00606             if (!cid->len) {
00607                cid->rawdata[cid->pos] = '\0';
00608                cid->sawflag = 5;
00609             }
00610             break;
00611          case 5: /* Check checksum */
00612             if (b != (256 - (cid->cksum & 0xff))) {
00613                ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
00614                /* Try again */
00615                cid->sawflag = 0;
00616                break;
00617             }
00618       
00619             cid->number[0] = '\0';
00620             cid->name[0] = '\0';
00621             /* If we get this far we're fine.  */
00622             if (cid->type == 0x80) {
00623                /* MDMF */
00624                /* Go through each element and process */
00625                for (x=0;x< cid->pos;) {
00626                   switch(cid->rawdata[x++]) {
00627                   case 1:
00628                      /* Date */
00629                      break;
00630                   case 2: /* Number */
00631                   case 3: /* Number (for Zebble) */
00632                   case 4: /* Number */
00633                      res = cid->rawdata[x];
00634                      if (res > 32) {
00635                         ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
00636                         res = 32; 
00637                      }
00638                      if (ast_strlen_zero(cid->number)) {
00639                         memcpy(cid->number, cid->rawdata + x + 1, res);
00640                         /* Null terminate */
00641                         cid->number[res] = '\0';
00642                      }
00643                      break;
00644                   case 6: /* Stentor Call Qualifier (ie. Long Distance call) */
00645                      break;
00646                   case 7: /* Name */
00647                   case 8: /* Name */
00648                      res = cid->rawdata[x];
00649                      if (res > 32) {
00650                         ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
00651                         res = 32; 
00652                      }
00653                      memcpy(cid->name, cid->rawdata + x + 1, res);
00654                      cid->name[res] = '\0';
00655                      break;
00656                   case 17: /* UK: Call type, 1=Voice Call, 2=Ringback when free, 129=Message waiting  */
00657                   case 19: /* UK: Network message system status (Number of messages waiting) */
00658                   case 22: /* Something French */
00659                      break;
00660                   default:
00661                      ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]);
00662                   }
00663                   res = cid->rawdata[x];
00664                   if (0 > res){  /* Negative offset in the CID Spill */
00665                      ast_log(LOG_NOTICE, "IE %d has bad field length of %d at offset %d\n", cid->rawdata[x-1], cid->rawdata[x], x);
00666                      /* Try again */
00667                      cid->sawflag = 0;
00668                      break;   /* Exit the loop */
00669                   }
00670                   x += cid->rawdata[x];
00671                   x++;
00672                }
00673             } else {
00674                /* SDMF */
00675                ast_copy_string(cid->number, cid->rawdata + 8, sizeof(cid->number));
00676             }
00677             /* Update flags */
00678             cid->flags = 0;
00679             if (!strcmp(cid->number, "P")) {
00680                strcpy(cid->number, "");
00681                cid->flags |= CID_PRIVATE_NUMBER;
00682             } else if (!strcmp(cid->number, "O") || ast_strlen_zero(cid->number)) {
00683                strcpy(cid->number, "");
00684                cid->flags |= CID_UNKNOWN_NUMBER;
00685             }
00686             if (!strcmp(cid->name, "P")) {
00687                strcpy(cid->name, "");
00688                cid->flags |= CID_PRIVATE_NAME;
00689             } else if (!strcmp(cid->name, "O") || ast_strlen_zero(cid->name)) {
00690                strcpy(cid->name, "");
00691                cid->flags |= CID_UNKNOWN_NAME;
00692             }
00693             free(obuf);
00694             return 1;
00695             break;
00696          default:
00697             ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
00698          }
00699       }
00700    }
00701    if (mylen) {
00702       memcpy(cid->oldstuff, buf, mylen * 2);
00703       cid->oldlen = mylen * 2;
00704    } else
00705       cid->oldlen = 0;
00706    free(obuf);
00707    return 0;
00708 }

int callerid_feed_jp ( 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 (for japanese style lines).
Returns:
Returns -1 on error, 0 for "needs more samples", and 1 if the CallerID spill reception is complete.

Definition at line 280 of file callerid.c.

References ast_calloc, ast_log(), AST_XLAW, calc_crc(), CID_UNKNOWN_NUMBER, callerid_state::crc, callerid_state::flags, free, fsk_serie(), callerid_state::fskd, callerid_state::len, LOG_DEBUG, LOG_ERROR, LOG_NOTICE, LOG_WARNING, callerid_state::name, callerid_state::number, callerid_state::oldlen, callerid_state::oldstuff, option_debug, callerid_state::pos, callerid_state::rawdata, callerid_state::sawflag, and callerid_state::skipflag.

Referenced by ss_thread().

00281 {
00282    int mylen = len;
00283    int olen;
00284    int b = 'X';
00285    int b2 ;
00286    int res;
00287    int x;
00288    short *buf;
00289    short *obuf;
00290 
00291    if (!(buf = ast_calloc(1, 2 * len + cid->oldlen))) {
00292       return -1;
00293    }
00294 
00295    obuf = buf;
00296    memcpy(buf, cid->oldstuff, cid->oldlen);
00297    mylen += cid->oldlen/2;
00298 
00299    for (x=0;x<len;x++) 
00300       buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
00301 
00302    while (mylen >= 160) {
00303       b = b2 = 0;
00304       olen = mylen;
00305       res = fsk_serie(&cid->