mpd.script 41.6 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3
#################################################################
#
# Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#
# Subject to the following obligations and disclaimer of warranty,
# use and redistribution of this software, in source or object code
# forms, with or without modifications are expressly permitted by
# Whistle Communications; provided, however, that:   (i) any and
# all reproductions of the source or object code must include the
# copyright notice above and the following disclaimer of warranties;
# and (ii) no rights are granted, in any manner or form, to use
# Whistle Communications, Inc. trademarks, including the mark "WHISTLE
# COMMUNICATIONS" on advertising, endorsements, or otherwise except
# as such appears in the above copyright notice or in the software.
#
# THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS",
# AND TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS
# MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED,
# REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE, OR NON-INFRINGEMENT.  WHISTLE COMMUNICATIONS DOES NOT
# WARRANT, GUARANTEE, OR MAKE ANY REPRESENTATIONS REGARDING THE USE
# OF, OR THE RESULTS OF THE USE OF THIS SOFTWARE IN TERMS OF ITS
# CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.  IN NO EVENT
# SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES RESULTING
# FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING WITHOUT
# LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED
# AND UNDER ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS
# IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# This BSD compatable licence re-added for clarity.
Ad Schellevis's avatar
Ad Schellevis committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#
#################################################################

##
## MPD Modem script variables:
##
##  $DialPrefix		Modem dialing prefix (eg. "DT")
##  $Telephone		Telephone number to dial (not duplicated using &)
##  $ConnectTimeout	Wait-for-connect timeout, in seconds (default 45 secs)
##  $SpeakerOff		Set to "yes" to quiet modem speakers
##  $Serial230K		If "yes", we can support 230K serial port speed
##  $CountryCode	Country code for Winmodem
##  $InitString		External initialization string
##  $LoginScript	If "yes", look for script login
##
51
## OPNsense specific variables:
Ad Schellevis's avatar
Ad Schellevis committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
##	$APN			Access Point (host)Name for 3G connections
##	$APNum			Access Point Number, typically "1", might not matter
##	$SimPin			SIM card PIN number
##	$PinWait		Wait for SIM to connect to network after PIN entered
##
## ISDN Terminal Adapter specific variables (all start with "TA_"):
##
##  $TA_Bonding		Bonding on TA: "yes" or "no"
##  $TA_NoDoubleTelno	When $TA_Bonding, don't double the dialed number
##  $TA_56K		Restrict to 56K on TA: "yes" or "no"
##  $TA_VoiceCall	Originate calls using voice mode
##  $TA_AuthChap	Tell TA to use CHAP: "yes" or "no"
##  $TA_Dirno1		TA directory #1
##  $TA_Dirno2		TA directory #2
##  $TA_SPID1		SPID #1
##  $TA_SPID2		SPID #2
##  $TA_SwitchType	One of these values
##				"NI-1"
##				"DMS-100"
##				"5ESS P2P"
##				"5ESS MP"
##  $TA_NewSwitch	Means the switch type is new, initiate auto-detect
##			(3Com Impact IQ only)
##
## We set $OptimizeNextTime to "yes" after a successful connection so we
## can avoid verifing configuration when things are working good.
##
## Internal variables:
##
##  $ModemSetupFunc	Routine to set up modem for dialing out
##  $ModemAnsSetupFunc	Routine to set up modem for answer mode (if different)
##  $ModemDetectRing	Routine to detect an incoming call for ringback
##  $ModemIsAnalog	If "yes" modem is analog (ie, not terminal adapter)
##

#################################################################
#
#	MODEM DIALING
#
#################################################################

DialPeer:
	set $CallingID ""
	set $CalledID $Telephone
	if $Telephone == "00000" goto DialNullModem
	set $optimize $OptimizeNextTime
	set $OptimizeNextTime "no"

	if $optimize == "yes" goto DialPeer2
	call ModemFind
	if $ErrorMsg == "" goto DialPeer1
	log $ErrorMsg
	failure
DialPeer1:
	set $ModTelephone $Telephone
	call ModemIdent
	if $ModemDescription != "" goto DialPeer2
	log "No match found in the ModemIdent function in the chat script."
	failure

DialPeer2:
	log "Detected $ModemDescription."
	call $ModemSetupFunc
	log "Dialing server at $Telephone..."
	call ModemDial
	if $dialResult == "OK" goto DialPeerOK
	set $optimize "no"
	failure

DialPeerOK:
	if $ConnectionSpeed == "" log "Connected at an unknown speed."
	if $ConnectionSpeed == "" goto DialPeer3
	log "Connected at $ConnectionSpeed."
DialPeer3:
	if $LoginScript == "yes" call AutoLogin
	set $OptimizeNextTime "yes"
	success

DialPeerSetPin:
	set $modemCmd "+CPIN?"
	log $modemCmd
	call ModemQuery
	log $modemQuery
	if $modemQuery match ".*READY.*" goto DialPinReady
	set $modemCmd "+CPIN=\"$SimPin\""
	call ModemCmd2
	wait $PinWait

DialPinReady:
	return

DialPeerSetAPN:
	set $modemCmd "+CGDCONT=$APNum,\"IP\",\"$APN\""
	log $modemCmd
	call ModemCmd2
	return
148

Ad Schellevis's avatar
Ad Schellevis committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
# Null-modem connection
DialNullModem:
	log "Connected via null modem connection."
	success

##
## Dial modem
##
## Variables:
##
##  $DialPrefix		Modem dialing prefix (eg. "DT")
##  $ModTelephone	Telephone number to dial
##  $ConnectTimeout	Wait-for-connect timeout, in seconds (default 45 secs)
##  $noDialToneSubr	(optional) Subroutine to call if NO DIALTONE
##  $dialErrorSubr	(optional) Subroutine to call if ERR
##
## Returns:
##
##  $dialResult		"OK" or "FAIL"
##  $ConnectionSpeed	Connection speed reported by modem (possibly empty)
##

ModemDial:
	set $dialResult "FAIL"
	set $ConnectionSpeed ""
	if $ConnectTimeout == "" set $ConnectTimeout 45
	print "AT${DialPrefix}${ModTelephone}\r\n"
	log "AT${DialPrefix}${ModTelephone}"
	match "NO CARRIER" DialAbortNoCar
	match "NO DIAL" DialAbortNoDial
	match "BUSY" DialAbortBusy
	regex "CONNECT *([0-9]*).*$" DialConnect
	match "ERR" DialError
	wait $ConnectTimeout
	log "No response from the modem after dialing."
	return
