Project

General

Profile

Download (39.8 KB) Statistics
| Branch: | Tag: | Revision:
1
#################################################################
2
#
3
# $Id: mpd.script.sample,v 1.9 2009/10/04 19:36:04 amotin Exp $
4
#
5
# Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
6
# See ``COPYRIGHT.whistle''
7
#
8
#################################################################
9

    
10
##
11
## MPD Modem script variables:
12
##
13
##  $DialPrefix		Modem dialing prefix (eg. "DT")
14
##  $Telephone		Telephone number to dial (not duplicated using &)
15
##  $ConnectTimeout	Wait-for-connect timeout, in seconds (default 45 secs)
16
##  $SpeakerOff		Set to "yes" to quiet modem speakers
17
##  $Serial230K		If "yes", we can support 230K serial port speed
18
##  $CountryCode	Country code for Winmodem
19
##  $InitString		External initialization string
20
##  $LoginScript	If "yes", look for script login
21
##
22
## pfSense specific variables:
23
##	$APN			Access Point (host)Name for 3G connections
24
##	$APNum			Access Point Number, typically "1", might not matter
25
##	$SimPin			SIM card PIN number
26
##	$PinWait		Wait for SIM to connect to network after PIN entered
27
##
28
## ISDN Terminal Adapter specific variables (all start with "TA_"):
29
##
30
##  $TA_Bonding		Bonding on TA: "yes" or "no"
31
##  $TA_NoDoubleTelno	When $TA_Bonding, don't double the dialed number
32
##  $TA_56K		Restrict to 56K on TA: "yes" or "no"
33
##  $TA_VoiceCall	Originate calls using voice mode
34
##  $TA_AuthChap	Tell TA to use CHAP: "yes" or "no"
35
##  $TA_Dirno1		TA directory #1
36
##  $TA_Dirno2		TA directory #2
37
##  $TA_SPID1		SPID #1
38
##  $TA_SPID2		SPID #2
39
##  $TA_SwitchType	One of these values
40
##				"NI-1"
41
##				"DMS-100"
42
##				"5ESS P2P"
43
##				"5ESS MP"
44
##  $TA_NewSwitch	Means the switch type is new, initiate auto-detect
45
##			(3Com Impact IQ only)
46
##
47
## We set $OptimizeNextTime to "yes" after a successful connection so we
48
## can avoid verifing configuration when things are working good.
49
##
50
## Internal variables:
51
##
52
##  $ModemSetupFunc	Routine to set up modem for dialing out
53
##  $ModemAnsSetupFunc	Routine to set up modem for answer mode (if different)
54
##  $ModemDetectRing	Routine to detect an incoming call for ringback
55
##  $ModemIsAnalog	If "yes" modem is analog (ie, not terminal adapter)
56
##
57

    
58
#################################################################
59
#
60
#	MODEM DIALING
61
#
62
#################################################################
63

    
64
DialPeer:
65
	set $CallingID ""
66
	set $CalledID $Telephone
67
	if $Telephone == "00000" goto DialNullModem
68
	set $optimize $OptimizeNextTime
69
	set $OptimizeNextTime "no"
70

    
71
	if $optimize == "yes" goto DialPeer2
72
	call ModemFind
73
	if $ErrorMsg == "" goto DialPeer1
74
	log $ErrorMsg
75
	failure
76
DialPeer1:
77
	set $ModTelephone $Telephone
78
	call ModemIdent
79
	if $ModemDescription != "" goto DialPeer2
80
	log "No match found in the ModemIdent function in the chat script."
81
	failure
82

    
83
DialPeer2:
84
	log "Detected $ModemDescription."
85
	call $ModemSetupFunc
86
	log "Dialing server at $Telephone..."
87
	call ModemDial
88
	if $dialResult == "OK" goto DialPeerOK
89
	set $optimize "no"
90
	failure
91

    
92
DialPeerOK:
93
	if $ConnectionSpeed == "" log "Connected at an unknown speed."
94
	if $ConnectionSpeed == "" goto DialPeer3
95
	log "Connected at $ConnectionSpeed."
96
DialPeer3:
97
	if $LoginScript == "yes" call AutoLogin
98
	set $OptimizeNextTime "yes"
99
	success
100

    
101
DialPeerSetPin:
102
	set $modemCmd "+CPIN?"
103
	log $modemCmd
104
	call ModemQuery
105
	log $modemQuery
106
	if $modemQuery match ".*READY.*" goto DialPinReady
107
	set $modemCmd "+CPIN=\"$SimPin\""
108
	call ModemCmd2
109
	wait $PinWait
110

    
111
DialPinReady:
112
	return
113

    
114
DialPeerSetAPN:
115
	set $modemCmd "+CGDCONT=$APNum,\"IP\",\"$APN\""
116
	log $modemCmd
117
	call ModemCmd2
118
	return
119
	
120
# Null-modem connection
121
DialNullModem:
122
	log "Connected via null modem connection."
123
	success
124

    
125
##
126
## Dial modem
127
##
128
## Variables:
129
##
130
##  $DialPrefix		Modem dialing prefix (eg. "DT")
131
##  $ModTelephone	Telephone number to dial
132
##  $ConnectTimeout	Wait-for-connect timeout, in seconds (default 45 secs)
133
##  $noDialToneSubr	(optional) Subroutine to call if NO DIALTONE
134
##  $dialErrorSubr	(optional) Subroutine to call if ERR
135
##
136
## Returns:
137
##
138
##  $dialResult		"OK" or "FAIL"
139
##  $ConnectionSpeed	Connection speed reported by modem (possibly empty)
140
##
141

    
142
ModemDial:
143
	set $dialResult "FAIL"
144
	set $ConnectionSpeed ""
145
	if $ConnectTimeout == "" set $ConnectTimeout 45
146
	print "AT${DialPrefix}${ModTelephone}\r\n"
147
	log "AT${DialPrefix}${ModTelephone}"
148
	match "NO CARRIER" DialAbortNoCar
149
	match "NO DIAL" DialAbortNoDial
150
	match "BUSY" DialAbortBusy
151
	regex "CONNECT *([0-9]*).*$" DialConnect
152
	match "ERR" DialError
153
	wait $ConnectTimeout
154
	log "No response from the modem after dialing."
155
	return
156
DialAbortNoCar:
157
	log "The remote modem did not answer."
158
	return
159
DialAbortNoDial:
160
	if $noDialToneSubr != "" goto $noDialToneSubr
161
	log "No dialtone. Is the modem plugged in?"
162
	return
163
DialError:
164
	if ${ModTelephone} != "" goto DialErrorInit
165
	log "Invalid empty telephone number."
166
	return
167
DialErrorInit:
168
	if $dialErrorSubr != "" goto $dialErrorSubr
169
	log "Invalid dial init string."
170
	return
