1 |
#!/usr/bin/perl |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use lib 'lib'; |
7 |
use SOAP::Transport::HTTP +trace => 'debug'; |
8 |
use HTTP::Cookies; |
9 |
use CGI::Simple::Cookie; |
10 |
use Data::Dump qw/dump/; |
11 |
|
12 |
#SOAP::Trace->import( 'all' ); |
13 |
|
14 |
# Eh... |
15 |
$SOAP::Constants::DO_NOT_CHECK_MUSTUNDERSTAND = 1; |
16 |
|
17 |
$SIG{PIPE} = $SIG{INT} = 'IGNORE'; # don't want to die on 'Broken pipe' or Ctrl-C |
18 |
|
19 |
my $cwmp = new CWMP; |
20 |
|
21 |
my $daemon = SOAP::Transport::HTTP::Daemon->new( |
22 |
#LocalAddr => 'localhost', |
23 |
LocalPort => 3333, |
24 |
Reuse => 1, |
25 |
#'Listen' => 128, |
26 |
) |
27 |
->dispatch_with({ |
28 |
'urn:dslforum-org:cwmp-1-0' => $cwmp, |
29 |
'http://schemas.xmlsoap.org/soap/envelope/' => $cwmp, |
30 |
}) |
31 |
; |
32 |
|
33 |
my $cookies = new CGI::Simple::Cookie( |
34 |
-name => 'foo', |
35 |
-value => 'bar', |
36 |
); |
37 |
$daemon->options->{simple_cookie} = $cookies; |
38 |
|
39 |
$daemon->options->{empty_request} = sub { |
40 |
$cwmp->empty_request( @_ ); |
41 |
}; |
42 |
|
43 |
$cwmp->enqueue( qw/GetRPCMethods/ ); |
44 |
|
45 |
print "Contact to ACS server at ", $daemon->url, " queue ", dump( $cwmp->queue ),"\n"; |
46 |
$daemon->handle; |
47 |
|
48 |
package CWMP; |
49 |
use strict; |
50 |
|
51 |
use Data::Dump qw/dump/; |
52 |
use Carp qw/cluck/; |
53 |
|
54 |
sub new { |
55 |
bless {}, shift; |
56 |
} |
57 |
|
58 |
# CPE methods Responding Calling |
59 |
# |
60 |
# GetRPCMethods Required Optional |
61 |
# SetParameterValues Required Required |
62 |
# GetParameterValues Required Required |
63 |
# GetParameterNames Required Required |
64 |
# SetParameterAttributes Required Optional |
65 |
# GetParameterAttributes Required Optional |
66 |
# AddObject Required Optional |
67 |
# DeleteObject Required Optional |
68 |
# Reboot Required Optional |
69 |
# Download Required Required |
70 |
# Upload Optional Optional |
71 |
# FactoryReset Optional Optional |
72 |
# GetQueuedTransfers Optional Optional |
73 |
# ScheduleInform Optional Optional |
74 |
# SetVouchers Optional Optional |
75 |
# GetOptions Optional Optional |
76 |
|
77 |
# Server methods Calling Responding |
78 |
# |
79 |
# GetRPCMethods Optional Required |
80 |
# Inform Required Required |
81 |
# TransferComplete Required Required |
82 |
# RequestDownload Optional Optional |
83 |
# Kicked Optional Optional |
84 |
|
85 |
|
86 |
my $ID; |
87 |
|
88 |
my @queue; |
89 |
|
90 |
sub enqueue { |
91 |
my $self = shift; |
92 |
push @queue, @_; |
93 |
} |
94 |
|
95 |
sub queue { |
96 |
my $self = shift; |
97 |
return @queue; |
98 |
} |
99 |
|
100 |
sub consume_queue { |
101 |
my @results = ( SOAP::Header->name('ID' => $ID)->prefix('cwmp')->mustUnderstand(1) ); |
102 |
|
103 |
|
104 |
my $op = shift @queue; |
105 |
|
106 |
if ( $op ) { |
107 |
push @results, SOAP::Data->name( $op )->prefix('cwmp'); |
108 |
warn "consume_queue $op\n"; |
109 |
}; |
110 |
|
111 |
push @results, SOAP::Header->name('NoMoreRequests' => 1)->prefix('cwmp') unless ( @queue ); |
112 |
|
113 |
warn "## conqume_queue results = ", dump( @results ); |
114 |
|
115 |
return @results; |
116 |
} |
117 |
|
118 |
## SOAP ACS methods |
119 |
|
120 |
sub Inform { |
121 |
my ($self, $DeviceId, $Event, $MaxEnvelopes, $CurrentTime, $RetryCount, $ParameterList, $headers) = @_; |
122 |
|
123 |
my $p; |
124 |
|
125 |
#cluck dump( $daemon ); |
126 |
#warn dump( $headers ); |
127 |
|
128 |
$ID = ( grep { $_->name eq 'ID' } @$headers )[0]->value; |
129 |
|
130 |
foreach ( @$ParameterList ) { |
131 |
$p->{ $_->{Name} } = $_->{Value}; |
132 |
} |
133 |
|
134 |
warn "request $ID from ", dump( $DeviceId ), " with events ",dump( $Event ), "maxEnvelopes: $MaxEnvelopes on $CurrentTime retry $RetryCount params ", dump( $p ); |
135 |
|
136 |
return SOAP::Data->name('MaxEnvelopes' => 1); |
137 |
} |
138 |
|
139 |
sub GetParameterValuesResponse { |
140 |
my $self = shift; |
141 |
|
142 |
warn "GetParameterValuesResponse ",dump( @_ ); |
143 |
|
144 |
return; |
145 |
} |
146 |
|
147 |
sub Fault { |
148 |
my $self = shift; |
149 |
|
150 |
warn "Fault ",dump( @_ ); |
151 |
|
152 |
return; |
153 |
} |
154 |
|
155 |
## special handling of empty POST request from CPE |
156 |
|
157 |
sub empty_request { |
158 |
|
159 |
my (undef, $self) = @_; |
160 |
|
161 |
# warn "empty_request headers = ",dump( $self->request->headers ); |
162 |
|
163 |
$self->serializer->soapversion(SOAP::Lite->soapversion); |
164 |
$self->serializer->xmlschema($SOAP::Constants::DEFAULT_XML_SCHEMA); |
165 |
|
166 |
my @results = ( |
167 |
# SOAP::Header->name('ID' => $ID)->prefix('cwmp')->mustUnderstand(1), |
168 |
# SOAP::Data->name('GetParameterValues' => |
169 |
# SOAP::Data->value( 'ParametarNames' => [ |
170 |
# 'InternetGatewayDevice.ManagementServer.', |
171 |
# 'a.', 'a.b.', 'a.b.c.', |
172 |
# ], |
173 |
# ), |
174 |
# )->prefix('cwmp'), |
175 |
); |
176 |
|
177 |
|
178 |
|
179 |
push @results, ( consume_queue ); |
180 |
|
181 |
my $result = $self->serializer |
182 |
# ->prefix('s') # distinguish generated element names between client and server |
183 |
# ->uri('urn:dslforum-org:cwmp-1-0') |
184 |
->ns('urn:dslforum-org:cwmp-1-0', 'cwmp') |
185 |
->envelope( freeform => @results ); |
186 |
|
187 |
my $xml = $result; |
188 |
$xml =~ s/</\n</gs; |
189 |
|
190 |
warn "empty_request result:\n$xml\n"; |
191 |
|
192 |
# warn $self->response->headers; |
193 |
|
194 |
return $result; |
195 |
} |
196 |
|