blib/lib/Modem/Vgetty.pm untested

filter: Statementstmt 0.0% 0 / 188 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 342 CC 27 SCAR 28.1
CC
27
Cov
0%
receive
56.0
del_handler
30.0
readnum
30.0
CRAP 16.6
1 #
2 # $Id: Vgetty.pm,v 1.13 1998/09/08 12:22:42 kas Exp $
3 #
4 # Copyright (c) 1998 Jan "Yenya" Kasprzak <kas@fi.muni.cz>. All rights
5 # reserved. This package is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7 #
8  
9 package Modem::Vgetty;
10  
11 use FileHandle;
12 use POSIX;
13 use strict;
14  
15 use Carp;
16  
17 use vars qw($testing $log_file $VERSION);
18  
19 $VERSION='0.03';
20 $testing = 0;
21 $log_file = '/var/log/voicelog';
22  
23 my @event_names = qw(BONG_TONE BUSY_TONE CALL_WAITING DIAL_TONE
24 DATA_CALLING_TONE DATA_OR_FAX_DETECTED FAX_CALLING_TONE
25 HANDSET_ON_HOOK LOOP_BREAK LOOP_POLARITY_CHANGE NO_ANSWER
26 NO_DIAL_TONE NO_VOICE_ENERGY RING_DETECTED RINGBACK_DETECTED
27 RECEIVED_DTMF SILENCE_DETECTED SIT_TONE TDD_DETECTED
28 VOICE_DETECTED UNKNOWN_EVENT);
29  
30  
31  
32 sub new {
33 my ($class, $infd, $outfd, $pid) = @_;
34 my $self = bless {}, $class;
35  
36 $infd ||= $ENV{'VOICE_INPUT'};
37 $outfd ||= $ENV{'VOICE_OUTPUT'};
38 $pid ||= $ENV{'VOICE_PID'};
39  
40  
41 $self->{'IN'} = FileHandle->new_from_fd( $infd, "r" )
42 || return;
43 $self->{'OUT'} = FileHandle->new_from_fd( $outfd, "w" )
44 || return;
45 $self->{'IN'}->autoflush;
46 $self->{'OUT'}->autoflush;
47  
48 $self->{'PIPE_BUF_LEN'} = POSIX::_POSIX_PIPE_BUF ;
49  
50 $self->{'PID'} = $pid;
51 $self->{'LOG'} = FileHandle->new();
52  
53 if ($testing > 0) {
54 $self->{'LOG'}->open(">>$log_file") || return undef;
55 $self->{'LOG'}->autoflush;
56 $self->{'LOG'}->print("-----------\n### Pid $$ opening log\n----------\n");
57 }
58 $self->{'EVENTS'} = { map { $_ => {} } @event_names };
59  
60 $self->init();
61  
62 return $self;
63  
64 }
65  
66  
67 # The basic two functions (a low-level interface);
68 sub receive {
69 my $self = shift;
70 my $input;
71 while(1) {
72 $input = $self->{IN}->getline;
73 chomp $input;
74 $self->{LOG}->print("received: $input\n") if $testing > 0;
75 last unless defined $self->{EVENTS}->{$input};
76 # Handle the event:
77 my $dtmf = '';
78 if ($input eq 'RECEIVED_DTMF') {
79 $dtmf = $self->{IN}->getline;
80 chomp $dtmf;
81 $self->{LOG}->print("DTMF $dtmf\n") if $testing > 0;
82 }
83 for (keys %{$self->{EVENTS}->{$input}}) {
84 $self->{LOG}->print("Running handler $_ for event $input\n") if $testing > 0;
85 &{$self->{EVENTS}->{$input}->{$_}}($self, $input, $dtmf);
86 $self->{LOG}->print("Handler $_ for event $input finished.\n") if $testing > 0;
87 }
88 }
89 $input;
90 }
91  
92 sub send {
93 my $self = shift;
94 my $output = shift;
95 $self->{OUT}->print("$output\n");
96 kill PIPE => $self->{PID};
97 $self->{LOG}->print("sent: $output\n") if $testing > 0;
98 }
99  
100 sub expect {
101 my $self = shift;
102 $self->{LOG}->print("expecting: ", (join '|', @_), "\n");
103 my $received = $self->receive || return undef;
104 for my $expected (@_) {
105 return $received if $received eq $expected;
106 }
107 return undef;
108 }
109  
110 sub waitfor {
111 my $self = shift;
112 my $string = shift;
113 while ($self->expect($string) ne $string) { }
114 }
115  
116 sub chat {
117 my $self = shift;
118 my @chatscript = @_;
119 my $received = 0;
120 for my $cmd (@chatscript) {
121 $received = 1 ^ $received;
122 next if $cmd eq '';
123 if ($received == 1) {
124 return undef unless $self->expect($cmd);
125  
126 } else {
127 $self->send($cmd);
128 }
129 }
130 return 1;
131 }
132  
133 # Initial chat
134 sub init {
135 my $self = shift;
136 # $self->chat ('HELLO SHELL', 'HELLO VOICE PROGRAM', 'READY');
137 $self->chat ('HELLO SHELL', 'HELLO VOICE PROGRAM');
138  
139 return $self;
140 }
141  
142 # Setting the voice device
143 sub device {
144 my $self = shift;
145 my $dev = shift;
146 $self->{LOG}->print("attempting to set device $dev") if $testing;
147 $self->chat ('', "DEVICE $dev", 'READY') || return undef;
148 $self->{DEVICE}=$dev;
149 $self->{LOG}->print("sucessfully set device $dev") if $testing;
150 }
151  
152 sub shutdown {
153 my $self = shift;
154 $self->chat ('', 'GOODBYE', 'GOODBYE SHELL');
155 $self->{IN}->close;
156 $self->{OUT}->close;
157 $self->{LOG}->close if $testing > 0;
158 }
159  
160 sub DESTROY {
161 my $self = shift;
162 $self->shutdown;
163 }
164  
165 sub enable_events {
166 my $self = shift;
167 $self->chat ('', 'ENABLE EVENTS', 'READY');
168 }
169  
170 sub disable_events {
171 my $self = shift;
172 $self->chat ('', 'DISABLE EVENTS', 'READY');
173 }
174  
175 sub beep {
176 my $self = shift;
177 my $freq = shift;
178 my $len = shift;
179 $self->chat ('', "BEEP $freq $len", 'BEEPING');
180 }
181  
182 sub dial {
183 my $self = shift;
184 my $num = shift;
185 $self->chat ('', "DIAL $num", 'DIALING');
186 }
187  
188 sub getty {
189 my $self = shift;
190 $self->chat ('', 'GET TTY') || return undef;
191 my $id = $self->receive;
192 $self->expect ('READY') || return undef;
193 return $id;
194 }
195  
196 sub modem_type {
197 # To be implemented in vgetty first.
198 return undef;
199 }
200  
201 sub autostop {
202 my $self = shift;
203 my $arg = shift;
204 $self->chat ('', "AUTOSTOP $arg", 'READY');
205 }
206  
207 sub play {
208 my $self = shift;
209 my $file = shift;
210 $self->chat ('', "PLAY $file", 'PLAYING');
211 }
212  
213 sub record {
214 my $self = shift;
215 my $file = shift;
216 $self->chat ('', "RECORD $file", 'RECORDING');
217 }
218  
219 sub wait {
220 my $self = shift;
221 my $sec = shift;
222 $self->chat ('', "WAIT $sec", 'WAITING');
223 }
224  
225 sub stop {
226 my $self = shift;
227 $self->send ('STOP'); # Nechceme READY.
228 }
229  
230 sub add_handler {
231 my $self = shift;
232 my $event = shift;
233 my $name = shift;
234 my $func = shift;
235 if (!defined($self->{EVENTS}->{$event})) {
236 $self->{LOG}->print("add_handler: unknown event $event\n")
237 if $testing > 0;
238 return undef;
239 }
240 $self->{EVENTS}->{$event}->{$name} = $func;
241 }
242  
243 sub del_handler {
244 my $self = shift;
245 my $event = shift;
246 my $name = shift;
247 if (!defined($self->{EVENTS}->{$event})) {
248 $self->{LOG}->print("del_handler: unknown event $event\n")
249 if $testing > 0;
250 return undef;
251 }
252 if (!defined($self->{EVENTS}->{$event}->{$name})) {
253 $self->{LOG}->print("del_handler: trying to delete nonexistent handler $name\n")
254 if $testing > 0;
255 } else {
256 delete $self->{EVENTS}->{$event}->{$name};
257 }
258 }
259  
260 sub play_and_wait {
261 my $self = shift;
262 my $file = shift;
263 $self->play($file);
264 $self->waitfor('READY');
265 }
266  
267 #####################################################################
268 # The readnum routine, its private variables and the event handler. #
269 #####################################################################
270  
271 my $_readnum_number = ''; # The number itself. Filled in by the event handler.
272 my $_readnum_timeout = 10; # The value of the timeout. Fileld in by readnum.
273 my $_readnum_in_timeout = 1; # 'READY' from timeout or from the '#' key?
274  
275 # Event handler. Just adds key to the $_readnum_number.
276 sub _readnum_event {
277 my $self = shift;
278 my $input = shift; # Unused. Should be 'RECEIVED_DTMF'.
279 my $dtmf = shift;
280  
281 if ($dtmf eq '#') { # Stop the reading now.
282 $_readnum_in_timeout = 0;
283 $self->stop;
284 $self->{LOG}->print("_readnum_event(): Got #; stopping\n");
285 return;
286 }
287 $_readnum_number .= $dtmf;
288 $self->stop;
289 $self->expect('READY');
290 # Restart the wait again.
291 $_readnum_in_timeout = 1;
292 $self->wait($_readnum_timeout);
293 }
294  
295 sub readnum {
296 my $self = shift;
297 my $message = shift;
298 my $timeout = shift;
299 my $times = shift;
300 $_readnum_number = '';
301 $_readnum_in_timeout = 1;
302 $_readnum_timeout = $timeout if $timeout != 0;
303 $times = 3 if $times == 0;
304  
305 # Install the handler.
306 $self->add_handler('RECEIVED_DTMF', 'readnum', \&_readnum_event);
307 while($_readnum_in_timeout != 0 && $_readnum_number eq ''
308 && $times-- > 0) {
309 $self->play_and_wait($message);
310 last if $_readnum_in_timeout == 0;
311 while ($_readnum_in_timeout != 0) {
312 $self->wait($_readnum_timeout);
313 $self->expect('READY');
314 }
315 }
316 return undef if $times < 0;
317 $self->del_handler('RECEIVED_DTMF', 'readnum');
318 $self->stop;
319 $self->expect('READY');
320 $_readnum_number;
321 }
322  
323 1;
324  
325 __END__