171
DialAbortBusy:
172
	log "The line was busy."
173
	return
174
DialConnect:
175
	set $ConnectionSpeed $matchedString1
176
	set $dialResult "OK"
177
	return
178

    
179
#################################################################
180
#
181
#	MODEM ANSWERING
182
#
183
#################################################################
184

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

    
194
AnswerCall:
195
	set $CallingID ""
196
	set $CalledID ""
197
	set $optimize $OptimizeNextTime
198
	set $OptimizeNextTime "no"
199

    
200
# Skip modem detection if we connected successfully last time
201

    
202
	if $optimize == "yes" goto AnswerCall2
203
	call ModemFind
204
	if $ErrorMsg == "" goto AnswerCall0
205
	log $ErrorMsg
206
	failure
207
AnswerCall0:
208
	call ModemIdent
209
	if $ModemDescription != "" goto AnswerCall1
210
	log "The modem is not responding."
211
	failure
212
AnswerCall1:
213
	log "Detected $ModemDescription."
214

    
215
AnswerCall2:
216
	if $ModemAnsSetupFunc == "" set $ModemAnsSetupFunc $ModemSetupFunc
217
	call $ModemAnsSetupFunc
218
	log "Waiting for ring..."
219
	call ModemAnswer
220
	if $answerReturn == "OK" goto AnswerCallOK
221
	set $IdleResult ""
222
	set $optimize "no"
223
	failure
224

    
225
AnswerCallOK:
226
	log "Connected at $ConnectionSpeed."
227
	set $OptimizeNextTime "yes"
228
	set $IdleResult "answer"
229
	success
230

    
231
#################################################################
232
#
233
#	MODEM RINGBACK
234
#
235
#################################################################
236

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

    
252
Ringback:
253
	set $CallingID ""
254
	set $CalledID ""
255
	if $RingbackTimeout == "" set $RingbackTimeout "3600"
256
	if $RingStoppedTime == "" set $RingStoppedTime "8"
257
	set $ModemDetectRing RingbackWait
258
	call ModemFind
259
	if $ErrorMsg == "" goto Ringback1
260
	log $ErrorMsg
261
	failure
262
Ringback1:
263
	call ModemIdent
264
	if $ModemDescription != "" goto Ringback2
265
	log "The modem is not responding."
266
	failure
267

    
268
Ringback2:
269
	log "Detected $ModemDescription."
270
	goto $ModemDetectRing
271

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

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

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

    
283
RingbackGotRings:
284
	log "Incoming call detected..."
285
	if $ModemIsAnalog != "yes" goto RingbackDone
286
RingbackWaitStop:
287
	match "RING\r\n" RingbackWaitStop
288
	wait $RingStoppedTime
289
RingbackDone:
290
	set $IdleResult "ringback"
291
	success
292

    
293
# Strip leading and trailing spaces and log calling party number
294

    
295
RingbackDetectCID:
296
	if $cid match " *(.*) *" nop
297
	set $cid $matchedString1
298
	if $cid == "" goto RingbackDetectNoCID
299
	log "Incoming call detected from $cid..."
300
	success
301

    
302
# We couldn't determine calling party number
303

    
304
RingbackDetectNoCID:
305
	log "Incoming call detected..."
306
	success
307

    
308
#################################################################
309
#
310
#	MODEM IDENTIFICATION
311
#
312
#################################################################
313

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

    
321
Identify:
322
	call ModemFind
323
	if $ErrorMsg == "" goto Identify1
324
	failure
325
Identify1:
326
	call ModemIdent
327
	if $ModemDescription == "" failure
328
	log "ANALOG=$ModemIsAnalog"
329
	log "DESCRIPTION=$ModemDescription"
330
	success
331

    
332
##
333
## ModemIdent
334
##
335
## This identifies the type of modem and sets these variables accordingly:
336
##
337
##	$ModemDescription
338
##	$ModemSetupFunc
339
##	$ModemAnsSetupFunc
340
##	$ModemDetectRing
341
##	$ModemIsAnalog
342
##
343
## If no response seen, this sets $ModemDescription to the empty string.
344
##
345

    
346
ModemIdent:
347
	set $ModemDescription ""
348
	set $ModemSetupFunc ""
349
	set $ModemAnsSetupFunc ""
350
	set $ModemDetectRing ""
351
	set $ModemIsAnalog "yes"
352
	if $InitString != "" goto ModemIdentCustom
353
	print "ATI\r\n"
354
	match "ADTRAN EXPRESS XR" ModemIdentAdtranXRT
355
	match "Model: Ovation MC950D Card" ModemIdentMC950D
356
	match "ERR" ModemIdentGeneric
357
	match "OK\r\n" ModemIdentGeneric
358
	wait 3
359
	print "ATI1\r\n"
360
	match "NTK omni.net" ModemIdentNTK
361
	match "ERR" ModemIdentGeneric
362
	match "OK\r\n" ModemIdentGeneric
363
	wait 3
364
	print "ATI3\r\n"
365
	match "Courier" ModemIdentUsr
366
	match "Sportster" ModemIdentUsr
367
	match "3ComImpact IQ" ModemIdentImpactIQ
368
	match "U.S. Robotics 56K" ModemIdentUsr
369
	match "ZOOM V.90" ModemIdentZoom56
370
	match "LT V.90" ModemIdentLucent
371
	match "ERR"
372
	match "OK\r\n"
373
	wait 3
374
	print "ATI4\r\n"
375
	match "AtermIT NEC Corporation" ModemIdentAterm
376
	match "INSMATEV-7 NTT Corporation" ModemIdentAterm
377
	match "MNP Class 10 V.34 Modem" ModemIdentDeskPorte
378
	match "Smart One 56" ModemIdentSmartOne
379
	match "ERR"
380
	match "OK\r\n"
381
	wait 3
382
	print "ATI5\r\n"
383
	match "C885" ModemIdentC885
384
	match "ERR"
385
	match "OK\r\n" ModemIdentGeneric
386
	wait 3
387
	print "ATI8\r\n"
388
	match "BitSURFR PRO\r" ModemIdentBitsurfr
389
	match "BitSURFR PRO EZ" ModemIdentBitsurfrEZ
390
	match "3C882" ModemIdentImpactIQ
391
	match "ERR"
392
	match "OK\r\n"
393
	wait 3
394
	log "The modem is not responding to any ATI[1,3-5,8] commands."
395
	failure
396

    
397
ModemIdentGeneric:
398
	set $ModemDescription "Hayes compatible modem"
399
	if $SimPin != "" call DialPeerSetPin
400
	if $APN != "" call DialPeerSetAPN
401
	set $ModemSetupFunc GenericSetup
402
	return
403

    
404
ModemIdentCustom:
405
	set $ModemDescription "Custom modem"
406
	set $ModemSetupFunc CustomSetup
