+#ifdef SOCKET_MISDN
+int mISDN_handler(void)
+{
+ int ret, work = 0;
+ struct mISDNport *mISDNport;
+ class PmISDN *isdnport;
+ int i;
+ unsigned char buffer[2048+MISDN_HEADER_LEN];
+ struct mISDNhead *hh = (struct mISDNhead *)buffer;
+ struct mbuffer *mb;
+ struct l3_msg *l3m;
+
+ /* process all ports */
+ mISDNport = mISDNport_first;
+ while(mISDNport)
+ {
+ /* process all bchannels */
+ i = 0;
+ while(i < mISDNport->b_num)
+ {
+ /* process timer events for bchannel handling */
+ if (mISDNport->b_timer[i])
+ {
+ if (mISDNport->b_timer[i] <= now_d)
+ bchannel_event(mISDNport, i, B_EVENT_TIMEOUT);
+ }
+ /* handle port of bchannel */
+ isdnport=mISDNport->b_port[i];
+ if (isdnport)
+ {
+ /* call bridges in user space OR crypto OR recording */
+ if (isdnport->p_m_joindata || isdnport->p_m_crypt_msg_loops || isdnport->p_m_crypt_listen || isdnport->p_record)
+ {
+ /* rx IS required */
+ if (isdnport->p_m_rxoff)
+ {
+ /* turn on RX */
+ isdnport->p_m_rxoff = 0;
+ PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
+ if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+ ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], DSP_RECEIVE_ON, 0, "DSP-RXOFF", 0);
+ return(1);
+ }
+ } else
+ {
+ /* rx NOT required */
+ if (!isdnport->p_m_rxoff)
+ {
+ /* turn off RX */
+ isdnport->p_m_rxoff = 1;
+ PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
+ if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+ ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
+ return(1);
+ }
+ }
+ /* recording */
+ if (isdnport->p_record)
+ {
+ /* txdata IS required */
+ if (!isdnport->p_m_txdata)
+ {
+ /* turn on RX */
+ isdnport->p_m_txdata = 1;
+ PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is required, so we turn them on\n");
+ if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+ ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], DSP_TXDATA_ON, 0, "DSP-TXDATA", 1);
+ return(1);
+ }
+ } else
+ {
+ /* txdata NOT required */
+ if (isdnport->p_m_txdata)
+ {
+ /* turn off RX */
+ isdnport->p_m_txdata = 0;
+ PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is not required, so we turn them off\n");
+ if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
+ ph_control(mISDNport, isdnport, mISDNport->b_socket[isdnport->p_m_b_index], DSP_TXDATA_OFF, 0, "DSP-TXDATA", 0);
+ return(1);
+ }
+ }
+ }
+
+ /* handle message from bchannel */
+ if (mISDNport->b_socket[i] > -1)
+ {
+ ret = recv(mISDNport->b_socket[i], buffer, sizeof(buffer), 0);
+ if (ret >= (int)MISDN_HEADER_LEN)
+ {
+ work = 1;
+ switch(hh->prim)
+ {
+ /* we don't care about confirms, we use rx data to sync tx */
+ case PH_DATA_CNF:
+ break;
+
+ /* we receive audio data, we respond to it AND we send tones */
+ case PH_DATA_IND:
+ case DL_DATA_IND:
+ case PH_CONTROL_IND:
+ if (mISDNport->b_port[i])
+ mISDNport->b_port[i]->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
+ else
+ PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (socket %d), ignoring.\n", mISDNport->b_socket[i]);
+ break;
+
+ case PH_ACTIVATE_IND:
+ case DL_ESTABLISH_IND:
+ case PH_ACTIVATE_CNF:
+ case DL_ESTABLISH_CNF:
+ PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", mISDNport->b_socket[i]);
+ bchannel_event(mISDNport, i, B_EVENT_ACTIVATED);
+ break;
+
+ case PH_DEACTIVATE_IND:
+ case DL_RELEASE_IND:
+ case PH_DEACTIVATE_CNF:
+ case DL_RELEASE_CNF:
+ PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", mISDNport->b_socket[i]);
+ bchannel_event(mISDNport, i, B_EVENT_DEACTIVATED);
+ break;
+
+ default:
+ PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, mISDNport->b_socket[i], ret-MISDN_HEADER_LEN);
+ }
+ } else
+ {
+ if (ret < 0 && errno != EWOULDBLOCK)
+ PERROR("Read from port %d, index %d failed with return code %d\n", mISDNport->portnum, i, ret);
+ }
+ }
+
+ i++;
+ }
+
+ /* handle queued up-messages (d-channel) */
+ while ((mb = mdequeue(&mISDNport->upqueue)))
+ {
+ l3m = &mb->l3;
+ switch(l3m->type)
+ {
+#warning SOCKET TBD: Layer 1 indication
+#if 0
+ case MT_L1ACTIVATE:
+ l1l2l3_trace_header(mISDNport, NULL, L1_ACTIVATE_IND, DIRECTION_IN);
+ end_trace();
+ mISDNport->l1link = 1;
+ break;
+
+ case MT_L1DEACTIVATE:
+ l1l2l3_trace_header(mISDNport, NULL, L1_DEACTIVATE_IND, DIRECTION_IN);
+ end_trace();
+ mISDNport->l1link = 0;
+ break;
+
+ case MT_L1CONTROL:
+ PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
+ // special config commands for interface (ip-address/LOS/AIS/RDI/SLIP)
+ break;
+#endif
+
+ case MT_L2ESTABLISH:
+ l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_IND, DIRECTION_IN);
+ end_trace();
+ if ((!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127)
+ {
+ if (mISDNport->l2establish)
+ {
+ mISDNport->l2establish = 0;
+ PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
+ }
+ mISDNport->l2link = 1;
+ }
+ break;
+
+ case MT_L2RELEASE:
+ l1l2l3_trace_header(mISDNport, NULL, L2_RELEASE_IND, DIRECTION_IN);
+ end_trace();
+ if ((!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127)
+ {
+ mISDNport->l2link = 0;
+ if (mISDNport->l2hold)
+ {
+ time(&mISDNport->l2establish);
+ PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
+ }
+ }
+ break;
+
+ default:
+ /* l3-data is sent to LCR */
+ stack2manager(mISDNport, l3m->type, l3m->pid, l3m);
+ }
+ }
+
+ /* free message */
+ free_l3_msg(l3m);
+
+#if 0
+ if (mISDNport->l1timeout && now>mISDNport->l1timeout)
+ { ---}
+ PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
+ mISDNport->l1timeout = 0;
+#endif
+
+ /* layer 2 establish timer */
+ if (mISDNport->l2establish)
+ {
+ if (now-mISDNport->l2establish > 5)
+ {
+ mISDNport->l2establish = 0;
+ if (mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode))
+ {
+
+ PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link portnum=%d.\n", mISDNport->portnum);
+ mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
+ l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_REQ, DIRECTION_OUT);
+ end_trace();
+ time(&mISDNport->l2establish);
+ return(1);
+ }
+ }
+ }
+
+
+ mISDNport = mISDNport->next;
+ }
+
+ /* if we received at least one b-frame, we will return 1 */
+ return(work);
+}
+#else