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;
64 #warning set delay by routing parameter or interface config
69 p_m_nodata = 1; /* may be 1, because call always notifies us */
70 p_m_dtmf = !options.nodtmf;
71 sollen wir daraus eine interface-option machen?:
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_txdata)
210 PDEBUG(DEBUG_BCHANNEL, "during activation, we set txdata to txdata=%d.\n", mISDNport->b_port[i]->p_m_txdata);
211 ph_control(mISDNport->b_addr[i], (mISDNport->b_port[i]->p_m_txdata)?CMX_TXDATA_ON:CMX_TXDATA_OFF);
213 if (mISDNport->b_port[i]->p_m_delay)
215 PDEBUG(DEBUG_BCHANNEL, "during activation, we set delay to delay=%d.\n", mISDNport->b_port[i]->p_m_delay);
216 ph_control(mISDNport->b_addr[i], CMX_DELAY, mISDNport->b_port[i]->p_m_delay);
218 if (mISDNport->b_port[i]->p_m_txvol)
220 PDEBUG(DEBUG_BCHANNEL, "during activation, we change tx-volume to shift=%d.\n", mISDNport->b_port[i]->p_m_txvol);
221 ph_control(mISDNport->b_addr[i], VOL_CHANGE_TX, mISDNport->b_port[i]->p_m_txvol);
223 if (mISDNport->b_port[i]->p_m_rxvol)
225 PDEBUG(DEBUG_BCHANNEL, "during activation, we change rx-volume to shift=%d.\n", mISDNport->b_port[i]->p_m_rxvol);
226 ph_control(mISDNport->b_addr[i], VOL_CHANGE_RX, mISDNport->b_port[i]->p_m_rxvol);
228 //tone if (mISDNport->b_port[i]->p_m_conf && !mISDNport->b_port[i]->p_m_tone)
229 if (mISDNport->b_port[i]->p_m_conf)
231 PDEBUG(DEBUG_BCHANNEL, "during activation, we change conference to conf=%d.\n", mISDNport->b_port[i]->p_m_conf);
232 ph_control(mISDNport->b_addr[i], CMX_CONF_JOIN, mISDNport->b_port[i]->p_m_conf);
234 if (mISDNport->b_port[i]->p_m_echo)
236 PDEBUG(DEBUG_BCHANNEL, "during activation, we set echo to echo=%d.\n", mISDNport->b_port[i]->p_m_echo);
237 ph_control(mISDNport->b_addr[i], CMX_ECHO_ON, 0);
239 if (mISDNport->b_port[i]->p_m_tone)
241 PDEBUG(DEBUG_BCHANNEL, "during activation, we set tone to tone=%d.\n", mISDNport->b_port[i]->p_m_tone);
242 ph_control(mISDNport->b_addr[i], TONE_PATT_ON, mISDNport->b_port[i]->p_m_tone);
244 if (mISDNport->b_port[i]->p_m_rxoff)
246 PDEBUG(DEBUG_BCHANNEL, "during activation, we set rxoff to rxoff=%d.\n", mISDNport->b_port[i]->p_m_rxoff);
247 ph_control(mISDNport->b_addr[i], CMX_RECEIVE_OFF, 0);
250 if (mISDNport->b_port[i]->p_m_txmix)
252 PDEBUG(DEBUG_BCHANNEL, "during activation, we set txmix to txmix=%d.\n", mISDNport->b_port[i]->p_m_txmix);
253 ph_control(mISDNport->b_addr[i], CMX_MIX_ON, 0);
256 if (mISDNport->b_port[i]->p_m_dtmf)
258 PDEBUG(DEBUG_BCHANNEL, "during activation, we set dtmf to dtmf=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
259 ph_control(mISDNport->b_addr[i], DTMF_TONE_START, 0);
261 if (mISDNport->b_port[i]->p_m_crypt)
263 PDEBUG(DEBUG_BCHANNEL, "during activation, we set crypt to crypt=%d.\n", mISDNport->b_port[i]->p_m_crypt);
264 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);
267 /* preload tx-buffer */
268 pre->len = mISDNport->b_port[i]->read_audio(p, ISDN_PRELOAD, 1);
271 PDEBUG(DEBUG_BCHANNEL, "port is activated, we fill our buffer (ISDN_PRELOAD = %d).\n", pre->len);
278 pre->prim = DL_DATA | REQUEST;
279 pre->addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
281 mISDN_write(mISDNdevice, pre, mISDN_HEADER_LEN+pre->len, TIMEOUT_1SEC);
286 static void bchannel_deactivate(struct mISDNport *mISDNport, int i)
290 if (mISDNport->b_state[i] == B_STATE_ACTIVE)
292 /* reset dsp features */
293 if (mISDNport->b_port[i])
295 if (mISDNport->b_port[i]->p_m_delay)
297 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset txdata from txdata=%d.\n", mISDNport->b_port[i]->p_m_txdata);
298 ph_control(mISDNport->b_addr[i], CMX_TXDATA_OFF, 0);
300 if (mISDNport->b_port[i]->p_m_delay)
302 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset delay from delay=%d.\n", mISDNport->b_port[i]->p_m_delay);
303 ph_control(mISDNport->b_addr[i], CMX_JITTER, 0);
305 if (mISDNport->b_port[i]->p_m_txvol)
307 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_txvol);
308 ph_control(mISDNport->b_addr[i], VOL_CHANGE_TX, 0);
310 if (mISDNport->b_port[i]->p_m_rxvol)
312 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_rxvol);
313 ph_control(mISDNport->b_addr[i], VOL_CHANGE_RX, 0);
315 if (mISDNport->b_port[i]->p_m_conf)
317 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we release conference from conf=%d.\n", mISDNport->b_port[i]->p_m_conf);
318 ph_control(mISDNport->b_addr[i], CMX_CONF_SPLIT, 0);
320 if (mISDNport->b_port[i]->p_m_echo)
322 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset echo from echo=%d.\n", mISDNport->b_port[i]->p_m_echo);
323 ph_control(mISDNport->b_addr[i], CMX_ECHO_OFF, 0);
325 if (mISDNport->b_port[i]->p_m_tone)
327 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tone from tone=%d.\n", mISDNport->b_port[i]->p_m_tone);
328 ph_control(mISDNport->b_addr[i], TONE_PATT_OFF, 0);
330 if (mISDNport->b_port[i]->p_m_rxoff)
332 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rxoff from rxoff=%d.\n", mISDNport->b_port[i]->p_m_rxoff);
333 ph_control(mISDNport->b_addr[i], CMX_RECEIVE_ON, 0);
336 if (mISDNport->b_port[i]->p_m_txmix)
338 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset txmix from txmix=%d.\n", mISDNport->b_port[i]->p_m_txmix);
339 ph_control(mISDNport->b_addr[i], CMX_MIX_OFF, 0);
342 if (mISDNport->b_port[i]->p_m_dtmf)
344 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset dtmf from dtmf=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
345 ph_control(mISDNport->b_addr[i], DTMF_TONE_STOP, 0);
347 if (mISDNport->b_port[i]->p_m_crypt)
349 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset crypt from crypt=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
350 ph_control(mISDNport->b_addr[i], BF_DISABLE, 0);
353 /* deactivate bchannel */
354 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d), because currently active.\n", i);
355 dact.prim = DL_RELEASE | REQUEST;
356 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
359 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
360 mISDNport->b_state[i] = B_STATE_DEACTIVATING;
367 * check for available channel and reserve+set it.
368 * give channel number or SEL_CHANNEL_ANY or SEL_CHANNEL_NO
370 * returns -(cause value) or x = channel x or 0 = no channel
372 denke ans aktivieren und deaktivieren
373 int PmISDN::seize_bchannel(int channel, int exclusive)
377 /* the channel is what we have */
378 if (p_m_b_channel == channel)
381 /* if channel already in use, release it */
386 if (channel==CHANNEL_NO || channel==0)
389 /* is channel in range ? */
391 || (channel>p_m_mISDNport->b_num && channel<16)
392 || ((channel-1)>p_m_mISDNport->b_num && channel>16)) /* channel-1 because channel 16 is not counted */
393 return(-6); /* channel unacceptable */
395 /* request exclusive channel */
396 if (exclusive && channel>0)
398 i = channel-1-(channel>16);
399 if (p_m_mISDNport->b_port[i])
400 return(-44); /* requested channel not available */
404 /* ask for channel */
407 i = channel-1-(channel>16);
408 if (p_m_mISDNport->b_port[i] == NULL)
412 /* search for channel */
414 while(i < p_m_mISDNport->b_num)
416 if (!p_m_mISDNport->b_port[i])
418 channel = i+1+(i>=15);
423 return(-34); /* no free channel */
427 p_m_mISDNport->b_port[i] = this;
429 p_m_b_channel = channel;
430 p_m_b_exclusive = exclusive;
431 p_m_b_stid = p_m_mISDNport->b_stid[i];
432 p_m_b_addr = p_m_mISDNport->b_addr[i];
435 /* reserve channel */
436 if (p_m_b_reserve == 0) // already reserved
439 mISDNport->b_reserved++;
442 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) seizing bchannel %d (index %d)\n", p_name, channel, i);
448 * drop reserved channel and unset it.
450 void PmISDN::drop_bchannel(void)
452 /* unreserve channel */
454 p_m_mISDNport->b_reserved--;
461 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) dropping bchannel\n", p_name);
463 p_m_mISDNport->b_port[p_m_b_index] = NULL;
474 int PmISDN::handler(void)
476 struct message *message;
478 // NOTE: deletion is done by the child class
480 /* handle timeouts */
483 if (p_m_timer+p_m_timeout < now_d)
485 PDEBUG(DEBUG_ISDN, "(%s) timeout after %d seconds detected (state=%d).\n", p_name, p_m_timeout, p_state);
487 /* send timeout to endpoint */
488 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TIMEOUT);
489 message->param.state = p_state;
490 message_put(message);
495 return(0); /* nothing done */
500 * whenever we get audio data from bchannel, we process it here
502 void PmISDN::bchannel_receive(iframe_t *frm)
504 unsigned char *data_temp;
505 unsigned long length_temp;
506 struct message *message;
509 long long jitter_now;
514 // iframe_t rsp; /* response to possible indication */
516 #warning BCHANNEL-DEBUG
518 // check if we are part of all ports */
519 class Port *port = port_first;
528 PERROR_RUNTIME("**************************************************\n");
529 PERROR_RUNTIME("*** BCHANNEL-DEBUG: !this! is not in list of ports\n");
530 PERROR_RUNTIME("**************************************************\n");
537 if (frm->prim == (PH_CONTROL | INDICATION))
539 cont = *((unsigned long *)&frm->data.p);
540 // PDEBUG(DEBUG_PORT, "PmISDN(%s) received a PH_CONTROL INDICATION 0x%x\n", p_name, cont);
541 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
543 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DTMF);
544 message->param.dtmf = cont & DTMF_TONE_MASK;
545 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL DTMF digit '%c'\n", p_name, message->param.dtmf);
546 message_put(message);
551 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
552 message->param.crypt.type = CC_ERROR_IND;
553 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL reject of blowfish.\n", p_name);
554 message_put(message);
558 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
559 message->param.crypt.type = CC_ACTBF_CONF;
560 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL accept of blowfish.\n", p_name);
561 message_put(message);
567 /* calls will not process any audio data unless
568 * the call is connected OR tones feature is enabled.
570 if (p_state!=PORT_STATE_CONNECT
571 && !p_m_mISDNport->is_tones)
575 /* the bearer capability must be audio in order to send and receive
576 * audio prior or after connect.
578 if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
582 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
585 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
589 /* randomize and listen to crypt message if enabled */
590 if (p_m_crypt_listen)
592 /* the noisy randomizer */
593 p = (unsigned char *)&frm->data.p;
596 mISDN_rand[mISDN_rand_count & 0xff] += *p++;
598 cryptman_listen_bch((unsigned char *)&frm->data.p, frm->len);
602 gettimeofday(&tv, &tz);
603 jitter_now = tv.tv_sec;
604 jitter_now = jitter_now*8000 + tv.tv_usec/125;
605 if (p_m_jittercheck == 0)
607 p_m_jittercheck = jitter_now;
608 p_m_jitterdropped = 0;
610 p_m_jittercheck += frm->len;
611 if (p_m_jittercheck < jitter_now)
613 // PERROR("jitter: ignoring slow data\n");
614 p_m_jittercheck = jitter_now;
616 if (p_m_jittercheck-ISDN_JITTERLIMIT > jitter_now)
618 p_m_jitterdropped += frm->len;
619 p_m_jittercheck -= frm->len;
623 if (p_m_jitterdropped)
625 PERROR("jitter: dropping, caused by fast data: %lld\n", p_m_jitterdropped);
626 p_m_jitterdropped = 0;
629 p = (unsigned char *)&frm->data.p;
631 /* send data to epoint */
632 if (ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
634 //printf("we are port %s and sending to epoint %d\n", p_m_cardname, p_epoint->serial);
635 length_temp = frm->len;
639 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
640 message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
641 memcpy(message->param.data.data, data_temp, message->param.data.len);
642 message->param.data.compressed = 1;
643 message->param.data.port_id = p_serial;
644 message->param.data.port_type = p_type;
645 message_put(message);
646 if (length_temp <= sizeof(message->param.data.data))
648 data_temp += sizeof(message->param.data.data);
649 length_temp -= sizeof(message->param.data.data);
652 /* return the same number of tone data, as we recieved */
653 newlen = read_audio(p, frm->len, 1);
654 /* send tone data to isdn device only if we have data, otherwhise we send nothing */
655 if (newlen>0 && (p_tone_fh>=0 || p_tone_fetched || !p_m_nodata || p_m_crypt_msg_loops))
657 //printf("jolly: sending.... %d %d %d %d %d\n", newlen, p_tone_fh, p_tone_fetched, p_m_nodata, p_m_crypt_msg_loops);
661 p_m_txmix_on -= newlen;
662 if (p_m_txmix_on <= 0)
665 p_m_txmix = !p_m_nodata;
666 PDEBUG(DEBUG_BCHANNEL, "after sending message, we set txmix to txmix=%d.\n", p_m_txmix);
668 ph_control(p_m_b_addr, CMX_MIX_ON, 0);
672 if (p_m_crypt_msg_loops)
674 /* send pending message */
677 tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
680 memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
681 p_m_crypt_msg_current += tosend;
682 if (p_m_crypt_msg_current == p_m_crypt_msg_len)
684 p_m_crypt_msg_current = 0;
685 p_m_crypt_msg_loops--;
687 // we need to disable rxmix some time after sending the loops...
688 if (!p_m_crypt_msg_loops && p_m_txmix)
690 p_m_txmix_on = 8000; /* one sec */
695 frm->prim = frm->prim & 0xfffffffc | REQUEST;
696 frm->addr = p_m_b_addr | FLG_MSG_DOWN;
698 mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
700 if (p_debug_nothingtosend)
702 p_debug_nothingtosend = 0;
703 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
707 if (!p_debug_nothingtosend)
709 p_debug_nothingtosend = 1;
710 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
714 /* response to the data indication */
715 rsp.prim = frm->prim & 0xfffffffc | RESPONSE;
716 rsp.addr = frm->addr & INST_ID_MASK | FLG_MSG_DOWN;
717 rsp.dinfo = frm->dinfo;
719 mISDN_write(mISDNdevice, &rsp, mISDN_HEADER_LEN+rsp.len, TIMEOUT_1SEC);
720 //PDEBUG(DEBUG_ISDN, "written %d bytes.\n", length);
728 void PmISDN::set_echotest(int echo)
730 if (p_m_echo != echo)
733 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
735 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
736 ph_control(p_m_b_addr, p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
743 void PmISDN::set_tone(char *dir, char *tone)
749 PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
756 /* check if we NOT really have to use a dsp-tone */
757 if (!options.dsptones)
761 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
763 PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
764 ph_control(p_m_b_addr, TONE_PATT_OFF, 0);
767 Port::set_tone(dir, tone);
773 /* now we USE dsp-tone, convert name */
774 else if (!strcmp(tone, "dialtone"))
776 switch(options.dsptones) {
777 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
778 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
779 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
781 } else if (!strcmp(tone, "dialpbx"))
783 switch(options.dsptones) {
784 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
785 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
786 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
788 } else if (!strcmp(tone, "ringing"))
790 switch(options.dsptones) {
791 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
792 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
793 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
795 } else if (!strcmp(tone, "ringpbx"))
797 switch(options.dsptones) {
798 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
799 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
800 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
802 } else if (!strcmp(tone, "busy"))
805 switch(options.dsptones) {
806 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
807 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
808 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
810 } else if (!strcmp(tone, "release"))
813 switch(options.dsptones) {
814 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
815 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
816 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
818 } else if (!strcmp(tone, "cause_10"))
820 else if (!strcmp(tone, "cause_11"))
822 else if (!strcmp(tone, "cause_22"))
824 switch(options.dsptones) {
825 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
826 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
827 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
829 } else if (!strncmp(tone, "cause_", 6))
830 id = TONE_SPECIAL_INFO;
834 /* if we have a tone that is not supported by dsp */
835 if (id==TONE_OFF && tone[0])
843 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
845 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
846 ph_control(p_m_b_addr, p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone);
848 /* turn user-space tones off in cases of no tone OR dsp tone */
849 Port::set_tone("",NULL);
853 /* MESSAGE_mISDNSIGNAL */
854 //extern struct message *dddebug;
855 void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union parameter *param)
857 switch(param->mISDNsignal.message)
859 case mISDNSIGNAL_VOLUME:
860 if (p_m_txvol != param->mISDNsignal.txvol)
862 p_m_txvol = param->mISDNsignal.txvol;
863 PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
865 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
866 ph_control(p_m_b_addr, VOL_CHANGE_TX, p_m_txvol);
868 if (p_m_rxvol != param->mISDNsignal.rxvol)
870 p_m_rxvol = param->mISDNsignal.rxvol;
871 PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
873 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
874 ph_control(p_m_b_addr, VOL_CHANGE_RX, p_m_rxvol);
878 case mISDNSIGNAL_CONF:
879 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
880 //tone if (!p_m_tone && p_m_conf!=param->mISDNsignal.conf)
881 if (p_m_conf != param->mISDNsignal.conf)
883 p_m_conf = param->mISDNsignal.conf;
884 PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
886 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
887 ph_control(p_m_b_addr, (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf);
889 /* we must set, even if currently tone forbids conf */
890 p_m_conf = param->mISDNsignal.conf;
891 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
894 case mISDNSIGNAL_CALLDATA:
895 if (p_m_calldata != param->mISDNsignal.calldata)
897 p_m_calldata = param->mISDNsignal.calldata;
898 PDEBUG(DEBUG_BCHANNEL, "we change to calldata=%d.\n", p_m_calldata);
904 case mISDNSIGNAL_NODATA:
905 p_m_nodata = param->mISDNsignal.nodata;
906 if (p_m_txmix == p_m_nodata) /* txmix != !nodata */
908 p_m_txmix = !p_m_nodata;
909 PDEBUG(DEBUG_BCHANNEL, "we change mix mode to txmix=%d (nodata=%d).\n", p_m_txmix, p_m_nodata);
911 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
912 ph_control(p_m_b_addr, p_m_txmix?CMX_MIX_ON:CMX_MIX_OFF, 0);
918 case mISDNSIGNAL_RXOFF:
919 if (p_m_rxoff != param->mISDNsignal.rxoff)
921 p_m_rxoff = param->mISDNsignal.rxoff;
922 PDEBUG(DEBUG_BCHANNEL, "we change receive mode to rxoff=%d.\n", p_m_rxoff);
924 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
925 ph_control(p_m_b_addr, p_m_rxoff?CMX_RECEIVE_OFF:CMX_RECEIVE_ON, 0);
929 case mISDNSIGNAL_DTMF:
930 if (p_m_dtmf != param->mISDNsignal.dtmf)
932 p_m_dtmf = param->mISDNsignal.dtmf;
933 PDEBUG(DEBUG_BCHANNEL, "we change dtmf mode to dtmf=%d.\n", p_m_dtmf);
935 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
936 ph_control(p_m_b_addr, p_m_dtmf?DTMF_TONE_START:DTMF_TONE_STOP, 0);
942 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
947 void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parameter *param)
949 struct message *message;
951 switch(param->crypt.type)
953 case CC_ACTBF_REQ: /* activate blowfish */
955 p_m_crypt_key_len = param->crypt.len;
956 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key))
958 PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
959 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
960 message->param.crypt.type = CC_ERROR_IND;
961 message_put(message);
964 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
966 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
968 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
969 ph_control_block(p_m_b_addr, p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len);
972 case CC_DACT_REQ: /* deactivate session encryption */
977 case CR_LISTEN_REQ: /* start listening to messages */
978 p_m_crypt_listen = 1;
979 p_m_crypt_listen_state = 0;
982 case CR_UNLISTEN_REQ: /* stop listening to messages */
983 p_m_crypt_listen = 0;
986 case CR_MESSAGE_REQ: /* send message */
987 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
988 if (!p_m_crypt_msg_len)
990 PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
993 p_m_crypt_msg_current = 0; /* reset */
994 p_m_crypt_msg_loops = 3; /* enable */
996 /* disable txmix, or we get corrupt data due to audio process */
999 PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
1000 ph_control(p_m_b_addr, CMX_MIX_OFF, 0);
1006 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
1012 * endpoint sends messages to the port
1014 int PmISDN::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
1016 if (Port::message_epoint(epoint_id, message_id, param))
1021 case MESSAGE_mISDNSIGNAL: /* user command */
1022 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
1023 message_mISDNsignal(epoint_id, message_id, param);
1026 case MESSAGE_CRYPT: /* crypt control command */
1027 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
1028 message_crypt(epoint_id, message_id, param);
1037 * main loop for processing messages from mISDN device
1039 int mISDN_handler(void)
1044 struct mISDNport *mISDNport;
1045 class PmISDN *isdnport;
1048 mISDNuser_head_t *hh;
1051 if ((ret = Port::handler()))
1054 /* the que avoids loopbacks when replying to stack after receiving
1056 mISDNport = mISDNport_first;
1059 /* process turning off rx */
1061 while(i < mISDNport->b_num)
1063 if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
1065 isdnport=mISDNport->b_port[i];
1066 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)
1068 /* rx IS required */
1069 if (isdnport->p_m_rxoff)
1072 isdnport->p_m_rxoff = 0;
1073 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
1074 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_ON, 0);
1078 /* rx NOT required */
1079 if (!isdnport->p_m_rxoff)
1082 isdnport->p_m_rxoff = 1;
1083 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
1084 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_OFF, 0);
1087 isdnport->p_m_jittercheck = 0; /* reset jitter detection check */
1092 if (mISDNport->l1timeout && now>mISDNport->l1timeout)
1094 PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
1095 mISDNport->l1timeout = 0;
1098 if (mISDNport->l2establish)
1100 if (now-mISDNport->l2establish > 5)
1102 if (mISDNport->ntmode)
1104 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link NT portnum=%d.\n", mISDNport->portnum);
1105 time(&mISDNport->l2establish);
1107 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1108 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1111 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link TE portnum=%d.\n", mISDNport->portnum);
1112 time(&mISDNport->l2establish);
1115 act.prim = DL_ESTABLISH | REQUEST;
1116 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
1119 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1123 if ((dmsg = msg_dequeue(&mISDNport->downqueue)))
1125 if (mISDNport->ntmode)
1127 hh = (mISDNuser_head_t *)dmsg->data;
1128 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);
1129 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1133 frm = (iframe_t *)dmsg->data;
1134 frm->addr = mISDNport->upper_id | FLG_MSG_DOWN;
1135 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
1136 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);
1137 mISDN_write(mISDNdevice, dmsg->data, dmsg->len, TIMEOUT_1SEC);
1142 mISDNport = mISDNport->next;
1145 /* get message from kernel */
1146 if (!(msg = alloc_msg(MAX_MSG_SIZE)))
1148 ret = mISDN_read(mISDNdevice, msg->data, MAX_MSG_SIZE, 0);
1152 if (errno == EAGAIN)
1154 PERROR("FATAL ERROR: failed to do mISDN_read()\n");
1160 // printf("%s: ERROR: mISDN_read() returns nothing\n");
1164 frm = (iframe_t *)msg->data;
1169 case MGR_INITTIMER | CONFIRM:
1170 case MGR_ADDTIMER | CONFIRM:
1171 case MGR_DELTIMER | CONFIRM:
1172 case MGR_REMOVETIMER | CONFIRM:
1173 // if (options.deb & DEBUG_ISDN)
1174 // PDEBUG(DEBUG_ISDN, "timer-confirm\n");
1180 mISDNport = mISDNport_first;
1183 if ((frm->prim==(MGR_TIMER | INDICATION)) && mISDNport->ntmode)
1185 itimer_t *it = mISDNport->nst.tlist;
1190 if (it->id == (int)frm->addr)
1196 mISDN_write_frame(mISDNdevice, msg->data, mISDNport->upper_id | FLG_MSG_DOWN,
1197 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1199 PDEBUG(DEBUG_ISDN, "timer-indication %s port %d it=%p\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, it);
1200 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1201 ret = it->function(it->data);
1204 /* we will continue here because we have a timer for a different mISDNport */
1206 //printf("comparing frm->addr %x with upper_id %x\n", frm->addr, mISDNport->upper_id);
1207 if ((frm->addr&STACK_ID_MASK) == (unsigned int)(mISDNport->upper_id&STACK_ID_MASK))
1212 case MGR_SHORTSTATUS | INDICATION:
1213 case MGR_SHORTSTATUS | CONFIRM:
1214 switch(frm->dinfo) {
1215 case SSTATUS_L1_ACTIVATED:
1216 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1218 case SSTATUS_L1_DEACTIVATED:
1219 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1221 case SSTATUS_L2_ESTABLISHED:
1222 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_ESTABLISHED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1224 case SSTATUS_L2_RELEASED:
1225 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_RELEASED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1230 case PH_ACTIVATE | CONFIRM:
1231 case PH_ACTIVATE | INDICATION:
1232 PDEBUG(DEBUG_ISDN, "Received PH_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1233 if (mISDNport->ntmode)
1235 mISDNport->l1link = 1;
1236 setup_queue(mISDNport, 1);
1240 mISDNport->l1link = 1;
1241 setup_queue(mISDNport, 1);
1244 case PH_DEACTIVATE | CONFIRM:
1245 case PH_DEACTIVATE | INDICATION:
1246 PDEBUG(DEBUG_ISDN, "Received PH_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1247 if (mISDNport->ntmode)
1249 mISDNport->l1link = 0;
1250 setup_queue(mISDNport, 0);
1254 mISDNport->l1link = 0;
1255 setup_queue(mISDNport, 0);
1258 case PH_CONTROL | CONFIRM:
1259 case PH_CONTROL | INDICATION:
1260 PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1263 case DL_ESTABLISH | INDICATION:
1264 case DL_ESTABLISH | CONFIRM:
1265 // PDEBUG(DEBUG_ISDN, "addr 0x%x established data link (DL) TE portnum=%d (DL_ESTABLISH)\n", frm->addr, mISDNport->portnum);
1266 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1268 // if (mISDNport->ntmode)
1270 if (mISDNport->l2establish)
1272 mISDNport->l2establish = 0;
1273 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1275 mISDNport->l2link = 1;
1278 case DL_RELEASE | INDICATION:
1279 case DL_RELEASE | CONFIRM:
1280 // PDEBUG(DEBUG_ISDN, "addr 0x%x released data link (DL) TE portnum=%d (DL_RELEASE)\n", frm->addr, mISDNport->portnum);
1281 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1283 // if (mISDNport->ntmode)
1285 mISDNport->l2link = 0;
1288 time(&mISDNport->l2establish);
1289 PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
1295 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);
1296 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM))
1298 PERROR("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
1301 if (mISDNport->ntmode)
1303 /* l1-data enters the nt-mode library */
1304 nst = &mISDNport->nst;
1305 if (nst->l1_l2(nst, msg))
1310 /* l3-data is sent to pbx */
1311 if (stack2manager_te(mISDNport, msg))
1318 //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));
1319 /* check if child, and if parent stack match */
1320 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)))
1325 /* we don't care about confirms, we use rx data to sync tx */
1326 case PH_DATA | CONFIRM:
1327 case DL_DATA | CONFIRM:
1330 /* we receive audio data, we respond to it AND we send tones */
1331 case PH_DATA | INDICATION:
1332 case DL_DATA | INDICATION:
1333 case PH_CONTROL | INDICATION:
1335 while(i < mISDNport->b_num)
1337 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1341 if (i == mISDNport->b_num)
1343 PERROR("unhandled b-message (address 0x%x).\n", frm->addr);
1346 if (mISDNport->b_port[i])
1348 //PERROR("port sech: %s data\n", mISDNport->b_port[i]->p_name);
1349 mISDNport->b_port[i]->bchannel_receive(frm);
1351 PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (address 0x%x), ignoring.\n", frm->addr);
1354 case PH_ACTIVATE | INDICATION:
1355 case DL_ESTABLISH | INDICATION:
1356 case PH_ACTIVATE | CONFIRM:
1357 case DL_ESTABLISH | CONFIRM:
1358 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
1360 while(i < mISDNport->b_num)
1362 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1366 if (i == mISDNport->b_num)
1368 PERROR("unhandled b-establish (address 0x%x).\n", frm->addr);
1371 mISDNport->b_state[i] = B_STATE_ACTIVE;
1372 if (!mISDNport->b_port[i])
1373 bchannel_deactivate(mISDNport, i);
1375 bchannel_activate(mISDNport, i);
1378 case PH_DEACTIVATE | INDICATION:
1379 case DL_RELEASE | INDICATION:
1380 case PH_DEACTIVATE | CONFIRM:
1381 case DL_RELEASE | CONFIRM:
1382 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
1384 while(i < mISDNport->b_num)
1386 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1390 if (i == mISDNport->b_num)
1392 PERROR("unhandled b-release (address 0x%x).\n", frm->addr);
1395 mISDNport->b_state[i] = B_STATE_IDLE;
1396 if (mISDNport->b_port[i])
1397 bchannel_activate(mISDNport, i);
1399 bchannel_deactivate(mISDNport, i);
1405 mISDNport = mISDNport->next;
1409 if (frm->prim == (MGR_TIMER | INDICATION))
1410 PERROR("unhandled timer indication message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1412 PERROR("unhandled message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1413 // 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);
1422 * global function to add a new card (port)
1424 struct mISDNport *mISDN_port_open(int port, int ptp, int ptmp)
1427 unsigned char buff[1025];
1428 iframe_t *frm = (iframe_t *)buff;
1429 stack_info_t *stinf;
1430 struct mISDNport *mISDNport, **mISDNportp;
1433 // interface_info_t ii;
1441 /* open mISDNdevice if not already open */
1442 if (mISDNdevice < 0)
1447 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));
1451 PDEBUG(DEBUG_ISDN, "mISDN device opened.\n");
1453 /* create entity for layer 3 TE-mode */
1454 mISDN_write_frame(mISDNdevice, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1455 ret = mISDN_read_frame(mISDNdevice, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
1456 if (ret < (int)mISDN_HEADER_LEN)
1459 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
1462 entity = frm->dinfo & 0xffff;
1465 PDEBUG(DEBUG_ISDN, "our entity for l3-processes is %d.\n", entity);
1468 /* query port's requirements */
1469 cnt = mISDN_get_stack_count(mISDNdevice);
1472 PERROR("Found no card. Please be sure to load card drivers.\n");
1475 if (port>cnt || port<1)
1477 PERROR("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
1480 ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
1483 PERROR("Cannot get stack info for port %d (ret=%d)\n", port, ret);
1486 stinf = (stack_info_t *)&frm->data.p;
1487 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1489 case ISDN_PID_L0_TE_S0:
1490 PDEBUG(DEBUG_ISDN, "TE-mode BRI S/T interface line\n");
1492 case ISDN_PID_L0_NT_S0:
1493 PDEBUG(DEBUG_ISDN, "NT-mode BRI S/T interface port\n");
1496 case ISDN_PID_L0_TE_U:
1497 PDEBUG(DEBUG_ISDN, "TE-mode BRI U interface line\n");
1499 case ISDN_PID_L0_NT_U:
1500 PDEBUG(DEBUG_ISDN, "NT-mode BRI U interface port\n");
1503 case ISDN_PID_L0_TE_UP2:
1504 PDEBUG(DEBUG_ISDN, "TE-mode BRI Up2 interface line\n");
1506 case ISDN_PID_L0_NT_UP2:
1507 PDEBUG(DEBUG_ISDN, "NT-mode BRI Up2 interface port\n");
1510 case ISDN_PID_L0_TE_E1:
1511 PDEBUG(DEBUG_ISDN, "TE-mode PRI E1 interface line\n");
1514 case ISDN_PID_L0_NT_E1:
1515 PDEBUG(DEBUG_ISDN, "LT-mode PRI E1 interface port\n");
1520 PERROR("unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
1526 if (stinf->pid.protocol[1] == 0)
1528 PERROR("Given port %d: Missing layer 1 NT-mode protocol.\n", port);
1531 if (stinf->pid.protocol[2])
1533 PERROR("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
1539 if (stinf->pid.protocol[1] == 0)
1541 PERROR("Given port %d: Missing layer 1 protocol.\n", port);
1544 if (stinf->pid.protocol[2] == 0)
1546 PERROR("Given port %d: Missing layer 2 protocol.\n", port);
1549 if (stinf->pid.protocol[3] == 0)
1551 PERROR("Given port %d: Missing layer 3 protocol.\n", port);
1555 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
1557 case ISDN_PID_L3_DSS1USER:
1561 PERROR("Given port %d: own protocol 0x%08x", port,stinf->pid.protocol[3]);
1565 if (stinf->pid.protocol[4])
1567 PERROR("Given port %d: Layer 4 protocol not allowed.\n", port);
1572 /* add mISDNport structure */
1573 mISDNportp = &mISDNport_first;
1575 mISDNportp = &mISDNport->next;
1576 mISDNport = (struct mISDNport *)calloc(1, sizeof(struct mISDNport));
1579 PERROR("Cannot alloc mISDNport structure\n");
1583 memset(mISDNport, 0, sizeof(mISDNport));
1584 *mISDNportp = mISDNport;
1586 /* allocate ressources of port */
1587 msg_queue_init(&mISDNport->downqueue);
1588 // SCPY(mISDNport->name, "noname");
1589 mISDNport->portnum = port;
1590 mISDNport->ntmode = nt;
1591 mISDNport->pri = pri;
1592 mISDNport->d_stid = stinf->id;
1593 PDEBUG(DEBUG_ISDN, "d_stid = 0x%x.\n", mISDNport->d_stid);
1594 mISDNport->b_num = stinf->childcnt;
1595 PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
1596 if ((stinf->pid.protocol[2]&ISDN_PID_L2_DF_PTP) || (nt&&ptp) || pri)
1598 PDEBUG(DEBUG_ISDN, "Port is point-to-point.\n");
1599 mISDNport->ptp = ptp = 1;
1602 PDEBUG(DEBUG_ISDN, "Port is forced to point-to-multipoint.\n");
1603 mISDNport->ptp = ptp = 0;
1607 while(i < stinf->childcnt)
1609 mISDNport->b_stid[i] = stinf->child[i];
1610 PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
1613 memset(&li, 0, sizeof(li));
1614 UCPY(&li.name[0], (nt)?"net l2":"pbx l4");
1617 li.pid.protocol[nt?2:4] = (nt)?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1618 li.pid.layermask = ISDN_LAYER((nt?2:4));
1619 li.st = mISDNport->d_stid;
1620 ret = mISDN_new_layer(mISDNdevice, &li);
1623 PERROR("Cannot add layer %d of port %d (ret %d)\n", nt?2:4, port, ret);
1625 mISDNport_close(mISDNport);
1628 mISDNport->upper_id = li.id;
1629 ret = mISDN_register_layer(mISDNdevice, mISDNport->d_stid, mISDNport->upper_id);
1632 PERROR("Cannot register layer %d of port %d\n", nt?2:4, port);
1635 mISDNport->lower_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?1:3); // id of lower layer (nt=1, te=3)
1636 if (mISDNport->lower_id < 0)
1638 PERROR("Cannot get layer(%d) id of port %d\n", nt?1:3, port);
1641 mISDNport->upper_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?2:4); // id of uppermost layer (nt=2, te=4)
1642 if (mISDNport->upper_id < 0)
1644 PERROR("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
1647 PDEBUG(DEBUG_ISDN, "Layer %d of port %d added.\n", nt?2:4, port);
1649 /* if ntmode, establish L1 to send the tei removal during start */
1650 if (mISDNport->ntmode)
1654 act.prim = PH_ACTIVATE | REQUEST;
1655 act.addr = mISDNport->upper_id | FLG_MSG_DOWN;
1656 printf("UPPER ID 0x%x, addr 0x%x\n",mISDNport->upper_id, act.addr);
1659 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1660 usleep(10000); /* to be sure, that l1 is up */
1663 /* create nst (nt-mode only) */
1666 mgr = &mISDNport->mgr;
1667 nst = &mISDNport->nst;
1672 nst->l3_manager = stack2manager_nt; /* messages from nt-mode */
1673 nst->device = mISDNdevice;
1675 nst->d_stid = mISDNport->d_stid;
1677 nst->feature = FEATURE_NET_HOLD;
1679 nst->feature |= FEATURE_NET_PTP;
1681 nst->feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1684 while(i < mISDNport->b_num)
1686 nst->b_stid[i] = mISDNport->b_stid[i];
1690 nst->l1_id = mISDNport->lower_id;
1691 nst->l2_id = mISDNport->upper_id;
1694 msg_queue_init(&nst->down_queue);
1700 /* if te-mode, query state link */
1701 if (!mISDNport->ntmode)
1705 PDEBUG(DEBUG_ISDN, "sending short status request for port %d.\n", port);
1706 act.prim = MGR_SHORTSTATUS | REQUEST;
1707 act.addr = mISDNport->upper_id | MSG_BROADCAST;
1708 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
1710 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1712 /* if ptp AND te-mode, pull up the link */
1713 if (mISDNport->ptp && !mISDNport->ntmode)
1717 act.prim = DL_ESTABLISH | REQUEST;
1718 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 4 | FLG_MSG_DOWN;
1721 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1723 /* if ptp AND nt-mode, pull up the link */
1724 if (mISDNport->ptp && mISDNport->ntmode)
1728 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1729 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1732 /* initially, we assume that the link is down, exept for nt-ptmp */
1733 mISDNport->l2link = (mISDNport->ntmode && !mISDNport->ptp)?1:0;
1735 PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
1737 /* add all bchannel layers */
1739 while(i < mISDNport->b_num)
1741 /* create new layer */
1742 PDEBUG(DEBUG_BCHANNEL, "creating bchannel %d (index %d).\n" , i+1+(i>=15), i);
1743 memset(&li, 0, sizeof(li));
1744 memset(&pid, 0, sizeof(pid));
1747 li.st = mISDNport->b_stid[i];
1748 UCPY(li.name, "B L4");
1749 li.pid.layermask = ISDN_LAYER((4));
1750 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
1751 ret = mISDN_new_layer(mISDNdevice, &li);
1755 PERROR("mISDN_new_layer() failed to add bchannel %d (index %d)\n", i+1+(i>=15), i);
1758 mISDNport->b_addr[i] = li.id;
1761 goto failed_new_layer;
1763 PDEBUG(DEBUG_BCHANNEL, "new layer (b_addr=0x%x)\n", mISDNport->b_addr[i]);
1765 /* create new stack */
1766 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1767 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1768 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1769 pid.protocol[4] = ISDN_PID_L4_B_USER;
1770 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1771 ret = mISDN_set_stack(mISDNdevice, mISDNport->b_stid[i], &pid);
1775 PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel (index %d) stid=0x%x\n", ret, i, mISDNport->b_stid[i]);
1776 mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i], MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1777 mISDNport->b_addr[i] = 0;
1778 // mISDNport->b_addr_low[i] = 0;
1781 ret = mISDN_get_setstack_ind(mISDNdevice, mISDNport->b_addr[i]);
1786 mISDNport->b_addr[i] = mISDN_get_layerid(mISDNdevice, mISDNport->b_stid[i], 4);
1787 if (!mISDNport->b_addr[i])
1789 /* deactivate bchannel if already enabled due to crash */
1790 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d) as a precaution.\n", i);
1791 dact.prim = DL_RELEASE | REQUEST;
1792 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
1795 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
1799 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);
1800 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);
1806 * function to free ALL cards (ports)
1808 void mISDNport_close_all(void)
1810 /* free all ports */
1811 while(mISDNport_first)
1812 mISDNport_close(mISDNport_first);
1816 * free only one port
1818 void mISDNport_close(struct mISDNport *mISDNport)
1820 struct mISDNport **mISDNportp;
1822 classs PmISDN *pmISDN;
1824 unsigned char buf[32];
1827 /* remove all port instance that are linked to this mISDNport */
1831 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN)
1833 pmISDN = (class PmISDN)*port;
1834 if (pmISDN->p_m_mISDNport)
1836 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", pnISDN->p_name, mISDNport->portnum);
1843 printlog("closing port %d\n", mISDNport->portnum);
1845 /* free bchannels */
1847 while(i < mISDNport->b_num)
1849 bchannel_deactivate(mISDNport, i);
1850 PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
1851 if (mISDNport->b_stid[i])
1853 mISDN_clear_stack(mISDNdevice, mISDNport->b_stid[i]);
1854 if (mISDNport->b_addr[i])
1855 mISDN_write_frame(mISDNdevice, buf, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1860 /* free ressources of port */
1861 msg_queue_purge(&mISDNport->downqueue);
1864 if (mISDNport->ntmode)
1866 nst = &mISDNport->nst;
1867 if (nst->manager) /* to see if initialized */
1869 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");
1870 cleanup_Isdnl3(nst);
1871 cleanup_Isdnl2(nst);
1874 msg_queue_purge(&nst->down_queue);
1875 if (nst->phd_down_msg)
1876 free(nst->phd_down_msg);
1880 PDEBUG(DEBUG_BCHANNEL, "freeing d-stack.\n");
1881 if (mISDNport->d_stid)
1883 // mISDN_clear_stack(mISDNdevice, mISDNport->d_stid);
1884 if (mISDNport->lower_id)
1885 mISDN_write_frame(mISDNdevice, buf, mISDNport->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1888 /* remove from list */
1889 mISDNportp = &mISDNport_first;
1892 if (*mISDNportp == mISDNport)
1894 *mISDNportp = (*mISDNportp)->next;
1897 mISDNportp = &((*mISDNportp)->next);
1902 PERROR("software error, mISDNport not in list\n");
1906 memset(mISDNport, 0, sizeof(struct mISDNport));
1910 /* close mISDNdevice, if no port */
1911 if (mISDNdevice>=0 && mISDNport_first==NULL)
1914 mISDN_write_frame(mISDNdevice, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
1916 mISDN_close(mISDNdevice);
1918 PDEBUG(DEBUG_ISDN, "mISDN device closed.\n");
1924 * global function to show all available isdn ports
1926 void mISDN_port_info(void)
1930 int useable, nt, pri;
1931 unsigned char buff[1025];
1932 iframe_t *frm = (iframe_t *)buff;
1933 stack_info_t *stinf;
1937 if ((device = mISDN_open()) < 0)
1939 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));
1943 /* get number of stacks */
1945 ii = mISDN_get_stack_count(device);
1949 printf("Found no card. Please be sure to load card drivers.\n");
1952 /* loop the number of cards and get their info */
1955 err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
1958 fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
1961 stinf = (stack_info_t *)&frm->data.p;
1966 /* output the port info */
1967 printf("Port %2d: ", i);
1968 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1970 case ISDN_PID_L0_TE_S0:
1971 printf("TE-mode BRI S/T interface line (for phone lines)");
1973 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
1974 printf(" HFC multiport card");
1977 case ISDN_PID_L0_NT_S0:
1979 printf("NT-mode BRI S/T interface port (for phones)");
1981 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
1982 printf(" HFC multiport card");
1985 case ISDN_PID_L0_TE_U:
1986 printf("TE-mode BRI U interface line");
1988 case ISDN_PID_L0_NT_U:
1990 printf("NT-mode BRI U interface port");
1992 case ISDN_PID_L0_TE_UP2:
1993 printf("TE-mode BRI Up2 interface line");
1995 case ISDN_PID_L0_NT_UP2:
1997 printf("NT-mode BRI Up2 interface port");
1999 case ISDN_PID_L0_TE_E1:
2001 printf("TE-mode PRI E1 interface line (for phone lines)");
2003 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
2004 printf(" HFC-E1 card");
2007 case ISDN_PID_L0_NT_E1:
2010 printf("NT-mode PRI E1 interface port (for phones)");
2012 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
2013 printf(" HFC-E1 card");
2018 printf("unknown type 0x%08x",stinf->pid.protocol[0]);
2024 if (stinf->pid.protocol[1] == 0)
2027 printf(" -> Missing layer 1 NT-mode protocol.\n");
2030 while(p <= MAX_LAYER_NR) {
2031 if (stinf->pid.protocol[p])
2034 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]);
2041 printf(" -> Interface is Point-To-Point (PRI).\n");
2043 printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
2047 if (stinf->pid.protocol[1] == 0)
2050 printf(" -> Missing layer 1 protocol.\n");
2052 if (stinf->pid.protocol[2] == 0)
2055 printf(" -> Missing layer 2 protocol.\n");
2057 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP)
2059 printf(" -> Interface is Poin-To-Point.\n");
2061 if (stinf->pid.protocol[3] == 0)
2064 printf(" -> Missing layer 3 protocol.\n");
2067 printf(" -> Protocol: ");
2068 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
2070 case ISDN_PID_L3_DSS1USER:
2071 printf("DSS1 (Euro ISDN)");
2076 printf("unknown protocol 0x%08x",stinf->pid.protocol[3]);
2081 while(p <= MAX_LAYER_NR) {
2082 if (stinf->pid.protocol[p])
2085 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]);
2090 printf(" - %d B-channels\n", stinf->childcnt);
2093 printf(" * Port NOT useable for PBX\n");
2095 printf("--------\n");
2102 if ((err = mISDN_close(device)))
2104 fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err));