407
	return
408

    
409
ModemIdentUsr:
410
	set $SportsterHack "no"
411
	if $matchedString == "Sportster" set $SportsterHack "yes"
412
	set $ModemDescription "USR $matchedString modem"
413
	call GetOK
414
	set $ModemSetupFunc UsrSetup
415
	return
416
ModemIdentC885:
417
	set $ModemDescription "Sierra Wireless Compass $matchedString USB 3G modem"
418
	set $ModemSetupFunc GenericSetup
419
	return
420
ModemIdentMC950D:
421
	set $ModemDescription "Novatel Wireless $matchedString USB 3G modem"
422
	set $ModemSetupFunc GenericSetup
423
	return
424
ModemIdentBitsurfrEZ:
425
	set $bitsEZ "yes"
426
ModemIdentBitsurfr:
427
	set $ModemDescription "Motorola $matchedString"
428
	call GetOK
429
	set $ModemSetupFunc BitsurfrSetup
430
	set $ModemIsAnalog "no"
431
	set $ModemDetectRing BitsurfrDetectRing
432
	return
433

    
434
ModemIdentImpactIQ:
435
	set $matchedString "3ComImpact IQ"
436
	set $ModemDescription "$matchedString"
437
	call GetOK
438
	set $ModemSetupFunc ImpactIQSetup
439
	set $ModemIsAnalog "no"
440
	set $ModemDetectRing ImpactIQDetectRing
441
	return
442

    
443
ModemIdentAdtranXRT:
444
	set $ModemDescription "AdTran Express XR/XRT"
445
	call GetOK
446
	set $ModemSetupFunc AdtranXRTSetup
447
	set $ModemIsAnalog "no"
448
	return
449

    
450
ModemIdentAterm:
451
	set $ModemDescription "NEC Aterm TA"
452
	call GetOK
453
	set $ModemSetupFunc AtermSetup
454
	set $ModemIsAnalog "no"
455
	return
456

    
457
ModemIdentNTK:
458
	set $ModemDescription "$matchedString"
459
	call GetOK
460
	set $ModemSetupFunc NTKSetup
461
	set $ModemIsAnalog "no"
462
	return
463

    
464
ModemIdentDeskPorte:
465
	set $ModemDescription "$matchedString"
466
	call GetOK
467
	set $ModemSetupFunc DeskPorteSetup
468
	return
469

    
470
ModemIdentZoom56:
471
ModemIdentSmartOne:
472
	set $ModemDescription "$matchedString"
473
	call GetOK
474
	set $ModemSetupFunc Modem56Setup
475
	return
476

    
477
# Support the Lucent Winmodem, Xircom 56k
478
ModemIdentLucent:
479
	set $ModemDescription "$matchedString"
480
	call GetOK
481
	set $ModemSetupFunc ModemLucentSetup
482
	return
483

    
484
#################################################################
485
#
486
#	GENERIC MODEM SETUP
487
#
488
#################################################################
489

    
490
GenericSetup:
491
	set $noDialToneSubr GenericNoDialtone
492
	set $temp "M1"
493
	if $SpeakerOff == "yes" set $temp "M0"
494
	set $modemCmd "&F&C1&D2E0S0=0${temp}"
495
	call ModemCmd2
496
	return
497

    
498
CustomSetup:
499
	set $noDialToneSubr GenericNoDialtone
500
	set $modemCmd "${InitString}"
501
	call ModemCmd2
502
	return
503

    
504
GenericNoDialtone:
505
	log "No dialtone. Is the modem plugged in?"
506
	return
507

    
508
#################################################################
509
#
510
#	USR MODEM SETUP
511
#
512
#################################################################
513

    
514
UsrSetup:
515
# Lower baudrate to 57600 for crappy internal Sportster modem
516
	if $SportsterHack != "yes" goto UsrSetup2
517
	if $modemDevice != "/dev/cuad2" goto UsrSetup2
518
	set $Baudrate 57600
519
	set $modemCmd ""
520
	call ModemCmd2
521
UsrSetup2:
522
	set $noDialToneSubr GenericNoDialtone
523
	set $temp "M1"
524
	if $SpeakerOff == "yes" set $temp "M0"
525
	set $modemCmd "&F1&C1&D2E0S0=0S13.0=1L0S6=5${temp}"
526
	call ModemCmd2
527
	return
528

    
529
#################################################################
530
#
531
#	GENERAL 56K MODEM SETUP
532
#
533
#################################################################
534

    
535
Modem56Setup:
536
	set $noDialToneSubr GenericNoDialtone
537
	set $temp "M1"
538
	if $SpeakerOff == "yes" set $temp "M0"
539
	set $modemCmd "&FL2W2E0${temp}"
540
	call ModemCmd2
541
	return
542

    
543
#################################################################
544
#
545
#	LUCENT WINMODEM AND XIRCOM 56K SETUP
546
#
547
#################################################################
548

    
549
ModemLucentSetup:
550
	set $noDialToneSubr GenericNoDialtone
551
	set $temp "M1"
552
	if $SpeakerOff == "yes" set $temp "M0"
553
	set $tempCountryCode ""
554
	if $CountryCode != "" set $tempCountryCode "+GCI=${CountryCode}"
555
	if $CountryCode != "" log "Use country ${CountryCode}"
556
	set $modemCmd "&FL2W2E0${temp}${tempCountryCode}"
557
	call ModemCmd2
558
	return
559

    
560
#################################################################
561
#
562
#	BITSURFR PRO AND BITSURFR PRO EZ SETUP
563
#
564
#################################################################
565

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

    
573
# Set to 230K baud if we support it
574

    
575
	if $bitsEZ != "yes" goto BitsurfrSetup1
576
	set $modemCmd "@P2=230400"
577
	set $newBaudrate "230400"
578
	call SetBaudrate
579

    
580
# Check software revision and ISDN settings
581

    
582
BitsurfrSetup1:
583
	if $optimize == "yes" goto BitsurfrSetup2
584
	if $bitsEZ != "yes" call BitsurfrCheckRev
585
	call BitsurfrCheckConfig
586

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

    
589
BitsurfrSetup2:
590
	set $authCmd "@M2=P"
591
	if $TA_AuthChap == "yes" set $authCmd "@M2=C"
592
	set $bondCmd "@B0=1"
593
	if $TA_Bonding == "yes" set $bondCmd "@B0=2"
594
	set $bearCmd "%A4=0"
595
	if $TA_56K == "yes" set $bearCmd "%A4=1"
596
	if $TA_VoiceCall == "yes" set $bearCmd "%A4=1"
597
	set $voiceCmd "%A98=D%A96=0"
598
	if $TA_VoiceCall == "yes" set $voiceCmd "%A98=S%A96=1"
