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
	if $SimPin != "" call DialPeerSetPin
87
	if $APN != "" call DialPeerSetAPN
88
	log "Dialing server at $Telephone..."
89
	call ModemDial
90
	if $dialResult == "OK" goto DialPeerOK
91
	set $optimize "no"
92
	failure
93

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

    
103
DialPeerSetPin:
104
	set $modemCmd "+CPIN=?"
105
	log $modemCmd
106
	call ModemQuery
107
	log $modemQuery
108
	if $modemQuery match ".*READY.*" PinReady
109
	set $modemCmd "+CPIN=\"$SimPin\""
110
	call ModemCmd2
111
	wait $PinWait
112
PinReady:
113
	return
114

    
115
DialPeerSetAPN:
116
	set $modemCmd "+CGDCONT=$APNum,\"IP\",\"$APN\""
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
	match "NO CARRIER" DialAbortNoCar
148
	match "NO DIAL" DialAbortNoDial
149
	match "BUSY" DialAbortBusy
150
	regex "CONNECT *([0-9]*).*$" DialConnect
151
	match "ERR" DialError
152
	wait $ConnectTimeout
153
	log "No response from the modem after dialing."
154
	return
155
DialAbortNoCar:
156
	log "The remote modem did not answer."
157
	return
158
DialAbortNoDial:
159
	if $noDialToneSubr != "" goto $noDialToneSubr
160
	log "No dialtone. Is the modem plugged in?"
161
	return
162
DialError:
163
	if ${ModTelephone} != "" goto DialErrorInit
164
	log "Invalid empty telephone number."
165
	return
166
DialErrorInit:
167
	if $dialErrorSubr != "" goto $dialErrorSubr
168
	log "Invalid dial init string."
169
	return
170
DialAbortBusy:
171
	log "The line was busy."
172
	return
173
DialConnect:
174
	set $ConnectionSpeed $matchedString1
175
	set $dialResult "OK"
176
	return
177

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
301
# We couldn't determine calling party number
302

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

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

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

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

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

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

    
396
ModemIdentGeneric:
397
	set $ModemDescription "Hayes compatible modem"
398
	set $ModemSetupFunc GenericSetup
399
	return
400

    
401
ModemIdentCustom:
402
	set $ModemDescription "Custom modem"
403
	set $ModemSetupFunc CustomSetup
404
	return
405

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

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

    
440
ModemIdentAdtranXRT:
441
	set $ModemDescription "AdTran Express XR/XRT"
442
	call GetOK
443
	set $ModemSetupFunc AdtranXRTSetup
444
	set $ModemIsAnalog "no"
445
	return
446

    
447
ModemIdentAterm:
448
	set $ModemDescription "NEC Aterm TA"
449
	call GetOK
450
	set $ModemSetupFunc AtermSetup
451
	set $ModemIsAnalog "no"
452
	return
453

    
454
ModemIdentNTK:
455
	set $ModemDescription "$matchedString"
456
	call GetOK
457
	set $ModemSetupFunc NTKSetup
458
	set $ModemIsAnalog "no"
459
	return
460

    
461
ModemIdentDeskPorte:
462
	set $ModemDescription "$matchedString"
463
	call GetOK
464
	set $ModemSetupFunc DeskPorteSetup
465
	return
466

    
467
ModemIdentZoom56:
468
ModemIdentSmartOne:
469
	set $ModemDescription "$matchedString"
470
	call GetOK
471
	set $ModemSetupFunc Modem56Setup
472
	return
473

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

    
481
#################################################################
482
#
483
#	GENERIC MODEM SETUP
484
#
485
#################################################################
486

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

    
495
CustomSetup:
496
	set $noDialToneSubr GenericNoDialtone
497
	set $modemCmd "${InitString}"
498
	call ModemCmd2
499
	return
500

    
501
GenericNoDialtone:
502
	log "No dialtone. Is the modem plugged in?"
503
	return
504

    
505
#################################################################
506
#
507
#	USR MODEM SETUP
508
#
509
#################################################################
510

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

    
526
#################################################################
527
#
528
#	GENERAL 56K MODEM SETUP
529
#
530
#################################################################
531

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

    
540
#################################################################
541
#
542
#	LUCENT WINMODEM AND XIRCOM 56K SETUP
543
#
544
#################################################################
545

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

    
557
#################################################################
558
#
559
#	BITSURFR PRO AND BITSURFR PRO EZ SETUP
560
#
561
#################################################################
562

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

    
570
# Set to 230K baud if we support it
571

    
572
	if $bitsEZ != "yes" goto BitsurfrSetup1
573
	set $modemCmd "@P2=230400"
574
	set $newBaudrate "230400"
575
	call SetBaudrate
576

    
577
# Check software revision and ISDN settings
578

    
579
BitsurfrSetup1:
580
	if $optimize == "yes" goto BitsurfrSetup2
581
	if $bitsEZ != "yes" call BitsurfrCheckRev
582
	call BitsurfrCheckConfig
