+ int elapsed, length;
+
+ if ((ret = Port::handler()))
+ return(ret);
+
+ /* send tone data to isdn device only if we have data */
+ if (p_tone_fh>=0 || p_tone_fetched || p_m_crypt_msg_loops)
+ {
+ /* calculate how much to transmit */
+ if (!p_last_tv.sec)
+ {
+ elapsed = ISDN_PRELOAD << 3; /* preload for the first time */
+ } else
+ {
+ elapsed = 1000 * (now_tv.sec - p_last_tv_sec)
+ + (now_tv.usec/1000) - p_last_tv_msec;
+ /* gap was greater preload, so only fill up to preload level */
+ if (elapsed > ISDN_PRELOAD)
+ {
+ elapsed = ISDN_PRELOAD << 3
+ }
+ }
+ if (elapsed >= ISDN_TRANSMIT)
+ {
+ unsigned char buf[mISDN_HEADER_LEN+(ISDN_PRELOAD<<3)];
+ iframe_t *frm = (iframe_t *)buf;
+
+ length = read_audio(buffer, elapsed);
+ p_last_tv_sec = now_tv.sec;
+ p_last_tv_msec = now_tv.usec/1000;
+
+ if (p_m_crypt_msg_loops)
+ {
+ /* send pending message */
+ int tosend;
+ check!!
+ tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
+ if (tosend > length)
+ tosend = length;
+ memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
+ p_m_crypt_msg_current += tosend;
+ if (p_m_crypt_msg_current == p_m_crypt_msg_len)
+ {
+ p_m_crypt_msg_current = 0;
+ p_m_crypt_msg_loops--;
+ }
+ }
+ frm->prim = DL_DATA | REQUEST;
+ frm->addr = p_m_b_addr | FLG_MSG_DOWN;
+ frm->dinfo = 0;
+ frm->len = length;
+ memcpy(&frm->data.p, buffer, length);
+ mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
+
+ if (p_debug_nothingtosend)
+ {
+ p_debug_nothingtosend = 0;
+ PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
+ }
+ return(1);
+ }
+ } else
+ {
+ p_last_tv.sec = p_last_tv.msec = 0; /* flag that we don't transmit data */
+ if (!cwp_debug_nothingtosend)
+ {
+ p_debug_nothingtosend = 1;
+ PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
+ }
+ }