1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN port abstraction for dss1 **
10 \*****************************************************************************/
14 #include <mISDN/q931.h>
17 #ifdef __compiler_offsetof
18 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
20 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
24 #define container_of(ptr, type, member) ({ \
25 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
26 (type *)( (char *)__mptr - offsetof(type,member) );})
29 // timeouts if activating/deactivating response from mISDN got lost
30 #define B_TIMER_ACTIVATING 1 // seconds
31 #define B_TIMER_DEACTIVATING 1 // seconds
33 /* list of mISDN ports */
34 struct mISDNport *mISDNport_first;
36 /* noise randomizer */
37 unsigned char mISDN_rand[256];
38 int mISDN_rand_count = 0;
41 unsigned int mt_assign_pid = ~0;
45 static int upqueue_pipe[2];
46 static struct lcr_fd upqueue_fd;
47 int upqueue_avail = 0;
49 static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, int i);
50 static int mISDN_timeout(struct lcr_timer *timer, void *instance, int i);
52 static int my_mISDNlib_debug(const char *file, int line, const char *func, int level, const char *fmt, va_list va)
57 ret = vfprintf(debug_fp, fmt, va);
61 static struct mi_ext_fn_s myfn;
63 int mISDN_initialize(void)
67 /* try to open raw socket to check kernel */
68 mISDNsocket = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
69 if (mISDNsocket < 0) {
70 fprintf(stderr, "Cannot open mISDN due to '%s'. (Does your Kernel support socket based mISDN? Protocol family is %d.)\n", strerror(errno), PF_ISDN);
75 // set debug printout function
76 myfn.prt_debug = my_mISDNlib_debug;
78 init_layer3(4, &myfn); // buffer of 4
80 /* open debug, if enabled and not only stack debugging */
82 SPRINT(filename, "%s/debug.log", LOG_DIR);
83 debug_fp = fopen(filename, "a");
86 if (options.deb & DEBUG_STACK)
87 mISDN_set_debug_level(0xfffffeff);
89 mISDN_set_debug_level(0);
91 if (pipe(upqueue_pipe) < 0)
92 FATAL("Failed to open pipe\n");
93 memset(&upqueue_fd, 0, sizeof(upqueue_fd));
94 upqueue_fd.fd = upqueue_pipe[0];
95 register_fd(&upqueue_fd, LCR_FD_READ, mISDN_upqueue, NULL, 0);
100 void mISDN_deinitialize(void)
108 if (mISDNsocket > -1)
111 if (upqueue_fd.inuse) {
112 unregister_fd(&upqueue_fd);
113 close(upqueue_pipe[0]);
114 close(upqueue_pipe[1]);
119 static int load_timer(struct lcr_timer *timer, void *instance, int index);
124 PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_settings *settings, struct interface *interface, int channel, int exclusive, int mode) : Port(type, portname, settings, interface)
126 p_m_mISDNport = mISDNport;
127 p_m_portnum = mISDNport->portnum;
134 p_m_tx_gain = mISDNport->ifport->interface->tx_gain;
135 p_m_rx_gain = mISDNport->ifport->interface->rx_gain;
141 p_m_preload = ISDN_LOAD;
142 p_m_disable_dejitter = 0;
146 p_m_inband_send_on = 0;
147 p_m_inband_receive_on = 0;
148 p_m_dtmf = !mISDNport->ifport->nodtmf;
149 p_m_dtmf_threshold = mISDNport->ifport->dtmf_threshold;
150 memset(&p_m_timeout, 0, sizeof(p_m_timeout));
151 add_timer(&p_m_timeout, mISDN_timeout, this, 0);
152 SCPY(p_m_pipeline, mISDNport->ifport->interface->pipeline);
155 memset(&p_m_loadtimer, 0, sizeof(p_m_loadtimer));
156 add_timer(&p_m_loadtimer, load_timer, this, 0);
162 p_m_crypt_listen = 0;
163 p_m_crypt_msg_loops = 0;
164 p_m_crypt_msg_loops = 0;
165 p_m_crypt_msg_len = 0;
166 p_m_crypt_msg[0] = '\0';
167 p_m_crypt_msg_current = 0;
168 p_m_crypt_key_len = 0;
169 p_m_crypt_listen = 0;
170 p_m_crypt_listen_state = 0;
171 p_m_crypt_listen_len = 0;
172 p_m_crypt_listen_msg[0] = '\0';
173 p_m_crypt_listen_crc = 0;
174 if (mISDNport->ifport->interface->bf_len >= 4 && mISDNport->ifport->interface->bf_len <= 56) {
175 memcpy(p_m_crypt_key, mISDNport->ifport->interface->bf_key, p_m_crypt_key_len);
176 p_m_crypt_key_len = mISDNport->ifport->interface->bf_len;
180 /* if any channel requested by constructor */
181 if (channel == CHANNEL_ANY) {
182 /* reserve channel */
184 mISDNport->b_reserved++;
187 /* reserve channel */
188 if (channel > 0) // only if constructor was called with a channel resevation
189 seize_bchannel(channel, exclusive);
191 /* we increase the number of objects: */
193 PDEBUG(DEBUG_ISDN, "Created new mISDNPort(%s). Currently %d objects use, port #%d\n", portname, mISDNport->use, p_m_portnum);
194 //inband_receive_on();
203 struct lcr_msg *message;
205 del_timer(&p_m_timeout);
206 del_timer(&p_m_loadtimer);
208 /* remove bchannel relation */
212 while (p_epointlist) {
213 PDEBUG(DEBUG_ISDN, "destroy mISDNPort(%s). endpoint still exists, releaseing.\n", p_name);
214 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
215 message->param.disconnectinfo.cause = 16;
216 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
217 message_put(message);
218 /* remove from list */
219 free_epointlist(p_epointlist);
222 /* we decrease the number of objects: */
223 p_m_mISDNport->use--;
224 PDEBUG(DEBUG_ISDN, "destroyed mISDNPort(%s). Currently %d objects\n", p_name, p_m_mISDNport->use);
231 void chan_trace_header(struct mISDNport *mISDNport, class PmISDN *port, const char *msgtext, int direction)
233 /* init trace with given values */
234 start_trace(mISDNport?mISDNport->portnum:-1,
235 (mISDNport)?((mISDNport->ifport)?mISDNport->ifport->interface:NULL):NULL,
236 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
237 port?port->p_dialinginfo.id:NULL,
240 port?port->p_serial:0,
248 static struct isdn_message {
252 {"PH_ACTIVATE", L1_ACTIVATE_REQ},
253 {"PH_DEACTIVATE", L1_DEACTIVATE_REQ},
254 {"DL_ESTABLISH", L2_ESTABLISH_REQ},
255 {"DL_RELEASE", L2_RELEASE_REQ},
256 {"UNKNOWN", L3_UNKNOWN_REQ},
257 {"MT_TIMEOUT", L3_TIMEOUT_REQ},
258 {"MT_SETUP", L3_SETUP_REQ},
259 {"MT_SETUP_ACK", L3_SETUP_ACKNOWLEDGE_REQ},
260 {"MT_PROCEEDING", L3_PROCEEDING_REQ},
261 {"MT_ALERTING", L3_ALERTING_REQ},
262 {"MT_CONNECT", L3_CONNECT_REQ},
263 {"MT_CONNECT_ACK", L3_CONNECT_ACKNOWLEDGE_REQ},
264 {"MT_DISCONNECT", L3_DISCONNECT_REQ},
265 {"MT_RELEASE", L3_RELEASE_REQ},
266 {"MT_RELEASE_COMP", L3_RELEASE_COMPLETE_REQ},
267 {"MT_INFORMATION", L3_INFORMATION_REQ},
268 {"MT_PROGRESS", L3_PROGRESS_REQ},
269 {"MT_NOTIFY", L3_NOTIFY_REQ},
270 {"MT_SUSPEND", L3_SUSPEND_REQ},
271 {"MT_SUSPEND_ACK", L3_SUSPEND_ACKNOWLEDGE_REQ},
272 {"MT_SUSPEND_REJ", L3_SUSPEND_REJECT_REQ},
273 {"MT_RESUME", L3_RESUME_REQ},
274 {"MT_RESUME_ACK", L3_RESUME_ACKNOWLEDGE_REQ},
275 {"MT_RESUME_REJ", L3_RESUME_REJECT_REQ},
276 {"MT_HOLD", L3_HOLD_REQ},
277 {"MT_HOLD_ACK", L3_HOLD_ACKNOWLEDGE_REQ},
278 {"MT_HOLD_REJ", L3_HOLD_REJECT_REQ},
279 {"MT_RETRIEVE", L3_RETRIEVE_REQ},
280 {"MT_RETRIEVE_ACK", L3_RETRIEVE_ACKNOWLEDGE_REQ},
281 {"MT_RETRIEVE_REJ", L3_RETRIEVE_REJECT_REQ},
282 {"MT_FACILITY", L3_FACILITY_REQ},
283 {"MT_STATUS", L3_STATUS_REQ},
284 {"MT_RESTART", L3_RESTART_REQ},
285 {"MT_NEW_L3ID", L3_NEW_L3ID_REQ},
286 {"MT_RELEASE_L3ID", L3_RELEASE_L3ID_REQ},
289 static const char *isdn_prim[4] = {
295 void l1l2l3_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg, int direction)
300 SCPY(msgtext, "<<UNKNOWN MESSAGE>>");
301 /* select message and primitive text */
303 while(isdn_message[i].name) {
304 // if (msg == L3_NOTIFY_REQ) printf("val = %x %s\n", isdn_message[i].value, isdn_message[i].name);
305 if (isdn_message[i].value == (msg&0xffffff00)) {
306 SCPY(msgtext, isdn_message[i].name);
311 SCAT(msgtext, isdn_prim[msg&0x00000003]);
314 if (direction && (msg&0xffffff00)!=L3_NEW_L3ID_REQ && (msg&0xffffff00)!=L3_RELEASE_L3ID_REQ) {
316 if (mISDNport->ntmode) {
317 if (direction == DIRECTION_OUT)
318 SCAT(msgtext, " N->U");
320 SCAT(msgtext, " N<-U");
322 if (direction == DIRECTION_OUT)
323 SCAT(msgtext, " U->N");
325 SCAT(msgtext, " U<-N");
330 /* init trace with given values */
331 start_trace(mISDNport?mISDNport->portnum:-1,
332 mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
333 port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
334 port?port->p_dialinginfo.id:NULL,
337 port?port->p_serial:0,
343 * send control information to the channel (dsp-module)
345 void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, int sock, unsigned int c1, unsigned int c2, const char *trace_name, int trace_value)
347 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
348 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
349 unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
356 if (c1 == DTMF_TONE_START && c2 == 0) {
360 ctrl->prim = PH_CONTROL_REQ;
364 ret = sendto(sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
366 PERROR("Failed to send to socket %d\n", sock);
367 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
368 if (c1 == DSP_CONF_JOIN)
369 add_trace(trace_name, NULL, "0x%08x", trace_value);
371 add_trace(trace_name, NULL, "%d", trace_value);
375 void ph_control_block(struct mISDNport *mISDNport, class PmISDN *isdnport, int sock, unsigned int c1, void *c2, int c2_len, const char *trace_name, int trace_value)
377 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
378 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
379 unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
385 ctrl->prim = PH_CONTROL_REQ;
388 memcpy(d, c2, c2_len);
389 ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
391 PERROR("Failed to send to socket %d\n", sock);
392 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
393 add_trace(trace_name, NULL, "%d", trace_value);
397 static int b_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int i);
400 * subfunction for bchannel_event
403 static int _bchannel_create(struct mISDNport *mISDNport, int i)
406 struct sockaddr_mISDN addr;
408 if (mISDNport->b_sock[i].inuse) {
409 PERROR("Error: Socket already created for index %d\n", i);
414 //#warning testing without DSP
415 // mISDNport->b_sock[i].fd = socket(PF_ISDN, SOCK_DGRAM, (mISDNport->b_mode[i]==B_MODE_HDLC)?ISDN_P_B_HDLC:ISDN_P_B_RAW);
416 mISDNport->b_sock[i].fd = socket(PF_ISDN, SOCK_DGRAM, (mISDNport->b_mode[i]==B_MODE_HDLC)?ISDN_P_B_L2DSPHDLC:ISDN_P_B_L2DSP);
417 if (mISDNport->b_sock[i].fd < 0) {
418 PERROR("Error: Failed to open bchannel-socket for index %d with mISDN-DSP layer. Did you load mISDN_dsp.ko?\n", i);
422 /* register callback for read */
423 register_fd(&mISDNport->b_sock[i], LCR_FD_READ, b_sock_callback, mISDNport, i);
425 /* bind socket to bchannel */
426 addr.family = AF_ISDN;
427 addr.dev = mISDNport->portnum;
428 addr.channel = i+1+(i>=15);
429 ret = bind(mISDNport->b_sock[i].fd, (struct sockaddr *)&addr, sizeof(addr));
431 PERROR("Error: Failed to bind bchannel-socket for index %d with mISDN-DSP layer (errno=%d). Did you load mISDN_dsp.ko?\n", i, errno);
432 close(mISDNport->b_sock[i].fd);
433 unregister_fd(&mISDNport->b_sock[i]);
437 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
438 add_trace("channel", NULL, "%d", i+1+(i>=15));
439 add_trace("socket", NULL, "%d", mISDNport->b_sock[i].fd);
447 * subfunction for bchannel_event
448 * activate / deactivate request
450 static void _bchannel_activate(struct mISDNport *mISDNport, int i, int activate, int timeout)
452 struct mISDNhead act;
455 if (!mISDNport->b_sock[i].inuse)
457 act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ;
459 ret = sendto(mISDNport->b_sock[i].fd, &act, MISDN_HEADER_LEN, 0, NULL, 0);
461 PERROR("Failed to send to socket %d\n", mISDNport->b_sock[i].fd);
464 chan_trace_header(mISDNport, mISDNport->b_port[i], activate ? "BCHANNEL activate" : "BCHANNEL deactivate", DIRECTION_OUT);
465 add_trace("channel", NULL, "%d", i+1+(i>=15));
467 add_trace("event", NULL, "timeout recovery");
473 * subfunction for bchannel_event
476 static void _bchannel_configure(struct mISDNport *mISDNport, int i)
481 if (!mISDNport->b_sock[i].inuse)
483 handle = mISDNport->b_sock[i].fd;
484 port = mISDNport->b_port[i];
485 mode = mISDNport->b_mode[i];
487 PERROR("bchannel index i=%d not associated with a port object\n", i);
491 /* set dsp features */
492 if (port->p_m_txdata)
493 ph_control(mISDNport, port, handle, (port->p_m_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", port->p_m_txdata);
494 if (port->p_m_delay && mode == B_MODE_TRANSPARENT)
495 ph_control(mISDNport, port, handle, DSP_DELAY, port->p_m_delay, "DSP-DELAY", port->p_m_delay);
496 if (port->p_m_tx_dejitter && mode == B_MODE_TRANSPARENT)
497 ph_control(mISDNport, port, handle, DSP_TX_DEJITTER, port->p_m_tx_dejitter, "DSP-TX_DEJITTER", port->p_m_tx_dejitter);
498 if (port->p_m_tx_gain && mode == B_MODE_TRANSPARENT)
499 ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_TX, port->p_m_tx_gain, "DSP-TX_GAIN", port->p_m_tx_gain);
500 if (port->p_m_rx_gain && mode == B_MODE_TRANSPARENT)
501 ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_RX, port->p_m_rx_gain, "DSP-RX_GAIN", port->p_m_rx_gain);
502 if (port->p_m_pipeline[0] && mode == B_MODE_TRANSPARENT)
503 ph_control_block(mISDNport, port, handle, DSP_PIPELINE_CFG, port->p_m_pipeline, strlen(port->p_m_pipeline)+1, "DSP-PIPELINE", 0);
504 if (port->p_m_conf && !port->p_m_mute)
505 ph_control(mISDNport, port, handle, DSP_CONF_JOIN, port->p_m_conf, "DSP-CONF", port->p_m_conf);
507 ph_control(mISDNport, port, handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
508 if (port->p_m_tone && mode == B_MODE_TRANSPARENT)
509 ph_control(mISDNport, port, handle, DSP_TONE_PATT_ON, port->p_m_tone, "DSP-TONE", port->p_m_tone);
511 ph_control(mISDNport, port, handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
512 // if (port->p_m_txmix && mode == B_MODE_TRANSPARENT)
513 // ph_control(mISDNport, port, handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
514 if (port->p_m_dtmf && mode == B_MODE_TRANSPARENT)
515 ph_control(mISDNport, port, handle, DTMF_TONE_START, port->p_m_dtmf_threshold, "DSP-DTMF", 1);
516 if (port->p_m_crypt && mode == B_MODE_TRANSPARENT)
517 ph_control_block(mISDNport, port, handle, DSP_BF_ENABLE_KEY, port->p_m_crypt_key, port->p_m_crypt_key_len, "DSP-CRYPT", port->p_m_crypt_key_len);
521 void PmISDN::set_conf(int oldconf, int newconf)
523 if (oldconf != newconf) {
524 PDEBUG(DEBUG_BCHANNEL, "we change conference from conf=%d to conf=%d.\n", oldconf, newconf);
525 if (p_m_b_index > -1)
526 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
527 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, (newconf)?DSP_CONF_JOIN:DSP_CONF_SPLIT, newconf, "DSP-CONF", newconf);
529 PDEBUG(DEBUG_BCHANNEL, "we already have conf=%d.\n", newconf);
534 * subfunction for bchannel_event
537 static void _bchannel_destroy(struct mISDNport *mISDNport, int i)
539 if (!mISDNport->b_sock[i].inuse)
541 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
542 add_trace("channel", NULL, "%d", i+1+(i>=15));
543 add_trace("socket", NULL, "%d", mISDNport->b_sock[i].fd);
545 close(mISDNport->b_sock[i].fd);
546 unregister_fd(&mISDNport->b_sock[i]);
554 A bchannel goes through the following states in this order:
557 No one is using the bchannel.
558 It is available and not linked to Port class, nor reserved.
561 The bchannel stack is created and an activation request is sent.
562 It MAY be linked to Port class, but already unlinked due to Port class removal.
565 The bchannel is active and cofigured to the Port class needs.
566 Also it is linked to a Port class, otherwhise it would be deactivated.
568 - B_STATE_DEACTIVATING
569 The bchannel is in deactivating state, due to deactivation request.
570 It may be linked to a Port class, that likes to reactivate it.
574 After deactivating bchannel, and if not used, the bchannel becomes idle again.
576 A bchannel can have the following events:
579 A bchannel is required by a Port class.
582 The bchannel beomes active.
585 The bchannel is not required by Port class anymore
587 - B_EVENT_DEACTIVATED
588 The bchannel becomes inactive.
590 All actions taken on these events depend on the current bchannel's state and if it is linked to a Port class.
595 * process bchannel events
596 * - mISDNport is a pointer to the port's structure
597 * - i is the index of the bchannel
598 * - event is the B_EVENT_* value
599 * - port is the PmISDN class pointer
601 void bchannel_event(struct mISDNport *mISDNport, int i, int event)
603 class PmISDN *b_port = mISDNport->b_port[i];
604 int state = mISDNport->b_state[i];
605 int timer = -1; // no change
609 char *p_m_pipeline = NULL;
610 unsigned char *p_m_crypt_key = NULL;
611 int p_m_crypt_key_len = 0;
612 int p_m_crypt_key_type = 0;
615 p_m_tx_gain = b_port->p_m_tx_gain;
616 p_m_rx_gain = b_port->p_m_rx_gain;
617 p_m_pipeline = b_port->p_m_pipeline;
618 p_m_crypt_key = b_port->p_m_crypt_key;
619 p_m_crypt_key_len = b_port->p_m_crypt_key_len;
620 p_m_crypt_key_type = /*b_port->p_m_crypt_key_type*/1;
626 /* port must be linked in order to allow activation */
628 FATAL("bchannel must be linked to a Port class\n");
631 /* create stack and send activation request */
632 if (_bchannel_create(mISDNport, i)) {
633 _bchannel_activate(mISDNport, i, 1, 0);
634 state = B_STATE_ACTIVATING;
635 timer = B_TIMER_ACTIVATING;
639 case B_STATE_ACTIVATING:
640 /* do nothing, because it is already activating */
644 /* problems that might ocurr:
645 * B_EVENT_USE is received when channel already in use.
647 PERROR("Illegal event %d at state %d, please correct.\n", event, state);
652 case B_EVENT_ACTIVATED:
655 case B_STATE_ACTIVATING:
657 /* bchannel is active and used by Port class, so we configure bchannel */
658 _bchannel_configure(mISDNport, i);
659 state = B_STATE_ACTIVE;
660 b_port->p_m_load = 0;
661 b_port->update_load();
663 /* bchannel is active, but not used anymore (or has wrong stack config), so we deactivate */
664 _bchannel_activate(mISDNport, i, 0, 0);
665 state = B_STATE_DEACTIVATING;
666 timer = B_TIMER_DEACTIVATING;
671 PERROR("Illegal event %d at state %d, please correct.\n", event, state);
677 FATAL("bchannel must be linked to a Port class\n");
680 /* bchannel is idle due to an error, so we do nothing */
683 case B_STATE_ACTIVATING:
684 /* do nothing because we must wait until bchanenl is active before deactivating */
688 /* bchannel is active, so we deactivate */
689 _bchannel_activate(mISDNport, i, 0, 0);
690 state = B_STATE_DEACTIVATING;
691 timer = B_TIMER_DEACTIVATING;
694 case B_STATE_DEACTIVATING:
695 /* we may have taken an already deactivating bchannel, but do not require it anymore, so we do nothing */
699 PERROR("Illegal event %d at state %d, please correct.\n", event, state);
703 case B_EVENT_DEACTIVATED:
707 /* ignore due to deactivation confirm after unloading */
710 case B_STATE_DEACTIVATING:
711 _bchannel_destroy(mISDNport, i);
712 state = B_STATE_IDLE;
714 /* bchannel is now deactivate, but is requied by Port class, so we reactivate */
715 if (_bchannel_create(mISDNport, i)) {
716 _bchannel_activate(mISDNport, i, 1, 0);
717 state = B_STATE_ACTIVATING;
718 timer = B_TIMER_ACTIVATING;
724 PERROR("Illegal event %d at state %d, please correct.\n", event, state);
728 case B_EVENT_TIMEOUT:
732 /* ignore due to deactivation confirm after unloading */
735 case B_STATE_ACTIVATING:
736 _bchannel_activate(mISDNport, i, 1, 1);
737 timer = B_TIMER_ACTIVATING;
740 case B_STATE_DEACTIVATING:
741 _bchannel_activate(mISDNport, i, 0, 1);
742 timer = B_TIMER_DEACTIVATING;
746 PERROR("Illegal event %d at state %d, please correct.\n", event, state);
751 PERROR("Illegal event %d, please correct.\n", event);
754 mISDNport->b_state[i] = state;
756 unsched_timer(&mISDNport->b_timer[i]);
758 schedule_timer(&mISDNport->b_timer[i], timer, 0);
765 * check for available channel and reserve+set it.
766 * give channel number or SEL_CHANNEL_ANY or SEL_CHANNEL_NO
768 * returns -(cause value) or x = channel x or 0 = no channel
769 * NOTE: no activation is done here
771 int PmISDN::seize_bchannel(int channel, int exclusive)
775 /* the channel is what we have */
776 if (p_m_b_channel == channel)
779 /* if channel already in use, release it */
784 if (channel==CHANNEL_NO || channel==0)
787 /* is channel in range ? */
789 || (channel>p_m_mISDNport->b_num && channel<16)
790 || ((channel-1)>p_m_mISDNport->b_num && channel>16)) /* channel-1 because channel 16 is not counted */
791 return(-6); /* channel unacceptable */
793 /* request exclusive channel */
794 if (exclusive && channel>0) {
795 i = channel-1-(channel>16);
796 if (p_m_mISDNport->b_port[i])
797 return(-44); /* requested channel not available */
801 /* ask for channel */
803 i = channel-1-(channel>16);
804 if (p_m_mISDNport->b_port[i] == NULL)
808 /* search for channel */
810 while(i < p_m_mISDNport->b_num) {
811 if (!p_m_mISDNport->b_port[i]) {
812 channel = i+1+(i>=15);
817 return(-34); /* no free channel */
820 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) seizing bchannel %d (index %d)\n", p_name, channel, i);
822 /* link Port, set parameters */
823 p_m_mISDNport->b_port[i] = this;
825 p_m_b_channel = channel;
826 p_m_b_exclusive = exclusive;
827 p_m_mISDNport->b_mode[i] = p_m_b_mode;
829 /* reserve channel */
830 if (!p_m_b_reserve) {
832 p_m_mISDNport->b_reserved++;
839 * drop reserved channel and unset it.
840 * deactivation is also done
842 void PmISDN::drop_bchannel(void)
844 /* unreserve channel */
846 p_m_mISDNport->b_reserved--;
855 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) dropping bchannel\n", p_name);
857 if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_IDLE)
858 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_DROP);
859 p_m_mISDNport->b_port[p_m_b_index] = NULL;
860 p_m_mISDNport->b_mode[p_m_b_index] = 0;
870 audio transmission procedure:
871 -----------------------------
874 three sources of audio transmission:
875 - crypto-data high priority
876 - tones high priority (also high)
877 - remote-data low priority
880 a variable that temporarily shows the number of samples elapsed since last transmission process.
881 p_m_last_tv_* is used to store that last timestamp. this is used to calculate the time elapsed.
884 a variable that is increased whenever data is transmitted.
885 it is decreased while time elapses. it stores the number of samples that
886 are currently loaded to dsp module.
887 since clock in dsp module is the same clock for user space process, these
891 there are two levels:
892 p_m_preload will give the load that have to be kept in dsp.
893 ISDN_MAXLOAD (2*p_m_preload) will give the maximum load before dropping.
895 * procedure for low priority data
896 see txfromup() for procedure
897 in short: remote data is ignored during high priority tones
899 * procedure for high priority data
900 whenever load is below p_m_preload, load is filled up to p_m_preload
901 if no more data is available, load becomes empty again.
904 0 p_m_preload ISDN_MAXLOAD
905 +--------------------+----------------------+
907 +--------------------+----------------------+
909 on empty load or on load below p_m_preload, the load is inceased to p_m_preload:
910 0 p_m_preload ISDN_MAXLOAD
911 +--------------------+----------------------+
912 |TTTTTTTTTTTTTTTTTTTT| |
913 +--------------------+----------------------+
915 on empty load, remote-audio causes the load with the remote audio to be increased to p_m_preload.
916 0 p_m_preload ISDN_MAXLOAD
917 +--------------------+----------------------+
918 |TTTTTTTTTTTTTTTTTTTTRRRRR |
919 +--------------------+----------------------+
922 void PmISDN::update_load(void)
924 /* don't trigger load event if event already active */
925 if (p_m_loadtimer.active)
928 schedule_timer(&p_m_loadtimer, 0, 0); /* no delay the first time */
931 static int load_timer(struct lcr_timer *timer, void *instance, int index)
933 class PmISDN *isdnport = (class PmISDN *)instance;
940 void PmISDN::load_tx(void)
944 struct timeval current_time;
947 gettimeofday(¤t_time, NULL);
948 if (p_m_last_tv_sec) {
949 elapsed = 8000 * (current_time.tv_sec - p_m_last_tv_sec)
950 + 8 * (current_time.tv_usec/1000 - p_m_last_tv_msec);
952 /* set clock of last process! */
953 p_m_last_tv_sec = current_time.tv_sec;
954 p_m_last_tv_msec = current_time.tv_usec/1000;
956 /* process only if we have samples and we are active */
957 if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_ACTIVE)
962 if (elapsed < p_m_load)
967 /* to send data, tone must be on */
968 if ((p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on) /* what tones? */
969 && (p_m_load < p_m_preload) /* not too much load? */
970 && (p_state==PORT_STATE_CONNECT || p_m_mISDNport->tones || p_m_inband_send_on)) { /* connected or inband-tones? */
971 int tosend = p_m_preload - p_m_load, length;
972 unsigned char buf[MISDN_HEADER_LEN+tosend];
973 struct mISDNhead *frm = (struct mISDNhead *)buf;
974 unsigned char *p = buf+MISDN_HEADER_LEN;
976 /* copy inband signalling (e.g. used by ss5) */
977 if (p_m_inband_send_on && tosend) {
978 tosend -= inband_send(p, tosend);
981 /* copy crypto loops */
982 while (p_m_crypt_msg_loops && tosend) {
983 /* how much do we have to send */
984 length = p_m_crypt_msg_len - p_m_crypt_msg_current;
990 /* copy message (part) to buffer */
991 memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, length);
994 p_m_crypt_msg_current += length;
995 if (p_m_crypt_msg_current == p_m_crypt_msg_len) {
997 p_m_crypt_msg_current = 0;
998 p_m_crypt_msg_loops--;
999 if (!p_m_crypt_msg_loops)
1001 // puts("eine loop weniger");
1009 if (p_tone_name[0] && tosend) {
1010 tosend -= read_audio(p, tosend);
1014 if (p_m_preload - p_m_load - tosend > 0) {
1015 frm->prim = PH_DATA_REQ;
1017 ret = sendto(p_m_mISDNport->b_sock[p_m_b_index].fd, buf, MISDN_HEADER_LEN+p_m_preload-p_m_load-tosend, 0, NULL, 0);
1019 PERROR("Failed to send to socket %d (samples = %d)\n", p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_preload-p_m_load-tosend);
1020 p_m_load += p_m_preload - p_m_load - tosend;
1025 schedule_timer(&p_m_loadtimer, 0, PORT_TRANSMIT * 125);
1028 /* handle timeouts */
1029 static int mISDN_timeout(struct lcr_timer *timer, void *instance, int i)
1031 class PmISDN *isdnport = (class PmISDN *)instance;
1032 struct lcr_msg *message;
1034 PDEBUG(DEBUG_ISDN, "(%s) timeout after %d seconds detected (state=%d).\n", isdnport->p_name, isdnport->p_m_timeout.timeout.tv_sec, isdnport->p_state);
1035 /* send timeout to endpoint */
1036 message = message_create(isdnport->p_serial, ACTIVE_EPOINT(isdnport->p_epointlist), PORT_TO_EPOINT, MESSAGE_TIMEOUT);
1037 message->param.state = isdnport->p_state;
1038 message_put(message);
1045 * whenever we get audio data from bchannel, we process it here
1047 void PmISDN::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
1049 unsigned int cont = *((unsigned int *)data);
1050 struct lcr_msg *message;
1054 if (hh->prim == PH_CONTROL_IND) {
1056 PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
1059 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL) {
1060 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1061 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
1063 add_trace("info", NULL, "DTMF is disabled");
1067 if (p_type == PORT_TYPE_POTS_FXS_IN && p_state == PORT_STATE_IN_OVERLAP) {
1068 class Pfxs *pfxs = (class Pfxs *)this;
1069 if (!pfxs->p_m_fxs_allow_dtmf) {
1070 PDEBUG(DEBUG_PORT, "PmISDN(%s) DTMF for FXS currently disabled\n", p_name);
1073 SCCAT(p_dialinginfo.id, cont & DTMF_TONE_MASK);
1074 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
1075 message->param.information.id[0] = cont & DTMF_TONE_MASK;
1076 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION INFORMATION digit '%s'\n", p_name, message->param.information.id);
1077 message_put(message);
1079 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DTMF);
1080 message->param.dtmf = cont & DTMF_TONE_MASK;
1081 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION DTMF digit '%c'\n", p_name, message->param.dtmf);
1082 message_put(message);
1088 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1089 add_trace("DSP-CRYPT", NULL, "error");
1091 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
1092 message->param.crypt.type = CC_ERROR_IND;
1093 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION reject of blowfish.\n", p_name);
1094 message_put(message);
1098 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1099 add_trace("DSP-CRYPT", NULL, "ok");
1101 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
1102 message->param.crypt.type = CC_ACTBF_CONF;
1103 PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION accept of blowfish.\n", p_name);
1104 message_put(message);
1108 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1109 add_trace("unknown", NULL, "0x%x", cont);
1114 if (hh->prim == PH_CONTROL_IND) {
1117 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1118 add_trace("unknown", NULL, "0x%x", hh->id);
1123 if (hh->prim == PH_DATA_REQ || hh->prim == DL_DATA_REQ) {
1125 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
1126 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
1129 /* see below (same condition) */
1130 if (p_state!=PORT_STATE_CONNECT
1131 && !p_m_mISDNport->tones)
1133 // printf(".");fflush(stdout);return;
1135 record(data, len, 1); // from up
1137 tap(data, len, 1); // from up
1140 if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND) {
1141 PERROR("Bchannel received unknown primitve: 0x%x\n", hh->prim);
1145 /* inband is processed */
1146 if (p_m_inband_receive_on)
1147 inband_receive(data, len);
1149 /* send to remote, if bridged */
1150 bridge_tx(data, len);
1152 /* calls will not process any audio data unless
1153 * the call is connected OR tones feature is enabled.
1155 #ifndef DEBUG_COREBRIDGE
1156 if (p_state!=PORT_STATE_CONNECT
1157 && !p_m_mISDNport->tones)
1162 /* the bearer capability must be audio in order to send and receive
1163 * audio prior or after connect.
1165 if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
1169 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
1171 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
1177 record(data, len, 0); // from down
1179 tap(data, len, 0); // from down
1181 /* randomize and listen to crypt message if enabled */
1182 if (p_m_crypt_listen) {
1183 /* the noisy randomizer */
1187 mISDN_rand[mISDN_rand_count & 0xff] += *p++;
1189 cryptman_listen_bch(data, len);
1197 void PmISDN::set_echotest(int echo)
1199 if (p_m_echo != echo) {
1201 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
1203 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1204 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_echo?DSP_ECHO_ON:DSP_ECHO_OFF, 0, "DSP-ECHO", p_m_echo);
1211 void PmISDN::set_tone(const char *dir, const char *tone)
1216 /* if no directory is given (by extension), we use interface.conf or options.conf */
1217 if (!dir || !dir[0]) {
1218 if (p_m_mISDNport->ifport->tones_dir[0])
1219 dir = p_m_mISDNport->ifport->tones_dir;
1220 else if (options.tones_dir[0])
1221 dir = options.tones_dir;
1226 PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
1232 /* check for dsp tones */
1233 if (!strcmp(dir, "american"))
1235 if (!strcmp(dir, "german"))
1237 if (!strcmp(dir, "oldgerman"))
1238 dsp = DSP_OLDGERMAN;
1240 /* check if we NOT really have to use a dsp-tone */
1241 if (dsp == DSP_NONE) {
1244 if (p_m_b_index > -1)
1245 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT) {
1246 PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
1247 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TONE_PATT_OFF, 0, "DSP-TONE", 0);
1250 Port::set_tone(dir, tone);
1254 /* now we USE dsp-tone, convert name */
1255 if (!strcmp(tone, "dialtone")) {
1257 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
1258 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
1259 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
1261 } else if (!strcmp(tone, "dialpbx")) {
1263 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
1264 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
1265 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
1267 } else if (!strcmp(tone, "ringing")) {
1269 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
1270 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
1271 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
1273 } else if (!strcmp(tone, "ringpbx")) {
1275 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
1276 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
1277 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
1279 } else if (!strcmp(tone, "busy")) {
1282 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
1283 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
1284 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
1286 } else if (!strcmp(tone, "release")) {
1289 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
1290 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
1291 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
1293 } else if (!strcmp(tone, "cause_10"))
1295 else if (!strcmp(tone, "cause_11"))
1297 else if (!strcmp(tone, "cause_22")) {
1299 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
1300 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
1301 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
1303 } else if (!strncmp(tone, "cause_", 6))
1304 id = TONE_SPECIAL_INFO;
1308 /* if we have a tone that is not supported by dsp */
1309 if (id==TONE_OFF && tone[0])
1313 if (p_m_tone != id) {
1316 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
1317 if (p_m_b_index > -1)
1318 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1319 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_tone?DSP_TONE_PATT_ON:DSP_TONE_PATT_OFF, p_m_tone, "DSP-TONE", p_m_tone);
1321 /* turn user-space tones off in cases of no tone OR dsp tone */
1322 Port::set_tone("",NULL);
1326 /* MESSAGE_mISDNSIGNAL */
1327 //extern struct lcr_msg *dddebug;
1328 void PmISDN::message_mISDNsignal(unsigned int epoint_id, int message_id, union parameter *param)
1330 int oldconf, newconf;
1331 switch(param->mISDNsignal.message) {
1332 case mISDNSIGNAL_VOLUME:
1333 if (p_m_tx_gain != param->mISDNsignal.tx_gain) {
1334 p_m_tx_gain = param->mISDNsignal.tx_gain;
1335 PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_tx_gain);
1336 if (p_m_b_index > -1)
1337 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1338 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_VOL_CHANGE_TX, p_m_tx_gain, "DSP-TX_GAIN", p_m_tx_gain);
1340 PDEBUG(DEBUG_BCHANNEL, "we already have tx-volume shift=%d.\n", p_m_rx_gain);
1341 if (p_m_rx_gain != param->mISDNsignal.rx_gain) {
1342 p_m_rx_gain = param->mISDNsignal.rx_gain;
1343 PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rx_gain);
1344 if (p_m_b_index > -1)
1345 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1346 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_VOL_CHANGE_RX, p_m_rx_gain, "DSP-RX_GAIN", p_m_rx_gain);
1348 PDEBUG(DEBUG_BCHANNEL, "we already have rx-volume shift=%d.\n", p_m_rx_gain);
1351 case mISDNSIGNAL_CONF:
1352 oldconf = p_m_mute?0:p_m_conf;
1353 p_m_conf = param->mISDNsignal.conf;
1354 newconf = p_m_mute?0:p_m_conf;
1355 set_conf(oldconf, newconf);
1358 case mISDNSIGNAL_DELAY:
1359 if (p_m_delay != param->mISDNsignal.delay) {
1360 p_m_delay = param->mISDNsignal.delay;
1361 PDEBUG(DEBUG_BCHANNEL, "we change delay mode to delay=%d.\n", p_m_delay);
1362 if (p_m_b_index > -1)
1363 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1364 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_delay?DSP_DELAY:DSP_JITTER, p_m_delay, "DSP-DELAY", p_m_delay);
1366 PDEBUG(DEBUG_BCHANNEL, "we already have delay=%d.\n", p_m_delay);
1370 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
1375 void PmISDN::message_crypt(unsigned int epoint_id, int message_id, union parameter *param)
1377 struct lcr_msg *message;
1379 switch(param->crypt.type) {
1380 case CC_ACTBF_REQ: /* activate blowfish */
1382 p_m_crypt_key_len = param->crypt.len;
1383 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key)) {
1384 PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
1385 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
1386 message->param.crypt.type = CC_ERROR_IND;
1387 message_put(message);
1390 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
1392 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
1393 if (p_m_b_index > -1)
1394 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1395 ph_control_block(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_crypt?DSP_BF_ENABLE_KEY:DSP_BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len, "DSP-CRYPT", p_m_crypt_key_len);
1398 case CC_DACT_REQ: /* deactivate session encryption */
1403 case CR_LISTEN_REQ: /* start listening to messages */
1404 p_m_crypt_listen = 1;
1406 p_m_crypt_listen_state = 0;
1409 case CR_UNLISTEN_REQ: /* stop listening to messages */
1410 p_m_crypt_listen = 0;
1414 case CR_MESSAGE_REQ: /* send message */
1415 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
1416 if (!p_m_crypt_msg_len) {
1417 PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
1420 p_m_crypt_msg_current = 0; /* reset */
1421 p_m_crypt_msg_loops = 6; /* enable */
1424 /* disable txmix, or we get corrupt data due to audio process */
1425 if (p_m_txmix && p_m_b_index>=0 && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT) {
1426 PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
1427 ph_control(p_m_mISDNport, this, p_mISDNport->b_sock[p_m_b_index].fd, DSP_MIX_OFF, 0, "DSP-TXMIX", 0);
1433 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
1439 * endpoint sends messages to the port
1441 int PmISDN::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1443 if (Port::message_epoint(epoint_id, message_id, param)) {
1444 if (message_id == MESSAGE_BRIDGE)
1449 switch(message_id) {
1450 case MESSAGE_mISDNSIGNAL: /* user command */
1451 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
1452 message_mISDNsignal(epoint_id, message_id, param);
1455 case MESSAGE_CRYPT: /* crypt control command */
1456 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
1457 message_crypt(epoint_id, message_id, param);
1460 case MESSAGE_DISABLE_DEJITTER:
1461 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received de-jitter disable order.\n", p_name);
1462 p_m_disable_dejitter = 1;
1463 p_m_preload = param->queue;
1471 void PmISDN::update_rxoff(void)
1473 int tx_dejitter = 0;
1475 /* call bridges in user space OR crypto OR recording */
1476 if (p_bridge || p_m_crypt_msg_loops || p_m_crypt_listen || p_record || p_tap || p_m_inband_receive_on) {
1477 /* rx IS required */
1481 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n", __FUNCTION__);
1482 if (p_m_b_index > -1)
1483 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1484 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_RECEIVE_ON, 0, "DSP-RXOFF", 0);
1487 /* rx NOT required */
1491 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n", __FUNCTION__);
1492 if (p_m_b_index > -1)
1493 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1494 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
1497 /* recording / tapping */
1498 if (p_record || p_tap) {
1499 /* txdata IS required */
1503 PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is required, so we turn them on\n", __FUNCTION__);
1504 if (p_m_b_index > -1)
1505 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1506 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TXDATA_ON, 0, "DSP-TXDATA", 1);
1509 /* txdata NOT required */
1513 PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is not required, so we turn them off\n", __FUNCTION__);
1514 if (p_m_b_index > -1)
1515 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1516 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TXDATA_OFF, 0, "DSP-TXDATA", 0);
1519 /* dejitter on bridge */
1520 if (p_bridge && !p_m_disable_dejitter)
1522 if (p_m_tx_dejitter != tx_dejitter) {
1523 p_m_tx_dejitter = tx_dejitter;
1524 PDEBUG(DEBUG_BCHANNEL, "we change dejitter mode to %s.\n", (p_m_tx_dejitter) ? "on" : "off");
1525 if (p_m_b_index > -1)
1526 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1527 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TX_DEJITTER, p_m_tx_dejitter, "DSP-TX_DEJITTER", p_m_tx_dejitter);
1531 static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, int i)
1533 struct mISDNport *mISDNport;
1537 int __attribute__((__unused__)) ret;
1539 /* unset global semaphore */
1541 // with a very small incident, upqueue_avail may be set by mISDN thread and
1542 // another byte may be sent to the pipe, which causes a call to this function
1543 // again with nothing in the upqueue. this is no problem.
1544 ret = read(fd->fd, &byte, 1);
1546 /* process all ports */
1547 mISDNport = mISDNport_first;
1549 /* handle queued up-messages (d-channel) */
1550 while ((mb = mdequeue(&mISDNport->upqueue))) {
1553 case MPH_ACTIVATE_IND:
1554 if (mISDNport->l1link != 1) {
1555 l1l2l3_trace_header(mISDNport, NULL, L1_ACTIVATE_IND, DIRECTION_IN);
1557 mISDNport->l1link = 1;
1561 case MPH_DEACTIVATE_IND:
1562 if (mISDNport->l1link != 0) {
1563 l1l2l3_trace_header(mISDNport, NULL, L1_DEACTIVATE_IND, DIRECTION_IN);
1565 mISDNport->l1link = 0;
1569 case MPH_INFORMATION_IND:
1570 PDEBUG(DEBUG_ISDN, "Received MPH_INFORMATION_IND for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1572 case L1_SIGNAL_LOS_ON:
1575 case L1_SIGNAL_LOS_OFF:
1578 case L1_SIGNAL_AIS_ON:
1581 case L1_SIGNAL_AIS_OFF:
1584 case L1_SIGNAL_RDI_ON:
1587 case L1_SIGNAL_RDI_OFF:
1590 case L1_SIGNAL_SLIP_TX:
1591 mISDNport->slip_tx++;
1593 case L1_SIGNAL_SLIP_RX:
1594 mISDNport->slip_rx++;
1599 case MT_L2ESTABLISH:
1600 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_IND, DIRECTION_IN);
1601 add_trace("tei", NULL, "%d", l3m->pid);
1603 mISDNport->l2link = 1;
1605 mISDNport->l2mask[l3m->pid >> 3] |= (1 << (l3m->pid & 7));
1606 if ((!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
1607 if (mISDNport->l2establish.active) {
1608 unsched_timer(&mISDNport->l2establish);
1609 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1616 mISDNport->l2mask[l3m->pid >> 3] &= ~(1 << (l3m->pid & 7));
1617 if (!mISDNport->l2establish.active) {
1618 l1l2l3_trace_header(mISDNport, NULL, L2_RELEASE_IND, DIRECTION_IN);
1619 add_trace("tei", NULL, "%d", l3m->pid);
1621 /* down if not nt-ptmp */
1622 if (!mISDNport->ntmode || mISDNport->ptp)
1623 mISDNport->l2link = 0;
1625 if ((!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
1626 if (!mISDNport->l2establish.active && mISDNport->l2hold) {
1627 PDEBUG(DEBUG_ISDN, "set timer and establish.\n");
1628 schedule_timer(&mISDNport->l2establish, 5, 0);
1629 mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
1635 /* l3-data is sent to LCR */
1636 stack2manager(mISDNport, l3m->type, l3m->pid, l3m);
1641 mISDNport = mISDNport->next;
1646 /* l2 establish timer fires */
1647 static int l2establish_timeout(struct lcr_timer *timer, void *instance, int i)
1649 struct mISDNport *mISDNport = (struct mISDNport *)instance;
1651 if (mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
1652 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link portnum=%d.\n", mISDNport->portnum);
1653 mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
1654 schedule_timer(&mISDNport->l2establish, 5, 0); /* 5 seconds */
1660 /* handle frames from bchannel */
1661 static int b_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int i)
1663 struct mISDNport *mISDNport = (struct mISDNport *)instance;
1664 unsigned char buffer[2048+MISDN_HEADER_LEN];
1665 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1668 ret = recv(fd->fd, buffer, sizeof(buffer), 0);
1670 PERROR("read error frame, errno %d\n", errno);
1673 if (ret < (int)MISDN_HEADER_LEN) {
1674 PERROR("read short frame, got %d, expected %d\n", ret, (int)MISDN_HEADER_LEN);
1678 /* we don't care about confirms, we use rx data to sync tx */
1682 /* we receive audio data, we respond to it AND we send tones */
1687 case PH_CONTROL_IND:
1688 if (mISDNport->b_port[i])
1689 mISDNport->b_port[i]->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1691 PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (socket %d), ignoring.\n", fd->fd);
1694 case PH_ACTIVATE_IND:
1695 case DL_ESTABLISH_IND:
1696 case PH_ACTIVATE_CNF:
1697 case DL_ESTABLISH_CNF:
1698 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", fd->fd);
1699 bchannel_event(mISDNport, i, B_EVENT_ACTIVATED);
1702 case PH_DEACTIVATE_IND:
1703 case DL_RELEASE_IND:
1704 case PH_DEACTIVATE_CNF:
1705 case DL_RELEASE_CNF:
1706 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", fd->fd);
1707 bchannel_event(mISDNport, i, B_EVENT_DEACTIVATED);
1711 PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, fd->fd, ret-MISDN_HEADER_LEN);
1717 /* process timer events for bchannel handling */
1718 static int b_timer_timeout(struct lcr_timer *timer, void *instance, int i)
1720 struct mISDNport *mISDNport = (struct mISDNport *)instance;
1722 bchannel_event(mISDNport, i, B_EVENT_TIMEOUT);
1728 int do_layer3(struct mlayer3 *ml3, unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1732 * l3m must be queued, except for MT_ASSIGN
1735 struct mISDNport *mISDNport = (struct mISDNport *)ml3->priv;
1738 #ifdef OLD_MT_ASSIGN
1739 /* special MT_ASSIGN handling:
1741 * if we request a PID from mlayer, we always do it while lcr is locked.
1742 * therefore we must check the MT_ASSIGN reply first before we lock.
1743 * this is because the MT_ASSIGN reply is received with the requesting
1744 * process, not by the mlayer thread!
1745 * this means, that the reply is sent during call of the request.
1746 * we must check if we get a reply and we know that we lcr is currently
1749 if (cmd==MT_ASSIGN && (pid&MISDN_PID_CR_FLAG) && (pid>>16)==MISDN_CES_MASTER) {
1750 /* let's do some checking if someone changes stack behaviour */
1751 if (mt_assign_pid != 0)
1752 FATAL("someone played with the mISDNuser stack. MT_ASSIGN not currently expected.\n");
1753 mt_assign_pid = pid;
1757 /* queue message, create, if required */
1759 l3m = alloc_l3_msg();
1761 FATAL("No memory for layer 3 message\n");
1763 mb = container_of(l3m, struct mbuffer, l3);
1766 mqueue_tail(&mISDNport->upqueue, mb);
1767 if (!upqueue_avail) {
1768 // multiple threads may cause multiple calls of this section, but this
1769 // results only in multiple processing of the upqueue read.
1770 // this is no problem.
1773 int __attribute__((__unused__)) ret;
1774 ret = write(upqueue_pipe[1], &byte, 1);
1779 int mISDN_getportbyname(int sock, int cnt, char *portname)
1781 struct mISDN_devinfo devinfo;
1785 while (port < cnt) {
1787 ret = ioctl(sock, IMGETDEVINFO, &devinfo);
1790 if (!strcasecmp(devinfo.name, portname))
1800 #ifdef ISDN_P_FXS_POTS
1801 /* handle frames from pots */
1802 static int pots_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int i)
1804 struct mISDNport *mISDNport = (struct mISDNport *)instance;
1805 unsigned char buffer[2048+MISDN_HEADER_LEN];
1806 struct mISDNhead *hh = (struct mISDNhead *)buffer;
1810 ret = recv(fd->fd, buffer, sizeof(buffer), 0);
1812 PERROR("read error frame, errno %d\n", errno);
1815 if (ret < (int)MISDN_HEADER_LEN) {
1816 PERROR("read short frame, got %d, expected %d\n", ret, (int)MISDN_HEADER_LEN);
1820 case PH_CONTROL_IND:
1821 cont = *((unsigned int *)(buffer + MISDN_HEADER_LEN));
1822 /* l1-control is sent to LCR */
1823 if (mISDNport->ntmode)
1824 stack2manager_fxs(mISDNport, cont);
1826 PERROR("FXO not supported!\n");
1828 case PH_ACTIVATE_REQ:
1832 PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, fd->fd, ret-MISDN_HEADER_LEN);
1840 * global function to add a new card (port)
1842 struct mISDNport *mISDNport_open(struct interface_port *ifport)
1845 struct mISDNport *mISDNport, **mISDNportp;
1846 int port = ifport->portnum;
1847 int ptp = ifport->ptp;
1848 int force_nt = ifport->nt;
1849 int l1hold = ifport->l1hold;
1850 int l2hold = ifport->l2hold;
1851 int ss5 = ifport->ss5;
1855 // struct mlayer3 *ml3;
1856 struct mISDN_devinfo devinfo;
1857 unsigned int protocol, prop;
1859 /* check port counts */
1860 ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1862 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1867 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1871 port = mISDN_getportbyname(mISDNsocket, cnt, ifport->portname);
1873 PERROR_RUNTIME("Port name '%s' not found, use 'misdn_info' tool to list all existing ports.\n", ifport->portname);
1876 // note: 'port' has still the port number
1878 if (port>cnt || port<0) {
1879 PERROR_RUNTIME("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 0, cnt);
1883 /* get port attributes */
1884 pri = bri = pots = nt = te = 0;
1886 ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1888 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", port, ret);
1891 if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1895 if (devinfo.Dprotocols & (1 << ISDN_P_NT_S0)) {
1899 if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1903 if (devinfo.Dprotocols & (1 << ISDN_P_NT_E1)) {
1907 #ifdef ISDN_P_FXS_POTS
1908 if (devinfo.Dprotocols & (1 << ISDN_P_FXO_POTS)) {
1912 if (devinfo.Dprotocols & (1 << ISDN_P_FXS_POTS)) {
1917 if (force_nt && !nt) {
1919 PERROR_RUNTIME("Port %d does not support NT-mode\n", port);
1921 PERROR_RUNTIME("Port %d does not support FXS-mode\n", port);
1925 PERROR_RUNTIME("Port %d supports BRI and PRI?? What kind of controller is that?. (Can't use this!)\n", port);
1928 if (!bri && !pri && !pots) {
1929 PERROR_RUNTIME("Port %d does not support BRI nor PRI nor POTS!\n", port);
1933 PERROR_RUNTIME("Port %d does not support NT-mode nor TE-mode!\n", port);
1936 /* set NT by turning off TE */
1939 /* if TE an NT is supported (and not forced to NT), turn off NT */
1943 PERROR_RUNTIME("Port %d uses FXO-mode, but not supported by LCR!\n", port);
1947 /* check for double use of port */
1949 mISDNport = mISDNport_first;
1951 if (mISDNport->portnum == port)
1953 mISDNport = mISDNport->next;
1956 PERROR_RUNTIME("Port %d already in use by LCR. You can't use a NT port multiple times.\n", port);
1961 /* check for continous channelmap with no bchannel on slot 16 */
1962 if (test_channelmap(0, devinfo.channelmap)) {
1963 PERROR_RUNTIME("Port %d provides channel 0, but we cannot access it!\n", port);
1967 while(i < (int)devinfo.nrbchan + 1) {
1969 if (test_channelmap(i, devinfo.channelmap)) {
1970 PERROR("Port %d provides bchannel 16. Pleas upgrade mISDN, if this port is mISDN loopback interface.\n", port);
1974 if (!test_channelmap(i, devinfo.channelmap)) {
1975 PERROR_RUNTIME("Port %d has no channel on slot %d!\n", port, i);
1982 /* add mISDNport structure */
1983 mISDNportp = &mISDNport_first;
1985 mISDNportp = &((*mISDNportp)->next);
1986 mISDNport = (struct mISDNport *)MALLOC(sizeof(struct mISDNport));
1987 add_timer(&mISDNport->l2establish, l2establish_timeout, mISDNport, 0);
1989 /* ss5 link is always active */
1990 mISDNport->l1link = 1;
1991 mISDNport->l2link = 1;
1993 mISDNport->l1link = -1;
1994 mISDNport->l2link = -1;
1997 *mISDNportp = mISDNport;
1999 /* if pri, must set PTP */
2003 /* set ss5 params */
2005 /* try to keep interface enabled */
2025 /* allocate ressources of port */
2028 protocol = (nt)?L3_PROTOCOL_DSS1_NET:L3_PROTOCOL_DSS1_USER;
2029 prop = (1 << MISDN_FLG_L2_CLEAN);
2030 if (ptp) // ptp forced
2031 prop |= (1 << MISDN_FLG_PTP);
2032 if (nt) // supports hold/retrieve on nt-mode
2033 prop |= (1 << MISDN_FLG_NET_HOLD);
2034 if (l1hold) // supports layer 1 hold
2035 prop |= (1 << MISDN_FLG_L1_HOLD);
2036 if (l2hold) // supports layer 2 hold
2037 prop |= (1 << MISDN_FLG_L2_HOLD);
2038 /* open layer 3 and init upqueue */
2039 /* queue must be initializes, because l3-thread may send messages during open_layer3() */
2040 mqueue_init(&mISDNport->upqueue);
2041 mISDNport->ml3 = open_layer3(port, protocol, prop , do_layer3, mISDNport);
2042 if (!mISDNport->ml3) {
2043 mqueue_purge(&mISDNport->upqueue);
2044 PERROR_RUNTIME("open_layer3() failed for port %d\n", port);
2052 "PORT (open failed)");
2054 mISDNport_close(mISDNport);
2058 #ifdef ISDN_P_FXS_POTS
2061 struct sockaddr_mISDN addr;
2062 struct mISDNhead act;
2065 /* queue must be initializes, because even pots interfaces are checked at mISDN_upqueue loop */
2066 mqueue_init(&mISDNport->upqueue);
2067 sock = socket(PF_ISDN, SOCK_DGRAM, (nt) ? ISDN_P_FXS_POTS : ISDN_P_FXO_POTS);
2069 PERROR_RUNTIME("Cannot open mISDN due to '%s'.\n", strerror(errno));
2072 /* bind socket to dchannel */
2073 addr.family = AF_ISDN;
2076 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
2078 PERROR_RUNTIME("Error: Failed to bind pots control channel\n");
2086 "PORT (open failed)");
2090 act.prim = PH_ACTIVATE_REQ;
2092 ret = sendto(sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
2094 PERROR("Failed to send to socket %d\n", sock);
2095 mISDNport->pots_sock.fd = sock;
2096 register_fd(&mISDNport->pots_sock, LCR_FD_READ, pots_sock_callback, mISDNport, i);
2100 SCPY(mISDNport->name, devinfo.name);
2101 mISDNport->b_num = devinfo.nrbchan;
2102 mISDNport->portnum = port;
2103 mISDNport->ntmode = nt;
2104 mISDNport->pots = pots;
2105 mISDNport->tespecial = ifport->tespecial;
2106 mISDNport->pri = pri;
2107 mISDNport->ptp = ptp;
2108 mISDNport->l1hold = l1hold;
2109 mISDNport->l2hold = l2hold;
2110 mISDNport->ss5 = ss5;
2111 PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
2113 while(i < mISDNport->b_num) {
2114 mISDNport->b_state[i] = B_STATE_IDLE;
2115 add_timer(&mISDNport->b_timer[i], b_timer_timeout, mISDNport, i);
2119 /* if ptp, pull up the link */
2120 if (!pots && mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
2121 mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
2122 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_REQ, DIRECTION_OUT);
2123 add_trace("tei", NULL, "%d", 0);
2125 schedule_timer(&mISDNport->l2establish, 5, 0); /* 5 seconds */
2128 /* for POTS or nt-mode ptmp the link is always up */
2129 if (pots || (mISDNport->ntmode && !mISDNport->ptp))
2130 mISDNport->l2link = 1;
2132 PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
2134 start_trace(mISDNport->portnum,
2143 add_trace("mode", NULL, (mISDNport->ntmode)?"network":"terminal");
2145 add_trace("mode", NULL, (mISDNport->ntmode)?"FXS":"FXO");
2146 add_trace("channels", NULL, "%d", mISDNport->b_num);
2148 add_trace("ccitt#5", NULL, "enabled");
2156 * load static port instances, if required by mISDNport
2158 void mISDNport_static(struct mISDNport *mISDNport)
2163 while(i < mISDNport->b_num) {
2166 ss5_create_channel(mISDNport, i);
2174 * function to free ALL cards (ports)
2176 void mISDNport_close_all(void)
2178 /* free all ports */
2179 while(mISDNport_first)
2180 mISDNport_close(mISDNport_first);
2184 * free only one port
2186 void mISDNport_close(struct mISDNport *mISDNport)
2188 struct mISDNport **mISDNportp;
2190 class PmISDN *isdnport;
2193 /* remove all port instance that are linked to this mISDNport */
2197 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN) {
2198 isdnport = (class PmISDN *)port;
2199 if (isdnport->p_m_mISDNport && isdnport->p_m_mISDNport == mISDNport) {
2200 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", isdnport->p_name, mISDNport->portnum);
2208 /* only if we are already part of interface */
2209 if (mISDNport->ifport) {
2210 start_trace(mISDNport->portnum,
2211 mISDNport->ifport->interface,
2221 /* free bchannels */
2223 while(i < mISDNport->b_num) {
2224 if (mISDNport->b_sock[i].inuse) {
2225 _bchannel_destroy(mISDNport, i);
2226 PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
2228 if (mISDNport->b_timer[i].inuse) {
2229 del_timer(&mISDNport->b_timer[i]);
2233 del_timer(&mISDNport->l2establish);
2235 /* close layer 3, if open */
2236 if (mISDNport->ml3) {
2237 close_layer3(mISDNport->ml3);
2240 /* close layer 1, if open */
2241 if (mISDNport->pots_sock.fd) {
2242 unregister_fd(&mISDNport->pots_sock);
2243 close(mISDNport->pots_sock.fd);
2247 mqueue_purge(&mISDNport->upqueue);
2249 /* remove from list */
2250 mISDNportp = &mISDNport_first;
2251 while(*mISDNportp) {
2252 if (*mISDNportp == mISDNport) {
2253 *mISDNportp = (*mISDNportp)->next;
2257 mISDNportp = &((*mISDNportp)->next);
2261 FATAL("mISDNport not in list\n");
2263 FREE(mISDNport, sizeof(struct mISDNport));
2270 * enque data from remote port
2272 int PmISDN::bridge_rx(unsigned char *data, int length)
2274 unsigned char buf[MISDN_HEADER_LEN+((length>p_m_preload)?length:p_m_preload)];
2275 struct mISDNhead *hh = (struct mISDNhead *)buf;
2278 if (p_m_b_index < 0)
2280 if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_ACTIVE)
2283 /* check if high priority tones exist
2284 * ignore data in this case
2286 if (p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on)
2289 /* preload procedure
2290 * if transmit buffer in DSP module is empty,
2291 * preload it to DSP_LOAD to prevent jitter gaps.
2293 * if load runs empty, preload again.
2295 if (p_m_disable_dejitter && p_m_load == 0 && p_m_preload > 0) {
2296 //printf("preload=%d\n", p_m_preload);
2297 hh->prim = PH_DATA_REQ;
2299 memset(buf+MISDN_HEADER_LEN, silence, p_m_preload);
2300 ret = sendto(p_m_mISDNport->b_sock[p_m_b_index].fd, buf, MISDN_HEADER_LEN+p_m_preload, 0, NULL, 0);
2302 PERROR("Failed to send to socket %d\n", p_m_mISDNport->b_sock[p_m_b_index].fd);
2303 p_m_load += p_m_preload;
2304 schedule_timer(&p_m_loadtimer, 0, PORT_TRANSMIT * 125);
2307 /* drop if load would exceed ISDN_MAXLOAD
2308 * this keeps the delay not too high
2310 //printf("load=%d len=%d 2*preload=%d\n", p_m_load, length, p_m_preload << 1);
2311 if (p_m_disable_dejitter && p_m_preload > 0 && p_m_load+length > (p_m_preload << 1))
2314 /* make and send frame */
2315 hh->prim = PH_DATA_REQ;
2317 memcpy(buf+MISDN_HEADER_LEN, data, length);
2318 ret = sendto(p_m_mISDNport->b_sock[p_m_b_index].fd, buf, MISDN_HEADER_LEN+length, 0, NULL, 0);
2320 PERROR("Failed to send to socket %d\n", p_m_mISDNport->b_sock[p_m_b_index].fd);
2326 int PmISDN::inband_send(unsigned char *buffer, int len)
2328 PERROR("this function must be derived to function!\n");
2332 void PmISDN::inband_send_on(void)
2334 PDEBUG(DEBUG_PORT, "turning inband signalling send on.\n");
2335 p_m_inband_send_on = 1;
2338 void PmISDN::inband_send_off(void)
2340 PDEBUG(DEBUG_PORT, "turning inband signalling send off.\n");
2341 p_m_inband_send_on = 0;
2344 void PmISDN::inband_receive(unsigned char *buffer, int len)
2347 // if (len >= SS5_DECODER_NPOINTS)
2348 // ss5_decode(buffer, SS5_DECODER_NPOINTS);
2349 PERROR("this function must be derived to function!\n");
2352 void PmISDN::inband_receive_on(void)
2354 /* this must work during constructor, see ss5.cpp */
2355 PDEBUG(DEBUG_PORT, "turning inband signalling receive on.\n");
2356 p_m_inband_receive_on = 1;
2360 void PmISDN::inband_receive_off(void)
2362 PDEBUG(DEBUG_PORT, "turning inband signalling receive off.\n");
2363 p_m_inband_receive_on = 0;
2367 void PmISDN::mute_on(void)
2371 PDEBUG(DEBUG_PORT, "turning mute on.\n");
2373 set_conf(p_m_conf, 0);
2376 void PmISDN::mute_off(void)
2380 PDEBUG(DEBUG_PORT, "turning mute off.\n");
2382 set_conf(0, p_m_conf);