--- wancfg_zaptel.pl	2009-03-10 13:07:30.000000000 -0400
+++ ../wancfg_zaptel_new/wancfg_zaptel.pl	2009-03-17 19:21:54.000000000 -0400
@@ -2,13 +2,18 @@
 # config-zaptel.pl 
 # Sangoma Dahdi/Zaptel/TDM API/SMG Configuration Script.
 #
-# Copyright     (c) 2008, Sangoma Technologies Inc.
+# Copyright     (c) 2009, Sangoma Technologies Inc.
 #
 #               This program is free software; you can redistribute it and/or
 #               modify it under the terms of the GNU General Public License
 #               as published by the Free Software Foundation; either version
 #               2 of the License, or (at your option) any later version.
 # ----------------------------------------------------------------------------
+# Mar 17   2009  2.27   Jignesh Patel  Added HWDTMF Option for BRI
+# Mar 10   2009  2.26   Yannick Lam     Added Config only Option 
+# Jan 09   2009	 2.25	Jignesh Patel 	Removed LAW option for FlexBRI-analog
+# Dec 11   2008  2.24   Konrad Hammmel  Added support for BUSID and multiple USBFXO
+# Nov 26   2008  2.23	Jignesh Patel   Added support for B600 and B700 
 # Oct 16   2008  2.22	Jignesh Patel	Added Support for A600 
 # Oct 07   2008  2.21   Jignesh Patel 	Dahdi Soft-EC conf support for Analog & A101/2 cards
 # Sep 30   2008  2.20	Jignesh Patel   Configuration Support for Dahdi
@@ -46,9 +51,9 @@
 print "\n########################################################################";
 print "\n#    		           Sangoma Wanpipe                              #";
 print "\n#        Dahdi/Zaptel/SMG/TDMAPI/BOOT Configuration Script             #";
-print "\n#                             v2.22                                    #";
+print "\n#                             v2.27                                    #";
 print "\n#                     Sangoma Technologies Inc.                        #";
-print "\n#                        Copyright(c) 2008.                            #";
+print "\n#                        Copyright(c) 2009.                            #";
 print "\n########################################################################\n\n";
 
 use strict;
@@ -59,6 +64,7 @@
 use A10u;
 use A20x;
 use A50x;
+use U10x;
 
 
 
@@ -101,6 +107,7 @@
 my $no_boot=$FALSE;
 my $boot_only=$FALSE;
 my $no_hwdtmf=$FALSE;
+my $usb_device_support=$TRUE;
 my $startup_string="";
 my $cfg_string="";
 my $first_cfg=1;
@@ -115,6 +122,9 @@
 my $current_zap_channel=1;
 my $num_analog_devices=0;
 my $num_analog_devices_total=0;
+my $num_usb_devices=0;
+my $num_usb_devices_total=0;
+my $num_usb_devices=0;
 my $num_bri_devices=0;
 my $num_bri_devices_total=0;
 my $num_digital_devices=0;
@@ -316,6 +326,9 @@
 config_t1e1();
 config_bri();
 config_analog();
+if($usb_device_support == $TRUE && $os_type_list =~ m/Linux/) {
+	config_usbfxo();
+}
 config_tdmv_dummy();
 summary();
 apply_changes();
@@ -334,7 +347,6 @@
 
 
 #######################################FUNCTIONS##################################################
-
 sub get_card_name{
 	my ($card_name) = @_;
 	if ( $card_name eq '600' || $card_name eq '700') {
@@ -345,6 +357,7 @@
 	return $card_name;
 }
 
+
 sub set_zaptel_hwhdlc{
 	print "Checking for native zaptel hardhdlc support...";
         my $cnt = 0;
@@ -612,9 +625,12 @@
 			if ( ! -d "$wanpipe_conf_dir/scripts" ) {
 				exec_command("mkdir -p $wanpipe_conf_dir/scripts");
 			} 
-						
 			exec_command("cp -f $current_dir/templates/$zaptel_cfg_script $wanpipe_conf_dir/scripts/start");
-			
+		} elsif(-e "$wanpipe_conf_dir/scripts/start") {
+				print ("\nWould you like to remove old wanrouter start scripts ?\n");
+				if (&prompt_user_list("YES","NO","") eq 'YES'){
+					exec_command("rm -rf $wanpipe_conf_dir/scripts/start");
+				}
 		}
 	}
 }
