Project

General

Profile

Download (39.6 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 "The modem is not responding."
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 "C885" ModemIdentC885
356
	match "ERR" ModemIdentGeneric
357
	match "OK\r\n" ModemIdentGeneric
358
	wait 3
359
	print "ATI8\r\n"
360
	match "BitSURFR PRO\r" ModemIdentBitsurfr
361
	match "BitSURFR PRO EZ" ModemIdentBitsurfrEZ
362
	match "3C882" ModemIdentImpactIQ
363
	match "ERR"
364
	match "OK\r\n"
365
	wait 3
366
	print "ATI3\r\n"
367
	match "Courier" ModemIdentUsr
368
	match "Sportster" ModemIdentUsr
369
	match "3ComImpact IQ" ModemIdentImpactIQ
370
	match "U.S. Robotics 56K" ModemIdentUsr
371
	match "ZOOM V.90" ModemIdentZoom56
372
	match "LT V.90" ModemIdentLucent
373
	match "ERR"
374
	match "OK\r\n"
375
	wait 3
376
	print "ATI4\r\n"
377
	match "AtermIT NEC Corporation" ModemIdentAterm
378
	match "INSMATEV-7 NTT Corporation" ModemIdentAterm
379
	match "MNP Class 10 V.34 Modem" ModemIdentDeskPorte
380
	match "Smart One 56" ModemIdentSmartOne
381
	match "ERR"
382
	match "OK\r\n"
383
	wait 3
384
	print "ATI1\r\n"
385
	match "NTK omni.net" ModemIdentNTK
386
	match "ERR" ModemIdentGeneric
387
	match "OK\r\n" ModemIdentGeneric
388
	wait 3
389
	log "The modem is not responding."
390
	failure
391

    
392
ModemIdentGeneric:
393
	set $ModemDescription "Hayes compatible modem"
394
	set $ModemSetupFunc GenericSetup
395
	return
396

    
397
ModemIdentCustom:
398
	set $ModemDescription "Custom modem"
399
	set $ModemSetupFunc CustomSetup
400
	return
401

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

    
427
ModemIdentImpactIQ:
428
	set $matchedString "3ComImpact IQ"
429
	set $ModemDescription "$matchedString"
430
	call GetOK
431
	set $ModemSetupFunc ImpactIQSetup
432
	set $ModemIsAnalog "no"
433
	set $ModemDetectRing ImpactIQDetectRing
434
	return
435

    
436
ModemIdentAdtranXRT:
437
	set $ModemDescription "AdTran Express XR/XRT"
438
	call GetOK
439
	set $ModemSetupFunc AdtranXRTSetup
440
	set $ModemIsAnalog "no"
441
	return
442

    
443
ModemIdentAterm:
444
	set $ModemDescription "NEC Aterm TA"
445
	call GetOK
446
	set $ModemSetupFunc AtermSetup
447
	set $ModemIsAnalog "no"
448
	return
449

    
450
ModemIdentNTK:
451
	set $ModemDescription "$matchedString"
452
	call GetOK
453
	set $ModemSetupFunc NTKSetup
454
	set $ModemIsAnalog "no"
455
	return
456

    
457
ModemIdentDeskPorte:
458
	set $ModemDescription "$matchedString"
459
	call GetOK
460
	set $ModemSetupFunc DeskPorteSetup
461
	return
462

    
463
ModemIdentZoom56:
464
ModemIdentSmartOne:
465
	set $ModemDescription "$matchedString"
466
	call GetOK
467
	set $ModemSetupFunc Modem56Setup
468
	return
469

    
470
# Support the Lucent Winmodem, Xircom 56k
471
ModemIdentLucent:
472
	set $ModemDescription "$matchedString"
473
	call GetOK
474
	set $ModemSetupFunc ModemLucentSetup
475
	return
476

    
477
#################################################################
478
#
479
#	GENERIC MODEM SETUP
480
#
481
#################################################################
482

    
483
GenericSetup:
484
	set $noDialToneSubr GenericNoDialtone
485
	set $temp "M1"
486
	if $SpeakerOff == "yes" set $temp "M0"
487
	set $modemCmd "&F&C1&D2E0S0=0${temp}"
488
	call ModemCmd2
489
	return
490

    
491
CustomSetup:
492
	set $noDialToneSubr GenericNoDialtone
493
	set $modemCmd "${InitString}"
494
	call ModemCmd2
495
	return
496

    
497
GenericNoDialtone:
498
	log "No dialtone. Is the modem plugged in?"
499
	return
500

    
501
#################################################################
502
#
503
#	USR MODEM SETUP
504
#
505
#################################################################
506

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

    
522
#################################################################
523
#
524
#	GENERAL 56K MODEM SETUP
525
#
526
#################################################################
527

    
528
Modem56Setup:
529
	set $noDialToneSubr GenericNoDialtone
530
	set $temp "M1"
531
	if $SpeakerOff == "yes" set $temp "M0"
532
	set $modemCmd "&FL2W2E0${temp}"
533
	call ModemCmd2
534
	return
535

    
536
#################################################################
537
#
538
#	LUCENT WINMODEM AND XIRCOM 56K SETUP
539
#
540
#################################################################
541

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

    
553
#################################################################
554
#
555
#	BITSURFR PRO AND BITSURFR PRO EZ SETUP
556
#
557
#################################################################
558

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

    
566
# Set to 230K baud if we support it
567

    
568
	if $bitsEZ != "yes" goto BitsurfrSetup1
569
	set $modemCmd "@P2=230400"
570
	set $newBaudrate "230400"
571
	call SetBaudrate
572

    
573
# Check software revision and ISDN settings
574

    
575
BitsurfrSetup1:
576
	if $optimize == "yes" goto BitsurfrSetup2
577
	if $bitsEZ != "yes" call BitsurfrCheckRev
578
	call BitsurfrCheckConfig
579

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

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

    
593
# BS PRO EZ changes
594

    
595
	if $bitsEZ == "yes" set $authCmd "${authCmd}@M20=\"\""
596
	set $modemCmd "$authCmd$bondCmd$bearCmd$voiceCmd"
597
	call ModemCmd2
598
	if $TA_NoDoubleTelno == "yes" return
599
	if $TA_Bonding == "yes" set $ModTelephone "${Telephone}&${Telephone}"
600
	return
601

    
602
##
603
## What to do when NO DIALTONE
604
##
605

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

    
610
##
611
## BitsurfrCheckConfig
612
##
613
## Verify and adjust ISDN configuration as necessary
614
##
615

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

    
620
# Check switch type
621

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

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

    
644
# Check directory numbers
645

    
646
	set $checkCmd "*1!N1"
647
	set $checkValue $TA_Dirno1
648
	set $checkValueNew $TA_Dirno1
649
	set $checkMsg "Reprogramming voice line #1 directory number..."
650
	call ModemCheckValue
651

    
652
	set $checkCmd "*2!N1"
653
	set $checkValue $TA_Dirno2
654
	set $checkValueNew $TA_Dirno2
655
	set $checkMsg "Reprogramming voice line #2 directory number..."
656
	call ModemCheckValue
657
	set $checkCmd "!N1"
658
	set $checkMsg "Reprogramming data line directory number..."
659
	call ModemCheckValue
660

    
661
# Check SPIDs
662

    
663
	set $checkCmd "*1!C6"
664
	set $checkValue $TA_SPID1
665
	set $checkValueNew $TA_SPID1
666
	set $checkMsg "Reprogramming voice line #1 SPID..."
667
	call ModemCheckValue
668

    
669
	set $checkCmd "*2!C6"
670
	set $checkValue $TA_SPID2
671
	set $checkValueNew $TA_SPID2
672
	set $checkMsg "Reprogramming voice line #2 SPID..."
673
	call ModemCheckValue
674
	set $checkCmd "!C6"
675
	set $checkMsg "Reprogramming data line SPID..."
676
	call ModemCheckValue
677

    
678
# Restart if necessary
679

    
680
	if $valueChanged == "no" return
681
	log "Restarting BitSURFR Pro with new configuration..."
682
	set $modemCmd ">W"
683
	call ModemCmd2
684
	set $modemCmd ">Z"
685
	call ModemCmd2
686
	failure
687

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

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

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

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

    
746
# Examine the CID from each port
747
BitsurfrDetectDone:
748
	set $cid $dataCID
749
	call BitsurfrDetectTryCID
750
	set $cid $voice1CID
751
	call BitsurfrDetectTryCID
752
	set $cid $voice2CID
753
	call BitsurfrDetectTryCID
754
	goto RingbackDetectNoCID
755

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

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

    
780
#################################################################
781
#
782
#	3COM IMPACT IQ SETUP
783
#
784
#################################################################
785

    
786
ImpactIQSetup:
787
	set $noDialToneSubr ImpactIQNoDialtone
788
	set $modemCmd "Z&F%C2E0"
789
	call ModemCmd2
790

    
791
# Set to 230K baud if we support it
792

    
793
	set $modemCmd "$$B230400"
794
	set $newBaudrate "230400"
795
	call SetBaudrate
796

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

    
799
ImpactIQSetup1:
800
	if $optimize == "yes" goto IQInitSetup2
801
	call ImpactIQCheckConfig
802
	call ImpactIQCheckLink
803

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

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

    
817
##
818
## What to do when NO DIALTONE
819
##
820

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

    
825
##
826
## ImpactIQCheckLink
827
##
828
## Check the link status registers
829
##
830

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

    
850
ImpactIQCheckInit:
851
	set $impactInitReg "57"
852
	call IQInitCheck
853
	if $TA_Bonding == "yes" return
854
	set $impactInitReg "58"
855
	call IQInitCheck
856
	return
857

    
858
# A little subroutine
859

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

    
879
##
880
## ImpactIQCheckConfig
881
##
882
## Verify and adjust ISDN configuration as necessary
883
##
884

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

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

    
896
	if $TA_NewSwitch != "yes" goto ImpactIQCheckDirnos
897
	log "Initiating switch type auto-detect..."
898
	set $modemCmd "S50=0"
899
	call ModemCmd2
900
	set $TA_NewSwitch ""
901
	set $valueChanged "yes"
902
	set $iq_new_switch "yes"
903

    
904
# Check directory numbers
905

    
906
ImpactIQCheckDirnos:
907
	set $checkCmd "S51"
908
	set $checkValue $TA_Dirno1
909
	set $checkValueNew $TA_Dirno1
910
	set $checkMsg "Reprogramming line #1 directory number..."
911
	call ModemCheckValue
912

    
913
	set $checkCmd "S53"
914
	set $checkValue $TA_Dirno2
915
	set $checkValueNew $TA_Dirno2
916
	set $checkMsg "Reprogramming line #2 directory number..."
917
	call ModemCheckValue
918

    
919
# Check SPIDs
920

    
921
	set $checkCmd "S52"
922
	set $checkValue $TA_SPID1
923
	set $checkValueNew $TA_SPID1
924
	set $checkMsg "Reprogramming line #1 SPID..."
925
	call ModemCheckValue
926

    
927
	set $checkCmd "S54"
928
	set $checkValue $TA_SPID2
929
	set $checkValueNew $TA_SPID2
930
	set $checkMsg "Reprogramming line #2 SPID..."
931
	call ModemCheckValue
932

    
933
# Check 56K/64K setting
934

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

    
944
# Check Multi-link setting
945

    
946
	set $checkValue "000"
947
	set $checkValueNew "0"
948
	if $TA_Bonding == "yes" set $checkValue "001"
949
	if $TA_Bonding == "yes" set $checkValueNew "1"
950
	set $checkCmd "S80"
951
	set $checkMsg "Reprogramming mutli-link PPP setting..."
952
	call ModemCheckValue
953

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

    
956
	if $valueChanged == "no" return
957
	log "Restarting ISDN modem with the new configuration..."
958
	set $modemCmd "Z"
959
	call ModemCmd2
960
	failure
961

    
962
##
963
## Detect an incoming call with the 3Com
964
##
965

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

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

    
999
# Look for a valid CID string
1000
ImpactIQDetectTryCID:
1001
	if $cid match " *" return
1002
	if $cid match " *N/A" return
1003
	goto RingbackDetectCID
1004

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

    
1030
#################################################################
1031
#
1032
#	NEC ATERM SETUP
1033
#
1034
#################################################################
1035

    
1036
AtermSetup:
1037
	if $AtermHardReset == "yes" goto AtermSetup2
1038

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

    
1041
	log "Initializing modem..."
1042
	print "ATZ98\r\n"
1043
	wait 6
1044
	set $AtermHardReset "yes"
1045
	failure
1046

    
1047
AtermSetup2:
1048

    
1049
# Set to 230K baud if we support it
1050

    
1051
	set $modemCmd ""
1052
	set $newBaudrate "230400"
1053
	call SetBaudrate
1054

    
1055
# Is this an IT55 or IT65?
1056

    
1057
	set $atermIT65 ""
1058
	set $modemCmd "$$N14=0"
1059
	call ModemCmd
1060
	if $modemCmdResult == "OK" set $atermIT65 "yes"
1061

    
1062
# Set normal stuff, PPP mode
1063

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

    
1067
# Set the multi-link setting
1068

    
1069
	set $bondCmd "$$N11=0"
1070
	if $TA_Bonding == "yes" set $bondCmd "$$N11=1"
1071
	set $modemCmd "$bondCmd"
1072
	call ModemCmd2
1073

    
1074
# Additional settings for the IT65 go here...
1075

    
1076
	if $atermIT65 != "yes" return
1077
	return
1078

    
1079
#################################################################
1080
#
1081
#	MICROCOM DESKPORTE SETUP
1082
#
1083
#################################################################
1084

    
1085
DeskPorteSetup:
1086
	set $noDialToneSubr GenericNoDialtone
1087
	set $temp "M1"
1088
	if $SpeakerOff == "yes" set $temp "M0"
1089
	set $modemCmd "Z&F&C1&D2X3E0S0=0${temp}"
1090
	call ModemCmd2
1091
	return
1092

    
1093
#################################################################
1094
#
1095
#	KOREAN TERMINAL ADAPTER SETUP
1096
#
1097
#################################################################
1098

    
1099
NTKSetup:
1100
	set $noDialToneSubr GenericNoDialtone
1101
	set $modemCmd "ZB40&J3"
1102
	call ModemCmd2
1103
	return
1104

    
1105
#################################################################
1106
#
1107
#	ADTRAN EXPRESS XRT SETUP
1108
#
1109
#################################################################
1110

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

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

    
1126
	if $optimize == "yes" return
1127
	call AdtranXRTCheckConfig
1128
	call AdtranXRTCheckLink
1129
	return
1130

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

    
1139
AdtranXRTDialError:
1140
	call AdtranXRTDiagnose
1141
	failure
1142

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

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

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

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

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

    
1176
##
1177
## AdtranXRTCheckLink
1178
##
1179
## Check the link status registers
1180
##
1181

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

    
1206
##
1207
## AdtranXRTCheckConfig
1208
##
1209
## Verify and adjust ISDN configuration as necessary
1210
##
1211

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

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

    
1226
# Check directory numbers
1227

    
1228
AdtranXRTCheckDirnos:
1229
	set $checkCmd "SS62"
1230
	set $checkValue $TA_Dirno1
1231
	set $checkValueNew $TA_Dirno1
1232
	set $checkMsg "Reprogramming line #1 directory number..."
1233
	call ModemCheckValue
1234

    
1235
	set $checkCmd "SS63"
1236
	set $checkValue $TA_Dirno2
1237
	set $checkValueNew $TA_Dirno2
1238
	set $checkMsg "Reprogramming line #2 directory number..."
1239
	call ModemCheckValue
1240

    
1241
# Check SPIDs
1242

    
1243
	set $checkCmd "SS60"
1244
	set $checkValue $TA_SPID1
1245
	set $checkValueNew $TA_SPID1
1246
	set $checkMsg "Reprogramming line #1 SPID..."
1247
	call ModemCheckValue
1248

    
1249
	set $checkCmd "SS61"
1250
	set $checkValue $TA_SPID2
1251
	set $checkValueNew $TA_SPID2
1252
	set $checkMsg "Reprogramming line #2 SPID..."
1253
	call ModemCheckValue
1254

    
1255
# Restart if necessary
1256

    
1257
	if $valueChanged == "no" return
1258
	log "Restarting ISDN modem with the new configuration..."
1259
	set $modemCmd "&W"
1260
	call ModemCmd2
1261
	set $modemCmd "Z"
1262
	call ModemCmd2
1263
	failure
1264

    
1265
#################################################################
1266
#
1267
#	BASIC MODEM STUFF
1268
#
1269
#################################################################
1270

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

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

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

    
1314
ModemFind:
1315

    
1316
# Do a quick check first...
1317

    
1318
	set $ErrorMsg ""
1319
	call ModemFind4
1320
	if $modemCmdResult == "OK" return
1321
	if $Serial230K != "yes" goto ModemFind1
1322
	set $newBaudrate 230400
1323
	if $Baudrate == "230400" set $newBaudrate 115200
1324
	set $Baudrate $newBaudrate
1325
	call ModemFind4
1326
	if $modemCmdResult == "OK" return
1327

    
1328
# Now try possible baud rates more extensively
1329

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

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

    
1344
ModemFind2:
1345
	call ModemFind3
1346
	if $modemCmdResult == "OK" return
1347
	call SendTriplePlus
1348
	call ModemFind3
1349
	return
1350

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

    
1353
ModemFind3:
1354
	set $modemCmd ""
1355
	set $modemCmdTimeout 1
1356
	call ModemCmd0
1357
	if $modemCmdResult == "OK" return
1358
	set $modemCmd "Z"
1359
	set $modemCmdTimeout 2
1360
	call ModemCmd0
1361
	if $modemCmdResult == "OK" return
1362
ModemFind4:
1363
	set $modemCmd ""
1364
	set $modemCmdTimeout 1
1365
	call ModemCmd0
1366
	return
1367

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

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

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

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

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

    
1426
ModemCmd:
1427
	set $modemCmdTimeout 3
1428
ModemCmd0:
1429
	print "AT$modemCmd\r\n"
1430
	match "OK\r\n" ModemCmdOk
1431
	match "ERR" ModemCmdErr
1432
	wait $modemCmdTimeout
1433
	set $modemCmdResult "TIMEOUT"
1434
ModemCmdTimeout:
1435
	set $ErrorMsg "The modem is not responding."
1436
	return
1437
ModemCmdOk:
1438
	set $modemCmdResult "OK"
1439
	return
1440
ModemCmdErr:
1441
	set $modemCmdResult "ERROR"
1442
	set $ErrorMsg "The modem did not accept the command AT$modemCmd."
1443
	return
1444

    
1445
##
1446
## ModemCmd2
1447
##
1448
## Do the modem command in $modemCmd. If we don't get OK back,
1449
## fail the script and log an error message.
1450
##
1451

    
1452
ModemCmd2:
1453
	call ModemCmd
1454
	if $modemCmdResult == "OK" return
1455
	log $ErrorMsg
1456
	failure
1457

    
1458
##
1459
## ModemCmdSend
1460
##
1461
## Send a modem command and read the echo'ed CR-LF
1462
##
1463

    
1464
ModemCmdSend:
1465
	print "AT$modemCmd\r\n"
1466
	match "\r\n" ModemCmdSend2
1467
	wait 3
1468
	goto ModemCmdTimeout
1469
ModemCmdSend2:
1470
	return
1471

    
1472
##
1473
## ModemQuery
1474
##
1475
## Do the $modemCmd and expect exactly one line of response, then OK.
1476
## Return the response in $modemQuery. If anything goes wrong, die.
1477
##
1478

    
1479
ModemQuery:
1480
	call ModemCmdSend
1481
	call ReadLine
1482
	set $modemQuery $matchedString
1483
	if $modemQuery == "ERR" goto ModemQueryError
1484
	if $modemQuery == "ERROR" goto ModemQueryError
1485
	goto GetOK
1486
ModemQueryError:
1487
	call ModemCmdErr
1488
	failure
1489

    
1490
# Same thing, but strip leading and trailing blanks
1491

    
1492
ModemQueryStrip:
1493
	call ModemQuery
1494
	if $modemQuery match " *(.*) *" nop
1495
	set $modemQuery $matchedString1
1496
	return
1497

    
1498
##
1499
## ReadLine
1500
##
1501
## Read the next line of output. It should come within $modemCmdTimeout
1502
## seconds, or else we fail. Return it in $matchedString.
1503
##
1504

    
1505
ReadLine:
1506
	regex "^.*$" ReadLineDone
1507
	wait $modemCmdTimeout
1508
	set $ErrorMsg "The modem is not responding."
1509
	log $ErrorMsg
1510
	failure
1511
ReadLineDone:
1512
	return
1513

    
1514
##
1515
## Get an OK within 3 seconds or die
1516
##
1517

    
1518
GetOK:
1519
	match "OK\r\n" GotOK
1520
	wait 3
1521
	log "The modem is not responding."
1522
	failure
1523
GotOK:
1524
	return
1525

    
1526
##
1527
## Send "+++" to get modem attention if on-line
1528
##
1529

    
1530
SendTriplePlus:
1531
	print "\r\n"
1532
	wait 2
1533
	print "+++"
1534
	wait 2
1535
	return
1536

    
1537
#################################################################
1538
#
1539
#	LOGIN AUTO-SCRIPTING
1540
#
1541
#################################################################
1542

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

    
1564
AutoLogin:
1565
	log "Initiating auto-login..."
1566

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

    
1569
	timer autoLogin 5 AutoLoginTimeout
1570

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

    
1573
	match autoLogin "\x7e\xff\x03\xc0\x21" AutoLoginFrame
1574
	match autoLogin "\x7e\xff\x7d\x23\xc0\x21\x7d\x21" AutoLoginFrame
1575
	match autoLogin "\x7e\xc0\x21" AutoLoginFrame
1576

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

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

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

    
1589
	match "ogin" AutoLoginPrompt
1590
	match "name" AutoLoginPrompt
1591
	wait 1
1592
	print "\r"
1593
	match "ogin" AutoLoginPrompt
1594
	match "name" AutoLoginPrompt
1595
	wait
1596

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

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

    
1618
LoginAgain:
1619
	set $didLogin "yes"
1620
	goto AutoLoginPrompt
1621

    
1622
# We saw a PPP frame
1623

    
1624
AutoLoginFrame:
1625
	log "Detected PPP frame."
1626
	cancel all
1627
	return
1628

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

    
1631
AutoLoginTimeout:
1632
	log "Auto-login timeout."
1633
	cancel all
1634
	return
1635

    
(7-7/18)