1 package FS::part_export::grandstream;
3 use base 'FS::part_export';
4 use vars qw($DEBUG $me %info $GAPSLITE_HOME $JAVA_HOME);
9 use FS::CGI qw(rooturl);
13 $me = '[' . __PACKAGE__ . ']';
14 $GAPSLITE_HOME = '/usr/local/src/GS_CFG_GEN/';
16 my @java = qw( /usr/lib/jvm/default-java/ /usr/java/default/
17 /usr/lib/jvm/java-6-sun/
18 /usr/lib/jvm/java-1.4.2-gcj-4.1-1.4.2.0/
19 ); #add more common places distros and people put their JREs
21 $JAVA_HOME = (grep { -e $_ } @java)[0];
23 tie my %options, 'Tie::IxHash',
24 'upload' => { label=>'Enable upload to TFTP server via SSH',
27 'user' => { label=>'User name for SSH to TFTP server' },
28 'tftproot' => { label=>'Directory in which to upload configuration' },
29 'java_home' => { label=>'Path to java to be used',
32 'gapslite_home' => { label=>'Path to grandstream configuration tool',
33 default=>$GAPSLITE_HOME,
35 'template' => { label=>'Configuration template',
37 notes=>'Type or paste the configuration template here',
42 'svc' => [ qw( part_device ) ], # svc_phone
43 'desc' => 'Provision phone numbers to Grandstream Networks phones/ATAs',
44 'options' => \%options,
45 'notes' => 'Provision phone numbers to Grandstream Networks phones/ATAs. Requires a Java runtime environment and the Grandstream configuration tool to be installed.',
48 sub rebless { shift; }
50 sub gs_create_config {
51 my($self, $mac, %opt) = (@_);
56 warn "gs_create_config called with mac of $mac\n" if $DEBUG;
57 $mac = sprintf('%012s', lc($mac));
58 my $dir = '%%%FREESIDE_CONF%%%/cache.'. $FS::UID::datasrc;
60 my $fh = new File::Temp(
61 TEMPLATE => "grandstream.$mac.XXXXXXXX",
66 my $filename = $fh->filename;
68 #my $template = new Text::Template (
70 # SOURCE => $self->option('template'),
71 # DELIMITERS => $delimiters,
75 #$template->compile or die "Can't compile template: $Text::Template::ERROR\n";
77 #my $config = $template->fill_in( HASH => { mac_addr => $mac } );
79 print $fh $self->option('template') or die "print failed: $!";
82 #system( "export GAPSLITE_HOME=$GAPSLITE_HOME; export JAVA_HOME=$JAVA_HOME; ".
83 # "cd $dir; $GAPSLITE_HOME/bin/encode.sh $mac $filename $dir/cfg$mac"
85 # or die "grandstream encode failed: $!";
87 my @cmd = ( "$JAVA_HOME/bin/java",
88 '-classpath', "$GAPSLITE_HOME/lib/gapslite.jar:$GAPSLITE_HOME/lib/bcprov-jdk14-124.jar:$GAPSLITE_HOME/config",
89 'com.grandstream.cmd.TextEncoder',
90 $mac, $filename, "$dir/cfg$mac",
92 run \@cmd, '>&', \$out_and_err
93 or die "grandstream encode failed: $out_and_err";
97 open my $encoded, "$dir/cfg$mac" or die "open cfg$mac failed: $!";
102 if ($self->option('upload')) {
103 my $scp = new Net::SCP ( {
104 'host' => $self->machine,
105 'user' => $self->option('user'),
106 'cwd' => $self->option('tftproot'),
109 $scp->put( "$dir/cfg$mac" ) or die "upload failed: ". $scp->errstr;
113 $content = <$encoded>;
117 unlink "$dir/cfg$mac";
123 my($self, $mac) = (shift, shift);
125 return unless $mac; # be more alarmed? Or check upstream?
127 $self->gs_create_config($mac, 'upload' => 1);
132 my($self, $mac) = (shift, shift);
134 $mac = sprintf('%012s', lc($mac));
136 ssh_cmd( user => $self->option('user'),
137 host => $self->machine,
139 args => [ '-f', $self->option('tftproot'). "/cfg$mac" ],
145 sub ssh_cmd { #subroutine, not method
147 &Net::SSH::ssh_cmd( { @_ } );
151 # my( $self, $svc_phone ) = (shift, shift);
152 # $self->gs_create($svc_phone->mac_addr);
156 sub _export_replace {
157 # my( $self, $new_svc, $old_svc ) = (shift, shift, shift);
158 # $self->gs_delete($old_svc->mac_addr);
159 # $self->gs_create($new_svc->mac_addr);
164 # my( $self, $svc_phone ) = (shift, shift);
165 # $self->gs_delete($svc_phone->mac_addr);
169 sub _export_suspend {
173 sub _export_unsuspend {
177 sub export_device_insert {
178 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
179 $self->gs_create($phone_device->mac_addr);
183 sub export_device_delete {
184 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
185 $self->gs_delete($phone_device->mac_addr);
189 sub export_device_config {
190 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
193 # if ($phone_device) {
194 $mac = $phone_device->mac_addr;
196 # $mac = $svc_phone->mac_addr;
199 return '' unless $mac; # be more alarmed? Or check upstream?
201 $self->gs_create_config($mac);
205 sub export_device_replace {
206 my( $self, $svc_phone, $new_svc_or_device, $old_svc_or_device ) =
207 (shift, shift, shift, shift);
209 $self->gs_delete($old_svc_or_device->mac_addr);
210 $self->gs_create($new_svc_or_device->mac_addr);
216 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
218 return; # remove if we actually support being an export for svc_phone;
220 my @deviceparts = map { $_->devicepart } $self->export_device;
221 my @devices = grep { my $part = $_->devicepart;
222 scalar( grep { $_ == $part } @deviceparts );
223 } $svc_phone->phone_device;
225 my $export = $self->exportnum;
226 my $fsurl = rooturl();
228 foreach my $device ( @devices ) {
229 next unless $device->mac_addr;
230 my $num = $device->devicenum;
232 qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;devicenum=$num">!.
233 qq! Phone config </A>!;
235 } elsif ($svc_phone->mac_addr) {
236 my $num = $svc_phone->svcnum;
238 qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;svcnum=$num">!.
239 qq! Phone config </A>!;
244 sub export_device_links {
245 my($self, $svc_phone, $device, $arrayref) = (shift, shift, shift, shift);
246 warn "export_device_links $self $svc_phone $device $arrayref\n" if $DEBUG;
247 return unless $device && $device->mac_addr;
248 my $export = $self->exportnum;
249 my $fsurl = rooturl();
250 my $num = $device->devicenum;
252 qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;devicenum=$num">!.
253 qq! Phone config </A>!;