File Coverage

blib/lib/Buffalo/G54.pm
Criterion Covered Total %
statement 15 134 11.1
branch 0 36 0.0
condition n/a
subroutine 5 20 25.0
pod 6 13 46.1
total 26 203 12.8


line stmt bran cond sub pod time code
1             ###########################################
2             package WWW::Mechanize::Retry;
3             ###########################################
4 1     1   85961 use Log::Log4perl qw(:easy);
  1         3  
  1         5  
5 1     1   474 use base 'WWW::Mechanize';
  1         2  
  1         1123  
6              
7             ###########################################
8             sub new {
9             ###########################################
10 0     0     my($class, %options) = @_;
11              
12 0           my $self = __PACKAGE__->SUPER::new();
13              
14             # Defaults
15 0           $self->{__PACKAGE__}->{nof_retries} = 5;
16 0           $self->{__PACKAGE__}->{sleep_between_retries} = 2;
17              
18 0           for(keys %options) {
19 0           $self->{__PACKAGE__}->{$_} = $options{$_};
20             }
21              
22             # Rebless
23 0           bless $self, $class;
24             }
25              
26             ###########################################
27             sub get {
28             ###########################################
29 0     0     my($self, $url, @params) = @_;
30              
31 0           for(0..$self->{__PACKAGE__}->{nof_retries}) {
32              
33 0 0         if($_) {
34 0           my $sleep = $self->{__PACKAGE__}->{sleep_between_retries};
35 0           DEBUG "Sleeping $sleep secs";
36 0           sleep $sleep;
37 0           DEBUG "Retrying $url";
38             }
39              
40 0           DEBUG "Fetching URL $url (#$_)";
41              
42 0           my $resp = $self->SUPER::get("$url");
43              
44 0 0         if($resp->is_success()) {
45 0           DEBUG "Success: ", $resp->code(),
46             " content=[", $resp->content(), "]";
47 0           return $resp;
48             }
49            
50 0           WARN "Error: " . $resp->code() . " (" . $resp->message() . " )";
51 0 0         LOGDIE "Unauthorized" if $resp->code() == 401;
52             }
53              
54 0           LOGDIE "Out of retries for ", $url;
55             }
56              
57             ###########################################
58             package Buffalo::G54;
59             ###########################################
60 1     1   462767 use strict;
  1         9  
  1         29  
61 1     1   5 use warnings;
  1         1  
  1         24  
62 1     1   5 use Log::Log4perl qw(:easy);
  1         1  
  1         9  