599

    
600
# BS PRO EZ changes
601

    
602
	if $bitsEZ == "yes" set $authCmd "${authCmd}@M20=\"\""
603
	set $modemCmd "$authCmd$bondCmd$bearCmd$voiceCmd"
604
	call ModemCmd2
605
	if $TA_NoDoubleTelno == "yes" return
606
	if $TA_Bonding == "yes" set $ModTelephone "${Telephone}&${Telephone}"
607
	return
608

    
609
##
610
## What to do when NO DIALTONE
611
##
612

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

    
617
##
618
## BitsurfrCheckConfig
619
##
620
## Verify and adjust ISDN configuration as necessary
621
##
622

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

    
627
# Check switch type
628

    
629
	set $checkCmd "!C0"
630
	set $checkValue "000"
631
	set $checkValueNew "0"
632
	if $TA_SwitchType == "DMS-100" set $checkValue "001"
633
	if $TA_SwitchType == "DMS-100" set $checkValueNew "1"
634
	if $TA_SwitchType == "NI-1" set $checkValue "002"
635
	if $TA_SwitchType == "NI-1" set $checkValueNew "2"
636
	set $checkMsg "Reprogramming switch type ($TA_SwitchType)..."
637
	call ModemCheckValue
638

    
639
	set $checkCmd "!C1"
640
	set $checkValue "004"
641
	set $checkValueNew "4"
642
	if $TA_SwitchType == "DMS-100" set $checkValue "003"
643
	if $TA_SwitchType == "DMS-100" set $checkValueNew "3"
644
	if $TA_SwitchType == "5ESS P2P" set $checkValue "000"
645
	if $TA_SwitchType == "5ESS P2P" set $checkValueNew "0"
646
	if $TA_SwitchType == "5ESS MP" set $checkValue "001"
647
	if $TA_SwitchType == "5ESS MP" set $checkValueNew "1"
648
	set $checkMsg "Reprogramming switch software version..."
649
	call ModemCheckValue
650

    
651
# Check directory numbers
652

    
653
	set $checkCmd "*1!N1"
654
	set $checkValue $TA_Dirno1
655
	set $checkValueNew $TA_Dirno1
656
	set $checkMsg "Reprogramming voice line #1 directory number..."
657
	call ModemCheckValue
658

    
659
	set $checkCmd "*2!N1"
660
	set $checkValue $TA_Dirno2
661
	set $checkValueNew $TA_Dirno2
662
	set $checkMsg "Reprogramming voice line #2 directory number..."
663
	call ModemCheckValue
664
	set $checkCmd "!N1"
665
	set $checkMsg "Reprogramming data line directory number..."
666
	call ModemCheckValue
667

    
668
# Check SPIDs
669

    
670
	set $checkCmd "*1!C6"
671
	set $checkValue $TA_SPID1
672
	set $checkValueNew $TA_SPID1
673
	set $checkMsg "Reprogramming voice line #1 SPID..."
674
	call ModemCheckValue
675

    
676
	set $checkCmd "*2!C6"
677
	set $checkValue $TA_SPID2
678
	set $checkValueNew $TA_SPID2
679
	set $checkMsg "Reprogramming voice line #2 SPID..."
680
	call ModemCheckValue
681
	set $checkCmd "!C6"
682
	set $checkMsg "Reprogramming data line SPID..."
683
	call ModemCheckValue
684

    
685
# Restart if necessary
686

    
687
	if $valueChanged == "no" return
688
	log "Restarting BitSURFR Pro with new configuration..."
689
	set $modemCmd ">W"
690
	call ModemCmd2
691
	set $modemCmd ">Z"
692
	call ModemCmd2
693
	failure
694

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

    
700
BitsurfrCheckRev:
701
	log "Checking the BitSURFR's software revision..."
702
	print "ATI3\r\n"
703
	match "-1A" BitsurfrCheckRevOld
704
	match "-1B" BitsurfrCheckRevOld
705
	match "-1C" BitsurfrCheckRevOld
706
	match "-1D" BitsurfrCheckRevOld
707
	match "-1E" BitsurfrCheckRevOld
708
	match "-1F" BitsurfrCheckRevOld
709
	match "-1G" BitsurfrCheckRevOld
710
	match "-1H" BitsurfrCheckRevOld
711
	match "-1I" BitsurfrCheckRevOld
712
	match "OK\r\n" BitsurfrCheckRevOK
713
	match "ERR"
714
	wait 5
715
	log "The BitSURFR did not properly answer ATI3."
716
	failure
717
BitsurfrCheckRevOK:
718
	return
719
BitsurfrCheckRevOld:
720
	log "The BitSURFR Pro has an old software revision $matchedString. Please upgrade according to the manufacturer's instructions."
721
	call GetOK
722
	failure
723

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

    
726
BitsurfrDetectRing:
727
# First, we need to be properly configured
728
	call BitsurfrSetup
729
# Enable Caller-ID logging
730
	set $modemCmd "@N0=1*1@N0=1*2@N0=1"
731
	call ModemCmd
732
	if $modemCmdResult != "OK" goto RingbackWait
733
# A "RING" at any time is good enough
734
	match ringset "RING\r\n" RingbackDone
735
# Clear Caller-ID buffers
736
	set $modemCmd "@L0*1@L0*2@L0"
737
	call ModemCmd2
738
# Read each buffer
739
	call BitsurfrDetectReadCID
740
	log "Remote Dial-Back mode enabled; waiting for incoming call."
741
# Now wait indefinitely for one of those buffers to change
742
BitsurfrDetectLoop:
743
	wait 5
744
	set $oldDataCID $dataCID
745
	set $oldVoice1CID $voice1CID
746
	set $oldVoice2CID $voice2CID
747
	call BitsurfrDetectReadCID
748
	if $oldDataCID != $dataCID goto BitsurfrDetectDone
749
	if $oldVoice1CID != $voice1CID goto BitsurfrDetectDone
750
	if $oldVoice2CID != $voice2CID goto BitsurfrDetectDone
751
	goto BitsurfrDetectLoop
752

    
753
# Examine the CID from each port
754
BitsurfrDetectDone:
755
	set $cid $dataCID
756
	call BitsurfrDetectTryCID
757
	set $cid $voice1CID
758
	call BitsurfrDetectTryCID
759
	set $cid $voice2CID
760
	call BitsurfrDetectTryCID
761
	goto RingbackDetectNoCID
762

    
763
# Try to extract the calling party number
764
BitsurfrDetectTryCID:
765
# Strip the log number
766
	if $cid match "[0-9] (.*)" set $cid $matchedString1
767
# Check for various strings
768
	if $cid match "BLOCKED" return
769
	if $cid match "NO NUMBER" return
770
	if $cid match "NO CID SERVICE" return
771
	if $cid match "OUT OF AREA" return
772
	goto RingbackDetectCID
