f09021aabef0b904d9d388f79c9020346fcd3671
[gsmperl.git] / GSM / SMS / NBS / Alcatel.pm
1 package GSM::SMS::NBS::Alcatel;
2
3 use strict;
4 use warnings;
5
6 require Exporter;
7 our @ISA=qw(Exporter);
8 our @EXPORT=qw();
9
10 use GSM::SMS::NBS::Lib;
11 use Carp;
12
13 use vars qw($VERSION);
14 $VERSION = '0.1';
15
16 use constant IEI_ALCATEL_PICTURE     => 0b100;
17 use constant IEI_ALCATEL_ANIMATION   => 0b101;
18 use constant IEI_ALCATEL_MSEQ_MELODY => 0b010;
19
20
21 # returns ($msg, $udh=[...])
22 sub alcatel_picture {
23         my ($class, $bitmap) = @_;
24
25         $bitmap->crop(0xFF,0xFF);
26         my @msglist=($bitmap->{"width"}, $bitmap->{"height"}, $bitmap->pixlist_vert());
27         return (pack("C*", @msglist), [
28                         {
29                                 "type"=>"alcatel",
30                                 "ems_compat"=>1
31                                                 && !($bitmap->{"width"}&0x7)                            # width%8 == 0
32                                                 && ($bitmap->{"width"}/8 * $bitmap->{"height"} <= 128), # width/8*height <= 128
33                                 "alcatel_type"=>IEI_ALCATEL_PICTURE,
34                                 "length"=>scalar(@msglist),
35                         }]);
36 }
37
38 # expects list of GSM::SMS::Bitmap instances mixed with numbers (display times)
39 # returns ($msg, $udh=[...])
40 sub alcatel_animation {
41         my ($class, @list) = @_;
42
43         my $sized;
44         my @words=();
45         my %imgs=();
46         my $imgmsg="";  # concatenated image bodies (produced by alcatel_picture())
47
48         my $header_len = 2*(1+@list);  # "2*"=words, "1+"=sequence_size, "@list"=items
49         for my $item (@list) {
50                 if ("GSM::SMS::Bitmap" eq ref $item) {
51                         if (!exists $imgs{$item}) {
52                                 $imgs{$item}=$header_len+length($imgmsg);
53                                 my ($msg)=$class->alcatel_picture($item);
54                                 $imgmsg.=$msg;
55                                 $sized = ((0  # condition tests whether we have _violated_ EMS compatibility
56                                                 || $item->{"width"} != $item->{"height"}
57                                                 || ($sized && $sized != $item->{"width"})
58                                                 || ($item->{"width"} != 8 && $item->{"width"} != 16)
59                                                 ) ? -1 : $item->{"width"});
60                         }
61                         push(@words,$imgs{$item});
62                 } else {  # "display time" here:
63                         push(@words,min($item & 0x0FFF));
64                 }
65         }
66         unshift(@words,scalar(@words));
67         my $r=pack("v*",@words).$imgmsg;
68         return ($r, [
69                         {
70                                 "type"=>"alcatel",
71                                 "ems_compat"=>1
72                                                 && 4 == keys %imgs  # exactly 4 frames
73                                                 && -1 != $sized,    # frame sizes valid
74                                 "alcatel_type"=>IEI_ALCATEL_ANIMATION,
75                                 "length"=>length($r),
76                         }]);
77 }
78
79 # returns ($msg, $udh=[...])
80 sub alcatel_mseq_melody_file {
81         my ($class, $file_data) = @_;
82
83         return ($file_data, [
84                         {
85                                 "type"=>"alcatel",
86                                 "ems_compat"=>!!(0x20 & ord(substr($file_data,4,1))),
87                                 "alcatel_type"=>IEI_ALCATEL_MSEQ_MELODY,
88                                 "length"=>length($file_data),
89                         }]);
90 }
91
92 1;