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 /* calls will not process any audio data unless
554 * the call is connected OR tones feature is enabled.
556 if (p_state!=PORT_STATE_CONNECT
557 && !p_m_mISDNport->is_tones)
561 /* the bearer capability must be audio in order to send and receive
562 * audio prior or after connect.
564 if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
568 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
571 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
575 /* randomize and listen to crypt message if enabled */
576 if (p_m_crypt_listen)
578 /* the noisy randomizer */
579 p = (unsigned char *)&frm->data.p;
582 mISDN_rand[mISDN_rand_count & 0xff] += *p++;
584 cryptman_listen_bch((unsigned char *)&frm->data.p, frm->len);
588 gettimeofday(&tv, &tz);
589 jitter_now = tv.tv_sec;
590 jitter_now = jitter_now*8000 + tv.tv_usec/125;
591 if (p_m_jittercheck == 0)
593 p_m_jittercheck = jitter_now;
594 p_m_jitterdropped = 0;
596 p_m_jittercheck += frm->len;
597 if (p_m_jittercheck < jitter_now)
599 // PERROR("jitter: ignoring slow data\n");
600 p_m_jittercheck = jitter_now;
602 if (p_m_jittercheck-ISDN_JITTERLIMIT > jitter_now)
604 p_m_jitterdropped += frm->len;
605 p_m_jittercheck -= frm->len;
609 if (p_m_jitterdropped)
611 PERROR("jitter: dropping, caused by fast data: %lld\n", p_m_jitterdropped);
612 p_m_jitterdropped = 0;
615 p = (unsigned char *)&frm->data.p;
617 /* send data to epoint */
618 if (ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
620 //printf("we are port %s and sending to epoint %d\n", p_m_cardname, p_epoint->serial);
621 length_temp = frm->len;
625 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
626 message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
627 memcpy(message->param.data.data, data_temp, message->param.data.len);
628 message->param.data.compressed = 1;
629 message->param.data.port_id = p_serial;
630 message->param.data.port_type = p_type;
631 message_put(message);
632 if (length_temp <= sizeof(message->param.data.data))
634 data_temp += sizeof(message->param.data.data);
635 length_temp -= sizeof(message->param.data.data);
638 /* return the same number of tone data, as we recieved */
639 newlen = read_audio(p, frm->len, 1);
640 /* send tone data to isdn device only if we have data, otherwhise we send nothing */
641 if (newlen>0 && (p_tone_fh>=0 || p_tone_fetched || !p_m_nodata || p_m_crypt_msg_loops))
643 //printf("jolly: sending.... %d %d %d %d %d\n", newlen, p_tone_fh, p_tone_fetched, p_m_nodata, p_m_crypt_msg_loops);
646 p_m_txmix_on -= newlen;
647 if (p_m_txmix_on <= 0)
650 p_m_txmix = !p_m_nodata;
651 PDEBUG(DEBUG_BCHANNEL, "after sending message, we set txmix to txmix=%d.\n", p_m_txmix);
653 ph_control(p_m_b_addr, CMX_MIX_ON, 0);
656 if (p_m_crypt_msg_loops)
658 /* send pending message */
661 tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
664 memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
665 p_m_crypt_msg_current += tosend;
666 if (p_m_crypt_msg_current == p_m_crypt_msg_len)
668 p_m_crypt_msg_current = 0;
669 p_m_crypt_msg_loops--;
670 // we need to disable rxmix some time after sending the loops...
671 if (!p_m_crypt_msg_loops && p_m_txmix)
673 p_m_txmix_on = 8000; /* one sec */
677 frm->prim = frm->prim & 0xfffffffc | REQUEST;
678 frm->addr = p_m_b_addr | FLG_MSG_DOWN;
680 mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
682 if (p_debug_nothingtosend)
684 p_debug_nothingtosend = 0;
685 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
689 if (!p_debug_nothingtosend)
691 p_debug_nothingtosend = 1;
692 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
696 /* response to the data indication */
697 rsp.prim = frm->prim & 0xfffffffc | RESPONSE;
698 rsp.addr = frm->addr & INST_ID_MASK | FLG_MSG_DOWN;
699 rsp.dinfo = frm->dinfo;
701 mISDN_write(mISDNdevice, &rsp, mISDN_HEADER_LEN+rsp.len, TIMEOUT_1SEC);
702 //PDEBUG(DEBUG_ISDN, "written %d bytes.\n", length);
710 void PmISDN::set_echotest(int echo)
712 if (p_m_echo != echo)
715 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
717 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
718 ph_control(p_m_b_addr, p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
725 void PmISDN::set_tone(char *dir, char *tone)
731 PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
738 /* check if we NOT really have to use a dsp-tone */
739 if (!options.dsptones)
743 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
745 PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
746 ph_control(p_m_b_addr, TONE_PATT_OFF, 0);
749 Port::set_tone(dir, tone);
755 /* now we USE dsp-tone, convert name */
756 else if (!strcmp(tone, "dialtone"))
758 switch(options.dsptones) {
759 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
760 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
761 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
763 } else if (!strcmp(tone, "dialpbx"))
765 switch(options.dsptones) {
766 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
767 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
768 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
770 } else if (!strcmp(tone, "ringing"))
772 switch(options.dsptones) {
773 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
774 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
775 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
777 } else if (!strcmp(tone, "ringpbx"))
779 switch(options.dsptones) {
780 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
781 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
782 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
784 } else if (!strcmp(tone, "busy"))
787 switch(options.dsptones) {
788 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
789 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
790 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
792 } else if (!strcmp(tone, "release"))
795 switch(options.dsptones) {
796 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
797 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
798 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
800 } else if (!strcmp(tone, "cause_10"))
802 else if (!strcmp(tone, "cause_11"))
804 else if (!strcmp(tone, "cause_22"))
806 switch(options.dsptones) {
807 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
808 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
809 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
811 } else if (!strncmp(tone, "cause_", 6))
812 id = TONE_SPECIAL_INFO;
816 /* if we have a tone that is not supported by dsp */
817 if (id==TONE_OFF && tone[0])
825 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
827 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
828 ph_control(p_m_b_addr, p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone);
830 /* turn user-space tones off in cases of no tone OR dsp tone */
831 Port::set_tone("",NULL);
835 /* MESSAGE_mISDNSIGNAL */
836 //extern struct message *dddebug;
837 void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union parameter *param)
839 switch(param->mISDNsignal.message)
841 case mISDNSIGNAL_VOLUME:
842 if (p_m_txvol != param->mISDNsignal.txvol)
844 p_m_txvol = param->mISDNsignal.txvol;
845 PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
847 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
848 ph_control(p_m_b_addr, VOL_CHANGE_TX, p_m_txvol);
850 if (p_m_rxvol != param->mISDNsignal.rxvol)
852 p_m_rxvol = param->mISDNsignal.rxvol;
853 PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
855 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
856 ph_control(p_m_b_addr, VOL_CHANGE_RX, p_m_rxvol);
860 case mISDNSIGNAL_CONF:
861 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
862 //tone if (!p_m_tone && p_m_conf!=param->mISDNsignal.conf)
863 if (p_m_conf != param->mISDNsignal.conf)
865 p_m_conf = param->mISDNsignal.conf;
866 PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
868 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
869 ph_control(p_m_b_addr, (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf);
871 /* we must set, even if currently tone forbids conf */
872 p_m_conf = param->mISDNsignal.conf;
873 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
876 case mISDNSIGNAL_NODATA:
877 p_m_nodata = param->mISDNsignal.nodata;
878 if (p_m_txmix == p_m_nodata) /* txmix != !nodata */
880 p_m_txmix = !p_m_nodata;
881 PDEBUG(DEBUG_BCHANNEL, "we change mix mode to txmix=%d (nodata=%d).\n", p_m_txmix, p_m_nodata);
883 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
884 ph_control(p_m_b_addr, p_m_txmix?CMX_MIX_ON:CMX_MIX_OFF, 0);
889 case mISDNSIGNAL_RXOFF:
890 if (p_m_rxoff != param->mISDNsignal.rxoff)
892 p_m_rxoff = param->mISDNsignal.rxoff;
893 PDEBUG(DEBUG_BCHANNEL, "we change receive mode to rxoff=%d.\n", p_m_rxoff);
895 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
896 ph_control(p_m_b_addr, p_m_rxoff?CMX_RECEIVE_OFF:CMX_RECEIVE_ON, 0);
900 case mISDNSIGNAL_DTMF:
901 if (p_m_dtmf != param->mISDNsignal.dtmf)
903 p_m_dtmf = param->mISDNsignal.dtmf;
904 PDEBUG(DEBUG_BCHANNEL, "we change dtmf mode to dtmf=%d.\n", p_m_dtmf);
906 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
907 ph_control(p_m_b_addr, p_m_dtmf?DTMF_TONE_START:DTMF_TONE_STOP, 0);
913 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
918 void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parameter *param)
920 struct message *message;
922 switch(param->crypt.type)
924 case CC_ACTBF_REQ: /* activate blowfish */
926 p_m_crypt_key_len = param->crypt.len;
927 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key))
929 PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
930 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
931 message->param.crypt.type = CC_ERROR_IND;
932 message_put(message);
935 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
937 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
939 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
940 ph_control_block(p_m_b_addr, p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len);
943 case CC_DACT_REQ: /* deactivate session encryption */
948 case CR_LISTEN_REQ: /* start listening to messages */
949 p_m_crypt_listen = 1;
950 p_m_crypt_listen_state = 0;
953 case CR_UNLISTEN_REQ: /* stop listening to messages */
954 p_m_crypt_listen = 0;
957 case CR_MESSAGE_REQ: /* send message */
958 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
959 if (!p_m_crypt_msg_len)
961 PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
964 p_m_crypt_msg_current = 0; /* reset */
965 p_m_crypt_msg_loops = 3; /* enable */
966 /* disable txmix, or we get corrupt data due to audio process */
969 PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
970 ph_control(p_m_b_addr, CMX_MIX_OFF, 0);
975 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
981 * endpoint sends messages to the port
983 int PmISDN::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
985 if (Port::message_epoint(epoint_id, message_id, param))
990 case MESSAGE_mISDNSIGNAL: /* user command */
991 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
992 message_mISDNsignal(epoint_id, message_id, param);
995 case MESSAGE_CRYPT: /* crypt control command */
996 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
997 message_crypt(epoint_id, message_id, param);
1006 * main loop for processing messages from mISDN device
1008 int mISDN_handler(void)
1013 struct mISDNport *mISDNport;
1014 class PmISDN *isdnport;
1017 mISDNuser_head_t *hh;
1020 /* the que avoids loopbacks when replying to stack after receiving
1022 mISDNport = mISDNport_first;
1025 /* process turning off rx */
1027 while(i < mISDNport->b_num)
1029 if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
1031 isdnport=mISDNport->b_port[i];
1032 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)
1034 /* rx IS required */
1035 if (isdnport->p_m_rxoff)
1038 isdnport->p_m_rxoff = 0;
1039 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
1040 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_ON, 0);
1044 /* rx NOT required */
1045 if (!isdnport->p_m_rxoff)
1048 isdnport->p_m_rxoff = 1;
1049 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
1050 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_OFF, 0);
1053 isdnport->p_m_jittercheck = 0; /* reset jitter detection check */
1058 if (mISDNport->l1timeout && now>mISDNport->l1timeout)
1060 PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
1061 mISDNport->l1timeout = 0;
1064 if (mISDNport->l2establish)
1066 if (now-mISDNport->l2establish > 5)
1068 if (mISDNport->ntmode)
1070 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link NT portnum=%d.\n", mISDNport->portnum);
1071 time(&mISDNport->l2establish);
1073 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1074 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1077 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link TE portnum=%d.\n", mISDNport->portnum);
1078 time(&mISDNport->l2establish);
1081 act.prim = DL_ESTABLISH | REQUEST;
1082 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
1085 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1089 if ((dmsg = msg_dequeue(&mISDNport->downqueue)))
1091 if (mISDNport->ntmode)
1093 hh = (mISDNuser_head_t *)dmsg->data;
1094 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);
1095 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1099 frm = (iframe_t *)dmsg->data;
1100 frm->addr = mISDNport->upper_id | FLG_MSG_DOWN;
1101 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
1102 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);
1103 mISDN_write(mISDNdevice, dmsg->data, dmsg->len, TIMEOUT_1SEC);
1108 mISDNport = mISDNport->next;
1111 /* get message from kernel */
1112 if (!(msg = alloc_msg(MAX_MSG_SIZE)))
1114 ret = mISDN_read(mISDNdevice, msg->data, MAX_MSG_SIZE, 0);
1118 if (errno == EAGAIN)
1120 PERROR("FATAL ERROR: failed to do mISDN_read()\n");
1126 // printf("%s: ERROR: mISDN_read() returns nothing\n");
1130 frm = (iframe_t *)msg->data;
1135 case MGR_INITTIMER | CONFIRM:
1136 case MGR_ADDTIMER | CONFIRM:
1137 case MGR_DELTIMER | CONFIRM:
1138 case MGR_REMOVETIMER | CONFIRM:
1139 // if (options.deb & DEBUG_ISDN)
1140 // PDEBUG(DEBUG_ISDN, "timer-confirm\n");
1146 mISDNport = mISDNport_first;
1149 if ((frm->prim==(MGR_TIMER | INDICATION)) && mISDNport->ntmode)
1151 itimer_t *it = mISDNport->nst.tlist;
1156 if (it->id == (int)frm->addr)
1162 mISDN_write_frame(mISDNdevice, msg->data, mISDNport->upper_id | FLG_MSG_DOWN,
1163 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1165 PDEBUG(DEBUG_ISDN, "timer-indication %s port %d it=%p\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, it);
1166 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1167 ret = it->function(it->data);
1170 /* we will continue here because we have a timer for a different mISDNport */
1172 //printf("comparing frm->addr %x with upper_id %x\n", frm->addr, mISDNport->upper_id);
1173 if ((frm->addr&STACK_ID_MASK) == (unsigned int)(mISDNport->upper_id&STACK_ID_MASK))
1178 case MGR_SHORTSTATUS | INDICATION:
1179 case MGR_SHORTSTATUS | CONFIRM:
1180 switch(frm->dinfo) {
1181 case SSTATUS_L1_ACTIVATED:
1182 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1184 case SSTATUS_L1_DEACTIVATED:
1185 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1187 case SSTATUS_L2_ESTABLISHED:
1188 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_ESTABLISHED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1190 case SSTATUS_L2_RELEASED:
1191 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_RELEASED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1196 case PH_ACTIVATE | CONFIRM:
1197 case PH_ACTIVATE | INDICATION:
1198 PDEBUG(DEBUG_ISDN, "Received PH_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1199 if (mISDNport->ntmode)
1201 mISDNport->l1link = 1;
1202 setup_queue(mISDNport, 1);
1206 mISDNport->l1link = 1;
1207 setup_queue(mISDNport, 1);
1210 case PH_DEACTIVATE | CONFIRM:
1211 case PH_DEACTIVATE | INDICATION:
1212 PDEBUG(DEBUG_ISDN, "Received PH_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1213 if (mISDNport->ntmode)
1215 mISDNport->l1link = 0;
1216 setup_queue(mISDNport, 0);
1220 mISDNport->l1link = 0;
1221 setup_queue(mISDNport, 0);
1224 case PH_CONTROL | CONFIRM:
1225 case PH_CONTROL | INDICATION:
1226 PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1229 case DL_ESTABLISH | INDICATION:
1230 case DL_ESTABLISH | CONFIRM:
1231 // PDEBUG(DEBUG_ISDN, "addr 0x%x established data link (DL) TE portnum=%d (DL_ESTABLISH)\n", frm->addr, mISDNport->portnum);
1232 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1234 // if (mISDNport->ntmode)
1236 if (mISDNport->l2establish)
1238 mISDNport->l2establish = 0;
1239 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1241 mISDNport->l2link = 1;
1244 case DL_RELEASE | INDICATION:
1245 case DL_RELEASE | CONFIRM:
1246 // PDEBUG(DEBUG_ISDN, "addr 0x%x released data link (DL) TE portnum=%d (DL_RELEASE)\n", frm->addr, mISDNport->portnum);
1247 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1249 // if (mISDNport->ntmode)
1251 mISDNport->l2link = 0;
1254 time(&mISDNport->l2establish);
1255 PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
1261 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);
1262 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM))
1264 PERROR("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
1267 if (mISDNport->ntmode)
1269 /* l1-data enters the nt-mode library */
1270 nst = &mISDNport->nst;
1271 if (nst->l1_l2(nst, msg))
1276 /* l3-data is sent to pbx */
1277 if (stack2manager_te(mISDNport, msg))
1284 //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));
1285 /* check if child, and if parent stack match */
1286 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)))
1291 /* we don't care about confirms, we use rx data to sync tx */
1292 case PH_DATA | CONFIRM:
1293 case DL_DATA | CONFIRM:
1296 /* we receive audio data, we respond to it AND we send tones */
1297 case PH_DATA | INDICATION:
1298 case DL_DATA | INDICATION:
1299 case PH_CONTROL | INDICATION:
1301 while(i < mISDNport->b_num)
1303 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1307 if (i == mISDNport->b_num)
1309 PERROR("unhandled b-message (address 0x%x).\n", frm->addr);
1312 if (mISDNport->b_port[i])
1314 //PERROR("port sech: %s data\n", mISDNport->b_port[i]->p_name);
1315 mISDNport->b_port[i]->bchannel_receive(frm);
1317 PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (address 0x%x), ignoring.\n", frm->addr);
1320 case PH_ACTIVATE | INDICATION:
1321 case DL_ESTABLISH | INDICATION:
1322 case PH_ACTIVATE | CONFIRM:
1323 case DL_ESTABLISH | CONFIRM:
1324 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
1326 while(i < mISDNport->b_num)
1328 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1332 if (i == mISDNport->b_num)
1334 PERROR("unhandled b-establish (address 0x%x).\n", frm->addr);
1337 mISDNport->b_state[i] = B_STATE_ACTIVE;
1338 if (!mISDNport->b_port[i])
1339 bchannel_deactivate(mISDNport, i);
1341 bchannel_activate(mISDNport, i);
1344 case PH_DEACTIVATE | INDICATION:
1345 case DL_RELEASE | INDICATION:
1346 case PH_DEACTIVATE | CONFIRM:
1347 case DL_RELEASE | CONFIRM:
1348 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
1350 while(i < mISDNport->b_num)
1352 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1356 if (i == mISDNport->b_num)
1358 PERROR("unhandled b-release (address 0x%x).\n", frm->addr);
1361 mISDNport->b_state[i] = B_STATE_IDLE;
1362 if (mISDNport->b_port[i])
1363 bchannel_activate(mISDNport, i);
1365 bchannel_deactivate(mISDNport, i);
1371 mISDNport = mISDNport->next;
1375 if (frm->prim == (MGR_TIMER | INDICATION))
1376 PERROR("unhandled timer indication message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1378 PERROR("unhandled message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1379 // 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);
1388 * global function to add a new card (port)
1390 struct mISDNport *mISDN_port_open(int port, int ptp, int ptmp)
1393 unsigned char buff[1025];
1394 iframe_t *frm = (iframe_t *)buff;
1395 stack_info_t *stinf;
1396 struct mISDNport *mISDNport, **mISDNportp;
1399 // interface_info_t ii;
1407 /* open mISDNdevice if not already open */
1408 if (mISDNdevice < 0)
1413 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));
1417 PDEBUG(DEBUG_ISDN, "mISDN device opened.\n");
1419 /* create entity for layer 3 TE-mode */
1420 mISDN_write_frame(mISDNdevice, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1421 ret = mISDN_read_frame(mISDNdevice, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
1422 if (ret < (int)mISDN_HEADER_LEN)
1425 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
1428 entity = frm->dinfo & 0xffff;
1431 PDEBUG(DEBUG_ISDN, "our entity for l3-processes is %d.\n", entity);
1434 /* query port's requirements */
1435 cnt = mISDN_get_stack_count(mISDNdevice);
1438 PERROR("Found no card. Please be sure to load card drivers.\n");
1441 if (port>cnt || port<1)
1443 PERROR("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
1446 ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
1449 PERROR("Cannot get stack info for port %d (ret=%d)\n", port, ret);
1452 stinf = (stack_info_t *)&frm->data.p;
1453 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1455 case ISDN_PID_L0_TE_S0:
1456 PDEBUG(DEBUG_ISDN, "TE-mode BRI S/T interface line\n");
1458 case ISDN_PID_L0_NT_S0:
1459 PDEBUG(DEBUG_ISDN, "NT-mode BRI S/T interface port\n");
1462 case ISDN_PID_L0_TE_U:
1463 PDEBUG(DEBUG_ISDN, "TE-mode BRI U interface line\n");
1465 case ISDN_PID_L0_NT_U:
1466 PDEBUG(DEBUG_ISDN, "NT-mode BRI U interface port\n");
1469 case ISDN_PID_L0_TE_UP2:
1470 PDEBUG(DEBUG_ISDN, "TE-mode BRI Up2 interface line\n");
1472 case ISDN_PID_L0_NT_UP2:
1473 PDEBUG(DEBUG_ISDN, "NT-mode BRI Up2 interface port\n");
1476 case ISDN_PID_L0_TE_E1:
1477 PDEBUG(DEBUG_ISDN, "TE-mode PRI E1 interface line\n");
1480 case ISDN_PID_L0_NT_E1:
1481 PDEBUG(DEBUG_ISDN, "LT-mode PRI E1 interface port\n");
1486 PERROR("unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
1492 if (stinf->pid.protocol[1] == 0)
1494 PERROR("Given port %d: Missing layer 1 NT-mode protocol.\n", port);
1497 if (stinf->pid.protocol[2])
1499 PERROR("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
1505 if (stinf->pid.protocol[1] == 0)
1507 PERROR("Given port %d: Missing layer 1 protocol.\n", port);
1510 if (stinf->pid.protocol[2] == 0)
1512 PERROR("Given port %d: Missing layer 2 protocol.\n", port);
1515 if (stinf->pid.protocol[3] == 0)
1517 PERROR("Given port %d: Missing layer 3 protocol.\n", port);
1521 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
1523 case ISDN_PID_L3_DSS1USER:
1527 PERROR("Given port %d: own protocol 0x%08x", port,stinf->pid.protocol[3]);
1531 if (stinf->pid.protocol[4])
1533 PERROR("Given port %d: Layer 4 protocol not allowed.\n", port);
1538 /* add mISDNport structure */
1539 mISDNportp = &mISDNport_first;
1541 mISDNportp = &mISDNport->next;
1542 mISDNport = (struct mISDNport *)calloc(1, sizeof(struct mISDNport));
1545 PERROR("Cannot alloc mISDNport structure\n");
1549 memset(mISDNport, 0, sizeof(mISDNport));
1550 *mISDNportp = mISDNport;
1552 /* allocate ressources of port */
1553 msg_queue_init(&mISDNport->downqueue);
1554 // SCPY(mISDNport->name, "noname");
1555 mISDNport->portnum = port;
1556 mISDNport->ntmode = nt;
1557 mISDNport->pri = pri;
1558 mISDNport->d_stid = stinf->id;
1559 PDEBUG(DEBUG_ISDN, "d_stid = 0x%x.\n", mISDNport->d_stid);
1560 mISDNport->b_num = stinf->childcnt;
1561 PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
1562 if ((stinf->pid.protocol[2]&ISDN_PID_L2_DF_PTP) || (nt&&ptp) || pri)
1564 PDEBUG(DEBUG_ISDN, "Port is point-to-point.\n");
1565 mISDNport->ptp = ptp = 1;
1568 PDEBUG(DEBUG_ISDN, "Port is forced to point-to-multipoint.\n");
1569 mISDNport->ptp = ptp = 0;
1573 while(i < stinf->childcnt)
1575 mISDNport->b_stid[i] = stinf->child[i];
1576 PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
1579 memset(&li, 0, sizeof(li));
1580 UCPY(&li.name[0], (nt)?"net l2":"pbx l4");
1583 li.pid.protocol[nt?2:4] = (nt)?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1584 li.pid.layermask = ISDN_LAYER((nt?2:4));
1585 li.st = mISDNport->d_stid;
1586 ret = mISDN_new_layer(mISDNdevice, &li);
1589 PERROR("Cannot add layer %d of port %d (ret %d)\n", nt?2:4, port, ret);
1591 mISDNport_close(mISDNport);
1594 mISDNport->upper_id = li.id;
1595 ret = mISDN_register_layer(mISDNdevice, mISDNport->d_stid, mISDNport->upper_id);
1598 PERROR("Cannot register layer %d of port %d\n", nt?2:4, port);
1601 mISDNport->lower_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?1:3); // id of lower layer (nt=1, te=3)
1602 if (mISDNport->lower_id < 0)
1604 PERROR("Cannot get layer(%d) id of port %d\n", nt?1:3, port);
1607 mISDNport->upper_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?2:4); // id of uppermost layer (nt=2, te=4)
1608 if (mISDNport->upper_id < 0)
1610 PERROR("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
1613 PDEBUG(DEBUG_ISDN, "Layer %d of port %d added.\n", nt?2:4, port);
1615 /* if ntmode, establish L1 to send the tei removal during start */
1616 if (mISDNport->ntmode)
1620 act.prim = PH_ACTIVATE | REQUEST;
1621 act.addr = mISDNport->upper_id | FLG_MSG_DOWN;
1622 printf("UPPER ID 0x%x, addr 0x%x\n",mISDNport->upper_id, act.addr);
1625 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1626 usleep(10000); /* to be sure, that l1 is up */
1629 /* create nst (nt-mode only) */
1632 mgr = &mISDNport->mgr;
1633 nst = &mISDNport->nst;
1638 nst->l3_manager = stack2manager_nt; /* messages from nt-mode */
1639 nst->device = mISDNdevice;
1641 nst->d_stid = mISDNport->d_stid;
1643 nst->feature = FEATURE_NET_HOLD;
1645 nst->feature |= FEATURE_NET_PTP;
1647 nst->feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1650 while(i < mISDNport->b_num)
1652 nst->b_stid[i] = mISDNport->b_stid[i];
1656 nst->l1_id = mISDNport->lower_id;
1657 nst->l2_id = mISDNport->upper_id;
1660 msg_queue_init(&nst->down_queue);
1666 /* if te-mode, query state link */
1667 if (!mISDNport->ntmode)
1671 PDEBUG(DEBUG_ISDN, "sending short status request for port %d.\n", port);
1672 act.prim = MGR_SHORTSTATUS | REQUEST;
1673 act.addr = mISDNport->upper_id | MSG_BROADCAST;
1674 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
1676 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1678 /* if ptp AND te-mode, pull up the link */
1679 if (mISDNport->ptp && !mISDNport->ntmode)
1683 act.prim = DL_ESTABLISH | REQUEST;
1684 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 4 | FLG_MSG_DOWN;
1687 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1689 /* if ptp AND nt-mode, pull up the link */
1690 if (mISDNport->ptp && mISDNport->ntmode)
1694 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1695 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1698 /* initially, we assume that the link is down, exept for nt-ptmp */
1699 mISDNport->l2link = (mISDNport->ntmode && !mISDNport->ptp)?1:0;
1701 PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
1703 /* add all bchannel layers */
1705 while(i < mISDNport->b_num)
1707 /* create new layer */
1708 PDEBUG(DEBUG_BCHANNEL, "creating bchannel %d (index %d).\n" , i+1+(i>=15), i);
1709 memset(&li, 0, sizeof(li));
1710 memset(&pid, 0, sizeof(pid));
1713 li.st = mISDNport->b_stid[i];
1714 UCPY(li.name, "B L4");
1715 li.pid.layermask = ISDN_LAYER((4));
1716 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
1717 ret = mISDN_new_layer(mISDNdevice, &li);
1721 PERROR("mISDN_new_layer() failed to add bchannel %d (index %d)\n", i+1+(i>=15), i);
1724 mISDNport->b_addr[i] = li.id;
1727 goto failed_new_layer;
1729 PDEBUG(DEBUG_BCHANNEL, "new layer (b_addr=0x%x)\n", mISDNport->b_addr[i]);
1731 /* create new stack */
1732 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1733 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1734 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1735 pid.protocol[4] = ISDN_PID_L4_B_USER;
1736 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1737 ret = mISDN_set_stack(mISDNdevice, mISDNport->b_stid[i], &pid);
1741 PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel (index %d) stid=0x%x\n", ret, i, mISDNport->b_stid[i]);
1742 mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i], MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1743 mISDNport->b_addr[i] = 0;
1744 // mISDNport->b_addr_low[i] = 0;
1747 ret = mISDN_get_setstack_ind(mISDNdevice, mISDNport->b_addr[i]);
1752 mISDNport->b_addr[i] = mISDN_get_layerid(mISDNdevice, mISDNport->b_stid[i], 4);
1753 if (!mISDNport->b_addr[i])
1755 /* deactivate bchannel if already enabled due to crash */
1756 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d) as a precaution.\n", i);
1757 dact.prim = DL_RELEASE | REQUEST;
1758 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
1761 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
1765 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);
1766 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);
1772 * function to free ALL cards (ports)
1774 void mISDNport_close_all(void)
1776 /* free all ports */
1777 while(mISDNport_first)
1778 mISDNport_close(mISDNport_first);
1782 * free only one port
1784 void mISDNport_close(struct mISDNport *mISDNport)
1786 struct mISDNport **mISDNportp;
1788 classs PmISDN *pmISDN;
1790 unsigned char buf[32];
1793 /* remove all port instance that are linked to this mISDNport */
1797 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN)
1799 pmISDN = (class PmISDN)*port;
1800 if (pmISDN->p_m_mISDNport)
1802 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", pnISDN->p_name, mISDNport->portnum);
1809 printlog("closing port %d\n", mISDNport->portnum);
1811 /* free bchannels */
1813 while(i < mISDNport->b_num)
1815 bchannel_deactivate(mISDNport, i);
1816 PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
1817 if (mISDNport->b_stid[i])
1819 mISDN_clear_stack(mISDNdevice, mISDNport->b_stid[i]);
1820 if (mISDNport->b_addr[i])
1821 mISDN_write_frame(mISDNdevice, buf, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1826 /* free ressources of port */
1827 msg_queue_purge(&mISDNport->downqueue);
1830 if (mISDNport->ntmode)
1832 nst = &mISDNport->nst;
1833 if (nst->manager) /* to see if initialized */
1835 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");
1836 cleanup_Isdnl3(nst);
1837 cleanup_Isdnl2(nst);
1840 msg_queue_purge(&nst->down_queue);
1841 if (nst->phd_down_msg)
1842 free(nst->phd_down_msg);
1846 PDEBUG(DEBUG_BCHANNEL, "freeing d-stack.\n");
1847 if (mISDNport->d_stid)
1849 // mISDN_clear_stack(mISDNdevice, mISDNport->d_stid);
1850 if (mISDNport->lower_id)
1851 mISDN_write_frame(mISDNdevice, buf, mISDNport->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1854 /* remove from list */
1855 mISDNportp = &mISDNport_first;
1858 if (*mISDNportp == mISDNport)
1860 *mISDNportp = (*mISDNportp)->next;
1863 mISDNportp = &((*mISDNportp)->next);
1868 PERROR("software error, mISDNport not in list\n");
1872 memset(mISDNport, 0, sizeof(struct mISDNport));
1876 /* close mISDNdevice, if no port */
1877 if (mISDNdevice>=0 && mISDNport_first==NULL)
1880 mISDN_write_frame(mISDNdevice, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
1882 mISDN_close(mISDNdevice);
1884 PDEBUG(DEBUG_ISDN, "mISDN device closed.\n");
1890 * global function to show all available isdn ports
1892 void mISDN_port_info(void)
1896 int useable, nt, pri;
1897 unsigned char buff[1025];
1898 iframe_t *frm = (iframe_t *)buff;
1899 stack_info_t *stinf;
1903 if ((device = mISDN_open()) < 0)
1905 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));
1909 /* get number of stacks */
1911 ii = mISDN_get_stack_count(device);
1915 printf("Found no card. Please be sure to load card drivers.\n");
1918 /* loop the number of cards and get their info */
1921 err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
1924 fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
1927 stinf = (stack_info_t *)&frm->data.p;
1932 /* output the port info */
1933 printf("Port %2d: ", i);
1934 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1936 case ISDN_PID_L0_TE_S0:
1937 printf("TE-mode BRI S/T interface line (for phone lines)");
1939 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
1940 printf(" HFC multiport card");
1943 case ISDN_PID_L0_NT_S0:
1945 printf("NT-mode BRI S/T interface port (for phones)");
1947 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
1948 printf(" HFC multiport card");
1951 case ISDN_PID_L0_TE_U:
1952 printf("TE-mode BRI U interface line");
1954 case ISDN_PID_L0_NT_U:
1956 printf("NT-mode BRI U interface port");
1958 case ISDN_PID_L0_TE_UP2:
1959 printf("TE-mode BRI Up2 interface line");
1961 case ISDN_PID_L0_NT_UP2:
1963 printf("NT-mode BRI Up2 interface port");
1965 case ISDN_PID_L0_TE_E1:
1967 printf("TE-mode PRI E1 interface line (for phone lines)");
1969 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
1970 printf(" HFC-E1 card");
1973 case ISDN_PID_L0_NT_E1:
1976 printf("NT-mode PRI E1 interface port (for phones)");
1978 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
1979 printf(" HFC-E1 card");
1984 printf("unknown type 0x%08x",stinf->pid.protocol[0]);
1990 if (stinf->pid.protocol[1] == 0)
1993 printf(" -> Missing layer 1 NT-mode protocol.\n");
1996 while(p <= MAX_LAYER_NR) {
1997 if (stinf->pid.protocol[p])
2000 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]);
2007 printf(" -> Interface is Point-To-Point (PRI).\n");
2009 printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
2013 if (stinf->pid.protocol[1] == 0)
2016 printf(" -> Missing layer 1 protocol.\n");
2018 if (stinf->pid.protocol[2] == 0)
2021 printf(" -> Missing layer 2 protocol.\n");
2023 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP)
2025 printf(" -> Interface is Poin-To-Point.\n");
2027 if (stinf->pid.protocol[3] == 0)
2030 printf(" -> Missing layer 3 protocol.\n");
2033 printf(" -> Protocol: ");
2034 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
2036 case ISDN_PID_L3_DSS1USER:
2037 printf("DSS1 (Euro ISDN)");
2042 printf("unknown protocol 0x%08x",stinf->pid.protocol[3]);
2047 while(p <= MAX_LAYER_NR) {
2048 if (stinf->pid.protocol[p])
2051 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]);
2056 printf(" - %d B-channels\n", stinf->childcnt);
2059 printf(" * Port NOT useable for PBX\n");
2061 printf("--------\n");
2068 if ((err = mISDN_close(device)))
2070 fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err));