DialAbortNoCar:
	log "The remote modem did not answer."
	return
DialAbortNoDial:
	if $noDialToneSubr != "" goto $noDialToneSubr
	log "No dialtone. Is the modem plugged in?"
	return
DialError:
	if ${ModTelephone} != "" goto DialErrorInit
	log "Invalid empty telephone number."
	return
DialErrorInit:
	if $dialErrorSubr != "" goto $dialErrorSubr
	log "Invalid dial init string."
	return
DialAbortBusy:
	log "The line was busy."
	return
DialConnect:
	set $ConnectionSpeed $matchedString1
	set $dialResult "OK"
	return

#################################################################
#
#	MODEM ANSWERING
#
#################################################################

##
## This is an idle script that waits for an incoming call and answers it
##
## Variables:
##
##  $RingTimeout	How long to wait for a RING before giving up
##  $ConnectTimeout	Wait-for-connect timeout, in seconds (default 45 secs)
##

AnswerCall:
	set $CallingID ""
	set $CalledID ""
	set $optimize $OptimizeNextTime
	set $OptimizeNextTime "no"

# Skip modem detection if we connected successfully last time

	if $optimize == "yes" goto AnswerCall2
	call ModemFind
	if $ErrorMsg == "" goto AnswerCall0
	log $ErrorMsg
	failure
AnswerCall0:
	call ModemIdent
	if $ModemDescription != "" goto AnswerCall1
	log "The modem is not responding."
	failure
AnswerCall1:
	log "Detected $ModemDescription."

AnswerCall2:
	if $ModemAnsSetupFunc == "" set $ModemAnsSetupFunc $ModemSetupFunc
	call $ModemAnsSetupFunc
	log "Waiting for ring..."
	call ModemAnswer
	if $answerReturn == "OK" goto AnswerCallOK
	set $IdleResult ""
	set $optimize "no"
	failure

AnswerCallOK:
	log "Connected at $ConnectionSpeed."
	set $OptimizeNextTime "yes"
	set $IdleResult "answer"
	success

#################################################################
#
#	MODEM RINGBACK
#
#################################################################

##
## This is an idle script that implements the ringback feature.
## When we're idle, and we detect an incoming call, then call back
## to bring up the link. For analog modems, we have to wait until
## the ringing stops before dialing back (otherwise we'd answer the
## call, which we don't want to do).
##
## Variables:
##
##  $RingbackTimeout	How long before giving up (reset and try again).
##			Default: 60 minutes
##  $RingStoppedTime	Max time between consecutive "RING"s (if analog)
##			Default: 8 seconds
##

Ringback:
	set $CallingID ""
	set $CalledID ""
	if $RingbackTimeout == "" set $RingbackTimeout "3600"
	if $RingStoppedTime == "" set $RingStoppedTime "8"
	set $ModemDetectRing RingbackWait
	call ModemFind
	if $ErrorMsg == "" goto Ringback1
	log $ErrorMsg
	failure
Ringback1:
	call ModemIdent
	if $ModemDescription != "" goto Ringback2
	log "The modem is not responding."
	failure

Ringback2:
	log "Detected $ModemDescription."
	goto $ModemDetectRing

# Detect an incoming call by waiting for a "RING" indication
# On an analog modem, we have to wait for the ringing to stop

RingbackWait:
	match "RING\r\n" RingbackGotRings
	log "Remote Dial-Back mode enabled; waiting for incoming call."
	wait $RingbackTimeout
	failure

# We saw it ring; wait for the ringing to stop (analog modems only).

RingbackGotRings:
	log "Incoming call detected..."
	if $ModemIsAnalog != "yes" goto RingbackDone
RingbackWaitStop:
	match "RING\r\n" RingbackWaitStop
	wait $RingStoppedTime
RingbackDone:
	set $IdleResult "ringback"
	success

# Strip leading and trailing spaces and log calling party number

RingbackDetectCID:
	if $cid match " *(.*) *" nop
	set $cid $matchedString1
	if $cid == "" goto RingbackDetectNoCID
	log "Incoming call detected from $cid..."
	success

# We couldn't determine calling party number

RingbackDetectNoCID:
	log "Incoming call detected..."
	success

#################################################################
#
#	MODEM IDENTIFICATION
#
#################################################################

##
## Identify
##
## This is meant to be called from the top level, to just identify
## what's on the serial port and print out some info about it.
##

Identify:
	call ModemFind
	if $ErrorMsg == "" goto Identify1
	failure
Identify1:
	call ModemIdent
	if $ModemDescription == "" failure
	log "ANALOG=$ModemIsAnalog"
	log "DESCRIPTION=$ModemDescription"
	success

##
## ModemIdent
##
## This identifies the type of modem and sets these variables accordingly:
##
##	$ModemDescription
##	$ModemSetupFunc
##	$ModemAnsSetupFunc
##	$ModemDetectRing
##	$ModemIsAnalog
##
## If no response seen, this sets $ModemDescription to the empty string.
##

ModemIdent:
	set $ModemDescription ""
	set $ModemSetupFunc ""
	set $ModemAnsSetupFunc ""
	set $ModemDetectRing ""
	set $ModemIsAnalog "yes"
	if $InitString != "" goto ModemIdentCustom
	print "ATI\r\n"
	match "ADTRAN EXPRESS XR" ModemIdentAdtranXRT
	match "Model: Ovation MC950D Card" ModemIdentMC950D
	match "ERR" ModemIdentGeneric
	match "OK\r\n" ModemIdentGeneric
	wait 3
	print "ATI1\r\n"
	match "NTK omni.net" ModemIdentNTK
	match "ERR" ModemIdentGeneric
	match "OK\r\n" ModemIdentGeneric
	wait 3
	print "ATI3\r\n"
	match "Courier" ModemIdentUsr
	match "Sportster" ModemIdentUsr
	match "3ComImpact IQ" ModemIdentImpactIQ
	match "U.S. Robotics 56K" ModemIdentUsr
	match "ZOOM V.90" ModemIdentZoom56
	match "LT V.90" ModemIdentLucent
	match "ERR"
	match "OK\r\n"
	wait 3
	print "ATI4\r\n"
	match "AtermIT NEC Corporation" ModemIdentAterm
	match "INSMATEV-7 NTT Corporation" ModemIdentAterm
	match "MNP Class 10 V.34 Modem" ModemIdentDeskPorte
	match "Smart One 56" ModemIdentSmartOne
	match "ERR"
	match "OK\r\n"
	wait 3
	print "ATI5\r\n"
	match "C885" ModemIdentC885
	match "ERR"
	match "OK\r\n" ModemIdentGeneric
	wait 3
	print "ATI8\r\n"
	match "BitSURFR PRO\r" ModemIdentBitsurfr
	match "BitSURFR PRO EZ" ModemIdentBitsurfrEZ
	match "3C882" ModemIdentImpactIQ
	match "ERR"
	match "OK\r\n"
	wait 3
	log "The modem is not responding to any ATI[1,3-5,8] commands."
	failure

