Fixed file reading for --ems-melody-file-send
[gsmperl.git] / GSM / examples / commandline / gsmcmd
index 0c2c220..51eb93e 100755 (executable)
@@ -10,11 +10,16 @@ use GSM::SMS::NBS;
 use GSM::SMS::Bitmap;
 use GSM::SMS::NBS::Message;
 use Getopt::Long;
+use Carp;
+use GSM::SMS::NBS::Lib;
 require GSM::SMS::NBS::Alcatel;
 
 $|=1;     # No output buffering
 
 
+my $EMStext="";
+my @EMSudh;
+
 #
 # Arguments
 my $opt_verbose;
@@ -22,9 +27,8 @@ my $opt_transport;
 my $opt_ring_send;
 my $opt_logo_group_send;
 my $opt_ems_force_variable;
-my $opt_ems_picture_send;
-my $opt_ems_animation_send;
-my $opt_ems_melody_file_send;
+my $opt_ems_picture_wrap=1;
+my $opt_ems_animation_wrap=1;
 my $opt_alcatel_name;
 my $opt_alcatel_picture_send;
 my $opt_alcatel_animation_send;
@@ -35,9 +39,12 @@ GetOptions(
        "ring-send:s"                     => \$opt_ring_send,
        "logo-group-send:s"               => \$opt_logo_group_send,
        "ems-force-variable"              => \$opt_ems_force_variable,
-       "ems-picture-send:s"              => \$opt_ems_picture_send,
-       "ems-animation-send:s"            => \$opt_ems_animation_send,
-       "ems-melody-file-send:s"          => \$opt_ems_melody_file_send,
+       "ems-picture-send:s"              => \&ems_picture_send,
+       "ems-picture-wrap!"               => \$opt_ems_picture_wrap,
+       "ems-animation-send:s"            => \&ems_animation_send,
+       "ems-animation-wrap!"             => \$opt_ems_animation_wrap,
+       "ems-melody-file-send:s"          => \&ems_melody_file_send,
+       "ems-text:s"                      => \&ems_text,
        "alcatel-name:s"                  => \$opt_alcatel_name,
        "alcatel-picture-send:s"          => \$opt_alcatel_picture_send,
        "alcatel-animation-send:s"        => \$opt_alcatel_animation_send,
@@ -88,39 +95,8 @@ for $msisdn (@ARGV) {
                $nbs->sendGroupGraphic_file($msisdn, $opt_logo_group_send);
        }
 
-       if ($opt_ems_picture_send) {
-               my $bitmap = GSM::SMS::Bitmap->new($opt_ems_picture_send);
-               $nbs->sendsms($msisdn, "", udh=>[ {
-                               "type"=>"ems_picture",
-                               TEXT_POSITION=>0,
-                               "bitmap"=>$bitmap,
-                               "force_variable"=>$opt_ems_force_variable,
-               } ]);
-       }
-
-       if ($opt_ems_animation_send) {
-               my @names=split /:/,$opt_ems_animation_send;
-               die "Required exactly 4 colon (':') delimited filenames for --ems-animation-send" if 4 != @names;
-               my @bitmaps=map { GSM::SMS::Bitmap->new($_); } @names;
-               $nbs->sendsms($msisdn, "", udh=>[ {
-                               "type"=>"ems_animation",
-                               TEXT_POSITION=>0,
-                               "bitmaps"=>\@bitmaps,
-               } ]);
-       }
-
-       if ($opt_ems_melody_file_send) {
-               local (*F);
-               open F, $opt_ems_melody_file_send;
-               my $ems_melody_file_data = <F>;
-               close F;
-               warn "EMS iMelody file format not recognized, wrapping anyway"
-                               if $ems_melody_file_data!~/^BEGIN:IMELODY\r\n.*END:IMELODY\r\n$/s;
-               $nbs->sendsms($msisdn, "", udh=>[ {
-                               "type"=>"ems_melody",
-                               TEXT_POSITION=>0,
-                               "textdata"=>$ems_melody_file_data,
-               } ]);
+       if (@EMSudh || $EMStext ne "") {
+               $nbs->sendsms($msisdn, $EMStext, udh=>[ @EMSudh ]);
        }
 
        if ($opt_alcatel_picture_send) {
@@ -161,3 +137,92 @@ for $msisdn (@ARGV) {
 }
 
 exit(0);
+
+sub ems_picture_send
+{
+my($keyword,$arg)=@_;
+
+       my $bitmap = GSM::SMS::Bitmap->new($arg);
+       $bitmap->crop(0xFF*8,0xFF);
+       my ($width, $height) = ($bitmap->{"width"}, $bitmap->{"height"});
+
+       # pixel-payload of concat8 SMS w/one IEI_PICTURE_VARIABLE is 129 bytes
+       my $stripe=int(129/int(($width+7)/8));
+       carp "Image width too big to fit in one SMS part" if $stripe<=0;        # ==0
+       # Will we need some newlines?
+       if ($opt_ems_picture_wrap && $stripe<$height) {
+               for (my $y=0;$y<$height;$y+=$stripe) {
+                       push @EMSudh,{
+                                       "type"=>"ems_picture",
+                                       TEXT_POSITION=>length($EMStext),
+                                       "bitmap"=>GSM::SMS::Bitmap->new($bitmap,0,$y,$width,min($stripe,$height-$y)),
+                                       "force_variable"=>$opt_ems_force_variable,
+                                       };
+                       $EMStext.="\n" if $y+$stripe<$height;
+                       }
+               return;
+               }
+       push @EMSudh,{
+                       "type"=>"ems_picture",
+                       TEXT_POSITION=>length($EMStext),
+                       "bitmap"=>$bitmap,
+                       "force_variable"=>$opt_ems_force_variable,
+                       };
+}
+
+sub ems_animation_send
+{
+my($keyword,$arg)=@_;
+
+       my @names=split /:/,$arg;
+       die "Required exactly 4 colon (':') delimited filenames for --ems-animation-send" if 4 != @names;
+       my @bitmaps=map { GSM::SMS::Bitmap->new($_); } @names;
+       my($width,$height)=GSM::SMS::NBS::Message::ems_animation_maxsize(\@bitmaps);
+       # Will we need some newlines?
+       if ($opt_ems_animation_wrap && $height>16) {
+               my $height_now;
+               for (my $y=0;$y<$height;$y+=$height_now) {
+                       $height_now=($y+8>=$height ? 8 : 16);
+                       # we don't do horizontal split here
+                       push @EMSudh,{
+                                       "type"=>"ems_animation",
+                                       TEXT_POSITION=>length($EMStext),
+                                       "bitmaps"=>[ map({
+                                                       GSM::SMS::Bitmap->new($_,0,$y,$width,$height_now);
+                                                       } @bitmaps) ],
+                                       };
+                       $EMStext.="\n" if $y+$height_now<$height;
+                       }
+               return;
+               }
+       push @EMSudh,{
+                       "type"=>"ems_animation",
+                       TEXT_POSITION=>length($EMStext),
+                       "bitmaps"=>\@bitmaps,
+                       };
+}
+
+sub ems_melody_file_send
+{
+my($keyword,$arg)=@_;
+
+       local (*F);
+       open F, $arg;
+       local $/=undef();
+       my $ems_melody_file_data = <F>;
+       close F;
+       warn "EMS iMelody file format not recognized, wrapping anyway"
+                       if $ems_melody_file_data!~/^BEGIN:IMELODY\r\n.*END:IMELODY\r\n$/s;
+       push @EMSudh,{
+                       "type"=>"ems_melody",
+                       TEXT_POSITION=>length($EMStext),
+                       "textdata"=>$ems_melody_file_data,
+                       };
+}
+
+sub ems_text
+{
+my($keyword,$arg)=@_;
+
+       $EMStext.=$arg;
+}