773

    
774
# Read Caller-ID buffers
775
BitsurfrDetectReadCID:
776
	set $modemCmd "@L1"
777
	call ModemQueryStrip
778
	set $dataCID $modemQuery
779
	set $modemCmd "*1@L1"
780
	call ModemQueryStrip
781
	set $voice1CID $modemQuery
782
	set $modemCmd "*2@L1"
783
	call ModemQueryStrip
784
	set $voice2CID $modemQuery
785
	return
786

    
787
#################################################################
788
#
789
#	3COM IMPACT IQ SETUP
790
#
791
#################################################################
792

    
793
ImpactIQSetup:
794
	set $noDialToneSubr ImpactIQNoDialtone
795
	set $modemCmd "Z&F%C2E0"
796
	call ModemCmd2
797

    
798
# Set to 230K baud if we support it
799

    
800
	set $modemCmd "$$B230400"
801
	set $newBaudrate "230400"
802
	call SetBaudrate
803

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

    
806
ImpactIQSetup1:
807
	if $optimize == "yes" goto IQInitSetup2
808
	call ImpactIQCheckConfig
809
	call ImpactIQCheckLink
810

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

    
813
IQInitSetup2:
814
	set $authCmd "S84=1"
815
	if $TA_AuthChap == "yes" set $authCmd "S84=0"
816
	set $voiceCmd "S61=0"
817
	if $TA_VoiceCall == "yes" set $voiceCmd "S61=1"
818
	set $modemCmd "$authCmd$voiceCmd"
819
	call ModemCmd2
820
	if $TA_NoDoubleTelno == "yes" return
821
	if $TA_Bonding == "yes" set $ModTelephone "${Telephone}&${Telephone}"
822
	return
823

    
824
##
825
## What to do when NO DIALTONE
826
##
827

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

    
832
##
833
## ImpactIQCheckLink
834
##
835
## Check the link status registers
836
##
837

    
838
ImpactIQCheckLink:
839
	log "Checking the ISDN modem's link status..."
840
	print "ATS59?\r\n"
841
	match "000" IQSyncFail
842
	match "001" IQSyncOK
843
	match "ERR"
844
	match "OK\r\n"
845
	wait 3
846
	call GetOK
847
	log "The ISDN modem did not properly answer ATS59?"
848
	failure
849
IQSyncOK:
850
	call GetOK
851
	goto ImpactIQCheckInit
852
IQSyncFail:
853
	log "The ISDN modem is not receiving any ISDN signal. Please verify that it's connected to an ISDN line."
854
	call GetOK
855
	failure
856

    
857
ImpactIQCheckInit:
858
	set $impactInitReg "57"
859
	call IQInitCheck
860
	if $TA_Bonding == "yes" return
861
	set $impactInitReg "58"
862
	call IQInitCheck
863
	return
864

    
865
# A little subroutine
866

    
867
IQInitCheck:
868
	print "ATS${impactInitReg}?\r\n"
869
	match "000" IQInitOK
870
	match "001" IQInitOK
871
	match "002" IQInitFail
872
	match "ERR"
873
	match "OK\r\n"
874
	wait 3
875
	call GetOK
876
	log "The ISDN modem did not properly answer ATS${impactInitReg}?"
877
	failure
878
IQInitOK:
879
	call GetOK
880
	return
881
IQInitFail:
882
	log "ISDN initialization failed. Please check your ISDN switch type, directory numbers, and SPID settings."
883
	call GetOK
884
	failure
885

    
886
##
887
## ImpactIQCheckConfig
888
##
889
## Verify and adjust ISDN configuration as necessary
890
##
891

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

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

    
903
	if $TA_NewSwitch != "yes" goto ImpactIQCheckDirnos
904
	log "Initiating switch type auto-detect..."
905
	set $modemCmd "S50=0"
906
	call ModemCmd2
907
	set $TA_NewSwitch ""
908
	set $valueChanged "yes"
909
	set $iq_new_switch "yes"
910

    
911
# Check directory numbers
912

    
913
ImpactIQCheckDirnos:
914
	set $checkCmd "S51"
915
	set $checkValue $TA_Dirno1
916
	set $checkValueNew $TA_Dirno1
917
	set $checkMsg "Reprogramming line #1 directory number..."
918
	call ModemCheckValue
919

    
920
	set $checkCmd "S53"
921
	set $checkValue $TA_Dirno2
922
	set $checkValueNew $TA_Dirno2
923
	set $checkMsg "Reprogramming line #2 directory number..."
924
	call ModemCheckValue
925

    
926
# Check SPIDs
927

    
928
	set $checkCmd "S52"
929
	set $checkValue $TA_SPID1
930
	set $checkValueNew $TA_SPID1
931
	set $checkMsg "Reprogramming line #1 SPID..."
932
	call ModemCheckValue
933

    
934
	set $checkCmd "S54"
935
	set $checkValue $TA_SPID2
936
	set $checkValueNew $TA_SPID2
937
	set $checkMsg "Reprogramming line #2 SPID..."
938
	call ModemCheckValue
939

    
940
# Check 56K/64K setting
941

    
942
	set $checkValue "064"
943
	set $checkValueNew "64"
944
	if $TA_56K == "yes" set $checkValue "056"
945
	if $TA_56K == "yes" set $checkValueNew "56"
946
	set $checkCmd "S60"
947
	set $checkMsg "Reprogramming B channel data rate to ${checkValueNew}K..."
948
	call ModemCheckValue
949
	set $checkValueNew ""
950

    
951
# Check Multi-link setting
952

    
953
	set $checkValue "000"
954
	set $checkValueNew "0"
955
	if $TA_Bonding == "yes" set $checkValue "001"
956
	if $TA_Bonding == "yes" set $checkValueNew "1"
957
	set $checkCmd "S80"
958
	set $checkMsg "Reprogramming mutli-link PPP setting..."
959
	call ModemCheckValue
960

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

    
963
	if $valueChanged == "no" return
964
	log "Restarting ISDN modem with the new configuration..."
965
	set $modemCmd "Z"
966
	call ModemCmd2
967
	failure
968

    
969
##
970
## Detect an incoming call with the 3Com
971
##
972

    
973
ImpactIQDetectRing:
974
	call ImpactIQSetup
975
	if $modemCmdResult != "OK" goto RingbackWait
976
	match ringset "RING\r\n" RingbackDone
977
# Clear Caller-ID buffers
978
	set $modemCmd "\\N0"
979
# We don't care if this command fails...
980
	call ModemCmd
981
# Read most recent incoming call
982
	call ImpactIQDetectReadCID
983
	log "Remote Dial-Back mode enabled; waiting for incoming call."
984
# Now wait for a change
985
ImpactIQDetectLoop:
986
	wait 5
987
	set $oldIncomingCall $incomingCall
988
	call ImpactIQDetectReadCID
