From 024a043e2c2fcf5c64ae4326db42cceb360fb60f Mon Sep 17 00:00:00 2001
From: James Zhang <jzhang@sangoma.com>
Date: Thu, 9 Feb 2012 14:04:45 -0500
Subject: [PATCH] freetdm: initial fix to rx cgb

---
 .../ftmod_sangoma_ss7_handle.c                | 54 ++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/libs/freetdm/src/ftmod/ftmod_sangoma_ss7/ftmod_sangoma_ss7_handle.c b/libs/freetdm/src/ftmod/ftmod_sangoma_ss7/ftmod_sangoma_ss7_handle.c
index 41223199b0..0999eed4d8 100644
--- a/libs/freetdm/src/ftmod/ftmod_sangoma_ss7/ftmod_sangoma_ss7_handle.c
+++ b/libs/freetdm/src/ftmod/ftmod_sangoma_ss7/ftmod_sangoma_ss7_handle.c
@@ -2338,7 +2338,8 @@ ftdm_status_t handle_cgb_req(uint32_t suInstId, uint32_t spInstId, uint32_t circ
 	int					blockType = 0;
 	int					byte = 0;
 	int					bit = 0;
-	int 				x;
+	int 					x;
+	int					loop_range=0;
 
 	ftdm_running_return(FTDM_FAIL);
 
@@ -2405,6 +2406,56 @@ ftdm_status_t handle_cgb_req(uint32_t suInstId, uint32_t spInstId, uint32_t circ
 	}
 
 	/* loop over the cics starting from circuit until range+1 */
+	loop_range = circuit + range + 1;
+	x circuit;
+	while( x < loop_range ) {
+		if (g_ftdm_sngss7_data.cfg.isupCkt[x].type != SNG_CKT_VOICE)  {
+			loop_range++;
+		}
+		else {
+			if (extract_chan_data(x, &sngss7_info, &ftdmchan)) {
+				SS7_ERROR("Failed to extract channel data for circuit = %d!\n", x);
+				break;
+			}
+		
+			ftdm_mutex_lock(ftdmchan->mutex);
+			if (status[byte] & (1 << bit)) {
+				switch (blockType) {
+				/**********************************************************************/
+				case 0:	/* maintenance oriented */
+					sngss7_set_ckt_blk_flag(sngss7_info, FLAG_GRP_MN_BLOCK_RX);
+					break;
+				/**********************************************************************/
+				case 1: /* hardware failure oriented */
+					sngss7_set_ckt_blk_flag(sngss7_info, FLAG_GRP_HW_BLOCK_RX);
+					break;
+				/**********************************************************************/
+				case 2: /* reserved for national use */
+					break;
+				/**********************************************************************/
+				default:
+					break;
+				/**********************************************************************/
+				}
+			}
+
+			/* bring the sig status down */
+			sngss7_set_sig_status(sngss7_info, FTDM_SIG_STATE_DOWN);
+
+			/* unlock the channel again before we exit */
+			ftdm_mutex_unlock(ftdmchan->mutex);
+
+			/* update the bit and byte counter*/
+			bit ++;
+			if (bit == 8) {
+				byte++;
+				bit = 0;
+			}
+		}
+		x++;
+	}
+
+#if 0
 	for (x = circuit; x < (circuit + range + 1); x++) {
 		/* confirm this is a voice channel */
 		if (g_ftdm_sngss7_data.cfg.isupCkt[x].type != SNG_CKT_VOICE) continue;
@@ -2460,6 +2511,7 @@ ftdm_status_t handle_cgb_req(uint32_t suInstId, uint32_t spInstId, uint32_t circ
 		}
 
 	} /* for (x = circuit; x < (circuit + range + 1); x++) */
+#endif
 
 	/* get the ftdmchan and ss7_chan_data from the circuit */
 	if (extract_chan_data(circuit, &sngss7_info, &ftdmchan)) {