| 1 | package Bosch::RCPPlus::Response; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use XML::LibXML; | ||
| 5 | use Carp qw(croak); | ||
| 6 | |||
| 7 | sub new | ||
| 8 | { | ||
| 9 | my ($proto, $content, $args, $format) = @_; | ||
| 10 | my $class = ref($proto) || $proto; | ||
| 11 | |||
| 12 | my $self = { | ||
| 13 | content => $content, | ||
| 14 | xml => XML::LibXML->load_xml(string => $content), | ||
| 15 | args => $args, | ||
| 16 | format => $format, | ||
| 17 | }; | ||
| 18 | |||
| 19 | bless ($self, $class); | ||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub type | ||
| 24 | { | ||
| 25 | my ($proto) = @_; | ||
| 26 | return $proto->{xml}->findvalue('/rcp/type'); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub error | ||
| 30 | { | ||
| 31 | my ($proto) = @_; | ||
| 32 | |||
| 33 | return $proto->{xml}->findvalue('/rcp/result/error'); | ||
| 34 | } | ||
| 35 | |||
| 36 | # RCPParser.parseXMLAnswer | ||
| 37 | sub result | ||
| 38 | { | ||
| 39 | my ($proto) = @_; | ||
| 40 | my $ret; | ||
| 41 | |||
| 42 | switch: for ($proto->type) { | ||
| 43 | /^F_FLAG$/ && do { | ||
| 44 | $ret = !!int($proto->{xml}->findvalue('/rcp/result/dec')); | ||
| 45 | last; | ||
| 46 | }; | ||
| 47 | |||
| 48 | /^P_UNICODE$/ && do { | ||
| 49 | my $content = $proto->{xml}->findvalue('/rcp/result/str'); | ||
| 50 | my @chars = split ' ', $content; | ||
| 51 | my $text = ''; | ||
| 52 | |||
| 53 | foreach my $char (@chars) { | ||
| 54 | $text = $text . chr(hex($char)) if ($char); | ||
| 55 | } | ||
| 56 | |||
| 57 | $ret = $text; | ||
| 58 | last; | ||
| 59 | }; | ||
| 60 | |||
| 61 | /^P_OCTET$/ && do { | ||
| 62 | my $content = $proto->{xml}->findvalue('/rcp/result/str'); | ||
| 63 | my @chars = split ' ', $content; | ||
| 64 | my @data; | ||
| 65 | |||
| 66 | foreach my $char (@chars) { | ||
| 67 | push @data, hex($char) if ($char); | ||
| 68 | } | ||
| 69 | |||
| 70 | $ret = \@data; | ||
| 71 | last; | ||
| 72 | }; | ||
| 73 | |||
| 74 | /^T_DWORD$/ && do { | ||
| 75 | $ret = int($proto->{xml}->findvalue('/rcp/result/dec')); | ||
| 76 | }; | ||
| 77 | |||
| 78 | croak("Unknown type $_"); | ||
| 79 | last; | ||
| 80 | } | ||
| 81 | |||
| 82 | $ret = $proto->{format}->($ret) if ($proto->{format}); | ||
| 83 | return $ret; | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; |