ModemIdentGeneric:
	set $ModemDescription "Hayes compatible modem"
	if $SimPin != "" call DialPeerSetPin
	if $APN != "" call DialPeerSetAPN
	set $ModemSetupFunc GenericSetup
	return

ModemIdentCustom:
	set $ModemDescription "Custom modem"
	set $ModemSetupFunc CustomSetup
	return

ModemIdentUsr:
	set $SportsterHack "no"
	if $matchedString == "Sportster" set $SportsterHack "yes"
	set $ModemDescription "USR $matchedString modem"
	call GetOK
	set $ModemSetupFunc UsrSetup
	return
ModemIdentC885:
	set $ModemDescription "Sierra Wireless Compass $matchedString USB 3G modem"
	set $ModemSetupFunc GenericSetup
	return
ModemIdentMC950D:
	set $ModemDescription "Novatel Wireless $matchedString USB 3G modem"
	set $ModemSetupFunc GenericSetup
	return
ModemIdentBitsurfrEZ:
	set $bitsEZ "yes"
ModemIdentBitsurfr:
	set $ModemDescription "Motorola $matchedString"
	call GetOK
	set $ModemSetupFunc BitsurfrSetup
	set $ModemIsAnalog "no"
	set $ModemDetectRing BitsurfrDetectRing
	return

ModemIdentImpactIQ:
	set $matchedString "3ComImpact IQ"
	set $ModemDescription "$matchedString"
	call GetOK
	set $ModemSetupFunc ImpactIQSetup
	set $ModemIsAnalog "no"
	set $ModemDetectRing ImpactIQDetectRing
	return

ModemIdentAdtranXRT:
	set $ModemDescription "AdTran Express XR/XRT"
	call GetOK
	set $ModemSetupFunc AdtranXRTSetup
	set $ModemIsAnalog "no"
	return

ModemIdentAterm:
	set $ModemDescription "NEC Aterm TA"
	call GetOK
	set $ModemSetupFunc AtermSetup
	set $ModemIsAnalog "no"
	return

ModemIdentNTK:
	set $ModemDescription "$matchedString"
	call GetOK
	set $ModemSetupFunc NTKSetup
	set $ModemIsAnalog "no"
	return

ModemIdentDeskPorte:
	set $ModemDescription "$matchedString"
	call GetOK
	set $ModemSetupFunc DeskPorteSetup
	return

ModemIdentZoom56:
ModemIdentSmartOne:
	set $ModemDescription "$matchedString"
	call GetOK
	set $ModemSetupFunc Modem56Setup
	return

# Support the Lucent Winmodem, Xircom 56k
ModemIdentLucent:
	set $ModemDescription "$matchedString"
	call GetOK
	set $ModemSetupFunc ModemLucentSetup
	return

#################################################################
#
#	GENERIC MODEM SETUP
#
#################################################################

GenericSetup:
	set $noDialToneSubr GenericNoDialtone
	set $temp "M1"
	if $SpeakerOff == "yes" set $temp "M0"
	set $modemCmd "&F&C1&D2E0S0=0${temp}"
	call ModemCmd2
	return

CustomSetup:
	set $noDialToneSubr GenericNoDialtone
	set $modemCmd "${InitString}"
	call ModemCmd2
	return

GenericNoDialtone:
	log "No dialtone. Is the modem plugged in?"
	return

#################################################################
#
#	USR MODEM SETUP
#
#################################################################

UsrSetup:
# Lower baudrate to 57600 for crappy internal Sportster modem
	if $SportsterHack != "yes" goto UsrSetup2
	if $modemDevice != "/dev/cuad2" goto UsrSetup2
	set $Baudrate 57600
	set $modemCmd ""
	call ModemCmd2
UsrSetup2:
	set $noDialToneSubr GenericNoDialtone
	set $temp "M1"
	if $SpeakerOff == "yes" set $temp "M0"
	set $modemCmd "&F1&C1&D2E0S0=0S13.0=1L0S6=5${temp}"
	call ModemCmd2
	return

#################################################################
#
#	GENERAL 56K MODEM SETUP
#
#################################################################

Modem56Setup:
	set $noDialToneSubr GenericNoDialtone
	set $temp "M1"
	if $SpeakerOff == "yes" set $temp "M0"
	set $modemCmd "&FL2W2E0${temp}"
	call ModemCmd2
	return

#################################################################
#
#	LUCENT WINMODEM AND XIRCOM 56K SETUP
#
#################################################################

ModemLucentSetup:
	set $noDialToneSubr GenericNoDialtone
	set $temp "M1"
	if $SpeakerOff == "yes" set $temp "M0"
	set $tempCountryCode ""
	if $CountryCode != "" set $tempCountryCode "+GCI=${CountryCode}"
	if $CountryCode != "" log "Use country ${CountryCode}"
	set $modemCmd "&FL2W2E0${temp}${tempCountryCode}"
	call ModemCmd2
	return

#################################################################
#
#	BITSURFR PRO AND BITSURFR PRO EZ SETUP
#
#################################################################

BitsurfrSetup:
	set $noDialToneSubr BitsurfrNoDialtone
	set $factory "1"
	if $bitsEZ == "yes" set $factory "0"
	set $modemCmd "Z&F${factory}&C1&D2E0W1X2%A2=95S0=0"
	call ModemCmd2

# Set to 230K baud if we support it

	if $bitsEZ != "yes" goto BitsurfrSetup1
	set $modemCmd "@P2=230400"
	set $newBaudrate "230400"
	call SetBaudrate

# Check software revision and ISDN settings