583

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

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

    
597
# BS PRO EZ changes
598

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

    
606
##
607
## What to do when NO DIALTONE
608
##
609

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

    
614
##
615
## BitsurfrCheckConfig
616
##
617
## Verify and adjust ISDN configuration as necessary
618
##
619

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

    
624
# Check switch type
625

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

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

    
648
# Check directory numbers
649

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

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

    
665
# Check SPIDs
666

    
667
	set $checkCmd "*1!C6"
668
	set $checkValue $TA_SPID1
669
	set $checkValueNew $TA_SPID1
670
	set $checkMsg "Reprogramming voice line #1 SPID..."
671
	call ModemCheckValue
672

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

    
682
# Restart if necessary
683

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

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

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

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

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

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

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

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

    
784
#################################################################
785
#
786
#	3COM IMPACT IQ SETUP
787
#
788
#################################################################
789

    
790
ImpactIQSetup:
791
	set $noDialToneSubr ImpactIQNoDialtone
792
	set $modemCmd "Z&F%C2E0"
793
	call ModemCmd2
794

    
795
# Set to 230K baud if we support it
796

    
797
	set $modemCmd "$$B230400"
798
	set $newBaudrate "230400"
799
	call SetBaudrate
800

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

    
803
ImpactIQSetup1:
804
	if $optimize == "yes" goto IQInitSetup2
805
	call ImpactIQCheckConfig
806
	call ImpactIQCheckLink
807

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

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

    
821
##
822
## What to do when NO DIALTONE
823
##
824

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

    
829
##
830
## ImpactIQCheckLink
831
##
832
## Check the link status registers
833
##
834

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

    
854
ImpactIQCheckInit:
855
	set $impactInitReg "57"
856
	call IQInitCheck
857
	if $TA_Bonding == "yes" return
858
	set $impactInitReg "58"
859
	call IQInitCheck
860
	return
861

    
862
# A little subroutine
863

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

    
883
##
884
## ImpactIQCheckConfig
885
##
886
## Verify and adjust ISDN configuration as necessary
887
##
888

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

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

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

    
908
# Check directory numbers
909

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

    
917
	set $checkCmd "S53"
918
	set $checkValue $TA_Dirno2
919
	set $checkValueNew $TA_Dirno2
920
	set $checkMsg "Reprogramming line #2 directory number..."
921
	call ModemCheckValue
922

    
923
# Check SPIDs
924

    
925
	set $checkCmd "S52"
926
	set $checkValue $TA_SPID1
927
	set $checkValueNew $TA_SPID1
928
	set $checkMsg "Reprogramming line #1 SPID..."
929
	call ModemCheckValue
930

    
931
	set $checkCmd "S54"
932
	set $checkValue $TA_SPID2
933
	set $checkValueNew $TA_SPID2
934
	set $checkMsg "Reprogramming line #2 SPID..."
935
	call ModemCheckValue
936

    
937
# Check 56K/64K setting
938

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

    
948
# Check Multi-link setting
949

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

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

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

    
966
##
967
## Detect an incoming call with the 3Com
968
##
969

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

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

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

    
1009
# Read first line of incoming call log (it's the 14th line of output)
1010
ImpactIQDetectReadCID:
1011
	set $modemCmd "\\N"
1012
	call ModemCmdSend
1013
	call ReadLine
1014
# Older version of the 3Com don't support this command
1015
	if $matchedString == "OK" return
1016
	if $matchedString match "ERR(O)?(R)?" return
1017
	call ReadLine
1018
	call ReadLine
1019
	call ReadLine
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
	set $incomingCall $matchedString
1031
	call GetOK
1032
	return
1033

    
1034
#################################################################
1035
#
1036
#	NEC ATERM SETUP
1037
#
1038
#################################################################
1039

    
1040
AtermSetup:
1041
	if $AtermHardReset == "yes" goto AtermSetup2
1042

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

    
1045
	log "Initializing modem..."
1046
	print "ATZ98\r\n"
1047
	wait 6
1048
	set $AtermHardReset "yes"
1049
	failure
1050

    
1051
AtermSetup2:
1052

    
1053
# Set to 230K baud if we support it
1054

    
1055
	set $modemCmd ""
1056
	set $newBaudrate "230400"
1057
	call SetBaudrate
1058

    
1059
# Is this an IT55 or IT65?
1060

    
1061
	set $atermIT65 ""
1062
	set $modemCmd "$$N14=0"
1063
	call ModemCmd
1064
	if $modemCmdResult == "OK" set $atermIT65 "yes"
1065

    
1066
# Set normal stuff, PPP mode
1067

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

    
1071
# Set the multi-link setting
1072

    
1073
	set $bondCmd "$$N11=0"
1074
	if $TA_Bonding == "yes" set $bondCmd "$$N11=1"
1075
	set $modemCmd "$bondCmd"
1076
	call ModemCmd2
1077

    
1078
# Additional settings for the IT65 go here...
1079

    
1080
	if $atermIT65 != "yes" return
1081
	return
1082

    
1083
#################################################################
1084
#
1085
#	MICROCOM DESKPORTE SETUP
1086
#
1087
#################################################################
1088

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

    
1097
#################################################################
1098
#
1099
#	KOREAN TERMINAL ADAPTER SETUP
1100
#
1101
#################################################################
1102

    
1103
NTKSetup:
1104
	set $noDialToneSubr GenericNoDialtone
