1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN port abstraction for dss1 and sip **
10 \*****************************************************************************/
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
28 #define ISDN_PID_L2_B_USER 0x420000ff
29 #define ISDN_PID_L3_B_USER 0x430000ff
30 #define ISDN_PID_L4_B_USER 0x440000ff
32 /* used for udevice */
35 /* noise randomizer */
36 unsigned char mISDN_rand[256];
37 int mISDN_rand_count = 0;
39 /* the device handler and port list */
42 /* list of mISDN ports */
43 struct mISDNport *mISDNport_first;
48 PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive) : Port(type, portname, settings)
50 p_m_mISDNport = mISDNport;
51 p_m_portnum = mISDNport->portnum;
61 p_m_txvol = p_m_rxvol = 0;
63 #warning set delay by routing parameter or interface config
70 p_m_nodata = 1; /* may be 1, because call always notifies us */
71 p_m_dtmf = !options.nodtmf;
74 #warning denke auch an die andere seite. also das setup sollte dies weitertragen
78 p_m_crypt_msg_loops = 0;
79 p_m_crypt_msg_loops = 0;
80 p_m_crypt_msg_len = 0;
81 p_m_crypt_msg[0] = '\0';
82 p_m_crypt_msg_current = 0;
83 p_m_crypt_key[0] = '\0';
84 p_m_crypt_key_len = 0;
86 p_m_crypt_listen_state = 0;
87 p_m_crypt_listen_len = 0;
88 p_m_crypt_listen_msg[0] = '\0';
89 p_m_crypt_listen_crc = 0;
91 /* if any channel requested by constructor */
92 if (channel == CHANNEL_ANY)
96 mISDNport->b_reserved++;
100 /* reserve channel */
101 if (channel) // only if constructor was called with a channel resevation
102 seize_bchannel(channel, exclusive);
104 /* we increase the number of objects: */
106 PDEBUG(DEBUG_ISDN, "Created new mISDNPort(%s). Currently %d objects use, port #%d\n", portname, mISDNport->use, p_m_portnum);
115 struct message *message;
117 /* remove bchannel relation */
123 PDEBUG(DEBUG_ISDN, "destroy mISDNPort(%s). endpoint still exists, releaseing.\n", p_name);
124 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
125 message->param.disconnectinfo.cause = 16;
126 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
127 message_put(message);
128 /* remove from list */
129 free_epointlist(p_epointlist);
132 /* we decrease the number of objects: */
133 p_m_mISDNport->use--;
134 PDEBUG(DEBUG_ISDN, "destroyed mISDNPort(%s). Currently %d objects\n", p_name, p_m_mISDNport->use);
139 * send control information to the channel (dsp-module)
141 void ph_control(unsigned long b_addr, int c1, int c2)
143 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
144 iframe_t *ctrl = (iframe_t *)buffer;
145 unsigned long *d = (unsigned long *)&ctrl->data.p;
147 ctrl->prim = PH_CONTROL | REQUEST;
148 ctrl->addr = b_addr | FLG_MSG_DOWN;
150 ctrl->len = sizeof(unsigned long)*2;
153 mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
156 void ph_control_block(unsigned long b_addr, int c1, void *c2, int c2_len)
158 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
159 iframe_t *ctrl = (iframe_t *)buffer;
160 unsigned long *d = (unsigned long *)&ctrl->data.p;
162 ctrl->prim = PH_CONTROL | REQUEST;
163 ctrl->addr = b_addr | FLG_MSG_DOWN;
165 ctrl->len = sizeof(unsigned long)*2;
167 memcpy(d, c2, c2_len);
168 mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
173 * activate / deactivate bchannel
175 static void bchannel_activate(struct mISDNport *mISDNport, int i)
179 /* we must activate if we are deactivated */
180 if (mISDNport->b_state[i] == B_STATE_IDLE)
182 /* activate bchannel */
183 PDEBUG(DEBUG_BCHANNEL, "activating bchannel (index %d), because currently idle (address 0x%x).\n", i, mISDNport->b_addr[i]);
184 act.prim = DL_ESTABLISH | REQUEST;
185 act.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
188 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
189 mISDNport->b_state[i] = B_STATE_ACTIVATING;
193 /* if we are active, we configure our channel */
194 if (mISDNport->b_state[i] == B_STATE_ACTIVE)
196 unsigned char buffer[mISDN_HEADER_LEN+ISDN_PRELOAD];
197 iframe_t *pre = (iframe_t *)buffer; /* preload data */
198 unsigned char *p = (unsigned char *)&pre->data.p;
200 /* it is an error if this channel is not associated with a port object */
201 if (!mISDNport->b_port[i])
203 PERROR("bchannel index i=%d not associated with a port object\n", i);
207 /* configure dsp features */
208 if (mISDNport->b_port[i]->p_m_delay)
210 PDEBUG(DEBUG_BCHANNEL, "during activation, we set delay to delay=%d.\n", mISDNport->b_port[i]->p_m_delay);
211 ph_control(mISDNport->b_addr[i], CMX_DELAY, mISDNport->b_port[i]->p_m_delay);
213 if (mISDNport->b_port[i]->p_m_txvol)
215 PDEBUG(DEBUG_BCHANNEL, "during activation, we change tx-volume to shift=%d.\n", mISDNport->b_port[i]->p_m_txvol);
216 ph_control(mISDNport->b_addr[i], VOL_CHANGE_TX, mISDNport->b_port[i]->p_m_txvol);
218 if (mISDNport->b_port[i]->p_m_rxvol)
220 PDEBUG(DEBUG_BCHANNEL, "during activation, we change rx-volume to shift=%d.\n", mISDNport->b_port[i]->p_m_rxvol);
221 ph_control(mISDNport->b_addr[i], VOL_CHANGE_RX, mISDNport->b_port[i]->p_m_rxvol);
223 //tone if (mISDNport->b_port[i]->p_m_conf && !mISDNport->b_port[i]->p_m_tone)
224 if (mISDNport->b_port[i]->p_m_conf)
226 PDEBUG(DEBUG_BCHANNEL, "during activation, we change conference to conf=%d.\n", mISDNport->b_port[i]->p_m_conf);
227 ph_control(mISDNport->b_addr[i], CMX_CONF_JOIN, mISDNport->b_port[i]->p_m_conf);
229 if (mISDNport->b_port[i]->p_m_echo)
231 PDEBUG(DEBUG_BCHANNEL, "during activation, we set echo to echo=%d.\n", mISDNport->b_port[i]->p_m_echo);
232 ph_control(mISDNport->b_addr[i], CMX_ECHO_ON, 0);
234 if (mISDNport->b_port[i]->p_m_tone)
236 PDEBUG(DEBUG_BCHANNEL, "during activation, we set tone to tone=%d.\n", mISDNport->b_port[i]->p_m_tone);
237 ph_control(mISDNport->b_addr[i], TONE_PATT_ON, mISDNport->b_port[i]->p_m_tone);
239 if (mISDNport->b_port[i]->p_m_rxoff)
241 PDEBUG(DEBUG_BCHANNEL, "during activation, we set rxoff to rxoff=%d.\n", mISDNport->b_port[i]->p_m_rxoff);
242 ph_control(mISDNport->b_addr[i], CMX_RECEIVE_OFF, 0);
244 if (mISDNport->b_port[i]->p_m_txmix)
246 PDEBUG(DEBUG_BCHANNEL, "during activation, we set txmix to txmix=%d.\n", mISDNport->b_port[i]->p_m_txmix);
247 ph_control(mISDNport->b_addr[i], CMX_MIX_ON, 0);
249 if (mISDNport->b_port[i]->p_m_dtmf)
251 PDEBUG(DEBUG_BCHANNEL, "during activation, we set dtmf to dtmf=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
252 ph_control(mISDNport->b_addr[i], DTMF_TONE_START, 0);
254 if (mISDNport->b_port[i]->p_m_crypt)
256 PDEBUG(DEBUG_BCHANNEL, "during activation, we set crypt to crypt=%d.\n", mISDNport->b_port[i]->p_m_crypt);
257 ph_control_block(mISDNport->b_addr[i], BF_ENABLE_KEY, mISDNport->b_port[i]->p_m_crypt_key, mISDNport->b_port[i]->p_m_crypt_key_len);
260 /* preload tx-buffer */
261 pre->len = mISDNport->b_port[i]->read_audio(p, ISDN_PRELOAD, 1);
264 PDEBUG(DEBUG_BCHANNEL, "port is activated, we fill our buffer (ISDN_PRELOAD = %d).\n", pre->len);
271 pre->prim = DL_DATA | REQUEST;
272 pre->addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
274 mISDN_write(mISDNdevice, pre, mISDN_HEADER_LEN+pre->len, TIMEOUT_1SEC);
279 static void bchannel_deactivate(struct mISDNport *mISDNport, int i)
283 if (mISDNport->b_state[i] == B_STATE_ACTIVE)
285 /* reset dsp features */
286 if (mISDNport->b_port[i])
288 if (mISDNport->b_port[i]->p_m_delay)
290 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset delay from delay=%d.\n", mISDNport->b_port[i]->p_m_delay);
291 ph_control(mISDNport->b_addr[i], CMX_JITTER, 0);
293 if (mISDNport->b_port[i]->p_m_txvol)
295 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_txvol);
296 ph_control(mISDNport->b_addr[i], VOL_CHANGE_TX, 0);
298 if (mISDNport->b_port[i]->p_m_rxvol)
300 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_rxvol);
301 ph_control(mISDNport->b_addr[i], VOL_CHANGE_RX, 0);
303 if (mISDNport->b_port[i]->p_m_conf)
305 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we release conference from conf=%d.\n", mISDNport->b_port[i]->p_m_conf);
306 ph_control(mISDNport->b_addr[i], CMX_CONF_SPLIT, 0);
308 if (mISDNport->b_port[i]->p_m_echo)
310 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset echo from echo=%d.\n", mISDNport->b_port[i]->p_m_echo);
311 ph_control(mISDNport->b_addr[i], CMX_ECHO_OFF, 0);
313 if (mISDNport->b_port[i]->p_m_tone)
315 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tone from tone=%d.\n", mISDNport->b_port[i]->p_m_tone);
316 ph_control(mISDNport->b_addr[i], TONE_PATT_OFF, 0);
318 if (mISDNport->b_port[i]->p_m_rxoff)
320 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rxoff from rxoff=%d.\n", mISDNport->b_port[i]->p_m_rxoff);
321 ph_control(mISDNport->b_addr[i], CMX_RECEIVE_ON, 0);
323 if (mISDNport->b_port[i]->p_m_txmix)
325 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset txmix from txmix=%d.\n", mISDNport->b_port[i]->p_m_txmix);
326 ph_control(mISDNport->b_addr[i], CMX_MIX_OFF, 0);
328 if (mISDNport->b_port[i]->p_m_dtmf)
330 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset dtmf from dtmf=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
331 ph_control(mISDNport->b_addr[i], DTMF_TONE_STOP, 0);
333 if (mISDNport->b_port[i]->p_m_crypt)
335 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset crypt from crypt=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
336 ph_control(mISDNport->b_addr[i], BF_DISABLE, 0);
339 /* deactivate bchannel */
340 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d), because currently active.\n", i);
341 dact.prim = DL_RELEASE | REQUEST;
342 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
345 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
346 mISDNport->b_state[i] = B_STATE_DEACTIVATING;
353 * check for available channel and reserve+set it.
354 * give channel number or SEL_CHANNEL_ANY or SEL_CHANNEL_NO
356 * returns -(cause value) or x = channel x or 0 = no channel
358 denke ans aktivieren und deaktivieren
359 int PmISDN::seize_bchannel(int channel, int exclusive)
363 /* the channel is what we have */
364 if (p_m_b_channel == channel)
367 /* if channel already in use, release it */
372 if (channel==CHANNEL_NO || channel==0)
375 /* is channel in range ? */
377 || (channel>p_m_mISDNport->b_num && channel<16)
378 || ((channel-1)>p_m_mISDNport->b_num && channel>16)) /* channel-1 because channel 16 is not counted */
379 return(-6); /* channel unacceptable */
381 /* request exclusive channel */
382 if (exclusive && channel>0)
384 i = channel-1-(channel>16);
385 if (p_m_mISDNport->b_port[i])
386 return(-44); /* requested channel not available */
390 /* ask for channel */
393 i = channel-1-(channel>16);
394 if (p_m_mISDNport->b_port[i] == NULL)
398 /* search for channel */
400 while(i < p_m_mISDNport->b_num)
402 if (!p_m_mISDNport->b_port[i])
404 channel = i+1+(i>=15);
409 return(-34); /* no free channel */
413 p_m_mISDNport->b_port[i] = this;
415 p_m_b_channel = channel;
416 p_m_b_exclusive = exclusive;
417 p_m_b_stid = p_m_mISDNport->b_stid[i];
418 p_m_b_addr = p_m_mISDNport->b_addr[i];
421 /* reserve channel */
422 if (p_m_b_reserve == 0) // already reserved
425 mISDNport->b_reserved++;
428 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) seizing bchannel %d (index %d)\n", p_name, channel, i);
434 * drop reserved channel and unset it.
436 void PmISDN::drop_bchannel(void)
438 /* unreserve channel */
440 p_m_mISDNport->b_reserved--;
447 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) dropping bchannel\n", p_name);
449 p_m_mISDNport->b_port[p_m_b_index] = NULL;
460 int PmISDN::handler(void)
462 struct message *message;
464 // NOTE: deletion is done by the child class
466 /* handle timeouts */
469 if (p_m_timer+p_m_timeout < now_d)
471 PDEBUG(DEBUG_ISDN, "(%s) timeout after %d seconds detected (state=%d).\n", p_name, p_m_timeout, p_state);
473 /* send timeout to endpoint */
474 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TIMEOUT);
475 message->param.state = p_state;
476 message_put(message);
481 return(0); /* nothing done */
486 * whenever we get audio data from bchannel, we process it here
488 void PmISDN::bchannel_receive(iframe_t *frm)
490 unsigned char *data_temp;
491 unsigned long length_temp;
492 struct message *message;
495 long long jitter_now;
500 // iframe_t rsp; /* response to possible indication */
502 #warning BCHANNEL-DEBUG
504 // check if we are part of all ports */
505 class Port *port = port_first;
514 PERROR_RUNTIME("**************************************************\n");
515 PERROR_RUNTIME("*** BCHANNEL-DEBUG: !this! is not in list of ports\n");
516 PERROR_RUNTIME("**************************************************\n");
523 if (frm->prim == (PH_CONTROL | INDICATION))
525 cont = *((unsigned long *)&frm->data.p);
526 // PDEBUG(DEBUG_PORT, "PmISDN(%s) received a PH_CONTROL INDICATION 0x%x\n", p_name, cont);
527 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
529 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DTMF);
530 message->param.dtmf = cont & DTMF_TONE_MASK;
531 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL DTMF digit '%c'\n", p_name, message->param.dtmf);
532 message_put(message);
537 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
538 message->param.crypt.type = CC_ERROR_IND;
539 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL reject of blowfish.\n", p_name);
540 message_put(message);
544 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
545 message->param.crypt.type = CC_ACTBF_CONF;
546 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL accept of blowfish.\n", p_name);
547 message_put(message);
553 /* external incoming calls will not process any audio data unless
554 * the call is connected OR inbandpattern feature is enabled.
556 if (p_type==PORT_TYPE_DSS1_TE_IN
557 && p_state!=PORT_STATE_CONNECT
558 && !options.inbandpattern)
562 /* the bearer capability must be audio in order to send and receive
563 * audio prior or after connect.
565 if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
569 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
572 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
576 /* randomize and listen to crypt message if enabled */
577 if (p_m_crypt_listen)
579 /* the noisy randomizer */
580 p = (unsigned char *)&frm->data.p;
583 mISDN_rand[mISDN_rand_count & 0xff] += *p++;
585 cryptman_listen_bch((unsigned char *)&frm->data.p, frm->len);
589 gettimeofday(&tv, &tz);
590 jitter_now = tv.tv_sec;
591 jitter_now = jitter_now*8000 + tv.tv_usec/125;
592 if (p_m_jittercheck == 0)
594 p_m_jittercheck = jitter_now;
595 p_m_jitterdropped = 0;
597 p_m_jittercheck += frm->len;
598 if (p_m_jittercheck < jitter_now)
600 // PERROR("jitter: ignoring slow data\n");
601 p_m_jittercheck = jitter_now;
603 if (p_m_jittercheck-ISDN_JITTERLIMIT > jitter_now)
605 p_m_jitterdropped += frm->len;
606 p_m_jittercheck -= frm->len;
610 if (p_m_jitterdropped)
612 PERROR("jitter: dropping, caused by fast data: %lld\n", p_m_jitterdropped);
613 p_m_jitterdropped = 0;
616 p = (unsigned char *)&frm->data.p;
618 /* send data to epoint */
619 if (ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
621 //printf("we are port %s and sending to epoint %d\n", p_m_cardname, p_epoint->serial);
622 length_temp = frm->len;
626 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
627 message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
628 memcpy(message->param.data.data, data_temp, message->param.data.len);
629 message->param.data.compressed = 1;
630 message->param.data.port_id = p_serial;
631 message->param.data.port_type = p_type;
632 message_put(message);
633 if (length_temp <= sizeof(message->param.data.data))
635 data_temp += sizeof(message->param.data.data);
636 length_temp -= sizeof(message->param.data.data);
639 /* return the same number of tone data, as we recieved */
640 newlen = read_audio(p, frm->len, 1);
641 /* send tone data to isdn device only if we have data, otherwhise we send nothing */
642 if (newlen>0 && (p_tone_fh>=0 || p_tone_fetched || !p_m_nodata || p_m_crypt_msg_loops))
644 //printf("jolly: sending.... %d %d %d %d %d\n", newlen, p_tone_fh, p_tone_fetched, p_m_nodata, p_m_crypt_msg_loops);
647 p_m_txmix_on -= newlen;
648 if (p_m_txmix_on <= 0)
651 p_m_txmix = !p_m_nodata;
652 PDEBUG(DEBUG_BCHANNEL, "after sending message, we set txmix to txmix=%d.\n", p_m_txmix);
654 ph_control(p_m_b_addr, CMX_MIX_ON, 0);
657 if (p_m_crypt_msg_loops)
659 /* send pending message */
662 tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
665 memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
666 p_m_crypt_msg_current += tosend;
667 if (p_m_crypt_msg_current == p_m_crypt_msg_len)
669 p_m_crypt_msg_current = 0;
670 p_m_crypt_msg_loops--;
671 // we need to disable rxmix some time after sending the loops...
672 if (!p_m_crypt_msg_loops && p_m_txmix)
674 p_m_txmix_on = 8000; /* one sec */
678 frm->prim = frm->prim & 0xfffffffc | REQUEST;
679 frm->addr = p_m_b_addr | FLG_MSG_DOWN;
681 mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
683 if (p_debug_nothingtosend)
685 p_debug_nothingtosend = 0;
686 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
690 if (!p_debug_nothingtosend)
692 p_debug_nothingtosend = 1;
693 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
697 /* response to the data indication */
698 rsp.prim = frm->prim & 0xfffffffc | RESPONSE;
699 rsp.addr = frm->addr & INST_ID_MASK | FLG_MSG_DOWN;
700 rsp.dinfo = frm->dinfo;
702 mISDN_write(mISDNdevice, &rsp, mISDN_HEADER_LEN+rsp.len, TIMEOUT_1SEC);
703 //PDEBUG(DEBUG_ISDN, "written %d bytes.\n", length);
711 void PmISDN::set_echotest(int echo)
713 if (p_m_echo != echo)
716 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
718 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
719 ph_control(p_m_b_addr, p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
726 void PmISDN::set_tone(char *dir, char *tone)
732 PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
739 /* check if we NOT really have to use a dsp-tone */
740 if (!options.dsptones)
744 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
746 PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
747 ph_control(p_m_b_addr, TONE_PATT_OFF, 0);
750 Port::set_tone(dir, tone);
756 /* now we USE dsp-tone, convert name */
757 else if (!strcmp(tone, "dialtone"))
759 switch(options.dsptones) {
760 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
761 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
762 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
764 } else if (!strcmp(tone, "dialpbx"))
766 switch(options.dsptones) {
767 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
768 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
769 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
771 } else if (!strcmp(tone, "ringing"))
773 switch(options.dsptones) {
774 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
775 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
776 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
778 } else if (!strcmp(tone, "ringpbx"))
780 switch(options.dsptones) {
781 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
782 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
783 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
785 } else if (!strcmp(tone, "busy"))
788 switch(options.dsptones) {
789 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
790 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
791 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
793 } else if (!strcmp(tone, "release"))
796 switch(options.dsptones) {
797 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
798 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
799 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
801 } else if (!strcmp(tone, "cause_10"))
803 else if (!strcmp(tone, "cause_11"))
805 else if (!strcmp(tone, "cause_22"))
807 switch(options.dsptones) {
808 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
809 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
810 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
812 } else if (!strncmp(tone, "cause_", 6))
813 id = TONE_SPECIAL_INFO;
817 /* if we have a tone that is not supported by dsp */
818 if (id==TONE_OFF && tone[0])
826 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
828 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
829 ph_control(p_m_b_addr, p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone);
831 /* turn user-space tones off in cases of no tone OR dsp tone */
832 Port::set_tone("",NULL);
836 /* MESSAGE_mISDNSIGNAL */
837 //extern struct message *dddebug;
838 void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union parameter *param)
840 switch(param->mISDNsignal.message)
842 case mISDNSIGNAL_VOLUME:
843 if (p_m_txvol != param->mISDNsignal.txvol)
845 p_m_txvol = param->mISDNsignal.txvol;
846 PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
848 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
849 ph_control(p_m_b_addr, VOL_CHANGE_TX, p_m_txvol);
851 if (p_m_rxvol != param->mISDNsignal.rxvol)
853 p_m_rxvol = param->mISDNsignal.rxvol;
854 PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
856 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
857 ph_control(p_m_b_addr, VOL_CHANGE_RX, p_m_rxvol);
861 case mISDNSIGNAL_CONF:
862 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
863 //tone if (!p_m_tone && p_m_conf!=param->mISDNsignal.conf)
864 if (p_m_conf != param->mISDNsignal.conf)
866 p_m_conf = param->mISDNsignal.conf;
867 PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
869 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
870 ph_control(p_m_b_addr, (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf);
872 /* we must set, even if currently tone forbids conf */
873 p_m_conf = param->mISDNsignal.conf;
874 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
877 case mISDNSIGNAL_NODATA:
878 p_m_nodata = param->mISDNsignal.nodata;
879 if (p_m_txmix == p_m_nodata) /* txmix != !nodata */
881 p_m_txmix = !p_m_nodata;
882 PDEBUG(DEBUG_BCHANNEL, "we change mix mode to txmix=%d (nodata=%d).\n", p_m_txmix, p_m_nodata);
884 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
885 ph_control(p_m_b_addr, p_m_txmix?CMX_MIX_ON:CMX_MIX_OFF, 0);
890 case mISDNSIGNAL_RXOFF:
891 if (p_m_rxoff != param->mISDNsignal.rxoff)
893 p_m_rxoff = param->mISDNsignal.rxoff;
894 PDEBUG(DEBUG_BCHANNEL, "we change receive mode to rxoff=%d.\n", p_m_rxoff);
896 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
897 ph_control(p_m_b_addr, p_m_rxoff?CMX_RECEIVE_OFF:CMX_RECEIVE_ON, 0);
901 case mISDNSIGNAL_DTMF:
902 if (p_m_dtmf != param->mISDNsignal.dtmf)
904 p_m_dtmf = param->mISDNsignal.dtmf;
905 PDEBUG(DEBUG_BCHANNEL, "we change dtmf mode to dtmf=%d.\n", p_m_dtmf);
907 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
908 ph_control(p_m_b_addr, p_m_dtmf?DTMF_TONE_START:DTMF_TONE_STOP, 0);
914 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
919 void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parameter *param)
921 struct message *message;
923 switch(param->crypt.type)
925 case CC_ACTBF_REQ: /* activate blowfish */
927 p_m_crypt_key_len = param->crypt.len;
928 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key))
930 PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
931 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
932 message->param.crypt.type = CC_ERROR_IND;
933 message_put(message);
936 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
938 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
940 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
941 ph_control_block(p_m_b_addr, p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len);
944 case CC_DACT_REQ: /* deactivate session encryption */
949 case CR_LISTEN_REQ: /* start listening to messages */
950 p_m_crypt_listen = 1;
951 p_m_crypt_listen_state = 0;
954 case CR_UNLISTEN_REQ: /* stop listening to messages */
955 p_m_crypt_listen = 0;
958 case CR_MESSAGE_REQ: /* send message */
959 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
960 if (!p_m_crypt_msg_len)
962 PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
965 p_m_crypt_msg_current = 0; /* reset */
966 p_m_crypt_msg_loops = 3; /* enable */
967 /* disable txmix, or we get corrupt data due to audio process */
970 PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
971 ph_control(p_m_b_addr, CMX_MIX_OFF, 0);
976 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
982 * endpoint sends messages to the port
984 int PmISDN::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
986 if (Port::message_epoint(epoint_id, message_id, param))
991 case MESSAGE_mISDNSIGNAL: /* user command */
992 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
993 message_mISDNsignal(epoint_id, message_id, param);
996 case MESSAGE_CRYPT: /* crypt control command */
997 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
998 message_crypt(epoint_id, message_id, param);
1007 * main loop for processing messages from mISDN device
1009 int mISDN_handler(void)
1014 struct mISDNport *mISDNport;
1015 class PmISDN *isdnport;
1018 mISDNuser_head_t *hh;
1021 /* the que avoids loopbacks when replying to stack after receiving
1023 mISDNport = mISDNport_first;
1026 /* process turning off rx */
1028 while(i < mISDNport->b_num)
1030 if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
1032 isdnport=mISDNport->b_port[i];
1033 if (isdnport->p_tone_name[0] || isdnport->p_tone_fh>=0 || isdnport->p_tone_fetched || !isdnport->p_m_nodata || isdnport->p_m_crypt_msg_loops || isdnport->p_m_crypt_listen || isdnport->p_record)
1035 /* rx IS required */
1036 if (isdnport->p_m_rxoff)
1039 isdnport->p_m_rxoff = 0;
1040 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
1041 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_ON, 0);
1045 /* rx NOT required */
1046 if (!isdnport->p_m_rxoff)
1049 isdnport->p_m_rxoff = 1;
1050 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
1051 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_OFF, 0);
1054 isdnport->p_m_jittercheck = 0; /* reset jitter detection check */
1059 if (mISDNport->l1timeout && now>mISDNport->l1timeout)
1061 PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
1062 mISDNport->l1timeout = 0;
1065 if (mISDNport->l2establish)
1067 if (now-mISDNport->l2establish > 5)
1069 if (mISDNport->ntmode)
1071 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link NT portnum=%d.\n", mISDNport->portnum);
1072 time(&mISDNport->l2establish);
1074 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1075 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1078 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link TE portnum=%d.\n", mISDNport->portnum);
1079 time(&mISDNport->l2establish);
1082 act.prim = DL_ESTABLISH | REQUEST;
1083 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
1086 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1090 if ((dmsg = msg_dequeue(&mISDNport->downqueue)))
1092 if (mISDNport->ntmode)
1094 hh = (mISDNuser_head_t *)dmsg->data;
1095 PDEBUG(DEBUG_ISDN, "sending queued NT l3-down-message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, dmsg->len);
1096 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1100 frm = (iframe_t *)dmsg->data;
1101 frm->addr = mISDNport->upper_id | FLG_MSG_DOWN;
1102 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
1103 PDEBUG(DEBUG_ISDN, "sending queued TE l3-down-message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, dmsg->len);
1104 mISDN_write(mISDNdevice, dmsg->data, dmsg->len, TIMEOUT_1SEC);
1109 mISDNport = mISDNport->next;
1112 /* get message from kernel */
1113 if (!(msg = alloc_msg(MAX_MSG_SIZE)))
1115 ret = mISDN_read(mISDNdevice, msg->data, MAX_MSG_SIZE, 0);
1119 if (errno == EAGAIN)
1121 PERROR("FATAL ERROR: failed to do mISDN_read()\n");
1127 // printf("%s: ERROR: mISDN_read() returns nothing\n");
1131 frm = (iframe_t *)msg->data;
1136 case MGR_INITTIMER | CONFIRM:
1137 case MGR_ADDTIMER | CONFIRM:
1138 case MGR_DELTIMER | CONFIRM:
1139 case MGR_REMOVETIMER | CONFIRM:
1140 // if (options.deb & DEBUG_ISDN)
1141 // PDEBUG(DEBUG_ISDN, "timer-confirm\n");
1147 mISDNport = mISDNport_first;
1150 if ((frm->prim==(MGR_TIMER | INDICATION)) && mISDNport->ntmode)
1152 itimer_t *it = mISDNport->nst.tlist;
1157 if (it->id == (int)frm->addr)
1163 mISDN_write_frame(mISDNdevice, msg->data, mISDNport->upper_id | FLG_MSG_DOWN,
1164 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1166 PDEBUG(DEBUG_ISDN, "timer-indication %s port %d it=%p\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, it);
1167 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1168 ret = it->function(it->data);
1171 /* we will continue here because we have a timer for a different mISDNport */
1173 //printf("comparing frm->addr %x with upper_id %x\n", frm->addr, mISDNport->upper_id);
1174 if ((frm->addr&STACK_ID_MASK) == (unsigned int)(mISDNport->upper_id&STACK_ID_MASK))
1179 case MGR_SHORTSTATUS | INDICATION:
1180 case MGR_SHORTSTATUS | CONFIRM:
1181 switch(frm->dinfo) {
1182 case SSTATUS_L1_ACTIVATED:
1183 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1185 case SSTATUS_L1_DEACTIVATED:
1186 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1188 case SSTATUS_L2_ESTABLISHED:
1189 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_ESTABLISHED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1191 case SSTATUS_L2_RELEASED:
1192 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_RELEASED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1197 case PH_ACTIVATE | CONFIRM:
1198 case PH_ACTIVATE | INDICATION:
1199 PDEBUG(DEBUG_ISDN, "Received PH_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1200 if (mISDNport->ntmode)
1202 mISDNport->l1link = 1;
1203 setup_queue(mISDNport, 1);
1207 mISDNport->l1link = 1;
1208 setup_queue(mISDNport, 1);
1211 case PH_DEACTIVATE | CONFIRM:
1212 case PH_DEACTIVATE | INDICATION:
1213 PDEBUG(DEBUG_ISDN, "Received PH_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1214 if (mISDNport->ntmode)
1216 mISDNport->l1link = 0;
1217 setup_queue(mISDNport, 0);
1221 mISDNport->l1link = 0;
1222 setup_queue(mISDNport, 0);
1225 case PH_CONTROL | CONFIRM:
1226 case PH_CONTROL | INDICATION:
1227 PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1230 case DL_ESTABLISH | INDICATION:
1231 case DL_ESTABLISH | CONFIRM:
1232 // PDEBUG(DEBUG_ISDN, "addr 0x%x established data link (DL) TE portnum=%d (DL_ESTABLISH)\n", frm->addr, mISDNport->portnum);
1233 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1235 // if (mISDNport->ntmode)
1237 if (mISDNport->l2establish)
1239 mISDNport->l2establish = 0;
1240 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1242 mISDNport->l2link = 1;
1245 case DL_RELEASE | INDICATION:
1246 case DL_RELEASE | CONFIRM:
1247 // PDEBUG(DEBUG_ISDN, "addr 0x%x released data link (DL) TE portnum=%d (DL_RELEASE)\n", frm->addr, mISDNport->portnum);
1248 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1250 // if (mISDNport->ntmode)
1252 mISDNport->l2link = 0;
1255 time(&mISDNport->l2establish);
1256 PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
1262 PDEBUG(DEBUG_STACK, "GOT d-msg from %s port %d prim 0x%x dinfo 0x%x addr 0x%x\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, frm->prim, frm->dinfo, frm->addr);
1263 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM))
1265 PERROR("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
1268 if (mISDNport->ntmode)
1270 /* l1-data enters the nt-mode library */
1271 nst = &mISDNport->nst;
1272 if (nst->l1_l2(nst, msg))
1277 /* l3-data is sent to pbx */
1278 if (stack2manager_te(mISDNport, msg))
1285 //PDEBUG(DEBUG_ISDN, "flg:%d upper_id=%x addr=%x\n", (frm->addr&FLG_CHILD_STACK), (mISDNport->b_addr[0])&(~IF_CHILDMASK), (frm->addr)&(~IF_CHILDMASK));
1286 /* check if child, and if parent stack match */
1287 if ((frm->addr&FLG_CHILD_STACK) && (((unsigned int)(mISDNport->b_addr[0])&(~CHILD_ID_MASK)&STACK_ID_MASK) == ((frm->addr)&(~CHILD_ID_MASK)&STACK_ID_MASK)))
1292 /* we don't care about confirms, we use rx data to sync tx */
1293 case PH_DATA | CONFIRM:
1294 case DL_DATA | CONFIRM:
1297 /* we receive audio data, we respond to it AND we send tones */
1298 case PH_DATA | INDICATION:
1299 case DL_DATA | INDICATION:
1300 case PH_CONTROL | INDICATION:
1302 while(i < mISDNport->b_num)
1304 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1308 if (i == mISDNport->b_num)
1310 PERROR("unhandled b-message (address 0x%x).\n", frm->addr);
1313 if (mISDNport->b_port[i])
1315 //PERROR("port sech: %s data\n", mISDNport->b_port[i]->p_name);
1316 mISDNport->b_port[i]->bchannel_receive(frm);
1318 PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (address 0x%x), ignoring.\n", frm->addr);
1321 case PH_ACTIVATE | INDICATION:
1322 case DL_ESTABLISH | INDICATION:
1323 case PH_ACTIVATE | CONFIRM:
1324 case DL_ESTABLISH | CONFIRM:
1325 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
1327 while(i < mISDNport->b_num)
1329 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1333 if (i == mISDNport->b_num)
1335 PERROR("unhandled b-establish (address 0x%x).\n", frm->addr);
1338 mISDNport->b_state[i] = B_STATE_ACTIVE;
1339 if (!mISDNport->b_port[i])
1340 bchannel_deactivate(mISDNport, i);
1342 bchannel_activate(mISDNport, i);
1345 case PH_DEACTIVATE | INDICATION:
1346 case DL_RELEASE | INDICATION:
1347 case PH_DEACTIVATE | CONFIRM:
1348 case DL_RELEASE | CONFIRM:
1349 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
1351 while(i < mISDNport->b_num)
1353 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1357 if (i == mISDNport->b_num)
1359 PERROR("unhandled b-release (address 0x%x).\n", frm->addr);
1362 mISDNport->b_state[i] = B_STATE_IDLE;
1363 if (mISDNport->b_port[i])
1364 bchannel_activate(mISDNport, i);
1366 bchannel_deactivate(mISDNport, i);
1372 mISDNport = mISDNport->next;
1376 if (frm->prim == (MGR_TIMER | INDICATION))
1377 PERROR("unhandled timer indication message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1379 PERROR("unhandled message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1380 // PERROR("test: is_child: %x of stack %x == %x (baddr %x frm %x)\n", (frm->addr&FLG_CHILD_STACK), ((unsigned int)(mISDNport_first->b_addr[0])&(~CHILD_ID_MASK)&STACK_ID_MASK), ((frm->addr)&(~CHILD_ID_MASK)&STACK_ID_MASK), mISDNport_first->b_addr[0], frm->addr);
1389 * global function to add a new card (port)
1391 struct mISDNport *mISDN_port_open(int port, int ptp)
1394 unsigned char buff[1025];
1395 iframe_t *frm = (iframe_t *)buff;
1396 stack_info_t *stinf;
1397 struct mISDNport *mISDNport, **mISDNportp;
1400 // interface_info_t ii;
1408 /* open mISDNdevice if not already open */
1409 if (mISDNdevice < 0)
1414 PERROR("cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", ret, errno, strerror(errno));
1418 PDEBUG(DEBUG_ISDN, "mISDN device opened.\n");
1420 /* create entity for layer 3 TE-mode */
1421 mISDN_write_frame(mISDNdevice, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1422 ret = mISDN_read_frame(mISDNdevice, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
1423 if (ret < (int)mISDN_HEADER_LEN)
1426 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
1429 entity = frm->dinfo & 0xffff;
1432 PDEBUG(DEBUG_ISDN, "our entity for l3-processes is %d.\n", entity);
1435 /* query port's requirements */
1436 cnt = mISDN_get_stack_count(mISDNdevice);
1439 PERROR("Found no card. Please be sure to load card drivers.\n");
1442 if (port>cnt || port<1)
1444 PERROR("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
1447 ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
1450 PERROR("Cannot get stack info for port %d (ret=%d)\n", port, ret);
1453 stinf = (stack_info_t *)&frm->data.p;
1454 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1456 case ISDN_PID_L0_TE_S0:
1457 PDEBUG(DEBUG_ISDN, "TE-mode BRI S/T interface line\n");
1459 case ISDN_PID_L0_NT_S0:
1460 PDEBUG(DEBUG_ISDN, "NT-mode BRI S/T interface port\n");
1463 case ISDN_PID_L0_TE_U:
1464 PDEBUG(DEBUG_ISDN, "TE-mode BRI U interface line\n");
1466 case ISDN_PID_L0_NT_U:
1467 PDEBUG(DEBUG_ISDN, "NT-mode BRI U interface port\n");
1470 case ISDN_PID_L0_TE_UP2:
1471 PDEBUG(DEBUG_ISDN, "TE-mode BRI Up2 interface line\n");
1473 case ISDN_PID_L0_NT_UP2:
1474 PDEBUG(DEBUG_ISDN, "NT-mode BRI Up2 interface port\n");
1477 case ISDN_PID_L0_TE_E1:
1478 PDEBUG(DEBUG_ISDN, "TE-mode PRI E1 interface line\n");
1481 case ISDN_PID_L0_NT_E1:
1482 PDEBUG(DEBUG_ISDN, "LT-mode PRI E1 interface port\n");
1487 PERROR("unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
1493 if (stinf->pid.protocol[1] == 0)
1495 PERROR("Given port %d: Missing layer 1 NT-mode protocol.\n", port);
1498 if (stinf->pid.protocol[2])
1500 PERROR("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
1506 if (stinf->pid.protocol[1] == 0)
1508 PERROR("Given port %d: Missing layer 1 protocol.\n", port);
1511 if (stinf->pid.protocol[2] == 0)
1513 PERROR("Given port %d: Missing layer 2 protocol.\n", port);
1516 if (stinf->pid.protocol[3] == 0)
1518 PERROR("Given port %d: Missing layer 3 protocol.\n", port);
1522 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
1524 case ISDN_PID_L3_DSS1USER:
1528 PERROR("Given port %d: own protocol 0x%08x", port,stinf->pid.protocol[3]);
1532 if (stinf->pid.protocol[4])
1534 PERROR("Given port %d: Layer 4 protocol not allowed.\n", port);
1539 /* add mISDNport structure */
1540 mISDNportp = &mISDNport_first;
1542 mISDNportp = &mISDNport->next;
1543 mISDNport = (struct mISDNport *)calloc(1, sizeof(struct mISDNport));
1546 PERROR("Cannot alloc mISDNport structure\n");
1550 memset(mISDNport, 0, sizeof(mISDNport));
1551 *mISDNportp = mISDNport;
1553 /* allocate ressources of port */
1554 msg_queue_init(&mISDNport->downqueue);
1555 // SCPY(mISDNport->name, "noname");
1556 mISDNport->portnum = port;
1557 mISDNport->ntmode = nt;
1558 mISDNport->pri = pri;
1559 mISDNport->d_stid = stinf->id;
1560 PDEBUG(DEBUG_ISDN, "d_stid = 0x%x.\n", mISDNport->d_stid);
1561 mISDNport->b_num = stinf->childcnt;
1562 PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
1563 if ((stinf->pid.protocol[2]&ISDN_PID_L2_DF_PTP) || (nt&&ptp) || pri)
1565 PDEBUG(DEBUG_ISDN, "Port is point-to-point.\n");
1566 mISDNport->ptp = ptp = 1;
1569 while(i < stinf->childcnt)
1571 mISDNport->b_stid[i] = stinf->child[i];
1572 PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
1575 memset(&li, 0, sizeof(li));
1576 UCPY(&li.name[0], (nt)?"net l2":"pbx l4");
1579 li.pid.protocol[nt?2:4] = (nt)?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1580 li.pid.layermask = ISDN_LAYER((nt?2:4));
1581 li.st = mISDNport->d_stid;
1582 ret = mISDN_new_layer(mISDNdevice, &li);
1585 PERROR("Cannot add layer %d of port %d (ret %d)\n", nt?2:4, port, ret);
1587 mISDNport_close(mISDNport);
1590 mISDNport->upper_id = li.id;
1591 ret = mISDN_register_layer(mISDNdevice, mISDNport->d_stid, mISDNport->upper_id);
1594 PERROR("Cannot register layer %d of port %d\n", nt?2:4, port);
1597 mISDNport->lower_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?1:3); // id of lower layer (nt=1, te=3)
1598 if (mISDNport->lower_id < 0)
1600 PERROR("Cannot get layer(%d) id of port %d\n", nt?1:3, port);
1603 mISDNport->upper_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?2:4); // id of uppermost layer (nt=2, te=4)
1604 if (mISDNport->upper_id < 0)
1606 PERROR("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
1609 PDEBUG(DEBUG_ISDN, "Layer %d of port %d added.\n", nt?2:4, port);
1611 /* if ntmode, establish L1 to send the tei removal during start */
1612 if (mISDNport->ntmode)
1616 act.prim = PH_ACTIVATE | REQUEST;
1617 act.addr = mISDNport->upper_id | FLG_MSG_DOWN;
1618 printf("UPPER ID 0x%x, addr 0x%x\n",mISDNport->upper_id, act.addr);
1621 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1622 usleep(10000); /* to be sure, that l1 is up */
1625 /* create nst (nt-mode only) */
1628 mgr = &mISDNport->mgr;
1629 nst = &mISDNport->nst;
1634 nst->l3_manager = stack2manager_nt; /* messages from nt-mode */
1635 nst->device = mISDNdevice;
1637 nst->d_stid = mISDNport->d_stid;
1639 nst->feature = FEATURE_NET_HOLD;
1641 nst->feature |= FEATURE_NET_PTP;
1643 nst->feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1646 while(i < mISDNport->b_num)
1648 nst->b_stid[i] = mISDNport->b_stid[i];
1652 nst->l1_id = mISDNport->lower_id;
1653 nst->l2_id = mISDNport->upper_id;
1656 msg_queue_init(&nst->down_queue);
1662 /* if te-mode, query state link */
1663 if (!mISDNport->ntmode)
1667 PDEBUG(DEBUG_ISDN, "sending short status request for port %d.\n", port);
1668 act.prim = MGR_SHORTSTATUS | REQUEST;
1669 act.addr = mISDNport->upper_id | MSG_BROADCAST;
1670 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
1672 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1674 /* if ptp AND te-mode, pull up the link */
1675 if (mISDNport->ptp && !mISDNport->ntmode)
1679 act.prim = DL_ESTABLISH | REQUEST;
1680 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 4 | FLG_MSG_DOWN;
1683 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1685 /* if ptp AND nt-mode, pull up the link */
1686 if (mISDNport->ptp && mISDNport->ntmode)
1690 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1691 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1694 /* initially, we assume that the link is down, exept for nt-ptmp */
1695 mISDNport->l2link = (mISDNport->ntmode && !mISDNport->ptp)?1:0;
1697 PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
1699 /* add all bchannel layers */
1701 while(i < mISDNport->b_num)
1703 /* create new layer */
1704 PDEBUG(DEBUG_BCHANNEL, "creating bchannel %d (index %d).\n" , i+1+(i>=15), i);
1705 memset(&li, 0, sizeof(li));
1706 memset(&pid, 0, sizeof(pid));
1709 li.st = mISDNport->b_stid[i];
1710 UCPY(li.name, "B L4");
1711 li.pid.layermask = ISDN_LAYER((4));
1712 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
1713 ret = mISDN_new_layer(mISDNdevice, &li);
1717 PERROR("mISDN_new_layer() failed to add bchannel %d (index %d)\n", i+1+(i>=15), i);
1720 mISDNport->b_addr[i] = li.id;
1723 goto failed_new_layer;
1725 PDEBUG(DEBUG_BCHANNEL, "new layer (b_addr=0x%x)\n", mISDNport->b_addr[i]);
1727 /* create new stack */
1728 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1729 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1730 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1731 pid.protocol[4] = ISDN_PID_L4_B_USER;
1732 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1733 ret = mISDN_set_stack(mISDNdevice, mISDNport->b_stid[i], &pid);
1737 PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel (index %d) stid=0x%x\n", ret, i, mISDNport->b_stid[i]);
1738 mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i], MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1739 mISDNport->b_addr[i] = 0;
1740 // mISDNport->b_addr_low[i] = 0;
1743 ret = mISDN_get_setstack_ind(mISDNdevice, mISDNport->b_addr[i]);
1748 mISDNport->b_addr[i] = mISDN_get_layerid(mISDNdevice, mISDNport->b_stid[i], 4);
1749 if (!mISDNport->b_addr[i])
1751 /* deactivate bchannel if already enabled due to crash */
1752 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d) as a precaution.\n", i);
1753 dact.prim = DL_RELEASE | REQUEST;
1754 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
1757 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
1761 PDEBUG(DEBUG_ISDN, "- port %d %s (%s) %d b-channels\n", mISDNport->portnum, (mISDNport->ntmode)?"NT-mode":"TE-mode", (mISDNport->ptp)?"Point-To-Point":"Multipoint", mISDNport->b_num);
1762 printlog("opening port %d %s (%s) %d b-channels\n", mISDNport->portnum, (mISDNport->ntmode)?"NT-mode":"TE-mode", (mISDNport->ptp)?"Point-To-Point":"Multipoint", mISDNport->b_num);
1768 * function to free ALL cards (ports)
1770 void mISDNport_close_all(void)
1772 /* free all ports */
1773 while(mISDNport_first)
1774 mISDNport_close(mISDNport_first);
1778 * free only one port
1780 void mISDNport_close(struct mISDNport *mISDNport)
1782 struct mISDNport **mISDNportp;
1784 classs PmISDN *pmISDN;
1786 unsigned char buf[32];
1789 /* remove all port instance that are linked to this mISDNport */
1793 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN)
1795 pmISDN = (class PmISDN)*port;
1796 if (pmISDN->p_m_mISDNport)
1798 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", pnISDN->p_name, mISDNport->portnum);
1805 printlog("closing port %d\n", mISDNport->portnum);
1807 /* free bchannels */
1809 while(i < mISDNport->b_num)
1811 bchannel_deactivate(mISDNport, i);
1812 PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
1813 if (mISDNport->b_stid[i])
1815 mISDN_clear_stack(mISDNdevice, mISDNport->b_stid[i]);
1816 if (mISDNport->b_addr[i])
1817 mISDN_write_frame(mISDNdevice, buf, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1822 /* free ressources of port */
1823 msg_queue_purge(&mISDNport->downqueue);
1826 if (mISDNport->ntmode)
1828 nst = &mISDNport->nst;
1829 if (nst->manager) /* to see if initialized */
1831 PDEBUG(DEBUG_STACK, "the following messages are ok: one L3 process always exists (broadcast process) and some L2 instances (broadcast + current telephone's instances)\n");
1832 cleanup_Isdnl3(nst);
1833 cleanup_Isdnl2(nst);
1836 msg_queue_purge(&nst->down_queue);
1837 if (nst->phd_down_msg)
1838 free(nst->phd_down_msg);
1842 PDEBUG(DEBUG_BCHANNEL, "freeing d-stack.\n");
1843 if (mISDNport->d_stid)
1845 // mISDN_clear_stack(mISDNdevice, mISDNport->d_stid);
1846 if (mISDNport->lower_id)
1847 mISDN_write_frame(mISDNdevice, buf, mISDNport->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1850 /* remove from list */
1851 mISDNportp = &mISDNport_first;
1854 if (*mISDNportp == mISDNport)
1856 *mISDNportp = (*mISDNportp)->next;
1859 mISDNportp = &((*mISDNportp)->next);
1864 PERROR("software error, mISDNport not in list\n");
1868 memset(mISDNport, 0, sizeof(struct mISDNport));
1872 /* close mISDNdevice, if no port */
1873 if (mISDNdevice>=0 && mISDNport_first==NULL)
1876 mISDN_write_frame(mISDNdevice, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
1878 mISDN_close(mISDNdevice);
1880 PDEBUG(DEBUG_ISDN, "mISDN device closed.\n");
1886 * global function to show all available isdn ports
1888 void mISDN_port_info(void)
1892 int useable, nt, pri;
1893 unsigned char buff[1025];
1894 iframe_t *frm = (iframe_t *)buff;
1895 stack_info_t *stinf;
1899 if ((device = mISDN_open()) < 0)
1901 fprintf(stderr, "cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", device, errno, strerror(errno));
1905 /* get number of stacks */
1907 ii = mISDN_get_stack_count(device);
1911 printf("Found no card. Please be sure to load card drivers.\n");
1914 /* loop the number of cards and get their info */
1917 err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
1920 fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
1923 stinf = (stack_info_t *)&frm->data.p;
1928 /* output the port info */
1929 printf("Port %2d: ", i);
1930 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1932 case ISDN_PID_L0_TE_S0:
1933 printf("TE-mode BRI S/T interface line (for phone lines)");
1935 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
1936 printf(" HFC multiport card");
1939 case ISDN_PID_L0_NT_S0:
1941 printf("NT-mode BRI S/T interface port (for phones)");
1943 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
1944 printf(" HFC multiport card");
1947 case ISDN_PID_L0_TE_U:
1948 printf("TE-mode BRI U interface line");
1950 case ISDN_PID_L0_NT_U:
1952 printf("NT-mode BRI U interface port");
1954 case ISDN_PID_L0_TE_UP2:
1955 printf("TE-mode BRI Up2 interface line");
1957 case ISDN_PID_L0_NT_UP2:
1959 printf("NT-mode BRI Up2 interface port");
1961 case ISDN_PID_L0_TE_E1:
1963 printf("TE-mode PRI E1 interface line (for phone lines)");
1965 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
1966 printf(" HFC-E1 card");
1969 case ISDN_PID_L0_NT_E1:
1972 printf("NT-mode PRI E1 interface port (for phones)");
1974 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
1975 printf(" HFC-E1 card");
1980 printf("unknown type 0x%08x",stinf->pid.protocol[0]);
1986 if (stinf->pid.protocol[1] == 0)
1989 printf(" -> Missing layer 1 NT-mode protocol.\n");
1992 while(p <= MAX_LAYER_NR) {
1993 if (stinf->pid.protocol[p])
1996 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]);
2003 printf(" -> Interface is Point-To-Point (PRI).\n");
2005 printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
2009 if (stinf->pid.protocol[1] == 0)
2012 printf(" -> Missing layer 1 protocol.\n");
2014 if (stinf->pid.protocol[2] == 0)
2017 printf(" -> Missing layer 2 protocol.\n");
2019 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP)
2021 printf(" -> Interface is Poin-To-Point.\n");
2023 if (stinf->pid.protocol[3] == 0)
2026 printf(" -> Missing layer 3 protocol.\n");
2029 printf(" -> Protocol: ");
2030 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
2032 case ISDN_PID_L3_DSS1USER:
2033 printf("DSS1 (Euro ISDN)");
2038 printf("unknown protocol 0x%08x",stinf->pid.protocol[3]);
2043 while(p <= MAX_LAYER_NR) {
2044 if (stinf->pid.protocol[p])
2047 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]);
2052 printf(" - %d B-channels\n", stinf->childcnt);
2055 printf(" * Port NOT useable for PBX\n");
2057 printf("--------\n");
2064 if ((err = mISDN_close(device)))
2066 fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err));