989
	if $oldIncomingCall != $incomingCall goto ImpactIQDetectDone
990
	goto ImpactIQDetectLoop
991

    
992
# Examine the CID from each port
993
ImpactIQDetectDone:
994
	if $incomingCall !match "((.{14}).{10}) ((.{14}).{10}) ((.{14}).{10})" goto RingbackDetectNoCID
995
	set $dataCID $matchedString2
996
	set $voice1CID $matchedString4
997
	set $voice2CID $matchedString6
998
	set $cid $dataCID
999
	call ImpactIQDetectTryCID
1000
	set $cid $voice1CID
1001
	call ImpactIQDetectTryCID
1002
	set $cid $voice2CID
1003
	call ImpactIQDetectTryCID
1004
	goto RingbackDetectNoCID
1005

    
1006
# Look for a valid CID string
1007
ImpactIQDetectTryCID:
1008
	if $cid match " *" return
1009
	if $cid match " *N/A" return
1010
	goto RingbackDetectCID
1011

    
1012
# Read first line of incoming call log (it's the 14th line of output)
1013
ImpactIQDetectReadCID:
1014
	set $modemCmd "\\N"
1015
	call ModemCmdSend
1016
	call ReadLine
1017
# Older version of the 3Com don't support this command
1018
	if $matchedString == "OK" return
1019
	if $matchedString match "ERR(O)?(R)?" return
1020
	call ReadLine
1021
	call ReadLine
1022
	call ReadLine
1023
	call ReadLine
1024
	call ReadLine
1025
	call ReadLine
1026
	call ReadLine
1027
	call ReadLine
1028
	call ReadLine
1029
	call ReadLine
1030
	call ReadLine
1031
	call ReadLine
1032
	call ReadLine
1033
	set $incomingCall $matchedString
1034
	call GetOK
1035
	return
1036

    
1037
#################################################################
1038
#
1039
#	NEC ATERM SETUP
1040
#
1041
#################################################################
1042

    
1043
AtermSetup:
1044
	if $AtermHardReset == "yes" goto AtermSetup2
1045

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

    
1048
	log "Initializing modem..."
1049
	print "ATZ98\r\n"
1050
	wait 6
1051
	set $AtermHardReset "yes"
1052
	failure
1053

    
1054
AtermSetup2:
1055

    
1056
# Set to 230K baud if we support it
1057

    
1058
	set $modemCmd ""
1059
	set $newBaudrate "230400"
1060
	call SetBaudrate
1061

    
1062
# Is this an IT55 or IT65?
1063

    
1064
	set $atermIT65 ""
1065
	set $modemCmd "$$N14=0"
1066
	call ModemCmd
1067
	if $modemCmdResult == "OK" set $atermIT65 "yes"
1068

    
1069
# Set normal stuff, PPP mode
1070

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

    
1074
# Set the multi-link setting
1075

    
1076
	set $bondCmd "$$N11=0"
1077
	if $TA_Bonding == "yes" set $bondCmd "$$N11=1"
1078
	set $modemCmd "$bondCmd"
1079
	call ModemCmd2
1080

    
1081
# Additional settings for the IT65 go here...
1082

    
1083
	if $atermIT65 != "yes" return
1084
	return
1085

    
1086
#################################################################
1087
#
1088
#	MICROCOM DESKPORTE SETUP
1089
#
1090
#################################################################
1091

    
1092
DeskPorteSetup:
1093
	set $noDialToneSubr GenericNoDialtone
1094
	set $temp "M1"
1095
	if $SpeakerOff == "yes" set $temp "M0"
1096
	set $modemCmd "Z&F&C1&D2X3E0S0=0${temp}"
1097
	call ModemCmd2
1098
	return
1099

    
1100
#################################################################
1101
#
1102
#	KOREAN TERMINAL ADAPTER SETUP
1103
#
1104
#################################################################
1105

    
1106
NTKSetup:
1107
	set $noDialToneSubr GenericNoDialtone
1108
	set $modemCmd "ZB40&J3"
1109
	call ModemCmd2
1110
	return
1111

    
1112
#################################################################
1113
#
1114
#	ADTRAN EXPRESS XRT SETUP
1115
#
1116
#################################################################
1117

    
1118
AdtranXRTSetup:
1119
	set $noDialToneSubr ImpactIQNoDialtone
1120
	set $dialErrorSubr AdtranXRTDialError
1121
	set $bonding "&F1"
1122
	if $TA_Bonding == "yes" set $bonding "&F2"
1123
	set $chap "S118=0"
1124
	if $TA_AuthChap == "yes" set $chap "S118=32"
1125
	set $origCmd "S53=3"
1126
	if $TA_56K == "yes" set $origCmd "S53=2"
1127
	if $TA_VoiceCall == "yes" set $origCmd "S53=0"
1128
	set $modemCmd "ZE0${bonding}&C1&D2${chap}${origCmd}"
1129
	call ModemCmd2
1130

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

    
1133
	if $optimize == "yes" return
1134
	call AdtranXRTCheckConfig
1135
	call AdtranXRTCheckLink
1136
	return
1137

    
1138
##
1139
## What to do when ERROR when dialing
1140
##
1141
## This is what the Adtran returns if the line is not ready yet.
1142
## So we look into the status buffer to see if we recognize the
1143
## error condition.
1144
##
1145

    
1146
AdtranXRTDialError:
1147
	call AdtranXRTDiagnose
1148
	failure
1149

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

    
1155
AdtranXRTDiagnose:
1156
	log "Checking the Adtran status buffer..."
1157
	print "AT!S\r\n"
1158
	match "\r\n\r\n" AdtranXRTDiagBlank
1159
	match "END OF STATUS BUFFER" AdtranXRTDiagBlank
1160
	match "L1 not up." AdtranXRTDiagL1
1161
	match "FACILITY_NOT_SUBSCRIBED" AdtranXRTDiagFNS
1162
	match "Unknown AT cmd" IQSyncOK
1163
	match "ERR"
1164
	match "OK\r\n"
1165
	wait 3
1166
	call GetOK
1167
	log "The ISDN modem did not properly answer ATS59?"
1168
	failure
1169
	goto ImpactIQCheckInit
1170

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

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

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

    
1183
##
1184
## AdtranXRTCheckLink
1185
##
1186
## Check the link status registers
1187
##
1188

    
1189
AdtranXRTCheckLink:
1190
	log "Checking the ISDN modem's link status..."
1191
	print "AT!S1\r\n"
1192
	match "Link In Sync" AdtranXRTSyncReg
1193
	match "Getting" AdtranXRTSyncReg
1194
	match "Register" AdtranXRTSyncReg
1195
	match "Ready" AdtranXRTSyncOK
1196
	match "Down" AdtranXRTSyncFail
1197
	match "ERR"