1105
	set $modemCmd "ZB40&J3"
1106
	call ModemCmd2
1107
	return
1108

    
1109
#################################################################
1110
#
1111
#	ADTRAN EXPRESS XRT SETUP
1112
#
1113
#################################################################
1114

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

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

    
1130
	if $optimize == "yes" return
1131
	call AdtranXRTCheckConfig
1132
	call AdtranXRTCheckLink
1133
	return
1134

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

    
1143
AdtranXRTDialError:
1144
	call AdtranXRTDiagnose
1145
	failure
1146

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

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

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

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

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

    
1180
##
1181
## AdtranXRTCheckLink
1182
##
1183
## Check the link status registers
1184
##
1185

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

    
1210
##
1211
## AdtranXRTCheckConfig
1212
##
1213
## Verify and adjust ISDN configuration as necessary
1214
##
1215

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

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

    
1230
# Check directory numbers
1231

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

    
1239
	set $checkCmd "SS63"
1240
	set $checkValue $TA_Dirno2
1241
	set $checkValueNew $TA_Dirno2
1242
	set $checkMsg "Reprogramming line #2 directory number..."
1243
	call ModemCheckValue
1244

    
1245
# Check SPIDs
1246

    
1247
	set $checkCmd "SS60"
1248
	set $checkValue $TA_SPID1
1249
	set $checkValueNew $TA_SPID1
1250
	set $checkMsg "Reprogramming line #1 SPID..."
1251
	call ModemCheckValue
1252

    
1253
	set $checkCmd "SS61"
1254
	set $checkValue $TA_SPID2
1255
	set $checkValueNew $TA_SPID2
1256
	set $checkMsg "Reprogramming line #2 SPID..."
1257
	call ModemCheckValue
1258

    
1259
# Restart if necessary
1260

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

    
1269
#################################################################
1270
#
1271
#	BASIC MODEM STUFF
1272
#
1273
#################################################################
1274

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

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

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

    
1318
ModemFind:
1319

    
1320
# Do a quick check first...
1321

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

    
1332
# Now try possible baud rates more extensively
1333

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

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

    
1348
ModemFind2:
1349
	call ModemFind3
1350
	if $modemCmdResult == "OK" return
1351
	call SendTriplePlus
1352
	call ModemFind3
1353
	return
1354

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

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

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

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

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

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

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

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

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

    
1456
ModemCmd2:
1457
	call ModemCmd
1458
	if $modemCmdResult == "OK" return
1459
	log $ErrorMsg
1460
	failure
1461

    
1462
##
1463
## ModemCmdSend
1464
##
1465
## Send a modem command and read the echo'ed CR-LF
1466
##
1467

    
1468
ModemCmdSend:
1469
	print "AT$modemCmd\r\n"
1470
	match "\r\n" ModemCmdSend2
1471
	wait 3
1472
	goto ModemCmdTimeout
1473
ModemCmdSend2:
1474
	return
1475

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

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

    
1494
# Same thing, but strip leading and trailing blanks
1495

    
1496
ModemQueryStrip:
1497
	call ModemQuery
1498
	if $modemQuery match " *(.*) *" nop
1499
	set $modemQuery $matchedString1
1500
	return
1501

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

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

    
1518
##
1519
## Get an OK within 3 seconds or die
1520
##
1521

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

    
1530
##
1531
## Send "+++" to get modem attention if on-line
1532
##
1533

    
1534
SendTriplePlus:
1535
	print "\r\n"
1536
	wait 2
1537
	print "+++"
1538
	wait 2
1539
	return
1540

    
1541
#################################################################
1542
#
1543
#	LOGIN AUTO-SCRIPTING
1544
#
1545
#################################################################
1546

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

    
1568
AutoLogin:
1569
	log "Initiating auto-login..."
1570

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

    
1573
	timer autoLogin 5 AutoLoginTimeout
1574

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

    
1577
	match autoLogin "\x7e\xff\x03\xc0\x21" AutoLoginFrame
1578
	match autoLogin "\x7e\xff\x7d\x23\xc0\x21\x7d\x21" AutoLoginFrame
1579
	match autoLogin "\x7e\xc0\x21" AutoLoginFrame
1580

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

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

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

    
1593
	match "ogin" AutoLoginPrompt
1594
	match "name" AutoLoginPrompt
1595
	wait 1
1596
	print "\r"
1597
	match "ogin" AutoLoginPrompt
1598
	match "name" AutoLoginPrompt
1599
	wait
1600

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

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

    
1622
LoginAgain:
1623
	set $didLogin "yes"
1624
	goto AutoLoginPrompt
1625

    
1626
# We saw a PPP frame
1627

    
1628
AutoLoginFrame:
1629
	log "Detected PPP frame."
1630
	cancel all
1631
	return
1632

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

    
1635
AutoLoginTimeout:
1636
	log "Auto-login timeout."
1637
	cancel all
1638
	return
1639

    
(3-3/16)