@@ -698,11 +714,55 @@
 					"Save cfg: Restart Asterisk & Wanpipe when convenient",
 					"Save cfg: Stop Asterisk & Wanpipe now", 
 					"Save cfg: Stop Asterisk & Wanpipe when convenient",
+					"Save cfg: Save cfg only", 
 					"Do not save cfg: Exit",
 					"");
 	}
 
 	
+	if ($res =~ m/cfg only/){
+		
+
+		print "\nRemoving old configuration files...\n";
+
+        	exec_command("rm -f $wanpipe_conf_dir/wanpipe*.conf");
+
+        	gen_wanrouter_rc();
+
+        	print "\nCopying new Wanpipe configuration files...\n";
+        	copy_config_files();
+        	if($num_bri_devices != 0){
+                	print "\nCopying new sangoma_brid configuration files ($bri_conf_file_t)...\n";
+                	exec_command("cp -f $bri_conf_file $bri_conf_file_t");
+                	exec_command("cp -f $woomera_conf_file $woomera_conf_file_t");
+        	}
+        	if ($zaptel_dahdi_installed==$TRUE){
+                	if($config_zaptel==$TRUE){
+                        	if ($num_zaptel_config !=0){
+                                	print "\nCopying new $zaptel_string configuration file ($zaptel_conf_file_t)...\n";
+                                	exec_command("cp -f $zaptel_conf_file $zaptel_conf_file_t");
+                        	}
+                	}
+        	}
+
+        	if ($config_zapata==$TRUE || $is_trixbox==$TRUE){
+                	if ($num_zaptel_config !=0){
+                        	print "\nCopying new $zapata_string configuration files ($zapata_conf_file_t)...\n";
+                        	exec_command("cp -f $zapata_conf_file $zapata_conf_file_t");
+                	}
+        	}
+                print "Saving files only\n";
+
+		if ($os_type_list =~ m/FreeBSD/){
+        	config_boot_freebsd();
+		} else {
+        		config_boot_linux();
+			}
+
+                exit 0;
+	}
+
+
 	if ($res =~ m/Exit/){
 		print "No changes made to your configuration files\n";
 		exit 0;
@@ -748,10 +808,6 @@
 				}
 			}
 
-
-
-
-
 		}else {
 			print "\nAsterisk is not running...\n";
 		}
