isdn_lib.c

Go to the documentation of this file.
00001 /*
00002  * Chan_Misdn -- Channel Driver for Asterisk
00003  *
00004  * Interface to mISDN
00005  *
00006  * Copyright (C) 2004, Christian Richter
00007  *
00008  * Christian Richter <crich@beronet.com>
00009  *
00010  * This program is free software, distributed under the terms of
00011  * the GNU General Public License
00012  */
00013 
00014 /*! \file
00015  * \brief Interface to mISDN
00016  * \author Christian Richter <crich@beronet.com>
00017  */
00018 
00019 /*** MODULEINFO
00020    <support_level>extended</support_level>
00021  ***/
00022 
00023 #include <syslog.h>
00024 #include <sys/time.h>
00025 #include <mISDNuser/isdn_debug.h>
00026 
00027 #include "isdn_lib_intern.h"
00028 #include "isdn_lib.h"
00029 
00030 enum event_response_e (*cb_event)(enum event_e event, struct misdn_bchannel *bc, void *user_data);
00031 
00032 void (*cb_log)(int level, int port, char *tmpl, ...)
00033    __attribute__ ((format (printf, 3, 4)));
00034 
00035 int (*cb_jb_empty)(struct misdn_bchannel *bc, char *buffer, int len);
00036 
00037 
00038 /*
00039  * Define ARRAY_LEN() because I cannot
00040  * #include "asterisk/utils.h"
00041  */
00042 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
00043 
00044 #include "asterisk/causes.h"
00045 
00046 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
00047 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
00048 
00049 int misdn_lib_get_l2_up(struct misdn_stack *stack);
00050 
00051 struct misdn_stack *get_misdn_stack(void);
00052 
00053 int misdn_lib_port_is_pri(int port)
00054 {
00055    struct misdn_stack *stack=get_misdn_stack();
00056    for ( ; stack; stack=stack->next) {
00057       if (stack->port == port) {
00058          return stack->pri;
00059       }
00060    }
00061 
00062    return -1;
00063 }
00064 
00065 int misdn_lib_port_is_nt(int port)
00066 {
00067    struct misdn_stack *stack=get_misdn_stack();
00068    for ( ; stack; stack=stack->next) {
00069       if (stack->port == port) {
00070          return stack->nt;
00071       }
00072    }
00073 
00074    return -1;
00075 }
00076 
00077 void misdn_make_dummy(struct misdn_bchannel *dummybc, int port, int l3id, int nt, int channel)
00078 {
00079    memset (dummybc,0,sizeof(struct misdn_bchannel));
00080    dummybc->port=port;
00081    if (l3id==0)
00082       dummybc->l3_id = MISDN_ID_DUMMY;
00083    else
00084       dummybc->l3_id=l3id;
00085 
00086    dummybc->nt=nt;
00087    dummybc->dummy=1;
00088    dummybc->channel=channel;
00089 }
00090 
00091 int misdn_lib_port_block(int port)
00092 {
00093    struct misdn_stack *stack=get_misdn_stack();
00094    for ( ; stack; stack=stack->next) {
00095       if (stack->port == port) {
00096          stack->blocked=1;
00097          return 0;
00098       }
00099    }
00100    return -1;
00101 
00102 }
00103 
00104 int misdn_lib_port_unblock(int port)
00105 {
00106    struct misdn_stack *stack=get_misdn_stack();
00107    for ( ; stack; stack=stack->next) {
00108       if (stack->port == port) {
00109          stack->blocked=0;
00110          return 0;
00111       }
00112    }
00113    return -1;
00114 
00115 }
00116 
00117 int misdn_lib_is_port_blocked(int port)
00118 {
00119    struct misdn_stack *stack=get_misdn_stack();
00120    for ( ; stack; stack=stack->next) {
00121       if (stack->port == port) {
00122          return stack->blocked;
00123       }
00124    }
00125    return -1;
00126 }
00127 
00128 int misdn_lib_is_ptp(int port)
00129 {
00130    struct misdn_stack *stack=get_misdn_stack();
00131    for ( ; stack; stack=stack->next) {
00132       if (stack->port == port) return stack->ptp;
00133    }
00134    return -1;
00135 }
00136 
00137 int misdn_lib_get_maxchans(int port)
00138 {
00139    struct misdn_stack *stack=get_misdn_stack();
00140    for ( ; stack; stack=stack->next) {
00141       if (stack->port == port) {
00142          if (stack->pri)
00143             return 30;
00144          else
00145             return 2;
00146       }
00147    }
00148    return -1;
00149 }
00150 
00151 
00152 struct misdn_stack *get_stack_by_bc(struct misdn_bchannel *bc)
00153 {
00154    struct misdn_stack *stack = get_misdn_stack();
00155 
00156    if (!bc)
00157       return NULL;
00158 
00159    for ( ; stack; stack = stack->next) {
00160       if (bc->port == stack->port)
00161          return stack;
00162    }
00163 
00164    return NULL;
00165 }
00166 
00167 
00168 void get_show_stack_details(int port, char *buf)
00169 {
00170    struct misdn_stack *stack = get_misdn_stack();
00171 
00172    for (; stack; stack = stack->next) {
00173       if (stack->port == port) {
00174          break;
00175       }
00176    }
00177 
00178    if (stack) {
00179       sprintf(buf, "* Port %2d Type %s Prot. %s L2Link %s L1Link:%s Blocked:%d",
00180          stack->port,
00181          stack->nt ? "NT" : "TE",
00182          stack->ptp ? "PTP" : "PMP",
00183          (stack->nt && !stack->ptp) ? "UNKN" : stack->l2link ? "UP  " : "DOWN",
00184          stack->l1link ? "UP  " : "DOWN",
00185          stack->blocked);
00186    } else {
00187       buf[0] = 0;
00188    }
00189 }
00190 
00191 
00192 static int nt_err_cnt =0 ;
00193 
00194 enum global_states {
00195    MISDN_INITIALIZING,
00196    MISDN_INITIALIZED
00197 } ;
00198 
00199 static enum global_states  global_state=MISDN_INITIALIZING;
00200 
00201 
00202 #include <mISDNuser/net_l2.h>
00203 #include <mISDNuser/tone.h>
00204 #include <unistd.h>
00205 #include <semaphore.h>
00206 #include <pthread.h>
00207 #include <signal.h>
00208 
00209 #include "isdn_lib.h"
00210 
00211 
00212 struct misdn_lib {
00213    /*! \brief mISDN device handle returned by mISDN_open() */
00214    int midev;
00215 
00216    pthread_t event_thread;
00217    pthread_t event_handler_thread;
00218 
00219    void *user_data;
00220 
00221    msg_queue_t activatequeue;
00222 
00223    sem_t new_msg;
00224 
00225    struct misdn_stack *stack_list;
00226 } ;
00227 
00228 #ifndef ECHOCAN_ON
00229 #define ECHOCAN_ON 123
00230 #define ECHOCAN_OFF 124
00231 #endif
00232 
00233 #define MISDN_DEBUG 0
00234 
00235 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
00236 
00237 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
00238 
00239 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
00240 
00241 int misdn_lib_port_restart(int port);
00242 int misdn_lib_pid_restart(int pid);
00243 
00244 extern struct isdn_msg msgs_g[];
00245 
00246 #define ISDN_PID_L3_B_USER 0x430000ff
00247 #define ISDN_PID_L4_B_USER 0x440000ff
00248 
00249 /* #define MISDN_IBUF_SIZE 1024 */
00250 #define MISDN_IBUF_SIZE 512
00251 
00252 /*  Fine Tuning of Inband  Signalling time */
00253 #define TONE_ALERT_CNT 41 /*  1 Sec  */
00254 #define TONE_ALERT_SILENCE_CNT 200 /*  4 Sec */
00255 
00256 #define TONE_BUSY_CNT 20 /*  ? */
00257 #define TONE_BUSY_SILENCE_CNT 48 /*  ? */
00258 
00259 static int entity;
00260 
00261 static struct misdn_lib *glob_mgr;
00262 
00263 static char tone_425_flip[TONE_425_SIZE];
00264 static char tone_silence_flip[TONE_SILENCE_SIZE];
00265 
00266 static void misdn_lib_isdn_event_catcher(void *arg);
00267 static int handle_event_nt(void *dat, void *arg);
00268 
00269 
00270 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
00271 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
00272 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
00273 
00274 /* from isdn_lib.h */
00275    /* user iface */
00276 void te_lib_destroy(int midev) ;
00277 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
00278 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
00279 void manager_clean_bc(struct misdn_bchannel *bc );
00280 void manager_bchannel_setup (struct misdn_bchannel *bc);
00281 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
00282 
00283 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
00284    /* end */
00285 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
00286 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
00287 /* end */
00288 
00289 
00290 static char *bearer2str(int cap) {
00291    static char *bearers[]={
00292       "Speech",
00293       "Audio 3.1k",
00294       "Unres Digital",
00295       "Res Digital",
00296       "Unknown Bearer"
00297    };
00298 
00299    switch (cap) {
00300    case INFO_CAPABILITY_SPEECH:
00301       return bearers[0];
00302       break;
00303    case INFO_CAPABILITY_AUDIO_3_1K:
00304       return bearers[1];
00305       break;
00306    case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
00307       return bearers[2];
00308       break;
00309    case INFO_CAPABILITY_DIGITAL_RESTRICTED:
00310       return bearers[3];
00311       break;
00312    default:
00313       return bearers[4];
00314       break;
00315    }
00316 }
00317 
00318 
00319 static char flip_table[256];
00320 
00321 static void init_flip_bits(void)
00322 {
00323    int i,k;
00324 
00325    for (i = 0 ; i < 256 ; i++) {
00326       unsigned char sample = 0 ;
00327       for (k = 0; k<8; k++) {
00328          if ( i & 1 << k ) sample |= 0x80 >>  k;
00329       }
00330       flip_table[i] = sample;
00331    }
00332 }
00333 
00334 static char * flip_buf_bits ( char * buf , int len)
00335 {
00336    int i;
00337    char * start = buf;
00338 
00339    for (i = 0 ; i < len; i++) {
00340       buf[i] = flip_table[(unsigned char)buf[i]];
00341    }
00342 
00343    return start;
00344 }
00345 
00346 
00347 
00348 
00349 static msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
00350 {
00351    int i = 0;
00352    msg_t *dmsg;
00353 
00354    while(i < 10)
00355    {
00356       dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
00357       if (dmsg)
00358          return(dmsg);
00359 
00360       if (!i)
00361          printf("cannot allocate memory, trying again...\n");
00362       i++;
00363       usleep(300000);
00364    }
00365    printf("cannot allocate memory, system overloaded.\n");
00366    exit(-1);
00367 }
00368 
00369 
00370 
00371 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
00372 {
00373    int i = 0;
00374    msg_t *dmsg;
00375    Q931_info_t *qi;
00376    iframe_t *frm;
00377 
00378    if (!ntmode)
00379       size = sizeof(Q931_info_t)+2;
00380 
00381    while(i < 10) {
00382       if (ntmode) {
00383          dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
00384          if (dmsg) {
00385             return(dmsg);
00386          }
00387       } else {
00388          dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
00389          if (dmsg)
00390          {
00391             memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
00392             frm = (iframe_t *)dmsg->data;
00393             frm->prim = prim;
00394             frm->dinfo = dinfo;
00395             qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
00396             qi->type = mt;
00397             return(dmsg);
00398          }
00399       }
00400 
00401       if (!i) printf("cannot allocate memory, trying again...\n");
00402       i++;
00403       usleep(300000);
00404    }
00405    printf("cannot allocate memory, system overloaded.\n");
00406    exit(-1);
00407 }
00408 
00409 
00410 static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
00411 {
00412    iframe_t *frm = (iframe_t *)dmsg->data;
00413    struct misdn_stack *stack=get_stack_by_bc(bc);
00414 
00415    if (!stack) {
00416       cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
00417       return -1;
00418    }
00419 
00420    frm->addr = (stack->upper_id | FLG_MSG_DOWN);
00421    frm->dinfo = bc->l3_id;
00422    frm->len = (dmsg->len) - mISDN_HEADER_LEN;
00423 
00424    cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
00425 
00426    mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
00427    free_msg(dmsg);
00428 
00429    return 0;
00430 }
00431 
00432 
00433 static int mypid=1;
00434 
00435 
00436 int misdn_cap_is_speech(int cap)
00437 /** Poor mans version **/
00438 {
00439    if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
00440         (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
00441    return 0;
00442 }
00443 
00444 int misdn_inband_avail(struct misdn_bchannel *bc)
00445 {
00446 
00447    if (!bc->early_bconnect) {
00448       /* We have opted to never receive any available inband recorded messages */
00449       return 0;
00450    }
00451 
00452    switch (bc->progress_indicator) {
00453    case INFO_PI_INBAND_AVAILABLE:
00454    case INFO_PI_CALL_NOT_E2E_ISDN:
00455    case INFO_PI_CALLED_NOT_ISDN:
00456       return 1;
00457    default:
00458       return 0;
00459    }
00460    return 0;
00461 }
00462 
00463 
00464 static void dump_chan_list(struct misdn_stack *stack)
00465 {
00466    int i;
00467 
00468    for (i = 0; i <= stack->b_num; ++i) {
00469       cb_log(6, stack->port, "Idx:%d stack->cchan:%d in_use:%d Chan:%d\n",
00470          i, stack->channels[i], stack->bc[i].in_use, i + 1);
00471    }
00472 #if defined(AST_MISDN_ENHANCEMENTS)
00473    for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
00474       if (stack->bc[i].in_use) {
00475          cb_log(6, stack->port, "Idx:%d stack->cchan:%d REGISTER Chan:%d in_use\n",
00476             i, stack->channels[i], i + 1);
00477       }
00478    }
00479 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
00480 }
00481 
00482 
00483 void misdn_dump_chanlist(void)
00484 {
00485    struct misdn_stack *stack=get_misdn_stack();
00486    for ( ; stack; stack=stack->next) {
00487       dump_chan_list(stack);
00488    }
00489 
00490 }
00491 
00492 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
00493 {
00494    cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
00495    dump_chan_list(stack);
00496    if (1 <= channel && channel <= ARRAY_LEN(stack->channels)) {
00497       if (!stack->channels[channel-1])
00498          stack->channels[channel-1] = 1;
00499       else {
00500          cb_log(4,stack->port,"channel already in use:%d\n", channel );
00501          return -1;
00502       }
00503    } else {
00504       cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
00505       return -1;
00506    }
00507 
00508    return 0;
00509 }
00510 
00511 
00512 
00513 static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel, int dec)
00514 {
00515    int i;
00516    int chan = 0;
00517    int bnums;
00518 
00519    if (bc->channel_found) {
00520       return 0;
00521    }
00522 
00523    bc->channel_found = 1;
00524 
00525 #if defined(AST_MISDN_ENHANCEMENTS)
00526    if (bc->is_register_pool) {
00527       pthread_mutex_lock(&stack->st_lock);
00528       for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->channels); ++i) {
00529          if (!stack->channels[i]) {
00530             chan = i + 1;
00531             cb_log(3, stack->port, " --> found REGISTER chan: %d\n", chan);
00532             break;
00533          }
00534       }
00535    } else
00536 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
00537    {
00538       cb_log(5, stack->port, "find_free_chan: req_chan:%d\n", channel);
00539 
00540       if (channel < 0 || channel > MAX_BCHANS) {
00541          cb_log(0, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
00542          return 0;
00543       }
00544 
00545       --channel;
00546 
00547       pthread_mutex_lock(&stack->st_lock);
00548       bnums = stack->pri ? stack->b_num : stack->b_num - 1;
00549       if (dec) {
00550          for (i = bnums; i >= 0; --i) {
00551             if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */
00552                if (!stack->channels[i]) {
00553                   chan = i + 1;
00554                   cb_log(3, stack->port, " --> found chan%s: %d\n", channel >= 0 ? " (preselected)" : "", chan);
00555                   break;
00556                }
00557             }
00558          }
00559       } else {
00560          for (i = 0; i <= bnums; ++i) {
00561             if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */
00562                if (!stack->channels[i]) {
00563                   chan = i + 1;
00564                   cb_log(3, stack->port, " --> found chan%s: %d\n", channel >= 0 ? " (preselected)" : "", chan);
00565                   break;
00566                }
00567             }
00568          }
00569       }
00570    }
00571 
00572    if (!chan) {
00573       pthread_mutex_unlock(&stack->st_lock);
00574       cb_log(1, stack->port, " !! NO FREE CHAN IN STACK\n");
00575       dump_chan_list(stack);
00576       bc->out_cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
00577       return -1;
00578    }
00579 
00580    if (set_chan_in_stack(stack, chan) < 0) {
00581       pthread_mutex_unlock(&stack->st_lock);
00582       cb_log(0, stack->port, "Channel Already in use:%d\n", chan);
00583       bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
00584       return -1;
00585    }
00586    pthread_mutex_unlock(&stack->st_lock);
00587 
00588    bc->channel = chan;
00589    return 0;
00590 }
00591 
00592 /*!
00593  * \internal
00594  * \brief Release a B channel to the allocation pool.
00595  *
00596  * \param stack Which port stack B channel belongs.
00597  * \param channel B channel to release. (Range 1-MAX_BCHANS representing B1-Bn)
00598  *
00599  * \return Nothing
00600  *
00601  * \note
00602  * Must be called after clean_up_bc() to make sure that the media stream is
00603  * no longer connected.
00604  */
00605 static void empty_chan_in_stack(struct misdn_stack *stack, int channel)
00606 {
00607    if (channel < 1 || ARRAY_LEN(stack->channels) < channel) {
00608       cb_log(0, stack->port, "empty_chan_in_stack: cannot empty channel %d\n", channel);
00609       return;
00610    }
00611 
00612    cb_log(4, stack->port, "empty_chan_in_stack: %d\n", channel);
00613    stack->channels[channel - 1] = 0;
00614    dump_chan_list(stack);
00615 }
00616 
00617 char *bc_state2str(enum bchannel_state state) {
00618    int i;
00619 
00620    struct bchan_state_s {
00621       char *n;
00622       enum bchannel_state s;
00623    } states[] = {
00624       {"BCHAN_CLEANED", BCHAN_CLEANED },
00625       {"BCHAN_EMPTY", BCHAN_EMPTY},
00626       {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
00627       {"BCHAN_BRIDGED", BCHAN_BRIDGED},
00628       {"BCHAN_RELEASE", BCHAN_RELEASE},
00629       {"BCHAN_ERROR", BCHAN_ERROR}
00630    };
00631 
00632    for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
00633       if ( states[i].s == state)
00634          return states[i].n;
00635 
00636    return "UNKNOWN";
00637 }
00638 
00639 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
00640 {
00641    cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n",
00642       bc->l3_id,
00643           bc_state2str(bc->bc_state),
00644           bc_state2str(state) );
00645 
00646    switch (state) {
00647       case BCHAN_ACTIVATED:
00648          if (bc->next_bc_state ==  BCHAN_BRIDGED) {
00649             misdn_join_conf(bc, bc->conf_id);
00650             bc->next_bc_state = BCHAN_EMPTY;
00651             return;
00652          }
00653       default:
00654          bc->bc_state=state;
00655          break;
00656    }
00657 }
00658 
00659 static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
00660 {
00661    cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
00662           bc_state2str(bc->next_bc_state),
00663           bc_state2str(state) );
00664 
00665    bc->next_bc_state=state;
00666 }
00667 
00668 /*!
00669  * \internal
00670  * \brief Empty the B channel record of most call data.
00671  *
00672  * \param bc B channel record to empty of most call data.
00673  *
00674  * \return Nothing
00675  *
00676  * \note
00677  * Sets the last_used time and must be called before clearing bc->in_use.
00678  */
00679 static void empty_bc(struct misdn_bchannel *bc)
00680 {
00681    bc->caller.presentation = 0;  /* allowed */
00682    bc->caller.number_plan = NUMPLAN_ISDN;
00683    bc->caller.number_type = NUMTYPE_UNKNOWN;
00684    bc->caller.name[0] = 0;
00685    bc->caller.number[0] = 0;
00686    bc->caller.subaddress[0] = 0;
00687 
00688    bc->connected.presentation = 0;  /* allowed */
00689    bc->connected.number_plan = NUMPLAN_ISDN;
00690    bc->connected.number_type = NUMTYPE_UNKNOWN;
00691    bc->connected.name[0] = 0;
00692    bc->connected.number[0] = 0;
00693    bc->connected.subaddress[0] = 0;
00694 
00695    bc->redirecting.from.presentation = 0; /* allowed */
00696    bc->redirecting.from.number_plan = NUMPLAN_ISDN;
00697    bc->redirecting.from.number_type = NUMTYPE_UNKNOWN;
00698    bc->redirecting.from.name[0] = 0;
00699    bc->redirecting.from.number[0] = 0;
00700    bc->redirecting.from.subaddress[0] = 0;
00701 
00702    bc->redirecting.to.presentation = 0;   /* allowed */
00703    bc->redirecting.to.number_plan = NUMPLAN_ISDN;
00704    bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
00705    bc->redirecting.to.name[0] = 0;
00706    bc->redirecting.to.number[0] = 0;
00707    bc->redirecting.to.subaddress[0] = 0;
00708 
00709    bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
00710    bc->redirecting.count = 0;
00711    bc->redirecting.to_changed = 0;
00712 
00713    bc->dummy=0;
00714 
00715    bc->bframe_len=0;
00716 
00717    bc->cw= 0;
00718 
00719    bc->dec=0;
00720    bc->channel = 0;
00721 
00722    bc->sending_complete = 0;
00723 
00724    bc->restart_channel=0;
00725 
00726    bc->conf_id = 0;
00727 
00728    bc->need_more_infos = 0;
00729 
00730    bc->send_dtmf=0;
00731    bc->nodsp=0;
00732    bc->nojitter=0;
00733 
00734    bc->time_usec=0;
00735 
00736    bc->rxgain=0;
00737    bc->txgain=0;
00738 
00739    bc->crypt=0;
00740    bc->curptx=0; bc->curprx=0;
00741 
00742    bc->crypt_key[0] = 0;
00743 
00744    bc->generate_tone=0;
00745    bc->tone_cnt=0;
00746 
00747    bc->active = 0;
00748 
00749    bc->early_bconnect = 1;
00750 
00751 #ifdef MISDN_1_2
00752    *bc->pipeline = 0;
00753 #else
00754    bc->ec_enable = 0;
00755    bc->ec_deftaps = 128;
00756 #endif
00757 
00758    bc->AOCD_need_export = 0;
00759 
00760    bc->orig=0;
00761 
00762    bc->cause = AST_CAUSE_NORMAL_CLEARING;
00763    bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
00764 
00765    bc->display_connected = 0; /* none */
00766    bc->display_setup = 0;  /* none */
00767 
00768    bc->outgoing_colp = 0;/* pass */
00769 
00770    bc->presentation = 0;   /* allowed */
00771    bc->set_presentation = 0;
00772 
00773    bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
00774 
00775    bc->progress_coding=0;
00776    bc->progress_location=0;
00777    bc->progress_indicator=0;
00778 
00779 #if defined(AST_MISDN_ENHANCEMENTS)
00780    bc->div_leg_3_rx_wanted = 0;
00781    bc->div_leg_3_tx_pending = 0;
00782 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
00783 
00784 /** Set Default Bearer Caps **/
00785    bc->capability=INFO_CAPABILITY_SPEECH;
00786    bc->law=INFO_CODEC_ALAW;
00787    bc->mode=0;
00788    bc->rate=0x10;
00789    bc->user1=0;
00790    bc->urate=0;
00791 
00792    bc->hdlc=0;
00793 
00794    bc->dialed.number_plan = NUMPLAN_ISDN;
00795    bc->dialed.number_type = NUMTYPE_UNKNOWN;
00796    bc->dialed.number[0] = 0;
00797    bc->dialed.subaddress[0] = 0;
00798 
00799    bc->info_dad[0] = 0;
00800    bc->display[0] = 0;
00801    bc->infos_pending[0] = 0;
00802    bc->uu[0]=0;
00803    bc->uulen=0;
00804 
00805    bc->fac_in.Function = Fac_None;
00806    bc->fac_out.Function = Fac_None;
00807 
00808    bc->te_choose_channel = 0;
00809    bc->channel_found= 0;
00810 
00811    gettimeofday(&bc->last_used, NULL);
00812 }
00813 
00814 
00815 static int clean_up_bc(struct misdn_bchannel *bc)
00816 {
00817    int ret=0;
00818    unsigned char buff[32];
00819    struct misdn_stack * stack;
00820 
00821    cb_log(3, bc->port, "$$$ CLEANUP CALLED pid:%d\n", bc->pid);
00822 
00823    stack=get_stack_by_bc(bc);
00824 
00825    if (!stack) return -1;
00826 
00827    switch (bc->bc_state ) {
00828    case BCHAN_CLEANED:
00829       cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
00830       return -1;
00831 
00832    default:
00833       break;
00834    }
00835 
00836    cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
00837 
00838    manager_ec_disable(bc);
00839 
00840    manager_bchannel_deactivate(bc);
00841 
00842    mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
00843 
00844    bc->b_stid = 0;
00845    bc_state_change(bc, BCHAN_CLEANED);
00846 
00847    return ret;
00848 }
00849 
00850 
00851 
00852 static void clear_l3(struct misdn_stack *stack)
00853 {
00854    int i;
00855 
00856    if (global_state == MISDN_INITIALIZED) {
00857       for (i = 0; i <= stack->b_num; ++i) {
00858          cb_event(EVENT_CLEANUP, &stack->bc[i], NULL);
00859          empty_bc(&stack->bc[i]);
00860          clean_up_bc(&stack->bc[i]);
00861          empty_chan_in_stack(stack, i + 1);
00862          stack->bc[i].in_use = 0;
00863       }
00864 #if defined(AST_MISDN_ENHANCEMENTS)
00865       for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
00866          empty_bc(&stack->bc[i]);
00867          empty_chan_in_stack(stack, i + 1);
00868          stack->bc[i].in_use = 0;
00869       }
00870 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
00871    }
00872 }
00873 
00874 static int new_te_id = 0;
00875 
00876 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
00877 {
00878    /* Pull Up L1 */
00879    iframe_t act;
00880    act.prim = PH_DEACTIVATE | REQUEST;
00881    act.addr = stack->upper_id | FLG_MSG_DOWN;
00882    act.dinfo = 0;
00883    act.len = 0;
00884 
00885    cb_log(1, stack->port, "SENDING PH_DEACTIVATE | REQ\n");
00886    return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00887 }
00888 
00889 
00890 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
00891 {
00892 
00893    if (stack->ptp && stack->nt) {
00894       msg_t *dmsg;
00895       /* L2 */
00896       dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
00897 
00898       pthread_mutex_lock(&stack->nstlock);
00899       if (stack->nst.manager_l3(&stack->nst, dmsg))
00900          free_msg(dmsg);
00901       pthread_mutex_unlock(&stack->nstlock);
00902    } else if (!stack->nt) {
00903       iframe_t act;
00904 
00905       act.prim = DL_RELEASE| REQUEST;
00906       act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
00907 
00908       act.dinfo = 0;
00909       act.len = 0;
00910       return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00911    }
00912    /* cannot deestablish L2 for NT PTMP to unknown TE TEIs */
00913 
00914    return 0;
00915 }
00916 
00917 
00918 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
00919 {
00920    /* Pull Up L1 */
00921    iframe_t act;
00922    act.prim = PH_ACTIVATE | REQUEST;
00923    act.addr = stack->upper_id | FLG_MSG_DOWN;
00924 
00925    act.dinfo = 0;
00926    act.len = 0;
00927 
00928    cb_log(1, stack->port, "SENDING PH_ACTIVATE | REQ\n");
00929    return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00930 }
00931 
00932 int misdn_lib_get_l2_up(struct misdn_stack *stack)
00933 {
00934 
00935    if (stack->ptp && stack->nt) {
00936       msg_t *dmsg;
00937       /* L2 */
00938       dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
00939 
00940       pthread_mutex_lock(&stack->nstlock);
00941       if (stack->nst.manager_l3(&stack->nst, dmsg))
00942          free_msg(dmsg);
00943       pthread_mutex_unlock(&stack->nstlock);
00944    } else if (!stack->nt) {
00945       iframe_t act;
00946 
00947       act.prim = DL_ESTABLISH | REQUEST;
00948       act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
00949 
00950       act.dinfo = 0;
00951       act.len = 0;
00952       return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00953    }
00954    /* cannot establish L2 for NT PTMP to unknown TE TEIs */
00955 
00956    return 0;
00957 }
00958 
00959 static int misdn_lib_get_short_status(struct misdn_stack *stack)
00960 {
00961    iframe_t act;
00962 
00963 
00964    act.prim = MGR_SHORTSTATUS | REQUEST;
00965 
00966    act.addr = (stack->upper_id | MSG_BROADCAST)  ;
00967 
00968    act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
00969 
00970    act.len = 0;
00971    return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
00972 }
00973 
00974 
00975 
00976 static int create_process(int midev, struct misdn_bchannel *bc)
00977 {
00978    iframe_t ncr;
00979    int l3_id;
00980    int proc_id;
00981    struct misdn_stack *stack;
00982 
00983    stack = get_stack_by_bc(bc);
00984    if (stack->nt) {
00985       if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, 0) < 0) {
00986          return -1;
00987       }
00988       cb_log(4, stack->port, " -->  found channel: %d\n", bc->channel);
00989 
00990       for (proc_id = 0; proc_id < MAXPROCS; ++proc_id) {
00991          if (stack->procids[proc_id] == 0) {
00992             break;
00993          }
00994       }
00995       if (proc_id == MAXPROCS) {
00996          cb_log(0, stack->port, "Couldn't Create New ProcId.\n");
00997          return -1;
00998       }
00999 
01000       stack->procids[proc_id] = 1;
01001 
01002       l3_id = 0xff00 | proc_id;
01003       bc->l3_id = l3_id;
01004       cb_log(3, stack->port, " --> new_l3id %x\n", l3_id);
01005    } else {
01006       if ((stack->pri && stack->ptp) || bc->te_choose_channel) {
01007          /* we know exactly which channels are in use */
01008          if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, bc->dec) < 0) {
01009             return -1;
01010          }
01011          cb_log(2, stack->port, " -->  found channel: %d\n", bc->channel);
01012       } else {
01013          /* other phones could have made a call also on this port (ptmp) */
01014          bc->channel = 0xff;
01015       }
01016 
01017       /* if we are in te-mode, we need to create a process first */
01018       if (++new_te_id > 0xffff) {
01019          new_te_id = 0x0001;
01020       }
01021 
01022       l3_id = (entity << 16) | new_te_id;
01023       bc->l3_id = l3_id;
01024       cb_log(3, stack->port, "--> new_l3id %x\n", l3_id);
01025 
01026       /* send message */
01027       ncr.prim = CC_NEW_CR | REQUEST;
01028       ncr.addr = (stack->upper_id | FLG_MSG_DOWN);
01029       ncr.dinfo = l3_id;
01030       ncr.len = 0;
01031       mISDN_write(midev, &ncr, mISDN_HEADER_LEN + ncr.len, TIMEOUT_1SEC);
01032    }
01033 
01034    return l3_id;
01035 }
01036 
01037 
01038 static int setup_bc(struct misdn_bchannel *bc)
01039 {
01040    unsigned char buff[1025];
01041    int midev;
01042    int channel;
01043    int b_stid;
01044    int i;
01045    mISDN_pid_t pid;
01046    int ret;
01047 
01048    struct misdn_stack *stack=get_stack_by_bc(bc);
01049 
01050    if (!stack) {
01051       cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
01052       return -1;
01053    }
01054 
01055    midev = stack->midev;
01056    channel = bc->channel - 1 - (bc->channel > 16);
01057    b_stid = stack->b_stids[channel >= 0 ? channel : 0];
01058 
01059    switch (bc->bc_state) {
01060       case BCHAN_CLEANED:
01061          break;
01062       default:
01063          cb_log(4, stack->port, "$$$ bc already setup stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
01064          return -1;
01065    }
01066 
01067    cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
01068 
01069    /*check if the b_stid is already initialized*/
01070    for (i=0; i <= stack->b_num; i++) {
01071       if (stack->bc[i].b_stid == b_stid) {
01072          cb_log(0, bc->port, "setup_bc: b_stid:%x already in use !!!\n", b_stid);
01073          return -1;
01074       }
01075    }
01076 
01077    if (b_stid <= 0) {
01078       cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
01079 
01080       bc_state_change(bc,BCHAN_ERROR);
01081       return 1;
01082    }
01083 
01084    bc->b_stid = b_stid;
01085 
01086    {
01087       layer_info_t li;
01088       memset(&li, 0, sizeof(li));
01089 
01090       li.object_id = -1;
01091       li.extentions = 0;
01092 
01093       li.st = bc->b_stid; /*  given idx */
01094 
01095 
01096 #define MISDN_DSP
01097 #ifndef MISDN_DSP
01098       bc->nodsp=1;
01099 #endif
01100       if ( bc->hdlc || bc->nodsp) {
01101          cb_log(4, stack->port,"setup_bc: without dsp\n");
01102          {
01103             int l = sizeof(li.name);
01104             strncpy(li.name, "B L3", l);
01105             li.name[l-1] = 0;
01106          }
01107          li.pid.layermask = ISDN_LAYER((3));
01108          li.pid.protocol[3] = ISDN_PID_L3_B_USER;
01109 
01110          bc->layer=3;
01111       } else {
01112          cb_log(4, stack->port,"setup_bc: with dsp\n");
01113          {
01114             int l = sizeof(li.name);
01115             strncpy(li.name, "B L4", l);
01116             li.name[l-1] = 0;
01117          }
01118          li.pid.layermask = ISDN_LAYER((4));
01119          li.pid.protocol[4] = ISDN_PID_L4_B_USER;
01120 
01121          bc->layer=4;
01122       }
01123 
01124       ret = mISDN_new_layer(midev, &li);
01125       if (ret ) {
01126          cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
01127 
01128          bc_state_change(bc,BCHAN_ERROR);
01129          return(-EINVAL);
01130       }
01131 
01132       bc->layer_id = li.id;
01133    }
01134 
01135    memset(&pid, 0, sizeof(pid));
01136 
01137 
01138 
01139    cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
01140 
01141    if (bc->nodsp && !bc->hdlc) {
01142       cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
01143       pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
01144       pid.protocol[2] = ISDN_PID_L2_B_TRANS;
01145       pid.protocol[3] = ISDN_PID_L3_B_USER;
01146       pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
01147 
01148    } else if ( bc->hdlc ) {
01149       cb_log(2, stack->port," --> HDLC Mode\n");
01150       pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
01151       pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
01152       pid.protocol[3] = ISDN_PID_L3_B_USER;
01153       pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
01154    } else {
01155       cb_log(2, stack->port," --> TRANSPARENT Mode\n");
01156       pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
01157       pid.protocol[2] = ISDN_PID_L2_B_TRANS;
01158       pid.protocol[3] = ISDN_PID_L3_B_DSP;
01159       pid.protocol[4] = ISDN_PID_L4_B_USER;
01160       pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
01161 
01162    }
01163 
01164    ret = mISDN_set_stack(midev, bc->b_stid, &pid);
01165 
01166    if (ret){
01167       cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
01168 
01169       mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01170 
01171       bc_state_change(bc,BCHAN_ERROR);
01172       cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
01173       return(-EINVAL);
01174    }
01175 
01176    ret = mISDN_get_setstack_ind(midev, bc->layer_id);
01177 
01178    if (ret) {
01179       cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
01180       mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01181 
01182       bc_state_change(bc,BCHAN_ERROR);
01183       cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
01184       return(-EINVAL);
01185    }
01186 
01187    ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
01188 
01189    bc->addr = ret>0? ret : 0;
01190 
01191    if (!bc->addr) {
01192       cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
01193       mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01194 
01195       bc_state_change(bc,BCHAN_ERROR);
01196       cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
01197       return (-EINVAL);
01198    }
01199 
01200    manager_bchannel_activate(bc);
01201 
01202    bc_state_change(bc,BCHAN_ACTIVATED);
01203 
01204    return 0;
01205 }
01206 
01207 
01208 
01209 /** IFACE **/
01210 static int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx)
01211 {
01212    if (!bc) {
01213       return -1;
01214    }
01215 
01216    cb_log(8, port, "Init.BC %d.\n",bidx);
01217 
01218    bc->send_lock = malloc(sizeof(struct send_lock)); /* XXX BUG! memory leak never freed */
01219    if (!bc->send_lock) {
01220       return -1;
01221    }
01222    pthread_mutex_init(&bc->send_lock->lock, NULL);
01223 
01224    empty_bc(bc);
01225 
01226    bc->port=stack->port;
01227    bc_state_change(bc, BCHAN_CLEANED);
01228    bc->nt=stack->nt?1:0;
01229    bc->pri=stack->pri;
01230 
01231    {
01232       ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
01233 
01234       if (!ibuf) return -1;
01235 
01236       clear_ibuffer( ibuf);
01237 
01238       ibuf->rsem=malloc(sizeof(sem_t));
01239       if (!ibuf->rsem) {
01240          return -1;
01241       }
01242 
01243       bc->astbuf=ibuf;
01244 
01245       if (sem_init(ibuf->rsem,1,0)<0)
01246          sem_init(ibuf->rsem,0,0);
01247 
01248    }
01249 
01250 #if 0 /* This code does not seem to do anything useful */
01251    if (bidx <= stack->b_num) {
01252       unsigned char buff[1025];
01253       iframe_t *frm = (iframe_t *) buff;
01254       stack_info_t *stinf;
01255       int ret;
01256 
01257       ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
01258       if (ret < 0) {
01259          cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
01260          return -1;
01261       }
01262 
01263       stinf = (stack_info_t *)&frm->data.p;
01264 
01265       cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
01266    }
01267 #endif
01268 
01269    return 0;
01270 }
01271 
01272 
01273 
01274 static struct misdn_stack *stack_init(int midev, int port, int ptp)
01275 {
01276    int ret;
01277    unsigned char buff[1025];
01278    iframe_t *frm = (iframe_t *)buff;
01279    stack_info_t *stinf;
01280    struct misdn_stack *stack;
01281    int i;
01282    layer_info_t li;
01283 
01284    stack = calloc(1, sizeof(struct misdn_stack));
01285    if (!stack) {
01286       return NULL;
01287    }
01288 
01289    cb_log(8, port, "Init. Stack.\n");
01290 
01291    stack->port=port;
01292    stack->midev=midev;
01293    stack->ptp=ptp;
01294 
01295    stack->holding=NULL;
01296    stack->pri=0;
01297 
01298    msg_queue_init(&stack->downqueue);
01299 
01300    pthread_mutex_init(&stack->st_lock, NULL);
01301 
01302    /* query port's requirements */
01303    ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
01304    if (ret < 0) {
01305       cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
01306       free(stack);
01307       return(NULL);
01308    }
01309 
01310    stinf = (stack_info_t *)&frm->data.p;
01311 
01312    stack->d_stid = stinf->id;
01313    stack->b_num = stinf->childcnt;
01314 
01315    for (i=0; i<=stinf->childcnt; i++)
01316       stack->b_stids[i] = stinf->child[i];
01317 
01318    switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
01319    case ISDN_PID_L0_TE_S0:
01320       cb_log(8, port, "TE Stack\n");
01321       stack->nt=0;
01322       break;
01323    case ISDN_PID_L0_NT_S0:
01324       cb_log(8, port, "NT Stack\n");
01325       stack->nt=1;
01326       break;
01327    case ISDN_PID_L0_TE_E1:
01328       cb_log(8, port, "TE S2M Stack\n");
01329       stack->nt=0;
01330       stack->pri=1;
01331       break;
01332    case ISDN_PID_L0_NT_E1:
01333       cb_log(8, port, "NT S2M Stack\n");
01334       stack->nt=1;
01335       stack->pri=1;
01336       break;
01337    default:
01338       cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
01339 
01340    }
01341 
01342    if (!stack->nt) {
01343       if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
01344          stack->ptp = 1;
01345       } else {
01346          stack->ptp = 0;
01347       }
01348    }
01349 
01350    {
01351       int ret;
01352       int nt=stack->nt;
01353 
01354       memset(&li, 0, sizeof(li));
01355       {
01356          int l = sizeof(li.name);
01357          strncpy(li.name,nt?"net l2":"user l4", l);
01358          li.name[l-1] = 0;
01359       }
01360       li.object_id = -1;
01361       li.extentions = 0;
01362       li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
01363       li.pid.layermask = ISDN_LAYER((nt?2:4));
01364       li.st = stack->d_stid;
01365 
01366 
01367       ret = mISDN_new_layer(midev, &li);
01368       if (ret) {
01369          cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
01370          free(stack);
01371          return(NULL);
01372       }
01373 
01374 
01375       ret = mISDN_register_layer(midev, stack->d_stid, li.id);
01376       if (ret)
01377       {
01378          cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
01379          free(stack);
01380          return(NULL);
01381       }
01382 
01383       stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
01384       if (stack->lower_id < 0) {
01385          cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
01386          free(stack);
01387          return(NULL);
01388       }
01389 
01390       stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
01391       if (stack->upper_id < 0) {
01392          cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?2:4);
01393          free(stack);
01394          return(NULL);
01395       }
01396 
01397       /* create nst (nt-mode only) */
01398       if (nt) {
01399 
01400          memset(&stack->nst, 0, sizeof(net_stack_t));
01401          memset(&stack->mgr, 0, sizeof(manager_t));
01402 
01403          stack->mgr.nst = &stack->nst;
01404          stack->nst.manager = &stack->mgr;
01405 
01406          stack->nst.l3_manager = handle_event_nt;
01407          stack->nst.device = midev;
01408          stack->nst.cardnr = port;
01409          stack->nst.d_stid = stack->d_stid;
01410 
01411          stack->nst.feature = FEATURE_NET_HOLD;
01412          if (stack->ptp)
01413             stack->nst.feature |= FEATURE_NET_PTP;
01414          if (stack->pri)
01415             stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
01416 
01417          stack->nst.l1_id = stack->lower_id; /* never used */
01418          stack->nst.l2_id = stack->upper_id;
01419 
01420          msg_queue_init(&stack->nst.down_queue);
01421          pthread_mutex_init(&stack->nstlock, NULL);
01422 
01423          Isdnl2Init(&stack->nst);
01424          Isdnl3Init(&stack->nst);
01425 
01426       }
01427 
01428       stack->l1link=0;
01429       stack->l2link=0;
01430 #if 0
01431       if (!stack->nt) {
01432          misdn_lib_get_short_status(stack);
01433       } else {
01434          misdn_lib_get_l1_up(stack);
01435          if (!stack->ptp) misdn_lib_get_l1_up(stack);
01436          misdn_lib_get_l2_up(stack);
01437       }
01438 #endif
01439 
01440       misdn_lib_get_short_status(stack);
01441       misdn_lib_get_l1_up(stack);
01442       /* handle_l1 will start L2 for NT. */
01443       if (!stack->nt) {
01444          misdn_lib_get_l2_up(stack);
01445       }
01446    }
01447 
01448    cb_log(8, port, "stack_init: lowerId:%x upperId:%x\n", stack->lower_id, stack->upper_id);
01449 
01450    return stack;
01451 }
01452 
01453 
01454 static void stack_destroy(struct misdn_stack *stack)
01455 {
01456    char buf[1024];
01457    if (!stack) return;
01458 
01459    if (stack->nt) {
01460       pthread_mutex_destroy(&stack->nstlock);
01461       cleanup_Isdnl2(&stack->nst);
01462       cleanup_Isdnl3(&stack->nst);
01463    }
01464 
01465    if (stack->upper_id)
01466       mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
01467 
01468    pthread_mutex_destroy(&stack->st_lock);
01469 }
01470 
01471 
01472 static struct misdn_stack * find_stack_by_addr(int  addr)
01473 {
01474    struct misdn_stack *stack;
01475 
01476    for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
01477       if ((stack->upper_id & STACK_ID_MASK) == (addr & STACK_ID_MASK)) {
01478          /* Found the stack */
01479          break;
01480       }
01481    }
01482 
01483    return stack;
01484 }
01485 
01486 
01487 static struct misdn_stack *find_stack_by_port(int port)
01488 {
01489    struct misdn_stack *stack;
01490 
01491    for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
01492       if (stack->port == port) {
01493          /* Found the stack */
01494          break;
01495       }
01496    }
01497 
01498    return stack;
01499 }
01500 
01501 static struct misdn_stack *find_stack_by_mgr(manager_t *mgr_nt)
01502 {
01503    struct misdn_stack *stack;
01504 
01505    for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
01506       if (&stack->mgr == mgr_nt) {
01507          /* Found the stack */
01508          break;
01509       }
01510    }
01511 
01512    return stack;
01513 }
01514 
01515 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
01516 {
01517    int i;
01518 
01519    for (i = 0; i <= stack->b_num; ++i) {
01520       if (stack->bc[i].in_use && (stack->bc[i].l3_id & mask) == (l3id & mask)) {
01521          return &stack->bc[i];
01522       }
01523    }
01524 #if defined(AST_MISDN_ENHANCEMENTS)
01525    /* Search the B channel records for a REGISTER signaling link. */
01526    for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
01527       if (stack->bc[i].in_use && (stack->bc[i].l3_id & mask) == (l3id & mask)) {
01528          return &stack->bc[i];
01529       }
01530    }
01531 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
01532    return stack_holder_find(stack, l3id);
01533 }
01534 
01535 
01536 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
01537 {
01538    int i;
01539 
01540    for (i = 0; i <= stack->b_num; ++i) {
01541       if (stack->bc[i].in_use && stack->bc[i].l3_id == l3id) {
01542          return &stack->bc[i];
01543       }
01544    }
01545 #if defined(AST_MISDN_ENHANCEMENTS)
01546    /* Search the B channel records for a REGISTER signaling link. */
01547    for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
01548       if (stack->bc[i].in_use && stack->bc[i].l3_id == l3id) {
01549          return &stack->bc[i];
01550       }
01551    }
01552 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
01553    return stack_holder_find(stack, l3id);
01554 }
01555 
01556 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
01557 {
01558    struct misdn_stack *stack;
01559    int i;
01560 
01561    for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
01562       for (i = 0; i <= stack->b_num; i++) {
01563          if (stack->bc[i].in_use
01564             && ((stack->bc[i].addr & STACK_ID_MASK) == (addr & STACK_ID_MASK)
01565                || stack->bc[i].layer_id == addr)) {
01566             return &stack->bc[i];
01567          }
01568       }
01569    }
01570 
01571    return NULL;
01572 }
01573 
01574 static struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
01575 {
01576    struct misdn_stack *stack;
01577    int i;
01578 
01579    for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
01580       for (i = 0; i <= stack->b_num; i++) {
01581          if (stack->bc[i].in_use && stack->bc[i].conf_id == confid) {
01582             return &stack->bc[i];
01583          }
01584       }
01585    }
01586    return NULL;
01587 }
01588 
01589 
01590 static int handle_event_te(struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
01591 {
01592    struct misdn_stack *stack=get_stack_by_bc(bc);
01593 
01594    switch (event) {
01595       case EVENT_CONNECT_ACKNOWLEDGE:
01596          setup_bc(bc);
01597 
01598          if ( *bc->crypt_key ) {
01599             cb_log(4, stack->port,
01600                "ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
01601                bc->channel,
01602                bc->caller.number_type,
01603                bc->caller.name,
01604                bc->caller.number,
01605                bc->dialed.number_type,
01606                bc->dialed.number);
01607             manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
01608          }
01609 
01610          if (misdn_cap_is_speech(bc->capability)) {
01611             if (  !bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
01612             manager_ec_enable(bc);
01613 
01614             if ( bc->txgain != 0 ) {
01615                cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
01616                manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
01617             }
01618             if ( bc->rxgain != 0 ) {
01619                cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
01620                manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
01621             }
01622          }
01623 
01624          break;
01625       case EVENT_CONNECT:
01626 
01627          if ( *bc->crypt_key ) {
01628             cb_log(4, stack->port,
01629                "ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
01630                bc->channel,
01631                bc->caller.number_type,
01632                bc->caller.name,
01633                bc->caller.number,
01634                bc->dialed.number_type,
01635                bc->dialed.number);
01636             manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
01637          }
01638       case EVENT_ALERTING:
01639       case EVENT_PROGRESS:
01640       case EVENT_PROCEEDING:
01641       case EVENT_SETUP_ACKNOWLEDGE:
01642       case EVENT_SETUP:
01643       {
01644          if (bc->channel == 0xff || bc->channel<=0)
01645             bc->channel=0;
01646 
01647          if (find_free_chan_in_stack(stack, bc, bc->channel, 0)<0){
01648             if (!stack->pri && !stack->ptp)  {
01649                bc->cw=1;
01650                break;
01651             }
01652 
01653             if (!bc->channel) {
01654                cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
01655             } else {
01656                cb_log(0, stack->port,
01657                   "Requested Channel Already in Use releasing this call with cause %d!!!!\n",
01658                   bc->out_cause);
01659             }
01660 
01661             /* when the channel is already in use, we can't
01662              * simply clear it, we need to make sure that
01663              * it will still be marked as in_use in the
01664              * available channels list.*/
01665             bc->channel=0;
01666 
01667             misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
01668             return -1;
01669          }
01670 
01671          if (event != EVENT_SETUP) {
01672             setup_bc(bc);
01673          }
01674          break;
01675       }
01676 
01677       case EVENT_RELEASE_COMPLETE:
01678       case EVENT_RELEASE:
01679          break;
01680       default:
01681          break;
01682    }
01683    return 0;
01684 }
01685 
01686 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
01687 {
01688    struct misdn_bchannel dummybc;
01689    struct misdn_bchannel *bc;
01690    int channel;
01691 
01692    if (!stack) return -1;
01693 
01694    switch (frm->prim) {
01695    case CC_NEW_CR|INDICATION:
01696       cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
01697 
01698       bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0);
01699       if (!bc) {
01700          cb_log(0, stack->port, " --> !! lib: No free channel!\n");
01701          return -1;
01702       }
01703 
01704       cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
01705       bc->l3_id=frm->dinfo;
01706       return 1;
01707    case CC_NEW_CR|CONFIRM:
01708       return 1;
01709    case CC_NEW_CR|REQUEST:
01710       return 1;
01711    case CC_RELEASE_CR|REQUEST:
01712       return 1;
01713    case CC_RELEASE_CR|CONFIRM:
01714       break;
01715    case CC_RELEASE_CR|INDICATION:
01716       cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n", frm->dinfo);
01717       bc = find_bc_by_l3id(stack, frm->dinfo);
01718       if (!bc) {
01719          cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
01720          misdn_make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0);
01721          bc = &dummybc;
01722       }
01723 
01724       channel = bc->channel;
01725       cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n", frm->dinfo);
01726 
01727       /* bc->pid = 0; */
01728       bc->need_disconnect = 0;
01729       bc->need_release = 0;
01730       bc->need_release_complete = 0;
01731 
01732       cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
01733 
01734       empty_bc(bc);
01735       clean_up_bc(bc);
01736 
01737       if (channel > 0)
01738          empty_chan_in_stack(stack, channel);
01739       bc->in_use = 0;
01740 
01741       dump_chan_list(stack);
01742 
01743       if (bc->stack_holder) {
01744          cb_log(4, stack->port, "REMOVING Holder\n");
01745          stack_holder_remove(stack, bc);
01746          free(bc);
01747       }
01748 
01749       return 1;
01750    default:
01751       break;
01752    }
01753 
01754    return 0;
01755 }
01756 
01757 
01758 /* Empties bc if it's reserved (no SETUP out yet) */
01759 void misdn_lib_release(struct misdn_bchannel *bc)
01760 {
01761    int channel;
01762    struct misdn_stack *stack=get_stack_by_bc(bc);
01763 
01764    if (!stack) {
01765       cb_log(1,0,"misdn_release: No Stack found\n");
01766       return;
01767    }
01768 
01769    channel = bc->channel;
01770    empty_bc(bc);
01771    clean_up_bc(bc);
01772    if (channel > 0) {
01773       empty_chan_in_stack(stack, channel);
01774    }
01775    bc->in_use=0;
01776 }
01777 
01778 
01779 
01780 
01781 int misdn_lib_get_port_up (int port)
01782 { /* Pull Up L1 */
01783    struct misdn_stack *stack;
01784 
01785    for (stack=glob_mgr->stack_list;
01786         stack;
01787         stack=stack->next) {
01788 
01789       if (stack->port == port) {
01790 
01791          if (!stack->l1link)
01792             misdn_lib_get_l1_up(stack);
01793          /* handle_l1 will start L2 for NT. */
01794          if (!stack->l2link && !stack->nt) {
01795             misdn_lib_get_l2_up(stack);
01796          }
01797 
01798          return 0;
01799       }
01800    }
01801    return 0;
01802 }
01803 
01804 
01805 int misdn_lib_get_port_down (int port)
01806 { /* Pull Down L1 */
01807    struct misdn_stack *stack;
01808    for (stack=glob_mgr->stack_list;
01809         stack;
01810         stack=stack->next) {
01811       if (stack->port == port) {
01812             if (stack->l2link)
01813                misdn_lib_get_l2_down(stack);
01814             misdn_lib_get_l1_down(stack);
01815          return 0;
01816       }
01817    }
01818    return 0;
01819 }
01820 
01821 int misdn_lib_port_up(int port, int check)
01822 {
01823    struct misdn_stack *stack;
01824 
01825 
01826    for (stack=glob_mgr->stack_list;
01827         stack;
01828         stack=stack->next) {
01829 
01830       if (stack->port == port) {
01831 
01832          if (stack->blocked) {
01833             cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
01834             return -1;
01835          }
01836 
01837          if (stack->ptp ) {
01838 
01839             if (stack->l1link && stack->l2link) {
01840                return 1;
01841             } else {
01842                cb_log(1,port, "Port Down L2:%d L1:%d\n",
01843                   stack->l2link, stack->l1link);
01844                return 0;
01845             }
01846          } else {
01847             if ( !check || stack->l1link )
01848                return 1;
01849             else {
01850                cb_log(1,port, "Port down PMP\n");
01851                return 0;
01852             }
01853          }
01854       }
01855    }
01856 
01857    return -1;
01858 }
01859 
01860 
01861 static int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh)
01862 {
01863    struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
01864    struct misdn_bchannel dummybc;
01865    iframe_t frm; /* fake te frm to remove callref from global callreflist */
01866 
01867    frm.dinfo = hh->dinfo;
01868    frm.addr=stack->upper_id | FLG_MSG_DOWN;
01869    frm.prim = CC_RELEASE_CR|INDICATION;
01870    cb_log(4, stack->port, " --> CC_RELEASE_CR: Faking Release_cr for %x l3id:%x\n",frm.addr, frm.dinfo);
01871 
01872    /** removing procid **/
01873    if (!bc) {
01874       cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
01875       misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0);
01876       bc=&dummybc;
01877    }
01878 
01879    if ((bc->l3_id & 0xff00) == 0xff00) {
01880       cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id & 0xff);
01881       stack->procids[bc->l3_id & 0xff] = 0;
01882    }
01883 
01884    if (handle_cr(stack, &frm)<0) {
01885    }
01886 
01887    return 0 ;
01888 }
01889 
01890 static int handle_event_nt(void *dat, void *arg)
01891 {
01892    struct misdn_bchannel dummybc;
01893    struct misdn_bchannel *bc;
01894    manager_t *mgr = (manager_t *)dat;
01895    msg_t *msg = (msg_t *)arg;
01896    msg_t *dmsg;
01897    mISDNuser_head_t *hh;
01898    struct misdn_stack *stack;
01899    enum event_e event;
01900    int reject=0;
01901    int l3id;
01902    int channel;
01903    int tmpcause;
01904 
01905    if (!msg || !mgr)
01906       return(-EINVAL);
01907 
01908    stack = find_stack_by_mgr(mgr);
01909    hh=(mISDNuser_head_t*)msg->data;
01910 
01911    /*
01912     * When we are called from the mISDNuser lib, the nstlock is held and it
01913     * must be held when we return.  We unlock here because the lib may be
01914     * entered again recursively.
01915     */
01916    pthread_mutex_unlock(&stack->nstlock);
01917 
01918    cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
01919    switch(hh->prim) {
01920    case CC_RETRIEVE|INDICATION:
01921    {
01922       struct misdn_bchannel *hold_bc;
01923       iframe_t frm; /* fake te frm to add callref to global callreflist */
01924 
01925       frm.dinfo = hh->dinfo;
01926       frm.addr=stack->upper_id | FLG_MSG_DOWN;
01927       frm.prim = CC_NEW_CR|INDICATION;
01928       if (handle_cr( stack, &frm)< 0) {
01929          goto ERR_NO_CHANNEL;
01930       }
01931 
01932       bc = find_bc_by_l3id(stack, hh->dinfo);
01933       hold_bc = stack_holder_find(stack, bc->l3_id);
01934       cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
01935 
01936       if (hold_bc) {
01937          cb_log(4, stack->port, "REMOVING Holder\n");
01938 
01939          /* swap the backup to our new channel back */
01940          stack_holder_remove(stack, hold_bc);
01941          memcpy(bc, hold_bc, sizeof(*bc));
01942          free(hold_bc);
01943 
01944          bc->holded=0;
01945          bc->b_stid=0;
01946       }
01947       break;
01948    }
01949 
01950    case CC_SETUP | CONFIRM:
01951       l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
01952 
01953       cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n", l3id);
01954 
01955       bc = find_bc_by_l3id(stack, hh->dinfo);
01956       if (bc) {
01957          cb_log (2, bc->port, "I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
01958          bc->l3_id = l3id;
01959          cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
01960       } else {
01961          cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n");
01962       }
01963       free_msg(msg);
01964       pthread_mutex_lock(&stack->nstlock);
01965       return 0;
01966 
01967    case CC_SETUP | INDICATION:
01968       bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0);
01969       if (!bc) {
01970          goto ERR_NO_CHANNEL;
01971       }
01972 
01973       cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
01974       bc->l3_id=hh->dinfo;
01975       break;
01976 
01977 #if defined(AST_MISDN_ENHANCEMENTS)
01978    case CC_REGISTER | CONFIRM:
01979       l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
01980 
01981       cb_log(4, stack->port, " --> lib: Event_ind:REGISTER CONFIRM [NT] : new L3ID is %x\n", l3id);
01982 
01983       bc = find_bc_by_l3id(stack, hh->dinfo);
01984       if (bc) {
01985          cb_log (2, bc->port, "I IND :CC_REGISTER|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
01986          bc->l3_id = l3id;
01987       } else {
01988          cb_log(4, stack->port, "Bc Not found (after REGISTER CONFIRM)\n");
01989       }
01990       free_msg(msg);
01991       pthread_mutex_lock(&stack->nstlock);
01992       return 0;
01993 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
01994 
01995 #if defined(AST_MISDN_ENHANCEMENTS)
01996    case CC_REGISTER | INDICATION:
01997       bc = misdn_lib_get_register_bc(stack->port);
01998       if (!bc) {
01999          goto ERR_NO_CHANNEL;
02000       }
02001 
02002       cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
02003       bc->l3_id=hh->dinfo;
02004       break;
02005 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
02006 
02007    case CC_CONNECT_ACKNOWLEDGE|INDICATION:
02008       break;
02009 
02010    case CC_ALERTING|INDICATION:
02011    case CC_PROCEEDING|INDICATION:
02012    case CC_SETUP_ACKNOWLEDGE|INDICATION:
02013    case CC_CONNECT|INDICATION:
02014       break;
02015    case CC_DISCONNECT|INDICATION:
02016       bc = find_bc_by_l3id(stack, hh->dinfo);
02017       if (!bc) {
02018          bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
02019          if (bc) {
02020             int myprocid=bc->l3_id&0x0000ffff;
02021 
02022             hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
02023             cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
02024             reject=1;
02025          }
02026       }
02027       break;
02028 
02029    case CC_FACILITY|INDICATION:
02030       bc = find_bc_by_l3id(stack, hh->dinfo);
02031       if (!bc) {
02032          bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
02033          if (bc) {
02034             int myprocid=bc->l3_id&0x0000ffff;
02035 
02036             hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
02037             cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
02038          }
02039       }
02040       break;
02041 
02042    case CC_RELEASE_COMPLETE|INDICATION:
02043       break;
02044 
02045    case CC_SUSPEND|INDICATION:
02046       cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
02047       dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
02048       pthread_mutex_lock(&stack->nstlock);
02049       stack->nst.manager_l3(&stack->nst, dmsg);
02050       free_msg(msg);
02051       return 0;
02052 
02053    case CC_RESUME|INDICATION:
02054       break;
02055 
02056    case CC_RELEASE|CONFIRM:
02057       bc = find_bc_by_l3id(stack, hh->dinfo);
02058       if (bc) {
02059          cb_log(1, stack->port, "CC_RELEASE|CONFIRM (l3id:%x), sending RELEASE_COMPLETE\n", hh->dinfo);
02060          misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
02061       }
02062       break;
02063 
02064    case CC_RELEASE|INDICATION:
02065       break;
02066 
02067    case CC_RELEASE_CR|INDICATION:
02068       release_cr(stack, hh);
02069       free_msg(msg);
02070       pthread_mutex_lock(&stack->nstlock);
02071       return 0;
02072 
02073    case CC_NEW_CR|INDICATION:
02074       /*  Got New CR for bchan, for now I handle this one in */
02075       /*  connect_ack, Need to be changed */
02076       l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
02077 
02078       bc = find_bc_by_l3id(stack, hh->dinfo);
02079       if (!bc) {
02080          cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n");
02081          pthread_mutex_lock(&stack->nstlock);
02082          return -1;
02083       }
02084       if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
02085          cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
02086          stack->procids[bc->l3_id&0xff] = 0 ;
02087       }
02088       cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID  is %x\n",l3id );
02089 
02090       bc->l3_id =l3id;
02091       if (!bc->is_register_pool) {
02092          cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
02093       }
02094 
02095       free_msg(msg);
02096       pthread_mutex_lock(&stack->nstlock);
02097       return 0;
02098 
02099    case DL_ESTABLISH | INDICATION:
02100    case DL_ESTABLISH | CONFIRM:
02101       cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
02102 
02103       if (stack->ptp && stack->l2link) {
02104          cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
02105          cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
02106       }
02107 
02108       if (stack->ptp && !stack->restart_sent) {
02109          /* make sure we restart the interface of the
02110           * other side */
02111          stack->restart_sent=1;
02112          misdn_lib_send_restart(stack->port, -1);
02113 
02114       }
02115 
02116       /* when we get the L2 UP, the L1 is UP definitely too*/
02117       stack->l2link = 1;
02118       stack->l2upcnt=0;
02119 
02120       free_msg(msg);
02121       pthread_mutex_lock(&stack->nstlock);
02122       return 0;
02123 
02124    case DL_RELEASE | INDICATION:
02125    case DL_RELEASE | CONFIRM:
02126       cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
02127       if (stack->ptp) {
02128 
02129          if (stack->l2upcnt>3) {
02130             cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attempts!!!\n");
02131          } else {
02132             if (stack->l1link) {
02133                misdn_lib_get_l2_up(stack);
02134                stack->l2upcnt++;
02135             }
02136          }
02137       }
02138 
02139       stack->l2link = 0;
02140       free_msg(msg);
02141       pthread_mutex_lock(&stack->nstlock);
02142       return 0;
02143 
02144    default:
02145       break;
02146    }
02147 
02148    /*  Parse Events and fire_up to App. */
02149    event = isdn_msg_get_event(msgs_g, msg, 1);
02150 
02151    bc = find_bc_by_l3id(stack, hh->dinfo);
02152    if (!bc) {
02153       cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x).\n", hh->dinfo);
02154       misdn_make_dummy(&dummybc, stack->port,  hh->dinfo, stack->nt, 0);
02155       bc = &dummybc;
02156    }
02157 
02158    isdn_msg_parse_event(msgs_g, msg, bc, 1);
02159 
02160    switch (event) {
02161    case EVENT_SETUP:
02162       if (bc->channel <= 0 || bc->channel == 0xff) {
02163          bc->channel = 0;
02164       }
02165 
02166       if (find_free_chan_in_stack(stack, bc, bc->channel, 0) < 0) {
02167          goto ERR_NO_CHANNEL;
02168       }
02169       break;
02170    case EVENT_RELEASE:
02171    case EVENT_RELEASE_COMPLETE:
02172       channel = bc->channel;
02173       tmpcause = bc->cause;
02174 
02175       empty_bc(bc);
02176       bc->cause = tmpcause;
02177       clean_up_bc(bc);
02178 
02179       if (channel > 0)
02180          empty_chan_in_stack(stack, channel);
02181       bc->in_use = 0;
02182       break;
02183    default:
02184       break;
02185    }
02186 
02187    if(!isdn_get_info(msgs_g, event, 1)) {
02188       cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n", hh->prim, hh->dinfo);
02189    } else {
02190       if (reject) {
02191          switch(bc->cause) {
02192          case AST_CAUSE_USER_BUSY:
02193             cb_log(1, stack->port, "Siemens Busy reject..\n");
02194             break;
02195          default:
02196             break;
02197          }
02198       }
02199       cb_event(event, bc, glob_mgr->user_data);
02200    }
02201 
02202    free_msg(msg);
02203    pthread_mutex_lock(&stack->nstlock);
02204    return 0;
02205 
02206 ERR_NO_CHANNEL:
02207    cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
02208    dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, hh->dinfo, sizeof(RELEASE_COMPLETE_t), 1);
02209    pthread_mutex_lock(&stack->nstlock);
02210    stack->nst.manager_l3(&stack->nst, dmsg);
02211    free_msg(msg);
02212    return 0;
02213 }
02214 
02215 
02216 static int handle_timers(msg_t* msg)
02217 {
02218    iframe_t *frm= (iframe_t*)msg->data;
02219    struct misdn_stack *stack;
02220 
02221    /* Timer Stuff */
02222    switch (frm->prim) {
02223    case MGR_INITTIMER | CONFIRM:
02224    case MGR_ADDTIMER | CONFIRM:
02225    case MGR_DELTIMER | CONFIRM:
02226    case MGR_REMOVETIMER | CONFIRM:
02227       free_msg(msg);
02228       return(1);
02229    }
02230 
02231 
02232 
02233    if (frm->prim==(MGR_TIMER | INDICATION) ) {
02234       for (stack = glob_mgr->stack_list;
02235            stack;
02236            stack = stack->next) {
02237          itimer_t *it;
02238 
02239          if (!stack->nt) continue;
02240 
02241          it = stack->nst.tlist;
02242          /* find timer */
02243          for(it=stack->nst.tlist;
02244              it;
02245              it=it->next) {
02246             if (it->id == (int)frm->addr)
02247                break;
02248          }
02249          if (it) {
02250             mISDN_write_frame(stack->midev, msg->data, frm->addr,
02251                     MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
02252             test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
02253             pthread_mutex_lock(&stack->nstlock);
02254             it->function(it->data);
02255             pthread_mutex_unlock(&stack->nstlock);
02256             free_msg(msg);
02257             return 1;
02258          }
02259       }
02260 
02261       cb_log(0, 0, "Timer Msg without Timer ??\n");
02262       free_msg(msg);
02263       return 1;
02264    }
02265 
02266    return 0;
02267 }
02268 
02269 
02270 
02271 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
02272 {
02273    bc->generate_tone=1;
02274 }
02275 
02276 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
02277 {
02278    bc->generate_tone=0;
02279 }
02280 
02281 
02282 static int do_tone(struct misdn_bchannel *bc, int len)
02283 {
02284    bc->tone_cnt=len;
02285 
02286    if (bc->generate_tone) {
02287       cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
02288 
02289       if ( !bc->nojitter ) {
02290          misdn_tx_jitter(bc,len);
02291       }
02292 
02293       return 1;
02294    }
02295 
02296    return 0;
02297 }
02298 
02299 
02300 #ifdef MISDN_SAVE_DATA
02301 static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2)
02302 {
02303    char n1[32],n2[32];
02304    FILE *rx, *tx;
02305 
02306    sprintf(n1,"/tmp/misdn-rx-%d.raw",id);
02307    sprintf(n2,"/tmp/misdn-tx-%d.raw",id);
02308 
02309    rx = fopen(n1,"a+");
02310    tx = fopen(n2,"a+");
02311 
02312    if (!rx || !tx) {
02313       cb_log(0,0,"Couldn't open files: %s\n",strerror(errno));
02314       if (rx)
02315          fclose(rx);
02316       if (tx)
02317          fclose(tx);
02318       return ;
02319    }
02320 
02321    fwrite(p1,1,l1,rx);
02322    fwrite(p2,1,l2,tx);
02323 
02324    fclose(rx);
02325    fclose(tx);
02326 
02327 }
02328 #endif
02329 
02330 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
02331 {
02332    char buf[4096 + mISDN_HEADER_LEN];
02333    char *data=&buf[mISDN_HEADER_LEN];
02334    iframe_t *txfrm= (iframe_t*)buf;
02335    int jlen, r;
02336 
02337    jlen=cb_jb_empty(bc,data,len);
02338 
02339    if (jlen) {
02340 #ifdef MISDN_SAVE_DATA
02341       misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len);
02342 #endif
02343       flip_buf_bits( data, jlen);
02344 
02345       if (jlen < len) {
02346          cb_log(1, bc->port, "Jitterbuffer Underrun. Got %d of expected %d\n", jlen, len);
02347       }
02348 
02349       txfrm->prim = DL_DATA|REQUEST;
02350 
02351       txfrm->dinfo = 0;
02352 
02353       txfrm->addr = bc->addr|FLG_MSG_DOWN; /*  | IF_DOWN; */
02354 
02355       txfrm->len =jlen;
02356       cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
02357 
02358       r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
02359    } else {
02360 #define MISDN_GEN_SILENCE
02361 #ifdef MISDN_GEN_SILENCE
02362       int cnt=len/TONE_SILENCE_SIZE;
02363       int rest=len%TONE_SILENCE_SIZE;
02364       int i;
02365 
02366       for (i=0; i<cnt; i++) {
02367          memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
02368          data +=TONE_SILENCE_SIZE;
02369       }
02370 
02371       if (rest) {
02372          memcpy(data, tone_silence_flip, rest);
02373       }
02374 
02375       txfrm->prim = DL_DATA|REQUEST;
02376 
02377       txfrm->dinfo = 0;
02378 
02379       txfrm->addr = bc->addr|FLG_MSG_DOWN; /*  | IF_DOWN; */
02380 
02381       txfrm->len =len;
02382       cb_log(5, bc->port, "Transmitting %d samples of silence to misdn\n", len);
02383 
02384       r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
02385 #else
02386       r = 0;
02387 #endif
02388    }
02389 
02390    if (r < 0) {
02391       cb_log(1, bc->port, "Error in mISDN_write (%s)\n", strerror(errno));
02392    }
02393 }
02394 
02395 static int handle_bchan(msg_t *msg)
02396 {
02397    iframe_t *frm= (iframe_t*)msg->data;
02398    struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
02399    struct misdn_stack *stack;
02400 
02401    if (!bc) {
02402       cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
02403       return 0 ;
02404    }
02405 
02406    stack = get_stack_by_bc(bc);
02407 
02408    if (!stack) {
02409       cb_log(0, bc->port,"handle_bchan: STACK not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
02410       return 0;
02411    }
02412 
02413    switch (frm->prim) {
02414 
02415    case MGR_SETSTACK| CONFIRM:
02416       cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
02417       break;
02418 
02419    case MGR_SETSTACK| INDICATION:
02420       cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
02421    break;
02422 
02423    case MGR_DELLAYER| INDICATION:
02424       cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
02425       break;
02426 
02427    case MGR_DELLAYER| CONFIRM:
02428       cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
02429 
02430       bc->pid=0;
02431       bc->addr=0;
02432 
02433       free_msg(msg);
02434       return 1;
02435 
02436    case PH_ACTIVATE | INDICATION:
02437    case DL_ESTABLISH | INDICATION:
02438       cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
02439 
02440       free_msg(msg);
02441       return 1;
02442 
02443    case PH_ACTIVATE | CONFIRM:
02444    case DL_ESTABLISH | CONFIRM:
02445 
02446       cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
02447       free_msg(msg);
02448 
02449       return 1;
02450 
02451    case DL_ESTABLISH | REQUEST:
02452       {
02453          char buf[128];
02454          mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN,  DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
02455       }
02456       free_msg(msg);
02457       return 1;
02458 
02459    case DL_RELEASE|REQUEST:
02460       {
02461          char buf[128];
02462          mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN,  DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
02463       }
02464       free_msg(msg);
02465       return 1;
02466 
02467    case PH_DEACTIVATE | INDICATION:
02468    case DL_RELEASE | INDICATION:
02469       cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
02470 
02471       free_msg(msg);
02472       return 1;
02473 
02474    case PH_DEACTIVATE | CONFIRM:
02475    case DL_RELEASE | CONFIRM:
02476       cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
02477 
02478       free_msg(msg);
02479       return 1;
02480 
02481    case PH_CONTROL|INDICATION:
02482    {
02483       unsigned int *cont = (unsigned int *) &frm->data.p;
02484 
02485       cb_log(4, stack->port,
02486          "PH_CONTROL: channel:%d caller%d:\"%s\" <%s> dialed%d:%s \n",
02487          bc->channel,
02488          bc->caller.number_type,
02489          bc->caller.name,
02490          bc->caller.number,
02491          bc->dialed.number_type,
02492          bc->dialed.number);
02493 
02494       if ((*cont & ~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
02495          int dtmf = *cont & DTMF_TONE_MASK;
02496          cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
02497          bc->dtmf=dtmf;
02498          cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
02499 
02500          free_msg(msg);
02501          return 1;
02502       }
02503       if (*cont == BF_REJECT) {
02504          cb_log(4, stack->port, " --> BF REJECT\n");
02505          free_msg(msg);
02506          return 1;
02507       }
02508       if (*cont == BF_ACCEPT) {
02509          cb_log(4, stack->port, " --> BF ACCEPT\n");
02510          free_msg(msg);
02511          return 1;
02512       }
02513    }
02514    break;
02515 
02516    case PH_DATA|REQUEST:
02517    case DL_DATA|REQUEST:
02518       cb_log(0, stack->port, "DL_DATA REQUEST \n");
02519       do_tone(bc, 64);
02520 
02521       free_msg(msg);
02522       return 1;
02523 
02524 
02525    case PH_DATA|INDICATION:
02526    case DL_DATA|INDICATION:
02527    {
02528       bc->bframe = (void*)&frm->data.i;
02529       bc->bframe_len = frm->len;
02530 
02531       /** Anyway flip the bufbits **/
02532       if ( misdn_cap_is_speech(bc->capability) )
02533          flip_buf_bits(bc->bframe, bc->bframe_len);
02534 
02535 
02536       if (!bc->bframe_len) {
02537          cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
02538          free_msg(msg);
02539          return 1;
02540       }
02541 
02542       if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
02543          cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
02544          free_msg(msg);
02545          return 1;
02546       }
02547 
02548 #if MISDN_DEBUG
02549       cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
02550 
02551 #endif
02552 
02553       if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
02554          int t;
02555 
02556 #ifdef MISDN_B_DEBUG
02557          cb_log(0,bc->port,"do_tone START\n");
02558 #endif
02559          t=do_tone(bc,frm->len);
02560 
02561 #ifdef MISDN_B_DEBUG
02562          cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
02563 #endif
02564          if (  !t ) {
02565             int i;
02566 
02567             if ( misdn_cap_is_speech(bc->capability)) {
02568                if ( !bc->nojitter ) {
02569 #ifdef MISDN_B_DEBUG
02570                   cb_log(0,bc->port,"tx_jitter START\n");
02571 #endif
02572                   misdn_tx_jitter(bc,frm->len);
02573 #ifdef MISDN_B_DEBUG
02574                   cb_log(0,bc->port,"tx_jitter STOP\n");
02575 #endif
02576                }
02577             }
02578 
02579 #ifdef MISDN_B_DEBUG
02580             cb_log(0,bc->port,"EVENT_B_DATA START\n");
02581 #endif
02582 
02583             i = cb_event(EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
02584 #ifdef MISDN_B_DEBUG
02585             cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
02586 #endif
02587 
02588             if (i<0) {
02589                cb_log(10,stack->port,"cb_event returned <0\n");
02590                /*clean_up_bc(bc);*/
02591             }
02592          }
02593       }
02594       free_msg(msg);
02595       return 1;
02596    }
02597 
02598 
02599    case PH_CONTROL | CONFIRM:
02600       cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
02601       free_msg(msg);
02602       return 1;
02603 
02604    case PH_DATA | CONFIRM:
02605    case DL_DATA|CONFIRM:
02606 #if MISDN_DEBUG
02607 
02608       cb_log(0, stack->port, "Data confirmed\n");
02609 
02610 #endif
02611       free_msg(msg);
02612       return 1;
02613    case DL_DATA|RESPONSE:
02614 #if MISDN_DEBUG
02615       cb_log(0, stack->port, "Data response\n");
02616 
02617 #endif
02618       break;
02619    }
02620 
02621    return 0;
02622 }
02623 
02624 
02625 
02626 static int handle_frm_nt(msg_t *msg)
02627 {
02628    iframe_t *frm= (iframe_t*)msg->data;
02629    struct misdn_stack *stack;
02630    int err=0;
02631 
02632    stack=find_stack_by_addr( frm->addr );
02633 
02634 
02635 
02636    if (!stack || !stack->nt) {
02637       return 0;
02638    }
02639 
02640 
02641    pthread_mutex_lock(&stack->nstlock);
02642    if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
02643       pthread_mutex_unlock(&stack->nstlock);
02644       if (nt_err_cnt > 0 ) {
02645          if (nt_err_cnt < 100) {
02646             nt_err_cnt++;
02647             cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
02648          } else if (nt_err_cnt < 105){
02649             cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
02650             nt_err_cnt = - 1;
02651          }
02652       }
02653       free_msg(msg);
02654       return 1;
02655    }
02656    pthread_mutex_unlock(&stack->nstlock);
02657    return 1;
02658 }
02659 
02660 
02661 static int handle_frm_te(msg_t *msg)
02662 {
02663    struct misdn_bchannel dummybc;
02664    struct misdn_bchannel *bc;
02665    iframe_t *frm;
02666    struct misdn_stack *stack;
02667    enum event_e event;
02668    enum event_response_e response;
02669    int ret;
02670    int channel;
02671    int tmpcause;
02672    int tmp_out_cause;
02673 
02674    frm = (iframe_t*) msg->data;
02675    stack = find_stack_by_addr(frm->addr);
02676    if (!stack || stack->nt) {
02677       return 0;
02678    }
02679 
02680    cb_log(4, stack->port, "handle_frm_te: frm->addr:%x frm->prim:%x\n", frm->addr, frm->prim);
02681 
02682    ret = handle_cr(stack, frm);
02683    if (ret < 0) {
02684       cb_log(3, stack->port, "handle_frm_te: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
02685    }
02686    if (ret) {
02687       free_msg(msg);
02688       return 1;
02689    }
02690 
02691    bc = find_bc_by_l3id(stack, frm->dinfo);
02692    if (!bc) {
02693       misdn_make_dummy(&dummybc, stack->port, 0, stack->nt, 0);
02694       switch (frm->prim) {
02695       case CC_RESTART | CONFIRM:
02696          dummybc.l3_id = MISDN_ID_GLOBAL;
02697          bc = &dummybc;
02698          break;
02699       case CC_SETUP | INDICATION:
02700          dummybc.l3_id = frm->dinfo;
02701          bc = &dummybc;
02702 
02703          /* set a reasonable cause */
02704          bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
02705          /* if we want to send something the flag must be set! */
02706          bc->need_release_complete = 1;
02707          misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
02708 
02709          free_msg(msg);
02710          return 1;
02711       default:
02712          if (frm->prim == (CC_FACILITY | INDICATION)) {
02713             cb_log(5, stack->port, " --> Using Dummy BC for FACILITY\n");
02714          } else {
02715             cb_log(0, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
02716             dummybc.l3_id = frm->dinfo;
02717          }
02718          bc = &dummybc;
02719          break;
02720       }
02721    }
02722 
02723    event = isdn_msg_get_event(msgs_g, msg, 0);
02724    isdn_msg_parse_event(msgs_g, msg, bc, 0);
02725 
02726    /* Preprocess some Events */
02727    ret = handle_event_te(bc, event, frm);
02728    if (ret < 0) {
02729       cb_log(0, stack->port, "couldn't handle event\n");
02730       free_msg(msg);
02731       return 1;
02732    }
02733 
02734    /* shoot up event to App: */
02735    cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n", frm->addr, frm->prim, frm->dinfo);
02736 
02737    if (!isdn_get_info(msgs_g, event, 0)) {
02738       cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n", frm->addr, frm->prim, frm->dinfo);
02739       response = RESPONSE_OK;
02740    } else {
02741       response = cb_event(event, bc, glob_mgr->user_data);
02742    }
02743 
02744    switch (event) {
02745    case EVENT_SETUP:
02746       switch (response) {
02747       case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
02748          cb_log(0, stack->port, "TOTALLY IGNORING SETUP\n");
02749          break;
02750       case RESPONSE_IGNORE_SETUP:
02751          cb_log(0, stack->port, "GOT IGNORE SETUP\n");
02752          /* I think we should send CC_RELEASE_CR, but am not sure*/
02753          bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
02754          /* fall through */
02755       case RESPONSE_RELEASE_SETUP:
02756          if (response == RESPONSE_RELEASE_SETUP) {
02757             cb_log(0, stack->port, "GOT RELEASE SETUP\n");
02758          }
02759          misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
02760          break;
02761       case RESPONSE_OK:
02762          cb_log(4, stack->port, "GOT SETUP OK\n");
02763          break;
02764       default:
02765          cb_log(0, stack->port, "GOT UNKNOWN SETUP RESPONSE\n");
02766          break;
02767       }
02768       break;
02769    case EVENT_RELEASE_COMPLETE:
02770       /* release bchannel only after we've announced the RELEASE_COMPLETE */
02771       channel = bc->channel;
02772       tmpcause = bc->cause;
02773       tmp_out_cause = bc->out_cause;
02774 
02775       empty_bc(bc);
02776       bc->cause = tmpcause;
02777       bc->out_cause = tmp_out_cause;
02778       clean_up_bc(bc);
02779       bc->in_use = 0;
02780 
02781       if (tmpcause == AST_CAUSE_REQUESTED_CHAN_UNAVAIL) {
02782          cb_log(0, stack->port, "**** Received CAUSE:%d, restarting channel %d\n", AST_CAUSE_REQUESTED_CHAN_UNAVAIL, channel);
02783          misdn_lib_send_restart(stack->port, channel);
02784       }
02785       if (channel > 0) {
02786          empty_chan_in_stack(stack, channel);
02787       }
02788       break;
02789    case EVENT_RESTART:
02790       cb_log(0, stack->port, "**** Received RESTART channel:%d\n", bc->restart_channel);
02791       empty_chan_in_stack(stack, bc->restart_channel);
02792       break;
02793    default:
02794       break;
02795    }
02796 
02797    cb_log(5, stack->port, "Freeing Msg on prim:%x \n", frm->prim);
02798    free_msg(msg);
02799    return 1;
02800 }
02801 
02802 
02803 static int handle_l1(msg_t *msg)
02804 {
02805    iframe_t *frm = (iframe_t*) msg->data;
02806    struct misdn_stack *stack = find_stack_by_addr(frm->addr);
02807 
02808    if (!stack) return 0 ;
02809 
02810    switch (frm->prim) {
02811    case PH_ACTIVATE | CONFIRM:
02812       /* we have to check for errors! */
02813       if (frm->len) {
02814          cb_log (3, stack->port, "L1: PH_ACTIVATE|REQUEST returned error!\n");
02815          free_msg(msg);
02816          return 1;
02817       }
02818       /* fall through */
02819    case PH_ACTIVATE | INDICATION:
02820       cb_log (3, stack->port, "L1: PH L1Link Up!\n");
02821       stack->l1link=1;
02822 
02823       if (stack->nt) {
02824 
02825          pthread_mutex_lock(&stack->nstlock);
02826          if (stack->nst.l1_l2(&stack->nst, msg))
02827             free_msg(msg);
02828          pthread_mutex_unlock(&stack->nstlock);
02829 
02830          if (stack->ptp)
02831             misdn_lib_get_l2_up(stack);
02832       } else {
02833          free_msg(msg);
02834       }
02835       return 1;
02836 
02837    case PH_ACTIVATE | REQUEST:
02838       free_msg(msg);
02839       cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
02840       return 1;
02841 
02842    case PH_DEACTIVATE | REQUEST:
02843       free_msg(msg);
02844       cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
02845       return 1;
02846 
02847    case PH_DEACTIVATE | CONFIRM:
02848       /* we have to check for errors! */
02849       if (frm->len) {
02850          cb_log (3, stack->port, "L1: PH_DEACTIVATE|REQUEST returned error!\n");
02851          free_msg(msg);
02852          return 1;
02853       }
02854       /* fall through */
02855    case PH_DEACTIVATE | INDICATION:
02856       cb_log (3, stack->port, "L1: PH L1Link Down! \n");
02857 
02858 #if 0
02859       for (i=0; i<=stack->b_num; i++) {
02860          if (global_state == MISDN_INITIALIZED)  {
02861             cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
02862          }
02863       }
02864 #endif
02865 
02866       if (stack->nt) {
02867          pthread_mutex_lock(&stack->nstlock);
02868          if (stack->nst.l1_l2(&stack->nst, msg))
02869             free_msg(msg);
02870          pthread_mutex_unlock(&stack->nstlock);
02871       } else {
02872          free_msg(msg);
02873       }
02874 
02875       stack->l1link=0;
02876       stack->l2link=0;
02877       return 1;
02878    }
02879 
02880    return 0;
02881 }
02882 
02883 static int handle_l2(msg_t *msg)
02884 {
02885    iframe_t *frm = (iframe_t*) msg->data;
02886 
02887    struct misdn_stack *stack = find_stack_by_addr(frm->addr);
02888 
02889    if (!stack) {
02890       return 0 ;
02891    }
02892 
02893    switch(frm->prim) {
02894 
02895    case DL_ESTABLISH | REQUEST:
02896       cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
02897       free_msg(msg);
02898       return 1;
02899    case DL_RELEASE | REQUEST:
02900       cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
02901       free_msg(msg);
02902       return 1;
02903 
02904    case DL_ESTABLISH | INDICATION:
02905    case DL_ESTABLISH | CONFIRM:
02906    {
02907       cb_log (3, stack->port, "L2: L2Link Up! \n");
02908       if (stack->ptp && stack->l2link) {
02909          cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n");
02910          cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
02911       }
02912       stack->l2link=1;
02913       free_msg(msg);
02914       return 1;
02915    }
02916    break;
02917 
02918    case DL_RELEASE | INDICATION:
02919    case DL_RELEASE | CONFIRM:
02920    {
02921       cb_log (3, stack->port, "L2: L2Link Down! \n");
02922       stack->l2link=0;
02923 
02924       free_msg(msg);
02925       return 1;
02926    }
02927    break;
02928    }
02929    return 0;
02930 }
02931 
02932 static int handle_mgmt(msg_t *msg)
02933 {
02934    iframe_t *frm = (iframe_t*) msg->data;
02935    struct misdn_stack *stack;
02936 
02937    if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
02938       cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
02939       free_msg(msg);
02940       return 1;
02941    }
02942 
02943    stack = find_stack_by_addr(frm->addr);
02944 
02945    if (!stack) {
02946       if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
02947          cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
02948                frm->addr) ;
02949          free_msg(msg);
02950          return 1;
02951       }
02952 
02953       return 0;
02954    }
02955 
02956    switch(frm->prim) {
02957    case MGR_SHORTSTATUS | INDICATION:
02958    case MGR_SHORTSTATUS | CONFIRM:
02959       cb_log(5, stack->port, "MGMT: Short status dinfo %x\n",frm->dinfo);
02960 
02961       switch (frm->dinfo) {
02962       case SSTATUS_L1_ACTIVATED:
02963          cb_log(3, stack->port, "MGMT: SSTATUS: L1_ACTIVATED \n");
02964          stack->l1link=1;
02965 
02966          break;
02967       case SSTATUS_L1_DEACTIVATED:
02968          cb_log(3, stack->port, "MGMT: SSTATUS: L1_DEACTIVATED \n");
02969          stack->l1link=0;
02970 #if 0
02971          clear_l3(stack);
02972 #endif
02973          break;
02974 
02975       case SSTATUS_L2_ESTABLISHED:
02976          cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
02977          stack->l2link=1;
02978          break;
02979 
02980       case SSTATUS_L2_RELEASED:
02981          cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
02982          stack->l2link=0;
02983          break;
02984       }
02985 
02986       free_msg(msg);
02987       return 1;
02988 
02989    case MGR_SETSTACK | INDICATION:
02990       cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
02991       free_msg(msg);
02992       return 1;
02993    case MGR_DELLAYER | CONFIRM:
02994       cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
02995       free_msg(msg);
02996       return 1;
02997 
02998    }
02999 
03000    /*
03001    if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
03002    cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
03003    free_msg(msg);
03004    return 1;
03005    } */
03006 
03007    return 0;
03008 }
03009 
03010 
03011 static msg_t *fetch_msg(int midev)
03012 {
03013    msg_t *msg=alloc_msg(MAX_MSG_SIZE);
03014    int r;
03015 
03016    if (!msg) {
03017       cb_log(0, 0, "fetch_msg: alloc msg failed !!");
03018       return NULL;
03019    }
03020 
03021    AGAIN:
03022       r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
03023       msg->len=r;
03024 
03025       if (r==0) {
03026          free_msg(msg); /* danger, cause usually freeing in main_loop */
03027          cb_log(6,0,"Got empty Msg..\n");
03028          return NULL;
03029       }
03030 
03031       if (r<0) {
03032          if (errno == EAGAIN) {
03033             /*we wait for mISDN here*/
03034             cb_log(4,0,"mISDN_read wants us to wait\n");
03035             usleep(5000);
03036             goto AGAIN;
03037          }
03038 
03039          cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
03040       }
03041 
03042 #if 0
03043                if  (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
03044                        cb_log(0,0,"prim: %x dinfo:%x addr:%x msglen:%d frm->len:%d\n",frm->prim, frm->dinfo, frm->addr, msg->len,frm->len );
03045 #endif
03046       return msg;
03047 }
03048 
03049 void misdn_lib_isdn_l1watcher(int port)
03050 {
03051    struct misdn_stack *stack;
03052 
03053    for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
03054       ;
03055 
03056    if (stack) {
03057       cb_log(4, port, "Checking L1 State\n");
03058       if (!stack->l1link) {
03059          cb_log(4, port, "L1 State Down, trying to get it up again\n");
03060          misdn_lib_get_short_status(stack);
03061          misdn_lib_get_l1_up(stack);
03062          misdn_lib_get_l2_up(stack);
03063       }
03064    }
03065 }
03066 
03067 /* This is a thread */
03068 static void misdn_lib_isdn_event_catcher(void *arg)
03069 {
03070    struct misdn_lib *mgr = arg;
03071    int zero_frm=0 , fff_frm=0 ;
03072    int midev= mgr->midev;
03073    int port=0;
03074 
03075    while (1) {
03076       msg_t *msg = fetch_msg(midev);
03077       iframe_t *frm;
03078 
03079 
03080       if (!msg) continue;
03081 
03082       frm = (iframe_t*) msg->data;
03083 
03084       /** When we make a call from NT2Ast we get these frames **/
03085       if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
03086          zero_frm++;
03087          free_msg(msg);
03088          continue;
03089       } else {
03090          if (zero_frm) {
03091             cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
03092             zero_frm = 0 ;
03093          }
03094       }
03095 
03096       /** I get this sometimes after setup_bc **/
03097       if (frm->len == 0 &&  frm->dinfo == 0 && frm->prim == 0xffffffff ) {
03098          fff_frm++;
03099          free_msg(msg);
03100          continue;
03101       } else {
03102          if (fff_frm) {
03103             cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
03104             fff_frm = 0 ;
03105          }
03106       }
03107 
03108       manager_isdn_handler(frm, msg);
03109    }
03110 
03111 }
03112 
03113 
03114 /** App Interface **/
03115 
03116 static int te_lib_init(void)
03117 {
03118    char buff[1025] = "";
03119    iframe_t *frm = (iframe_t *) buff;
03120    int midev;
03121    int ret;
03122 
03123    midev = mISDN_open();
03124    if (midev <= 0) {
03125       return midev;
03126    }
03127 
03128    /* create entity for layer 3 TE-mode */
03129    mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
03130 
03131    ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
03132    entity = frm->dinfo & 0xffff;
03133    if (ret < mISDN_HEADER_LEN || !entity) {
03134       fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n", strerror(errno));
03135       mISDN_close(midev);
03136       return -1;
03137    }
03138 
03139    return midev;
03140 }
03141 
03142 void te_lib_destroy(int midev)
03143 {
03144    char buf[1024];
03145    mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
03146 
03147    cb_log(4, 0, "Entity deleted\n");
03148    mISDN_close(midev);
03149    cb_log(4, 0, "midev closed\n");
03150 }
03151 
03152 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
03153 {
03154    struct misdn_stack *stack;
03155    int i;
03156 
03157    for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
03158       for (i = 0; i <= stack->b_num; i++) {
03159          if (stack->bc[i].in_use && stack->bc[i].pid == pid) {
03160             return &stack->bc[i];
03161          }
03162       }
03163    }
03164 
03165    return NULL;
03166 }
03167 
03168 static int test_inuse(struct misdn_bchannel *bc)
03169 {
03170    struct timeval now;
03171 
03172    if (!bc->in_use) {
03173       gettimeofday(&now, NULL);
03174       if (bc->last_used.tv_sec == now.tv_sec
03175          && misdn_lib_port_is_pri(bc->port)) {
03176          cb_log(2, bc->port, "channel with stid:%x for one second still in use! (n:%d lu:%d)\n",
03177             bc->b_stid, (int) now.tv_sec, (int) bc->last_used.tv_sec);
03178          return 1;
03179       }
03180 
03181       cb_log(3,bc->port, "channel with stid:%x not in use!\n", bc->b_stid);
03182       return 0;
03183    }
03184 
03185    cb_log(2,bc->port, "channel with stid:%x in use!\n", bc->b_stid);
03186    return 1;
03187 }
03188 
03189 
03190 static void prepare_bc(struct misdn_bchannel*bc, int channel)
03191 {
03192    bc->channel = channel;
03193    bc->channel_preselected = channel?1:0;
03194    bc->need_disconnect=1;
03195    bc->need_release=1;
03196    bc->need_release_complete=1;
03197    bc->cause = AST_CAUSE_NORMAL_CLEARING;
03198 
03199    if (++mypid>5000) mypid=1;
03200    bc->pid=mypid;
03201 
03202    bc->in_use = 1;
03203 }
03204 
03205 struct misdn_bchannel *misdn_lib_get_free_bc(int port, int channel, int inout, int dec)
03206 {
03207    struct misdn_stack *stack;
03208    int i;
03209    int maxnum;
03210 
03211    if (channel < 0 || channel > MAX_BCHANS) {
03212       cb_log(0, port, "Requested channel out of bounds (%d)\n", channel);
03213       return NULL;
03214    }
03215 
03216    /* Find the port stack structure */
03217    stack = find_stack_by_port(port);
03218    if (!stack) {
03219       cb_log(0, port, "Port is not configured (%d)\n", port);
03220       return NULL;
03221    }
03222 
03223    if (stack->blocked) {
03224       cb_log(0, port, "Port is blocked\n");
03225       return NULL;
03226    }
03227 
03228    pthread_mutex_lock(&stack->st_lock);
03229    if (channel > 0) {
03230       if (channel <= stack->b_num) {
03231          for (i = 0; i < stack->b_num; i++) {
03232             if (stack->bc[i].channel == channel) {
03233                if (test_inuse(&stack->bc[i])) {
03234                   pthread_mutex_unlock(&stack->st_lock);
03235                   cb_log(0, port, "Requested channel:%d on port:%d is already in use\n", channel, port);
03236                   return NULL;
03237                } else {
03238                   prepare_bc(&stack->bc[i], channel);
03239                   pthread_mutex_unlock(&stack->st_lock);
03240                   return &stack->bc[i];
03241                }
03242             }
03243          }
03244       } else {
03245          pthread_mutex_unlock(&stack->st_lock);
03246          cb_log(0, port, "Requested channel:%d is out of bounds on port:%d\n", channel, port);
03247          return NULL;
03248       }
03249    }
03250 
03251    /* Note: channel == 0 here */
03252    maxnum = (inout && !stack->pri && !stack->ptp) ? stack->b_num + 1 : stack->b_num;
03253    if (dec) {
03254       for (i = maxnum - 1; i >= 0; --i) {
03255          if (!test_inuse(&stack->bc[i])) {
03256             /* 3. channel on bri means CW*/
03257             if (!stack->pri && i == stack->b_num) {
03258                stack->bc[i].cw = 1;
03259             }
03260 
03261             prepare_bc(&stack->bc[i], channel);
03262             stack->bc[i].dec = 1;
03263             pthread_mutex_unlock(&stack->st_lock);
03264             return &stack->bc[i];
03265          }
03266       }
03267    } else {
03268       for (i = 0; i < maxnum; ++i) {
03269          if (!test_inuse(&stack->bc[i])) {
03270             /* 3. channel on bri means CW */
03271             if (!stack->pri && i == stack->b_num) {
03272                stack->bc[i].cw = 1;
03273             }
03274 
03275             prepare_bc(&stack->bc[i], channel);
03276             pthread_mutex_unlock(&stack->st_lock);
03277             return &stack->bc[i];
03278          }
03279       }
03280    }
03281    pthread_mutex_unlock(&stack->st_lock);
03282 
03283    cb_log(1, port, "There is no free channel on port (%d)\n", port);
03284    return NULL;
03285 }
03286 
03287 #if defined(AST_MISDN_ENHANCEMENTS)
03288 /*!
03289  * \brief Allocate a B channel struct from the REGISTER pool
03290  *
03291  * \param port Logical port number
03292  *
03293  * \retval B channel struct on success.
03294  * \retval NULL on error.
03295  */
03296 struct misdn_bchannel *misdn_lib_get_register_bc(int port)
03297 {
03298    struct misdn_stack *stack;
03299    struct misdn_bchannel *bc;
03300    unsigned index;
03301 
03302    /* Find the port stack structure */
03303    stack = find_stack_by_port(port);
03304    if (!stack) {
03305       cb_log(0, port, "Port is not configured (%d)\n", port);
03306       return NULL;
03307    }
03308 
03309    if (stack->blocked) {
03310       cb_log(0, port, "Port is blocked\n");
03311       return NULL;
03312    }
03313 
03314    pthread_mutex_lock(&stack->st_lock);
03315    for (index = MAX_BCHANS + 1; index < ARRAY_LEN(stack->bc); ++index) {
03316       bc = &stack->bc[index];
03317       if (!test_inuse(bc)) {
03318          prepare_bc(bc, 0);
03319          bc->need_disconnect = 0;
03320          bc->need_release = 0;
03321          pthread_mutex_unlock(&stack->st_lock);
03322          return bc;
03323       }
03324    }
03325    pthread_mutex_unlock(&stack->st_lock);
03326 
03327    cb_log(1, port, "There is no free REGISTER link on port (%d)\n", port);
03328    return NULL;
03329 }
03330 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
03331 
03332 /*!
03333  * \internal
03334  * \brief Convert the facility function enum value into a string.
03335  *
03336  * \return String version of the enum value
03337  */
03338 static const char *fac2str(enum FacFunction facility)
03339 {
03340    static const struct {
03341       enum FacFunction facility;
03342       char *name;
03343    } arr[] = {
03344 /* *INDENT-OFF* */
03345       { Fac_None, "Fac_None" },
03346 #if defined(AST_MISDN_ENHANCEMENTS)
03347       { Fac_ERROR, "Fac_ERROR" },
03348       { Fac_RESULT, "Fac_RESULT" },
03349       { Fac_REJECT, "Fac_REJECT" },
03350 
03351       { Fac_ActivationDiversion, "Fac_ActivationDiversion" },
03352       { Fac_DeactivationDiversion, "Fac_DeactivationDiversion" },
03353       { Fac_ActivationStatusNotificationDiv, "Fac_ActivationStatusNotificationDiv" },
03354       { Fac_DeactivationStatusNotificationDiv, "Fac_DeactivationStatusNotificationDiv" },
03355       { Fac_InterrogationDiversion, "Fac_InterrogationDiversion" },
03356       { Fac_DiversionInformation, "Fac_DiversionInformation" },
03357       { Fac_CallDeflection, "Fac_CallDeflection" },
03358       { Fac_CallRerouteing, "Fac_CallRerouteing" },
03359       { Fac_DivertingLegInformation2, "Fac_DivertingLegInformation2" },
03360       { Fac_InterrogateServedUserNumbers, "Fac_InterrogateServedUserNumbers" },
03361       { Fac_DivertingLegInformation1, "Fac_DivertingLegInformation1" },
03362       { Fac_DivertingLegInformation3, "Fac_DivertingLegInformation3" },
03363 
03364       { Fac_EctExecute, "Fac_EctExecute" },
03365       { Fac_ExplicitEctExecute, "Fac_ExplicitEctExecute" },
03366       { Fac_RequestSubaddress, "Fac_RequestSubaddress" },
03367       { Fac_SubaddressTransfer, "Fac_SubaddressTransfer" },
03368       { Fac_EctLinkIdRequest, "Fac_EctLinkIdRequest" },
03369       { Fac_EctInform, "Fac_EctInform" },
03370       { Fac_EctLoopTest, "Fac_EctLoopTest" },
03371 
03372       { Fac_ChargingRequest, "Fac_ChargingRequest" },
03373       { Fac_AOCSCurrency, "Fac_AOCSCurrency" },
03374       { Fac_AOCSSpecialArr, "Fac_AOCSSpecialArr" },
03375       { Fac_AOCDCurrency, "Fac_AOCDCurrency" },
03376       { Fac_AOCDChargingUnit, "Fac_AOCDChargingUnit" },
03377       { Fac_AOCECurrency, "Fac_AOCECurrency" },
03378       { Fac_AOCEChargingUnit, "Fac_AOCEChargingUnit" },
03379 
03380       { Fac_StatusRequest, "Fac_StatusRequest" },
03381 
03382       { Fac_CallInfoRetain, "Fac_CallInfoRetain" },
03383       { Fac_EraseCallLinkageID, "Fac_EraseCallLinkageID" },
03384       { Fac_CCBSDeactivate, "Fac_CCBSDeactivate" },
03385       { Fac_CCBSErase, "Fac_CCBSErase" },
03386       { Fac_CCBSRemoteUserFree, "Fac_CCBSRemoteUserFree" },
03387       { Fac_CCBSCall, "Fac_CCBSCall" },
03388       { Fac_CCBSStatusRequest, "Fac_CCBSStatusRequest" },
03389       { Fac_CCBSBFree, "Fac_CCBSBFree" },
03390       { Fac_CCBSStopAlerting, "Fac_CCBSStopAlerting" },
03391 
03392       { Fac_CCBSRequest, "Fac_CCBSRequest" },
03393       { Fac_CCBSInterrogate, "Fac_CCBSInterrogate" },
03394 
03395       { Fac_CCNRRequest, "Fac_CCNRRequest" },
03396       { Fac_CCNRInterrogate, "Fac_CCNRInterrogate" },
03397 
03398       { Fac_CCBS_T_Call, "Fac_CCBS_T_Call" },
03399       { Fac_CCBS_T_Suspend, "Fac_CCBS_T_Suspend" },
03400       { Fac_CCBS_T_Resume, "Fac_CCBS_T_Resume" },
03401       { Fac_CCBS_T_RemoteUserFree, "Fac_CCBS_T_RemoteUserFree" },
03402       { Fac_CCBS_T_Available, "Fac_CCBS_T_Available" },
03403 
03404       { Fac_CCBS_T_Request, "Fac_CCBS_T_Request" },
03405 
03406       { Fac_CCNR_T_Request, "Fac_CCNR_T_Request" },
03407 
03408 #else
03409 
03410       { Fac_CFActivate, "Fac_CFActivate" },
03411       { Fac_CFDeactivate, "Fac_CFDeactivate" },
03412       { Fac_CD, "Fac_CD" },
03413 
03414       { Fac_AOCDCurrency, "Fac_AOCDCurrency" },
03415       { Fac_AOCDChargingUnit, "Fac_AOCDChargingUnit" },
03416 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
03417 /* *INDENT-ON* */
03418    };
03419 
03420    unsigned index;
03421 
03422    for (index = 0; index < ARRAY_LEN(arr); ++index) {
03423       if (arr[index].facility == facility) {
03424          return arr[index].name;
03425       }
03426    }
03427 
03428    return "unknown";
03429 }
03430 
03431 void misdn_lib_log_ies(struct misdn_bchannel *bc)
03432 {
03433    struct misdn_stack *stack;
03434 
03435    if (!bc) return;
03436 
03437    stack = get_stack_by_bc(bc);
03438 
03439    if (!stack) return;
03440 
03441    cb_log(2, stack->port,
03442       " --> channel:%d mode:%s cause:%d ocause:%d\n",
03443       bc->channel,
03444       stack->nt ? "NT" : "TE",
03445       bc->cause,
03446       bc->out_cause);
03447 
03448    cb_log(2, stack->port,
03449       " --> info_dad:%s dialed numtype:%d plan:%d\n",
03450       bc->info_dad,
03451       bc->dialed.number_type,
03452       bc->dialed.number_plan);
03453 
03454    cb_log(2, stack->port,
03455       " --> caller:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
03456       bc->caller.name,
03457       bc->caller.number,
03458       bc->caller.number_type,
03459       bc->caller.number_plan,
03460       bc->caller.presentation,
03461       bc->caller.screening);
03462 
03463    cb_log(2, stack->port,
03464       " --> redirecting-from:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
03465       bc->redirecting.from.name,
03466       bc->redirecting.from.number,
03467       bc->redirecting.from.number_type,
03468       bc->redirecting.from.number_plan,
03469       bc->redirecting.from.presentation,
03470       bc->redirecting.from.screening);
03471    cb_log(2, stack->port,
03472       " --> redirecting-to:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
03473       bc->redirecting.to.name,
03474       bc->redirecting.to.number,
03475       bc->redirecting.to.number_type,
03476       bc->redirecting.to.number_plan,
03477       bc->redirecting.to.presentation,
03478       bc->redirecting.to.screening);
03479    cb_log(2, stack->port,
03480       " --> redirecting reason:%d count:%d\n",
03481       bc->redirecting.reason,
03482       bc->redirecting.count);
03483 
03484    cb_log(2, stack->port,
03485       " --> connected:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
03486       bc->connected.name,
03487       bc->connected.number,
03488       bc->connected.number_type,
03489       bc->connected.number_plan,
03490       bc->connected.presentation,
03491       bc->connected.screening);
03492 
03493    cb_log(3, stack->port, " --> caps:%s pi:%x keypad:%s sending_complete:%d\n", bearer2str(bc->capability),bc->progress_indicator, bc->keypad, bc->sending_complete);
03494 
03495    cb_log(4, stack->port, " --> set_pres:%d pres:%d\n", bc->set_presentation, bc->presentation);
03496 
03497    cb_log(4, stack->port, " --> addr:%x l3id:%x b_stid:%x layer_id:%x\n", bc->addr, bc->l3_id, bc->b_stid, bc->layer_id);
03498 
03499    cb_log(4, stack->port, " --> facility in:%s out:%s\n", fac2str(bc->fac_in.Function), fac2str(bc->fac_out.Function));
03500 
03501    cb_log(5, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
03502 
03503    cb_log(5, stack->port, " --> bc:%p h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
03504 }
03505 
03506 
03507 #define RETURN(a,b) {retval=a; goto b;}
03508 
03509 static void misdn_send_lock(struct misdn_bchannel *bc)
03510 {
03511    //cb_log(0,bc->port,"Locking bc->pid:%d\n", bc->pid);
03512    if (bc->send_lock)
03513       pthread_mutex_lock(&bc->send_lock->lock);
03514 }
03515 
03516 static void misdn_send_unlock(struct misdn_bchannel *bc)
03517 {
03518    //cb_log(0,bc->port,"UnLocking bc->pid:%d\n", bc->pid);
03519    if (bc->send_lock)
03520       pthread_mutex_unlock(&bc->send_lock->lock);
03521 }
03522 
03523 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
03524 {
03525    msg_t *msg;
03526    struct misdn_bchannel *bc2;
03527    struct misdn_bchannel *held_bc;
03528    struct misdn_stack *stack;
03529    int retval = 0;
03530    int channel;
03531    int tmpcause;
03532    int tmp_out_cause;
03533 
03534    if (!bc)
03535       RETURN(-1,OUT_POST_UNLOCK);
03536 
03537    stack = get_stack_by_bc(bc);
03538    if (!stack) {
03539       cb_log(0,bc->port,
03540          "SENDEVENT: no Stack for event:%s caller:\"%s\" <%s> dialed:%s \n",
03541          isdn_get_info(msgs_g, event, 0),
03542          bc->caller.name,
03543          bc->caller.number,
03544          bc->dialed.number);
03545       RETURN(-1,OUT);
03546    }
03547 
03548    misdn_send_lock(bc);
03549 
03550 
03551    cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->upperid:%x\n",stack->nt, stack->upper_id);
03552 
03553    if ( stack->nt && !stack->l1link) {
03554       misdn_lib_get_l1_up(stack);
03555    }
03556 
03557    cb_log(1, stack->port,
03558       "I SEND:%s caller:\"%s\" <%s> dialed:%s pid:%d\n",
03559       isdn_get_info(msgs_g, event, 0),
03560       bc->caller.name,
03561       bc->caller.number,
03562       bc->dialed.number,
03563       bc->pid);
03564    cb_log(4, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state));
03565    misdn_lib_log_ies(bc);
03566 
03567    switch (event) {
03568    case EVENT_REGISTER:
03569    case EVENT_SETUP:
03570       if (create_process(glob_mgr->midev, bc) < 0) {
03571          cb_log(0, stack->port, " No free channel at the moment @ send_event\n");
03572 
03573          RETURN(-ENOCHAN,OUT);
03574       }
03575       break;
03576 
03577    case EVENT_PROGRESS:
03578    case EVENT_ALERTING:
03579    case EVENT_PROCEEDING:
03580    case EVENT_SETUP_ACKNOWLEDGE:
03581    case EVENT_CONNECT:
03582       if (!stack->nt) {
03583          if (stack->ptp) {
03584             setup_bc(bc);
03585          }
03586          break;
03587       }
03588       /* fall through */
03589 
03590    case EVENT_RETRIEVE_ACKNOWLEDGE:
03591       if (stack->nt) {
03592          if (bc->channel <=0 ) { /*  else we have the channel already */
03593             if (find_free_chan_in_stack(stack, bc, 0, 0)<0) {
03594                cb_log(0, stack->port, " No free channel at the moment\n");
03595                /*FIXME: add disconnect*/
03596                RETURN(-ENOCHAN,OUT);
03597             }
03598          }
03599          /* Its that i generate channels */
03600       }
03601 
03602       retval=setup_bc(bc);
03603       if (retval == -EINVAL) {
03604          cb_log(0,bc->port,"send_event: setup_bc failed\n");
03605       }
03606 
03607       if (misdn_cap_is_speech(bc->capability)) {
03608          if ((event==EVENT_CONNECT)||(event==EVENT_RETRIEVE_ACKNOWLEDGE)) {
03609             if ( *bc->crypt_key ) {
03610                cb_log(4, stack->port,
03611                   " --> ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
03612                   bc->channel,
03613                   bc->caller.number_type,
03614                   bc->caller.name,
03615                   bc->caller.number,
03616                   bc->dialed.number_type,
03617                   bc->dialed.number);
03618 
03619                manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
03620             }
03621 
03622             if (!bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
03623             manager_ec_enable(bc);
03624 
03625             if (bc->txgain != 0) {
03626                cb_log(4, stack->port,  "--> Changing txgain to %d\n", bc->txgain);
03627                manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
03628             }
03629 
03630             if ( bc->rxgain != 0 ) {
03631                cb_log(4, stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
03632                manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
03633             }
03634          }
03635       }
03636       break;
03637 
03638    case EVENT_HOLD_ACKNOWLEDGE:
03639       held_bc = malloc(sizeof(struct misdn_bchannel));
03640       if (!held_bc) {
03641          cb_log(0, bc->port, "Could not allocate held_bc!!!\n");
03642          RETURN(-1,OUT);
03643       }
03644 
03645       /* backup the bc and put it in storage */
03646       *held_bc = *bc;
03647       held_bc->holded = 1;
03648       held_bc->channel = 0;/* A held call does not have a channel anymore. */
03649       held_bc->channel_preselected = 0;
03650       held_bc->channel_found = 0;
03651       bc_state_change(held_bc, BCHAN_CLEANED);
03652       stack_holder_add(stack, held_bc);
03653 
03654       /* kill the bridge and clean the real b-channel record */
03655       if (stack->nt) {
03656          if (bc->bc_state == BCHAN_BRIDGED) {
03657             misdn_split_conf(bc,bc->conf_id);
03658             bc2 = find_bc_by_confid(bc->conf_id);
03659             if (!bc2) {
03660                cb_log(0,bc->port,"We have no second bc in bridge???\n");
03661             } else {
03662                misdn_split_conf(bc2,bc->conf_id);
03663             }
03664          }
03665 
03666          channel = bc->channel;
03667 
03668          empty_bc(bc);
03669          clean_up_bc(bc);
03670 
03671          if (channel>0)
03672             empty_chan_in_stack(stack,channel);
03673 
03674          bc->in_use=0;
03675       }
03676       break;
03677 
03678    /* finishing the channel eh ? */
03679    case EVENT_DISCONNECT:
03680       if (!bc->need_disconnect) {
03681          cb_log(0, bc->port, " --> we have already sent DISCONNECT\n");
03682          RETURN(-1,OUT);
03683       }
03684       /* IE cause is mandatory for DISCONNECT, but optional for the answers to DISCONNECT.
03685        * We must initialize cause, so it is later correctly indicated to ast_channel
03686        * in case the answer does not include one!
03687        */
03688       bc->cause = bc->out_cause;
03689 
03690       bc->need_disconnect=0;
03691       break;
03692    case EVENT_RELEASE:
03693       if (!bc->need_release) {
03694          cb_log(0, bc->port, " --> we have already sent RELEASE\n");
03695          RETURN(-1,OUT);
03696       }
03697       bc->need_disconnect=0;
03698       bc->need_release=0;
03699       break;
03700    case EVENT_RELEASE_COMPLETE:
03701       if (!bc->need_release_complete) {
03702          cb_log(0, bc->port, " --> we have already sent RELEASE_COMPLETE\n");
03703          RETURN(-1,OUT);
03704       }
03705       bc->need_disconnect=0;
03706       bc->need_release=0;
03707       bc->need_release_complete=0;
03708 
03709       if (!stack->nt) {
03710          /* create cleanup in TE */
03711          channel = bc->channel;
03712          tmpcause = bc->cause;
03713          tmp_out_cause = bc->out_cause;
03714 
03715          empty_bc(bc);
03716          bc->cause=tmpcause;
03717          bc->out_cause=tmp_out_cause;
03718          clean_up_bc(bc);
03719 
03720          if (channel>0)
03721             empty_chan_in_stack(stack,channel);
03722 
03723          bc->in_use=0;
03724       }
03725       break;
03726 
03727    case EVENT_CONNECT_ACKNOWLEDGE:
03728       if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
03729          int retval=setup_bc(bc);
03730          if (retval == -EINVAL){
03731             cb_log(0,bc->port,"send_event: setup_bc failed\n");
03732 
03733          }
03734       }
03735 
03736       if (misdn_cap_is_speech(bc->capability)) {
03737          if (  !bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
03738          manager_ec_enable(bc);
03739 
03740          if ( bc->txgain != 0 ) {
03741             cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
03742             manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
03743          }
03744          if ( bc->rxgain != 0 ) {
03745             cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
03746             manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
03747          }
03748       }
03749       break;
03750 
03751    default:
03752       break;
03753    }
03754 
03755    /* Later we should think about sending bchannel data directly to misdn. */
03756    msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
03757    if (!msg) {
03758       /*
03759        * The message was not built.
03760        *
03761        * NOTE:  The only time that the message will fail to build
03762        * is because the requested FACILITY message is not supported.
03763        * A failed malloc() results in exit() being called.
03764        */
03765       RETURN(-1, OUT);
03766    } else {
03767       msg_queue_tail(&stack->downqueue, msg);
03768       sem_post(&glob_mgr->new_msg);
03769    }
03770 
03771 OUT:
03772    misdn_send_unlock(bc);
03773 
03774 OUT_POST_UNLOCK:
03775    return retval;
03776 }
03777 
03778 
03779 static int handle_err(msg_t *msg)
03780 {
03781    iframe_t *frm = (iframe_t*) msg->data;
03782 
03783 
03784    if (!frm->addr) {
03785       static int cnt=0;
03786       if (!cnt)
03787          cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo);
03788       cnt++;
03789       if (cnt>100) {
03790          cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo);
03791          cnt=0;
03792       }
03793 
03794       free_msg(msg);
03795       return 1;
03796 
03797    }
03798 
03799    switch (frm->prim) {
03800       case MGR_SETSTACK|INDICATION:
03801          return handle_bchan(msg);
03802       break;
03803 
03804       case MGR_SETSTACK|CONFIRM:
03805       case MGR_CLEARSTACK|CONFIRM:
03806          free_msg(msg) ;
03807          return 1;
03808       break;
03809 
03810       case DL_DATA|CONFIRM:
03811          cb_log(4,0,"DL_DATA|CONFIRM\n");
03812          free_msg(msg);
03813          return 1;
03814 
03815       case PH_CONTROL|CONFIRM:
03816          cb_log(4,0,"PH_CONTROL|CONFIRM\n");
03817          free_msg(msg);
03818          return 1;
03819 
03820       case DL_DATA|INDICATION:
03821       {
03822          int port=(frm->addr&MASTER_ID_MASK) >> 8;
03823          int channel=(frm->addr&CHILD_ID_MASK) >> 16;
03824 
03825          /*we flush the read buffer here*/
03826 
03827          cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel);
03828 
03829          free_msg(msg);
03830          return 1;
03831       }
03832    }
03833 
03834    return 0;
03835 }
03836 
03837 int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
03838 {
03839 
03840    if (frm->dinfo==0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
03841       cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
03842    }
03843 
03844    /* Timer primitives must be handled first, because the frm->addr is a different
03845     * "address space" than the stack/instance address of other Lx primitives.
03846     */
03847    if (handle_timers(msg)) {
03848       return 0;
03849    }
03850 
03851    if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
03852       static int unhandled_bmsg_count=1000;
03853       if (handle_bchan(msg)) {
03854          return 0 ;
03855       }
03856 
03857       if (unhandled_bmsg_count==1000) {
03858          cb_log(0, 0, "received 1k Unhandled Bchannel Messages: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);
03859          unhandled_bmsg_count=0;
03860       }
03861 
03862       unhandled_bmsg_count++;
03863       free_msg(msg);
03864       return 0;
03865    }
03866 
03867 #ifdef RECV_FRM_SYSLOG_DEBUG
03868    syslog(LOG_NOTICE,"mISDN recv: ADDR:%x PRIM:%x DINFO:%x\n", frm->addr, frm->prim, frm->dinfo);
03869 #endif
03870 
03871    if (handle_mgmt(msg))
03872       return 0 ;
03873 
03874    if (handle_l2(msg))
03875       return 0 ;
03876 
03877    /* Its important to handle l1 AFTER l2  */
03878    if (handle_l1(msg))
03879       return 0 ;
03880 
03881    if (handle_frm_nt(msg)) {
03882       return 0;
03883    }
03884 
03885    if (handle_frm_te(msg)) {
03886       return 0;
03887    }
03888 
03889    if (handle_err(msg)) {
03890       return 0 ;
03891    }
03892 
03893    cb_log(0, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);
03894    free_msg(msg);
03895 
03896 
03897    return 0;
03898 }
03899 
03900 
03901 
03902 
03903 int misdn_lib_get_port_info(int port)
03904 {
03905    msg_t *msg=alloc_msg(MAX_MSG_SIZE);
03906    iframe_t *frm;
03907    struct misdn_stack *stack=find_stack_by_port(port);
03908    if (!msg) {
03909       cb_log(0, port, "misdn_lib_get_port_info: alloc_msg failed!\n");
03910       return -1;
03911    }
03912    frm=(iframe_t*)msg->data;
03913    if (!stack ) {
03914       cb_log(0, port, "There is no Stack for this port.\n");
03915       return -1;
03916    }
03917    /* activate bchannel */
03918    frm->prim = CC_STATUS_ENQUIRY | REQUEST;
03919 
03920    frm->addr = stack->upper_id| FLG_MSG_DOWN;
03921 
03922    frm->dinfo = 0;
03923    frm->len = 0;
03924 
03925    msg_queue_tail(&glob_mgr->activatequeue, msg);
03926    sem_post(&glob_mgr->new_msg);
03927 
03928 
03929    return 0;
03930 }
03931 
03932 int misdn_lib_pid_restart(int pid)
03933 {
03934    struct misdn_bchannel *bc=manager_find_bc_by_pid(pid);
03935 
03936    if (bc) {
03937       manager_clean_bc(bc);
03938    }
03939    return 0;
03940 }
03941 
03942 /*Sends Restart message for every bchannel*/
03943 int misdn_lib_send_restart(int port, int channel)
03944 {
03945    struct misdn_stack *stack=find_stack_by_port(port);
03946    struct misdn_bchannel dummybc;
03947    /*default is all channels*/
03948    cb_log(0, port, "Sending Restarts on this port.\n");
03949 
03950    misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0);
03951 
03952    /*default is all channels*/
03953    if (channel <0) {
03954       dummybc.channel=-1;
03955       cb_log(0, port, "Restarting and all Interfaces\n");
03956       misdn_lib_send_event(&dummybc, EVENT_RESTART);
03957 
03958       return 0;
03959    }
03960 
03961    /*if a channel is specified we restart only this one*/
03962    if (channel >0) {
03963       int cnt;
03964       dummybc.channel=channel;
03965       cb_log(0, port, "Restarting and cleaning channel %d\n",channel);
03966       misdn_lib_send_event(&dummybc, EVENT_RESTART);
03967       /* clean up chan in stack, to be sure we don't think it's
03968        * in use anymore */
03969       for (cnt=0; cnt<=stack->b_num; cnt++) {
03970          if (stack->bc[cnt].in_use && stack->bc[cnt].channel == channel) {
03971             empty_bc(&stack->bc[cnt]);
03972             clean_up_bc(&stack->bc[cnt]);
03973             stack->bc[cnt].in_use=0;
03974          }
03975       }
03976    }
03977 
03978    return 0;
03979 }
03980 
03981 /*reinitializes the L2/L3*/
03982 int misdn_lib_port_restart(int port)
03983 {
03984    struct misdn_stack *stack=find_stack_by_port(port);
03985 
03986    cb_log(0, port, "Restarting this port.\n");
03987    if (stack) {
03988       cb_log(0, port, "Stack:%p\n",stack);
03989 
03990       clear_l3(stack);
03991       {
03992          msg_t *msg=alloc_msg(MAX_MSG_SIZE);
03993          iframe_t *frm;
03994 
03995          if (!msg) {
03996             cb_log(0, port, "port_restart: alloc_msg failed\n");
03997             return -1;
03998          }
03999 
04000          frm=(iframe_t*)msg->data;
04001          /* we must activate if we are deactivated */
04002          /* activate bchannel */
04003          frm->prim = DL_RELEASE | REQUEST;
04004          frm->addr = stack->upper_id | FLG_MSG_DOWN;
04005 
04006          frm->dinfo = 0;
04007          frm->len = 0;
04008          msg_queue_tail(&glob_mgr->activatequeue, msg);
04009          sem_post(&glob_mgr->new_msg);
04010       }
04011 
04012       if (stack->nt)
04013          misdn_lib_reinit_nt_stack(stack->port);
04014 
04015    }
04016 
04017    return 0;
04018 }
04019 
04020 
04021 
04022 static sem_t handler_started;
04023 
04024 /* This is a thread */
04025 static void manager_event_handler(void *arg)
04026 {
04027    sem_post(&handler_started);
04028    while (1) {
04029       struct misdn_stack *stack;
04030       msg_t *msg;
04031 
04032       /** wait for events **/
04033       sem_wait(&glob_mgr->new_msg);
04034 
04035       for (msg=msg_dequeue(&glob_mgr->activatequeue);
04036            msg;
04037            msg=msg_dequeue(&glob_mgr->activatequeue)
04038          )
04039       {
04040 
04041          iframe_t *frm =  (iframe_t*) msg->data ;
04042 
04043          switch ( frm->prim) {
04044          case MGR_SETSTACK | REQUEST :
04045             free_msg(msg);
04046             break;
04047          default:
04048             mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
04049             free_msg(msg);
04050          }
04051       }
04052 
04053       for (stack=glob_mgr->stack_list;
04054            stack;
04055            stack=stack->next ) {
04056          /* Here we should check if we really want to
04057             send all the messages we've queued, lets
04058             assume we've queued a Disconnect, but
04059             received it already from the other side!*/
04060 
04061          while ( (msg=msg_dequeue(&stack->downqueue)) ) {
04062             if (stack->nt ) {
04063                pthread_mutex_lock(&stack->nstlock);
04064                if (stack->nst.manager_l3(&stack->nst, msg))
04065                   cb_log(0, stack->port, "Error@ Sending Message in NT-Stack.\n");
04066                pthread_mutex_unlock(&stack->nstlock);
04067             } else {
04068                iframe_t *frm = (iframe_t *)msg->data;
04069                struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo);
04070                if (bc)
04071                   send_msg(glob_mgr->midev, bc, msg);
04072                else  {
04073                   struct misdn_bchannel dummybc;
04074 
04075                   misdn_make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0);
04076                   if (frm->dinfo == MISDN_ID_GLOBAL || frm->dinfo == MISDN_ID_DUMMY ) {
04077                      cb_log(5,0," --> GLOBAL/DUMMY\n");
04078                   } else {
04079                      /*
04080                       * We need to be able to at least answer with RELEASE_COMPLETE
04081                       * on SETUP|INDICATION errors so use a dummy bc.
04082                       */
04083                      cb_log(0,0,"No bc for Message.  Using dummy_bc\n");
04084                   }
04085                   send_msg(glob_mgr->midev, &dummybc, msg);
04086                }
04087             }
04088          }
04089       }
04090    }
04091 }
04092 
04093 
04094 int misdn_lib_maxports_get(void)
04095 {
04096    /* BE AWARE WE HAVE NO cb_log() HERE! */
04097 
04098    int i = mISDN_open();
04099    int max=0;
04100 
04101    if (i<0)
04102       return -1;
04103 
04104    max = mISDN_get_stack_count(i);
04105 
04106    mISDN_close(i);
04107 
04108    return max;
04109 }
04110 
04111 
04112 void misdn_lib_nt_keepcalls( int kc)
04113 {
04114 #ifdef FEATURE_NET_KEEPCALLS
04115    if (kc) {
04116       struct misdn_stack *stack=get_misdn_stack();
04117       for ( ; stack; stack=stack->next) {
04118          stack->nst.feature |= FEATURE_NET_KEEPCALLS;
04119       }
04120    }
04121 #endif
04122 }
04123 
04124 void misdn_lib_nt_debug_init( int flags, char *file )
04125 {
04126    static int init=0;
04127    char *f;
04128 
04129    if (!flags)
04130       f=NULL;
04131    else
04132       f=file;
04133 
04134    if (!init) {
04135       debug_init( flags , f, f, f);
04136       init=1;
04137    } else {
04138       debug_close();
04139       debug_init( flags , f, f, f);
04140    }
04141 }
04142 
04143 int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_data)
04144 {
04145    struct misdn_lib *mgr;
04146    char *tok, *tokb;
04147    char plist[1024];
04148    int midev;
04149    int port_count=0;
04150 
04151    cb_log = iface->cb_log;
04152    cb_event = iface->cb_event;
04153    cb_jb_empty = iface->cb_jb_empty;
04154 
04155    if (!portlist || (*portlist == 0)) {
04156       return 1;
04157    }
04158 
04159    mgr = calloc(1, sizeof(*mgr));
04160    if (!mgr) {
04161       return 1;
04162    }
04163    glob_mgr = mgr;
04164 
04165    msg_init();
04166 
04167    misdn_lib_nt_debug_init(0,NULL);
04168 
04169    init_flip_bits();
04170 
04171    strncpy(plist, portlist, 1024);
04172    plist[1023] = 0;
04173 
04174    memcpy(tone_425_flip,tone_425,TONE_425_SIZE);
04175    flip_buf_bits(tone_425_flip,TONE_425_SIZE);
04176 
04177    memcpy(tone_silence_flip,tone_SILENCE,TONE_SILENCE_SIZE);
04178    flip_buf_bits(tone_silence_flip,TONE_SILENCE_SIZE);
04179 
04180    midev=te_lib_init();
04181    if (midev <= 0) {
04182       free(glob_mgr);
04183       glob_mgr = NULL;
04184       return 1;
04185    }
04186    mgr->midev=midev;
04187 
04188    port_count=mISDN_get_stack_count(midev);
04189 
04190    msg_queue_init(&mgr->activatequeue);
04191 
04192    if (sem_init(&mgr->new_msg, 1, 0)<0)
04193       sem_init(&mgr->new_msg, 0, 0);
04194 
04195    for (tok=strtok_r(plist," ,",&tokb );
04196         tok;
04197         tok=strtok_r(NULL," ,",&tokb)) {
04198       int port = atoi(tok);
04199       struct misdn_stack *stack;
04200       struct misdn_stack *help;
04201       int ptp=0;
04202       int i;
04203       int r;
04204 
04205       if (strstr(tok, "ptp"))
04206          ptp=1;
04207 
04208       if (port > port_count) {
04209          cb_log(0, port, "Couldn't Initialize this port since we have only %d ports\n", port_count);
04210          continue;
04211       }
04212 
04213       stack = stack_init(midev, port, ptp);
04214       if (!stack) {
04215          cb_log(0, port, "stack_init() failed for this port\n");
04216          continue;
04217       }
04218 
04219       /* Initialize the B channel records for real B channels. */
04220       for (i = 0; i <= stack->b_num; i++) {
04221          r = init_bc(stack, &stack->bc[i], stack->midev, port, i);
04222          if (r < 0) {
04223             cb_log(0, port, "Got Err @ init_bc :%d\n", r);
04224             break;
04225          }
04226       }
04227       if (i <= stack->b_num) {
04228          stack_destroy(stack);
04229          free(stack);
04230          continue;
04231       }
04232 #if defined(AST_MISDN_ENHANCEMENTS)
04233       /* Initialize the B channel records for REGISTER signaling links. */
04234       for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
04235          r = init_bc(stack, &stack->bc[i], stack->midev, port, i);
04236          if (r < 0) {
04237             cb_log(0, port, "Got Err @ init_bc :%d\n", r);
04238             break;
04239          }
04240          stack->bc[i].is_register_pool = 1;
04241       }
04242       if (i < ARRAY_LEN(stack->bc)) {
04243          stack_destroy(stack);
04244          free(stack);
04245          continue;
04246       }
04247 #endif   /* defined(AST_MISDN_ENHANCEMENTS) */
04248 
04249       /* Add the new stack to the end of the list */
04250       help = mgr->stack_list;
04251       if (!help) {
04252          mgr->stack_list = stack;
04253       } else {
04254          while (help->next) {
04255             help = help->next;
04256          }
04257          help->next = stack;
04258       }
04259    }
04260 
04261    if (!mgr->stack_list) {
04262       /* no stacks were successfully initialized !? */
04263       te_lib_destroy(midev);
04264       free(glob_mgr);
04265       glob_mgr = NULL;
04266       return 1;
04267    }
04268    if (sem_init(&handler_started, 1, 0)<0)
04269       sem_init(&handler_started, 0, 0);
04270 
04271    cb_log(8, 0, "Starting Event Handler\n");
04272    pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr);
04273 
04274    sem_wait(&handler_started) ;
04275    cb_log(8, 0, "Starting Event Catcher\n");
04276    pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr);
04277 
04278    cb_log(8, 0, "Event Catcher started\n");
04279 
04280    global_state= MISDN_INITIALIZED;
04281 
04282    return (mgr == NULL);
04283 }
04284 
04285 void misdn_lib_destroy(void)
04286 {
04287    struct misdn_stack *help;
04288    int i;
04289 
04290    for ( help=glob_mgr->stack_list; help; help=help->next ) {
04291       for(i=0;i<=help->b_num; i++) {
04292          char buf[1024];
04293          mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
04294          help->bc[i].addr = 0;
04295       }
04296       cb_log (1, help->port, "Destroying this port.\n");
04297       stack_destroy(help);
04298    }
04299 
04300    if (global_state == MISDN_INITIALIZED) {
04301       cb_log(4, 0, "Killing Handler Thread\n");
04302       if ( pthread_cancel(glob_mgr->event_handler_thread) == 0 ) {
04303          cb_log(4, 0, "Joining Handler Thread\n");
04304          pthread_join(glob_mgr->event_handler_thread, NULL);
04305       }
04306 
04307       cb_log(4, 0, "Killing Main Thread\n");
04308       if ( pthread_cancel(glob_mgr->event_thread) == 0 ) {
04309          cb_log(4, 0, "Joining Main Thread\n");
04310          pthread_join(glob_mgr->event_thread, NULL);
04311       }
04312    }
04313 
04314    cb_log(1, 0, "Closing mISDN device\n");
04315    te_lib_destroy(glob_mgr->midev);
04316    while ((help = glob_mgr->stack_list)) {
04317       glob_mgr->stack_list = help->next;
04318       free(help);
04319    }
04320    free(glob_mgr);
04321    glob_mgr = NULL;
04322 }
04323 
04324 char *manager_isdn_get_info(enum event_e event)
04325 {
04326    return isdn_get_info(msgs_g , event, 0);
04327 }
04328 
04329 void manager_bchannel_activate(struct misdn_bchannel *bc)
04330 {
04331    char buf[128];
04332 
04333    struct misdn_stack *stack=get_stack_by_bc(bc);
04334 
04335    if (!stack) {
04336       cb_log(0, bc->port, "bchannel_activate: Stack not found !");
04337       return ;
04338    }
04339 
04340    /* we must activate if we are deactivated */
04341    clear_ibuffer(bc->astbuf);
04342 
04343    cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
04344 
04345    mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN,  DL_ESTABLISH | REQUEST, 0,0, NULL, TIMEOUT_1SEC);
04346 
04347    return ;
04348 }
04349 
04350 
04351 void manager_bchannel_deactivate(struct misdn_bchannel * bc)
04352 {
04353    struct misdn_stack *stack=get_stack_by_bc(bc);
04354    char buf[128];
04355 
04356    switch (bc->bc_state) {
04357       case BCHAN_ACTIVATED:
04358          break;
04359       case BCHAN_BRIDGED:
04360          misdn_split_conf(bc,bc->conf_id);
04361          break;
04362       default:
04363          cb_log( 4, bc->port,"bchan_deactivate: called but not activated\n");
04364          return ;
04365 
04366    }
04367 
04368    cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
04369 
04370    bc->generate_tone=0;
04371 
04372    mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN, DL_RELEASE|REQUEST,0,0,NULL, TIMEOUT_1SEC);
04373 
04374    clear_ibuffer(bc->astbuf);
04375 
04376    bc_state_change(bc,BCHAN_RELEASE);
04377 
04378    return;
04379 }
04380 
04381 
04382 int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
04383 {
04384    struct misdn_stack *stack=get_stack_by_bc(bc);
04385    char buf[4096 + mISDN_HEADER_LEN];
04386    iframe_t *frm = (iframe_t*)buf;
04387 
04388    switch (bc->bc_state) {
04389       case BCHAN_ACTIVATED:
04390       case BCHAN_BRIDGED:
04391          break;
04392       default:
04393          cb_log(3, bc->port, "BC not yet activated (state:%s)\n",bc_state2str(bc->bc_state));
04394          return -1;
04395    }
04396 
04397    frm->prim = DL_DATA|REQUEST;
04398    frm->dinfo = 0;
04399    frm->addr = bc->addr | FLG_MSG_DOWN ;
04400 
04401    frm->len = len;
04402    memcpy(&buf[mISDN_HEADER_LEN], data,len);
04403 
04404    if ( misdn_cap_is_speech(bc->capability) )
04405       flip_buf_bits( &buf[mISDN_HEADER_LEN], len);
04406    else
04407       cb_log(6, stack->port, "Writing %d data bytes\n",len);
04408 
04409    cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len);
04410    mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
04411    return 0;
04412 }
04413 
04414 
04415 
04416 /*
04417  * send control information to the channel (dsp-module)
04418  */
04419 void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2)
04420 {
04421    unsigned char buffer[mISDN_HEADER_LEN+2*sizeof(int)];
04422    iframe_t *ctrl = (iframe_t *)buffer; /* preload data */
04423    unsigned int *d = (unsigned int*)&ctrl->data.p;
04424    /*struct misdn_stack *stack=get_stack_by_bc(bc);*/
04425 
04426    cb_log(4,bc->port,"ph_control: c1:%x c2:%x\n",c1,c2);
04427 
04428    ctrl->prim = PH_CONTROL | REQUEST;
04429    ctrl->addr = bc->addr | FLG_MSG_DOWN;
04430    ctrl->dinfo = 0;
04431    ctrl->len = sizeof(unsigned int)*2;
04432    *d++ = c1;
04433    *d++ = c2;
04434    mISDN_write(glob_mgr->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
04435 }
04436 
04437 /*
04438  * allow live control of channel parameters
04439  */
04440 void isdn_lib_update_rxgain (struct misdn_bchannel *bc)
04441 {
04442    manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
04443 }
04444 
04445 void isdn_lib_update_txgain (struct misdn_bchannel *bc)
04446 {
04447    manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
04448 }
04449 
04450 void isdn_lib_update_ec (struct misdn_bchannel *bc)
04451 {
04452 #ifdef MISDN_1_2
04453    if (*bc->pipeline)
04454 #else
04455    if (bc->ec_enable)
04456 #endif
04457       manager_ec_enable(bc);
04458    else
04459       manager_ec_disable(bc);
04460 }
04461 
04462 void isdn_lib_stop_dtmf (struct misdn_bchannel *bc)
04463 {
04464    manager_ph_control(bc, DTMF_TONE_STOP, 0);
04465 }
04466 
04467 /*
04468  * send control information to the channel (dsp-module)
04469  */
04470 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len)
04471 {
04472    unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
04473    iframe_t *ctrl = (iframe_t *)buffer;
04474    unsigned int *d = (unsigned int *)&ctrl->data.p;
04475    /*struct misdn_stack *stack=get_stack_by_bc(bc);*/
04476 
04477    ctrl->prim = PH_CONTROL | REQUEST;
04478    ctrl->addr = bc->addr | FLG_MSG_DOWN;
04479    ctrl->dinfo = 0;
04480    ctrl->len = sizeof(unsigned int) + c2_len;
04481    *d++ = c1;
04482    memcpy(d, c2, c2_len);
04483    mISDN_write(glob_mgr->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
04484 }
04485 
04486 
04487 
04488 
04489 void manager_clean_bc(struct misdn_bchannel *bc )
04490 {
04491    struct misdn_stack *stack=get_stack_by_bc(bc);
04492 
04493    if (stack && bc->channel > 0) {
04494       empty_chan_in_stack(stack, bc->channel);
04495    }
04496    empty_bc(bc);
04497    bc->in_use=0;
04498 
04499    cb_event(EVENT_CLEANUP, bc, NULL);
04500 }
04501 
04502 
04503 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder)
04504 {
04505    struct misdn_bchannel *help;
04506    cb_log(4,stack->port, "*HOLDER: add %x\n",holder->l3_id);
04507 
04508    holder->stack_holder=1;
04509    holder->next=NULL;
04510 
04511    if (!stack->holding) {
04512       stack->holding = holder;
04513       return;
04514    }
04515 
04516    for (help=stack->holding;
04517         help;
04518         help=help->next) {
04519       if (!help->next) {
04520          help->next=holder;
04521          break;
04522       }
04523    }
04524 
04525 }
04526 
04527 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder)
04528 {
04529    struct misdn_bchannel *h1;
04530 
04531    if (!holder->stack_holder) return;
04532 
04533    holder->stack_holder=0;
04534 
04535    cb_log(4,stack->port, "*HOLDER: remove %x\n",holder->l3_id);
04536    if (!stack || ! stack->holding) return;
04537 
04538    if (holder == stack->holding) {
04539       stack->holding = stack->holding->next;
04540       return;
04541    }
04542 
04543    for (h1=stack->holding;
04544         h1;
04545         h1=h1->next) {
04546       if (h1->next == holder) {
04547          h1->next=h1->next->next;
04548          return ;
04549       }
04550    }
04551 }
04552 
04553 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id)
04554 {
04555    struct misdn_bchannel *help;
04556 
04557    cb_log(4, stack->port, "*HOLDER: find %lx\n",l3id);
04558 
04559    for (help=stack->holding;
04560         help;
04561         help=help->next) {
04562       if (help->l3_id == l3id) {
04563          cb_log(4,stack->port, "*HOLDER: found bc\n");
04564          return help;
04565       }
04566    }
04567 
04568    cb_log(4,stack->port, "*HOLDER: find nothing\n");
04569    return NULL;
04570 }
04571 
04572 /*!
04573  * \brief Find a held call's B channel record.
04574  *
04575  * \param port Port the call is on.
04576  * \param l3_id mISDN Layer 3 ID of held call.
04577  *
04578  * \return Found bc-record or NULL.
04579  */
04580 struct misdn_bchannel *misdn_lib_find_held_bc(int port, int l3_id)
04581 {
04582    struct misdn_bchannel *bc;
04583    struct misdn_stack *stack;
04584 
04585    bc = NULL;
04586    for (stack = get_misdn_stack(); stack; stack = stack->next) {
04587       if (stack->port == port) {
04588          bc = stack_holder_find(stack, l3_id);
04589          break;
04590       }
04591    }
04592 
04593    return bc;
04594 }
04595 
04596 void misdn_lib_send_tone(struct misdn_bchannel *bc, enum tone_e tone)
04597 {
04598    char buf[mISDN_HEADER_LEN + 128] = "";
04599    iframe_t *frm = (iframe_t*)buf;
04600 
04601    switch(tone) {
04602    case TONE_DIAL:
04603       manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_DIALTONE);
04604    break;
04605 
04606    case TONE_ALERTING:
04607       manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_RINGING);
04608    break;
04609 
04610    case TONE_HANGUP:
04611       manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_HANGUP);
04612    break;
04613 
04614    case TONE_NONE:
04615    default:
04616       manager_ph_control(bc, TONE_PATT_OFF, TONE_GERMAN_HANGUP);
04617    }
04618 
04619    frm->prim=DL_DATA|REQUEST;
04620    frm->addr=bc->addr|FLG_MSG_DOWN;
04621    frm->dinfo=0;
04622    frm->len=128;
04623 
04624    mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
04625 }
04626 
04627 
04628 void manager_ec_enable(struct misdn_bchannel *bc)
04629 {
04630    struct misdn_stack *stack=get_stack_by_bc(bc);
04631 
04632    cb_log(4, stack?stack->port:0,"ec_enable\n");
04633 
04634    if (!misdn_cap_is_speech(bc->capability)) {
04635       cb_log(1, stack?stack->port:0, " --> no speech? cannot enable EC\n");
04636    } else {
04637 
04638 #ifdef MISDN_1_2
04639    if (*bc->pipeline) {
04640       cb_log(3, stack?stack->port:0,"Sending Control PIPELINE_CFG %s\n",bc->pipeline);
04641       manager_ph_control_block(bc, PIPELINE_CFG, bc->pipeline, strlen(bc->pipeline) + 1);
04642    }
04643 #else
04644    int ec_arr[2];
04645 
04646    if (bc->ec_enable) {
04647       cb_log(3, stack?stack->port:0,"Sending Control ECHOCAN_ON taps:%d\n",bc->ec_deftaps);
04648 
04649       switch (bc->ec_deftaps) {
04650       case 4:
04651       case 8:
04652       case 16:
04653       case 32:
04654       case 64:
04655       case 128:
04656       case 256:
04657       case 512:
04658       case 1024:
04659          cb_log(4, stack->port, "Taps is %d\n",bc->ec_deftaps);
04660          break;
04661       default:
04662          cb_log(0, stack->port, "Taps should be power of 2\n");
04663          bc->ec_deftaps=128;
04664       }
04665 
04666       ec_arr[0]=bc->ec_deftaps;
04667       ec_arr[1]=0;
04668 
04669       manager_ph_control_block(bc,  ECHOCAN_ON,  ec_arr, sizeof(ec_arr));
04670    }
04671 #endif
04672    }
04673 }
04674 
04675 
04676 
04677 void manager_ec_disable(struct misdn_bchannel *bc)
04678 {
04679    struct misdn_stack *stack=get_stack_by_bc(bc);
04680 
04681    cb_log(4, stack?stack->port:0," --> ec_disable\n");
04682 
04683    if (!misdn_cap_is_speech(bc->capability)) {
04684       cb_log(1, stack?stack->port:0, " --> no speech? cannot disable EC\n");
04685       return;
04686    }
04687 
04688 #ifdef MISDN_1_2
04689    manager_ph_control_block(bc, PIPELINE_CFG, "", 0);
04690 #else
04691    if ( ! bc->ec_enable) {
04692       cb_log(3, stack?stack->port:0, "Sending Control ECHOCAN_OFF\n");
04693       manager_ph_control(bc,  ECHOCAN_OFF, 0);
04694    }
04695 #endif
04696 }
04697 
04698 struct misdn_stack *get_misdn_stack(void)
04699 {
04700    return glob_mgr->stack_list;
04701 }
04702 
04703 
04704 
04705 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id)
04706 {
04707    char data[16] = "";
04708 
04709    bc_state_change(bc,BCHAN_BRIDGED);
04710    manager_ph_control(bc, CMX_RECEIVE_OFF, 0);
04711    manager_ph_control(bc, CMX_CONF_JOIN, conf_id);
04712 
04713    cb_log(3,bc->port, "Joining bc:%x in conf:%d\n",bc->addr,conf_id);
04714 
04715    misdn_lib_tx2misdn_frm(bc, data, sizeof(data) - 1);
04716 }
04717 
04718 
04719 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id)
04720 {
04721    bc_state_change(bc,BCHAN_ACTIVATED);
04722    manager_ph_control(bc, CMX_RECEIVE_ON, 0);
04723    manager_ph_control(bc, CMX_CONF_SPLIT, conf_id);
04724 
04725    cb_log(4,bc->port, "Splitting bc:%x in conf:%d\n",bc->addr,conf_id);
04726 }
04727 
04728 void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
04729 {
04730    int conf_id = bc1->pid + 1;
04731    struct misdn_bchannel *bc_list[] = { bc1, bc2, NULL };
04732    struct misdn_bchannel **bc;
04733 
04734    cb_log(4, bc1->port, "I Send: BRIDGE from:%d to:%d\n",bc1->port,bc2->port);
04735 
04736    for (bc=bc_list; *bc;  bc++) {
04737       (*bc)->conf_id=conf_id;
04738       cb_log(4, (*bc)->port, " --> bc_addr:%x\n",(*bc)->addr);
04739 
04740       switch((*bc)->bc_state) {
04741          case BCHAN_ACTIVATED:
04742             misdn_join_conf(*bc,conf_id);
04743             break;
04744          default:
04745             bc_next_state_change(*bc,BCHAN_BRIDGED);
04746             break;
04747       }
04748    }
04749 }
04750 
04751 void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
04752 {
04753 
04754    struct misdn_bchannel *bc_list[]={
04755       bc1,bc2,NULL
04756    };
04757    struct misdn_bchannel **bc;
04758 
04759    for (bc=bc_list; *bc;  bc++) {
04760       if ( (*bc)->bc_state == BCHAN_BRIDGED){
04761          misdn_split_conf( *bc, (*bc)->conf_id);
04762       } else {
04763          cb_log( 2, (*bc)->port, "BC not bridged (state:%s) so not splitting it\n",bc_state2str((*bc)->bc_state));
04764       }
04765    }
04766 
04767 }
04768 
04769 
04770 
04771 void misdn_lib_echo(struct misdn_bchannel *bc, int onoff)
04772 {
04773    cb_log(3,bc->port, " --> ECHO %s\n", onoff?"ON":"OFF");
04774    manager_ph_control(bc, onoff?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
04775 }
04776 
04777 
04778 
04779 void misdn_lib_reinit_nt_stack(int port)
04780 {
04781    struct misdn_stack *stack=find_stack_by_port(port);
04782 
04783    if (stack) {
04784       stack->l2link=0;
04785       stack->blocked=0;
04786 
04787       cleanup_Isdnl3(&stack->nst);
04788       cleanup_Isdnl2(&stack->nst);
04789 
04790 
04791       memset(&stack->nst, 0, sizeof(net_stack_t));
04792       memset(&stack->mgr, 0, sizeof(manager_t));
04793 
04794       stack->mgr.nst = &stack->nst;
04795       stack->nst.manager = &stack->mgr;
04796 
04797       stack->nst.l3_manager = handle_event_nt;
04798       stack->nst.device = glob_mgr->midev;
04799       stack->nst.cardnr = port;
04800       stack->nst.d_stid = stack->d_stid;
04801 
04802       stack->nst.feature = FEATURE_NET_HOLD;
04803       if (stack->ptp)
04804          stack->nst.feature |= FEATURE_NET_PTP;
04805       if (stack->pri)
04806          stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
04807 
04808       stack->nst.l1_id = stack->lower_id; /* never used */
04809       stack->nst.l2_id = stack->upper_id;
04810 
04811       msg_queue_init(&stack->nst.down_queue);
04812 
04813       Isdnl2Init(&stack->nst);
04814       Isdnl3Init(&stack->nst);
04815 
04816       if (!stack->ptp)
04817          misdn_lib_get_l1_up(stack);
04818    }
04819 }
04820 
04821 

Generated on Thu Apr 16 06:27:37 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6