1198
	match "OK\r\n"
1199
	wait 3
1200
	call GetOK
1201
	log "The ISDN modem did not properly answer AT!S1."
1202
	failure
1203
AdtranXRTSyncOK:
1204
	return
1205
AdtranXRTSyncFail:
1206
	print "ATDT1\r\n"
1207
	wait 1
1208
	goto AdtranXRTDiagnose
1209
AdtranXRTSyncReg:
1210
	log "The ISDN modem is initializing itself."
1211
	failure
1212

    
1213
##
1214
## AdtranXRTCheckConfig
1215
##
1216
## Verify and adjust ISDN configuration as necessary
1217
##
1218

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

    
1223
	set $checkCmd "S52"
1224
	set $checkValue "000"
1225
	set $checkValueNew "0"
1226
	if $TA_SwitchType == "DMS-100" set $checkValue "001"
1227
	if $TA_SwitchType == "DMS-100" set $checkValueNew "1"
1228
	if $TA_SwitchType == "NI-1" set $checkValue "002"
1229
	if $TA_SwitchType == "NI-1" set $checkValueNew "2"
1230
	set $checkMsg "Reprogramming switch type ($TA_SwitchType)..."
1231
	call ModemCheckValue
1232

    
1233
# Check directory numbers
1234

    
1235
AdtranXRTCheckDirnos:
1236
	set $checkCmd "SS62"
1237
	set $checkValue $TA_Dirno1
1238
	set $checkValueNew $TA_Dirno1
1239
	set $checkMsg "Reprogramming line #1 directory number..."
1240
	call ModemCheckValue
1241

    
1242
	set $checkCmd "SS63"
1243
	set $checkValue $TA_Dirno2
1244
	set $checkValueNew $TA_Dirno2
1245
	set $checkMsg "Reprogramming line #2 directory number..."
1246
	call ModemCheckValue
1247

    
1248
# Check SPIDs
1249

    
1250
	set $checkCmd "SS60"
1251
	set $checkValue $TA_SPID1
1252
	set $checkValueNew $TA_SPID1
1253
	set $checkMsg "Reprogramming line #1 SPID..."
1254
	call ModemCheckValue
1255

    
1256
	set $checkCmd "SS61"
1257
	set $checkValue $TA_SPID2
1258
	set $checkValueNew $TA_SPID2
1259
	set $checkMsg "Reprogramming line #2 SPID..."
1260
	call ModemCheckValue
1261

    
1262
# Restart if necessary
1263

    
1264
	if $valueChanged == "no" return
1265
	log "Restarting ISDN modem with the new configuration..."
1266
	set $modemCmd "&W"
1267
	call ModemCmd2
1268
	set $modemCmd "Z"
1269
	call ModemCmd2
1270
	failure
1271

    
1272
#################################################################
1273
#
1274
#	BASIC MODEM STUFF
1275
#
1276
#################################################################
1277

    
1278
##
1279
## ModemAnswer
1280
##
1281
## Wait for a RING and answer it. Variables:
1282
##
1283
##  $RingTimeout	How long for a RING before giving up (default 10 min)
1284
##  $ConnectTimeout	How long for connect after answering (default 45 secs)
1285
##
1286
## Returns:
1287
##
1288
##  $answerReturn	"OK" or "FAIL"
1289
##  $ConnectionSpeed	Connection speed reported by modem (possibly empty)
1290
##
1291

    
1292
ModemAnswer:
1293
	if $RingTimeout == "" set $RingTimeout 600
1294
	if $ConnectTimeout == "" set $ConnectTimeout 45
1295
	match "RING\r" ModemAnswerGotRing
1296
	wait $RingTimeout
1297
	log "No RING detected after $RingTimeout seconds."
1298
	set $answerReturn "FAIL"
1299
	return
1300
ModemAnswerGotRing:
1301
	log "Incoming call detected..."
1302
	print "ATA\r\n"
1303
	regex "CONNECT *([0-9]*).*$" ModemAnswerConnect
1304
	wait $ConnectTimeout
1305
	log "Failed to connect incoming call."
1306
	set $answerReturn "FAIL"
1307
	return
1308
ModemAnswerConnect:
1309
	set $ConnectionSpeed $matchedString1
1310
	set $answerReturn "OK"
1311
	return
1312

    
1313
##
1314
## ModemFind
1315
##
1316
## Just try to get where we are talking to the modem.
1317
## Returns with $ErrorMsg equal to the empty string if
1318
## we found the modem, or else some error message if not.
1319
##
1320

    
1321
ModemFind:
1322

    
1323
# Do a quick check first...
1324

    
1325
	set $ErrorMsg ""
1326
	call ModemFind4
1327
	if $modemCmdResult == "OK" return
1328
	if $Serial230K != "yes" goto ModemFind1
1329
	set $newBaudrate 230400
1330
	if $Baudrate == "230400" set $newBaudrate 115200
1331
	set $Baudrate $newBaudrate
1332
	call ModemFind4
1333
	if $modemCmdResult == "OK" return
1334

    
1335
# Now try possible baud rates more extensively
1336

    
1337
ModemFind1:
1338
	set $Baudrate 115200
1339
	call ModemFind2
1340
	if $modemCmdResult == "OK" return
1341
	if $Serial230K != "yes" return
1342
	set $Baudrate 230400
1343
	wait 1
1344
	call ModemFind2
1345
	if $modemCmdResult == "OK" return
1346
	set $ErrorMsg "The modem is not responding."
1347
	return
1348

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

    
1351
ModemFind2:
1352
	call ModemFind3
1353
	if $modemCmdResult == "OK" return
1354
	call SendTriplePlus
1355
	call ModemFind3
1356
	return
1357

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

    
1360
ModemFind3:
1361
	set $modemCmd ""
1362
	set $modemCmdTimeout 1
1363
	call ModemCmd0
1364
	if $modemCmdResult == "OK" return
1365
	set $modemCmd "Z"
1366
	set $modemCmdTimeout 2
1367
	call ModemCmd0
1368
	if $modemCmdResult == "OK" return
1369
ModemFind4:
1370
	set $modemCmd ""
1371
	set $modemCmdTimeout 1
1372
	call ModemCmd0
1373
	return
1374

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

    
1385
SetBaudrate:
1386
	if $Baudrate == $newBaudrate return
1387
	if $newBaudrate != 230400 goto SetBaudrate2
1388
	if $Serial230K != "yes" return
1389
SetBaudrate2:
1390
	log "Setting serial port speed to $newBaudrate..."
1391
	print "AT$modemCmd\r\n"
1392
	wait 1
1393
	set $Baudrate $newBaudrate
1394
	wait 1
1395
	set $modemCmd ""
1396
	goto ModemCmd2