BitsurfrSetup1:
	if $optimize == "yes" goto BitsurfrSetup2
	if $bitsEZ != "yes" call BitsurfrCheckRev
	call BitsurfrCheckConfig

# Set the PAP/CHAP, multi-link, and 56K/64K settings, and return

BitsurfrSetup2:
	set $authCmd "@M2=P"
	if $TA_AuthChap == "yes" set $authCmd "@M2=C"
	set $bondCmd "@B0=1"
	if $TA_Bonding == "yes" set $bondCmd "@B0=2"
	set $bearCmd "%A4=0"
	if $TA_56K == "yes" set $bearCmd "%A4=1"
	if $TA_VoiceCall == "yes" set $bearCmd "%A4=1"
	set $voiceCmd "%A98=D%A96=0"
	if $TA_VoiceCall == "yes" set $voiceCmd "%A98=S%A96=1"

# BS PRO EZ changes

	if $bitsEZ == "yes" set $authCmd "${authCmd}@M20=\"\""
	set $modemCmd "$authCmd$bondCmd$bearCmd$voiceCmd"
	call ModemCmd2
	if $TA_NoDoubleTelno == "yes" return
	if $TA_Bonding == "yes" set $ModTelephone "${Telephone}&${Telephone}"
	return

##
## What to do when NO DIALTONE
##

BitsurfrNoDialtone:
	log "ISDN initialization failed (or BitSURFR restarting). Please verify proper line connection and ISDN settings."
	return

##
## BitsurfrCheckConfig
##
## Verify and adjust ISDN configuration as necessary
##

BitsurfrCheckConfig:
	log "Checking the BitSURFR's ISDN configuration..."
	set $valueChanged "no"

# Check switch type

	set $checkCmd "!C0"
	set $checkValue "000"
	set $checkValueNew "0"
	if $TA_SwitchType == "DMS-100" set $checkValue "001"
	if $TA_SwitchType == "DMS-100" set $checkValueNew "1"
	if $TA_SwitchType == "NI-1" set $checkValue "002"
	if $TA_SwitchType == "NI-1" set $checkValueNew "2"
	set $checkMsg "Reprogramming switch type ($TA_SwitchType)..."
	call ModemCheckValue

	set $checkCmd "!C1"
	set $checkValue "004"
	set $checkValueNew "4"
	if $TA_SwitchType == "DMS-100" set $checkValue "003"
	if $TA_SwitchType == "DMS-100" set $checkValueNew "3"
	if $TA_SwitchType == "5ESS P2P" set $checkValue "000"
	if $TA_SwitchType == "5ESS P2P" set $checkValueNew "0"
	if $TA_SwitchType == "5ESS MP" set $checkValue "001"
	if $TA_SwitchType == "5ESS MP" set $checkValueNew "1"
	set $checkMsg "Reprogramming switch software version..."
	call ModemCheckValue

# Check directory numbers

	set $checkCmd "*1!N1"
	set $checkValue $TA_Dirno1
	set $checkValueNew $TA_Dirno1
	set $checkMsg "Reprogramming voice line #1 directory number..."
	call ModemCheckValue

	set $checkCmd "*2!N1"
	set $checkValue $TA_Dirno2
	set $checkValueNew $TA_Dirno2
	set $checkMsg "Reprogramming voice line #2 directory number..."
	call ModemCheckValue
	set $checkCmd "!N1"
	set $checkMsg "Reprogramming data line directory number..."
	call ModemCheckValue

# Check SPIDs

	set $checkCmd "*1!C6"
	set $checkValue $TA_SPID1
	set $checkValueNew $TA_SPID1
	set $checkMsg "Reprogramming voice line #1 SPID..."
	call ModemCheckValue

	set $checkCmd "*2!C6"
	set $checkValue $TA_SPID2
	set $checkValueNew $TA_SPID2
	set $checkMsg "Reprogramming voice line #2 SPID..."
	call ModemCheckValue
	set $checkCmd "!C6"
	set $checkMsg "Reprogramming data line SPID..."
	call ModemCheckValue

# Restart if necessary

	if $valueChanged == "no" return
	log "Restarting BitSURFR Pro with new configuration..."
	set $modemCmd ">W"
	call ModemCmd2
	set $modemCmd ">Z"
	call ModemCmd2
	failure

##
## Verify the BitSURFR's software revision. Only do this
## once, the first time we try to dial.
##

BitsurfrCheckRev:
	log "Checking the BitSURFR's software revision..."
	print "ATI3\r\n"
	match "-1A" BitsurfrCheckRevOld
	match "-1B" BitsurfrCheckRevOld
	match "-1C" BitsurfrCheckRevOld
	match "-1D" BitsurfrCheckRevOld
	match "-1E" BitsurfrCheckRevOld
	match "-1F" BitsurfrCheckRevOld
	match "-1G" BitsurfrCheckRevOld
	match "-1H" BitsurfrCheckRevOld
	match "-1I" BitsurfrCheckRevOld
	match "OK\r\n" BitsurfrCheckRevOK
	match "ERR"
	wait 5
	log "The BitSURFR did not properly answer ATI3."
	failure
BitsurfrCheckRevOK:
	return
BitsurfrCheckRevOld:
	log "The BitSURFR Pro has an old software revision $matchedString. Please upgrade according to the manufacturer's instructions."
	call GetOK
	failure

# This is how we detect an incoming call with a BitSURFR

BitsurfrDetectRing:
# First, we need to be properly configured
	call BitsurfrSetup
# Enable Caller-ID logging
	set $modemCmd "@N0=1*1@N0=1*2@N0=1"
	call ModemCmd
	if $modemCmdResult != "OK" goto RingbackWait
# A "RING" at any time is good enough
	match ringset "RING\r\n" RingbackDone
# Clear Caller-ID buffers
	set $modemCmd "@L0*1@L0*2@L0"
	call ModemCmd2
# Read each buffer
	call BitsurfrDetectReadCID
	log "Remote Dial-Back mode enabled; waiting for incoming call."
# Now wait indefinitely for one of those buffers to change
BitsurfrDetectLoop:
	wait 5
	set $oldDataCID $dataCID
	set $oldVoice1CID $voice1CID
	set $oldVoice2CID $voice2CID
	call BitsurfrDetectReadCID
	if $oldDataCID != $dataCID goto BitsurfrDetectDone
	if $oldVoice1CID != $voice1CID goto BitsurfrDetectDone
	if $oldVoice2CID != $voice2CID goto BitsurfrDetectDone
	goto BitsurfrDetectLoop

