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
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(int)*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(int)+c2_len;
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);
266 todo: wer startet/stoppt eigentlich den audio-transmit
270 static void bchannel_deactivate(struct mISDNport *mISDNport, int i)
274 if (mISDNport->b_state[i] == B_STATE_ACTIVE)
276 /* reset dsp features */
277 if (mISDNport->b_port[i])
279 if (mISDNport->b_port[i]->p_m_delay)
281 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset txdata from txdata=%d.\n", mISDNport->b_port[i]->p_m_txdata);
282 ph_control(mISDNport->b_addr[i], CMX_TXDATA_OFF, 0);
284 if (mISDNport->b_port[i]->p_m_delay)
286 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset delay from delay=%d.\n", mISDNport->b_port[i]->p_m_delay);
287 ph_control(mISDNport->b_addr[i], CMX_JITTER, 0);
289 if (mISDNport->b_port[i]->p_m_txvol)
291 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_txvol);
292 ph_control(mISDNport->b_addr[i], VOL_CHANGE_TX, 0);
294 if (mISDNport->b_port[i]->p_m_rxvol)
296 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_rxvol);
297 ph_control(mISDNport->b_addr[i], VOL_CHANGE_RX, 0);
299 if (mISDNport->b_port[i]->p_m_conf)
301 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we release conference from conf=%d.\n", mISDNport->b_port[i]->p_m_conf);
302 ph_control(mISDNport->b_addr[i], CMX_CONF_SPLIT, 0);
304 if (mISDNport->b_port[i]->p_m_echo)
306 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset echo from echo=%d.\n", mISDNport->b_port[i]->p_m_echo);
307 ph_control(mISDNport->b_addr[i], CMX_ECHO_OFF, 0);
309 if (mISDNport->b_port[i]->p_m_tone)
311 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tone from tone=%d.\n", mISDNport->b_port[i]->p_m_tone);
312 ph_control(mISDNport->b_addr[i], TONE_PATT_OFF, 0);
314 if (mISDNport->b_port[i]->p_m_rxoff)
316 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rxoff from rxoff=%d.\n", mISDNport->b_port[i]->p_m_rxoff);
317 ph_control(mISDNport->b_addr[i], CMX_RECEIVE_ON, 0);
320 if (mISDNport->b_port[i]->p_m_txmix)
322 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset txmix from txmix=%d.\n", mISDNport->b_port[i]->p_m_txmix);
323 ph_control(mISDNport->b_addr[i], CMX_MIX_OFF, 0);
326 if (mISDNport->b_port[i]->p_m_dtmf)
328 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset dtmf from dtmf=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
329 ph_control(mISDNport->b_addr[i], DTMF_TONE_STOP, 0);
331 if (mISDNport->b_port[i]->p_m_crypt)
333 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset crypt from crypt=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
334 ph_control(mISDNport->b_addr[i], BF_DISABLE, 0);
337 /* deactivate bchannel */
338 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d), because currently active.\n", i);
339 dact.prim = DL_RELEASE | REQUEST;
340 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
343 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
344 mISDNport->b_state[i] = B_STATE_DEACTIVATING;
351 * check for available channel and reserve+set it.
352 * give channel number or SEL_CHANNEL_ANY or SEL_CHANNEL_NO
354 * returns -(cause value) or x = channel x or 0 = no channel
356 denke ans aktivieren und deaktivieren
357 int PmISDN::seize_bchannel(int channel, int exclusive)
361 /* the channel is what we have */
362 if (p_m_b_channel == channel)
365 /* if channel already in use, release it */
370 if (channel==CHANNEL_NO || channel==0)
373 /* is channel in range ? */
375 || (channel>p_m_mISDNport->b_num && channel<16)
376 || ((channel-1)>p_m_mISDNport->b_num && channel>16)) /* channel-1 because channel 16 is not counted */
377 return(-6); /* channel unacceptable */
379 /* request exclusive channel */
380 if (exclusive && channel>0)
382 i = channel-1-(channel>16);
383 if (p_m_mISDNport->b_port[i])
384 return(-44); /* requested channel not available */
388 /* ask for channel */
391 i = channel-1-(channel>16);
392 if (p_m_mISDNport->b_port[i] == NULL)
396 /* search for channel */
398 while(i < p_m_mISDNport->b_num)
400 if (!p_m_mISDNport->b_port[i])
402 channel = i+1+(i>=15);
407 return(-34); /* no free channel */
411 p_m_mISDNport->b_port[i] = this;
413 p_m_b_channel = channel;
414 p_m_b_exclusive = exclusive;
415 p_m_b_stid = p_m_mISDNport->b_stid[i];
416 p_m_b_addr = p_m_mISDNport->b_addr[i];
419 /* reserve channel */
420 if (p_m_b_reserve == 0) // already reserved
423 mISDNport->b_reserved++;
426 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) seizing bchannel %d (index %d)\n", p_name, channel, i);
432 * drop reserved channel and unset it.
434 void PmISDN::drop_bchannel(void)
436 /* unreserve channel */
438 p_m_mISDNport->b_reserved--;
445 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) dropping bchannel\n", p_name);
447 p_m_mISDNport->b_port[p_m_b_index] = NULL;
457 * data is to be transmitted to the remote
459 wech void Port::transmit(unsigned char *buffer, int length, int tonelength)
461 /* send tone data to isdn device only if we have data, otherwhise we send nothing */
462 if (tonelength>0 && (p_tone_fh>=0 || p_tone_fetched || p_m_crypt_msg_loops))
464 unsigned char buf[mISDN_HEADER_LEN+tonelength];
465 iframe_t *frm = (iframe_t *)buf;
467 if (p_m_crypt_msg_loops)
469 /* send pending message */
472 tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
475 memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
476 p_m_crypt_msg_current += tosend;
477 if (p_m_crypt_msg_current == p_m_crypt_msg_len)
479 p_m_crypt_msg_current = 0;
480 p_m_crypt_msg_loops--;
483 frm->prim = DL_DATA | REQUEST;
484 frm->addr = p_m_b_addr | FLG_MSG_DOWN;
486 frm->len = tonelength;
487 memcpy(&frm->data.p, buffer, tonelength);
488 mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
490 if (p_debug_nothingtosend)
492 p_debug_nothingtosend = 0;
493 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
497 if (!p_debug_nothingtosend)
499 p_debug_nothingtosend = 1;
500 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
509 int PmISDN::handler(void)
511 struct message *message;
514 if ((ret = Port::handler()))
517 /* send tone data to isdn device only if we have data */
518 if (p_tone_fh>=0 || p_tone_fetched || p_m_crypt_msg_loops)
520 /* calculate how much to transmit */
523 elapsed = ISDN_PRELOAD << 3; /* preload for the first time */
526 elapsed = 1000 * (now_tv.sec - p_last_tv_sec)
527 + (now_tv.usec/1000) - p_last_tv_msec;
528 /* gap was greater preload, so only fill up to preload level */
529 if (elapsed > ISDN_PRELOAD)
531 elapsed = ISDN_PRELOAD << 3
534 if (elapsed >= ISDN_TRANSMIT)
536 unsigned char buf[mISDN_HEADER_LEN+(ISDN_PRELOAD<<3)];
537 iframe_t *frm = (iframe_t *)buf;
539 length = read_audio(buffer, elapsed);
540 p_last_tv_sec = now_tv.sec;
541 p_last_tv_msec = now_tv.usec/1000;
543 if (p_m_crypt_msg_loops)
545 /* send pending message */
548 tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
551 memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
552 p_m_crypt_msg_current += tosend;
553 if (p_m_crypt_msg_current == p_m_crypt_msg_len)
555 p_m_crypt_msg_current = 0;
556 p_m_crypt_msg_loops--;
559 frm->prim = DL_DATA | REQUEST;
560 frm->addr = p_m_b_addr | FLG_MSG_DOWN;
563 memcpy(&frm->data.p, buffer, length);
564 mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
566 if (p_debug_nothingtosend)
568 p_debug_nothingtosend = 0;
569 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
575 p_last_tv.sec = p_last_tv.msec = 0; /* flag that we don't transmit data */
576 if (!cwp_debug_nothingtosend)
578 p_debug_nothingtosend = 1;
579 PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
583 // NOTE: deletion is done by the child class
585 /* handle timeouts */
588 if (p_m_timer+p_m_timeout < now_d)
590 PDEBUG(DEBUG_ISDN, "(%s) timeout after %d seconds detected (state=%d).\n", p_name, p_m_timeout, p_state);
592 /* send timeout to endpoint */
593 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TIMEOUT);
594 message->param.state = p_state;
595 message_put(message);
600 return(0); /* nothing done */
605 * whenever we get audio data from bchannel, we process it here
607 void PmISDN::bchannel_receive(iframe_t *frm)
609 unsigned char *data_temp;
610 unsigned long length_temp;
611 struct message *message;
614 long long jitter_now;
619 // iframe_t rsp; /* response to possible indication */
621 #warning BCHANNEL-DEBUG
623 // check if we are part of all ports */
624 class Port *port = port_first;
633 PERROR_RUNTIME("**************************************************\n");
634 PERROR_RUNTIME("*** BCHANNEL-DEBUG: !this! is not in list of ports\n");
635 PERROR_RUNTIME("**************************************************\n");
642 if (frm->prim == (PH_CONTROL | INDICATION))
644 cont = *((unsigned long *)&frm->data.p);
645 // PDEBUG(DEBUG_PORT, "PmISDN(%s) received a PH_CONTROL INDICATION 0x%x\n", p_name, cont);
646 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
648 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DTMF);
649 message->param.dtmf = cont & DTMF_TONE_MASK;
650 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL DTMF digit '%c'\n", p_name, message->param.dtmf);
651 message_put(message);
656 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
657 message->param.crypt.type = CC_ERROR_IND;
658 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL reject of blowfish.\n", p_name);
659 message_put(message);
663 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
664 message->param.crypt.type = CC_ACTBF_CONF;
665 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL accept of blowfish.\n", p_name);
666 message_put(message);
672 /* calls will not process any audio data unless
673 * the call is connected OR tones feature is enabled.
675 if (p_state!=PORT_STATE_CONNECT
676 && !p_m_mISDNport->is_tones)
680 /* the bearer capability must be audio in order to send and receive
681 * audio prior or after connect.
683 if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
687 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
690 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
694 /* randomize and listen to crypt message if enabled */
695 if (p_m_crypt_listen)
697 /* the noisy randomizer */
698 p = (unsigned char *)&frm->data.p;
701 mISDN_rand[mISDN_rand_count & 0xff] += *p++;
703 cryptman_listen_bch((unsigned char *)&frm->data.p, frm->len);
706 p = (unsigned char *)&frm->data.p;
708 /* send data to epoint */
709 if (p_m_calldata && ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
711 //printf("we are port %s and sending to epoint %d\n", p_m_cardname, p_epoint->serial);
712 length_temp = frm->len;
716 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
717 message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
718 memcpy(message->param.data.data, data_temp, message->param.data.len);
719 message->param.data.compressed = 1;
720 message->param.data.port_id = p_serial;
721 message->param.data.port_type = p_type;
722 message_put(message);
723 if (length_temp <= sizeof(message->param.data.data))
725 data_temp += sizeof(message->param.data.data);
726 length_temp -= sizeof(message->param.data.data);
730 /* response to the data indication */
731 rsp.prim = frm->prim & 0xfffffffc | RESPONSE;
732 rsp.addr = frm->addr & INST_ID_MASK | FLG_MSG_DOWN;
733 rsp.dinfo = frm->dinfo;
735 mISDN_write(mISDNdevice, &rsp, mISDN_HEADER_LEN+rsp.len, TIMEOUT_1SEC);
736 //PDEBUG(DEBUG_ISDN, "written %d bytes.\n", length);
744 void PmISDN::set_echotest(int echo)
746 if (p_m_echo != echo)
749 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
751 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
752 ph_control(p_m_b_addr, p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
759 void PmISDN::set_tone(char *dir, char *tone)
765 PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
772 /* check if we NOT really have to use a dsp-tone */
773 if (!options.dsptones)
777 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
779 PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
780 ph_control(p_m_b_addr, TONE_PATT_OFF, 0);
783 Port::set_tone(dir, tone);
789 /* now we USE dsp-tone, convert name */
790 else if (!strcmp(tone, "dialtone"))
792 switch(options.dsptones) {
793 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
794 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
795 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
797 } else if (!strcmp(tone, "dialpbx"))
799 switch(options.dsptones) {
800 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
801 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
802 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
804 } else if (!strcmp(tone, "ringing"))
806 switch(options.dsptones) {
807 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
808 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
809 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
811 } else if (!strcmp(tone, "ringpbx"))
813 switch(options.dsptones) {
814 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
815 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
816 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
818 } else if (!strcmp(tone, "busy"))
821 switch(options.dsptones) {
822 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
823 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
824 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
826 } else if (!strcmp(tone, "release"))
829 switch(options.dsptones) {
830 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
831 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
832 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
834 } else if (!strcmp(tone, "cause_10"))
836 else if (!strcmp(tone, "cause_11"))
838 else if (!strcmp(tone, "cause_22"))
840 switch(options.dsptones) {
841 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
842 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
843 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
845 } else if (!strncmp(tone, "cause_", 6))
846 id = TONE_SPECIAL_INFO;
850 /* if we have a tone that is not supported by dsp */
851 if (id==TONE_OFF && tone[0])
859 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
861 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
862 ph_control(p_m_b_addr, p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone);
864 /* turn user-space tones off in cases of no tone OR dsp tone */
865 Port::set_tone("",NULL);
869 /* MESSAGE_mISDNSIGNAL */
870 //extern struct message *dddebug;
871 void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union parameter *param)
873 switch(param->mISDNsignal.message)
875 case mISDNSIGNAL_VOLUME:
876 if (p_m_txvol != param->mISDNsignal.txvol)
878 p_m_txvol = param->mISDNsignal.txvol;
879 PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
881 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
882 ph_control(p_m_b_addr, VOL_CHANGE_TX, p_m_txvol);
884 if (p_m_rxvol != param->mISDNsignal.rxvol)
886 p_m_rxvol = param->mISDNsignal.rxvol;
887 PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
889 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
890 ph_control(p_m_b_addr, VOL_CHANGE_RX, p_m_rxvol);
894 case mISDNSIGNAL_CONF:
895 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
896 //tone if (!p_m_tone && p_m_conf!=param->mISDNsignal.conf)
897 if (p_m_conf != param->mISDNsignal.conf)
899 p_m_conf = param->mISDNsignal.conf;
900 PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
902 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
903 ph_control(p_m_b_addr, (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf);
905 /* we must set, even if currently tone forbids conf */
906 p_m_conf = param->mISDNsignal.conf;
907 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
910 case mISDNSIGNAL_CALLDATA:
911 if (p_m_calldata != param->mISDNsignal.calldata)
913 p_m_calldata = param->mISDNsignal.calldata;
914 PDEBUG(DEBUG_BCHANNEL, "we change to calldata=%d.\n", p_m_calldata);
920 case mISDNSIGNAL_NODATA:
921 p_m_nodata = param->mISDNsignal.nodata;
922 if (p_m_txmix == p_m_nodata) /* txmix != !nodata */
924 p_m_txmix = !p_m_nodata;
925 PDEBUG(DEBUG_BCHANNEL, "we change mix mode to txmix=%d (nodata=%d).\n", p_m_txmix, p_m_nodata);
927 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
928 ph_control(p_m_b_addr, p_m_txmix?CMX_MIX_ON:CMX_MIX_OFF, 0);
934 case mISDNSIGNAL_RXOFF:
935 if (p_m_rxoff != param->mISDNsignal.rxoff)
937 p_m_rxoff = param->mISDNsignal.rxoff;
938 PDEBUG(DEBUG_BCHANNEL, "we change receive mode to rxoff=%d.\n", p_m_rxoff);
940 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
941 ph_control(p_m_b_addr, p_m_rxoff?CMX_RECEIVE_OFF:CMX_RECEIVE_ON, 0);
945 case mISDNSIGNAL_DTMF:
946 if (p_m_dtmf != param->mISDNsignal.dtmf)
948 p_m_dtmf = param->mISDNsignal.dtmf;
949 PDEBUG(DEBUG_BCHANNEL, "we change dtmf mode to dtmf=%d.\n", p_m_dtmf);
951 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
952 ph_control(p_m_b_addr, p_m_dtmf?DTMF_TONE_START:DTMF_TONE_STOP, 0);
958 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
963 void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parameter *param)
965 struct message *message;
967 switch(param->crypt.type)
969 case CC_ACTBF_REQ: /* activate blowfish */
971 p_m_crypt_key_len = param->crypt.len;
972 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key))
974 PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
975 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
976 message->param.crypt.type = CC_ERROR_IND;
977 message_put(message);
980 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
982 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
984 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
985 ph_control_block(p_m_b_addr, p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len);
988 case CC_DACT_REQ: /* deactivate session encryption */
993 case CR_LISTEN_REQ: /* start listening to messages */
994 p_m_crypt_listen = 1;
995 p_m_crypt_listen_state = 0;
998 case CR_UNLISTEN_REQ: /* stop listening to messages */
999 p_m_crypt_listen = 0;
1002 case CR_MESSAGE_REQ: /* send message */
1003 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
1004 if (!p_m_crypt_msg_len)
1006 PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
1009 p_m_crypt_msg_current = 0; /* reset */
1010 p_m_crypt_msg_loops = 3; /* enable */
1012 /* disable txmix, or we get corrupt data due to audio process */
1015 PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
1016 ph_control(p_m_b_addr, CMX_MIX_OFF, 0);
1022 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
1028 * endpoint sends messages to the port
1030 int PmISDN::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
1032 if (Port::message_epoint(epoint_id, message_id, param))
1037 case MESSAGE_mISDNSIGNAL: /* user command */
1038 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
1039 message_mISDNsignal(epoint_id, message_id, param);
1042 case MESSAGE_CRYPT: /* crypt control command */
1043 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
1044 message_crypt(epoint_id, message_id, param);
1053 * transmit function for data to mISDN b-channel
1055 void Port::transmit(unsigned char *buffer, int length)
1061 * main loop for processing messages from mISDN device
1063 int mISDN_handler(void)
1068 struct mISDNport *mISDNport;
1069 class PmISDN *isdnport;
1072 mISDNuser_head_t *hh;
1075 /* the que avoids loopbacks when replying to stack after receiving
1077 mISDNport = mISDNport_first;
1080 /* process turning on/off rx */
1082 while(i < mISDNport->b_num)
1084 if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
1086 isdnport=mISDNport->b_port[i];
1087 if (isdnport->p_m_calldata || isdnport->p_m_crypt_msg_loops || isdnport->p_m_crypt_listen || isdnport->p_record)
1089 /* rx IS required */
1090 if (isdnport->p_m_rxoff)
1093 isdnport->p_m_rxoff = 0;
1094 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
1095 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_ON, 0);
1100 /* rx NOT required */
1101 if (!isdnport->p_m_rxoff)
1104 isdnport->p_m_rxoff = 1;
1105 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
1106 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_OFF, 0);
1114 if (mISDNport->l1timeout && now>mISDNport->l1timeout)
1116 PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
1117 mISDNport->l1timeout = 0;
1120 if (mISDNport->l2establish)
1122 if (now-mISDNport->l2establish > 5)
1124 if (mISDNport->ntmode)
1126 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link NT portnum=%d.\n", mISDNport->portnum);
1127 time(&mISDNport->l2establish);
1129 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1130 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1133 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link TE portnum=%d.\n", mISDNport->portnum);
1134 time(&mISDNport->l2establish);
1137 act.prim = DL_ESTABLISH | REQUEST;
1138 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
1141 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1146 if ((dmsg = msg_dequeue(&mISDNport->downqueue)))
1148 if (mISDNport->ntmode)
1150 hh = (mISDNuser_head_t *)dmsg->data;
1151 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);
1152 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1156 frm = (iframe_t *)dmsg->data;
1157 frm->addr = mISDNport->upper_id | FLG_MSG_DOWN;
1158 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
1159 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);
1160 mISDN_write(mISDNdevice, dmsg->data, dmsg->len, TIMEOUT_1SEC);
1165 mISDNport = mISDNport->next;
1168 /* get message from kernel */
1169 if (!(msg = alloc_msg(MAX_MSG_SIZE)))
1171 ret = mISDN_read(mISDNdevice, msg->data, MAX_MSG_SIZE, 0);
1175 if (errno == EAGAIN)
1177 PERROR("FATAL ERROR: failed to do mISDN_read()\n");
1183 // printf("%s: ERROR: mISDN_read() returns nothing\n");
1187 frm = (iframe_t *)msg->data;
1192 case MGR_INITTIMER | CONFIRM:
1193 case MGR_ADDTIMER | CONFIRM:
1194 case MGR_DELTIMER | CONFIRM:
1195 case MGR_REMOVETIMER | CONFIRM:
1196 // if (options.deb & DEBUG_ISDN)
1197 // PDEBUG(DEBUG_ISDN, "timer-confirm\n");
1203 mISDNport = mISDNport_first;
1206 if ((frm->prim==(MGR_TIMER | INDICATION)) && mISDNport->ntmode)
1208 itimer_t *it = mISDNport->nst.tlist;
1213 if (it->id == (int)frm->addr)
1219 mISDN_write_frame(mISDNdevice, msg->data, mISDNport->upper_id | FLG_MSG_DOWN,
1220 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1222 PDEBUG(DEBUG_ISDN, "timer-indication %s port %d it=%p\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, it);
1223 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1224 ret = it->function(it->data);
1227 /* we will continue here because we have a timer for a different mISDNport */
1229 //printf("comparing frm->addr %x with upper_id %x\n", frm->addr, mISDNport->upper_id);
1230 if ((frm->addr&STACK_ID_MASK) == (unsigned int)(mISDNport->upper_id&STACK_ID_MASK))
1235 case MGR_SHORTSTATUS | INDICATION:
1236 case MGR_SHORTSTATUS | CONFIRM:
1237 switch(frm->dinfo) {
1238 case SSTATUS_L1_ACTIVATED:
1239 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1241 case SSTATUS_L1_DEACTIVATED:
1242 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1244 case SSTATUS_L2_ESTABLISHED:
1245 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_ESTABLISHED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1247 case SSTATUS_L2_RELEASED:
1248 PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_RELEASED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1253 case PH_ACTIVATE | CONFIRM:
1254 case PH_ACTIVATE | INDICATION:
1255 PDEBUG(DEBUG_ISDN, "Received PH_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1256 if (mISDNport->ntmode)
1258 mISDNport->l1link = 1;
1259 setup_queue(mISDNport, 1);
1263 mISDNport->l1link = 1;
1264 setup_queue(mISDNport, 1);
1267 case PH_DEACTIVATE | CONFIRM:
1268 case PH_DEACTIVATE | INDICATION:
1269 PDEBUG(DEBUG_ISDN, "Received PH_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1270 if (mISDNport->ntmode)
1272 mISDNport->l1link = 0;
1273 setup_queue(mISDNport, 0);
1277 mISDNport->l1link = 0;
1278 setup_queue(mISDNport, 0);
1281 case PH_CONTROL | CONFIRM:
1282 case PH_CONTROL | INDICATION:
1283 PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1286 case DL_ESTABLISH | INDICATION:
1287 case DL_ESTABLISH | CONFIRM:
1288 // PDEBUG(DEBUG_ISDN, "addr 0x%x established data link (DL) TE portnum=%d (DL_ESTABLISH)\n", frm->addr, mISDNport->portnum);
1289 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1291 // if (mISDNport->ntmode)
1293 if (mISDNport->l2establish)
1295 mISDNport->l2establish = 0;
1296 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1298 mISDNport->l2link = 1;
1301 case DL_RELEASE | INDICATION:
1302 case DL_RELEASE | CONFIRM:
1303 // PDEBUG(DEBUG_ISDN, "addr 0x%x released data link (DL) TE portnum=%d (DL_RELEASE)\n", frm->addr, mISDNport->portnum);
1304 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1306 // if (mISDNport->ntmode)
1308 mISDNport->l2link = 0;
1311 time(&mISDNport->l2establish);
1312 PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
1318 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);
1319 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM))
1321 PERROR("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
1324 if (mISDNport->ntmode)
1326 /* l1-data enters the nt-mode library */
1327 nst = &mISDNport->nst;
1328 if (nst->l1_l2(nst, msg))
1333 /* l3-data is sent to pbx */
1334 if (stack2manager_te(mISDNport, msg))
1341 //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));
1342 /* check if child, and if parent stack match */
1343 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)))
1348 /* we don't care about confirms, we use rx data to sync tx */
1349 case PH_DATA | CONFIRM:
1350 case DL_DATA | CONFIRM:
1353 /* we receive audio data, we respond to it AND we send tones */
1354 case PH_DATA | INDICATION:
1355 case DL_DATA | INDICATION:
1356 case PH_CONTROL | INDICATION:
1358 while(i < mISDNport->b_num)
1360 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1364 if (i == mISDNport->b_num)
1366 PERROR("unhandled b-message (address 0x%x).\n", frm->addr);
1369 if (mISDNport->b_port[i])
1371 //PERROR("port sech: %s data\n", mISDNport->b_port[i]->p_name);
1372 mISDNport->b_port[i]->bchannel_receive(frm);
1374 PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (address 0x%x), ignoring.\n", frm->addr);
1377 case PH_ACTIVATE | INDICATION:
1378 case DL_ESTABLISH | INDICATION:
1379 case PH_ACTIVATE | CONFIRM:
1380 case DL_ESTABLISH | CONFIRM:
1381 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
1383 while(i < mISDNport->b_num)
1385 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1389 if (i == mISDNport->b_num)
1391 PERROR("unhandled b-establish (address 0x%x).\n", frm->addr);
1394 mISDNport->b_state[i] = B_STATE_ACTIVE;
1395 if (!mISDNport->b_port[i])
1396 bchannel_deactivate(mISDNport, i);
1398 bchannel_activate(mISDNport, i);
1401 case PH_DEACTIVATE | INDICATION:
1402 case DL_RELEASE | INDICATION:
1403 case PH_DEACTIVATE | CONFIRM:
1404 case DL_RELEASE | CONFIRM:
1405 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
1407 while(i < mISDNport->b_num)
1409 if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1413 if (i == mISDNport->b_num)
1415 PERROR("unhandled b-release (address 0x%x).\n", frm->addr);
1418 mISDNport->b_state[i] = B_STATE_IDLE;
1419 if (mISDNport->b_port[i])
1420 bchannel_activate(mISDNport, i);
1422 bchannel_deactivate(mISDNport, i);
1428 mISDNport = mISDNport->next;
1432 if (frm->prim == (MGR_TIMER | INDICATION))
1433 PERROR("unhandled timer indication message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1435 PERROR("unhandled message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1436 // 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);
1445 * global function to add a new card (port)
1447 struct mISDNport *mISDN_port_open(int port, int ptp, int ptmp)
1450 unsigned char buff[1025];
1451 iframe_t *frm = (iframe_t *)buff;
1452 stack_info_t *stinf;
1453 struct mISDNport *mISDNport, **mISDNportp;
1456 // interface_info_t ii;
1464 /* open mISDNdevice if not already open */
1465 if (mISDNdevice < 0)
1470 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));
1474 PDEBUG(DEBUG_ISDN, "mISDN device opened.\n");
1476 /* create entity for layer 3 TE-mode */
1477 mISDN_write_frame(mISDNdevice, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1478 ret = mISDN_read_frame(mISDNdevice, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
1479 if (ret < (int)mISDN_HEADER_LEN)
1482 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
1485 entity = frm->dinfo & 0xffff;
1488 PDEBUG(DEBUG_ISDN, "our entity for l3-processes is %d.\n", entity);
1491 /* query port's requirements */
1492 cnt = mISDN_get_stack_count(mISDNdevice);
1495 PERROR("Found no card. Please be sure to load card drivers.\n");
1498 if (port>cnt || port<1)
1500 PERROR("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
1503 ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
1506 PERROR("Cannot get stack info for port %d (ret=%d)\n", port, ret);
1509 stinf = (stack_info_t *)&frm->data.p;
1510 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1512 case ISDN_PID_L0_TE_S0:
1513 PDEBUG(DEBUG_ISDN, "TE-mode BRI S/T interface line\n");
1515 case ISDN_PID_L0_NT_S0:
1516 PDEBUG(DEBUG_ISDN, "NT-mode BRI S/T interface port\n");
1519 case ISDN_PID_L0_TE_U:
1520 PDEBUG(DEBUG_ISDN, "TE-mode BRI U interface line\n");
1522 case ISDN_PID_L0_NT_U:
1523 PDEBUG(DEBUG_ISDN, "NT-mode BRI U interface port\n");
1526 case ISDN_PID_L0_TE_UP2:
1527 PDEBUG(DEBUG_ISDN, "TE-mode BRI Up2 interface line\n");
1529 case ISDN_PID_L0_NT_UP2:
1530 PDEBUG(DEBUG_ISDN, "NT-mode BRI Up2 interface port\n");
1533 case ISDN_PID_L0_TE_E1:
1534 PDEBUG(DEBUG_ISDN, "TE-mode PRI E1 interface line\n");
1537 case ISDN_PID_L0_NT_E1:
1538 PDEBUG(DEBUG_ISDN, "LT-mode PRI E1 interface port\n");
1543 PERROR("unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
1549 if (stinf->pid.protocol[1] == 0)
1551 PERROR("Given port %d: Missing layer 1 NT-mode protocol.\n", port);
1554 if (stinf->pid.protocol[2])
1556 PERROR("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
1562 if (stinf->pid.protocol[1] == 0)
1564 PERROR("Given port %d: Missing layer 1 protocol.\n", port);
1567 if (stinf->pid.protocol[2] == 0)
1569 PERROR("Given port %d: Missing layer 2 protocol.\n", port);
1572 if (stinf->pid.protocol[3] == 0)
1574 PERROR("Given port %d: Missing layer 3 protocol.\n", port);
1578 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
1580 case ISDN_PID_L3_DSS1USER:
1584 PERROR("Given port %d: own protocol 0x%08x", port,stinf->pid.protocol[3]);
1588 if (stinf->pid.protocol[4])
1590 PERROR("Given port %d: Layer 4 protocol not allowed.\n", port);
1595 /* add mISDNport structure */
1596 mISDNportp = &mISDNport_first;
1598 mISDNportp = &mISDNport->next;
1599 mISDNport = (struct mISDNport *)calloc(1, sizeof(struct mISDNport));
1602 PERROR("Cannot alloc mISDNport structure\n");
1606 memset(mISDNport, 0, sizeof(mISDNport));
1607 *mISDNportp = mISDNport;
1609 /* allocate ressources of port */
1610 msg_queue_init(&mISDNport->downqueue);
1611 // SCPY(mISDNport->name, "noname");
1612 mISDNport->portnum = port;
1613 mISDNport->ntmode = nt;
1614 mISDNport->pri = pri;
1615 mISDNport->d_stid = stinf->id;
1616 PDEBUG(DEBUG_ISDN, "d_stid = 0x%x.\n", mISDNport->d_stid);
1617 mISDNport->b_num = stinf->childcnt;
1618 PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
1619 if ((stinf->pid.protocol[2]&ISDN_PID_L2_DF_PTP) || (nt&&ptp) || pri)
1621 PDEBUG(DEBUG_ISDN, "Port is point-to-point.\n");
1622 mISDNport->ptp = ptp = 1;
1625 PDEBUG(DEBUG_ISDN, "Port is forced to point-to-multipoint.\n");
1626 mISDNport->ptp = ptp = 0;
1630 while(i < stinf->childcnt)
1632 mISDNport->b_stid[i] = stinf->child[i];
1633 PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
1636 memset(&li, 0, sizeof(li));
1637 UCPY(&li.name[0], (nt)?"net l2":"pbx l4");
1640 li.pid.protocol[nt?2:4] = (nt)?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1641 li.pid.layermask = ISDN_LAYER((nt?2:4));
1642 li.st = mISDNport->d_stid;
1643 ret = mISDN_new_layer(mISDNdevice, &li);
1646 PERROR("Cannot add layer %d of port %d (ret %d)\n", nt?2:4, port, ret);
1648 mISDNport_close(mISDNport);
1651 mISDNport->upper_id = li.id;
1652 ret = mISDN_register_layer(mISDNdevice, mISDNport->d_stid, mISDNport->upper_id);
1655 PERROR("Cannot register layer %d of port %d\n", nt?2:4, port);
1658 mISDNport->lower_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?1:3); // id of lower layer (nt=1, te=3)
1659 if (mISDNport->lower_id < 0)
1661 PERROR("Cannot get layer(%d) id of port %d\n", nt?1:3, port);
1664 mISDNport->upper_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?2:4); // id of uppermost layer (nt=2, te=4)
1665 if (mISDNport->upper_id < 0)
1667 PERROR("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
1670 PDEBUG(DEBUG_ISDN, "Layer %d of port %d added.\n", nt?2:4, port);
1672 /* if ntmode, establish L1 to send the tei removal during start */
1673 if (mISDNport->ntmode)
1677 act.prim = PH_ACTIVATE | REQUEST;
1678 act.addr = mISDNport->upper_id | FLG_MSG_DOWN;
1679 printf("UPPER ID 0x%x, addr 0x%x\n",mISDNport->upper_id, act.addr);
1682 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1683 usleep(10000); /* to be sure, that l1 is up */
1686 /* create nst (nt-mode only) */
1689 mgr = &mISDNport->mgr;
1690 nst = &mISDNport->nst;
1695 nst->l3_manager = stack2manager_nt; /* messages from nt-mode */
1696 nst->device = mISDNdevice;
1698 nst->d_stid = mISDNport->d_stid;
1700 nst->feature = FEATURE_NET_HOLD;
1702 nst->feature |= FEATURE_NET_PTP;
1704 nst->feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1707 while(i < mISDNport->b_num)
1709 nst->b_stid[i] = mISDNport->b_stid[i];
1713 nst->l1_id = mISDNport->lower_id;
1714 nst->l2_id = mISDNport->upper_id;
1717 msg_queue_init(&nst->down_queue);
1723 /* if te-mode, query state link */
1724 if (!mISDNport->ntmode)
1728 PDEBUG(DEBUG_ISDN, "sending short status request for port %d.\n", port);
1729 act.prim = MGR_SHORTSTATUS | REQUEST;
1730 act.addr = mISDNport->upper_id | MSG_BROADCAST;
1731 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
1733 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1735 /* if ptp AND te-mode, pull up the link */
1736 if (mISDNport->ptp && !mISDNport->ntmode)
1740 act.prim = DL_ESTABLISH | REQUEST;
1741 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 4 | FLG_MSG_DOWN;
1744 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1746 /* if ptp AND nt-mode, pull up the link */
1747 if (mISDNport->ptp && mISDNport->ntmode)
1751 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1752 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1755 /* initially, we assume that the link is down, exept for nt-ptmp */
1756 mISDNport->l2link = (mISDNport->ntmode && !mISDNport->ptp)?1:0;
1758 PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
1760 /* add all bchannel layers */
1762 while(i < mISDNport->b_num)
1764 /* create new layer */
1765 PDEBUG(DEBUG_BCHANNEL, "creating bchannel %d (index %d).\n" , i+1+(i>=15), i);
1766 memset(&li, 0, sizeof(li));
1767 memset(&pid, 0, sizeof(pid));
1770 li.st = mISDNport->b_stid[i];
1771 UCPY(li.name, "B L4");
1772 li.pid.layermask = ISDN_LAYER((4));
1773 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
1774 ret = mISDN_new_layer(mISDNdevice, &li);
1778 PERROR("mISDN_new_layer() failed to add bchannel %d (index %d)\n", i+1+(i>=15), i);
1781 mISDNport->b_addr[i] = li.id;
1784 goto failed_new_layer;
1786 PDEBUG(DEBUG_BCHANNEL, "new layer (b_addr=0x%x)\n", mISDNport->b_addr[i]);
1788 /* create new stack */
1789 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1790 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1791 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1792 pid.protocol[4] = ISDN_PID_L4_B_USER;
1793 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1794 ret = mISDN_set_stack(mISDNdevice, mISDNport->b_stid[i], &pid);
1798 PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel (index %d) stid=0x%x\n", ret, i, mISDNport->b_stid[i]);
1799 mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i], MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1800 mISDNport->b_addr[i] = 0;
1801 // mISDNport->b_addr_low[i] = 0;
1804 ret = mISDN_get_setstack_ind(mISDNdevice, mISDNport->b_addr[i]);
1809 mISDNport->b_addr[i] = mISDN_get_layerid(mISDNdevice, mISDNport->b_stid[i], 4);
1810 if (!mISDNport->b_addr[i])
1812 /* deactivate bchannel if already enabled due to crash */
1813 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d) as a precaution.\n", i);
1814 dact.prim = DL_RELEASE | REQUEST;
1815 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
1818 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
1822 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);
1823 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);
1829 * function to free ALL cards (ports)
1831 void mISDNport_close_all(void)
1833 /* free all ports */
1834 while(mISDNport_first)
1835 mISDNport_close(mISDNport_first);
1839 * free only one port
1841 void mISDNport_close(struct mISDNport *mISDNport)
1843 struct mISDNport **mISDNportp;
1845 classs PmISDN *pmISDN;
1847 unsigned char buf[32];
1850 /* remove all port instance that are linked to this mISDNport */
1854 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN)
1856 pmISDN = (class PmISDN)*port;
1857 if (pmISDN->p_m_mISDNport)
1859 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", pnISDN->p_name, mISDNport->portnum);
1866 printlog("closing port %d\n", mISDNport->portnum);
1868 /* free bchannels */
1870 while(i < mISDNport->b_num)
1872 bchannel_deactivate(mISDNport, i);
1873 PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
1874 if (mISDNport->b_stid[i])
1876 mISDN_clear_stack(mISDNdevice, mISDNport->b_stid[i]);
1877 if (mISDNport->b_addr[i])
1878 mISDN_write_frame(mISDNdevice, buf, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1883 /* free ressources of port */
1884 msg_queue_purge(&mISDNport->downqueue);
1887 if (mISDNport->ntmode)
1889 nst = &mISDNport->nst;
1890 if (nst->manager) /* to see if initialized */
1892 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");
1893 cleanup_Isdnl3(nst);
1894 cleanup_Isdnl2(nst);
1897 msg_queue_purge(&nst->down_queue);
1898 if (nst->phd_down_msg)
1899 free(nst->phd_down_msg);
1903 PDEBUG(DEBUG_BCHANNEL, "freeing d-stack.\n");
1904 if (mISDNport->d_stid)
1906 // mISDN_clear_stack(mISDNdevice, mISDNport->d_stid);
1907 if (mISDNport->lower_id)
1908 mISDN_write_frame(mISDNdevice, buf, mISDNport->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1911 /* remove from list */
1912 mISDNportp = &mISDNport_first;
1915 if (*mISDNportp == mISDNport)
1917 *mISDNportp = (*mISDNportp)->next;
1920 mISDNportp = &((*mISDNportp)->next);
1925 PERROR("software error, mISDNport not in list\n");
1929 memset(mISDNport, 0, sizeof(struct mISDNport));
1933 /* close mISDNdevice, if no port */
1934 if (mISDNdevice>=0 && mISDNport_first==NULL)
1937 mISDN_write_frame(mISDNdevice, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
1939 mISDN_close(mISDNdevice);
1941 PDEBUG(DEBUG_ISDN, "mISDN device closed.\n");
1947 * global function to show all available isdn ports
1949 void mISDN_port_info(void)
1953 int useable, nt, pri;
1954 unsigned char buff[1025];
1955 iframe_t *frm = (iframe_t *)buff;
1956 stack_info_t *stinf;
1960 if ((device = mISDN_open()) < 0)
1962 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));
1966 /* get number of stacks */
1968 ii = mISDN_get_stack_count(device);
1972 printf("Found no card. Please be sure to load card drivers.\n");
1975 /* loop the number of cards and get their info */
1978 err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
1981 fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
1984 stinf = (stack_info_t *)&frm->data.p;
1989 /* output the port info */
1990 printf("Port %2d: ", i);
1991 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1993 case ISDN_PID_L0_TE_S0:
1994 printf("TE-mode BRI S/T interface line (for phone lines)");
1996 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
1997 printf(" HFC multiport card");
2000 case ISDN_PID_L0_NT_S0:
2002 printf("NT-mode BRI S/T interface port (for phones)");
2004 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
2005 printf(" HFC multiport card");
2008 case ISDN_PID_L0_TE_U:
2009 printf("TE-mode BRI U interface line");
2011 case ISDN_PID_L0_NT_U:
2013 printf("NT-mode BRI U interface port");
2015 case ISDN_PID_L0_TE_UP2:
2016 printf("TE-mode BRI Up2 interface line");
2018 case ISDN_PID_L0_NT_UP2:
2020 printf("NT-mode BRI Up2 interface port");
2022 case ISDN_PID_L0_TE_E1:
2024 printf("TE-mode PRI E1 interface line (for phone lines)");
2026 if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
2027 printf(" HFC-E1 card");
2030 case ISDN_PID_L0_NT_E1:
2033 printf("NT-mode PRI E1 interface port (for phones)");
2035 if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
2036 printf(" HFC-E1 card");
2041 printf("unknown type 0x%08x",stinf->pid.protocol[0]);
2047 if (stinf->pid.protocol[1] == 0)
2050 printf(" -> Missing layer 1 NT-mode protocol.\n");
2053 while(p <= MAX_LAYER_NR) {
2054 if (stinf->pid.protocol[p])
2057 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]);
2064 printf(" -> Interface is Point-To-Point (PRI).\n");
2066 printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
2070 if (stinf->pid.protocol[1] == 0)
2073 printf(" -> Missing layer 1 protocol.\n");
2075 if (stinf->pid.protocol[2] == 0)
2078 printf(" -> Missing layer 2 protocol.\n");
2080 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP)
2082 printf(" -> Interface is Poin-To-Point.\n");
2084 if (stinf->pid.protocol[3] == 0)
2087 printf(" -> Missing layer 3 protocol.\n");
2090 printf(" -> Protocol: ");
2091 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
2093 case ISDN_PID_L3_DSS1USER:
2094 printf("DSS1 (Euro ISDN)");
2099 printf("unknown protocol 0x%08x",stinf->pid.protocol[3]);
2104 while(p <= MAX_LAYER_NR) {
2105 if (stinf->pid.protocol[p])
2108 printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]);
2113 printf(" - %d B-channels\n", stinf->childcnt);
2116 printf(" * Port NOT useable for PBX\n");
2118 printf("--------\n");
2125 if ((err = mISDN_close(device)))
2127 fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err));