1397

    
1398
##
1399
## ModemCheckValue 
1400
##
1401
## Check the value in an S-register (or equivalent). If it is
1402
## not what we want, then change it and set $valueChanged to "yes",
1403
## and log $checkMsg (if not empty). The value $checkValueNew is
1404
## what we program the S-register to equal if it doesn't match.
1405
##
1406
## The $checkValue must match exactly on the first line of output.
1407
## If we get any weird error, then fail the script.
1408
##
1409

    
1410
ModemCheckValue:
1411
	set $modemCmd "${checkCmd}?"
1412
	call ModemQuery
1413
	if $modemQuery == $checkValue return
1414
	set $valueChanged "yes"
1415
	set $checkValueMyNew $checkValueNew
1416
	if $checkValueNew == "" set $checkValueMyNew $checkValue
1417
	set $modemCmd "${checkCmd}=${checkValueMyNew}"
1418
	if $checkMsg != "" log $checkMsg
1419
	goto ModemCmd2
1420

    
1421
##
1422
## ModemCmd
1423
##
1424
## Do the modem command in $modemCmd. Set $modemCmdResult to
1425
##
1426
##	OK	The modem returned "OK"
1427
##	ERROR	The modem returned "ERR" or "ERROR"
1428
##	TIMEOUT	The modem did not respond within three seconds
1429
##
1430
## If ERR or TIMEOUT, also set $ErrorMsg to an appropriate message
1431
##
1432

    
1433
ModemCmd:
1434
	set $modemCmdTimeout 3
1435
ModemCmd0:
1436
	print "AT$modemCmd\r\n"
1437
	match "OK\r\n" ModemCmdOk
1438
	match "ERR" ModemCmdErr
1439
	wait $modemCmdTimeout
1440
	set $modemCmdResult "TIMEOUT"
1441
ModemCmdTimeout:
1442
	set $ErrorMsg "The modem is not responding to \"AT$modemCmd\" at ModemCmd: label."
1443
	return
1444
ModemCmdOk:
1445
	set $modemCmdResult "OK"
1446
	return
1447
ModemCmdErr:
1448
	set $modemCmdResult "ERROR"
1449
	set $ErrorMsg "The modem responded with \"ERROR\" to the command \"AT$modemCmd\" at ModemCmd: label."
1450
	return
1451

    
1452
##
1453
## ModemCmd2
1454
##
1455
## Do the modem command in $modemCmd. If we don't get OK back,
1456
## fail the script and log an error message.
1457
##
1458

    
1459
ModemCmd2:
1460
	call ModemCmd
1461
	if $modemCmdResult == "OK" return
1462
	log $ErrorMsg
1463
	failure
1464

    
1465
##
1466
## ModemCmdSend
1467
##
1468
## Send a modem command and read the echo'ed CR-LF
1469
##
1470

    
1471
ModemCmdSend:
1472
	print "AT$modemCmd\r\n"
1473
	match "\r\n" ModemCmdSend2
1474
	wait 3
1475
	goto ModemCmdTimeout
1476
ModemCmdSend2:
1477
	return
1478

    
1479
##
1480
## ModemQuery
1481
##
1482
## Do the $modemCmd and expect exactly one line of response, then OK.
1483
## Return the response in $modemQuery. If anything goes wrong, die.
1484
##
1485

    
1486
ModemQuery:
1487
	call ModemCmdSend
1488
	call ReadLine
1489
	set $modemQuery $matchedString
1490
	if $modemQuery == "ERR" goto ModemQueryError
1491
	if $modemQuery == "ERROR" goto ModemQueryError
1492
	goto GetOK
1493
ModemQueryError:
1494
	call ModemCmdErr
1495
	failure
1496

    
1497
# Same thing, but strip leading and trailing blanks
1498

    
1499
ModemQueryStrip:
1500
	call ModemQuery
1501
	if $modemQuery match " *(.*) *" nop
1502
	set $modemQuery $matchedString1
1503
	return
1504

    
1505
##
1506
## ReadLine
1507
##
1508
## Read the next line of output. It should come within $modemCmdTimeout
1509
## seconds, or else we fail. Return it in $matchedString.
1510
##
1511

    
1512
ReadLine:
1513
	regex "^.*$" ReadLineDone
1514
	wait $modemCmdTimeout
1515
	set $ErrorMsg "The modem is not responding."
1516
	log $ErrorMsg
1517
	failure
1518
ReadLineDone:
1519
	return
1520

    
1521
##
1522
## Get an OK within 3 seconds or die
1523
##
1524

    
1525
GetOK:
1526
	match "OK\r\n" GotOK
1527
	wait 3
1528
	log "The modem did not respond with \"OK\"."
1529
	failure
1530
GotOK:
1531
	return
1532

    
1533
##
1534
## Send "+++" to get modem attention if on-line
1535
##
1536

    
1537
SendTriplePlus:
1538
	print "\r\n"
1539
	wait 2
1540
	print "+++"
1541
	wait 2
1542
	return
1543

    
1544
#################################################################
1545
#
1546
#	LOGIN AUTO-SCRIPTING
1547
#
1548
#################################################################
1549

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

    
1571
AutoLogin:
1572
	log "Initiating auto-login..."
1573

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

    
1576
	timer autoLogin 5 AutoLoginTimeout
1577

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

    
1580
	match autoLogin "\x7e\xff\x03\xc0\x21" AutoLoginFrame
1581
	match autoLogin "\x7e\xff\x7d\x23\xc0\x21\x7d\x21" AutoLoginFrame
1582
	match autoLogin "\x7e\xc0\x21" AutoLoginFrame
1583

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

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

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

    
1596
	match "ogin" AutoLoginPrompt
1597
	match "name" AutoLoginPrompt
1598
	wait 1
1599
	print "\r"
1600
	match "ogin" AutoLoginPrompt
1601
	match "name" AutoLoginPrompt
1602
	wait
1603

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

    
1606
AutoLoginPrompt:
1607
	log "Sending login..."
1608
	print "${Login}\r"
1609
	match "word"
1610
	wait
1611
	log "Sending password..."
1612
	print "${Password}\r"
1613
	match "\r"
1614
	wait
1615
	if $didLogin != "yes" match "ogin:" LoginAgain
1616
	match ">"
1617
	match "%"
1618
	match ":"
1619
	wait
1620
	log "Enabling PPP..."
1621
	print "ppp\r"
1622
	cancel all
1623
	return
1624

    
1625
LoginAgain:
1626
	set $didLogin "yes"
1627
	goto AutoLoginPrompt
1628

    
1629
# We saw a PPP frame
1630

    
1631
AutoLoginFrame:
1632
	log "Detected PPP frame."
1633
	cancel all
1634
	return
1635

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

    
1638
AutoLoginTimeout:
1639
	log "Auto-login timeout."
1640
	cancel all
1641
	return
1642

    
(4-4/23)