# Examine the CID from each port
BitsurfrDetectDone:
	set $cid $dataCID
	call BitsurfrDetectTryCID
	set $cid $voice1CID
	call BitsurfrDetectTryCID
	set $cid $voice2CID
	call BitsurfrDetectTryCID
	goto RingbackDetectNoCID

# Try to extract the calling party number
BitsurfrDetectTryCID:
# Strip the log number
	if $cid match "[0-9] (.*)" set $cid $matchedString1
# Check for various strings
	if $cid match "BLOCKED" return
	if $cid match "NO NUMBER" return
	if $cid match "NO CID SERVICE" return
	if $cid match "OUT OF AREA" return
	goto RingbackDetectCID

# Read Caller-ID buffers
BitsurfrDetectReadCID:
	set $modemCmd "@L1"
	call ModemQueryStrip
	set $dataCID $modemQuery
	set $modemCmd "*1@L1"
	call ModemQueryStrip
	set $voice1CID $modemQuery
	set $modemCmd "*2@L1"
	call ModemQueryStrip
	set $voice2CID $modemQuery
	return

#################################################################
#
#	3COM IMPACT IQ SETUP
#
#################################################################

ImpactIQSetup:
	set $noDialToneSubr ImpactIQNoDialtone
	set $modemCmd "Z&F%C2E0"
	call ModemCmd2

# Set to 230K baud if we support it

	set $modemCmd "$$B230400"
	set $newBaudrate "230400"
	call SetBaudrate

# Check ISDN config and link status (XXX should we check revision too?)

ImpactIQSetup1:
	if $optimize == "yes" goto IQInitSetup2
	call ImpactIQCheckConfig
	call ImpactIQCheckLink

# Now set the PAP/CHAP setting and voice/data originate mode setting

IQInitSetup2:
	set $authCmd "S84=1"
	if $TA_AuthChap == "yes" set $authCmd "S84=0"
	set $voiceCmd "S61=0"
	if $TA_VoiceCall == "yes" set $voiceCmd "S61=1"
	set $modemCmd "$authCmd$voiceCmd"
	call ModemCmd2
	if $TA_NoDoubleTelno == "yes" return
	if $TA_Bonding == "yes" set $ModTelephone "${Telephone}&${Telephone}"
	return

##
## What to do when NO DIALTONE
##

ImpactIQNoDialtone:
	log "ISDN config problem (or modem restart). Check your ISDN switch type, directory numbers, and SPID settings."
	return

##
## ImpactIQCheckLink
##
## Check the link status registers
##

ImpactIQCheckLink:
	log "Checking the ISDN modem's link status..."
	print "ATS59?\r\n"
	match "000" IQSyncFail
	match "001" IQSyncOK
	match "ERR"
	match "OK\r\n"
	wait 3
	call GetOK
	log "The ISDN modem did not properly answer ATS59?"
	failure
IQSyncOK:
	call GetOK
	goto ImpactIQCheckInit
IQSyncFail:
	log "The ISDN modem is not receiving any ISDN signal. Please verify that it's connected to an ISDN line."
	call GetOK
	failure

ImpactIQCheckInit:
	set $impactInitReg "57"
	call IQInitCheck
	if $TA_Bonding == "yes" return
	set $impactInitReg "58"
	call IQInitCheck
	return

# A little subroutine

IQInitCheck:
	print "ATS${impactInitReg}?\r\n"
	match "000" IQInitOK
	match "001" IQInitOK
	match "002" IQInitFail
	match "ERR"
	match "OK\r\n"
	wait 3
	call GetOK
	log "The ISDN modem did not properly answer ATS${impactInitReg}?"
	failure
IQInitOK:
	call GetOK
	return
IQInitFail:
	log "ISDN initialization failed. Please check your ISDN switch type, directory numbers, and SPID settings."
	call GetOK
	failure

##
## ImpactIQCheckConfig
##
## Verify and adjust ISDN configuration as necessary
##

ImpactIQCheckConfig:
	log "Checking the ISDN modem's ISDN configuration..."
	set $valueChanged "no"

# Check switch type XXX
#
# NOTE: The Impact IQ changes the value of this field as it auto-detects.
# Not only that, but it cycles through each switch type as it is "trying
# out" that type, so that reading this field is pretty much useless...
# Our strategy is just to hope that auto-detect works!

	if $TA_NewSwitch != "yes" goto ImpactIQCheckDirnos
	log "Initiating switch type auto-detect..."
	set $modemCmd "S50=0"
	call ModemCmd2
	set $TA_NewSwitch ""
	set $valueChanged "yes"
	set $iq_new_switch "yes"

# Check directory numbers

ImpactIQCheckDirnos:
	set $checkCmd "S51"
	set $checkValue $TA_Dirno1
	set $checkValueNew $TA_Dirno1
	set $checkMsg "Reprogramming line #1 directory number..."
	call ModemCheckValue

	set $checkCmd "S53"
	set $checkValue $TA_Dirno2
	set $checkValueNew $TA_Dirno2
	set $checkMsg "Reprogramming line #2 directory number..."
	call ModemCheckValue

# Check SPIDs

	set $checkCmd "S52"
	set $checkValue $TA_SPID1
	set $checkValueNew $TA_SPID1
	set $checkMsg "Reprogramming line #1 SPID..."
	call ModemCheckValue

	set $checkCmd "S54"
	set $checkValue $TA_SPID2
	set $checkValueNew $TA_SPID2
	set $checkMsg "Reprogramming line #2 SPID..."
	call ModemCheckValue

# Check 56K/64K setting

	set $checkValue "064"
	set $checkValueNew "64"
	if $TA_56K == "yes" set $checkValue "056"
	if $TA_56K == "yes" set $checkValueNew "56"
	set $checkCmd "S60"
	set $checkMsg "Reprogramming B channel data rate to ${checkValueNew}K..."
	call ModemCheckValue
	set $checkValueNew ""

# Check Multi-link setting

	set $checkValue "000"
	set $checkValueNew "0"
	if $TA_Bonding == "yes" set $checkValue "001"
	if $TA_Bonding == "yes" set $checkValueNew "1"
	set $checkCmd "S80"
	set $checkMsg "Reprogramming mutli-link PPP setting..."
	call ModemCheckValue

