File Coverage

blib/lib/Win32/Wlan.pm
Criterion Covered Total %
statement 12 40 30.0
branch 0 18 0.0
condition 0 6 0.0
subroutine 4 12 33.3
pod 7 7 100.0
total 23 83 27.7


line stmt bran cond sub pod time code
1             package Win32::Wlan;
2 1     1   677 use strict;
  1         3  
  1         32  
3 1     1   5 use Carp qw(croak);
  1         2  
  1         56  
4 1         132 use Win32::Wlan::API qw<
5             WlanOpenHandle
6             WlanCloseHandle
7             WlanQueryCurrentConnection
8             WlanEnumInterfaces
9             WlanGetAvailableNetworkList
10             $wlan_available
11 1     1   480 >;
  1         2  
12 1     1   7 use vars qw<$VERSION>;
  1         2  
  1         474  
13             $VERSION = '0.07';
14              
15             # Ideally, the handle should be (another) singleton
16             # that fetches and keeps the handle until the application
17             # closes or the last Win32::Wlan object gets destroyed
18              
19             =head1 NAME
20              
21             Win32::Wlan - Query wlan properties
22              
23             =head1 SYNOPSIS
24              
25             require Win32::Wlan;
26             my $wlan = Win32::Wlan->new;
27             if ($wlan->available) {
28             print "Connected to ", $wlan->connection->{profile_name},"\n";
29             print "I see the following networks\n";
30             for ($wlan->visible_networks) {
31             printf "%s\t-%d dbm\n", $_->{name}, $_->{signal_quality};
32             };
33              
34             } else {
35             print "No Wlan detected (or switched off)\n";
36             };
37              
38             =head1 METHODS
39              
40             =head2 C<< Win32::Wlan->new( %args ) >>
41              
42             my $wlan = Win32::Wlan->new();
43              
44             Creates a new Win32::Wlan object.
45              
46             =over 4
47              
48             =item *
49              
50             C - optional argument to force detection of general Wlan availability
51              
52             =item *
53              
54             C - optional argument to give an existing Wlan handle to the object
55              
56             =item *
57              
58             C - optional argument to give an existing guuid to the object
59              
60             =back
61              
62             =cut
63              
64             sub new {
65 0     0 1   my ($class,%args) = @_;
66              
67 0 0 0       if ($args{ available } or !exists $args{ available }) {
68 0 0         if (! $args{handle}) {
69 0           $args{handle} = eval { WlanOpenHandle() };
  0            
70 0           $args{available} = $wlan_available;
71             } else {
72             #
73             # User gave a handle, so assume availibility is true
74             #
75 0           $args{available} = 1;
76             }
77 0 0         if ($args{available}) {
78 0 0         if (! $args{ interface }) {
79 0           my @interfaces = WlanEnumInterfaces($args{handle});
80 0 0         if (@interfaces > 1) {
81 0           warn "More than one Wlan interface found. Using first.";
82             }
83 0           $args{interface} = $interfaces[0];
84             }
85             }
86             }
87 0           bless \%args => $class;
88             };
89              
90              
91             sub DESTROY {
92 0     0     my ($self) = @_;
93 0 0 0       if ($self->handle and $self->available) {
94 0           WlanCloseHandle($self->handle);
95             };
96             }
97              
98             =head2 C<< $wlan->handle >>
99              
100             Returns the Windows API handle for the Wlan API.
101              
102             =cut
103              
104 0     0 1   sub handle { $_[0]->{handle} };
105              
106             =head2 C<< $wlan->interface >>
107              
108             print $wlan->interface->{name};
109              
110             Returns a hashref describing the interface. The keys are
111             C for the guuid, C for the human-readable name and
112             C for the status of the interface.
113              
114             =cut
115              
116 0     0 1   sub interface { $_[0]->{interface} };
117              
118             =head2 C<< $wlan->available >>
119              
120             $wlan->available
121             or warn "Wlan API is not available";
122              
123             Returns whether the Wlan API is available. The Wlan API is available
124             on Windows XP SP3 or higher.
125              
126             =cut
127              
128 0     0 1   sub available { $_[0]->{available} };
129              
130             =head2 C<< $wlan->connected >>
131              
132             $wlan->connected
133             or warn "Wlan connection unavailable";
134              
135             Returns whether a Wlan connection is established. No connection is established
136             when Wlan is switched off or no access point is in range.
137              
138             =cut
139              
140             sub connected {
141 0     0 1   my $conn = $_[0]->connection;
142             defined $conn->{profile_name} && $conn->{profile_name}
143 0 0         };
144              
145             =head2 C<< $wlan->connection >>
146              
147             if ($wlan->connected) {
148             print "Connected to ";
149             print $wlan->connection->{profile_name};
150             };
151              
152             Returns information about the current connection in a hashref. The keys
153             are
154              
155             =over 4
156              
157             =item *
158              
159             C - the name of the profile of the current connection
160              
161             =back
162              
163             =cut
164              
165             sub connection {
166 0     0 1   my ($self) = @_;
167 0 0         if ($self->available) {
168 0           return { WlanQueryCurrentConnection( $self->handle, $self->interface->{guuid} ) };
169             };
170             };
171              
172             =head2 C<< $wlan->visible_networks >>
173              
174             Returns information about the currently visible networks as a list of
175             hashrefs.
176              
177             =over 4
178              
179             =item *
180              
181             C - the SSID of the network
182              
183             =item *
184              
185             C - the signal quality ranging linearly from 0 to 100
186             meaning -100 dbm to -50 dbm
187              
188             =back
189              
190             =cut
191              
192             sub visible_networks {
193 0     0 1   my ($self) = @_;
194 0 0         if ($self->available) {
195 0           return WlanGetAvailableNetworkList( $self->handle, $self->interface->{guuid} );
196             };
197             };
198              
199             1;
200              
201             __END__