63              
64             our $VERSION = "0.03";
65              
66             ###########################################
67             sub new {
68             ###########################################
69 0     0 1   my($class) = @_;
70              
71 0           my $self = {
72             defaults => { user => "root",
73             ip => "192.168.0.1",
74             },
75             realm => "BUFFALO WBR2-G54",
76             nof_retries => 5,
77             sleep_between_retries => 2,
78             };
79              
80 0           bless $self, $class;
81             }
82              
83             ###########################################
84             sub connect {
85             ###########################################
86 0     0 1   my($self, %options) = @_;
87              
88 0 0         if($ENV{BUFFALO}) {
89             # For regression tests only
90 0           my ($ip, $user, $password) = split /:/, $ENV{BUFFALO};
91 0           $self->{ip} = $ip;
92 0           $self->{user} = $user;
93 0           $self->{password} = $password;
94             } else {
95 0           for(qw(user ip)) {
96 0           $self->{$_} = def_or($options{$_},
97             $self->{$_},
98             $self->{defaults}->{$_}
99             );
100             }
101 0           $self->{password} = def_or($options{password},
102             $self->{password},
103             ""
104             );
105             }
106              
107 0           $self->{agent} = WWW::Mechanize::Retry->new(
108 0           map { $_ => $self->{$_} } qw(nof_retries sleep_between_retries)
109             );
110              
111 0           DEBUG "Setting credentials for $self->{ip}:80 $self->{user} $self->{realm}";
112              
113 0           $self->{agent}->credentials(
114             "$self->{ip}:80",
115             $self->{realm},
116             $self->{user},
117             $self->{password}
118             );
119              
120 0           $self->{url} = "http://$self->{ip}";
121              
122 0           $self->geturl("/");
123             }
124              
125             ###########################################
126             sub geturl {
127             ###########################################
128 0     0 0   my($self, $relurl) = @_;
129              
130 0           my $resp = $self->{agent}->get($self->{url} . $relurl);
131 0 0         LOGDIE "Failed for fetch $relurl" if $resp->is_error();
132 0           my $content = $resp->content();
133 0           return $content;
134             }
135              
136             ###########################################
137             sub version {
138             ###########################################
139 0     0 1   my($self) = @_;
140              
141 0           my $content = $self->geturl("/advance/ad-admin-system.htm");
142 0 0         if($content =~ /WBR2-G54 Ver.([0-9.]+)/) {
143 0           DEBUG "Found Buffalo Version $1";
144 0           return $1;
145             }
146              
147 0           ERROR "Version not found ($content)";
148 0           return undef;
149             }
150              
151             ###########################################
152             sub def_or {
153             ###########################################
154 0     0 0   my($def, @alts) = @_;
155              
156             # Still waiting for //= ...
157              
158 0           for my $alt ($def, @alts) {
159 0 0         if(defined $alt) {
160 0           return $alt;
161             }
162             }
163            
164 0           return undef;
165             }
166              
167             ###########################################
168             sub wireless {
169             ###########################################
170 0     0 1   my($self, $action) = @_;
171              
172 0 0         if(!defined $action) {
173 0           return $self->is_wireless_on();
174             }
175              
176 0 0         if($action eq "on") {
    0          
177 0           return $self->wireless_on();
178             } elsif($action eq "off") {
179 0           return $self->wireless_off();
180             }
181              
182 0           LOGDIE "Unknown action '$action'";
183             }
184              
185             ###########################################
186             sub is_wireless_on {
187             ###########################################
188 0     0 0   my($self) = @_;
189              
190 0           $self->geturl("/advance/advance-lan-wireless.htm");
191 0           my $agent = $self->{agent};
192 0           $agent->follow_link(n => 3);
193              
194 0           my $content = $agent->content();
195              
196 0 0         if($content =~ /wl_radio" value="1.*?checked/) {
    0          
197 0           DEBUG "wireless is on";
198 0           return 1;
199             } elsif($content =~ /wl_radio" value="0.*?checked/) {
200 0           DEBUG "wireless is off";
201 0           return 0;
202             }
203              
204 0           LOGDIE "Cannot determine wireless state: $content";
205             }
206              
207             ###########################################
208             sub wireless_on {
209             ###########################################
210 0     0 0   my($self) = @_;
211              
212 0           my $agent = $self->{agent};
213              
214 0           $self->geturl("/advance/advance-lan-wireless.htm");
215 0           $agent->follow_link(n => 3);
216 0           $agent->form_number(1);
217 0           $agent->field("wl_radio", "1");
218 0           $agent->submit_form(form_number => "1");
219             }
220              
221             ###########################################
222             sub wireless_off {
223             ###########################################
224 0     0 0   my($self) = @_;
225              
226 0           my $agent = $self->{agent};
227              
228 0           $self->geturl("/advance/advance-lan-wireless.htm");
229              
230 0           $agent->follow_link(n => 3);
231 0           $agent->form_number(1);
232 0           $agent->field("wl_radio", "0");
233 0           $agent->submit_form(form_number => "1");
234             }
235              
236             ###########################################
237             sub lan_proto {
238             ###########################################
239 0     0 0   my($self, $proto) = @_;
240              
241 0           my $agent = $self->{agent};
242              
243 0           $self->geturl("/advance/ad-lan-dhcp.htm");
244 0           my $form = $agent->form_number(1);
245              
246 0 0         if(defined $proto) {
247 0           $agent->field("lan_proto", $proto);
248 0           $agent->submit_form(form_number => "1");
249             } else {
250 0           return $form->find_input("lan_proto")->value();
251             }
252             }
253              
254             ###########################################
255             sub dhcp {
256             ###########################################
257 0     0 1   my($self, $status) = @_;
258              
259 0 0         if(defined $status) {
260 0 0         if($status eq "on") {
    0          
261 0           $self->lan_proto("dhcp");
262             } elsif($status eq "off") {
263 0           $self->lan_proto("static");
264             }
265             }
266              
267 0           my $lan_proto = $self->lan_proto();
268              
269 0 0         if($lan_proto eq "dhcp") {
    0          
270 0           return 1;
271             } elsif ($lan_proto eq "static") {
272 0           return 0;
273             } else {
274 0           LOGDIE "Unknown return lan_proto value";
275             }
276             }
277              
278             ###########################################
279             sub reboot {
280             ###########################################
281 0     0 1   my($self) = @_;
282              
283 0           my $agent = $self->{agent};
284              
285 0           $self->geturl("/advance/ad-admin-init.htm");
286 0           $agent->submit_form(form_number => "1");
287             }
288              
289             ###########################################
290             sub password {
291             ###########################################
292 0     0 0   my($self) = @_;
293              
294 0           system("stty -echo");
295 0           $|++;
296 0           print "Password: ";
297 0           my $password = ;
298 0           system("stty echo");
299 0           chomp $password;
300 0           $self->{password} = $password;
301             }
302              
303             1;
304              
305             __END__