# Restart if necessary, and wait extra long if the switch type changed

	if $valueChanged == "no" return
	log "Restarting ISDN modem with the new configuration..."
	set $modemCmd "Z"
	call ModemCmd2
	failure

##
## Detect an incoming call with the 3Com
##

ImpactIQDetectRing:
	call ImpactIQSetup
	if $modemCmdResult != "OK" goto RingbackWait
	match ringset "RING\r\n" RingbackDone
# Clear Caller-ID buffers
	set $modemCmd "\\N0"
# We don't care if this command fails...
	call ModemCmd
# Read most recent incoming call
	call ImpactIQDetectReadCID
	log "Remote Dial-Back mode enabled; waiting for incoming call."
# Now wait for a change
ImpactIQDetectLoop:
	wait 5
	set $oldIncomingCall $incomingCall
	call ImpactIQDetectReadCID
	if $oldIncomingCall != $incomingCall goto ImpactIQDetectDone
	goto ImpactIQDetectLoop

# Examine the CID from each port
ImpactIQDetectDone:
	if $incomingCall !match "((.{14}).{10}) ((.{14}).{10}) ((.{14}).{10})" goto RingbackDetectNoCID
	set $dataCID $matchedString2
	set $voice1CID $matchedString4
	set $voice2CID $matchedString6
	set $cid $dataCID
	call ImpactIQDetectTryCID
	set $cid $voice1CID
	call ImpactIQDetectTryCID
	set $cid $voice2CID
	call ImpactIQDetectTryCID
	goto RingbackDetectNoCID

# Look for a valid CID string
ImpactIQDetectTryCID:
	if $cid match " *" return
	if $cid match " *N/A" return
	goto RingbackDetectCID

# Read first line of incoming call log (it's the 14th line of output)
ImpactIQDetectReadCID:
	set $modemCmd "\\N"
	call ModemCmdSend
	call ReadLine
# Older version of the 3Com don't support this command
	if $matchedString == "OK" return
	if $matchedString match "ERR(O)?(R)?" return
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	call ReadLine
	set $incomingCall $matchedString
	call GetOK
	return

#################################################################
#
#	NEC ATERM SETUP
#
#################################################################

AtermSetup:
	if $AtermHardReset == "yes" goto AtermSetup2

# Do hardware reset, which takes a while and doesn't give back "OK"

	log "Initializing modem..."
	print "ATZ98\r\n"
	wait 6
	set $AtermHardReset "yes"
	failure

AtermSetup2:

# Set to 230K baud if we support it

	set $modemCmd ""
	set $newBaudrate "230400"
	call SetBaudrate

# Is this an IT55 or IT65?

	set $atermIT65 ""
	set $modemCmd "$$N14=0"
	call ModemCmd
	if $modemCmdResult == "OK" set $atermIT65 "yes"

# Set normal stuff, PPP mode

	set $modemCmd "&C1&D0&K3X4$$N1=1$$N9=0"
	call ModemCmd2

# Set the multi-link setting

	set $bondCmd "$$N11=0"
	if $TA_Bonding == "yes" set $bondCmd "$$N11=1"
	set $modemCmd "$bondCmd"
	call ModemCmd2

# Additional settings for the IT65 go here...

	if $atermIT65 != "yes" return
	return

#################################################################
#
#	MICROCOM DESKPORTE SETUP
#
#################################################################

DeskPorteSetup:
	set $noDialToneSubr GenericNoDialtone
	set $temp "M1"
	if $SpeakerOff == "yes" set $temp "M0"
	set $modemCmd "Z&F&C1&D2X3E0S0=0${temp}"
	call ModemCmd2
	return

#################################################################
#
#	KOREAN TERMINAL ADAPTER SETUP
#
#################################################################

NTKSetup:
	set $noDialToneSubr GenericNoDialtone
	set $modemCmd "ZB40&J3"
	call ModemCmd2
	return

#################################################################
#
#	ADTRAN EXPRESS XRT SETUP
#
#################################################################

AdtranXRTSetup:
	set $noDialToneSubr ImpactIQNoDialtone
	set $dialErrorSubr AdtranXRTDialError
	set $bonding "&F1"
	if $TA_Bonding == "yes" set $bonding "&F2"
	set $chap "S118=0"
	if $TA_AuthChap == "yes" set $chap "S118=32"
	set $origCmd "S53=3"
	if $TA_56K == "yes" set $origCmd "S53=2"
	if $TA_VoiceCall == "yes" set $origCmd "S53=0"
	set $modemCmd "ZE0${bonding}&C1&D2${chap}${origCmd}"
	call ModemCmd2

# Check ISDN config and link status (XXX should we check revision too?)

	if $optimize == "yes" return
	call AdtranXRTCheckConfig
	call AdtranXRTCheckLink
	return

##
## What to do when ERROR when dialing
##
## This is what the Adtran returns if the line is not ready yet.
## So we look into the status buffer to see if we recognize the
## error condition.
##

AdtranXRTDialError:
	call AdtranXRTDiagnose
	failure

##
## Check the last status bufffer entry for what the problem is
## XXX Figure out more stuff to look for and when it can happen
##

AdtranXRTDiagnose:
	log "Checking the Adtran status buffer..."
	print "AT!S\r\n"
	match "\r\n\r\n" AdtranXRTDiagBlank
	match "END OF STATUS BUFFER" AdtranXRTDiagBlank
	match "L1 not up." AdtranXRTDiagL1
	match "FACILITY_NOT_SUBSCRIBED" AdtranXRTDiagFNS
	match "Unknown AT cmd" IQSyncOK
	match "ERR"
	match "OK\r\n"
	wait 3
	call GetOK
	log "The ISDN modem did not properly answer ATS59?"
	failure
	goto ImpactIQCheckInit

AdtranXRTDiagFNS:
	log "ISDN initialization failed. Please check your ISDN switch type, directory numbers, and SPID settings."
	failure

AdtranXRTDiagL1:
	log "The ISDN modem is not receiving any ISDN signal. Please verify that it's connected to an ISDN line."
	failure

AdtranXRTDiagBlank:
	log "ISDN initialization failed or restarting link. Please verify proper line connection and ISDN settings."
	failure

##
## AdtranXRTCheckLink
##
## Check the link status registers
##

