Fixed name coding of Alcatel format payload name (now it is 7bit coded)
[gsmperl.git] / GSM / SMS / NBS / Message.pm
1 package GSM::SMS::NBS::Message;
2
3 use strict;
4 use warnings;
5
6 use     GSM::SMS::NBS::Lib;
7 use     GSM::SMS::PDU;
8 use Carp;
9
10 use vars qw($VERSION);
11 $VERSION = '0.1';
12
13 use constant DEFAULT_DCS_UDH    => 0xF5; # 8bit, ME specific
14 use constant DEFAULT_DCS_NOUDH  => 0x00; # default DCS (typically 7bit, SIM specific)
15 use constant DEFAULT_VALIDITY   => "1d"; # FIXME: currently fixed to 1 day
16 use constant DEFAULT_SMS_CENTER => "";   # FIXME: currently fixed to empty SMS center address
17
18 use constant USERDATA_LENGTH    => 140;
19 use constant DECONCAT_TOTAL     => "_deconcat_total";  # internal: undef=>try single message, *=>try & messages
20 use constant TEXT_POSITION      => "text_position";    # numeric position in $payload, UDH IE will be present only once
21
22 use constant IEI_ANIMATION_LARGE  => 0x0E;  #IEI: EMS large animation (16x16 x4 = 128 bytes)
23 use constant IEI_ANIMATION_SMALL  => 0x0F;  #IEI: EMS small animation ( 8x 8 x4 =  32 bytes)
24 use constant IEI_PICTURE_LARGE    => 0x10;  #IEI: EMS large    picture (32x32 = 128 bytes)
25 use constant IEI_PICTURE_SMALL    => 0x11;  #IEI: EMS small    picture (16x16 =  32 bytes)
26 use constant IEI_PICTURE_VARIABLE => 0x12;  #IEI: EMS variable picture (other)
27 use constant IEI_ALCATEL          => 0x80;  #IEI: Alcatel proprietary "data download" type
28
29 # SAR for NBS messages
30 # --------------------
31 # This part does the segmentation ... look in Stack.pm for reassembly
32
33 sub new {
34         my $proto = shift;
35         my $class = ref($proto) || $proto;
36
37         my $self = {};
38         $self->{'__FRAMES__'} = [];
39
40         bless($self, $class);
41         return $self;
42 }
43
44 # Create a message from a payload -- compatibility cludge!
45 sub create {
46         my ($self, $number, $payload, $destination_port, $source_port, $datacodingscheme) = @_;
47         my %dcs_compat = (
48                         "7bit" =>0x00,  # default DCS (typically 7bit, SIM specific)
49                         "7biti"=>0xF0,  # 7bit, immediate display
50                         "8bit" =>0xF6,  # 8bit, SIM specific
51                         "8biti"=>0xF4,  # 8bit, immediate display
52                         "8bitm"=>0xF5,  # 8bit, ME specific
53         );
54
55         $datacodingscheme=$dcs_compat{$datacodingscheme} if $datacodingscheme && exists $dcs_compat{$datacodingscheme};
56
57         return $self->store($number, pack("H*", $payload),
58                         "dcs"=>$datacodingscheme,
59                         "udh"=>[
60                                 {
61                                         "type"            =>"port16",  #IEI: application port addressing scheme, 16 bit address
62                                         "destination_port"=>$destination_port,
63                                         "source_port"     =>$source_port,
64                                 },
65                         ],
66                         );
67 }
68
69 sub _iebuilder {
70         my ($self, $iei, @iedata) = @_;
71
72         return ($iei,scalar(@iedata),@iedata);
73 }
74
75 sub _ie_port16 {
76         my ($self, %args) = @_;
77
78         return () if !defined $args{"destination_port"};
79         my $source_port=$args{"source_port"} || $args{"destination_port"};
80
81         return $self->_iebuilder(0x05,  #IEI: application port addressing scheme, 16 bit address
82                         ($args{"destination_port"} >> 8), ($args{"destination_port"} & 0xFF),
83                         ($source_port >> 8), ($source_port & 0xFF),
84         );
85 }
86
87 sub _ie_concat8 {
88         my ($self, %args) = @_;
89
90         return $self->_iebuilder(0x00,  #IEI: concatenated short message, 8 bit reference
91                         $args{"drn"},  # reference number
92                         $args{"fmax"}, # maximum number of short messages
93                         $args{"fsn"},  # sequence number of the current short message
94         );
95 }
96
97 sub _ie_ems_picture {
98         my ($self, %args) = @_;
99         my $bitmap = $args{"bitmap"};  # GSM::SMS::Bitmap instance
100         
101         $bitmap->crop(0xFF*8,0xFF);
102         my ($width, $height) = ($bitmap->{"width"}, $bitmap->{"height"});
103
104         my $iei;
105            if ($width == 16 && $height == 16) { $iei = IEI_PICTURE_SMALL   ; }
106         elsif ($width == 32 && $height == 32) { $iei = IEI_PICTURE_LARGE   ; }
107         else                                  { $iei = IEI_PICTURE_VARIABLE; }
108
109         return $self->_iebuilder($iei,
110                         $args{TEXT_POSITION}, # position in the SMS
111                         ($iei != IEI_PICTURE_VARIABLE ? () : (int(($width+7)/8), $height)),  # image size (optional)
112                         $bitmap->pixlist_horiz()
113         );
114 }
115
116 sub _ie_ems_animation {
117         my ($self, %args) = @_;
118         my $bitmaps = $args{"bitmaps"};  # list of GSM::SMS::Bitmap instances
119
120         if (4 != @$bitmaps) {
121                 carp "Invalid length of EMS animation (4 required)"; return ();
122         }
123         my $bitmap = $$bitmaps[0];
124         my ($width, $height) = ($bitmap->{"width"}, $bitmap->{"height"});
125         for my $testbitmap (@$bitmaps) {
126                 if ($width != $testbitmap->{"width"} || $height != $testbitmap->{"height"}) {
127                         carp "Non-matching sizes in EMS animation";
128                 }
129         }
130
131         my $iei;
132            if ($width ==  8 && $height ==  8) { $iei = IEI_ANIMATION_SMALL   ; }
133         elsif ($width == 16 && $height == 16) { $iei = IEI_ANIMATION_LARGE   ; }
134         else {
135                 $iei = IEI_ANIMATION_LARGE;
136                 carp "Invalid frame size of EMS animation (8x8 or 16x16 required)";
137         }
138
139         return $self->_iebuilder($iei,
140                         $args{TEXT_POSITION}, # position in the SMS
141                         &{sub {
142                                 my @r=();
143                                 for $bitmap (@$bitmaps) {
144                                         push @r,$bitmap->pixlist_horiz();
145                                 }
146                                 return @r;
147                         }}
148         );
149 }
150
151 sub _ie_alcatel {
152         my ($self, %args) = @_;
153
154         return $self->_iebuilder(IEI_ALCATEL,
155                         (!defined($args{"name"}) ? (0x00) :  # bit 7=0 (use GSM charset)
156                                         (length($args{"name"}), unpack("C*",
157                                                         encode_payload(0xF1, GSM::SMS::PDU->inversetranslate($args{"name"}))))  # 0xF1 DCS=any 7bit
158                                         ),
159                         (!!$args{"ems_compat"} << 7)  # bit 7=EMS compatibility
160                                         |($args{"alcatel_type"} & 0x07),  # bits 0..2=Alcatel message type
161                         ($args{"length"} >> 8), ($args{"length"} & 0xFF),
162         );
163 }
164
165 sub _udh_build {
166         my ($self, @srcs) = @_;
167
168         my @r=();
169         no strict 'refs';
170         my @list=map { &{"_ie_".$$_{"type"}}($self,%$_); } @srcs;
171         use strict 'refs';
172         return "" if !@list;
173         return pack "C*",scalar(@list),@list;
174 }
175
176 # Create a message from a payload
177 # returns either error message or list (DECONCAT_TOTAL,total messages)
178 sub _store_try {
179         my ($self, $number, $payload, %args) = @_;
180
181         # Reset the FRAME array
182         $self->{'__FRAMES__'} = [];
183
184
185         my $PDU = GSM::SMS::PDU->new();
186
187         my @udhs=($args{"udh"} ? @{$args{"udh"}} : ());
188
189         # We put ME-specific default if ANY element of UDH was specified
190         # Autodetect $dcs BEFORE possible concatenation UDH IEs get inserted!
191         my $dcs=$args{"dcs"} || (@udhs ? DEFAULT_DCS_UDH : DEFAULT_DCS_NOUDH);
192         # print "DCS: $dcs\n";
193
194         my $deconcat_ieref; # defined only if deconcatenating
195         if (defined $args{DECONCAT_TOTAL}) {
196                 my %deconcat_ie=(
197                                 "type"=>"concat8",
198                                 "drn"=>int(rand(0xFF)),        # reference number
199                                 "fmax"=>$args{DECONCAT_TOTAL}, # maximum number of short messages
200                                 "fsn"=>0x00,                   # sequence number of the current short message
201                 );
202                 $deconcat_ieref=\%deconcat_ie;
203                 push @udhs,$deconcat_ieref;
204                 return "Too long message (".$deconcat_ie{"fmax"}." elements)" if (0xFF < $deconcat_ie{"fmax"});
205         }
206
207         my @udhstatic=();
208         my @udhpositioned=();
209         for my $udh (@udhs) {
210                 if (exists $udh->{TEXT_POSITION}) {
211                         push(@udhpositioned, $udh);
212                 } else {
213                         push(@udhstatic, $udh);
214                 }
215         }
216         @udhpositioned = sort { $a->{TEXT_POSITION} <=> $b->{TEXT_POSITION}; } @udhpositioned;
217
218         my $position=0;           # absolute current position in the whole long SMS
219         while (length $payload || @udhpositioned) {
220                 my @udhpositioned_now=();    # items from @udhpositioned placed in the current SMS part
221                 my $payload_now="";          # text  from $payload       placed in the current SMS part
222                 while (1) {
223                         if (@udhpositioned && $udhpositioned[0]{TEXT_POSITION} <= $position) { # "<=" should be "=="
224                                 last if USERDATA_LENGTH < length($self->_udh_build(@udhstatic,@udhpositioned_now,$udhpositioned[0]))
225                                                 +nail_payload_len($dcs,length($payload_now));
226
227                                 my %udhei=%{shift @udhpositioned};  # copy it to local state - we will be modifying it
228                                 # we subtract the absolute position base of current SMS part:
229                                 $udhei{TEXT_POSITION} -= $position-length($payload_now);
230
231                                 push @udhpositioned_now,\%udhei;
232                                 next;
233                         }
234                         my $minlen=length $payload;
235                         $minlen=min($minlen,$udhpositioned[0]{TEXT_POSITION}-$position) if @udhpositioned;
236                         $minlen=min($minlen,empty_subload($dcs,
237                                         USERDATA_LENGTH-length($self->_udh_build(@udhstatic,@udhpositioned_now))-length($payload_now)))
238                                         if defined $args{DECONCAT_TOTAL};
239                         last if $minlen<=0; # "<=" should be "=="
240                         $payload_now.=substr($payload,0,$minlen);
241                         $payload=substr($payload,$minlen);
242                         $position+=$minlen;
243                 }
244
245                 # We will ship out the current SMS part here
246                 ++$deconcat_ieref->{"fsn"} if $deconcat_ieref;
247                 # We exceeded userdata size, this is probably the first undeconcatenaed try from store()!
248                 my $userdata=$self->_udh_build(@udhstatic,@udhpositioned_now).encode_payload($dcs,$payload_now);
249
250                 return (DECONCAT_TOTAL, undef) if USERDATA_LENGTH < length($userdata);
251
252                 my $pdu = $PDU->SMSSubmit(DEFAULT_SMS_CENTER, $number, $userdata,
253                                 $dcs, DEFAULT_VALIDITY, !!@udhstatic || !!@udhpositioned_now
254                                 );
255                 # Push on to frame array
256                 # print "--> $pdu\n"; 
257                 push(@{$self->{"__FRAMES__"}}, $pdu);
258         }
259
260         carp "Last fsn==".$deconcat_ieref->{"fsn"}." but fmax==".$deconcat_ieref->{"fmax"}
261                         if $args{DECONCAT_TOTAL} && $deconcat_ieref->{"fsn"}!=$deconcat_ieref->{"fmax"};
262         return (undef,(!$deconcat_ieref ? 1 : $deconcat_ieref->{"fsn"})); # success, return number of _PRODUCED_ MSes
263 }
264
265 sub store {
266         my ($self, $number, $payload, %args) = @_;
267
268         my ($err, $total);
269         ($err, $total) = $self->_store_try($number, $payload, %args, DECONCAT_TOTAL=>undef ); return $err if !$err || $err ne DECONCAT_TOTAL;
270         ($err, $total) = $self->_store_try($number, $payload, %args, DECONCAT_TOTAL=>0     ); return $err if $err;
271         ($err, $total) = $self->_store_try($number, $payload, %args, DECONCAT_TOTAL=>$total); return $err;
272 }
273
274 # Return the frames
275 sub get_frames {
276         my $self = shift;
277         return $self->{'__FRAMES__'};
278 }
279
280
281 1;
282
283 =head1 NAME
284
285 GSM::SMS::NBS::Message - SAR functionality for NBS messages.
286
287 =head1 DESCRIPTION
288
289 Implements the segmentation in the SAR engine ( Segmentation And Reassembly ).
290
291 =head1 AUTHOR
292
293 Johan Van den Brande <johan@vandenbrande.com>