From a2d983628e2f4b07c6ec6d6f297a4f391d3eaea2 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Sat, 9 Jun 2007 00:56:53 +0000 Subject: [PATCH] more callerid code git-svn-id: http://svn.openzap.org/svn/openzap/trunk@239 a93c3328-9c30-0410-af19-c9cd2b2d52af --- libs/openzap/src/include/openzap.h | 10 +++++++- libs/openzap/src/testcid.c | 12 ++++++--- libs/openzap/src/zap_callerid.c | 14 ++++++++--- libs/openzap/src/zap_io.c | 40 ++++++++++++++++++++++++++---- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/libs/openzap/src/include/openzap.h b/libs/openzap/src/include/openzap.h index 0e7887bd20..13683aaa3b 100644 --- a/libs/openzap/src/include/openzap.h +++ b/libs/openzap/src/include/openzap.h @@ -274,6 +274,8 @@ struct zap_fsk_modulator { uint32_t bit_factor; uint32_t bit_accum; uint32_t sample_counter; + int32_t samples_per_bit; + int32_t est_bytes; fsk_modem_types_t modem_type; zap_fsk_data_state_t *fsk_data; zap_fsk_write_sample_t write_sample_callback; @@ -310,7 +312,7 @@ struct zap_channel { zap_channel_state_t last_state; zap_mutex_t *mutex; teletone_dtmf_detect_state_t dtmf_detect; - uint32_t dtmf_delay; + uint32_t buffer_delay; zap_event_t event_header; char last_error[256]; zio_event_cb_t event_callback; @@ -318,6 +320,7 @@ struct zap_channel { uint32_t skip_read_frames; zap_buffer_t *dtmf_buffer; zap_buffer_t *digit_buffer; + zap_buffer_t *fsk_buffer; uint32_t dtmf_on; uint32_t dtmf_off; teletone_generation_session_t tone_session; @@ -404,6 +407,10 @@ struct zap_io_interface { zio_span_next_event_t next_event; }; +zap_size_t zap_fsk_modulator_generate_bit(zap_fsk_modulator_t *fsk_trans, int8_t bit, int16_t *buf, zap_size_t buflen); +int32_t zap_fsk_modulator_generate_carrier_bits(zap_fsk_modulator_t *fsk_trans, uint32_t bits); +void zap_fsk_modulator_generate_chan_sieze(zap_fsk_modulator_t *fsk_trans); +void zap_fsk_modulator_send_data(zap_fsk_modulator_t *fsk_trans); #define zap_fsk_modulator_send_all(_it) zap_fsk_modulator_generate_chan_sieze(_it); \ zap_fsk_modulator_generate_carrier_bits(_it, _it->carrier_bits_start); \ zap_fsk_modulator_send_data(_it); \ @@ -433,6 +440,7 @@ zap_status_t zap_channel_outgoing_call(zap_channel_t *zchan); void zap_channel_rotate_tokens(zap_channel_t *zchan); void zap_channel_clear_detected_tones(zap_channel_t *zchan); void zap_channel_clear_needed_tones(zap_channel_t *zchan); +zap_status_t zap_channel_send_fsk_data(zap_channel_t *zchan, zap_fsk_data_state_t *fsk_data, float db_level); zap_status_t zap_channel_clear_token(zap_channel_t *zchan, int32_t token_id); zap_status_t zap_channel_add_token(zap_channel_t *zchan, char *token); zap_status_t zap_channel_set_state(zap_channel_t *zchan, zap_channel_state_t state); diff --git a/libs/openzap/src/testcid.c b/libs/openzap/src/testcid.c index ec9efd169a..eca951d252 100644 --- a/libs/openzap/src/testcid.c +++ b/libs/openzap/src/testcid.c @@ -2,6 +2,7 @@ struct helper { int fd; + int wrote; }; zap_status_t my_write_sample(int16_t *buf, zap_size_t buflen, void *user_data) @@ -10,6 +11,8 @@ zap_status_t my_write_sample(int16_t *buf, zap_size_t buflen, void *user_data) struct helper *foo = (struct helper *) user_data; write(foo->fd, buf, buflen * 2); + foo->wrote += buflen * 2; + return ZAP_SUCCESS; } int main(int argc, char *argv[]) @@ -24,8 +27,8 @@ int main(int argc, char *argv[]) char str[128] = ""; char fbuf[256]; uint8_t databuf[1024] = ""; - struct helper foo; - + struct helper foo = {0}; + int x, bytes, start_bits = 180, stop_bits = 5, sbits = 300; if (argc < 2) { @@ -43,7 +46,8 @@ int main(int argc, char *argv[]) zap_fsk_data_add_mdmf(&fsk_data, MDMF_DATETIME, "06061234", 8); zap_fsk_data_add_mdmf(&fsk_data, MDMF_PHONE_NUM, "5551212", 7); zap_fsk_data_add_mdmf(&fsk_data, MDMF_PHONE_NAME, "Fred Smith", 10); - //zap_fsk_data_add_mdmf(&fsk_data, MDMF_ALT_ROUTE, url, strlen(url)); + for(x = 0; x < 0; x++) + zap_fsk_data_add_mdmf(&fsk_data, MDMF_ALT_ROUTE, url, strlen(url)); #else zap_fsk_data_add_sdmf(&fsk_data, "06061234", "0"); //zap_fsk_data_add_sdmf(&state, "06061234", "5551212"); @@ -56,6 +60,8 @@ int main(int argc, char *argv[]) zap_fsk_modulator_init(&fsk_trans, FSK_BELL202, 8000, &fsk_data, -14, 180, 5, 300, my_write_sample, &foo); zap_fsk_modulator_send_all((&fsk_trans)); + printf("%d %d %d\n", fsk_data.dlen, foo.wrote, fsk_trans.est_bytes); + if (fd > -1) { close (fd); } diff --git a/libs/openzap/src/zap_callerid.c b/libs/openzap/src/zap_callerid.c index 7b41117670..8c44e11e60 100644 --- a/libs/openzap/src/zap_callerid.c +++ b/libs/openzap/src/zap_callerid.c @@ -223,7 +223,9 @@ int32_t zap_fsk_modulator_generate_carrier_bits(zap_fsk_modulator_t *fsk_trans, for (i = 0; i < bits; i++) { if ((r = zap_fsk_modulator_generate_bit(fsk_trans, bit, fsk_trans->sample_buffer, sizeof(fsk_trans->sample_buffer) / 2))) { - fsk_trans->write_sample_callback(fsk_trans->sample_buffer, r, fsk_trans->user_data); + if (fsk_trans->write_sample_callback(fsk_trans->sample_buffer, r, fsk_trans->user_data) != ZAP_SUCCESS) { + break; + } } else { break; } @@ -241,7 +243,9 @@ void zap_fsk_modulator_generate_chan_sieze(zap_fsk_modulator_t *fsk_trans) for (i = 0; i < fsk_trans->chan_sieze_bits; i++) { if ((r = zap_fsk_modulator_generate_bit(fsk_trans, bit, fsk_trans->sample_buffer, sizeof(fsk_trans->sample_buffer) / 2))) { - fsk_trans->write_sample_callback(fsk_trans->sample_buffer, r, fsk_trans->user_data); + if (fsk_trans->write_sample_callback(fsk_trans->sample_buffer, r, fsk_trans->user_data) != ZAP_SUCCESS) { + break; + } } else { break; } @@ -259,7 +263,9 @@ void zap_fsk_modulator_send_data(zap_fsk_modulator_t *fsk_trans) while((bit = zap_bitstream_get_bit(&fsk_trans->bs)) > -1) { if ((r = zap_fsk_modulator_generate_bit(fsk_trans, bit, fsk_trans->sample_buffer, sizeof(fsk_trans->sample_buffer) / 2))) { - fsk_trans->write_sample_callback(fsk_trans->sample_buffer, r, fsk_trans->user_data); + if (fsk_trans->write_sample_callback(fsk_trans->sample_buffer, r, fsk_trans->user_data) != ZAP_SUCCESS) { + break; + } } else { break; } @@ -283,6 +289,8 @@ zap_status_t zap_fsk_modulator_init(zap_fsk_modulator_t *fsk_trans, teletone_dds_state_set_tone(&fsk_trans->dds, fsk_modem_definitions[fsk_trans->modem_type].freq_space, sample_rate, 0); teletone_dds_state_set_tone(&fsk_trans->dds, fsk_modem_definitions[fsk_trans->modem_type].freq_mark, sample_rate, 1); fsk_trans->bit_factor = (fsk_modem_definitions[fsk_trans->modem_type].baud_rate * ZAP_FSK_MOD_FACTOR) / (float)sample_rate; + fsk_trans->samples_per_bit = (uint32_t) (sample_rate / fsk_modem_definitions[fsk_trans->modem_type].baud_rate); + fsk_trans->est_bytes = ((fsk_data->dlen * 10) + carrier_bits_start + carrier_bits_stop + chan_sieze_bits) * ((fsk_trans->samples_per_bit + 1) * 2); fsk_trans->bit_accum = 0; fsk_trans->fsk_data = fsk_data; teletone_dds_state_set_tx_level(&fsk_trans->dds, db_level); diff --git a/libs/openzap/src/zap_io.c b/libs/openzap/src/zap_io.c index 597979c231..cb9a38bafc 100644 --- a/libs/openzap/src/zap_io.c +++ b/libs/openzap/src/zap_io.c @@ -411,6 +411,27 @@ zap_status_t zap_span_next_event(zap_span_t *span, zap_event_t **event) return ZAP_NOTIMPL; } +static zap_status_t zchan_fsk_write_sample(int16_t *buf, zap_size_t buflen, void *user_data) +{ + zap_channel_t *zchan = (zap_channel_t *) user_data; + zap_buffer_write(zchan->fsk_buffer, buf, buflen * 2); + return ZAP_SUCCESS; +} + +zap_status_t zap_channel_send_fsk_data(zap_channel_t *zchan, zap_fsk_data_state_t *fsk_data, float db_level) +{ + struct zap_fsk_modulator fsk_trans; + + if (!zchan->fsk_buffer) { + zap_buffer_create(&zchan->fsk_buffer, 128, 128, 0); + } + zap_fsk_modulator_init(&fsk_trans, FSK_BELL202, zchan->rate, fsk_data, -14, 180, 5, 180, zchan_fsk_write_sample, zchan); + zap_fsk_modulator_send_all((&fsk_trans)); + zchan->buffer_delay = 2000 / zchan->effective_interval; + return ZAP_SUCCESS; +} + + zap_status_t zap_channel_set_event_callback(zap_channel_t *zchan, zio_event_cb_t event_callback) { zap_mutex_lock(zchan->mutex); @@ -1454,8 +1475,9 @@ zap_status_t zap_channel_write(zap_channel_t *zchan, void *data, zap_size_t data { zap_status_t status = ZAP_FAIL; zio_codec_t codec_func = NULL; - zap_size_t dtmf_blen, max = datasize; - + zap_size_t blen, max = datasize; + zap_buffer_t *buffer = NULL; + assert(zchan != NULL); assert(zchan->zio != NULL); @@ -1488,7 +1510,15 @@ zap_status_t zap_channel_write(zap_channel_t *zchan, void *data, zap_size_t data } } - if (zchan->dtmf_buffer && (dtmf_blen = zap_buffer_inuse(zchan->dtmf_buffer)) && (!zchan->dtmf_delay || --zchan->dtmf_delay == 0)) { + if (!zchan->buffer_delay || --zchan->buffer_delay == 0) { + if (zchan->fsk_buffer && (blen = zap_buffer_inuse(zchan->fsk_buffer))) { + buffer = zchan->fsk_buffer; + } else if (zchan->dtmf_buffer && (blen = zap_buffer_inuse(zchan->dtmf_buffer))) { + buffer = zchan->dtmf_buffer; + } + } + + if (buffer) { zap_size_t dlen = *datalen; uint8_t auxbuf[1024]; zap_size_t len, br; @@ -1497,9 +1527,9 @@ zap_status_t zap_channel_write(zap_channel_t *zchan, void *data, zap_size_t data dlen *= 2; } - len = dtmf_blen > dlen ? dlen : dtmf_blen; + len = blen > dlen ? dlen : blen; - br = zap_buffer_read(zchan->dtmf_buffer, auxbuf, len); + br = zap_buffer_read(buffer, auxbuf, len); if (br < dlen) { memset(auxbuf + br, 0, dlen - br);