@@ -902,7 +958,7 @@
 		@options = ("PSTN", "INTERNAL");
 	}
 	if ($silent==$FALSE){
-		printf ("Select dialplan context for AFT-%s on port %s\n", get_card_name($card_model), $card_port);
+		printf ("Select dialplan context for AFT-A%s on port %s\n", get_card_name($card_model), $card_port);
 		my $res = &prompt_user_list(@options,$def_zapata_context);
 		if($res eq "PSTN"){
 			$context="from-zaptel";
@@ -1005,7 +1061,7 @@
 	#update comments for zapata.conf or chan_dahdi.conf
 	my $zapfile="";
 	if (!open (FH,"$zapata_conf_file")) {
-# 		printf("Unable to modify $zapata_conf_file\n");
+		printf("Unable to modify $zapata_conf_file\n");
 		
 	}
 	while (<FH>) {
@@ -1193,7 +1249,9 @@
 		print("  $num_digital_devices_total T1/E1 port(s) detected, $num_digital_devices configured\n");
 		print("  $num_bri_devices_total ISDN BRI port(s) detected, $num_bri_devices configured\n");
 		print("  $num_analog_devices_total analog card(s) detected, $num_analog_devices configured\n");
+  	    print("  $num_usb_devices_total usb device(s) detected, $num_usb_devices configured\n");
 
+		
 		print "\nConfigurator will create the following files:\n";
 		print "\t1. Wanpipe config files in $wanpipe_conf_dir\n";
 		$file_list++;
@@ -1659,15 +1717,16 @@
 	}
 	$first_cfg=0;
 	print "------------------------------------\n";
-	print "Configuring ISDN BRI cards [A500]\n";
+	print "Configuring ISDN BRI cards [A500/B700]\n";
 	print "------------------------------------\n";
 	my $skip_card=$FALSE;
 	$zaptel_conf.="\n";
 	$zapata_conf.="\n";
 	foreach my $dev (@hwprobe) {
-		if ( $dev =~ /A(\d+)(.*):.*SLOT=(\d+).*BUS=(\d+).*PORT=(\w+).*HWEC=(\w+).*/){
+		if ( $dev =~ /.*AFT-A(\d+)(.*):.*SLOT=(\d+).*BUS=(\d+).*PORT=(\d+).*HWEC=(\w+).*/ ||
+			 $dev =~ /.*AFT-B(\d+)(.*):.*SLOT=(\d+).*BUS=(\d+).*PORT=(\d+).*HWEC=(\w+).*/){
 			$skip_card=$FALSE;
-			if ($1 eq '500'){
+			if ($1 eq '500' || ($1 eq '700' && $5 < '5')){
 				my $card = eval {new Card(); } or die ($@);
 
 				$card->current_dir($current_dir);
@@ -1687,7 +1746,7 @@
 					$card->hwec_mode('YES');
 				}
 
-				if ($card->card_model eq '500'){
+				if ($card->card_model eq '500' || $card->card_model eq '700'){
 					$num_bri_devices_total++;
 					if($5 eq '1'){
 						$bri_device_has_master_clock=$FALSE;
@@ -1734,6 +1793,13 @@
 						$num_bri_devices++;
 						$card->tdmv_span_no($current_tdmapi_span);
 						$current_tdmapi_span++;
+						if ($silent==$FALSE){
+							if ($card->hwec_mode eq "YES"){
+								$card->hw_dtmf(&prompt_hw_dtmf());
+							} else {
+								$card->hw_dtmf("NO");
+							}
+						}
 					}else{
 						printf ("%s on slot:%s bus:%s port:%s not configured\n", 												get_card_name($card->card_model), $3, $4, $5);
 						prompt_user("Press any key to continue");
@@ -1748,7 +1814,7 @@
 	 		my $group="";
 			my $bri_pos=$a50x->card->tdmv_span_no;
 			
-	 		printf("\nConfiguring port %d on AFT-%s [slot:%d bus:%d span:%d]\n", $a50x->fe_line(), get_card_name($a50x->card->card_model()), $a50x->card->pci_slot(), $a50x->card->pci_bus(), $current_tdmapi_span-1);
+			printf("\nConfiguring port %d on AFT-%s [slot:%d bus:%d span:%d]\n", $a50x->fe_line(), get_card_name($a50x->card->card_model()), $a50x->card->pci_slot(), $a50x->card->pci_bus(), $current_tdmapi_span-1);
 			my $conn_type=get_bri_conn_type($a50x->fe_line());
 			my $country=get_bri_country();
 			my $operator=get_bri_operator();
@@ -1794,14 +1860,14 @@
 					$current_bri_default_tei=$def_bri_default_tei;
 				}
 				printf("\nConfiguring span:%s as TEI:%s\n", $bri_pos, $current_bri_default_tei);
-                                if($silent==$FALSE){
-                                        my @options = ("YES - Keep this setting", "NO  - Specify a different TEI");
-                                        my $res = &prompt_user_list(@options, "YES");
-                                        if ($res =~ m/NO/) {
-                                                $def_bri_default_tei_opt=$TRUE;
-                                                $current_bri_default_tei=get_bri_default_tei();	
-                                        }
-                                }
+  				if($silent==$FALSE){
+					my @options = ("YES - Keep this setting", "NO  - Specify a different TEI");
+					my $res = &prompt_user_list(@options, "YES");
+					if ($res =~ m/NO/) {
+						$def_bri_default_tei_opt=$TRUE;
+						$current_bri_default_tei=get_bri_default_tei();
+						}
+				}
 				if ($def_bri_default_tei_opt==$FALSE){
 					$bri_conf.=$a50x->gen_bri_conf($bri_pos,"bri_te", $group, $country, $operator, $conn_type, '');
 				} else { 
@@ -2333,6 +2399,7 @@
 				if($silent==$FALSE){
 					my @options = ("NORMAL", "MASTER");
 					printf ("Select clock for AFT-%s on port %s [slot:%s bus:%s span:$devnum]\n", get_card_name($card->card_model), $port, $card->pci_slot, $card->pci_bus);
+
 					$def_feclock=&prompt_user_list(@options, $def_feclock);
 				} else {
 					if($#silent_feclocks >= 0){
@@ -2358,28 +2425,7 @@
 					$def_te_ref_clock=&get_te_ref_clock(@device_normal_clocks);
 					$a10x->te_ref_clock($def_te_ref_clock);
 				}
-
-
 								
-				if ($silent==$FALSE){
-					if ($card->hwec_mode eq "YES"){
-						$card->hw_dtmf(&prompt_hw_dtmf());
-					} else {
-						$card->hw_dtmf("NO");
-					}
-				} else {
-					if($#silent_hwdtmfs >= 0){
-						$silent_hwdtmf=pop(@silent_hwdtmfs);
-					}
-					if ($card->hwec_mode eq "YES" && $no_hwdtmf==$FALSE){
-						$card->hw_dtmf($silent_hwdtmf);
-					} else {
-						$card->hw_dtmf("NO");
-					}
-				} 
-
-
-				
 				my @options="";	
 				if ($is_smg==$TRUE && $zaptel_dahdi_installed==$TRUE){
 					@options = ("PRI CPE", "PRI NET", "E & M", "E & M Wink", "FXS - Loop Start", "FXS - Ground Start", "FXS - Kewl Start", "FX0 - Loop Start", "FX0 - Ground Start", "FX0 - Kewl Start", "SS7 - Sangoma Signal Media Gateway", "No Signaling (Voice Only)");
@@ -2604,6 +2650,30 @@
 						$a10x->pri_switchtype($silent_pri_switchtype);
 					}
 				}
+
+				# prompt the user if they would like to enable HW DTMF...HW_DTMF needs to be disabled for SMG
+				if (!($a10x->signalling eq 'SS7 - Sangoma Signal Media Gateway'| $a10x->signalling eq 'No Signaling (Voice Only)')){
+					if ($silent==$FALSE){
+						printf("\n");
+						if ($card->hwec_mode eq "YES"){
+							$card->hw_dtmf(&prompt_hw_dtmf());
+						} else {
+							$card->hw_dtmf("NO");
+						}
+					} else {
+						if($#silent_hwdtmfs >= 0){
+							$silent_hwdtmf=pop(@silent_hwdtmfs);
+						}
+						if ($card->hwec_mode eq "YES" && $no_hwdtmf==$FALSE){
+							$card->hw_dtmf($silent_hwdtmf);
+						} else {
+							$card->hw_dtmf("NO");
+						}
+					}
+				}else{
+					$card->hw_dtmf("NO");
+				} 
+
 				#wanpipe gen section		
 				if( $a10x->signalling eq 'SS7 - Sangoma Signal Media Gateway' ){
 					$a10x->ss7_subinterface(1);
@@ -2731,17 +2801,17 @@
 				}elsif ($is_tdm_api == $FALSE && $is_hp_tdm_api == $FALSE ){
 					$zaptel_conf.=$a10x->gen_zaptel_conf($dchan_str);
 				}
-					$devnum++;
-					$num_digital_devices++;
-					my $msg ="\nPort ".$port." on AFT-A".$card->card_model." configuration complete...\n";
-					print "$msg";
-					if($silent==$FALSE){
-						prompt_user("Press any key to continue");
-					}
 
-				}  	
-	
+				# done with this port time to move on to the next port
+				$devnum++;
+				$num_digital_devices++;
+				my $msg ="\nPort ".$port." on AFT-A".$card->card_model." configuration complete...\n";
+				print "$msg";
+				if($silent==$FALSE){
+					prompt_user("Press any key to continue");
+				}
 
+				}				
 			} 
 
 		}
@@ -2769,7 +2839,7 @@
 	}
 	$first_cfg=0;
 	print "------------------------------------\n";
-	print "Configuring analog cards [A200/A400/B600]\n";
+	print "Configuring analog cards [A200/A400/B600/B700]\n";
 	print "------------------------------------\n";
 
 	my $skip_card=$FALSE;
@@ -2779,8 +2849,10 @@
 	}
 	foreach my $dev (@hwprobe) {
 		
-		
-	        if ( $dev =~ /(\d+).(\w+\w+).*SLOT=(\d+).*BUS=(\d+).*.*HWEC=(\d+)/){
+			        
+				
+		if ( ($dev =~ /(\d+).(\w+\w+).*SLOT=(\d+).*BUS=(\d+).*CPU=(\w+).*PORT=(\w+).*HWEC=(\d+)/) ||
+			 ($dev =~ /(\d+).(\w+\w+).*SLOT=(\d+).*BUS=(\d+).*(\w+).*PORT=(\d+).*HWEC=(\d+)/)){
 
 			$skip_card=$FALSE;
 			my $card = eval {new Card(); } or die ($@);
@@ -2790,9 +2862,8 @@
 			$card->card_model($1);
 			$card->pci_slot($3);
 			$card->pci_bus($4);
-			#fe_cpu is not used for analog cards
-			#$card->fe_cpu($5);
-			my $hwec=$5;
+			$card->fe_cpu($5);
+			my $hwec=$7;
 			if($dahdi_installed == $TRUE) {
 					$card->dahdi_conf('YES');
 					$card->dahdi_echo($dahdi_echo)
@@ -2804,12 +2875,12 @@
 				$card->hwec_mode('YES');
 				
 			}
-			if ($card->card_model eq '200' | $card->card_model eq '400' | $card->card_model eq '600'){
+			if ($card->card_model eq '200' || $card->card_model eq '400' || $card->card_model eq '600' || ($card->card_model eq '700' && $6 == '5')){
 				$num_analog_devices_total++;
 				if($silent==$FALSE) {
 					system('clear');
 					print "\n-----------------------------------------------------------\n";
-					print "AFT-$1 detected on slot:$3 bus:$4\n";
+					print "A$1 detected on slot:$3 bus:$4\n";
 					print "-----------------------------------------------------------\n";
 				}
 				if($is_trixbox==$FALSE){
@@ -2845,7 +2916,11 @@
 						} else {
 							$card->hw_dtmf("NO");
 						}
-						$a20x->tdm_law(&prompt_tdm_law());
+						if ($card->card_model eq '700'){
+							$a20x->tdm_law("ALAW");
+						} else {
+							$a20x->tdm_law(&prompt_tdm_law());
+						}
 						$a20x->tdm_opermode(&prompt_tdm_opemode());
 		
 					} else {
@@ -2941,7 +3016,7 @@
 sub config_tdmv_dummy 
 {
 	my $command='';
-	if( $num_digital_devices == 0 && $num_analog_devices == 0 &&  $num_bri_devices !=0 && $zaptel_dahdi_installed==$TRUE && $os_type_list =~ m/Linux/  && $silent == $FALSE ){
+	if( $num_digital_devices == 0 && $num_analog_devices == 0 &&  $num_usb_devices == 0 && $num_bri_devices !=0 && $zaptel_dahdi_installed==$TRUE && $os_type_list =~ m/Linux/  && $silent == $FALSE ){
 		system('clear');
 		print("Would you like to configure A500 BRI card as timing source for $zaptel_string?\n");
 		print("(Visit http://wiki.sangoma.com/wanpipe-linux-asterisk-appendix#bri-tdmv for more information)\n");		
@@ -2961,9 +3036,129 @@
 			}	
 		} 
 	}
+}
 
+sub config_usbfxo{
 
-	
-	
-}
+	my $u10x;
+	if (!$first_cfg && $silent==$FALSE) {
+		system('clear');
+	}
+	$first_cfg=0;
+	print "------------------------------------\n";
+	print "Configuring USB devices [U100]\n";
+	print "------------------------------------\n";
 
+	my $skip_card=$FALSE;
+	if($is_tdm_api == $FALSE) {
+		$zaptel_conf.="\n";
+		$zapata_conf.="\n";
+	}
+	foreach my $dev (@hwprobe) {
+			if ( $dev =~ m/ *.(\D\d+).*BUSID=(\d-\d.*\s).* /){
+		
+			$skip_card=$FALSE;
+			my $card = eval {new Card(); } or die ($@);
+			$card->current_dir($current_dir);
+			$card->cfg_dir($cfg_dir);
+			$card->device_no($devnum);
+			$card->card_model($1);
+			$card->pci_bus($2);
+			
+			if ($card->card_model eq 'U100'){
+				$num_usb_devices_total++;
+				
+				if($silent==$FALSE) {
+					system('clear');
+					print "\n-----------------------------------------------------------\n";
+					print "USB $1 detected on  bus:$2\n";
+					print "-----------------------------------------------------------\n";
+				}
+				if($is_trixbox==$FALSE){
+					if ($silent==$FALSE){
+						print "\nWould you like to configure USB $1 on bus:$2\n";
+						if (&prompt_user_list(("YES","NO","")) eq 'NO'){
+							$skip_card=$TRUE;	
+						}
+					}
+				}
+				if ($skip_card==$FALSE){
+					
+					$u10x = eval {new U10x(); } or die ($@);
+					$u10x->card($card);
+					$card->first_chan($current_zap_channel);
+			
+									
+					if ($silent==$FALSE){
+						$u10x->tdm_law(&prompt_tdm_law());
+						$u10x->tdm_opermode(&prompt_tdm_opemode());
+		
+					} else {
+												
+						if($#silent_tdm_laws >= 0){
+							$silent_tdm_law=pop(@silent_tdm_laws);
+						}						
+						
+						$u10x->tdm_law($silent_tdm_law);
+						
+					} 
+			
+					$startup_string.="wanpipe$devnum ";
+					$cfg_string.="wanpipe$devnum ";
+					
+					if($silent==$FALSE){
+						prompt_user("Press any key to continue");
+					}
+					my $i;
+					if( $is_tdm_api == $FALSE) {
+						print "$1 configured on  bus:$2 span:$current_zap_span\n";
+						$zaptel_conf.="#Sangoma USB $1  [bus:$2 span:$current_zap_span] <wanpipe".$u10x->card->device_no.">\n";
+						$zapata_conf.=";Sangoma A$1 [slot:$3 bus:$4 span:$current_zap_span]  <wanpipe".$u10x->card->device_no.">\n";
+						$current_zap_channel+=2;	
+						$num_zaptel_config++;
+						$card->tdmv_span_no($current_zap_span);
+						$current_zap_span++;
+					}else{
+						print "A$1 configured on slot:$3 bus:$4 span:$current_tdmapi_span\n";
+						$u10x->is_tdm_api($TRUE);
+						$card->tdmv_span_no($current_tdmapi_span);
+						$current_tdmapi_span++;
+					}
+						$devnum++;
+						$num_usb_devices++;
+					if ($os_type_list =~ m/FreeBSD/){
+						$u10x->gen_wanpipe_conf(1);
+					} else {
+						$u10x->gen_wanpipe_conf(0);
+					}
+					for (my $mod = 0; $mod <= 1; $mod++){
+						my $zap_pos = $mod+$current_zap_channel-2;
+						if($silent==$TRUE & $silent_zapata_context_opt == $TRUE){
+							if($#silent_zapata_contexts >= 0){
+								$silent_zapata_context=pop(@silent_zapata_contexts);
+							}
+							$u10x->card->zap_context($silent_zapata_context);
+						} else {
+							$u10x->card->zap_context("from-zaptel");
+						}
+						$u10x->card->zap_group("0");
+						$zaptel_conf.=$u10x->gen_zaptel_conf($zap_pos,"fxs");
+						$zapata_conf.=$u10x->gen_zapata_conf($zap_pos,"fxs");
+					}
+					
+				}else{
+					print "$1 on  bus:$2 not configured\n";
+					prompt_user("Press any key to continue");
+				}
+			}
+		
+		}
+	}
+	if($num_usb_devices_total!=0){
+		print("\nUSB analog card configuration complete\n\n");
+		if( $silent==$FALSE){
+			prompt_user("Press any key to continue");
+		}
+	}
+
+}
