+ if (mISDNport->b_socket[i] < 0)
+ return;
+ act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ;
+ act.id = 0;
+ ret = sendto(mISDNport->b_socket[i], &act, MISDN_HEADER_LEN, 0, NULL, 0);
+ if (ret <= 0)
+ PERROR("Failed to send to socket %d\n", mISDNport->b_socket[i]);
+
+ /* trace */
+ chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
+ add_trace("channel", NULL, "%d", i+1+(i>=15));
+ if (mISDNport->b_timer[i])
+ add_trace("event", NULL, "timeout recovery");
+ end_trace();
+}
+
+
+/*
+ * subfunction for bchannel_event
+ * set features
+ */
+static void _bchannel_configure(struct mISDNport *mISDNport, int i)
+{
+ struct PmISDN *port;
+ int handle;
+
+ if (mISDNport->b_socket[i] < 0)
+ return;
+ handle = mISDNport->b_socket[i];
+ port = mISDNport->b_port[i];
+ if (!port)
+ {
+ PERROR("bchannel index i=%d not associated with a port object\n", i);
+ return;
+ }
+
+ /* set dsp features */
+ if (port->p_m_txdata)
+ ph_control(mISDNport, port, handle, (port->p_m_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", port->p_m_txdata);
+ if (port->p_m_delay)
+ ph_control(mISDNport, port, handle, DSP_DELAY, port->p_m_delay, "DSP-DELAY", port->p_m_delay);
+ if (port->p_m_tx_gain)
+ ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_TX, port->p_m_tx_gain, "DSP-TX_GAIN", port->p_m_tx_gain);
+ if (port->p_m_rx_gain)
+ ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_RX, port->p_m_rx_gain, "DSP-RX_GAIN", port->p_m_rx_gain);
+ if (port->p_m_pipeline[0])
+ ph_control_block(mISDNport, port, handle, DSP_PIPELINE_CFG, port->p_m_pipeline, strlen(port->p_m_pipeline)+1, "DSP-PIPELINE", 0);
+ if (port->p_m_conf)
+ ph_control(mISDNport, port, handle, DSP_CONF_JOIN, port->p_m_conf, "DSP-CONF", port->p_m_conf);
+ if (port->p_m_echo)
+ ph_control(mISDNport, port, handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
+ if (port->p_m_tone)
+ ph_control(mISDNport, port, handle, DSP_TONE_PATT_ON, port->p_m_tone, "DSP-TONE", port->p_m_tone);
+ if (port->p_m_rxoff)
+ ph_control(mISDNport, port, handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
+// if (port->p_m_txmix)
+// ph_control(mISDNport, port, handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
+ if (port->p_m_dtmf)
+ ph_control(mISDNport, port, handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
+ if (port->p_m_crypt)
+ 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);
+}
+
+/*
+ * subfunction for bchannel_event
+ * destroy stack
+ */
+static void _bchannel_destroy(struct mISDNport *mISDNport, int i)
+{
+ if (mISDNport->b_socket[i] < 0)
+ return;
+ chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
+ add_trace("channel", NULL, "%d", i+1+(i>=15));
+ add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
+ end_trace();
+ close(mISDNport->b_socket[i]);
+ mISDNport->b_socket[i] = -1;
+}
+
+
+/*
+bchannel procedure
+------------------
+
+A bchannel goes through the following states in this order:
+
+- B_STATE_IDLE
+No one is using the bchannel.
+It is available and not linked to Port class, nor reserved.
+
+- B_STATE_ACTIVATING
+The bchannel stack is created and an activation request is sent.
+It MAY be linked to Port class, but already unlinked due to Port class removal.
+
+- B_STATE_ACTIVE
+The bchannel is active and cofigured to the Port class needs.
+Also it is linked to a Port class, otherwhise it would be deactivated.
+
+- B_STATE_DEACTIVATING
+The bchannel is in deactivating state, due to deactivation request.
+It may be linked to a Port class, that likes to reactivate it.
+
+- B_STATE_IDLE
+See above.
+After deactivating bchannel, and if not used, the bchannel becomes idle again.
+
+Also the bchannel may be exported, but only if the state is or becomes idle:
+
+- B_STATE_EXPORTING
+The bchannel assignment has been sent to the remove application.
+
+- B_STATE_REMOTE
+The bchannel assignment is acknowledged by the remote application.
+
+- B_STATE_IMPORTING
+The bchannel is re-imported by mISDN port object.
+
+- B_STATE_IDLE
+See above.
+After re-importing bchannel, and if not used, the bchannel becomes idle again.
+
+
+A bchannel can have the following events:
+
+- B_EVENT_USE
+A bchannel is required by a Port class.
+
+- B_EVENT_ACTIVATED
+The bchannel beomes active.
+
+- B_EVENT_DROP
+The bchannel is not required by Port class anymore
+
+- B_EVENT_DEACTIVATED
+The bchannel becomes inactive.
+
+- B_EVENT_EXPORTED
+The bchannel is now used by remote application.
+
+- B_EVENT_IMPORTED
+The bchannel is not used by remote application.
+
+- B_EVENT_EXPORTREQUEST
+The bchannel shall be exported to the remote application.
+
+- B_EVENT_IMPORTREQUEST
+The bchannel is released from the remote application.
+
+All actions taken on these events depend on the current bchannel's state and if it is linked to a Port class.
+
+if an export request is receive by remote application, p_m_remote_* is set.
+the b_remote_*[index] indicates if and where the channel is exported to. (set from the point on, where export is initiated, until imported is acknowledged.)
+- set on export request from remote application (if port is assigned)
+- set on channel use, if requested by remote application (p_m_remote_*)
+- cleared on drop request
+
+the bchannel will be exported with ref and stack given. remote application uses the ref to link bchannel to the call.
+the bchannel will be imported with stack given only. remote application must store stack id with the bchannel process.
+the bchannel import/export is acknowledged with stack given.
+
+if exporting, b_remote_*[index] is set to the remote socket id.
+if importing has been acknowledged. b_remote_*[index] is cleared.
+
+*/
+
+/*
+ * process bchannel events
+ * - mISDNport is a pointer to the port's structure
+ * - i is the index of the bchannel
+ * - event is the B_EVENT_* value
+ * - port is the PmISDN class pointer
+ */
+void bchannel_event(struct mISDNport *mISDNport, int i, int event)
+{
+ class PmISDN *b_port = mISDNport->b_port[i];
+ int state = mISDNport->b_state[i];
+ double timer = mISDNport->b_timer[i];
+ unsigned int p_m_remote_ref = 0;
+ unsigned int p_m_remote_id = 0;
+ int p_m_tx_gain = 0;
+ int p_m_rx_gain = 0;
+ char *p_m_pipeline = NULL;
+ unsigned char *p_m_crypt_key = NULL;
+ int p_m_crypt_key_len = 0;
+ int p_m_crypt_key_type = 0;
+ unsigned int portid = (mISDNport->portnum<<8) + i+1+(i>=15);
+
+ if (b_port)
+ {
+ p_m_remote_id = b_port->p_m_remote_id;
+ p_m_remote_ref = b_port->p_m_remote_ref;
+ p_m_tx_gain = b_port->p_m_tx_gain;
+ p_m_rx_gain = b_port->p_m_rx_gain;
+ p_m_pipeline = b_port->p_m_pipeline;
+ p_m_crypt_key = b_port->p_m_crypt_key;
+ p_m_crypt_key_len = b_port->p_m_crypt_key_len;
+ p_m_crypt_key_type = /*b_port->p_m_crypt_key_type*/1;
+ }
+
+ switch(event)
+ {
+ case B_EVENT_USE:
+ /* port must be linked in order to allow activation */
+ if (!b_port)
+ FATAL("bchannel must be linked to a Port class\n");
+ switch(state)