AdtranXRTCheckLink:
	log "Checking the ISDN modem's link status..."
	print "AT!S1\r\n"
	match "Link In Sync" AdtranXRTSyncReg
	match "Getting" AdtranXRTSyncReg
	match "Register" AdtranXRTSyncReg
	match "Ready" AdtranXRTSyncOK
	match "Down" AdtranXRTSyncFail
	match "ERR"
	match "OK\r\n"
	wait 3
	call GetOK
	log "The ISDN modem did not properly answer AT!S1."
	failure
AdtranXRTSyncOK:
	return
AdtranXRTSyncFail:
	print "ATDT1\r\n"
	wait 1
	goto AdtranXRTDiagnose
AdtranXRTSyncReg:
	log "The ISDN modem is initializing itself."
	failure

##
## AdtranXRTCheckConfig
##
## Verify and adjust ISDN configuration as necessary
##

AdtranXRTCheckConfig:
	log "Checking the ISDN modem's ISDN configuration..."
	set $valueChanged "no"

	set $checkCmd "S52"
	set $checkValue "000"
	set $checkValueNew "0"
	if $TA_SwitchType == "DMS-100" set $checkValue "001"
	if $TA_SwitchType == "DMS-100" set $checkValueNew "1"
	if $TA_SwitchType == "NI-1" set $checkValue "002"
	if $TA_SwitchType == "NI-1" set $checkValueNew "2"
	set $checkMsg "Reprogramming switch type ($TA_SwitchType)..."
	call ModemCheckValue

# Check directory numbers

AdtranXRTCheckDirnos:
	set $checkCmd "SS62"
	set $checkValue $TA_Dirno1
	set $checkValueNew $TA_Dirno1
	set $checkMsg "Reprogramming line #1 directory number..."
	call ModemCheckValue

	set $checkCmd "SS63"
	set $checkValue $TA_Dirno2
	set $checkValueNew $TA_Dirno2
	set $checkMsg "Reprogramming line #2 directory number..."
	call ModemCheckValue

# Check SPIDs

	set $checkCmd "SS60"
	set $checkValue $TA_SPID1
	set $checkValueNew $TA_SPID1
	set $checkMsg "Reprogramming line #1 SPID..."
	call ModemCheckValue

	set $checkCmd "SS61"
	set $checkValue $TA_SPID2
	set $checkValueNew $TA_SPID2
	set $checkMsg "Reprogramming line #2 SPID..."
	call ModemCheckValue

# Restart if necessary

	if $valueChanged == "no" return
	log "Restarting ISDN modem with the new configuration..."
	set $modemCmd "&W"
	call ModemCmd2
	set $modemCmd "Z"
	call ModemCmd2
	failure

#################################################################
#
#	BASIC MODEM STUFF
#
#################################################################

##
## ModemAnswer
##
## Wait for a RING and answer it. Variables:
##
##  $RingTimeout	How long for a RING before giving up (default 10 min)
##  $ConnectTimeout	How long for connect after answering (default 45 secs)
##
## Returns:
##
##  $answerReturn	"OK" or "FAIL"
##  $ConnectionSpeed	Connection speed reported by modem (possibly empty)
##

ModemAnswer:
	if $RingTimeout == "" set $RingTimeout 600
	if $ConnectTimeout == "" set $ConnectTimeout 45
	match "RING\r" ModemAnswerGotRing
	wait $RingTimeout
	log "No RING detected after $RingTimeout seconds."
	set $answerReturn "FAIL"
	return
ModemAnswerGotRing:
	log "Incoming call detected..."
	print "ATA\r\n"
	regex "CONNECT *([0-9]*).*$" ModemAnswerConnect
	wait $ConnectTimeout
	log "Failed to connect incoming call."
	set $answerReturn "FAIL"
	return
ModemAnswerConnect:
	set $ConnectionSpeed $matchedString1
	set $answerReturn "OK"
	return

##
## ModemFind
##
## Just try to get where we are talking to the modem.
## Returns with $ErrorMsg equal to the empty string if
## we found the modem, or else some error message if not.
##

ModemFind:

# Do a quick check first...

	set $ErrorMsg ""
	call ModemFind4
	if $modemCmdResult == "OK" return
	if $Serial230K != "yes" goto ModemFind1
	set $newBaudrate 230400
	if $Baudrate == "230400" set $newBaudrate 115200
	set $Baudrate $newBaudrate
	call ModemFind4
	if $modemCmdResult == "OK" return

# Now try possible baud rates more extensively

ModemFind1:
	set $Baudrate 115200
	call ModemFind2
	if $modemCmdResult == "OK" return
	if $Serial230K != "yes" return
	set $Baudrate 230400
	wait 1
	call ModemFind2
	if $modemCmdResult == "OK" return
	set $ErrorMsg "The modem is not responding."
	return

# This tries the +++ sequence in case the modem is in "on-line" mode

ModemFind2:
	call ModemFind3
	if $modemCmdResult == "OK" return
	call SendTriplePlus
	call ModemFind3
	return

# This tries a few commands to see if the modem responds with "OK"

ModemFind3:
	set $modemCmd ""
	set $modemCmdTimeout 1
	call ModemCmd0
	if $modemCmdResult == "OK" return
	set $modemCmd "Z"
	set $modemCmdTimeout 2
	call ModemCmd0
	if $modemCmdResult == "OK" return
ModemFind4:
	set $modemCmd ""
	set $modemCmdTimeout 1
	call ModemCmd0
	return

##
## SetBaudrate
##
## Change baud rates (as allowed) and verify modem is still there.
## If not, then fail the script.
##
##   $newBaudrate	New baud rate; if we don't support it, just return.
##   $modemCmd		Command to send modem to jump to the new baud rate.
##

SetBaudrate:
	if $Baudrate == $newBaudrate return
	if $newBaudrate != 230400 goto SetBaudrate2
	if $Serial230K != "yes" return
SetBaudrate2:
	log "Setting serial port speed to $newBaudrate..."
	print "AT$modemCmd\r\n"
	wait 1
	set $Baudrate $newBaudrate
	wait 1
	set $modemCmd ""
	goto ModemCmd2

##
1428
## ModemCheckValue
Ad Schellevis's avatar
Ad Schellevis committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
##
## Check the value in an S-register (or equivalent). If it is
## not what we want, then change it and set $valueChanged to "yes",
## and log $checkMsg (if not empty). The value $checkValueNew is
## what we program the S-register to equal if it doesn't match.
##
## The $checkValue must match exactly on the first line of output.
## If we get any wierd error, then fail the script.
##

