00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "asterisk.h"
00038
00039 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00040
00041 #include <stdio.h>
00042 #include <pthread.h>
00043 #include <string.h>
00044 #include <sys/socket.h>
00045 #include <sys/time.h>
00046 #include <errno.h>
00047 #include <unistd.h>
00048 #include <stdlib.h>
00049 #include <arpa/inet.h>
00050 #include <fcntl.h>
00051 #include <sys/ioctl.h>
00052 #include <signal.h>
00053 #include <sys/file.h>
00054 #include <semaphore.h>
00055
00056 #include "asterisk/channel.h"
00057 #include "asterisk/config.h"
00058 #include "asterisk/logger.h"
00059 #include "asterisk/module.h"
00060 #include "asterisk/pbx.h"
00061 #include "asterisk/options.h"
00062 #include "asterisk/io.h"
00063 #include "asterisk/frame.h"
00064 #include "asterisk/translate.h"
00065 #include "asterisk/cli.h"
00066 #include "asterisk/musiconhold.h"
00067 #include "asterisk/dsp.h"
00068 #include "asterisk/translate.h"
00069 #include "asterisk/config.h"
00070 #include "asterisk/file.h"
00071 #include "asterisk/callerid.h"
00072 #include "asterisk/indications.h"
00073 #include "asterisk/app.h"
00074 #include "asterisk/features.h"
00075 #include "asterisk/term.h"
00076 #include "asterisk/sched.h"
00077 #include "asterisk/stringfields.h"
00078 #include "asterisk/causes.h"
00079
00080 #include "chan_misdn_config.h"
00081 #include "isdn_lib.h"
00082
00083 char global_tracefile[BUFFERSIZE + 1];
00084
00085 static int g_config_initialized = 0;
00086
00087 struct misdn_jb{
00088 int size;
00089 int upper_threshold;
00090 char *samples, *ok;
00091 int wp,rp;
00092 int state_empty;
00093 int state_full;
00094 int state_buffer;
00095 int bytes_wrote;
00096 ast_mutex_t mutexjb;
00097 };
00098
00099
00100
00101
00102 struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
00103
00104
00105 void misdn_jb_destroy(struct misdn_jb *jb);
00106
00107
00108
00109 int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
00110
00111
00112
00113
00114 int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
00115
00116
00117
00118
00119 ast_mutex_t release_lock;
00120
00121 enum misdn_chan_state {
00122 MISDN_NOTHING=0,
00123 MISDN_WAITING4DIGS,
00124 MISDN_EXTCANTMATCH,
00125 MISDN_INCOMING_SETUP,
00126 MISDN_DIALING,
00127 MISDN_PROGRESS,
00128 MISDN_PROCEEDING,
00129 MISDN_CALLING,
00130 MISDN_CALLING_ACKNOWLEDGE,
00131 MISDN_ALERTING,
00132 MISDN_BUSY,
00133 MISDN_CONNECTED,
00134 MISDN_PRECONNECTED,
00135 MISDN_DISCONNECTED,
00136 MISDN_RELEASED,
00137 MISDN_BRIDGED,
00138 MISDN_CLEANING,
00139 MISDN_HUNGUP_FROM_MISDN,
00140 MISDN_HUNGUP_FROM_AST,
00141 MISDN_HOLDED,
00142 MISDN_HOLD_DISCONNECT,
00143
00144 };
00145
00146 #define ORG_AST 1
00147 #define ORG_MISDN 2
00148
00149 struct hold_info {
00150 int port;
00151 int channel;
00152 };
00153
00154 struct chan_list {
00155
00156 char allowed_bearers[BUFFERSIZE + 1];
00157
00158 enum misdn_chan_state state;
00159 int need_queue_hangup;
00160 int need_hangup;
00161 int need_busy;
00162
00163 int originator;
00164 int noautorespond_on_setup;
00165
00166 int norxtone;
00167 int notxtone;
00168
00169 int toggle_ec;
00170
00171 int incoming_early_audio;
00172
00173 int ignore_dtmf;
00174
00175 int pipe[2];
00176 char ast_rd_buf[4096];
00177 struct ast_frame frame;
00178
00179 int faxdetect;
00180 int faxdetect_timeout;
00181 struct timeval faxdetect_tv;
00182 int faxhandled;
00183
00184 int ast_dsp;
00185
00186 int jb_len;
00187 int jb_upper_threshold;
00188 struct misdn_jb *jb;
00189
00190 struct ast_dsp *dsp;
00191 struct ast_trans_pvt *trans;
00192
00193 struct ast_channel * ast;
00194
00195 int dummy;
00196
00197 struct misdn_bchannel *bc;
00198
00199 struct hold_info hold_info;
00200
00201 unsigned int l3id;
00202 int addr;
00203
00204 char context[BUFFERSIZE];
00205
00206 int zero_read_cnt;
00207 int dropped_frame_cnt;
00208
00209 int far_alerting;
00210
00211 int nttimeout;
00212
00213 int other_pid;
00214 struct chan_list *other_ch;
00215
00216 const struct ind_tone_zone_sound *ts;
00217
00218 int overlap_dial;
00219 int overlap_dial_task;
00220 ast_mutex_t overlap_tv_lock;
00221 struct timeval overlap_tv;
00222
00223 struct chan_list *peer;
00224 struct chan_list *next;
00225 struct chan_list *prev;
00226 struct chan_list *first;
00227 };
00228
00229
00230
00231 void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
00232 void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
00233
00234 struct robin_list {
00235 char *group;
00236 int port;
00237 int channel;
00238 struct robin_list *next;
00239 struct robin_list *prev;
00240 };
00241 static struct robin_list *robin = NULL;
00242
00243
00244
00245 static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame);
00246
00247
00248
00249 static inline void free_robin_list_r (struct robin_list *r)
00250 {
00251 if (r) {
00252 if (r->next)
00253 free_robin_list_r(r->next);
00254 if (r->group)
00255 free(r->group);
00256 free(r);
00257 }
00258 }
00259
00260 static void free_robin_list ( void )
00261 {
00262 free_robin_list_r(robin);
00263 robin = NULL;
00264 }
00265
00266 static struct robin_list* get_robin_position (char *group)
00267 {
00268 struct robin_list *new;
00269 struct robin_list *iter = robin;
00270 for (; iter; iter = iter->next) {
00271 if (!strcasecmp(iter->group, group))
00272 return iter;
00273 }
00274 new = (struct robin_list *) calloc(1, sizeof(struct robin_list));
00275 new->group = strndup(group, strlen(group));
00276 new->port = 0;
00277 new->channel = 0;
00278 if (robin) {
00279 new->next = robin;
00280 robin->prev = new;
00281 }
00282 robin = new;
00283 return robin;
00284 }
00285
00286
00287
00288 static struct sched_context *misdn_tasks = NULL;
00289 static pthread_t misdn_tasks_thread;
00290
00291 static int *misdn_ports;
00292
00293 static void chan_misdn_log(int level, int port, char *tmpl, ...)
00294 __attribute__((format(printf, 3, 4)));
00295
00296 static struct ast_channel *misdn_new(struct chan_list *cl, int state, char *exten, char *callerid, int format, int port, int c);
00297 static void send_digit_to_chan(struct chan_list *cl, char digit );
00298
00299 static void hangup_chan(struct chan_list *ch);
00300 static int pbx_start_chan(struct chan_list *ch);
00301
00302 #define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
00303 #define MISDN_ASTERISK_PVT(ast) 1
00304
00305 #include "asterisk/strings.h"
00306
00307
00308
00309 static const char misdn_type[] = "mISDN";
00310
00311 static int tracing = 0 ;
00312
00313
00314 static int prefformat = AST_FORMAT_ALAW ;
00315
00316 static int *misdn_debug;
00317 static int *misdn_debug_only;
00318 static int max_ports;
00319
00320 static int *misdn_in_calls;
00321 static int *misdn_out_calls;
00322
00323
00324 struct chan_list dummy_cl;
00325
00326 struct chan_list *cl_te=NULL;
00327 ast_mutex_t cl_te_lock;
00328
00329 static enum event_response_e
00330 cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
00331
00332 static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc, struct chan_list *ch);
00333
00334 static void cl_queue_chan(struct chan_list **list, struct chan_list *chan);
00335 static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan);
00336 static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc);
00337 static struct chan_list *find_chan_by_pid(struct chan_list *list, int pid);
00338
00339
00340
00341 static int dialtone_indicate(struct chan_list *cl);
00342 static int hanguptone_indicate(struct chan_list *cl);
00343 static int stop_indicate(struct chan_list *cl);
00344
00345 static int start_bc_tones(struct chan_list *cl);
00346 static int stop_bc_tones(struct chan_list *cl);
00347 static void release_chan(struct misdn_bchannel *bc);
00348
00349 static int misdn_check_l2l1(struct ast_channel *chan, void *data);
00350 static int misdn_set_opt_exec(struct ast_channel *chan, void *data);
00351 static int misdn_facility_exec(struct ast_channel *chan, void *data);
00352
00353 int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
00354
00355
00356 void debug_numplan(int port, int numplan, char *type);
00357
00358
00359 int add_out_calls(int port);
00360 int add_in_calls(int port);
00361
00362
00363 #ifdef MISDN_1_2
00364 static int update_pipeline_config(struct misdn_bchannel *bc);
00365 #else
00366 static int update_ec_config(struct misdn_bchannel *bc);
00367 #endif
00368
00369
00370
00371
00372
00373 static struct chan_list * get_chan_by_ast(struct ast_channel *ast)
00374 {
00375 struct chan_list *tmp;
00376
00377 for (tmp=cl_te; tmp; tmp = tmp->next) {
00378 if ( tmp->ast == ast ) return tmp;
00379 }
00380
00381 return NULL;
00382 }
00383
00384 static struct chan_list * get_chan_by_ast_name(char *name)
00385 {
00386 struct chan_list *tmp;
00387
00388 for (tmp=cl_te; tmp; tmp = tmp->next) {
00389 if ( tmp->ast && strcmp(tmp->ast->name,name) == 0) return tmp;
00390 }
00391
00392 return NULL;
00393 }
00394
00395
00396
00397 struct allowed_bearers {
00398 char *name;
00399 char *display;
00400 int cap;
00401 int deprecated;
00402 };
00403
00404
00405 static const struct allowed_bearers allowed_bearers_array[]= {
00406
00407 { "speech", "Speech", INFO_CAPABILITY_SPEECH, 0 },
00408 { "3_1khz", "3.1KHz Audio", INFO_CAPABILITY_AUDIO_3_1K, 0 },
00409 { "digital_unrestricted", "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 },
00410 { "digital_restricted", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 0 },
00411 { "digital_restriced", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 1 },
00412 { "video", "Video", INFO_CAPABILITY_VIDEO, 0 }
00413 };
00414
00415
00416 static const char *bearer2str(int cap)
00417 {
00418 unsigned index;
00419
00420 for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) {
00421 if (allowed_bearers_array[index].cap == cap) {
00422 return allowed_bearers_array[index].display;
00423 }
00424 }
00425
00426 return "Unknown Bearer";
00427 }
00428
00429
00430 static void print_facility(struct FacParm *fac, struct misdn_bchannel *bc)
00431 {
00432 switch (fac->Function) {
00433 case Fac_CD:
00434 chan_misdn_log(1,bc->port," --> calldeflect to: %s, screened: %s\n", fac->u.CDeflection.DeflectedToNumber,
00435 fac->u.CDeflection.PresentationAllowed ? "yes" : "no");
00436 break;
00437 case Fac_AOCDCurrency:
00438 if (fac->u.AOCDcur.chargeNotAvailable)
00439 chan_misdn_log(1,bc->port," --> AOCD currency: charge not available\n");
00440 else if (fac->u.AOCDcur.freeOfCharge)
00441 chan_misdn_log(1,bc->port," --> AOCD currency: free of charge\n");
00442 else if (fac->u.AOCDchu.billingId >= 0)
00443 chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n",
00444 fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
00445 (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId);
00446 else
00447 chan_misdn_log(1,bc->port," --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n",
00448 fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
00449 (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total");
00450 break;
00451 case Fac_AOCDChargingUnit:
00452 if (fac->u.AOCDchu.chargeNotAvailable)
00453 chan_misdn_log(1,bc->port," --> AOCD charging unit: charge not available\n");
00454 else if (fac->u.AOCDchu.freeOfCharge)
00455 chan_misdn_log(1,bc->port," --> AOCD charging unit: free of charge\n");
00456 else if (fac->u.AOCDchu.billingId >= 0)
00457 chan_misdn_log(1,bc->port," --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s billingId:%d\n",
00458 fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId);
00459 else
00460 chan_misdn_log(1,bc->port," --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s\n",
00461 fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total");
00462 break;
00463 default:
00464 chan_misdn_log(1,bc->port," --> unknown facility\n");
00465 break;
00466 }
00467 }
00468
00469 static void print_bearer(struct misdn_bchannel *bc)
00470 {
00471
00472 chan_misdn_log(2, bc->port, " --> Bearer: %s\n",bearer2str(bc->capability));
00473
00474 switch(bc->law) {
00475 case INFO_CODEC_ALAW:
00476 chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
00477 break;
00478 case INFO_CODEC_ULAW:
00479 chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
00480 break;
00481 }
00482 }
00483
00484 static void export_aoc_vars(int originator, struct ast_channel *ast, struct misdn_bchannel *bc)
00485 {
00486 char buf[128];
00487
00488 if (!ast)
00489 return;
00490
00491 if (originator == ORG_AST) {
00492 ast = ast_bridged_channel(ast);
00493 if (!ast)
00494 return;
00495 }
00496
00497 switch (bc->AOCDtype) {
00498 case Fac_AOCDCurrency:
00499 pbx_builtin_setvar_helper(ast, "AOCD_Type", "currency");
00500 if (bc->AOCD.currency.chargeNotAvailable)
00501 pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
00502 else {
00503 pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
00504 if (bc->AOCD.currency.freeOfCharge)
00505 pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
00506 else {
00507 pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
00508 if (snprintf(buf, sizeof(buf), "%d %s", bc->AOCD.currency.currencyAmount * bc->AOCD.currency.multiplier, bc->AOCD.currency.currency) < sizeof(buf)) {
00509 pbx_builtin_setvar_helper(ast, "AOCD_Amount", buf);
00510 if (bc->AOCD.currency.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.currency.billingId) < sizeof(buf))
00511 pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
00512 }
00513 }
00514 }
00515 break;
00516 case Fac_AOCDChargingUnit:
00517 pbx_builtin_setvar_helper(ast, "AOCD_Type", "charging_unit");
00518 if (bc->AOCD.chargingUnit.chargeNotAvailable)
00519 pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
00520 else {
00521 pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
00522 if (bc->AOCD.chargingUnit.freeOfCharge)
00523 pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
00524 else {
00525 pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
00526 if (snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.recordedUnits) < sizeof(buf)) {
00527 pbx_builtin_setvar_helper(ast, "AOCD_RecordedUnits", buf);
00528 if (bc->AOCD.chargingUnit.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.billingId) < sizeof(buf))
00529 pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
00530 }
00531 }
00532 }
00533 break;
00534 default:
00535 break;
00536 }
00537 }
00538
00539
00540
00541 static void sighandler(int sig)
00542 {}
00543
00544 static void* misdn_tasks_thread_func (void *data)
00545 {
00546 int wait;
00547 struct sigaction sa;
00548
00549 sa.sa_handler = sighandler;
00550 sa.sa_flags = SA_NODEFER;
00551 sigemptyset(&sa.sa_mask);
00552 sigaddset(&sa.sa_mask, SIGUSR1);
00553 sigaction(SIGUSR1, &sa, NULL);
00554
00555 sem_post((sem_t *)data);
00556
00557 while (1) {
00558 wait = ast_sched_wait(misdn_tasks);
00559 if (wait < 0)
00560 wait = 8000;
00561 if (poll(NULL, 0, wait) < 0)
00562 chan_misdn_log(4, 0, "Waking up misdn_tasks thread\n");
00563 ast_sched_runq(misdn_tasks);
00564 }
00565 return NULL;
00566 }
00567
00568 static void misdn_tasks_init (void)
00569 {
00570 sem_t blocker;
00571 int i = 5;
00572
00573 if (sem_init(&blocker, 0, 0)) {
00574 perror("chan_misdn: Failed to initialize semaphore!");
00575 exit(1);
00576 }
00577
00578 chan_misdn_log(4, 0, "Starting misdn_tasks thread\n");
00579
00580 misdn_tasks = sched_context_create();
00581 pthread_create(&misdn_tasks_thread, NULL, misdn_tasks_thread_func, &blocker);
00582
00583 while (sem_wait(&blocker) && --i);
00584 sem_destroy(&blocker);
00585 }
00586
00587 static void misdn_tasks_destroy (void)
00588 {
00589 if (misdn_tasks) {
00590 chan_misdn_log(4, 0, "Killing misdn_tasks thread\n");
00591 if ( pthread_cancel(misdn_tasks_thread) == 0 ) {
00592 cb_log(4, 0, "Joining misdn_tasks thread\n");
00593 pthread_join(misdn_tasks_thread, NULL);
00594 }
00595 sched_context_destroy(misdn_tasks);
00596 }
00597 }
00598
00599 static inline void misdn_tasks_wakeup (void)
00600 {
00601 pthread_kill(misdn_tasks_thread, SIGUSR1);
00602 }
00603
00604 static inline int _misdn_tasks_add_variable (int timeout, ast_sched_cb callback, const void *data, int variable)
00605 {
00606 int task_id;
00607
00608 if (!misdn_tasks) {
00609 misdn_tasks_init();
00610 }
00611 task_id = ast_sched_add_variable(misdn_tasks, timeout, callback, data, variable);
00612 misdn_tasks_wakeup();
00613
00614 return task_id;
00615 }
00616
00617 static int misdn_tasks_add (int timeout, ast_sched_cb callback, const void *data)
00618 {
00619 return _misdn_tasks_add_variable(timeout, callback, data, 0);
00620 }
00621
00622 static int misdn_tasks_add_variable (int timeout, ast_sched_cb callback, const void *data)
00623 {
00624 return _misdn_tasks_add_variable(timeout, callback, data, 1);
00625 }
00626
00627 static void misdn_tasks_remove (int task_id)
00628 {
00629 AST_SCHED_DEL(misdn_tasks, task_id);
00630 }
00631
00632 static int misdn_l1_task (const void *data)
00633 {
00634 misdn_lib_isdn_l1watcher(*(int *)data);
00635 chan_misdn_log(5, *(int *)data, "L1watcher timeout\n");
00636 return 1;
00637 }
00638
00639 static int misdn_overlap_dial_task (const void *data)
00640 {
00641 struct timeval tv_end, tv_now;
00642 int diff;
00643 struct chan_list *ch = (struct chan_list *)data;
00644
00645 chan_misdn_log(4, ch->bc->port, "overlap dial task, chan_state: %d\n", ch->state);
00646
00647 if (ch->state != MISDN_WAITING4DIGS) {
00648 ch->overlap_dial_task = -1;
00649 return 0;
00650 }
00651
00652 ast_mutex_lock(&ch->overlap_tv_lock);
00653 tv_end = ch->overlap_tv;
00654 ast_mutex_unlock(&ch->overlap_tv_lock);
00655
00656 tv_end.tv_sec += ch->overlap_dial;
00657 tv_now = ast_tvnow();
00658
00659 diff = ast_tvdiff_ms(tv_end, tv_now);
00660
00661 if (diff <= 100) {
00662 char *dad=ch->bc->dad, sexten[]="s";
00663
00664 stop_indicate(ch);
00665
00666 if (ast_strlen_zero(ch->bc->dad)) {
00667 dad=sexten;
00668 strcpy(ch->ast->exten, sexten);
00669 }
00670
00671 if (ast_exists_extension(ch->ast, ch->context, dad, 1, ch->bc->oad)) {
00672 ch->state=MISDN_DIALING;
00673 if (pbx_start_chan(ch) < 0) {
00674 chan_misdn_log(-1, ch->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
00675 goto misdn_overlap_dial_task_disconnect;
00676 }
00677 } else {
00678 misdn_overlap_dial_task_disconnect:
00679 hanguptone_indicate(ch);
00680 ch->bc->out_cause = AST_CAUSE_UNALLOCATED;
00681 ch->state=MISDN_CLEANING;
00682 misdn_lib_send_event(ch->bc, EVENT_DISCONNECT);
00683 }
00684 ch->overlap_dial_task = -1;
00685 return 0;
00686 } else
00687 return diff;
00688 }
00689
00690 static void send_digit_to_chan(struct chan_list *cl, char digit )
00691 {
00692 static const char* dtmf_tones[] = {
00693 "!941+1336/100,!0/100",
00694 "!697+1209/100,!0/100",
00695 "!697+1336/100,!0/100",
00696 "!697+1477/100,!0/100",
00697 "!770+1209/100,!0/100",
00698 "!770+1336/100,!0/100",
00699 "!770+1477/100,!0/100",
00700 "!852+1209/100,!0/100",
00701 "!852+1336/100,!0/100",
00702 "!852+1477/100,!0/100",
00703 "!697+1633/100,!0/100",
00704 "!770+1633/100,!0/100",
00705 "!852+1633/100,!0/100",
00706 "!941+1633/100,!0/100",
00707 "!941+1209/100,!0/100",
00708 "!941+1477/100,!0/100" };
00709 struct ast_channel *chan=cl->ast;
00710
00711 if (digit >= '0' && digit <='9')
00712 ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
00713 else if (digit >= 'A' && digit <= 'D')
00714 ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
00715 else if (digit == '*')
00716 ast_playtones_start(chan,0,dtmf_tones[14], 0);
00717 else if (digit == '#')
00718 ast_playtones_start(chan,0,dtmf_tones[15], 0);
00719 else {
00720
00721 ast_log(LOG_DEBUG, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
00722 }
00723 }
00724
00725
00726 static int misdn_set_debug(int fd, int argc, char *argv[])
00727 {
00728 int level;
00729
00730 if (argc != 4 && argc != 5 && argc != 6 && argc != 7)
00731 return RESULT_SHOWUSAGE;
00732
00733 level = atoi(argv[3]);
00734
00735 switch (argc) {
00736 case 4:
00737 case 5:
00738 {
00739 int i;
00740 int only = 0;
00741 if (argc == 5) {
00742 if (strncasecmp(argv[4], "only", strlen(argv[4])))
00743 return RESULT_SHOWUSAGE;
00744 else
00745 only = 1;
00746 }
00747
00748 for (i = 0; i <= max_ports; i++) {
00749 misdn_debug[i] = level;
00750 misdn_debug_only[i] = only;
00751 }
00752 ast_cli(fd, "changing debug level for all ports to %d%s\n",misdn_debug[0], only?" (only)":"");
00753 }
00754 break;
00755 case 6:
00756 case 7:
00757 {
00758 int port;
00759 if (strncasecmp(argv[4], "port", strlen(argv[4])))
00760 return RESULT_SHOWUSAGE;
00761 port = atoi(argv[5]);
00762 if (port <= 0 || port > max_ports) {
00763 switch (max_ports) {
00764 case 0:
00765 ast_cli(fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
00766 break;
00767 case 1:
00768 ast_cli(fd, "port number not valid! only port 1 is available.\n");
00769 break;
00770 default:
00771 ast_cli(fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
00772 }
00773 return 0;
00774 }
00775 if (argc == 7) {
00776 if (strncasecmp(argv[6], "only", strlen(argv[6])))
00777 return RESULT_SHOWUSAGE;
00778 else
00779 misdn_debug_only[port] = 1;
00780 } else
00781 misdn_debug_only[port] = 0;
00782 misdn_debug[port] = level;
00783 ast_cli(fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port]?" (only)":"", port);
00784 }
00785 }
00786 return 0;
00787 }
00788
00789 static int misdn_set_crypt_debug(int fd, int argc, char *argv[])
00790 {
00791 if (argc != 5) return RESULT_SHOWUSAGE;
00792
00793 return 0;
00794 }
00795
00796 static int misdn_port_block(int fd, int argc, char *argv[])
00797 {
00798 int port;
00799
00800 if (argc != 4)
00801 return RESULT_SHOWUSAGE;
00802
00803 port = atoi(argv[3]);
00804
00805 misdn_lib_port_block(port);
00806
00807 return 0;
00808 }
00809
00810 static int misdn_port_unblock(int fd, int argc, char *argv[])
00811 {
00812 int port;
00813
00814 if (argc != 4)
00815 return RESULT_SHOWUSAGE;
00816
00817 port = atoi(argv[3]);
00818
00819 misdn_lib_port_unblock(port);
00820
00821 return 0;
00822 }
00823
00824
00825 static int misdn_restart_port (int fd, int argc, char *argv[])
00826 {
00827 int port;
00828
00829 if (argc != 4)
00830 return RESULT_SHOWUSAGE;
00831
00832 port = atoi(argv[3]);
00833
00834 misdn_lib_port_restart(port);
00835
00836 return 0;
00837 }
00838
00839 static int misdn_restart_pid (int fd, int argc, char *argv[])
00840 {
00841 int pid;
00842
00843 if (argc != 4)
00844 return RESULT_SHOWUSAGE;
00845
00846 pid = atoi(argv[3]);
00847
00848 misdn_lib_pid_restart(pid);
00849
00850 return 0;
00851 }
00852
00853 static int misdn_port_up (int fd, int argc, char *argv[])
00854 {
00855 int port;
00856
00857 if (argc != 4)
00858 return RESULT_SHOWUSAGE;
00859
00860 port = atoi(argv[3]);
00861
00862 misdn_lib_get_port_up(port);
00863
00864 return 0;
00865 }
00866
00867 static int misdn_port_down (int fd, int argc, char *argv[])
00868 {
00869 int port;
00870
00871 if (argc != 4)
00872 return RESULT_SHOWUSAGE;
00873
00874 port = atoi(argv[3]);
00875
00876 misdn_lib_get_port_down(port);
00877
00878 return 0;
00879 }
00880
00881 static inline void show_config_description (int fd, enum misdn_cfg_elements elem)
00882 {
00883 char section[BUFFERSIZE];
00884 char name[BUFFERSIZE];
00885 char desc[BUFFERSIZE];
00886 char def[BUFFERSIZE];
00887 char tmp[BUFFERSIZE];
00888
00889 misdn_cfg_get_name(elem, tmp, sizeof(tmp));
00890 term_color(name, tmp, COLOR_BRWHITE, 0, sizeof(tmp));
00891 misdn_cfg_get_desc(elem, desc, sizeof(desc), def, sizeof(def));
00892
00893 if (elem < MISDN_CFG_LAST)
00894 term_color(section, "PORTS SECTION", COLOR_YELLOW, 0, sizeof(section));
00895 else
00896 term_color(section, "GENERAL SECTION", COLOR_YELLOW, 0, sizeof(section));
00897
00898 if (*def)
00899 ast_cli(fd, "[%s] %s (Default: %s)\n\t%s\n", section, name, def, desc);
00900 else
00901 ast_cli(fd, "[%s] %s\n\t%s\n", section, name, desc);
00902 }
00903
00904 static int misdn_show_config (int fd, int argc, char *argv[])
00905 {
00906 char buffer[BUFFERSIZE];
00907 enum misdn_cfg_elements elem;
00908 int linebreak;
00909 int onlyport = -1;
00910 int ok = 0;
00911
00912 if (argc >= 4) {
00913 if (!strcmp(argv[3], "description")) {
00914 if (argc == 5) {
00915 enum misdn_cfg_elements elem = misdn_cfg_get_elem(argv[4]);
00916 if (elem == MISDN_CFG_FIRST)
00917 ast_cli(fd, "Unknown element: %s\n", argv[4]);
00918 else
00919 show_config_description(fd, elem);
00920 return 0;
00921 }
00922 return RESULT_SHOWUSAGE;
00923 }
00924 if (!strcmp(argv[3], "descriptions")) {
00925 if ((argc == 4) || ((argc == 5) && !strcmp(argv[4], "general"))) {
00926 for (elem = MISDN_GEN_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
00927 show_config_description(fd, elem);
00928 ast_cli(fd, "\n");
00929 }
00930 ok = 1;
00931 }
00932 if ((argc == 4) || ((argc == 5) && !strcmp(argv[4], "ports"))) {
00933 for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_CFG_LAST - 1 ; ++elem) {
00934 show_config_description(fd, elem);
00935 ast_cli(fd, "\n");
00936 }
00937 ok = 1;
00938 }
00939 return ok ? 0 : RESULT_SHOWUSAGE;
00940 }
00941 if (!sscanf(argv[3], "%d", &onlyport) || onlyport < 0) {
00942 ast_cli(fd, "Unknown option: %s\n", argv[3]);
00943 return RESULT_SHOWUSAGE;
00944 }
00945 }
00946
00947 if (argc == 3 || onlyport == 0) {
00948 ast_cli(fd, "Misdn General-Config:\n");
00949 for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
00950 misdn_cfg_get_config_string(0, elem, buffer, BUFFERSIZE);
00951 ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
00952 }
00953 ast_cli(fd, "\n");
00954 }
00955
00956 if (onlyport < 0) {
00957 int port = misdn_cfg_get_next_port(0);
00958 for (; port > 0; port = misdn_cfg_get_next_port(port)) {
00959 ast_cli(fd, "\n[PORT %d]\n", port);
00960 for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
00961 misdn_cfg_get_config_string(port, elem, buffer, BUFFERSIZE);
00962 ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
00963 }
00964 ast_cli(fd, "\n");
00965 }
00966 }
00967
00968 if (onlyport > 0) {
00969 if (misdn_cfg_is_port_valid(onlyport)) {
00970 ast_cli(fd, "[PORT %d]\n", onlyport);
00971 for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
00972 misdn_cfg_get_config_string(onlyport, elem, buffer, BUFFERSIZE);
00973 ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
00974 }
00975 ast_cli(fd, "\n");
00976 } else {
00977 ast_cli(fd, "Port %d is not active!\n", onlyport);
00978 }
00979 }
00980
00981 return 0;
00982 }
00983
00984 struct state_struct {
00985 enum misdn_chan_state state;
00986 char txt[255];
00987 };
00988
00989 static struct state_struct state_array[] = {
00990 {MISDN_NOTHING,"NOTHING"},
00991 {MISDN_WAITING4DIGS,"WAITING4DIGS"},
00992 {MISDN_EXTCANTMATCH,"EXTCANTMATCH"},
00993 {MISDN_INCOMING_SETUP,"INCOMING SETUP"},
00994 {MISDN_DIALING,"DIALING"},
00995 {MISDN_PROGRESS,"PROGRESS"},
00996 {MISDN_PROCEEDING,"PROCEEDING"},
00997 {MISDN_CALLING,"CALLING"},
00998 {MISDN_CALLING_ACKNOWLEDGE,"CALLING_ACKNOWLEDGE"},
00999 {MISDN_ALERTING,"ALERTING"},
01000 {MISDN_BUSY,"BUSY"},
01001 {MISDN_CONNECTED,"CONNECTED"},
01002 {MISDN_PRECONNECTED,"PRECONNECTED"},
01003 {MISDN_DISCONNECTED,"DISCONNECTED"},
01004 {MISDN_RELEASED,"RELEASED"},
01005 {MISDN_BRIDGED,"BRIDGED"},
01006 {MISDN_CLEANING,"CLEANING"},
01007 {MISDN_HUNGUP_FROM_MISDN,"HUNGUP_FROM_MISDN"},
01008 {MISDN_HOLDED,"HOLDED"},
01009 {MISDN_HOLD_DISCONNECT,"HOLD_DISCONNECT"},
01010 {MISDN_HUNGUP_FROM_AST,"HUNGUP_FROM_AST"}
01011 };
01012
01013 static const char *misdn_get_ch_state(struct chan_list *p)
01014 {
01015 int i;
01016 static char state[8];
01017
01018 if( !p) return NULL;
01019
01020 for (i = 0; i < sizeof(state_array) / sizeof(struct state_struct); i++) {
01021 if (state_array[i].state == p->state)
01022 return state_array[i].txt;
01023 }
01024
01025 snprintf(state, sizeof(state), "%d", p->state) ;
01026
01027 return state;
01028 }
01029
01030
01031
01032 static void reload_config(void)
01033 {
01034 int i, cfg_debug;
01035
01036 if (!g_config_initialized) {
01037 ast_log(LOG_WARNING, "chan_misdn is not initialized properly, still reloading ?\n");
01038 return ;
01039 }
01040
01041 free_robin_list();
01042 misdn_cfg_reload();
01043 misdn_cfg_update_ptp();
01044 misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
01045 misdn_cfg_get(0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(int));
01046
01047 for (i = 0; i <= max_ports; i++) {
01048 misdn_debug[i] = cfg_debug;
01049 misdn_debug_only[i] = 0;
01050 }
01051 }
01052
01053 static int misdn_reload (int fd, int argc, char *argv[])
01054 {
01055 ast_cli(fd, "Reloading mISDN configuration\n");
01056 reload_config();
01057 return 0;
01058 }
01059
01060 static void print_bc_info (int fd, struct chan_list *help, struct misdn_bchannel *bc)
01061 {
01062 struct ast_channel *ast = help->ast;
01063 ast_cli(fd,
01064 "* Pid:%d Prt:%d Ch:%d Mode:%s Org:%s dad:%s oad:%s rad:%s ctx:%s state:%s\n",
01065
01066 bc->pid, bc->port, bc->channel,
01067 bc->nt ? "NT" : "TE",
01068 help->originator == ORG_AST ? "*" : "I",
01069 ast ? ast->exten : NULL,
01070 ast ? ast->cid.cid_num : NULL,
01071 bc->rad,
01072 ast ? ast->context : NULL,
01073 misdn_get_ch_state(help)
01074 );
01075 if (misdn_debug[bc->port] > 0)
01076 ast_cli(fd,
01077 " --> astname: %s\n"
01078 " --> ch_l3id: %x\n"
01079 " --> ch_addr: %x\n"
01080 " --> bc_addr: %x\n"
01081 " --> bc_l3id: %x\n"
01082 " --> display: %s\n"
01083 " --> activated: %d\n"
01084 " --> state: %s\n"
01085 " --> capability: %s\n"
01086 #ifdef MISDN_1_2
01087 " --> pipeline: %s\n"
01088 #else
01089 " --> echo_cancel: %d\n"
01090 #endif
01091 " --> notone : rx %d tx:%d\n"
01092 " --> bc_hold: %d\n",
01093 help->ast->name,
01094 help->l3id,
01095 help->addr,
01096 bc->addr,
01097 bc ? bc->l3_id : -1,
01098 bc->display,
01099
01100 bc->active,
01101 bc_state2str(bc->bc_state),
01102 bearer2str(bc->capability),
01103 #ifdef MISDN_1_2
01104 bc->pipeline,
01105 #else
01106 bc->ec_enable,
01107 #endif
01108
01109 help->norxtone, help->notxtone,
01110 bc->holded
01111 );
01112
01113 }
01114
01115 static int misdn_show_cls (int fd, int argc, char *argv[])
01116 {
01117 struct chan_list *help;
01118
01119 help = cl_te;
01120
01121 ast_cli(fd, "Channel List: %p\n", cl_te);
01122
01123 for (; help; help = help->next) {
01124 struct misdn_bchannel *bc = help->bc;
01125 struct ast_channel *ast = help->ast;
01126 if (!ast) {
01127 if (!bc) {
01128 ast_cli(fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id);
01129 continue;
01130 }
01131 ast_cli(fd, "bc with pid:%d has no Ast Leg\n", bc->pid);
01132 continue;
01133 }
01134
01135 if (misdn_debug[0] > 2)
01136 ast_cli(fd, "Bc:%p Ast:%p\n", bc, ast);
01137 if (bc) {
01138 print_bc_info(fd, help, bc);
01139 } else {
01140 if (help->state == MISDN_HOLDED) {
01141 ast_cli(fd, "ITS A HOLDED BC:\n");
01142 ast_cli(fd, " --> l3_id: %x\n"
01143 " --> dad:%s oad:%s\n"
01144 " --> hold_port: %d\n"
01145 " --> hold_channel: %d\n",
01146 help->l3id,
01147 ast->exten,
01148 ast->cid.cid_num,
01149 help->hold_info.port,
01150 help->hold_info.channel
01151 );
01152 } else {
01153 ast_cli(fd, "* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n", ast->exten, ast->cid.cid_num);
01154 }
01155 }
01156 }
01157
01158 misdn_dump_chanlist();
01159
01160 return 0;
01161 }
01162
01163 static int misdn_show_cl (int fd, int argc, char *argv[])
01164 {
01165 struct chan_list *help;
01166
01167 if (argc != 4)
01168 return RESULT_SHOWUSAGE;
01169
01170 help = cl_te;
01171
01172 for (; help; help = help->next) {
01173 struct misdn_bchannel *bc = help->bc;
01174 struct ast_channel *ast = help->ast;
01175
01176 if (bc && ast) {
01177 if (!strcasecmp(ast->name,argv[3])) {
01178 print_bc_info(fd, help, bc);
01179 break;
01180 }
01181 }
01182 }
01183
01184 return 0;
01185 }
01186
01187 ast_mutex_t lock;
01188 int MAXTICS = 8;
01189
01190 static int misdn_set_tics (int fd, int argc, char *argv[])
01191 {
01192 if (argc != 4)
01193 return RESULT_SHOWUSAGE;
01194
01195 MAXTICS = atoi(argv[3]);
01196
01197 return 0;
01198 }
01199
01200 static int misdn_show_stacks (int fd, int argc, char *argv[])
01201 {
01202 int port;
01203
01204 ast_cli(fd, "BEGIN STACK_LIST:\n");
01205 for (port = misdn_cfg_get_next_port(0); port > 0;
01206 port = misdn_cfg_get_next_port(port)) {
01207 char buf[128];
01208 get_show_stack_details(port, buf);
01209 ast_cli(fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
01210 }
01211
01212 return 0;
01213 }
01214
01215 static int misdn_show_ports_stats (int fd, int argc, char *argv[])
01216 {
01217 int port;
01218
01219 ast_cli(fd, "Port\tin_calls\tout_calls\n");
01220 for (port = misdn_cfg_get_next_port(0); port > 0;
01221 port = misdn_cfg_get_next_port(port)) {
01222 ast_cli(fd, "%d\t%d\t\t%d\n", port, misdn_in_calls[port], misdn_out_calls[port]);
01223 }
01224 ast_cli(fd, "\n");
01225
01226 return 0;
01227 }
01228
01229 static int misdn_show_port (int fd, int argc, char *argv[])
01230 {
01231 int port;
01232 char buf[128];
01233
01234 if (argc != 4)
01235 return RESULT_SHOWUSAGE;
01236
01237 port = atoi(argv[3]);
01238
01239 ast_cli(fd, "BEGIN STACK_LIST:\n");
01240 get_show_stack_details(port, buf);
01241 ast_cli(fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
01242
01243 return 0;
01244 }
01245
01246 static int misdn_send_cd (int fd, int argc, char *argv[])
01247 {
01248 char *channame;
01249 char *nr;
01250 struct chan_list *tmp;
01251
01252 if (argc != 5)
01253 return RESULT_SHOWUSAGE;
01254
01255
01256 {
01257 channame = argv[3];
01258 nr = argv[4];
01259
01260 ast_cli(fd, "Sending Calldeflection (%s) to %s\n", nr, channame);
01261 tmp = get_chan_by_ast_name(channame);
01262 if (!tmp) {
01263 ast_cli(fd, "Sending CD with nr %s to %s failed: Channel does not exist.\n",nr, channame);
01264 return 0;
01265 }
01266
01267 if (strlen(nr) >= 15) {
01268 ast_cli(fd, "Sending CD with nr %s to %s failed: Number too long (up to 15 digits are allowed).\n",nr, channame);
01269 return 0;
01270 }
01271 tmp->bc->fac_out.Function = Fac_CD;
01272 ast_copy_string((char *)tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr, sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber));
01273 misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
01274 }
01275
01276 return 0;
01277 }
01278
01279 static int misdn_send_restart(int fd, int argc, char *argv[])
01280 {
01281 int port;
01282 int channel;
01283
01284 if (argc < 4 || argc > 5)
01285 return RESULT_SHOWUSAGE;
01286
01287 port = atoi(argv[3]);
01288
01289 if (argc == 5) {
01290 channel = atoi(argv[4]);
01291 misdn_lib_send_restart(port, channel);
01292 } else {
01293 misdn_lib_send_restart(port, -1);
01294 }
01295
01296 return 0;
01297 }
01298
01299 static int misdn_send_digit (int fd, int argc, char *argv[])
01300 {
01301 char *channame;
01302 char *msg;
01303 struct chan_list *tmp;
01304 int i, msglen;
01305
01306 if (argc != 5)
01307 return RESULT_SHOWUSAGE;
01308
01309 channame = argv[3];
01310 msg = argv[4];
01311 msglen = strlen(msg);
01312
01313 ast_cli(fd, "Sending %s to %s\n", msg, channame);
01314
01315 tmp = get_chan_by_ast_name(channame);
01316 if (!tmp) {
01317 ast_cli(fd, "Sending %s to %s failed Channel does not exist\n", msg, channame);
01318 return 0;
01319 }
01320 #if 1
01321 for (i = 0; i < msglen; i++) {
01322 ast_cli(fd, "Sending: %c\n", msg[i]);
01323 send_digit_to_chan(tmp, msg[i]);
01324
01325 usleep(250000);
01326
01327 }
01328 #else
01329 ast_dtmf_stream(tmp->ast, NULL, msg, 250);
01330 #endif
01331
01332 return 0;
01333 }
01334
01335 static int misdn_toggle_echocancel (int fd, int argc, char *argv[])
01336 {
01337 char *channame;
01338 struct chan_list *tmp;
01339
01340 if (argc != 4)
01341 return RESULT_SHOWUSAGE;
01342
01343 channame = argv[3];
01344
01345 ast_cli(fd, "Toggling EchoCancel on %s\n", channame);
01346
01347 tmp = get_chan_by_ast_name(channame);
01348 if (!tmp) {
01349 ast_cli(fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame);
01350 return 0;
01351 }
01352
01353 tmp->toggle_ec = tmp->toggle_ec ? 0 : 1;
01354
01355 if (tmp->toggle_ec) {
01356 #ifdef MISDN_1_2
01357 update_pipeline_config(tmp->bc);
01358 #else
01359 update_ec_config(tmp->bc);
01360 #endif
01361 manager_ec_enable(tmp->bc);
01362 } else {
01363 manager_ec_disable(tmp->bc);
01364 }
01365
01366 return 0;
01367 }
01368
01369 static int misdn_send_display (int fd, int argc, char *argv[])
01370 {
01371 char *channame;
01372 char *msg;
01373 struct chan_list *tmp;
01374
01375 if (argc != 5)
01376 return RESULT_SHOWUSAGE;
01377
01378 channame = argv[3];
01379 msg = argv[4];
01380
01381 ast_cli(fd, "Sending %s to %s\n", msg, channame);
01382 tmp = get_chan_by_ast_name(channame);
01383
01384 if (tmp && tmp->bc) {
01385 ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display));
01386 misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
01387 } else {
01388 ast_cli(fd, "No such channel %s\n", channame);
01389 return RESULT_FAILURE;
01390 }
01391
01392 return RESULT_SUCCESS;
01393 }
01394
01395 static char *complete_ch_helper(const char *line, const char *word, int pos, int state, int rpos)
01396 {
01397 struct ast_channel *c;
01398 int which=0;
01399 char *ret;
01400 if (pos != rpos)
01401 return NULL;
01402 c = ast_channel_walk_locked(NULL);
01403 while(c) {
01404 if (!strncasecmp(word, c->name, strlen(word))) {
01405 if (++which > state)
01406 break;
01407 }
01408 ast_mutex_unlock(&c->lock);
01409 c = ast_channel_walk_locked(c);
01410 }
01411 if (c) {
01412 ret = strdup(c->name);
01413 ast_mutex_unlock(&c->lock);
01414 } else
01415 ret = NULL;
01416 return ret;
01417 }
01418
01419 static char *complete_ch(const char *line, const char *word, int pos, int state)
01420 {
01421 return complete_ch_helper(line, word, pos, state, 3);
01422 }
01423
01424 static char *complete_debug_port (const char *line, const char *word, int pos, int state)
01425 {
01426 if (state)
01427 return NULL;
01428
01429 switch (pos) {
01430 case 4:
01431 if (*word == 'p')
01432 return strdup("port");
01433 else if (*word == 'o')
01434 return strdup("only");
01435 break;
01436 case 6:
01437 if (*word == 'o')
01438 return strdup("only");
01439 break;
01440 }
01441 return NULL;
01442 }
01443
01444 static char *complete_show_config (const char *line, const char *word, int pos, int state)
01445 {
01446 char buffer[BUFFERSIZE];
01447 enum misdn_cfg_elements elem;
01448 int wordlen = strlen(word);
01449 int which = 0;
01450 int port = 0;
01451
01452 switch (pos) {
01453 case 3:
01454 if ((!strncmp(word, "description", wordlen)) && (++which > state))
01455 return strdup("description");
01456 if ((!strncmp(word, "descriptions", wordlen)) && (++which > state))
01457 return strdup("descriptions");
01458 if ((!strncmp(word, "0", wordlen)) && (++which > state))
01459 return strdup("0");
01460 while ((port = misdn_cfg_get_next_port(port)) != -1) {
01461 snprintf(buffer, sizeof(buffer), "%d", port);
01462 if ((!strncmp(word, buffer, wordlen)) && (++which > state)) {
01463 return strdup(buffer);
01464 }
01465 }
01466 break;
01467 case 4:
01468 if (strstr(line, "description ")) {
01469 for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
01470 if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST))
01471 continue;
01472 misdn_cfg_get_name(elem, buffer, BUFFERSIZE);
01473 if (!wordlen || !strncmp(word, buffer, wordlen)) {
01474 if (++which > state)
01475 return strdup(buffer);
01476 }
01477 }
01478 } else if (strstr(line, "descriptions ")) {
01479 if ((!wordlen || !strncmp(word, "general", wordlen)) && (++which > state))
01480 return strdup("general");
01481 if ((!wordlen || !strncmp(word, "ports", wordlen)) && (++which > state))
01482 return strdup("ports");
01483 }
01484 break;
01485 }
01486 return NULL;
01487 }
01488
01489 static struct ast_cli_entry chan_misdn_clis[] = {
01490 { {"misdn","send","calldeflect", NULL}, misdn_send_cd, "Sends CallDeflection to mISDN Channel",
01491 "Usage: misdn send calldeflect <channel> \"<nr>\" \n", complete_ch },
01492 { {"misdn","send","digit", NULL}, misdn_send_digit, "Sends DTMF Digit to mISDN Channel",
01493 "Usage: misdn send digit <channel> \"<msg>\" \n"
01494 " Send <digit> to <channel> as DTMF Tone\n"
01495 " when channel is a mISDN channel\n", complete_ch },
01496 { {"misdn","toggle","echocancel", NULL}, misdn_toggle_echocancel, "Toggles EchoCancel on mISDN Channel",
01497 "Usage: misdn toggle echocancel <channel>\n", complete_ch },
01498 { {"misdn","send","display", NULL}, misdn_send_display, "Sends Text to mISDN Channel",
01499 "Usage: misdn send display <channel> \"<msg>\" \n"
01500 " Send <msg> to <channel> as Display Message\n"
01501 " when channel is a mISDN channel\n", complete_ch },
01502 { {"misdn","show","config", NULL}, misdn_show_config, "Shows internal mISDN config, read from cfg-file",
01503 "Usage: misdn show config [<port> | description <config element> | descriptions [general|ports]]\n"
01504 " Use 0 for <port> to only print the general config.\n", complete_show_config },
01505 { {"misdn","reload", NULL}, misdn_reload, "Reloads internal mISDN config, read from cfg-file",
01506 "Usage: misdn reload\n" },
01507 { {"misdn","set","tics", NULL}, misdn_set_tics, "",
01508 "\n" },
01509 { {"misdn","show","channels", NULL}, misdn_show_cls, "Shows internal mISDN chan_list",
01510 "Usage: misdn show channels\n" },
01511 { {"misdn","show","channel", NULL}, misdn_show_cl, "Shows internal mISDN chan_list",
01512 "Usage: misdn show channels\n", complete_ch },
01513 { {"misdn","port","block", NULL}, misdn_port_block, "Blocks the given port",
01514 "Usage: misdn port block\n" },
01515 { {"misdn","port","unblock", NULL}, misdn_port_unblock, "Unblocks the given port",
01516 "Usage: misdn port unblock\n" },
01517 { {"misdn","restart","port", NULL}, misdn_restart_port, "Restarts the given port",
01518 "Usage: misdn restart port\n" },
01519 { {"misdn","restart","pid", NULL}, misdn_restart_pid, "Restarts the given pid",
01520 "Usage: misdn restart pid\n" },
01521 { {"misdn","send","restart", NULL}, misdn_send_restart,
01522 "Sends a restart for every bchannel on the given port",
01523 "Usage: misdn send restart <port>\n"},
01524 { {"misdn","port","up", NULL}, misdn_port_up, "Tries to establish L1 on the given port",
01525 "Usage: misdn port up <port>\n" },
01526 { {"misdn","port","down", NULL}, misdn_port_down, "Tries to deactivate the L1 on the given port",
01527 "Usage: misdn port down <port>\n" },
01528 { {"misdn","show","stacks", NULL}, misdn_show_stacks, "Shows internal mISDN stack_list",
01529 "Usage: misdn show stacks\n" },
01530 { {"misdn","show","ports","stats", NULL}, misdn_show_ports_stats, "Shows chan_misdns call statistics per port",
01531 "Usage: misdn show port stats\n" },
01532 { {"misdn","show","port", NULL}, misdn_show_port, "Shows detailed information for given port",
01533 "Usage: misdn show port <port>\n" },
01534 { {"misdn","set","debug", NULL}, misdn_set_debug, "Sets Debuglevel of chan_misdn",
01535 "Usage: misdn set debug <level> [only] | [port <port> [only]]\n", complete_debug_port },
01536 { {"misdn","set","crypt","debug", NULL}, misdn_set_crypt_debug, "Sets CryptDebuglevel of chan_misdn, at the moment, level={1,2}",
01537 "Usage: misdn set crypt debug <level>\n" }
01538 };
01539
01540 static int update_config(struct chan_list *ch, int orig)
01541 {
01542 struct ast_channel *ast;
01543 struct misdn_bchannel *bc;
01544 int port, hdlc = 0;
01545 int pres, screen;
01546
01547 if (!ch) {
01548 ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
01549 return -1;
01550 }
01551
01552 ast = ch->ast;
01553 bc = ch->bc;
01554 if (! ast || ! bc) {
01555 ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
01556 return -1;
01557 }
01558
01559 port = bc->port;
01560
01561 chan_misdn_log(7, port, "update_config: Getting Config\n");
01562
01563 misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
01564
01565 if (hdlc) {
01566 switch (bc->capability) {
01567 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
01568 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
01569 chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
01570 bc->hdlc = 1;
01571 break;
01572 }
01573 }
01574
01575
01576 misdn_cfg_get(port, MISDN_CFG_PRES, &pres, sizeof(pres));
01577 misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen));
01578 chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen);
01579
01580 if (pres < 0 || screen < 0) {
01581 chan_misdn_log(2, port, " --> pres: %x\n", ast->cid.cid_pres);
01582
01583 switch (ast->cid.cid_pres & 0x60) {
01584 case AST_PRES_RESTRICTED:
01585 bc->pres = 1;
01586 chan_misdn_log(2, port, " --> PRES: Restricted (1)\n");
01587 break;
01588 case AST_PRES_UNAVAILABLE:
01589 bc->pres = 2;
01590 chan_misdn_log(2, port, " --> PRES: Unavailable (2)\n");
01591 break;
01592 default:
01593 bc->pres = 0;
01594 chan_misdn_log(2, port, " --> PRES: Allowed (0)\n");
01595 break;
01596 }
01597
01598 switch (ast->cid.cid_pres & 0x3) {
01599 default:
01600 case AST_PRES_USER_NUMBER_UNSCREENED:
01601 bc->screen = 0;
01602 chan_misdn_log(2, port, " --> SCREEN: Unscreened (0)\n");
01603 break;
01604 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
01605 bc->screen = 1;
01606 chan_misdn_log(2, port, " --> SCREEN: Passed Screen (1)\n");
01607 break;
01608 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
01609 bc->screen = 2;
01610 chan_misdn_log(2, port, " --> SCREEN: Failed Screen (2)\n");
01611 break;
01612 case AST_PRES_NETWORK_NUMBER:
01613 bc->screen = 3;
01614 chan_misdn_log(2, port, " --> SCREEN: Network Nr. (3)\n");
01615 break;
01616 }
01617 } else {
01618 bc->screen = screen;
01619 bc->pres = pres;
01620 }
01621
01622 return 0;
01623 }
01624
01625
01626 static void config_jitterbuffer(struct chan_list *ch)
01627 {
01628 struct misdn_bchannel *bc = ch->bc;
01629 int len = ch->jb_len, threshold = ch->jb_upper_threshold;
01630
01631 chan_misdn_log(5, bc->port, "config_jb: Called\n");
01632
01633 if (! len) {
01634 chan_misdn_log(1, bc->port, "config_jb: Deactivating Jitterbuffer\n");
01635 bc->nojitter=1;
01636 } else {
01637 if (len <= 100 || len > 8000) {
01638 chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer out of Bounds, setting to 1000\n");
01639 len = 1000;
01640 }
01641
01642 if ( threshold > len ) {
01643 chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
01644 }
01645
01646 if ( ch->jb) {
01647 cb_log(0, bc->port, "config_jb: We've got a Jitterbuffer Already on this port.\n");
01648 misdn_jb_destroy(ch->jb);
01649 ch->jb = NULL;
01650 }
01651
01652 ch->jb=misdn_jb_init(len, threshold);
01653
01654 if (!ch->jb )
01655 bc->nojitter = 1;
01656 }
01657 }
01658
01659
01660 void debug_numplan(int port, int numplan, char *type)
01661 {
01662 switch (numplan) {
01663 case NUMPLAN_INTERNATIONAL:
01664 chan_misdn_log(2, port, " --> %s: International\n", type);
01665 break;
01666 case NUMPLAN_NATIONAL:
01667 chan_misdn_log(2, port, " --> %s: National\n", type);
01668 break;
01669 case NUMPLAN_SUBSCRIBER:
01670 chan_misdn_log(2, port, " --> %s: Subscriber\n", type);
01671 break;
01672 case NUMPLAN_UNKNOWN:
01673 chan_misdn_log(2, port, " --> %s: Unknown\n", type);
01674 break;
01675
01676 default:
01677 chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
01678 break;
01679 }
01680 }
01681
01682
01683 #ifdef MISDN_1_2
01684 static int update_pipeline_config(struct misdn_bchannel *bc)
01685 {
01686 int ec;
01687
01688 misdn_cfg_get(bc->port, MISDN_CFG_PIPELINE, bc->pipeline, sizeof(bc->pipeline));
01689
01690 if (*bc->pipeline)
01691 return 0;
01692
01693 misdn_cfg_get(bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
01694 if (ec == 1)
01695 ast_copy_string(bc->pipeline, "mg2ec", sizeof(bc->pipeline));
01696 else if (ec > 1)
01697 snprintf(bc->pipeline, sizeof(bc->pipeline), "mg2ec(deftaps=%d)", ec);
01698
01699 return 0;
01700 }
01701 #else
01702 static int update_ec_config(struct misdn_bchannel *bc)
01703 {
01704 int ec;
01705 int port = bc->port;
01706
01707 misdn_cfg_get(port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
01708
01709 if (ec == 1) {
01710 bc->ec_enable = 1;
01711 } else if (ec > 1) {
01712 bc->ec_enable = 1;
01713 bc->ec_deftaps = ec;
01714 }
01715
01716 return 0;
01717 }
01718 #endif
01719
01720
01721 static int read_config(struct chan_list *ch, int orig)
01722 {
01723 struct ast_channel *ast;
01724 struct misdn_bchannel *bc;
01725 int port;
01726 int hdlc = 0;
01727 char lang[BUFFERSIZE + 1];
01728 char localmusicclass[BUFFERSIZE + 1];
01729 char faxdetect[BUFFERSIZE + 1];
01730 char buf[256];
01731 char buf2[256];
01732 ast_group_t pg;
01733 ast_group_t cg;
01734
01735 if (!ch) {
01736 ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
01737 return -1;
01738 }
01739
01740 ast = ch->ast;
01741 bc = ch->bc;
01742 if (! ast || ! bc) {
01743 ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
01744 return -1;
01745 }
01746
01747 port = bc->port;
01748 chan_misdn_log(1, port, "read_config: Getting Config\n");
01749
01750 misdn_cfg_get(port, MISDN_CFG_LANGUAGE, lang, BUFFERSIZE);
01751 ast_string_field_set(ast, language, lang);
01752
01753 misdn_cfg_get(port, MISDN_CFG_MUSICCLASS, localmusicclass, BUFFERSIZE);
01754 ast_string_field_set(ast, musicclass, localmusicclass);
01755
01756 misdn_cfg_get(port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(int));
01757 misdn_cfg_get(port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(int));
01758
01759 misdn_cfg_get(port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(int));
01760
01761 misdn_cfg_get(port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(int));
01762
01763 misdn_cfg_get(port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int));
01764
01765 if (ch->ast_dsp) {
01766 ch->ignore_dtmf = 1;
01767 }
01768
01769 misdn_cfg_get(port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(int));
01770 misdn_cfg_get(port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(int));
01771
01772 misdn_cfg_get(port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(int));
01773
01774 misdn_cfg_get(port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(int));
01775
01776 misdn_cfg_get(port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, BUFFERSIZE);
01777
01778 misdn_cfg_get(port, MISDN_CFG_FAXDETECT, faxdetect, BUFFERSIZE);
01779
01780 misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
01781
01782 if (hdlc) {
01783 switch (bc->capability) {
01784 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
01785 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
01786 chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
01787 bc->hdlc = 1;
01788 break;
01789 }
01790
01791 }
01792
01793 misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(int));
01794 misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(int));
01795
01796 config_jitterbuffer(ch);
01797
01798 misdn_cfg_get(bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
01799
01800 ast_copy_string(ast->context, ch->context, sizeof(ast->context));
01801
01802 #ifdef MISDN_1_2
01803 update_pipeline_config(bc);
01804 #else
01805 update_ec_config(bc);
01806 #endif
01807
01808 {
01809 int eb3;
01810
01811 misdn_cfg_get( bc->port, MISDN_CFG_EARLY_BCONNECT, &eb3, sizeof(int));
01812 bc->early_bconnect=eb3;
01813 }
01814
01815 misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
01816 misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
01817
01818 chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n", ast_print_group(buf, sizeof(buf), cg), ast_print_group(buf2, sizeof(buf2), pg));
01819 ast->pickupgroup = pg;
01820 ast->callgroup = cg;
01821
01822 if (orig == ORG_AST) {
01823 char callerid[BUFFERSIZE + 1];
01824
01825 misdn_cfg_get(port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(int));
01826
01827 if (strstr(faxdetect, "outgoing") || strstr(faxdetect, "both")) {
01828 if (strstr(faxdetect, "nojump"))
01829 ch->faxdetect = 2;
01830 else
01831 ch->faxdetect = 1;
01832 }
01833
01834 misdn_cfg_get(port, MISDN_CFG_CALLERID, callerid, BUFFERSIZE);
01835 if ( ! ast_strlen_zero(callerid) ) {
01836 chan_misdn_log(1, port, " --> * Setting Cid to %s\n", callerid);
01837 ast_copy_string(bc->oad, callerid, sizeof(bc->oad));
01838 }
01839
01840 misdn_cfg_get(port, MISDN_CFG_DIALPLAN, &bc->dnumplan, sizeof(int));
01841 misdn_cfg_get(port, MISDN_CFG_LOCALDIALPLAN, &bc->onumplan, sizeof(int));
01842 misdn_cfg_get(port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int));
01843 debug_numplan(port, bc->dnumplan, "TON");
01844 debug_numplan(port, bc->onumplan, "LTON");
01845 debug_numplan(port, bc->cpnnumplan, "CTON");
01846
01847 ch->overlap_dial = 0;
01848 } else {
01849 char prefix[BUFFERSIZE + 1] = "";
01850
01851 if (strstr(faxdetect, "incoming") || strstr(faxdetect, "both")) {
01852 if (strstr(faxdetect, "nojump"))
01853 ch->faxdetect = 2;
01854 else
01855 ch->faxdetect = 1;
01856 }
01857
01858 misdn_cfg_get(port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int));
01859 debug_numplan(port, bc->cpnnumplan, "CTON");
01860
01861 switch (bc->onumplan) {
01862 case NUMPLAN_INTERNATIONAL:
01863 misdn_cfg_get(bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
01864 break;
01865
01866 case NUMPLAN_NATIONAL:
01867 misdn_cfg_get(bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
01868 break;
01869 default:
01870 break;
01871 }
01872
01873 ast_copy_string(buf, bc->oad, sizeof(buf));
01874 snprintf(bc->oad, sizeof(bc->oad), "%s%s", prefix, buf);
01875
01876 if (!ast_strlen_zero(bc->dad)) {
01877 ast_copy_string(bc->orig_dad, bc->dad, sizeof(bc->orig_dad));
01878 }
01879
01880 if ( ast_strlen_zero(bc->dad) && !ast_strlen_zero(bc->keypad)) {
01881 ast_copy_string(bc->dad, bc->keypad, sizeof(bc->dad));
01882 }
01883
01884 prefix[0] = 0;
01885
01886 switch (bc->dnumplan) {
01887 case NUMPLAN_INTERNATIONAL:
01888 misdn_cfg_get(bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
01889 break;
01890 case NUMPLAN_NATIONAL:
01891 misdn_cfg_get(bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
01892 break;
01893 default:
01894 break;
01895 }
01896
01897 ast_copy_string(buf, bc->dad, sizeof(buf));
01898 snprintf(bc->dad, sizeof(bc->dad), "%s%s", prefix, buf);
01899
01900 if (strcmp(bc->dad, ast->exten)) {
01901 ast_copy_string(ast->exten, bc->dad, sizeof(ast->exten));
01902 }
01903
01904 ast_set_callerid(ast, bc->oad, NULL, bc->oad);
01905
01906 if ( !ast_strlen_zero(bc->rad) ) {
01907 if (ast->cid.cid_rdnis)
01908 free(ast->cid.cid_rdnis);
01909 ast->cid.cid_rdnis = strdup(bc->rad);
01910 }
01911
01912 misdn_cfg_get(bc->port, MISDN_CFG_OVERLAP_DIAL, &ch->overlap_dial, sizeof(ch->overlap_dial));
01913 ast_mutex_init(&ch->overlap_tv_lock);
01914 }
01915
01916 ch->overlap_dial_task = -1;
01917
01918 if (ch->faxdetect || ch->ast_dsp) {
01919 misdn_cfg_get(port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
01920 if (!ch->dsp)
01921 ch->dsp = ast_dsp_new();
01922 if (ch->dsp) {
01923 if (ch->faxdetect)
01924 ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT | DSP_FEATURE_FAX_DETECT);
01925 else
01926 ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT );
01927 }
01928 if (!ch->trans)
01929 ch->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW);
01930 }
01931
01932
01933 bc->AOCDtype = Fac_None;
01934
01935 return 0;
01936 }
01937
01938
01939
01940
01941
01942
01943 static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
01944 {
01945 int port = 0;
01946 int r;
01947 int exceed;
01948 int bridging;
01949 struct chan_list *ch;
01950 struct misdn_bchannel *newbc;
01951 char *opts, *ext;
01952 char *dest_cp;
01953
01954 if (!ast) {
01955 ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
01956 return -1;
01957 }
01958
01959 if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest ) {
01960 ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
01961 ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
01962 ast_setstate(ast, AST_STATE_DOWN);
01963 return -1;
01964 }
01965
01966 ch = MISDN_ASTERISK_TECH_PVT(ast);
01967 if (!ch) {
01968 ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
01969 ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
01970 ast_setstate(ast, AST_STATE_DOWN);
01971 return -1;
01972 }
01973
01974 newbc = ch->bc;
01975 if (!newbc) {
01976 ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
01977 ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
01978 ast_setstate(ast, AST_STATE_DOWN);
01979 return -1;
01980 }
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990 dest_cp = ast_strdupa(dest);
01991 strsep(&dest_cp, "/");
01992 ext = strsep(&dest_cp, "/");
01993 if (!ext) {
01994 ext = "";
01995 }
01996 opts = dest_cp;
01997
01998 port = newbc->port;
01999
02000 if ((exceed = add_out_calls(port))) {
02001 char tmp[16];
02002 snprintf(tmp, sizeof(tmp), "%d", exceed);
02003 pbx_builtin_setvar_helper(ast, "MAX_OVERFLOW", tmp);
02004 return -1;
02005 }
02006
02007 chan_misdn_log(1, port, "* CALL: %s\n", dest);
02008
02009 chan_misdn_log(2, port, " --> * dad:%s tech:%s ctx:%s\n", ast->exten, ast->name, ast->context);
02010
02011 chan_misdn_log(3, port, " --> * adding2newbc ext %s\n", ast->exten);
02012 if (ast->exten) {
02013 ast_copy_string(ast->exten, ext, sizeof(ast->exten));
02014 ast_copy_string(newbc->dad, ext, sizeof(newbc->dad));
02015 }
02016
02017 ast_copy_string(newbc->rad, S_OR(ast->cid.cid_rdnis, ""), sizeof(newbc->rad));
02018
02019 chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n", ast->cid.cid_num);
02020 if (ast_strlen_zero(newbc->oad) && !ast_strlen_zero(ast->cid.cid_num)) {
02021 ast_copy_string(newbc->oad, ast->cid.cid_num, sizeof(newbc->oad));
02022 }
02023
02024 newbc->capability = ast->transfercapability;
02025 pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY", ast_transfercapability2str(newbc->capability));
02026 if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
02027 chan_misdn_log(2, port, " --> * Call with flag Digital\n");
02028 }
02029
02030
02031 update_config(ch, ORG_AST);
02032
02033
02034 import_ch(ast, newbc, ch);
02035
02036
02037 if (opts)
02038 misdn_set_opt_exec(ast, opts);
02039 else
02040 chan_misdn_log(2, port, "NO OPTS GIVEN\n");
02041
02042
02043 misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
02044 if (bridging && ch->other_ch) {
02045 #ifdef MISDN_1_2
02046 chan_misdn_log(1, port, "Disabling EC (aka Pipeline) on both Sides\n");
02047 *ch->bc->pipeline = 0;
02048 *ch->other_ch->bc->pipeline = 0;
02049 #else
02050 chan_misdn_log(1, port, "Disabling EC on both Sides\n");
02051 ch->bc->ec_enable = 0;
02052 ch->other_ch->bc->ec_enable = 0;
02053 #endif
02054 }
02055
02056 r = misdn_lib_send_event( newbc, EVENT_SETUP );
02057
02058
02059 ch->l3id = newbc->l3_id;
02060
02061 if ( r == -ENOCHAN ) {
02062 chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
02063 chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n", newbc ? newbc->pid : -1);
02064 ast->hangupcause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
02065 ast_setstate(ast, AST_STATE_DOWN);
02066 return -1;
02067 }
02068
02069 chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n", newbc ? newbc->pid : 1);
02070
02071 ast_setstate(ast, AST_STATE_DIALING);
02072 ast->hangupcause = AST_CAUSE_NORMAL_CLEARING;
02073
02074 if (newbc->nt)
02075 stop_bc_tones(ch);
02076
02077 ch->state = MISDN_CALLING;
02078
02079 return 0;
02080 }
02081
02082
02083 static int misdn_answer(struct ast_channel *ast)
02084 {
02085 struct chan_list *p;
02086 const char *tmp;
02087
02088 if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) return -1;
02089
02090 chan_misdn_log(1, p ? (p->bc ? p->bc->port : 0) : 0, "* ANSWER:\n");
02091
02092 if (!p) {
02093 ast_log(LOG_WARNING, " --> Channel not connected ??\n");
02094 ast_queue_hangup(ast);
02095 }
02096
02097 if (!p->bc) {
02098 chan_misdn_log(1, 0, " --> Got Answer, but theres no bc obj ??\n");
02099
02100 ast_queue_hangup(ast);
02101 }
02102
02103 tmp = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY");
02104 if (!ast_strlen_zero(tmp)) {
02105 chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
02106 ast_copy_string(p->bc->crypt_key, tmp, sizeof(p->bc->crypt_key));
02107 } else {
02108 chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
02109 }
02110
02111 tmp = pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
02112 if (!ast_strlen_zero(tmp) && ast_true(tmp)) {
02113 chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
02114 p->bc->nodsp = 1;
02115 p->bc->hdlc = 0;
02116 p->bc->nojitter = 1;
02117 }
02118
02119 p->state = MISDN_CONNECTED;
02120 stop_indicate(p);
02121
02122 if ( ast_strlen_zero(p->bc->cad) ) {
02123 chan_misdn_log(2,p->bc->port," --> empty cad using dad\n");
02124 ast_copy_string(p->bc->cad, p->bc->dad, sizeof(p->bc->cad));
02125 }
02126
02127 misdn_lib_send_event( p->bc, EVENT_CONNECT);
02128 start_bc_tones(p);
02129
02130 return 0;
02131 }
02132
02133 static int misdn_digit_begin(struct ast_channel *chan, char digit)
02134 {
02135
02136 return 0;
02137 }
02138
02139 static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
02140 {
02141 struct chan_list *p;
02142 struct misdn_bchannel *bc;
02143
02144 if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) return -1;
02145
02146 bc = p->bc;
02147 chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit);
02148
02149 if (!bc) {
02150 ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n");
02151 return -1;
02152 }
02153
02154 switch (p->state ) {
02155 case MISDN_CALLING:
02156 {
02157 int l;
02158 char buf[8];
02159 buf[0]=digit;
02160 buf[1]=0;
02161
02162 l = sizeof(bc->infos_pending);
02163 strncat(bc->infos_pending, buf, l - strlen(bc->infos_pending) - 1);
02164 }
02165 break;
02166 case MISDN_CALLING_ACKNOWLEDGE:
02167 {
02168 bc->info_dad[0]=digit;
02169 bc->info_dad[1]=0;
02170
02171 {
02172 int l = sizeof(bc->dad);
02173 strncat(bc->dad, bc->info_dad, l - strlen(bc->dad) - 1);
02174 }
02175 {
02176 int l = sizeof(p->ast->exten);
02177 strncpy(p->ast->exten, bc->dad, l);
02178 p->ast->exten[l-1] = 0;
02179 }
02180
02181 misdn_lib_send_event( bc, EVENT_INFORMATION);
02182 }
02183 break;
02184 default:
02185
02186
02187 if (p->other_ch )
02188 return 0;
02189
02190 if ( bc->send_dtmf )
02191 send_digit_to_chan(p,digit);
02192 break;
02193 }
02194
02195 return 0;
02196 }
02197
02198
02199 static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
02200 {
02201 struct chan_list *p;
02202
02203 if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) )) return -1;
02204
02205 chan_misdn_log(1, p->bc ? p->bc->port : 0, "* IND: Got Fixup State:%s L3id:%x\n", misdn_get_ch_state(p), p->l3id);
02206
02207 p->ast = ast;
02208
02209 return 0;
02210 }
02211
02212
02213
02214 static int misdn_indication(struct ast_channel *ast, int cond, const void *data, size_t datalen)
02215 {
02216 struct chan_list *p;
02217
02218 if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) {
02219 ast_log(LOG_WARNING, "Returned -1 in misdn_indication\n");
02220 return -1;
02221 }
02222
02223 if (!p->bc ) {
02224 chan_misdn_log(1, 0, "* IND : Indication from %s\n", ast->exten);
02225 ast_log(LOG_WARNING, "Private Pointer but no bc ?\n");
02226 return -1;
02227 }
02228
02229 chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] from %s\n", cond, ast->exten);
02230
02231 switch (cond) {
02232 case AST_CONTROL_BUSY:
02233 chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n", p->bc ? p->bc->pid : -1);
02234 ast_setstate(ast, AST_STATE_BUSY);
02235
02236 p->bc->out_cause = AST_CAUSE_USER_BUSY;
02237 if (p->state != MISDN_CONNECTED) {
02238 start_bc_tones(p);
02239 misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
02240 } else {
02241 chan_misdn_log(-1, p->bc->port, " --> !! Got Busy in Connected State !?! ast:%s\n", ast->name);
02242 }
02243 return -1;
02244 case AST_CONTROL_RING:
02245 chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n", p->bc ? p->bc->pid : -1);
02246 return -1;
02247 case AST_CONTROL_RINGING:
02248 chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc ? p->bc->pid : -1);
02249 switch (p->state) {
02250 case MISDN_ALERTING:
02251 chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc ? p->bc->pid : -1);
02252 break;
02253 case MISDN_CONNECTED:
02254 chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n", p->bc ? p->bc->pid : -1);
02255 return -1;
02256 default:
02257 p->state = MISDN_ALERTING;
02258 chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n", p->bc ? p->bc->pid : -1);
02259 misdn_lib_send_event( p->bc, EVENT_ALERTING);
02260
02261 if (p->other_ch && p->other_ch->bc) {
02262 if (misdn_inband_avail(p->other_ch->bc)) {
02263 chan_misdn_log(2, p->bc->port, " --> other End is mISDN and has inband info available\n");
02264 break;
02265 }
02266
02267 if (!p->other_ch->bc->nt) {
02268 chan_misdn_log(2, p->bc->port, " --> other End is mISDN TE so it has inband info for sure (?)\n");
02269 break;
02270 }
02271 }
02272
02273 chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n", p->bc ? p->bc->pid : -1);
02274 ast_setstate(ast, AST_STATE_RING);
02275
02276 if (!p->bc->nt && (p->originator == ORG_MISDN) && !p->incoming_early_audio)
02277 chan_misdn_log(2, p->bc->port, " --> incoming_early_audio off\n");
02278 else
02279 return -1;
02280 }
02281 break;
02282 case AST_CONTROL_ANSWER:
02283 chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n", p->bc ? p->bc->pid : -1);
02284 start_bc_tones(p);
02285 break;
02286 case AST_CONTROL_TAKEOFFHOOK:
02287 chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n", p->bc ? p->bc->pid : -1);
02288 return -1;
02289 case AST_CONTROL_OFFHOOK:
02290 chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n", p->bc ? p->bc->pid : -1);
02291 return -1;
02292 case AST_CONTROL_FLASH:
02293 chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n", p->bc ? p->bc->pid : -1);
02294 break;
02295 case AST_CONTROL_PROGRESS:
02296 chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n", p->bc ? p->bc->pid : -1);
02297 misdn_lib_send_event( p->bc, EVENT_PROGRESS);
02298 break;
02299 case AST_CONTROL_PROCEEDING:
02300 chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n", p->bc ? p->bc->pid : -1);
02301 misdn_lib_send_event( p->bc, EVENT_PROCEEDING);
02302 break;
02303 case AST_CONTROL_CONGESTION:
02304 chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n", p->bc ? p->bc->pid : -1);
02305
02306 p->bc->out_cause = AST_CAUSE_SWITCH_CONGESTION;
02307 start_bc_tones(p);
02308 misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
02309
02310 if (p->bc->nt) {
02311 hanguptone_indicate(p);
02312 }
02313 break;
02314 case -1 :
02315 chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n", p->bc ? p->bc->pid : -1);
02316
02317 stop_indicate(p);
02318
02319 if (p->state == MISDN_CONNECTED)
02320 start_bc_tones(p);
02321 break;
02322 case AST_CONTROL_HOLD:
02323 ast_moh_start(ast,data,ast->musicclass);
02324 chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc ? p->bc->pid : -1);
02325 break;
02326 case AST_CONTROL_UNHOLD:
02327 ast_moh_stop(ast);
02328 chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc ? p->bc->pid : -1);
02329 break;
02330 default:
02331 chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc ? p->bc->pid : -1);
02332 }
02333
02334 return 0;
02335 }
02336
02337 static int misdn_hangup(struct ast_channel *ast)
02338 {
02339 struct chan_list *p;
02340 struct misdn_bchannel *bc = NULL;
02341 const char *varcause = NULL;
02342
02343 ast_log(LOG_DEBUG, "misdn_hangup(%s)\n", ast->name);
02344
02345 if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) ) ) return -1;
02346
02347 if (!p) {
02348 chan_misdn_log(3, 0, "misdn_hangup called, without chan_list obj.\n");
02349 return 0 ;
02350 }
02351
02352 bc = p->bc;
02353
02354 if (bc) {
02355 const char *tmp=pbx_builtin_getvar_helper(ast,"MISDN_USERUSER");
02356 if (tmp) {
02357 ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp);
02358 strcpy(bc->uu, tmp);
02359 bc->uulen=strlen(bc->uu);
02360 }
02361 }
02362
02363 MISDN_ASTERISK_TECH_PVT(ast) = NULL;
02364 p->ast = NULL;
02365
02366 if (ast->_state == AST_STATE_RESERVED ||
02367 p->state == MISDN_NOTHING ||
02368 p->state == MISDN_HOLDED ||
02369 p->state == MISDN_HOLD_DISCONNECT ) {
02370
02371 CLEAN_CH:
02372
02373 ast_log(LOG_DEBUG, "State Reserved (or nothing) => chanIsAvail\n");
02374 MISDN_ASTERISK_TECH_PVT(ast) = NULL;
02375
02376 ast_mutex_lock(&release_lock);
02377 cl_dequeue_chan(&cl_te, p);
02378 close(p->pipe[0]);
02379 close(p->pipe[1]);
02380 free(p);
02381 ast_mutex_unlock(&release_lock);
02382
02383 if (bc)
02384 misdn_lib_release(bc);
02385
02386 return 0;
02387 }
02388
02389 if (!bc) {
02390 ast_log(LOG_WARNING, "Hangup with private but no bc ? state:%s l3id:%x\n", misdn_get_ch_state(p), p->l3id);
02391 goto CLEAN_CH;
02392 }
02393
02394
02395 p->need_hangup = 0;
02396 p->need_queue_hangup = 0;
02397 p->need_busy = 0;
02398
02399
02400 if (!p->bc->nt)
02401 stop_bc_tones(p);
02402
02403 bc->out_cause = ast->hangupcause ? ast->hangupcause : AST_CAUSE_NORMAL_CLEARING;
02404
02405 if ((varcause = pbx_builtin_getvar_helper(ast, "HANGUPCAUSE")) ||
02406 (varcause = pbx_builtin_getvar_helper(ast, "PRI_CAUSE"))) {
02407 int tmpcause = atoi(varcause);
02408 bc->out_cause = tmpcause ? tmpcause : AST_CAUSE_NORMAL_CLEARING;
02409 }
02410
02411 chan_misdn_log(1, bc->port, "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n", p->bc ? p->bc->pid : -1, ast->context, ast->exten, ast->cid.cid_num, misdn_get_ch_state(p));
02412 chan_misdn_log(3, bc->port, " --> l3id:%x\n", p->l3id);
02413 chan_misdn_log(3, bc->port, " --> cause:%d\n", bc->cause);
02414 chan_misdn_log(2, bc->port, " --> out_cause:%d\n", bc->out_cause);
02415 chan_misdn_log(2, bc->port, " --> state:%s\n", misdn_get_ch_state(p));
02416
02417 switch (p->state) {
02418 case MISDN_INCOMING_SETUP:
02419 case MISDN_CALLING:
02420
02421
02422
02423 ast_log(LOG_NOTICE, "release channel, in CALLING/INCOMING_SETUP state.. no other events happened\n");
02424 release_chan(bc);
02425
02426 p->state = MISDN_CLEANING;
02427 if (bc->need_release_complete)
02428 misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE);
02429 break;
02430 case MISDN_HOLDED:
02431 case MISDN_DIALING:
02432 start_bc_tones(p);
02433 hanguptone_indicate(p);
02434
02435 if (bc->need_disconnect)
02436 misdn_lib_send_event( bc, EVENT_DISCONNECT);
02437 break;
02438 case MISDN_CALLING_ACKNOWLEDGE:
02439 start_bc_tones(p);
02440 hanguptone_indicate(p);
02441
02442 if (bc->need_disconnect)
02443 misdn_lib_send_event( bc, EVENT_DISCONNECT);
02444 break;
02445
02446 case MISDN_ALERTING:
02447 case MISDN_PROGRESS:
02448 case MISDN_PROCEEDING:
02449 if (p->originator != ORG_AST)
02450 hanguptone_indicate(p);
02451
02452
02453 if (bc->need_disconnect)
02454 misdn_lib_send_event( bc, EVENT_DISCONNECT);
02455 break;
02456 case MISDN_CONNECTED:
02457 case MISDN_PRECONNECTED:
02458
02459 if (p->bc->nt) {
02460 start_bc_tones(p);
02461 hanguptone_indicate(p);
02462 p->bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
02463 }
02464 if (bc->need_disconnect)
02465 misdn_lib_send_event( bc, EVENT_DISCONNECT);
02466
02467
02468 break;
02469 case MISDN_DISCONNECTED:
02470 if (bc->need_release)
02471 misdn_lib_send_event( bc, EVENT_RELEASE);
02472 p->state = MISDN_CLEANING;
02473 break;
02474
02475 case MISDN_RELEASED:
02476 case MISDN_CLEANING:
02477 p->state = MISDN_CLEANING;
02478 break;
02479
02480 case MISDN_BUSY:
02481 break;
02482
02483 case MISDN_HOLD_DISCONNECT:
02484
02485 chan_misdn_log(1, bc->port, " --> cause %d\n", bc->cause);
02486 chan_misdn_log(1, bc->port, " --> out_cause %d\n", bc->out_cause);
02487
02488 bc->out_cause = -1;
02489 if (bc->need_release)
02490 misdn_lib_send_event(bc, EVENT_RELEASE);
02491 p->state = MISDN_CLEANING;
02492 break;
02493 default:
02494 if (bc->nt) {
02495 bc->out_cause = -1;
02496 if (bc->need_release)
02497 misdn_lib_send_event(bc, EVENT_RELEASE);
02498 p->state = MISDN_CLEANING;
02499 } else {
02500 if (bc->need_disconnect)
02501 misdn_lib_send_event(bc, EVENT_DISCONNECT);
02502 }
02503 }
02504
02505 p->state = MISDN_CLEANING;
02506
02507 chan_misdn_log(3, bc->port, " --> Channel: %s hanguped new state:%s\n", ast->name, misdn_get_ch_state(p));
02508
02509 return 0;
02510 }
02511
02512
02513 static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame)
02514 {
02515 struct ast_frame *f,*f2;
02516
02517 if (tmp->trans) {
02518 f2 = ast_translate(tmp->trans, frame, 0);
02519 f = ast_dsp_process(tmp->ast, tmp->dsp, f2);
02520 } else {
02521 chan_misdn_log(0, tmp->bc->port, "No T-Path found\n");
02522 return NULL;
02523 }
02524
02525 if (!f || (f->frametype != AST_FRAME_DTMF))
02526 return frame;
02527
02528 ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c\n", f->subclass);
02529
02530 if (tmp->faxdetect && (f->subclass == 'f')) {
02531
02532 if (!tmp->faxhandled) {
02533 struct ast_channel *ast = tmp->ast;
02534 tmp->faxhandled++;
02535 chan_misdn_log(0, tmp->bc->port, "Fax detected, preparing %s for fax transfer.\n", ast->name);
02536 tmp->bc->rxgain = 0;
02537 isdn_lib_update_rxgain(tmp->bc);
02538 tmp->bc->txgain = 0;
02539 isdn_lib_update_txgain(tmp->bc);
02540 #ifdef MISDN_1_2
02541 *tmp->bc->pipeline = 0;
02542 #else
02543 tmp->bc->ec_enable = 0;
02544 #endif
02545 isdn_lib_update_ec(tmp->bc);
02546 isdn_lib_stop_dtmf(tmp->bc);
02547 switch (tmp->faxdetect) {
02548 case 1:
02549 if (strcmp(ast->exten, "fax")) {
02550 char *context;
02551 char context_tmp[BUFFERSIZE];
02552 misdn_cfg_get(tmp->bc->port, MISDN_CFG_FAXDETECT_CONTEXT, &context_tmp, sizeof(context_tmp));
02553 context = ast_strlen_zero(context_tmp) ? (ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext) : context_tmp;
02554 if (ast_exists_extension(ast, context, "fax", 1, ast->cid.cid_num)) {
02555 if (option_verbose > 2)
02556 ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension (context:%s)\n", ast->name, context);
02557
02558 pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast->exten);
02559 if (ast_async_goto(ast, context, "fax", 1))
02560 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, context);
02561 } else
02562 ast_log(LOG_NOTICE, "Fax detected, but no fax extension ctx:%s exten:%s\n", context, ast->exten);
02563 } else {
02564 ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
02565 }
02566 break;
02567 case 2:
02568 ast_verbose(VERBOSE_PREFIX_3 "Not redirecting %s to fax extension, nojump is set.\n", ast->name);
02569 break;
02570 }
02571 } else {
02572 ast_log(LOG_DEBUG, "Fax already handled\n");
02573 }
02574 }
02575
02576 if (tmp->ast_dsp && (f->subclass != 'f')) {
02577 chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n", f->subclass);
02578 }
02579
02580 return f;
02581 }
02582
02583
02584 static struct ast_frame *misdn_read(struct ast_channel *ast)
02585 {
02586 struct chan_list *tmp;
02587 fd_set rrfs;
02588 struct timeval tv;
02589 int len, t;
02590
02591 if (!ast) {
02592 chan_misdn_log(1, 0, "misdn_read called without ast\n");
02593 return NULL;
02594 }
02595 if (!(tmp = MISDN_ASTERISK_TECH_PVT(ast))) {
02596 chan_misdn_log(1, 0, "misdn_read called without ast->pvt\n");
02597 return NULL;
02598 }
02599
02600 if (!tmp->bc && !(tmp->state == MISDN_HOLDED)) {
02601 chan_misdn_log(1, 0, "misdn_read called without bc\n");
02602 return NULL;
02603 }
02604
02605 tv.tv_sec=0;
02606 tv.tv_usec=20000;
02607
02608 FD_ZERO(&rrfs);
02609 FD_SET(tmp->pipe[0],&rrfs);
02610
02611 t=select(FD_SETSIZE,&rrfs,NULL, NULL,&tv);
02612
02613 if (!t) {
02614 chan_misdn_log(3, tmp->bc->port, "read Select Timed out\n");
02615 len=160;
02616 }
02617
02618 if (t<0) {
02619 chan_misdn_log(-1, tmp->bc->port, "Select Error (err=%s)\n",strerror(errno));
02620 return NULL;
02621 }
02622
02623 if (FD_ISSET(tmp->pipe[0],&rrfs)) {
02624 len=read(tmp->pipe[0],tmp->ast_rd_buf,sizeof(tmp->ast_rd_buf));
02625
02626 if (len<=0) {
02627
02628 chan_misdn_log(2,tmp->bc->port,"misdn_read: Pipe closed, hanging up\n");
02629 return NULL;
02630 }
02631
02632 } else {
02633 return NULL;
02634 }
02635
02636 tmp->frame.frametype = AST_FRAME_VOICE;
02637 tmp->frame.subclass = AST_FORMAT_ALAW;
02638 tmp->frame.datalen = len;
02639 tmp->frame.samples = len;
02640 tmp->frame.mallocd = 0;
02641 tmp->frame.offset = 0;
02642 tmp->frame.delivery = ast_tv(0,0);
02643 tmp->frame.src = NULL;
02644 tmp->frame.data = tmp->ast_rd_buf;
02645
02646 if (tmp->faxdetect && !tmp->faxhandled) {
02647 if (tmp->faxdetect_timeout) {
02648 if (ast_tvzero(tmp->faxdetect_tv)) {
02649 tmp->faxdetect_tv = ast_tvnow();
02650 chan_misdn_log(2, tmp->bc->port, "faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout);
02651 return process_ast_dsp(tmp, &tmp->frame);
02652 } else {
02653 struct timeval tv_now = ast_tvnow();
02654 int diff = ast_tvdiff_ms(tv_now, tmp->faxdetect_tv);
02655 if (diff <= (tmp->faxdetect_timeout * 1000)) {
02656 chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ...\n");
02657 return process_ast_dsp(tmp, &tmp->frame);
02658 } else {
02659 chan_misdn_log(2, tmp->bc->port, "faxdetect: stopping detection (time ran out) ...\n");
02660 tmp->faxdetect = 0;
02661 return &tmp->frame;
02662 }
02663 }
02664 } else {
02665 chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ... (no timeout)\n");
02666 return process_ast_dsp(tmp, &tmp->frame);
02667 }
02668 } else {
02669 if (tmp->ast_dsp)
02670 return process_ast_dsp(tmp, &tmp->frame);
02671 else
02672 return &tmp->frame;
02673 }
02674 }
02675
02676
02677 static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
02678 {
02679 struct chan_list *ch;
02680 int i = 0;
02681
02682 if (!ast || ! (ch = MISDN_ASTERISK_TECH_PVT(ast)) ) return -1;
02683
02684 if (ch->state == MISDN_HOLDED) {
02685 chan_misdn_log(7, 0, "misdn_write: Returning because holded\n");
02686 return 0;
02687 }
02688
02689 if (!ch->bc ) {
02690 ast_log(LOG_WARNING, "private but no bc\n");
02691 return -1;
02692 }
02693
02694 if (ch->notxtone) {
02695 chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxtone\n");
02696 return 0;
02697 }
02698
02699
02700 if (!frame->subclass) {
02701 chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n");
02702 return 0;
02703 }
02704
02705 if (!(frame->subclass & prefformat)) {
02706
02707 chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
02708 return 0;
02709 }
02710
02711
02712 if (!frame->samples ) {
02713 chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n");
02714
02715 if (!strcmp(frame->src,"ast_prod")) {
02716 chan_misdn_log(1, ch->bc->port, "misdn_write: state (%s) prodded.\n", misdn_get_ch_state(ch));
02717
02718 if (ch->ts) {
02719 chan_misdn_log(4, ch->bc->port, "Starting Playtones\n");
02720 misdn_lib_tone_generator_start(ch->bc);
02721 }
02722 return 0;
02723 }
02724
02725 return -1;
02726 }
02727
02728 if ( ! ch->bc->addr ) {
02729 chan_misdn_log(8, ch->bc->port, "misdn_write: no addr for bc dropping:%d\n", frame->samples);
02730 return 0;
02731 }
02732
02733 #if MISDN_DEBUG
02734 {
02735 int i, max = 5 > frame->samples ? frame->samples : 5;
02736
02737 printf("write2mISDN %p %d bytes: ", p, frame->samples);
02738
02739 for (i = 0; i < max; i++)
02740 printf("%2.2x ", ((char*) frame->data)[i]);
02741 printf ("\n");
02742 }
02743 #endif
02744
02745 switch (ch->bc->bc_state) {
02746 case BCHAN_ACTIVATED:
02747 case BCHAN_BRIDGED:
02748 break;
02749 default:
02750 if (!ch->dropped_frame_cnt)
02751 chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n", frame->samples, ch->bc->addr, ast->exten, ast->cid.cid_num, misdn_get_ch_state( ch), ch->bc->bc_state, ch->bc->l3_id);
02752
02753 ch->dropped_frame_cnt++;
02754 if (ch->dropped_frame_cnt > 100) {
02755 ch->dropped_frame_cnt = 0;
02756 chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x dropped > 100 frames!\n", frame->samples, ch->bc->addr);
02757 }
02758
02759 return 0;
02760 }
02761
02762 chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples);
02763 if ( !ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability) ) {
02764
02765 if (misdn_jb_fill(ch->jb, frame->data, frame->samples) < 0) {
02766 if (ch->bc->active)
02767 cb_log(0, ch->bc->port, "Misdn Jitterbuffer Overflow.\n");
02768 }
02769
02770 } else {
02771
02772 i = misdn_lib_tx2misdn_frm(ch->bc, frame->data, frame->samples);
02773 }
02774
02775 return 0;
02776 }
02777
02778
02779
02780
02781 static enum ast_bridge_result misdn_bridge (struct ast_channel *c0,
02782 struct ast_channel *c1, int flags,
02783 struct ast_frame **fo,
02784 struct ast_channel **rc,
02785 int timeoutms)
02786
02787 {
02788 struct chan_list *ch1, *ch2;
02789 struct ast_channel *carr[2], *who;
02790 int to = -1;
02791 struct ast_frame *f;
02792 int p1_b, p2_b;
02793 int bridging;
02794
02795 ch1 = get_chan_by_ast(c0);
02796 ch2 = get_chan_by_ast(c1);
02797
02798 carr[0] = c0;
02799 carr[1] = c1;
02800
02801 if (!(ch1 && ch2))
02802 return -1;
02803
02804 misdn_cfg_get(ch1->bc->port, MISDN_CFG_BRIDGING, &p1_b, sizeof(int));
02805 misdn_cfg_get(ch2->bc->port, MISDN_CFG_BRIDGING, &p2_b, sizeof(int));
02806
02807 if (! p1_b || ! p2_b) {
02808 ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n");
02809 return AST_BRIDGE_FAILED;
02810 }
02811
02812 misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
02813 if (bridging) {
02814
02815 chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid + 1);
02816 misdn_lib_bridge(ch1->bc, ch2->bc);
02817 }
02818
02819 if (option_verbose > 2)
02820 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
02821
02822 chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between %s and %s\n", ch1->bc->oad, ch2->bc->oad);
02823
02824 if (! (flags & AST_BRIDGE_DTMF_CHANNEL_0) )
02825 ch1->ignore_dtmf = 1;
02826
02827 if (! (flags & AST_BRIDGE_DTMF_CHANNEL_1) )
02828 ch2->ignore_dtmf = 1;
02829
02830 for (;;) {
02831 to = -1;
02832 who = ast_waitfor_n(carr, 2, &to);
02833
02834 if (!who) {
02835 ast_log(LOG_NOTICE, "misdn_bridge: empty read, breaking out\n");
02836 break;
02837 }
02838 f = ast_read(who);
02839
02840 if (!f || f->frametype == AST_FRAME_CONTROL) {
02841
02842
02843 if (!f)
02844 chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n");
02845 else
02846 chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass);
02847
02848 *fo = f;
02849 *rc = who;
02850 break;
02851 }
02852
02853 if ( f->frametype == AST_FRAME_DTMF ) {
02854 chan_misdn_log(1, 0, "Read DTMF %d from %s\n", f->subclass, who->exten);
02855
02856 *fo = f;
02857 *rc = who;
02858 break;
02859 }
02860
02861 #if 0
02862 if (f->frametype == AST_FRAME_VOICE) {
02863 chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1);
02864
02865 continue;
02866 }
02867 #endif
02868
02869 if (who == c0) {
02870 ast_write(c1, f);
02871 }
02872 else {
02873 ast_write(c0, f);
02874 }
02875 }
02876
02877 chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid + 1);
02878
02879 misdn_lib_split_bridge(ch1->bc, ch2->bc);
02880
02881 return AST_BRIDGE_COMPLETE;
02882 }
02883
02884
02885
02886 static int dialtone_indicate(struct chan_list *cl)
02887 {
02888 const struct ind_tone_zone_sound *ts = NULL;
02889 struct ast_channel *ast = cl->ast;
02890 int nd = 0;
02891
02892 if (!ast) {
02893 chan_misdn_log(0, cl->bc->port, "No Ast in dialtone_indicate\n");
02894 return -1;
02895 }
02896
02897 misdn_cfg_get(cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd));
02898
02899 if (nd) {
02900 chan_misdn_log(1, cl->bc->port, "Not sending Dialtone, because config wants it\n");
02901 return 0;
02902 }
02903
02904 chan_misdn_log(3, cl->bc->port, " --> Dial\n");
02905 ts = ast_get_indication_tone(ast->zone, "dial");
02906 cl->ts = ts;
02907
02908 if (ts) {
02909 cl->notxtone = 0;
02910 cl->norxtone = 0;
02911
02912 ast_playtones_start(ast, 0, ts->data, 0);
02913 }
02914
02915 return 0;
02916 }
02917
02918 static int hanguptone_indicate(struct chan_list *cl)
02919 {
02920 misdn_lib_send_tone(cl->bc, TONE_HANGUP);
02921 return 0;
02922 }
02923
02924 static int stop_indicate(struct chan_list *cl)
02925 {
02926 struct ast_channel *ast = cl->ast;
02927
02928 if (!ast) {
02929 chan_misdn_log(0, cl->bc->port, "No Ast in stop_indicate\n");
02930 return -1;
02931 }
02932
02933 chan_misdn_log(3, cl->bc->port, " --> None\n");
02934 misdn_lib_tone_generator_stop(cl->bc);
02935 ast_playtones_stop(ast);
02936
02937 cl->ts = NULL;
02938
02939
02940 return 0;
02941 }
02942
02943
02944 static int start_bc_tones(struct chan_list* cl)
02945 {
02946 misdn_lib_tone_generator_stop(cl->bc);
02947 cl->notxtone = 0;
02948 cl->norxtone = 0;
02949 return 0;
02950 }
02951
02952 static int stop_bc_tones(struct chan_list *cl)
02953 {
02954 if (!cl) return -1;
02955
02956 cl->notxtone = 1;
02957 cl->norxtone = 1;
02958
02959 return 0;
02960 }
02961
02962
02963 static struct chan_list *init_chan_list(int orig)
02964 {
02965 struct chan_list *cl;
02966
02967 cl = calloc(1, sizeof(struct chan_list));
02968 if (!cl) {
02969 chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
02970 return NULL;
02971 }
02972
02973 cl->originator = orig;
02974 cl->need_queue_hangup = 1;
02975 cl->need_hangup = 1;
02976 cl->need_busy = 1;
02977 cl->overlap_dial_task = -1;
02978
02979 return cl;
02980 }
02981
02982 static struct ast_channel *misdn_request(const char *type, int format, void *data, int *cause)
02983 {
02984 struct ast_channel *tmp = NULL;
02985 char group[BUFFERSIZE + 1] = "";
02986 char dial_str[128];
02987 char *buf2 = ast_strdupa(data);
02988 char *ext;
02989 char *port_str;
02990 char *p = NULL;
02991 int channel = 0;
02992 int port = 0;
02993 struct misdn_bchannel *newbc = NULL;
02994 int dec = 0;
02995
02996 struct chan_list *cl = init_chan_list(ORG_AST);
02997
02998 snprintf(dial_str, sizeof(dial_str), "%s/%s", misdn_type, (char *) data);
02999
03000
03001
03002
03003
03004
03005
03006
03007
03008 port_str = strsep(&buf2, "/");
03009 if (!ast_strlen_zero(port_str)) {
03010 if (port_str[0] == 'g' && port_str[1] == ':' ) {
03011
03012 port_str += 2;
03013 ast_copy_string(group, port_str, sizeof(group));
03014 chan_misdn_log(2, 0, " --> Group Call group: %s\n", group);
03015 } else if ((p = strchr(port_str, ':'))) {
03016
03017 *p = 0;
03018 channel = atoi(++p);
03019 port = atoi(port_str);
03020 chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
03021 } else {
03022 port = atoi(port_str);
03023 }
03024 } else {
03025 ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT Port or Group, check extensions.conf\n", dial_str);
03026 return NULL;
03027 }
03028
03029 ext = strsep(&buf2, "/");
03030 if (!ext) {
03031 ext = "";
03032 }
03033
03034 if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) {
03035 chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n");
03036 dec = 1;
03037 }
03038
03039 if (!ast_strlen_zero(group)) {
03040 char cfg_group[BUFFERSIZE + 1];
03041 struct robin_list *rr = NULL;
03042
03043 if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) {
03044 chan_misdn_log(4, port, " --> STARTING ROUND ROBIN...\n");
03045 rr = get_robin_position(group);
03046 }
03047
03048 if (rr) {
03049 int port_start = 0;
03050 int port_bak = rr->port;
03051 int chan_bak = rr->channel;
03052
03053 if (!rr->port)
03054 rr->port = misdn_cfg_get_next_port_spin(rr->port);
03055
03056 for (; rr->port > 0; rr->port = misdn_cfg_get_next_port_spin(rr->port)) {
03057 int port_up;
03058 int check;
03059 int max_chan;
03060 int last_chance = 0;
03061
03062 misdn_cfg_get(rr->port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
03063 if (strcasecmp(cfg_group, group))
03064 continue;
03065
03066 misdn_cfg_get(rr->port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(int));
03067 port_up = misdn_lib_port_up(rr->port, check);
03068
03069 if (check && !port_up)
03070 chan_misdn_log(1, rr->port, "L1 is not Up on this Port\n");
03071
03072 if (check && port_up < 0)
03073 ast_log(LOG_WARNING,"This port (%d) is blocked\n", rr->port);
03074
03075 if ((port_start == rr->port) && (port_up <= 0))
03076 break;
03077
03078 if (!port_start)
03079 port_start = rr->port;
03080
03081 if (port_up <= 0)
03082 continue;
03083
03084 max_chan = misdn_lib_get_maxchans(rr->port);
03085
03086 for (++rr->channel; !last_chance && rr->channel <= max_chan; ++rr->channel) {
03087 if (rr->port == port_bak && rr->channel == chan_bak)
03088 last_chance = 1;
03089
03090 chan_misdn_log(1, 0, "trying port:%d channel:%d\n", rr->port, rr->channel);
03091 newbc = misdn_lib_get_free_bc(rr->port, rr->channel, 0, 0);
03092 if (newbc) {
03093 chan_misdn_log(4, rr->port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
03094 if (port_up)
03095 chan_misdn_log(4, rr->port, "portup:%d\n", port_up);
03096 port = rr->port;
03097 break;
03098 }
03099 }
03100
03101 if (newbc || last_chance)
03102 break;
03103
03104 rr->channel = 0;
03105 }
03106 if (!newbc) {
03107 rr->port = port_bak;
03108 rr->channel = chan_bak;
03109 }
03110 } else {
03111 for (port = misdn_cfg_get_next_port(0); port > 0;
03112 port = misdn_cfg_get_next_port(port)) {
03113
03114 misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
03115
03116 chan_misdn_log(3, port, "Group [%s] Port [%d]\n", group, port);
03117 if (!strcasecmp(cfg_group, group)) {
03118 int port_up;
03119 int check;
03120 misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(int));
03121 port_up = misdn_lib_port_up(port, check);
03122
03123 chan_misdn_log(4, port, "portup:%d\n", port_up);
03124
03125 if (port_up > 0) {
03126 newbc = misdn_lib_get_free_bc(port, 0, 0, dec);
03127 if (newbc)
03128 break;
03129 }
03130 }
03131 }
03132 }
03133
03134
03135 if (!newbc) {
03136 ast_log(LOG_WARNING,
03137 "Could not Dial out on group '%s'.\n"
03138 "\tEither the L2 and L1 on all of these ports where DOWN (see 'show application misdn_check_l2l1')\n"
03139 "\tOr there was no free channel on none of the ports\n\n"
03140 , group);
03141 return NULL;
03142 }
03143 } else {
03144 if (channel)
03145 chan_misdn_log(1, port, " --> preselected_channel: %d\n", channel);
03146 newbc = misdn_lib_get_free_bc(port, channel, 0, dec);
03147
03148 if (!newbc) {
03149 ast_log(LOG_WARNING, "Could not create channel on port:%d with extensions:%s\n", port, ext);
03150 return NULL;
03151 }
03152 }
03153
03154
03155
03156 cl->bc = newbc;
03157
03158 tmp = misdn_new(cl, AST_STATE_RESERVED, ext, NULL, format, port, channel);
03159 if (!tmp) {
03160 ast_log(LOG_ERROR,"Could not create Asterisk object\n");
03161 return NULL;
03162 }
03163
03164 cl->ast=tmp;
03165
03166
03167 cl_queue_chan(&cl_te, cl) ;
03168
03169
03170 read_config(cl, ORG_AST);
03171
03172
03173 cl->need_hangup = 0;
03174
03175 return tmp;
03176 }
03177
03178
03179 static int misdn_send_text(struct ast_channel *chan, const char *text)
03180 {
03181 struct chan_list *tmp = chan->tech_pvt;
03182
03183 if (tmp && tmp->bc) {
03184 ast_copy_string(tmp->bc->display, text, sizeof(tmp->bc->display));
03185 misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
03186 } else {
03187 ast_log(LOG_WARNING, "No chan_list but send_text request?\n");
03188 return -1;
03189 }
03190
03191 return 0;
03192 }
03193
03194 static struct ast_channel_tech misdn_tech = {
03195 .type = "mISDN",
03196 .description = "Channel driver for mISDN Support (Bri/Pri)",
03197 .capabilities = AST_FORMAT_ALAW ,
03198 .requester = misdn_request,
03199 .send_digit_begin = misdn_digit_begin,
03200 .send_digit_end = misdn_digit_end,
03201 .call = misdn_call,
03202 .bridge = misdn_bridge,
03203 .hangup = misdn_hangup,
03204 .answer = misdn_answer,
03205 .read = misdn_read,
03206 .write = misdn_write,
03207 .indicate = misdn_indication,
03208 .fixup = misdn_fixup,
03209 .send_text = misdn_send_text,
03210 .properties = 0
03211 };
03212
03213 static struct ast_channel_tech misdn_tech_wo_bridge = {
03214 .type = "mISDN",
03215 .description = "Channel driver for mISDN Support (Bri/Pri)",
03216 .capabilities = AST_FORMAT_ALAW ,
03217 .requester = misdn_request,
03218 .send_digit_begin = misdn_digit_begin,
03219 .send_digit_end = misdn_digit_end,
03220 .call = misdn_call,
03221 .hangup = misdn_hangup,
03222 .answer = misdn_answer,
03223 .read = misdn_read,
03224 .write = misdn_write,
03225 .indicate = misdn_indication,
03226 .fixup = misdn_fixup,
03227 .send_text = misdn_send_text,
03228 .properties = 0
03229 };
03230
03231
03232 static int glob_channel = 0;
03233
03234 static void update_name(struct ast_channel *tmp, int port, int c)
03235 {
03236 int chan_offset = 0;
03237 int tmp_port = misdn_cfg_get_next_port(0);
03238 for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
03239 if (tmp_port == port)
03240 break;
03241 chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
03242 }
03243 if (c < 0)
03244 c = 0;
03245
03246 ast_string_field_build(tmp, name, "%s/%d-u%d",
03247 misdn_type, chan_offset + c, glob_channel++);
03248
03249 chan_misdn_log(3, port, " --> updating channel name to [%s]\n", tmp->name);
03250 }
03251
03252 static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char *exten, char *callerid, int format, int port, int c)
03253 {
03254 struct ast_channel *tmp;
03255 char *cid_name = 0, *cid_num = 0;
03256 int chan_offset = 0;
03257 int tmp_port = misdn_cfg_get_next_port(0);
03258 int bridging;
03259
03260 for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
03261 if (tmp_port == port)
03262 break;
03263 chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
03264 }
03265 if (c < 0)
03266 c = 0;
03267
03268 if (callerid) {
03269 ast_callerid_parse(callerid, &cid_name, &cid_num);
03270 }
03271
03272 tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", 0, "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
03273 if (tmp) {
03274 chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s\n", exten, callerid);
03275
03276 tmp->nativeformats = prefformat;
03277
03278 tmp->readformat = format;
03279 tmp->rawreadformat = format;
03280 tmp->writeformat = format;
03281 tmp->rawwriteformat = format;
03282
03283 tmp->tech_pvt = chlist;
03284
03285 misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
03286
03287 if (bridging)
03288 tmp->tech = &misdn_tech;
03289 else
03290 tmp->tech = &misdn_tech_wo_bridge;
03291
03292 tmp->writeformat = format;
03293 tmp->readformat = format;
03294 tmp->priority=1;
03295
03296 if (exten)
03297 ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
03298 else
03299 chan_misdn_log(1, 0, "misdn_new: no exten given.\n");
03300
03301 if (callerid)
03302
03303
03304 tmp->cid.cid_ani = ast_strdup(cid_num);
03305
03306 if (pipe(chlist->pipe) < 0)
03307 perror("Pipe failed\n");
03308 tmp->fds[0] = chlist->pipe[0];
03309
03310 if (state == AST_STATE_RING)
03311 tmp->rings = 1;
03312 else
03313 tmp->rings = 0;
03314
03315 } else {
03316 chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
03317 }
03318
03319 return tmp;
03320 }
03321
03322 static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc)
03323 {
03324 struct chan_list *help = list;
03325 for (; help; help = help->next) {
03326 if (help->bc == bc) return help;
03327 }
03328
03329 chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n", bc->oad, bc->dad);
03330
03331 return NULL;
03332 }
03333
03334 static struct chan_list *find_chan_by_pid(struct chan_list *list, int pid)
03335 {
03336 struct chan_list *help = list;
03337 for (; help; help = help->next) {
03338 if ( help->bc && (help->bc->pid == pid) ) return help;
03339 }
03340
03341 chan_misdn_log(6, 0, "$$$ find_chan: No channel found for pid:%d\n", pid);
03342
03343 return NULL;
03344 }
03345
03346 static struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc)
03347 {
03348 struct chan_list *help = list;
03349
03350 if (bc->pri) return NULL;
03351
03352 chan_misdn_log(6, bc->port, "$$$ find_holded: channel:%d oad:%s dad:%s\n", bc->channel, bc->oad, bc->dad);
03353 for (;help; help = help->next) {
03354 chan_misdn_log(4, bc->port, "$$$ find_holded: --> holded:%d channel:%d\n", help->state == MISDN_HOLDED, help->hold_info.channel);
03355 if ((help->state == MISDN_HOLDED) &&
03356 (help->hold_info.port == bc->port))
03357 return help;
03358 }
03359 chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n", bc->oad, bc->dad);
03360
03361 return NULL;
03362 }
03363
03364
03365 static struct chan_list *find_holded_l3(struct chan_list *list, unsigned long l3_id, int w)
03366 {
03367 struct chan_list *help = list;
03368
03369 for (; help; help = help->next) {
03370 if ( (help->state == MISDN_HOLDED) &&
03371 (help->l3id == l3_id)
03372 )
03373 return help;
03374 }
03375
03376 return NULL;
03377 }
03378
03379 static void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
03380 {
03381 chan_misdn_log(4, chan->bc ? chan->bc->port : 0, "* Queuing chan %p\n", chan);
03382
03383 ast_mutex_lock(&cl_te_lock);
03384 if (!*list) {
03385 *list = chan;
03386 } else {
03387 struct chan_list *help = *list;
03388 for (; help->next; help = help->next);
03389 help->next = chan;
03390 }
03391 chan->next = NULL;
03392 ast_mutex_unlock(&cl_te_lock);
03393 }
03394
03395 static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan)
03396 {
03397 struct chan_list *help;
03398
03399 if (chan->dsp)
03400 ast_dsp_free(chan->dsp);
03401 if (chan->trans)
03402 ast_translator_free_path(chan->trans);
03403
03404 ast_mutex_lock(&cl_te_lock);
03405 if (!*list) {
03406 ast_mutex_unlock(&cl_te_lock);
03407 return;
03408 }
03409
03410 if (*list == chan) {
03411 *list = (*list)->next;
03412 ast_mutex_unlock(&cl_te_lock);
03413 return;
03414 }
03415
03416 for (help = *list; help->next; help = help->next) {
03417 if (help->next == chan) {
03418 help->next = help->next->next;
03419 ast_mutex_unlock(&cl_te_lock);
03420 return;
03421 }
03422 }
03423
03424 ast_mutex_unlock(&cl_te_lock);
03425 }
03426
03427
03428
03429
03430 static int pbx_start_chan(struct chan_list *ch)
03431 {
03432 int ret = ast_pbx_start(ch->ast);
03433
03434 if (ret >= 0)
03435 ch->need_hangup = 0;
03436 else
03437 ch->need_hangup = 1;
03438
03439 return ret;
03440 }
03441
03442 static void hangup_chan(struct chan_list *ch)
03443 {
03444 int port = ch ? (ch->bc ? ch->bc->port : 0) : 0;
03445 if (!ch) {
03446 cb_log(1, 0, "Cannot hangup chan, no ch\n");
03447 return;
03448 }
03449
03450 cb_log(5, port, "hangup_chan called\n");
03451
03452 if (ch->need_hangup) {
03453 cb_log(2, port, " --> hangup\n");
03454 send_cause2ast(ch->ast, ch->bc, ch);
03455 ch->need_hangup = 0;
03456 ch->need_queue_hangup = 0;
03457 if (ch->ast)
03458 ast_hangup(ch->ast);
03459 return;
03460 }
03461
03462 if (!ch->need_queue_hangup) {
03463 cb_log(2, port, " --> No need to queue hangup\n");
03464 }
03465
03466 ch->need_queue_hangup = 0;
03467 if (ch->ast) {
03468 send_cause2ast(ch->ast, ch->bc, ch);
03469
03470 if (ch->ast)
03471 ast_queue_hangup(ch->ast);
03472 cb_log(2, port, " --> queue_hangup\n");
03473 } else {
03474 cb_log(1, port, "Cannot hangup chan, no ast\n");
03475 }
03476 }
03477
03478
03479 static void release_chan(struct misdn_bchannel *bc) {
03480 struct ast_channel *ast = NULL;
03481
03482 ast_mutex_lock(&release_lock);
03483 {
03484 struct chan_list *ch=find_chan_by_bc(cl_te, bc);
03485 if (!ch) {
03486 chan_misdn_log(1, bc->port, "release_chan: Ch not found!\n");
03487 ast_mutex_unlock(&release_lock);
03488 return;
03489 }
03490
03491 if (ch->ast) {
03492 ast = ch->ast;
03493 }
03494
03495 chan_misdn_log(5, bc->port, "release_chan: bc with l3id: %x\n", bc->l3_id);
03496
03497
03498 if (ch->jb ) {
03499 misdn_jb_destroy(ch->jb);
03500 ch->jb = NULL;
03501 } else {
03502 if (!bc->nojitter)
03503 chan_misdn_log(5, bc->port, "Jitterbuffer already destroyed.\n");
03504 }
03505
03506 if (ch->overlap_dial) {
03507 if (ch->overlap_dial_task != -1) {
03508 misdn_tasks_remove(ch->overlap_dial_task);
03509 ch->overlap_dial_task = -1;
03510 }
03511 ast_mutex_destroy(&ch->overlap_tv_lock);
03512 }
03513
03514 if (ch->originator == ORG_AST) {
03515 misdn_out_calls[bc->port]--;
03516 } else {
03517 misdn_in_calls[bc->port]--;
03518 }
03519
03520 if (ch) {
03521 close(ch->pipe[0]);
03522 close(ch->pipe[1]);
03523
03524 if (ast && MISDN_ASTERISK_TECH_PVT(ast)) {
03525 chan_misdn_log(1, bc->port, "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s state: %s\n", bc ? bc->pid : -1, ast->context, ast->exten, ast->cid.cid_num, misdn_get_ch_state(ch));
03526 chan_misdn_log(3, bc->port, " --> * State Down\n");
03527 MISDN_ASTERISK_TECH_PVT(ast) = NULL;
03528
03529 if (ast->_state != AST_STATE_RESERVED) {
03530 chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
03531 ast_setstate(ast, AST_STATE_DOWN);
03532 }
03533 }
03534
03535 ch->state = MISDN_CLEANING;
03536 cl_dequeue_chan(&cl_te, ch);
03537
03538 free(ch);
03539 } else {
03540
03541 }
03542 }
03543 ast_mutex_unlock(&release_lock);
03544
03545 }
03546
03547 static void misdn_transfer_bc(struct chan_list *tmp_ch, struct chan_list *holded_chan)
03548 {
03549 chan_misdn_log(4, 0, "TRANSFERRING %s to %s\n", holded_chan->ast->name, tmp_ch->ast->name);
03550
03551 tmp_ch->state = MISDN_HOLD_DISCONNECT;
03552
03553 ast_moh_stop(ast_bridged_channel(holded_chan->ast));
03554
03555 holded_chan->state=MISDN_CONNECTED;
03556
03557 ast_channel_masquerade(holded_chan->ast, ast_bridged_channel(tmp_ch->ast));
03558 }
03559
03560
03561 static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch, struct ast_channel *ast)
03562 {
03563 char predial[256]="";
03564 char *p = predial;
03565
03566 struct ast_frame fr;
03567
03568 strncpy(predial, ast->exten, sizeof(predial) -1 );
03569
03570 ch->state = MISDN_DIALING;
03571
03572 if (!ch->noautorespond_on_setup) {
03573 if (bc->nt) {
03574 int ret;
03575 ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
03576 } else {
03577 int ret;
03578 if ( misdn_lib_is_ptp(bc->port)) {
03579 ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
03580 } else {
03581 ret = misdn_lib_send_event(bc, EVENT_PROCEEDING );
03582 }
03583 }
03584 } else {
03585 ch->state = MISDN_INCOMING_SETUP;
03586 }
03587
03588 chan_misdn_log(1, bc->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, ast->cid.cid_num);
03589
03590 strcpy(ast->exten, "s");
03591
03592 if (pbx_start_chan(ch) < 0) {
03593 ast = NULL;
03594 hangup_chan(ch);
03595 hanguptone_indicate(ch);
03596
03597 if (bc->nt)
03598 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
03599 else
03600 misdn_lib_send_event(bc, EVENT_DISCONNECT );
03601 }
03602
03603
03604 while (!ast_strlen_zero(p) ) {
03605 fr.frametype = AST_FRAME_DTMF;
03606 fr.subclass = *p;
03607 fr.src = NULL;
03608 fr.data = NULL;
03609 fr.datalen = 0;
03610 fr.samples = 0;
03611 fr.mallocd = 0;
03612 fr.offset = 0;
03613 fr.delivery = ast_tv(0,0);
03614
03615 if (ch->ast && MISDN_ASTERISK_PVT(ch->ast) && MISDN_ASTERISK_TECH_PVT(ch->ast)) {
03616 ast_queue_frame(ch->ast, &fr);
03617 }
03618 p++;
03619 }
03620 }
03621
03622
03623
03624 static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch) {
03625 if (!ast) {
03626 chan_misdn_log(1, 0, "send_cause2ast: No Ast\n");
03627 return;
03628 }
03629 if (!bc) {
03630 chan_misdn_log(1, 0, "send_cause2ast: No BC\n");
03631 return;
03632 }
03633 if (!ch) {
03634 chan_misdn_log(1, 0, "send_cause2ast: No Ch\n");
03635 return;
03636 }
03637
03638 ast->hangupcause = bc->cause;
03639
03640 switch (bc->cause) {
03641
03642 case AST_CAUSE_UNALLOCATED:
03643 case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
03644 case AST_CAUSE_NO_ROUTE_DESTINATION:
03645 case 4:
03646 case AST_CAUSE_NUMBER_CHANGED:
03647 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
03648
03649
03650
03651
03652
03653
03654
03655
03656
03657
03658 break;
03659
03660 case AST_CAUSE_CALL_REJECTED:
03661 case AST_CAUSE_USER_BUSY:
03662 ch->state = MISDN_BUSY;
03663
03664 if (!ch->need_busy) {
03665 chan_misdn_log(1, bc ? bc->port : 0, "Queued busy already\n");
03666 break;
03667 }
03668
03669 chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Busy pid:%d\n", bc ? bc->pid : -1);
03670
03671 ast_queue_control(ast, AST_CONTROL_BUSY);
03672
03673 ch->need_busy = 0;
03674
03675 break;
03676 }
03677 }
03678
03679
03680 void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
03681 {
03682 const char *tmp;
03683
03684 tmp = pbx_builtin_getvar_helper(chan, "MISDN_PID");
03685 if (tmp) {
03686 ch->other_pid = atoi(tmp);
03687 chan_misdn_log(3, bc->port, " --> IMPORT_PID: importing pid:%s\n", tmp);
03688 if (ch->other_pid > 0) {
03689 ch->other_ch = find_chan_by_pid(cl_te, ch->other_pid);
03690 if (ch->other_ch)
03691 ch->other_ch->other_ch = ch;
03692 }
03693 }
03694
03695 tmp = pbx_builtin_getvar_helper(chan, "MISDN_ADDRESS_COMPLETE");
03696 if (tmp && (atoi(tmp) == 1)) {
03697 bc->sending_complete = 1;
03698 }
03699
03700 tmp = pbx_builtin_getvar_helper(chan, "MISDN_USERUSER");
03701 if (tmp) {
03702 ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp);
03703 ast_copy_string(bc->uu, tmp, sizeof(bc->uu));
03704 bc->uulen = strlen(bc->uu);
03705 }
03706
03707 tmp = pbx_builtin_getvar_helper(chan, "MISDN_KEYPAD");
03708 if (tmp) {
03709 ast_copy_string(bc->keypad, tmp, sizeof(bc->keypad));
03710 }
03711 }
03712
03713 void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
03714 {
03715 char tmp[32];
03716 chan_misdn_log(3, bc->port, " --> EXPORT_PID: pid:%d\n", bc->pid);
03717 snprintf(tmp, sizeof(tmp), "%d", bc->pid);
03718 pbx_builtin_setvar_helper(chan, "_MISDN_PID", tmp);
03719
03720 if (bc->sending_complete) {
03721 snprintf(tmp, sizeof(tmp), "%d", bc->sending_complete);
03722 pbx_builtin_setvar_helper(chan, "MISDN_ADDRESS_COMPLETE", tmp);
03723 }
03724
03725 if (bc->urate) {
03726 snprintf(tmp, sizeof(tmp), "%d", bc->urate);
03727 pbx_builtin_setvar_helper(chan, "MISDN_URATE", tmp);
03728 }
03729
03730 if (bc->uulen && (bc->uulen < sizeof(bc->uu))) {
03731 bc->uu[bc->uulen] = 0;
03732 pbx_builtin_setvar_helper(chan, "MISDN_USERUSER", bc->uu);
03733 }
03734
03735 if (!ast_strlen_zero(bc->keypad))
03736 pbx_builtin_setvar_helper(chan, "MISDN_KEYPAD", bc->keypad);
03737 }
03738
03739 int add_in_calls(int port)
03740 {
03741 int max_in_calls;
03742
03743 misdn_cfg_get(port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls));
03744 misdn_in_calls[port]++;
03745
03746 if (max_in_calls >= 0 && max_in_calls < misdn_in_calls[port]) {
03747 ast_log(LOG_NOTICE, "Marking Incoming Call on port[%d]\n", port);
03748 return misdn_in_calls[port] - max_in_calls;
03749 }
03750
03751 return 0;
03752 }
03753
03754 int add_out_calls(int port)
03755 {
03756 int max_out_calls;
03757
03758 misdn_cfg_get(port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls));
03759
03760 if (max_out_calls >= 0 && max_out_calls <= misdn_out_calls[port]) {
03761 ast_log(LOG_NOTICE, "Rejecting Outgoing Call on port[%d]\n", port);
03762 return (misdn_out_calls[port] + 1) - max_out_calls;
03763 }
03764
03765 misdn_out_calls[port]++;
03766
03767 return 0;
03768 }
03769
03770 static void start_pbx(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan) {
03771 if (pbx_start_chan(ch) < 0) {
03772 hangup_chan(ch);
03773 chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
03774 if (bc->nt) {
03775 hanguptone_indicate(ch);
03776 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
03777 } else
03778 misdn_lib_send_event(bc, EVENT_RELEASE);
03779 }
03780 }
03781
03782 static void wait_for_digits(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan) {
03783 ch->state=MISDN_WAITING4DIGS;
03784 misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
03785 if (bc->nt && !bc->dad[0])
03786 dialtone_indicate(ch);
03787 }
03788
03789
03790
03791
03792
03793 static enum event_response_e
03794 cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
03795 {
03796 int msn_valid;
03797 struct chan_list *ch = find_chan_by_bc(cl_te, bc);
03798
03799 if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) {
03800 int debuglevel = 1;
03801 if ( event == EVENT_CLEANUP && !user_data)
03802 debuglevel = 5;
03803
03804 chan_misdn_log(debuglevel, bc->port, "I IND :%s oad:%s dad:%s pid:%d state:%s\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->pid, ch ? misdn_get_ch_state(ch) : "none");
03805 if (debuglevel == 1) {
03806 misdn_lib_log_ies(bc);
03807 chan_misdn_log(4, bc->port, " --> bc_state:%s\n", bc_state2str(bc->bc_state));
03808 }
03809 }
03810
03811 if (!ch) {
03812 switch(event) {
03813 case EVENT_SETUP:
03814 case EVENT_DISCONNECT:
03815 case EVENT_PORT_ALARM:
03816 case EVENT_RETRIEVE:
03817 case EVENT_NEW_BC:
03818 case EVENT_FACILITY:
03819 break;
03820 case EVENT_RELEASE_COMPLETE:
03821 chan_misdn_log(1, bc->port, " --> no Ch, so we've already released.\n");
03822 break;
03823 case EVENT_CLEANUP:
03824 case EVENT_TONE_GENERATE:
03825 case EVENT_BCHAN_DATA:
03826 return -1;
03827 default:
03828 chan_misdn_log(1, bc->port, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n", bc->l3_id, bc, manager_isdn_get_info(event), bc->port, bc->channel);
03829 return -1;
03830 }
03831 }
03832
03833 if (ch) {
03834 switch (event) {
03835 case EVENT_TONE_GENERATE:
03836 break;
03837 case EVENT_DISCONNECT:
03838 case EVENT_RELEASE:
03839 case EVENT_RELEASE_COMPLETE:
03840 case EVENT_CLEANUP:
03841 case EVENT_TIMEOUT:
03842 if (!ch->ast)
03843 chan_misdn_log(3, bc->port, "ast_hangup already called, so we have no ast ptr anymore in event(%s)\n", manager_isdn_get_info(event));
03844 break;
03845 default:
03846 if (!ch->ast || !MISDN_ASTERISK_PVT(ch->ast) || !MISDN_ASTERISK_TECH_PVT(ch->ast)) {
03847 if (event != EVENT_BCHAN_DATA)
03848 ast_log(LOG_NOTICE, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event));
03849 return -1;
03850 }
03851 }
03852 }
03853
03854
03855 switch (event) {
03856 case EVENT_PORT_ALARM:
03857 {
03858 int boa = 0;
03859 misdn_cfg_get(bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(int));
03860 if (boa) {
03861 cb_log(1, bc->port, " --> blocking\n");
03862 misdn_lib_port_block(bc->port);
03863 }
03864 }
03865 break;
03866 case EVENT_BCHAN_ACTIVATED:
03867 break;
03868
03869 case EVENT_NEW_CHANNEL:
03870 update_name(ch->ast,bc->port,bc->channel);
03871 break;
03872
03873 case EVENT_NEW_L3ID:
03874 ch->l3id=bc->l3_id;
03875 ch->addr=bc->addr;
03876 break;
03877
03878 case EVENT_NEW_BC:
03879 if (!ch) {
03880 ch = find_holded(cl_te,bc);
03881 }
03882
03883 if (!ch) {
03884 ast_log(LOG_WARNING, "NEW_BC without chan_list?\n");
03885 break;
03886 }
03887
03888 if (bc)
03889 ch->bc = (struct misdn_bchannel *)user_data;
03890 break;
03891
03892 case EVENT_DTMF_TONE:
03893 {
03894
03895 struct ast_frame fr;
03896
03897 memset(&fr, 0, sizeof(fr));
03898 fr.frametype = AST_FRAME_DTMF;
03899 fr.subclass = bc->dtmf ;
03900 fr.src = NULL;
03901 fr.data = NULL;
03902 fr.datalen = 0;
03903 fr.samples = 0;
03904 fr.mallocd = 0;
03905 fr.offset = 0;
03906 fr.delivery = ast_tv(0,0);
03907
03908 if (!ch->ignore_dtmf) {
03909 chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
03910 ast_queue_frame(ch->ast, &fr);
03911 } else {
03912 chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf);
03913 }
03914 }
03915 break;
03916 case EVENT_STATUS:
03917 break;
03918
03919 case EVENT_INFORMATION:
03920 {
03921 if ( ch->state != MISDN_CONNECTED )
03922 stop_indicate(ch);
03923
03924 if (!ch->ast)
03925 break;
03926
03927 if (ch->state == MISDN_WAITING4DIGS ) {
03928
03929 if (ast_strlen_zero(bc->info_dad) && ! ast_strlen_zero(bc->keypad)) {
03930 chan_misdn_log(1, bc->port, " --> using keypad as info\n");
03931 ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad));
03932 }
03933
03934 strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1);
03935 ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
03936
03937
03938 if (!strcmp(ch->ast->exten, ast_pickup_ext())) {
03939 if (ast_pickup_call(ch->ast)) {
03940 hangup_chan(ch);
03941 } else {
03942 struct ast_channel *chan = ch->ast;
03943 ch->state = MISDN_CALLING_ACKNOWLEDGE;
03944 ast_setstate(chan, AST_STATE_DOWN);
03945 hangup_chan(ch);
03946 ch->ast = NULL;
03947 break;
03948 }
03949 }
03950
03951 if (!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
03952 if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->oad)) {
03953 ast_log(LOG_WARNING, "Extension can never match, So jumping to 'i' extension. port(%d)\n", bc->port);
03954 strcpy(ch->ast->exten, "i");
03955
03956 ch->state = MISDN_DIALING;
03957 start_pbx(ch, bc, ch->ast);
03958 break;
03959 }
03960
03961 ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d).\n"
03962 "\tMaybe you want to add an 'i' extension to catch this case.\n",
03963 bc->port);
03964
03965 if (bc->nt)
03966 hanguptone_indicate(ch);
03967 ch->state = MISDN_EXTCANTMATCH;
03968 bc->out_cause = AST_CAUSE_UNALLOCATED;
03969
03970 misdn_lib_send_event(bc, EVENT_DISCONNECT);
03971 break;
03972 }
03973
03974 if (ch->overlap_dial) {
03975 ast_mutex_lock(&ch->overlap_tv_lock);
03976 ch->overlap_tv = ast_tvnow();
03977 ast_mutex_unlock(&ch->overlap_tv_lock);
03978 if (ch->overlap_dial_task == -1) {
03979 ch->overlap_dial_task =
03980 misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
03981 }
03982 break;
03983 }
03984
03985 if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
03986
03987 ch->state = MISDN_DIALING;
03988 start_pbx(ch, bc, ch->ast);
03989 }
03990 } else {
03991
03992 struct ast_frame fr;
03993 int digits;
03994
03995 memset(&fr, 0, sizeof(fr));
03996 fr.frametype = AST_FRAME_DTMF;
03997 fr.subclass = bc->info_dad[0] ;
03998 fr.src = NULL;
03999 fr.data = NULL;
04000 fr.datalen = 0;
04001 fr.samples = 0;
04002 fr.mallocd = 0;
04003 fr.offset = 0;
04004 fr.delivery = ast_tv(0,0);
04005
04006 misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(int));
04007 if (ch->state != MISDN_CONNECTED ) {
04008 if (digits) {
04009 strncat(bc->dad, bc->info_dad, sizeof(bc->dad) - strlen(bc->dad) - 1);
04010 ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
04011 ast_cdr_update(ch->ast);
04012 }
04013
04014 ast_queue_frame(ch->ast, &fr);
04015 }
04016 }
04017 }
04018 break;
04019 case EVENT_SETUP:
04020 {
04021 struct chan_list *ch = find_chan_by_bc(cl_te, bc);
04022 struct ast_channel *chan;
04023 int exceed;
04024 int pres, screen;
04025 int ai;
04026 int im;
04027
04028 if (ch) {
04029 switch (ch->state) {
04030 case MISDN_NOTHING:
04031 ch = NULL;
04032 break;
04033 default:
04034 chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n");
04035 return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE;
04036 }
04037 }
04038
04039 msn_valid = misdn_cfg_is_msn_valid(bc->port, bc->dad);
04040 if (!bc->nt && ! msn_valid) {
04041 chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
04042 return RESPONSE_IGNORE_SETUP;
04043 }
04044
04045 if (bc->cw) {
04046 int cause;
04047 chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
04048 misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
04049 bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
04050 return RESPONSE_RELEASE_SETUP;
04051 }
04052
04053 print_bearer(bc);
04054
04055 ch = init_chan_list(ORG_MISDN);
04056
04057 if (!ch) {
04058 chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n");
04059 return 0;
04060 }
04061
04062 ch->bc = bc;
04063 ch->l3id = bc->l3_id;
04064 ch->addr = bc->addr;
04065 ch->originator = ORG_MISDN;
04066
04067 chan = misdn_new(ch, AST_STATE_RESERVED, bc->dad, bc->oad, AST_FORMAT_ALAW, bc->port, bc->channel);
04068 if (!chan) {
04069 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
04070 ast_log(LOG_ERROR, "cb_events: misdn_new failed !\n");
04071 return 0;
04072 }
04073
04074 ch->ast = chan;
04075
04076 if ((exceed = add_in_calls(bc->port))) {
04077 char tmp[16];
04078 snprintf(tmp, sizeof(tmp), "%d", exceed);
04079 pbx_builtin_setvar_helper(chan, "MAX_OVERFLOW", tmp);
04080 }
04081
04082 read_config(ch, ORG_MISDN);
04083
04084 export_ch(chan, bc, ch);
04085
04086 ch->ast->rings = 1;
04087 ast_setstate(ch->ast, AST_STATE_RINGING);
04088
04089 switch (bc->pres) {
04090 case 1:
04091 pres = AST_PRES_RESTRICTED;
04092 chan_misdn_log(2, bc->port, " --> PRES: Restricted (1)\n");
04093 break;
04094 case 2:
04095 pres = AST_PRES_UNAVAILABLE;
04096 chan_misdn_log(2, bc->port, " --> PRES: Unavailable (2)\n");
04097 break;
04098 default:
04099 pres = AST_PRES_ALLOWED;
04100 chan_misdn_log(2, bc->port, " --> PRES: Allowed (%d)\n", bc->pres);
04101 break;
04102 }
04103
04104 switch (bc->screen) {
04105 default:
04106 case 0:
04107 screen = AST_PRES_USER_NUMBER_UNSCREENED;
04108 chan_misdn_log(2, bc->port, " --> SCREEN: Unscreened (%d)\n", bc->screen);
04109 break;
04110 case 1:
04111 screen = AST_PRES_USER_NUMBER_PASSED_SCREEN;
04112 chan_misdn_log(2, bc->port, " --> SCREEN: Passed screen (1)\n");
04113 break;
04114 case 2:
04115 screen = AST_PRES_USER_NUMBER_FAILED_SCREEN;
04116 chan_misdn_log(2, bc->port, " --> SCREEN: failed screen (2)\n");
04117 break;
04118 case 3:
04119 screen = AST_PRES_NETWORK_NUMBER;
04120 chan_misdn_log(2, bc->port, " --> SCREEN: Network Number (3)\n");
04121 break;
04122 }
04123
04124 chan->cid.cid_pres = pres | screen;
04125
04126 pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
04127 chan->transfercapability = bc->capability;
04128
04129 switch (bc->capability) {
04130 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
04131 pbx_builtin_setvar_helper(chan, "CALLTYPE", "DIGITAL");
04132 break;
04133 default:
04134 pbx_builtin_setvar_helper(chan, "CALLTYPE", "SPEECH");
04135 }
04136
04137
04138 cl_queue_chan(&cl_te, ch);
04139
04140 if (!strstr(ch->allowed_bearers, "all")) {
04141 int i;
04142
04143 for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) {
04144 if (allowed_bearers_array[i].cap == bc->capability) {
04145 if (strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
04146
04147 if (allowed_bearers_array[i].deprecated) {
04148 chan_misdn_log(0, bc->port, "%s in allowed_bearers list is deprecated\n",
04149 allowed_bearers_array[i].name);
04150 }
04151 break;
04152 }
04153 }
04154 }
04155 if (i == ARRAY_LEN(allowed_bearers_array)) {
04156
04157 chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n",
04158 bearer2str(bc->capability), bc->capability);
04159 bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
04160
04161 ch->state = MISDN_EXTCANTMATCH;
04162 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
04163 return RESPONSE_OK;
04164 }
04165 }
04166
04167
04168 if (!strcmp(chan->exten, ast_pickup_ext())) {
04169 if (!ch->noautorespond_on_setup) {
04170 int ret;
04171 ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
04172 } else {
04173 ch->state = MISDN_INCOMING_SETUP;
04174 }
04175 if (ast_pickup_call(chan)) {
04176 hangup_chan(ch);
04177 } else {
04178 ch->state = MISDN_CALLING_ACKNOWLEDGE;
04179 ast_setstate(chan, AST_STATE_DOWN);
04180 hangup_chan(ch);
04181 ch->ast = NULL;
04182 break;
04183 }
04184 }
04185
04186
04187
04188
04189
04190 misdn_cfg_get(bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
04191 if (ai) {
04192 do_immediate_setup(bc, ch, chan);
04193 break;
04194 }
04195
04196
04197 misdn_cfg_get(bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im));
04198 if (im && ast_strlen_zero(bc->dad)) {
04199 do_immediate_setup(bc, ch, chan);
04200 break;
04201 }
04202
04203 chan_misdn_log(5, bc->port, "CONTEXT:%s\n", ch->context);
04204 if(!ast_canmatch_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
04205 if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->oad)) {
04206 ast_log(LOG_WARNING, "Extension can never match, So jumping to 'i' extension. port(%d)\n", bc->port);
04207 strcpy(ch->ast->exten, "i");
04208 misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
04209 ch->state = MISDN_DIALING;
04210 start_pbx(ch, bc, chan);
04211 break;
04212 }
04213
04214 ast_log(LOG_WARNING, "Extension can never match, so disconnecting on port(%d).\n"
04215 "\tMaybe you want to add an 'i' extension to catch this case.\n",
04216 bc->port);
04217 if (bc->nt)
04218 hanguptone_indicate(ch);
04219
04220 ch->state = MISDN_EXTCANTMATCH;
04221 bc->out_cause = AST_CAUSE_UNALLOCATED;
04222
04223 if (bc->nt)
04224 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
04225 else
04226 misdn_lib_send_event(bc, EVENT_RELEASE );
04227
04228 break;
04229 }
04230
04231
04232
04233
04234 if (bc->sending_complete || (!bc->nt && !misdn_lib_is_ptp(bc->port))) {
04235 if (!ch->noautorespond_on_setup) {
04236 ch->state=MISDN_DIALING;
04237 misdn_lib_send_event(bc, EVENT_PROCEEDING );
04238 } else {
04239 ch->state = MISDN_INCOMING_SETUP;
04240 }
04241 start_pbx(ch, bc, chan);
04242 break;
04243 }
04244
04245
04246
04247
04248
04249
04250
04251 if (ch->overlap_dial && bc->nt && !bc->dad[0] ) {
04252 wait_for_digits(ch, bc, chan);
04253 break;
04254 }
04255
04256
04257
04258
04259
04260 if (ch->overlap_dial) {
04261 ast_mutex_lock(&ch->overlap_tv_lock);
04262 ch->overlap_tv = ast_tvnow();
04263 ast_mutex_unlock(&ch->overlap_tv_lock);
04264
04265 wait_for_digits(ch, bc, chan);
04266 if (ch->overlap_dial_task == -1)
04267 ch->overlap_dial_task =
04268 misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
04269
04270 break;
04271 }
04272
04273
04274
04275
04276 if (!ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
04277 wait_for_digits(ch, bc, chan);
04278 break;
04279 }
04280
04281
04282
04283
04284 if (ast_exists_extension(ch->ast, ch->context, bc->dad, 1, bc->oad)) {
04285 if (bc->need_more_infos)
04286 misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
04287 else
04288 misdn_lib_send_event(bc, EVENT_PROCEEDING);
04289
04290 ch->state = MISDN_DIALING;
04291 start_pbx(ch, bc, chan);
04292 break;
04293 }
04294 }
04295 break;
04296
04297 case EVENT_SETUP_ACKNOWLEDGE:
04298 {
04299 ch->state = MISDN_CALLING_ACKNOWLEDGE;
04300
04301 if (bc->channel)
04302 update_name(ch->ast,bc->port,bc->channel);
04303
04304 if (!ast_strlen_zero(bc->infos_pending)) {
04305
04306 strncat(bc->dad, bc->infos_pending, sizeof(bc->dad) - strlen(bc->dad) - 1);
04307
04308 if (!ch->ast)
04309 break;
04310 ast_copy_string(ch->ast->exten, bc->dad, sizeof(ch->ast->exten));
04311 ast_copy_string(bc->info_dad, bc->infos_pending, sizeof(bc->info_dad));
04312 ast_copy_string(bc->infos_pending, "", sizeof(bc->infos_pending));
04313
04314 misdn_lib_send_event(bc, EVENT_INFORMATION);
04315 }
04316 }
04317 break;
04318 case EVENT_PROCEEDING:
04319 {
04320 if (bc->channel)
04321 update_name(ch->ast, bc->port, bc->channel);
04322
04323 if (misdn_cap_is_speech(bc->capability) &&
04324 misdn_inband_avail(bc) ) {
04325 start_bc_tones(ch);
04326 }
04327
04328 ch->state = MISDN_PROCEEDING;
04329
04330 if (!ch->ast)
04331 break;
04332
04333 ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING);
04334 }
04335 break;
04336 case EVENT_PROGRESS:
04337 if (bc->channel)
04338 update_name(ch->ast, bc->port, bc->channel);
04339
04340 if (!bc->nt ) {
04341 if ( misdn_cap_is_speech(bc->capability) &&
04342 misdn_inband_avail(bc)
04343 ) {
04344 start_bc_tones(ch);
04345 }
04346
04347 ch->state = MISDN_PROGRESS;
04348
04349 if (!ch->ast)
04350 break;
04351 ast_queue_control(ch->ast, AST_CONTROL_PROGRESS);
04352 }
04353 break;
04354
04355
04356 case EVENT_ALERTING:
04357 {
04358 if (bc->channel)
04359 update_name(ch->ast, bc->port, bc->channel);
04360
04361 ch->state = MISDN_ALERTING;
04362
04363 if (!ch->ast)
04364 break;
04365
04366 ast_queue_control(ch->ast, AST_CONTROL_RINGING);
04367 ast_setstate(ch->ast, AST_STATE_RINGING);
04368
04369 cb_log(7, bc->port, " --> Set State Ringing\n");
04370
04371 if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) {
04372 cb_log(1, bc->port, "Starting Tones, we have inband Data\n");
04373 start_bc_tones(ch);
04374 } else {
04375 cb_log(3, bc->port, " --> We have no inband Data, the other end must create ringing\n");
04376 if (ch->far_alerting) {
04377 cb_log(1, bc->port, " --> The other end can not do ringing eh ?.. we must do all ourself..");
04378 start_bc_tones(ch);
04379
04380 }
04381 }
04382 }
04383 break;
04384 case EVENT_CONNECT:
04385 {
04386 struct ast_channel *bridged;
04387
04388
04389 misdn_lib_send_event(bc, EVENT_CONNECT_ACKNOWLEDGE);
04390
04391 if (!ch->ast)
04392 break;
04393
04394 bridged = ast_bridged_channel(ch->ast);
04395 stop_indicate(ch);
04396
04397 if (bridged && !strcasecmp(bridged->tech->type, "mISDN")) {
04398 struct chan_list *bridged_ch = MISDN_ASTERISK_TECH_PVT(bridged);
04399
04400 chan_misdn_log(1, bc->port, " --> copying cpndialplan:%d and cad:%s to the A-Channel\n", bc->cpnnumplan, bc->cad);
04401 if (bridged_ch) {
04402 bridged_ch->bc->cpnnumplan = bc->cpnnumplan;
04403 ast_copy_string(bridged_ch->bc->cad, bc->cad, sizeof(bridged_ch->bc->cad));
04404 }
04405 }
04406 }
04407 ch->l3id=bc->l3_id;
04408 ch->addr=bc->addr;
04409
04410 start_bc_tones(ch);
04411
04412 ch->state = MISDN_CONNECTED;
04413
04414 ast_queue_control(ch->ast, AST_CONTROL_ANSWER);
04415 break;
04416 case EVENT_CONNECT_ACKNOWLEDGE:
04417 {
04418 ch->l3id = bc->l3_id;
04419 ch->addr = bc->addr;
04420
04421 start_bc_tones(ch);
04422
04423 ch->state = MISDN_CONNECTED;
04424 }
04425 break;
04426 case EVENT_DISCONNECT:
04427
04428 if (ch) {
04429 struct chan_list *holded_ch = find_holded(cl_te, bc);
04430
04431 chan_misdn_log(3, bc->port, " --> org:%d nt:%d, inbandavail:%d state:%d\n", ch->originator, bc->nt, misdn_inband_avail(bc), ch->state);
04432 if (ch->originator == ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
04433
04434
04435
04436
04437
04438 chan_misdn_log(1, bc->port, " --> Inband Info Avail, not sending RELEASE\n");
04439
04440 ch->state = MISDN_DISCONNECTED;
04441 start_bc_tones(ch);
04442
04443 if (ch->ast) {
04444 ch->ast->hangupcause = bc->cause;
04445 if (bc->cause == AST_CAUSE_USER_BUSY)
04446 ast_queue_control(ch->ast, AST_CONTROL_BUSY);
04447 }
04448 ch->need_busy = 0;
04449 break;
04450 }
04451
04452
04453 if (holded_ch && holded_ch != ch && ch->ast && ch->state == MISDN_CONNECTED) {
04454 cb_log(1, bc->port, " --> found holded ch\n");
04455 misdn_transfer_bc(ch, holded_ch) ;
04456 }
04457
04458 bc->need_disconnect = 0;
04459
04460 stop_bc_tones(ch);
04461 hangup_chan(ch);
04462 #if 0
04463 } else {
04464 ch = find_holded_l3(cl_te, bc->l3_id,1);
04465 if (ch) {
04466 hangup_chan(ch);
04467 }
04468 #endif
04469 }
04470 bc->out_cause = -1;
04471 if (bc->need_release)
04472 misdn_lib_send_event(bc, EVENT_RELEASE);
04473 break;
04474
04475 case EVENT_RELEASE:
04476 {
04477 bc->need_disconnect = 0;
04478 bc->need_release = 0;
04479
04480 hangup_chan(ch);
04481 release_chan(bc);
04482 }
04483 break;
04484 case EVENT_RELEASE_COMPLETE:
04485 {
04486 bc->need_disconnect = 0;
04487 bc->need_release = 0;
04488 bc->need_release_complete = 0;
04489
04490 stop_bc_tones(ch);
04491 hangup_chan(ch);
04492
04493 if (ch)
04494 ch->state = MISDN_CLEANING;
04495
04496 release_chan(bc);
04497 }
04498 break;
04499 case EVENT_BCHAN_ERROR:
04500 case EVENT_CLEANUP:
04501 {
04502 stop_bc_tones(ch);
04503
04504 switch (ch->state) {
04505 case MISDN_CALLING:
04506 bc->cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
04507 break;
04508 default:
04509 break;
04510 }
04511
04512 hangup_chan(ch);
04513 release_chan(bc);
04514 }
04515 break;
04516
04517 case EVENT_TONE_GENERATE:
04518 {
04519 int tone_len = bc->tone_cnt;
04520 struct ast_channel *ast = ch->ast;
04521 void *tmp;
04522 int res;
04523 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
04524
04525 chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len);
04526
04527 if (!ast)
04528 break;
04529
04530 if (!ast->generator)
04531 break;
04532
04533 tmp = ast->generatordata;
04534 ast->generatordata = NULL;
04535 generate = ast->generator->generate;
04536
04537 if (tone_len < 0 || tone_len > 512 ) {
04538 ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n", tone_len);
04539 tone_len = 128;
04540 }
04541
04542 res = generate(ast, tmp, tone_len, tone_len);
04543 ast->generatordata = tmp;
04544
04545 if (res) {
04546 ast_log(LOG_WARNING, "Auto-deactivating generator\n");
04547 ast_deactivate_generator(ast);
04548 } else {
04549 bc->tone_cnt = 0;
04550 }
04551 }
04552 break;
04553
04554 case EVENT_BCHAN_DATA:
04555 {
04556 if (!misdn_cap_is_speech(ch->bc->capability)) {
04557 struct ast_frame frame;
04558
04559 frame.frametype = AST_FRAME_VOICE;
04560 frame.subclass = AST_FORMAT_ALAW;
04561 frame.datalen = bc->bframe_len;
04562 frame.samples = bc->bframe_len;
04563 frame.mallocd = 0;
04564 frame.offset = 0;
04565 frame.delivery = ast_tv(0,0);
04566 frame.src = NULL;
04567 frame.data = bc->bframe;
04568
04569 if (ch->ast)
04570 ast_queue_frame(ch->ast, &frame);
04571 } else {
04572 fd_set wrfs;
04573 struct timeval tv = { 0, 0 };
04574 int t;
04575
04576 FD_ZERO(&wrfs);
04577 FD_SET(ch->pipe[1], &wrfs);
04578
04579 t = select(FD_SETSIZE, NULL, &wrfs, NULL, &tv);
04580
04581 if (!t) {
04582 chan_misdn_log(9, bc->port, "Select Timed out\n");
04583 break;
04584 }
04585
04586 if (t < 0) {
04587 chan_misdn_log(-1, bc->port, "Select Error (err=%s)\n", strerror(errno));
04588 break;
04589 }
04590
04591 if (FD_ISSET(ch->pipe[1], &wrfs)) {
04592 chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len);
04593 if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) {
04594 chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno));
04595
04596 stop_bc_tones(ch);
04597 hangup_chan(ch);
04598 release_chan(bc);
04599 }
04600 } else {
04601 chan_misdn_log(1, bc->port, "Write Pipe full!\n");
04602 }
04603 }
04604 }
04605 break;
04606 case EVENT_TIMEOUT:
04607 {
04608 if (ch && bc)
04609 chan_misdn_log(1, bc->port, "--> state: %s\n", misdn_get_ch_state(ch));
04610
04611 switch (ch->state) {
04612 case MISDN_DIALING:
04613 case MISDN_PROGRESS:
04614 if (bc->nt && !ch->nttimeout)
04615 break;
04616
04617 case MISDN_CALLING:
04618 case MISDN_ALERTING:
04619 case MISDN_PROCEEDING:
04620 case MISDN_CALLING_ACKNOWLEDGE:
04621 if (bc->nt) {
04622 bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
04623 hanguptone_indicate(ch);
04624 }
04625
04626 bc->out_cause = AST_CAUSE_UNALLOCATED;
04627 misdn_lib_send_event(bc, EVENT_DISCONNECT);
04628 break;
04629
04630 case MISDN_WAITING4DIGS:
04631 if (bc->nt) {
04632 bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
04633 bc->out_cause = AST_CAUSE_UNALLOCATED;
04634 hanguptone_indicate(ch);
04635 misdn_lib_send_event(bc, EVENT_DISCONNECT);
04636 } else {
04637 bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
04638 misdn_lib_send_event(bc, EVENT_RELEASE);
04639 }
04640
04641 break;
04642
04643 case MISDN_CLEANING:
04644 chan_misdn_log(1,bc->port," --> in state cleaning .. so ignoring, the stack should clean it for us\n");
04645 break;
04646
04647 default:
04648 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
04649 }
04650 }
04651 break;
04652
04653
04654
04655
04656
04657 case EVENT_RETRIEVE:
04658 {
04659 struct ast_channel *hold_ast;
04660
04661 if (!ch) {
04662 chan_misdn_log(4, bc->port, " --> no CH, searching in holded\n");
04663 ch = find_holded_l3(cl_te, bc->l3_id, 1);
04664 }
04665
04666 if (!ch) {
04667 ast_log(LOG_WARNING, "Found no Holded channel, cannot Retrieve\n");
04668 misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
04669 break;
04670 }
04671
04672
04673 ch->bc = bc;
04674 ch->state = MISDN_CONNECTED;
04675
04676 ch->hold_info.port = 0;
04677 ch->hold_info.channel = 0;
04678
04679 hold_ast = ast_bridged_channel(ch->ast);
04680
04681 if (hold_ast) {
04682 ast_moh_stop(hold_ast);
04683 }
04684
04685 if (misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) {
04686 chan_misdn_log(4, bc->port, " --> RETRIEVE_ACK failed\n");
04687 misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
04688 }
04689 }
04690 break;
04691
04692 case EVENT_HOLD:
04693 {
04694 int hold_allowed;
04695 struct ast_channel *bridged;
04696
04697 misdn_cfg_get(bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int));
04698
04699 if (!hold_allowed) {
04700
04701 chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n");
04702 misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
04703 break;
04704 }
04705
04706 bridged = ast_bridged_channel(ch->ast);
04707 if (bridged) {
04708 chan_misdn_log(2, bc->port, "Bridge Partner is of type: %s\n", bridged->tech->type);
04709 ch->state = MISDN_HOLDED;
04710 ch->l3id = bc->l3_id;
04711
04712 misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE);
04713
04714
04715
04716 ast_moh_start(bridged, NULL, NULL);
04717
04718
04719 ch->bc = NULL;
04720 ch->hold_info.port = bc->port;
04721 ch->hold_info.channel = bc->channel;
04722
04723 } else {
04724 misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
04725 chan_misdn_log(0, bc->port, "We aren't bridged to anybody\n");
04726 }
04727 }
04728 break;
04729
04730 case EVENT_FACILITY:
04731 if (!ch) {
04732
04733 chan_misdn_log(-1, bc->port, "Got EVENT_FACILITY but we don't have a ch!\n");
04734 break;
04735 }
04736
04737 print_facility(&(bc->fac_in), bc);
04738
04739 switch (bc->fac_in.Function) {
04740 case Fac_CD:
04741 {
04742 struct ast_channel *bridged = ast_bridged_channel(ch->ast);
04743 struct chan_list *ch_br;
04744 if (bridged && MISDN_ASTERISK_TECH_PVT(bridged)) {
04745 ch_br = MISDN_ASTERISK_TECH_PVT(bridged);
04746
04747 if (ch_br->bc) {
04748 if (ast_exists_extension(bridged, ch->context, (char *)bc->fac_in.u.CDeflection.DeflectedToNumber, 1, bc->oad)) {
04749 ch_br->state = MISDN_DIALING;
04750 if (pbx_start_chan(ch_br) < 0) {
04751 chan_misdn_log(-1, ch_br->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
04752 }
04753 }
04754 }
04755 }
04756 misdn_lib_send_event(bc, EVENT_DISCONNECT);
04757 }
04758 break;
04759 case Fac_AOCDCurrency:
04760 {
04761 bc->AOCDtype = Fac_AOCDCurrency;
04762 memcpy(&(bc->AOCD.currency), &(bc->fac_in.u.AOCDcur), sizeof(struct FacAOCDCurrency));
04763 export_aoc_vars(ch->originator, ch->ast, bc);
04764 }
04765 break;
04766 case Fac_AOCDChargingUnit:
04767 {
04768 bc->AOCDtype = Fac_AOCDChargingUnit;
04769 memcpy(&(bc->AOCD.chargingUnit), &(bc->fac_in.u.AOCDchu), sizeof(struct FacAOCDChargingUnit));
04770 export_aoc_vars(ch->originator, ch->ast, bc);
04771 }
04772 break;
04773 default:
04774 chan_misdn_log(0, bc->port," --> not yet handled: facility type:%d\n", bc->fac_in.Function);
04775 }
04776
04777 break;
04778
04779 case EVENT_RESTART:
04780
04781 if (!bc->dummy) {
04782 stop_bc_tones(ch);
04783 release_chan(bc);
04784 }
04785 break;
04786
04787 default:
04788 chan_misdn_log(1, 0, "Got Unknown Event\n");
04789 break;
04790 }
04791
04792 return RESPONSE_OK;
04793 }
04794
04795
04796
04797
04798
04799
04800
04801
04802
04803
04804
04805
04806 static int unload_module(void)
04807 {
04808
04809 ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n");
04810
04811 misdn_tasks_destroy();
04812
04813 if (!g_config_initialized)
04814 return 0;
04815
04816 ast_cli_unregister_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
04817
04818
04819 ast_unregister_application("misdn_set_opt");
04820 ast_unregister_application("misdn_facility");
04821 ast_unregister_application("misdn_check_l2l1");
04822
04823 ast_channel_unregister(&misdn_tech);
04824
04825 free_robin_list();
04826 misdn_cfg_destroy();
04827 misdn_lib_destroy();
04828
04829 if (misdn_debug)
04830 free(misdn_debug);
04831 if (misdn_debug_only)
04832 free(misdn_debug_only);
04833 free(misdn_ports);
04834
04835 return 0;
04836 }
04837
04838 static int load_module(void)
04839 {
04840 int i, port;
04841 int ntflags = 0, ntkc = 0;
04842 char ports[256] = "";
04843 char tempbuf[BUFFERSIZE + 1];
04844 char ntfile[BUFFERSIZE + 1];
04845 struct misdn_lib_iface iface = {
04846 .cb_event = cb_events,
04847 .cb_log = chan_misdn_log,
04848 .cb_jb_empty = chan_misdn_jb_empty,
04849 };
04850
04851 max_ports = misdn_lib_maxports_get();
04852
04853 if (max_ports <= 0) {
04854 ast_log(LOG_ERROR, "Unable to initialize mISDN\n");
04855 return AST_MODULE_LOAD_DECLINE;
04856 }
04857
04858 if (misdn_cfg_init(max_ports)) {
04859 ast_log(LOG_ERROR, "Unable to initialize misdn_config.\n");
04860 return AST_MODULE_LOAD_DECLINE;
04861 }
04862 g_config_initialized = 1;
04863
04864 misdn_debug = (int *) malloc(sizeof(int) * (max_ports + 1));
04865 if (!misdn_debug) {
04866 ast_log(LOG_ERROR, "Out of memory for misdn_debug\n");
04867 return AST_MODULE_LOAD_DECLINE;
04868 }
04869 misdn_ports = (int *) malloc(sizeof(int) * (max_ports + 1));
04870 if (!misdn_ports) {
04871 ast_log(LOG_ERROR, "Out of memory for misdn_ports\n");
04872 return AST_MODULE_LOAD_DECLINE;
04873 }
04874 misdn_cfg_get(0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(int));
04875 for (i = 1; i <= max_ports; i++) {
04876 misdn_debug[i] = misdn_debug[0];
04877 misdn_ports[i] = i;
04878 }
04879 *misdn_ports = 0;
04880 misdn_debug_only = (int *) calloc(max_ports + 1, sizeof(int));
04881
04882 misdn_cfg_get(0, MISDN_GEN_TRACEFILE, tempbuf, BUFFERSIZE);
04883 if (!ast_strlen_zero(tempbuf))
04884 tracing = 1;
04885
04886 misdn_in_calls = (int *) malloc(sizeof(int) * (max_ports + 1));
04887 misdn_out_calls = (int *) malloc(sizeof(int) * (max_ports + 1));
04888
04889 for (i = 1; i <= max_ports; i++) {
04890 misdn_in_calls[i] = 0;
04891 misdn_out_calls[i] = 0;
04892 }
04893
04894 ast_mutex_init(&cl_te_lock);
04895 ast_mutex_init(&release_lock);
04896
04897 misdn_cfg_update_ptp();
04898 misdn_cfg_get_ports_string(ports);
04899
04900 if (!ast_strlen_zero(ports))
04901 chan_misdn_log(0, 0, "Got: %s from get_ports\n", ports);
04902 if (misdn_lib_init(ports, &iface, NULL))
04903 chan_misdn_log(0, 0, "No te ports initialized\n");
04904
04905 misdn_cfg_get(0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(int));
04906 misdn_cfg_get(0, MISDN_GEN_NTDEBUGFILE, &ntfile, BUFFERSIZE);
04907 misdn_lib_nt_debug_init(ntflags, ntfile);
04908
04909 misdn_cfg_get( 0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(int));
04910 misdn_lib_nt_keepcalls(ntkc);
04911
04912 if (ast_channel_register(&misdn_tech)) {
04913 ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type);
04914 unload_module();
04915 return -1;
04916 }
04917
04918 ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
04919
04920 ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
04921 "misdn_set_opt(:<opt><optarg>:<opt><optarg>...):\n"
04922 "Sets mISDN opts. and optargs\n"
04923 "\n"
04924 "The available options are:\n"
04925 " a - Have Asterisk detect DTMF tones on called channel\n"
04926 " c - Make crypted outgoing call, optarg is keyindex\n"
04927 " d - Send display text to called phone, text is the optarg\n"
04928 " e - Perform echo cancelation on this channel,\n"
04929 " takes taps as optarg (32,64,128,256)\n"
04930 " e! - Disable echo cancelation on this channel\n"
04931 " f - Enable fax detection\n"
04932 " h - Make digital outgoing call\n"
04933 " h1 - Make HDLC mode digital outgoing call\n"
04934 " i - Ignore detected DTMF tones, don't signal them to Asterisk,\n"
04935 " they will be transported inband.\n"
04936 " jb - Set jitter buffer length, optarg is length\n"
04937 " jt - Set jitter buffer upper threshold, optarg is threshold\n"
04938 " jn - Disable jitter buffer\n"
04939 " n - disable DSP on channel, disables: Echocancel, DTMF Detection and Volume Control.\n"
04940 " p - Caller ID presentation,\n"
04941 " optarg is either 'allowed' or 'restricted'\n"
04942 " s - Send Non-inband DTMF as inband\n"
04943 " vr - Rx gain control, optarg is gain\n"
04944 " vt - Tx gain control, optarg is gain\n"
04945 );
04946
04947
04948 ast_register_application("misdn_facility", misdn_facility_exec, "misdn_facility",
04949 "misdn_facility(<FACILITY_TYPE>|<ARG1>|..)\n"
04950 "Sends the Facility Message FACILITY_TYPE with \n"
04951 "the given Arguments to the current ISDN Channel\n"
04952 "Supported Facilities are:\n"
04953 "\n"
04954 "type=calldeflect args=Nr where to deflect\n"
04955 );
04956
04957
04958 ast_register_application("misdn_check_l2l1", misdn_check_l2l1, "misdn_check_l2l1",
04959 "misdn_check_l2l1(<port>||g:<groupname>,timeout)"
04960 "Checks if the L2 and L1 are up on either the given <port> or\n"
04961 "on the ports in the group with <groupname>\n"
04962 "If the L1/L2 are down, check_l2l1 gets up the L1/L2 and waits\n"
04963 "for <timeout> seconds that this happens. Otherwise, nothing happens\n"
04964 "\n"
04965 "This application, ensures the L1/L2 state of the Ports in a group\n"
04966 "it is intended to make the pmp_l1_check option redundant and to\n"
04967 "fix a buggy switch config from your provider\n"
04968 "\n"
04969 "a sample dialplan would look like:\n\n"
04970 "exten => _X.,1,misdn_check_l2l1(g:out|2)\n"
04971 "exten => _X.,n,dial(mISDN/g:out/${EXTEN})\n"
04972 "\n"
04973 );
04974
04975
04976 misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
04977
04978
04979
04980 for (port = misdn_cfg_get_next_port(0); port >= 0; port = misdn_cfg_get_next_port(port)) {
04981 int l1timeout;
04982 misdn_cfg_get(port, MISDN_CFG_L1_TIMEOUT, &l1timeout, sizeof(l1timeout));
04983 if (l1timeout) {
04984 chan_misdn_log(4, 0, "Adding L1watcher task: port:%d timeout:%ds\n", port, l1timeout);
04985 misdn_tasks_add(l1timeout * 1000, misdn_l1_task, &misdn_ports[port]);
04986 }
04987 }
04988
04989 chan_misdn_log(0, 0, "-- mISDN Channel Driver Registered --\n");
04990
04991 return 0;
04992 }
04993
04994
04995
04996 static int reload(void)
04997 {
04998 reload_config();
04999
05000 return 0;
05001 }
05002
05003
05004
05005 static int misdn_facility_exec(struct ast_channel *chan, void *data)
05006 {
05007 struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
05008 char *tok, *tokb;
05009
05010 chan_misdn_log(0, 0, "TYPE: %s\n", chan->tech->type);
05011
05012 if (strcasecmp(chan->tech->type, "mISDN")) {
05013 ast_log(LOG_WARNING, "misdn_facility makes only sense with chan_misdn channels!\n");
05014 return -1;
05015 }
05016
05017 if (ast_strlen_zero((char *)data)) {
05018 ast_log(LOG_WARNING, "misdn_facility Requires arguments\n");
05019 return -1;
05020 }
05021
05022 tok = strtok_r((char*) data, "|", &tokb) ;
05023
05024 if (!tok) {
05025 ast_log(LOG_WARNING, "misdn_facility Requires arguments\n");
05026 return -1;
05027 }
05028
05029 if (!strcasecmp(tok, "calldeflect")) {
05030 tok = strtok_r(NULL, "|", &tokb) ;
05031
05032 if (!tok) {
05033 ast_log(LOG_WARNING, "Facility: Call Defl Requires arguments\n");
05034 }
05035
05036 if (strlen(tok) >= sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber)) {
05037 ast_log(LOG_WARNING, "Facility: Number argument too long (up to 15 digits are allowed). Ignoring.\n");
05038 return 0;
05039 }
05040 ch->bc->fac_out.Function = Fac_CD;
05041 ast_copy_string((char *)ch->bc->fac_out.u.CDeflection.DeflectedToNumber, tok, sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber));
05042 misdn_lib_send_event(ch->bc, EVENT_FACILITY);
05043 } else {
05044 chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n", tok);
05045 }
05046
05047 return 0;
05048 }
05049
05050 static int misdn_check_l2l1(struct ast_channel *chan, void *data)
05051 {
05052 char group[BUFFERSIZE + 1];
05053 char *port_str;
05054 int port = 0;
05055 int timeout;
05056 int dowait = 0;
05057 int port_up;
05058
05059 AST_DECLARE_APP_ARGS(args,
05060 AST_APP_ARG(grouppar);
05061 AST_APP_ARG(timeout);
05062 );
05063
05064 if (ast_strlen_zero((char *)data)) {
05065 ast_log(LOG_WARNING, "misdn_check_l2l1 Requires arguments\n");
05066 return -1;
05067 }
05068
05069 AST_STANDARD_APP_ARGS(args, data);
05070
05071 if (args.argc != 2) {
05072 ast_log(LOG_WARNING, "Wrong argument count\n");
05073 return 0;
05074 }
05075
05076
05077 timeout = atoi(args.timeout);
05078 port_str = args.grouppar;
05079
05080 if (port_str[0] == 'g' && port_str[1] == ':' ) {
05081
05082 port_str += 2;
05083 ast_copy_string(group, port_str, sizeof(group));
05084 chan_misdn_log(2, 0, "Checking Ports in group: %s\n", group);
05085
05086 for ( port = misdn_cfg_get_next_port(port);
05087 port > 0;
05088 port = misdn_cfg_get_next_port(port)) {
05089 char cfg_group[BUFFERSIZE + 1];
05090
05091 chan_misdn_log(2, 0, "trying port %d\n", port);
05092
05093 misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
05094
05095 if (!strcasecmp(cfg_group, group)) {
05096 port_up = misdn_lib_port_up(port, 1);
05097
05098 if (!port_up) {
05099 chan_misdn_log(2, 0, " --> port '%d'\n", port);
05100 misdn_lib_get_port_up(port);
05101 dowait = 1;
05102 }
05103 }
05104 }
05105
05106 } else {
05107 port = atoi(port_str);
05108 chan_misdn_log(2, 0, "Checking Port: %d\n",port);
05109 port_up = misdn_lib_port_up(port, 1);
05110 if (!port_up) {
05111 misdn_lib_get_port_up(port);
05112 dowait = 1;
05113 }
05114 }
05115
05116 if (dowait) {
05117 chan_misdn_log(2, 0, "Waiting for '%d' seconds\n", timeout);
05118 sleep(timeout);
05119 }
05120
05121 return 0;
05122 }
05123
05124 static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
05125 {
05126 struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
05127 char *tok, *tokb;
05128 int keyidx = 0;
05129 int rxgain = 0;
05130 int txgain = 0;
05131 int change_jitter = 0;
05132
05133 if (strcasecmp(chan->tech->type, "mISDN")) {
05134 ast_log(LOG_WARNING, "misdn_set_opt makes only sense with chan_misdn channels!\n");
05135 return -1;
05136 }
05137
05138 if (ast_strlen_zero((char *)data)) {
05139 ast_log(LOG_WARNING, "misdn_set_opt Requires arguments\n");
05140 return -1;
05141 }
05142
05143 for (tok = strtok_r((char*) data, ":", &tokb);
05144 tok;
05145 tok = strtok_r(NULL, ":", &tokb) ) {
05146 int neglect = 0;
05147
05148 if (tok[0] == '!' ) {
05149 neglect = 1;
05150 tok++;
05151 }
05152
05153 switch(tok[0]) {
05154
05155 case 'd' :
05156 ast_copy_string(ch->bc->display, ++tok, sizeof(ch->bc->display));
05157 chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n", ch->bc->display);
05158 break;
05159
05160 case 'n':
05161 chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n");
05162 ch->bc->nodsp = 1;
05163 break;
05164
05165 case 'j':
05166 chan_misdn_log(1, ch->bc->port, "SETOPT: jitter\n");
05167 tok++;
05168 change_jitter = 1;
05169
05170 switch ( tok[0] ) {
05171 case 'b':
05172 ch->jb_len = atoi(++tok);
05173 chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n", ch->jb_len);
05174 break;
05175 case 't' :
05176 ch->jb_upper_threshold = atoi(++tok);
05177 chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n", ch->jb_upper_threshold);
05178 break;
05179 case 'n':
05180 ch->bc->nojitter = 1;
05181 chan_misdn_log(1, ch->bc->port, " --> nojitter\n");
05182 break;
05183 default:
05184 ch->jb_len = 4000;
05185 ch->jb_upper_threshold = 0;
05186 chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n", ch->jb_len);
05187 chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n", ch->jb_upper_threshold);
05188 }
05189 break;
05190 case 'v':
05191 tok++;
05192
05193 switch (tok[0]) {
05194 case 'r' :
05195 rxgain = atoi(++tok);
05196 if (rxgain < -8)
05197 rxgain = -8;
05198 if (rxgain > 8)
05199 rxgain = 8;
05200 ch->bc->rxgain = rxgain;
05201 chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", rxgain);
05202 break;
05203 case 't':
05204 txgain = atoi(++tok);
05205 if (txgain < -8)
05206 txgain = -8;
05207 if (txgain > 8)
05208 txgain = 8;
05209 ch->bc->txgain = txgain;
05210 chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", txgain);
05211 break;
05212 }
05213 break;
05214
05215 case 'c':
05216 keyidx = atoi(++tok);
05217 {
05218 char keys[4096];
05219 char *key = NULL, *tmp = keys;
05220 int i;
05221 misdn_cfg_get(0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys));
05222
05223 for (i = 0; i < keyidx; i++) {
05224 key = strsep(&tmp, ",");
05225 }
05226
05227 if (key) {
05228 ast_copy_string(ch->bc->crypt_key, key, sizeof(ch->bc->crypt_key));
05229 }
05230
05231 chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n", ch->bc->crypt_key);
05232 break;
05233 }
05234 case 'e':
05235 chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n");
05236
05237 if (neglect) {
05238 chan_misdn_log(1, ch->bc->port, " --> disabled\n");
05239 #ifdef MISDN_1_2
05240 *ch->bc->pipeline = 0;
05241 #else
05242 ch->bc->ec_enable = 0;
05243 #endif
05244 } else {
05245 #ifdef MISDN_1_2
05246 update_pipeline_config(ch->bc);
05247 #else
05248 ch->bc->ec_enable = 1;
05249 ch->bc->orig = ch->originator;
05250 tok++;
05251 if (*tok) {
05252 ch->bc->ec_deftaps = atoi(tok);
05253 }
05254 #endif
05255 }
05256
05257 break;
05258 case 'h':
05259 chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
05260
05261 if (strlen(tok) > 1 && tok[1] == '1') {
05262 chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n");
05263 if (!ch->bc->hdlc) {
05264 ch->bc->hdlc = 1;
05265 }
05266 }
05267 ch->bc->capability = INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
05268 break;
05269
05270 case 's':
05271 chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n");
05272 ch->bc->send_dtmf = 1;
05273 break;
05274
05275 case 'f':
05276 chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n");
05277 ch->faxdetect = 1;
05278 misdn_cfg_get(ch->bc->port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
05279 break;
05280
05281 case 'a':
05282 chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n");
05283 ch->ast_dsp = 1;
05284 break;
05285
05286 case 'p':
05287 chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n", &tok[1]);
05288
05289 if (strstr(tok,"allowed")) {
05290 ch->bc->pres = 0;
05291 } else if (strstr(tok, "restricted")) {
05292 ch->bc->pres = 1;
05293 } else if (strstr(tok, "not_screened")) {
05294 chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n");
05295 ch->bc->pres = 1;
05296 }
05297 break;
05298 case 'i' :
05299 chan_misdn_log(1, ch->bc->port, "Ignoring dtmf tones, just use them inband\n");
05300 ch->ignore_dtmf=1;
05301 break;
05302 default:
05303 break;
05304 }
05305 }
05306
05307 if (change_jitter)
05308 config_jitterbuffer(ch);
05309
05310 if (ch->faxdetect || ch->ast_dsp) {
05311 if (!ch->dsp)
05312 ch->dsp = ast_dsp_new();
05313 if (ch->dsp)
05314 ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
05315 if (!ch->trans)
05316 ch->trans = ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW);
05317 }
05318
05319 if (ch->ast_dsp) {
05320 chan_misdn_log(1, ch->bc->port, "SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
05321 ch->bc->nodsp = 1;
05322 ch->bc->nojitter = 1;
05323 }
05324
05325 return 0;
05326 }
05327
05328
05329 int chan_misdn_jb_empty ( struct misdn_bchannel *bc, char *buf, int len)
05330 {
05331 struct chan_list *ch = find_chan_by_bc(cl_te, bc);
05332
05333 if (ch && ch->jb) {
05334 return misdn_jb_empty(ch->jb, buf, len);
05335 }
05336
05337 return -1;
05338 }
05339
05340
05341
05342
05343
05344
05345
05346
05347
05348 struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
05349 {
05350 int i;
05351 struct misdn_jb *jb;
05352
05353 jb = malloc(sizeof(struct misdn_jb));
05354 if (!jb) {
05355 chan_misdn_log(-1, 0, "No free Mem for jb\n");
05356 return NULL;
05357 }
05358 jb->size = size;
05359 jb->upper_threshold = upper_threshold;
05360 jb->wp = 0;
05361 jb->rp = 0;
05362 jb->state_full = 0;
05363 jb->state_empty = 0;
05364 jb->bytes_wrote = 0;
05365 jb->samples = malloc(size * sizeof(char));
05366 if (!jb->samples) {
05367 free(jb);
05368 chan_misdn_log(-1, 0, "No free Mem for jb->samples\n");
05369 return NULL;
05370 }
05371
05372 jb->ok = malloc(size * sizeof(char));
05373 if (!jb->ok) {
05374 free(jb->samples);
05375 free(jb);
05376 chan_misdn_log(-1, 0, "No free Mem for jb->ok\n");
05377 return NULL;
05378 }
05379
05380 for (i = 0; i < size; i++)
05381 jb->ok[i] = 0;
05382
05383 ast_mutex_init(&jb->mutexjb);
05384
05385 return jb;
05386 }
05387
05388
05389 void misdn_jb_destroy(struct misdn_jb *jb)
05390 {
05391 ast_mutex_destroy(&jb->mutexjb);
05392
05393 free(jb->ok);
05394 free(jb->samples);
05395 free(jb);
05396 }
05397
05398
05399
05400 int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
05401 {
05402 int i, j, rp, wp;
05403
05404 if (!jb || ! data)
05405 return 0;
05406
05407 ast_mutex_lock(&jb->mutexjb);
05408
05409 wp = jb->wp;
05410 rp = jb->rp;
05411
05412 for (i = 0; i < len; i++) {
05413 jb->samples[wp] = data[i];
05414 jb->ok[wp] = 1;
05415 wp = (wp != jb->size - 1) ? wp + 1 : 0;
05416
05417 if (wp == jb->rp)
05418 jb->state_full = 1;
05419 }
05420
05421 if (wp >= rp)
05422 jb->state_buffer = wp - rp;
05423 else
05424 jb->state_buffer = jb->size - rp + wp;
05425 chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
05426
05427 if (jb->state_full) {
05428 jb->wp = wp;
05429
05430 rp = wp;
05431 for (j = 0; j < jb->upper_threshold; j++)
05432 rp = (rp != 0) ? rp - 1 : jb->size - 1;
05433 jb->rp = rp;
05434 jb->state_full = 0;
05435 jb->state_empty = 1;
05436
05437 ast_mutex_unlock(&jb->mutexjb);
05438
05439 return -1;
05440 }
05441
05442 if (!jb->state_empty) {
05443 jb->bytes_wrote += len;
05444 if (jb->bytes_wrote >= jb->upper_threshold) {
05445 jb->state_empty = 1;
05446 jb->bytes_wrote = 0;
05447 }
05448 }
05449 jb->wp = wp;
05450
05451 ast_mutex_unlock(&jb->mutexjb);
05452
05453 return 0;
05454 }
05455
05456
05457
05458
05459 int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
05460 {
05461 int i, wp, rp, read = 0;
05462
05463 ast_mutex_lock(&jb->mutexjb);
05464
05465 rp = jb->rp;
05466 wp = jb->wp;
05467
05468 if (jb->state_empty) {
05469 for (i = 0; i < len; i++) {
05470 if (wp == rp) {
05471 jb->rp = rp;
05472 jb->state_empty = 0;
05473
05474 ast_mutex_unlock(&jb->mutexjb);
05475
05476 return read;
05477 } else {
05478 if (jb->ok[rp] == 1) {
05479 data[i] = jb->samples[rp];
05480 jb->ok[rp] = 0;
05481 rp = (rp != jb->size - 1) ? rp + 1 : 0;
05482 read += 1;
05483 }
05484 }
05485 }
05486
05487 if (wp >= rp)
05488 jb->state_buffer = wp - rp;
05489 else
05490 jb->state_buffer = jb->size - rp + wp;
05491 chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
05492
05493 jb->rp = rp;
05494 } else
05495 chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb);
05496
05497 ast_mutex_unlock(&jb->mutexjb);
05498
05499 return read;
05500 }
05501
05502
05503
05504
05505
05506
05507
05508
05509
05510
05511
05512 static void chan_misdn_log(int level, int port, char *tmpl, ...)
05513 {
05514 va_list ap;
05515 char buf[1024];
05516 char port_buf[8];
05517
05518 if (! ((0 <= port) && (port <= max_ports))) {
05519 ast_log(LOG_WARNING, "cb_log called with out-of-range port number! (%d)\n", port);
05520 port = 0;
05521 level = -1;
05522 }
05523
05524 snprintf(port_buf, sizeof(port_buf), "P[%2d] ", port);
05525
05526 va_start(ap, tmpl);
05527 vsnprintf(buf, sizeof(buf), tmpl, ap);
05528 va_end(ap);
05529
05530 if (level == -1)
05531 ast_log(LOG_WARNING, "%s", buf);
05532
05533 else if (misdn_debug_only[port] ?
05534 (level == 1 && misdn_debug[port]) || (level == misdn_debug[port])
05535 : level <= misdn_debug[port]) {
05536
05537 ast_console_puts(port_buf);
05538 ast_console_puts(buf);
05539 }
05540
05541 if ((level <= misdn_debug[0]) && !ast_strlen_zero(global_tracefile) ) {
05542 time_t tm = time(NULL);
05543 char *tmp = ctime(&tm), *p;
05544
05545 FILE *fp = fopen(global_tracefile, "a+");
05546
05547 p = strchr(tmp, '\n');
05548 if (p)
05549 *p = ':';
05550
05551 if (!fp) {
05552 ast_console_puts("Error opening Tracefile: [ ");
05553 ast_console_puts(global_tracefile);
05554 ast_console_puts(" ] ");
05555
05556 ast_console_puts(strerror(errno));
05557 ast_console_puts("\n");
05558 return ;
05559 }
05560
05561 fputs(tmp, fp);
05562 fputs(" ", fp);
05563 fputs(port_buf, fp);
05564 fputs(" ", fp);
05565 fputs(buf, fp);
05566
05567 fclose(fp);
05568 }
05569 }
05570
05571 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for mISDN Support (BRI/PRI)",
05572 .load = load_module,
05573 .unload = unload_module,
05574 .reload = reload,
05575 );