lib/Ekahau/Response.pm

filter: Statementstmt 80.8% 118 / 146 Branchbran 61.7% 21 / 34 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 84.6% 22 / 26 Podpod 90.0% 9 / 10 totaltotal 77.8% 176 / 226 CC 23 SCAR 22.5
CC
23
Cov
77%
tostring
42.0
parse
12.0
get_props
6.0
CRAP 9.4
« lib/Ekahau/Server/Test.pm ↑ Summary script/ekahau-example.pl »
1 package Ekahau::Response;
2 15 ▶use Ekahau::Base; our $VERSION=Ekahau::Base::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # Written by Scott Gifford <gifford@umich.edu>
5 # Copyright (C) 2004 The Regents of the University of Michigan.
6 # See the file LICENSE included with the distribution for license
7 # information.
8  
9 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Ekahau::Response - Response from an Ekahau server
16  
17 =head1 SYNOPSIS
18  
19 Provides a straightforward encapsulation of the response objects
20 returned by the Ekahau Positioning Engine. This is the base class for
21 the specific responses; in general you will want to use one of them.
22  
23 =head1 DESCRIPTION
24  
25 This class takes care of parsing responses from the Ekahau server into
26 their individual components.
27  
28 The responses returned by Ekahau are a sort of half-assed XML. They
29 look superficially like XML, but not enough that they can be parsed by
30 an XML parser. Instead, this module takes a simplistic approach to
31 parsing them. The exact rules for parsing aren't clear from the
32 documentation, so this parsing may not be correct in all
33 circumstances, but we have not observed any misparsing with the
34 current code.
35  
36 The response is parsed into the hash reference that comprises this
37 object. It's parsed like this:
38  
39 <#tag cmd args[0] args[1] ...
40 params{key1}=value1
41 params{key2}=value2
42 SEPARATOR1
43 params{SEPERATOR1}[0]{key3}=value3
44 params{SEPERATOR1}[0]{key4}=value4
45 SEPARATOR1
46 params{SEPERATOR1}[1]{key3}=value3
47 params{SEPERATOR1}[1]{key4}=value4
48 >
49  
50 Here are some examples. First,
51  
52 <#LOC LOCATION_ESTIMATE 4
53 accurateX=1427.09
54 accurateY=2141.73
55 accurateTime=1117138297067
56 accurateContextId=22940
57 latestX=1429.14
58 latestY=2140.60
59 latestTime=1117138301067
60 latestContextId=22940
61 speed=0.09101
62 heading=5.78188
63 >
64  
65  
66 parses to:
67  
68 {
69 'tag' => 'LOC',
70 'cmd' => 'LOCATION_ESTIMATE',
71 'args' => [ '4' ],
72 'params' => {
73 'latestX' => '1429.14',
74 'accurateY' => '2141.73',
75 'heading' => '5.78188',
76 'accurateTime' => '1117138297067',
77 'latestY' => '2140.60',
78 'accurateContextId' => '22940',
79 'speed' => '0.09101',
80 'accurateX' => '1427.09',
81 'latestTime' => '1117138301067',
82 'latestContextId' => '22940'
83 },
84 }
85  
86 Second,
87  
88 <#AREA AREA_ESTIMATE 4
89 AREA
90 name=2425
91 probability=1.000
92 contextId=22940
93 polygon=1396;1396;1554;1554;1394;1396&1964;2200;2200;1964;1964;1964;
94 AREA
95 name=2431
96 probability=0.000
97 contextId=22940
98 polygon=1560;1559;1682;1680;1804;1804;1559;1560&1968;2197;2196;2153;2154;1966;1966;1968;
99 >
100  
101 parses to:
102  
103 {
104 'tag' => 'AREA',
105 'cmd' => 'AREA_ESTIMATE',
106 'args' => [ '4' ],
107 'params' => {
108 'AREA' => [
109 {
110 'contextId' => '22940',
111 'probability' => '1.000',
112 'name' => '2425',
113 'polygon' => '1396;1396;1554;1554;1394;1396&1964;2200;2200;1964;1964;1964;'
114 }, {
115 'contextId' => '22940',
116 'probability' => '0.000',
117 'name' => '2431',
118 'polygon' => '1560;1559;1682;1680;1804;1804;1559;1560&1968;2197;2196;2153;2154;1966;1966;1968;'
119 }
120 ]
121 }
122 }
123  
124 Finally, this response:
125  
126 <#2376 DEVICE_LIST
127 1
128 2
129 3
130 >
131  
132 parses to this:
133  
134 {
135 'tag' => '2376',
136 'cmd' => 'DEVICE_LIST',
137 'args' => [],
138 'params' => {
139 '3' => [ {} ],
140 '2' => [ {} ],
141 '1' => [ {} ],
142 }
143 }
144  
145 =cut
146  
147 15 ▶use Ekahau::Response::DeviceList;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 15 ▶use Ekahau::Response::DeviceProperties;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 15 ▶use Ekahau::Response::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 15 ▶use Ekahau::Response::LocationEstimate;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 15 ▶use Ekahau::Response::LocationContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 15 ▶use Ekahau::Response::AreaEstimate;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 15 ▶use Ekahau::Response::AreaList;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 15 ▶use Ekahau::Response::StopLocationTrackOK;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 15 ▶use Ekahau::Response::StopAreaTrackOK;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 15 ▶use Ekahau::Response::MapImage;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 15 ▶use constant RESPONSEBASE => 'Ekahau::Response::';
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 15 our %CMDCLASS = (
161 DEVICE_LIST => RESPONSEBASE.'DeviceList',
162 DEVICE_PROPERTIES => RESPONSEBASE.'DeviceProperties',
163 LOCATION_ESTIMATE => RESPONSEBASE.'LocationEstimate',
164 CONTEXT => RESPONSEBASE.'LocationContext',
165 AREA_ESTIMATE => RESPONSEBASE.'AreaEstimate',
166 STOP_LOCATION_TRACK_OK => RESPONSEBASE.'StopLocationTrackOK',
167 STOP_AREA_TRACK_OK => RESPONSEBASE.'StopAreaTrackOK',
168 AREALIST => RESPONSEBASE.'AreaList',
169 MAP => RESPONSEBASE.'MapImage',
170 );
171 15 ▶use constant ERROR_CLASS => 'Ekahau::Response::Error';
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 =head2 Constructors
174  
175 =head3 new ( %params )
176  
177 Creates a new empty object. The only parameter recognized is C<tag>,
178 which sets the tag property for the response.
179  
180 =cut
181  
182 sub new
183 174 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
184 174 my $class = shift;
185 174 my(%p) = @_;
186 174 my $self = {};
187  
188 174 ▶ if ($p{tag})
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"tag"}) 0 174 174
189 {
190 0 $self->{tag} = $p{tag};
191 }
192 174 bless $self, $class;
193 }
194  
195 # Internal method
196 sub init
197 0 ▶{
Subroutine init: not called CC=1 SCAR=0.0
Pod undocumented
198 # Do nothing
199 }
200  
201 =head3 parsenew ( $response_str )
202  
203 Parse a response string into an object. The results are undefined if
204 C<$response_str> is not a valid Ekahau response.
205  
206 =cut
207  
208 sub parsenew
209 174 ▶{
Subroutine parsenew: called CC=4 SCAR=13.9
Pod documented
210 174 my $class = shift;
211 174 my $self = $class->new();
212 174 $self->parse(@_);
213 warn "parsenew: cmd is '$self->{cmd}'\n"
214 174 ▶ if ($ENV{VERBOSE});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"VERBOSE"} 0 174 174
215 174 ▶ if (my $newclass = $CMDCLASS{$self->{cmd}})
Branch: 2 branches
BranchTrueFalseTotal
if (my $newclass = $Ekahau::Response::CMDCLASS{$self->{'cmd'}}) { } 56 118 174
elsif ($self->{'cmd'} =~ /^(?:MALFORMED_REQUEST|.*_NOT_FOUND|FAILURE|.*_FAILED|.*_PROBLEM)/) { } 15 103 118
216 {
217 56 bless $self,$newclass;
218 56 $self->init;
219 }
220 elsif ($self->{cmd} =~ /^(?:MALFORMED_REQUEST|.*_NOT_FOUND|FAILURE|.*_FAILED|.*_PROBLEM)/)
221 {
222 15 bless $self,ERROR_CLASS;
223 15 $self->init;
224 }
225 174 $self;
226 }
227  
228 =head2 Methods
229  
230 =head3 parse ( $response_str )
231  
232 Populate the fields of an object with the ones in C<$response_str>,
233 overwriting any existing values. The results are undefined if
234 C<$response_str> is not a valid Ekahau response.
235  
236 =cut
237  
238 sub parse
239 174 ▶{
Subroutine parse: called CC=12 SCAR=24.9
Pod documented
240 174 my $self = shift;
241 174 my($r) = @_;
242 174 my $data;
243  
244 174 $r =~ s/^\s+//;
245 174 $r =~ s/\s+$//;
246 174 $self->{string} = $r;
247
248 # Look for a tag
249 174 ▶ if ($r =~ s/^\s*<(\#\w*?\s)?\s*// and $1)
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ s/^\s*<(\#\w*?\s)?\s*// and $1) 109 65 174
Condition66%2/3
$r =~ s/^\s*<(\#\w*?\s)?\s*// and $1
!ll&&!rl&&r
065109
MC/DC50%1/2
$r =~ s/^\s*<(\#\w*?\s)?\s*// and $1
$r =~ s/^\s*<(\#\w*?\s)?\s*// $1
Truth table66%2/3
A and B
A= $r =~ s/^\s*<(\#\w*?\s)?\s*//B= $1
ABresult
0X0
100
111
250 {
251 # Preserve taintedness with substr(X,0,0)
252 109 chop($self->{tag} = substr($1,1).substr($self->{string},0,0))
253 }
254 # Does this contain sized data?
255 174 ▶ if ($r =~ /\x0asize=(\d+).*?\x0adata=/sg)
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /\x0asize=(\d+).*?\x0adata=/gs) 4 170 174
256 {
257 4 my $data_len = $1;
258 4 my $data_pos = pos($r);
259 4 $data = substr($r,$data_pos,$data_len,'');
260 # Remove the "data="
261 4 substr($r,-5,5,'');
262 }
263  
264 # Remove trailing angle bracket and whitespace
265 174 $r =~ s/\s*>\s*$//;
266  
267 # Split the response into lines
268 174 my @lines = split(/(?<!\\)\x0d?\x0a/,$r);
269 # This probably doesn't handle quoting correctly
270 174 my @firstline = split(' ',shift @lines);
271  
272 # The first line
273 174 $self->{cmd} = shift @firstline;
274 174 $self->{args} = [map { s/^\"//; s/\"$//; $_ } @firstline];
275 174 $self->{params}={};
276  
277 # Are there any arguments that are really parameters?
278 174 foreach my $i (0..$#{$self->{args}})
279 {
280 249 ▶ if ($self->{args}[$i] =~ /^(\w+)=(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"args"}[$i] =~ /^(\w+)=(.*)$/) 6 243 249
281 {
282 # Use substr to keep taintedness
283 6 $self->{params}{$1}=$2.substr($self->{args}[$i],0,0);
284 # Remove argument
285 6 splice(@{$self->{args}},$i,1,());
286 }
287 }
288 # Parameters on other lines
289 174 my $datahash = $self->{params};
290 174 foreach my $l (@lines)
291 {
292 385 my $keep_taintedness = substr($l,0,0);
293 385 $l =~ s/\\(<|>|\x0d\x0a)/$1/g;
294 385 ▶ if ($l =~ /^([\w.\-]+?)=(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^([\w.\-]+?)=(.*)$/) { } 336 49 385
295 {
296 # Using this as a hash key will implictly untaint it,
297 # so require that values be "word characters".
298 336 my $val = $2.$keep_taintedness;
299 336 $datahash->{$1}=$val;
300 }
301 else
302 {
303 49 $datahash = {};
304 49 push(@{$self->{params}{$l}},$datahash);
305 }
306 }
307 174 ▶ if (defined($data))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 4 170 174
308 {
309 4 $self->{params}{data}=$data;
310 }
311 # Special case: copy area up to main parameters, for backwards
312 # compatibility.
313 174 ▶ if ($self->{params}{AREA} and $self->{params}{AREA}[0])
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"}{"AREA"} and $self->{"params"}{"AREA"}[0]) 11 163 174
Condition66%2/3
$self->{"params"}{"AREA"} and $self->{"params"}{"AREA"}[0]
!ll&&!rl&&r
163011
MC/DC50%1/2
$self->{"params"}{"AREA"} and $self->{"params"}{"AREA"}[0]
$self->{"params"}{"AREA"} $self->{"params"}{"AREA"}[0]
Truth table66%2/3
A and B
A= $self->{"params"}{"AREA"}B= $self->{"params"}{"AREA"}[0]
ABresult
0X0
100
111
314 {
315 11 ▶ while(my($k,$v)=each(%{$self->{params}{AREA}[0]}))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"params"}{"AREA"}[0];}) 53 11 64
316 {
317 53 $self->{params}{$k} = $v;
318 }
319 }
320
321 174 $self;
322 }
323  
324 =head3 get_props ( @prop_names )
325  
326 Returns a hash containing the values for the list of properties in
327 C<@prop_names>. If C<@prop_names> is empty, all properties will be
328 returned.
329  
330 =cut
331  
332 sub get_props
333 0 ▶{
Subroutine get_props: not called CC=2 SCAR=17.9
Pod documented
334 0 my $self = shift;
335 0 ▶ if (!@_) { @_ = keys %{$self->{params}} };
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
336
337 0 return map { $_ => $self->{params}{$_} } @_;
338 }
339  
340 =head3 get_prop ( $prop_name )
341  
342 Returns the value for one of this object's properties, specified by
343 C<$prop_name>. If no property named C<$prop_name> exists, C<undef> is
344 returned.
345  
346 =cut
347  
348 sub get_prop
349 24 ▶{
Subroutine get_prop: called CC=1 SCAR=0.0
Pod documented
350 24 my $self = shift;
351 24 my $prop = $_[0];
352  
353 24 return $self->{params}{($prop)};
354 }
355  
356  
357 =head3 error ( )
358  
359 Returns true if this response is an L<Ekahau::Response::Error|Ekahau::Response::Error> object,
360 else returns false.
361  
362 =cut
363  
364 sub error
365 59 ▶{
Subroutine error: called CC=1 SCAR=0.0
Pod documented
366 59 0;
367 }
368  
369 =head3 eventname ( )
370  
371 Returns the name of this event in the same format used by
372 L<Ekahau::Events|Ekahau::Events>.
373  
374 =cut
375  
376 sub eventname
377 4 ▶{
Subroutine eventname: called CC=2 SCAR=7.2
Pod documented
378 4 my $self = shift;
379 4 ▶ $self->error ? 'ERROR' : uc $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 4 4
380 }
381  
382 =head3 type ( )
383  
384 Returns the string I<Response>, to identify the type of this object,
385 and that no more specific information is available.
386  
387 =cut
388  
389 sub type
390 0 ▶{
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
391 0 'Response';
392 }
393  
394 =head3 tostring ( )
395  
396 Return a string representation of the object. This is reconstructed
397 from the object's properties, and so may not be identical to the
398 string which was parsed to create it.
399  
400 =cut
401  
402 sub tostring
403 0 ▶{
Subroutine tostring: not called CC=6 SCAR=37.4
Pod documented
404 0 my $self = shift;
405
406 0 my $str = "<";
407 0 ▶ if (defined($self->{tag})) { $str .= "#$self->{tag} " };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"tag"}) 0 0 0
408 0 $str .= $self->{cmd};
409 0 ▶ if (@{$self->{args}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"args"};}) 0 0 0
410 {
411 0 $str .= " ".join(" ",@{$self->{args}});
412 }
413 0 ▶ if ($self->{params})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"}) 0 0 0
414 {
415 0 $str .= "\x0d\x0a";
416
417 0 foreach my $var (keys %{$self->{params}})
418 {
419 0 my $val = $self->{params}{$var};
420 0 ▶ if (defined($val))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
421 {
422 0 $val =~ s/(<|>|\x0d\x0a)/\\$1/g;
423 0 $str .= "$var=$val\x0d\x0a";
424 }
425 else
426 {
427 0 $str .= "$var\x0d\x0a";
428 }
429 }
430 }
431 0 $str .= ">\x0d\x0a";
432 0 return $str;
433 }
434  
435 15 1;
436  
437 =head1 AUTHOR
438  
439 Scott Gifford E<lt>gifford@umich.eduE<gt>, E<lt>sgifford@suspectclass.comE<gt>
440  
441 Copyright (C) 2005 The Regents of the University of Michigan.
442  
443 See the file LICENSE included with the distribution for license
444 information.
445  
446  
447 =head1 SEE ALSO
448  
449 L<Ekahau::Base|Ekahau::Base>, L<Ekahau::Response::DeviceList|Ekahau::Response::DeviceList>,
450 L<Ekahau::Response::DeviceProperties|Ekahau::Response::DeviceProperties>, L<Ekahau::Response::Error|Ekahau::Response::Error>,
451 L<Ekahau::Response::LocationEstimate|Ekahau::Response::LocationEstimate>,
452 L<Ekahau::Response::LocationContext|Ekahau::Response::LocationContext>,
453 L<Ekahau::Response::AreaEstimate|Ekahau::Response::AreaEstimate>, L<Ekahau::Response::AreaList|Ekahau::Response::AreaList>,
454 L<Ekahau::Response::StopLocationTrackOK|Ekahau::Response::StopLocationTrackOK>,
455 L<Ekahau::Response::StopAreaTrackOK|Ekahau::Response::StopAreaTrackOK>, L<Ekahau::Response::MapImage|Ekahau::Response::MapImage>.
456  
457 =cut
458  
459 15 1;
« lib/Ekahau/Server/Test.pm ↑ Summary script/ekahau-example.pl »