ModemCheckValue:
	set $modemCmd "${checkCmd}?"
	call ModemQuery
	if $modemQuery == $checkValue return
	set $valueChanged "yes"
	set $checkValueMyNew $checkValueNew
	if $checkValueNew == "" set $checkValueMyNew $checkValue
	set $modemCmd "${checkCmd}=${checkValueMyNew}"
	if $checkMsg != "" log $checkMsg
	goto ModemCmd2

##
## ModemCmd
##
## Do the modem command in $modemCmd. Set $modemCmdResult to
##
##	OK	The modem returned "OK"
##	ERROR	The modem returned "ERR" or "ERROR"
##	TIMEOUT	The modem did not respond within three seconds
##
## If ERR or TIMEOUT, also set $ErrorMsg to an appropriate message
##

ModemCmd:
	set $modemCmdTimeout 3
ModemCmd0:
	print "AT$modemCmd\r\n"
	match "OK\r\n" ModemCmdOk
	match "ERR" ModemCmdErr
	wait $modemCmdTimeout
	set $modemCmdResult "TIMEOUT"
ModemCmdTimeout:
	set $ErrorMsg "The modem is not responding to \"AT$modemCmd\" at ModemCmd: label."
	return
ModemCmdOk:
	set $modemCmdResult "OK"
	return
ModemCmdErr:
	set $modemCmdResult "ERROR"
	set $ErrorMsg "The modem responded with \"ERROR\" to the command \"AT$modemCmd\" at ModemCmd: label."
	return

##
## ModemCmd2
##
## Do the modem command in $modemCmd. If we don't get OK back,
## fail the script and log an error message.
##

ModemCmd2:
	call ModemCmd
	if $modemCmdResult == "OK" return
	log $ErrorMsg
	failure

##
## ModemCmdSend
##
## Send a modem command and read the echo'ed CR-LF
##

ModemCmdSend:
	print "AT$modemCmd\r\n"
	match "\r\n" ModemCmdSend2
	wait 3
	goto ModemCmdTimeout
ModemCmdSend2:
	return

##
## ModemQuery
##
## Do the $modemCmd and expect exactly one line of response, then OK.
## Return the response in $modemQuery. If anything goes wrong, die.
##

ModemQuery:
	call ModemCmdSend
	call ReadLine
	set $modemQuery $matchedString
	if $modemQuery == "ERR" goto ModemQueryError
	if $modemQuery == "ERROR" goto ModemQueryError
	goto GetOK
ModemQueryError:
	call ModemCmdErr
	failure

# Same thing, but strip leading and trailing blanks

ModemQueryStrip:
	call ModemQuery
	if $modemQuery match " *(.*) *" nop
	set $modemQuery $matchedString1
	return

##
## ReadLine
##
## Read the next line of output. It should come within $modemCmdTimeout
## seconds, or else we fail. Return it in $matchedString.
##

ReadLine:
	regex "^.*$" ReadLineDone
	wait $modemCmdTimeout
	set $ErrorMsg "The modem is not responding."
	log $ErrorMsg
	failure
ReadLineDone:
	return

##
## Get an OK within 3 seconds or die
##

GetOK:
	match "OK\r\n" GotOK
	wait 3
	log "The modem did not respond with \"OK\"."
	failure
GotOK:
	return

##
## Send "+++" to get modem attention if on-line
##

SendTriplePlus:
	print "\r\n"
	wait 2
	print "+++"
	wait 2
	return

#################################################################
#
#	LOGIN AUTO-SCRIPTING
#
#################################################################

##
## AutoLogin
##
## Here we attempt to figure out what the remote server wants
## from us. We do this by checking for bytes that correspond
## to PPP packets (in which case we are done) as well as common
## login type stuff like "name:", "ogin:", etc.
##
## This always returns. The hope is that when it returns, the
## remote side has reached PPP mode.
##
## This has been crafted from empirical evidence. Lots of terminal
## servers have various intelligent/stupid features which we
## take advantage of/have to work around.
##
## Variables (set automatically by mpd):
##
##  $Login		Authorization login
##  $Password		Authorization password
##

AutoLogin:
	log "Initiating auto-login..."

# Spend at most this long doing auto-login before giving up

	timer autoLogin 5 AutoLoginTimeout

# At any time if we see an LCP frame (not our own echo) then we're done

	match autoLogin "\x7e\xff\x03\xc0\x21" AutoLoginFrame
	match autoLogin "\x7e\xff\x7d\x23\xc0\x21\x7d\x21" AutoLoginFrame
	match autoLogin "\x7e\xc0\x21" AutoLoginFrame

# Now send a "fake" PPP frame (this is an empty config-reject with id# 172).
# This should trigger any auto-detecting servers to jump into PPP mode,
# which is good because it's faster (by avoiding human readable messages)
# and more reliable (PPP framing).

	print "\x7e\xff\x7d\x23\xc0\x21\x7d\x24\xac\x7d\x20\x7d\x24\x2e\x2b\x7e"

# Wait one second for server to auto-detect PPP or send a login prompt.
# After one second of neither, try sending a carriage return (some servers
# require this). After that, we have to see something recognizable from
# the peer, otherwise we'll just timeout.

	match "ogin" AutoLoginPrompt
	match "name" AutoLoginPrompt
	wait 1
	print "\r"
	match "ogin" AutoLoginPrompt
	match "name" AutoLoginPrompt
	wait

# At this point we've seen a login prompt; do the manual login

AutoLoginPrompt:
	log "Sending login..."
	print "${Login}\r"
	match "word"
	wait
	log "Sending password..."
	print "${Password}\r"
	match "\r"
	wait
	if $didLogin != "yes" match "ogin:" LoginAgain
	match ">"
	match "%"
	match ":"
	wait
	log "Enabling PPP..."
	print "ppp\r"
	cancel all
	return

LoginAgain:
	set $didLogin "yes"
	goto AutoLoginPrompt

# We saw a PPP frame

AutoLoginFrame:
	log "Detected PPP frame."
	cancel all
	return

# We timed out before seeing a PPP frame. Cross your fingers and pray.

AutoLoginTimeout:
	log "Auto-login timeout."
	cancel all
	return