:pserver:anonymous@intra.tektonica.com:/opt/cvs - gsmperl - Fri Dec 21 07:37 CET...
[gsmperl.git] / GSM / SMS / NBS / Message.pm
1 package GSM::SMS::NBS::Message;
2 use     GSM::SMS::PDU;
3 use GSM::SMS::NBS::Frame;
4
5 $VERSION = '0.1';
6
7 use constant DATAGRAM_LENGTH => 128;
8
9 # SAR for NBS messages
10 # --------------------
11 # This part does the segmentation ... look in Stack.pm for reassembly
12
13 sub new {
14         my $proto = shift;
15         my $class = ref($proto) || $proto;
16
17         my $self = {};
18         $self->{'__FRAMES__'} = [];
19
20         bless($self, $class);
21         return $self;
22 }
23
24 # Create a message from a payload
25 sub create {
26         my ($self, $number, $payload, $destination_port, $source_port, $datacodingscheme) = @_;
27
28         # Reset the FRAME array
29         $#{$self->{'__FRAMES__'}} = -1;
30
31
32         my $nbs = GSM::SMS::NBS::Frame->new();
33         my $pdu = GSM::SMS::PDU->new();
34
35         my $datagram_length = DATAGRAM_LENGTH;
36
37         $source_port = $destination_port if (!defined($source_port));
38         my $dcs = '7bit';
39         $dcs = '8bitm' if (defined($destination_port));
40         
41         # print "DATACODINGSCHEME: $datacodingscheme\n";
42                 
43         $dcs = $datacodingscheme if (defined($datacodingscheme));
44
45         my $udhi = -1;  
46         $udhi = 0 if (!defined($destination_port));
47
48
49     my $payload_len = length($payload)/2;
50         my $frags;
51
52         # test if the payload can fit in one message (6= length of minimal header)
53         if (($payload_len + 6) <= 140) {
54                 # Ok we can have 1 message
55                 $frags=1;
56                 $datagram_length = $payload_len;
57         } else {
58         $frags = int($payload_len/$datagram_length) + 1;
59         }
60
61     $nbs->destination_port($destination_port);
62     $nbs->source_port($source_port);
63         
64         # If no destination port defined, then also no source port can be defined
65         # There can be a problem ! what if 2 nbs's send a nbs to the same phone with 2 the same datagrams?
66         # We have to solve this!
67         $nbs->datagram_reference_number(int(rand(255)));
68     $nbs->fragment_maximum($frags);
69
70         my $ok = 0; 
71     for (my $i=1; $i<=$frags; $i++) {
72         my $subload = substr($payload, ($i-1)*$datagram_length*2, $datagram_length*2);
73         $nbs->fragment_sequence_number($i);
74                 my $msg;
75                 if ($destination_port) {        
76                         $msg = uc $nbs->asString().$subload;
77                 } else {
78                         $msg = $subload;
79                 }
80                 # print "DCS-> $dcs\n";
81         my $p =  $pdu->SMSSubmit('', $number, $msg, $dcs, '1d', $udhi);
82         # print "--> $p\n"; 
83                 # Push on to frame array
84                 push(@{$self->{'__FRAMES__'}}, $p );
85    
86         }       
87         return 0 if ($ok == $frags);
88         return -1;
89 }
90
91 # Return the frames
92 sub get_frames {
93         my $self = shift;
94         return $self->{'__FRAMES__'};
95 }
96
97
98 1;
99
100 =head1 NAME
101
102 GSM::SMS::NBS::Message - SAR functionality for NBS messages.
103
104 =head1 DESCRIPTION
105
106 Implements the segmentation in the SAR engine ( Segmentation And Reassembly ).
107
108 =head1 AUTHOR
109
110 Johan Van den Brande <johan@vandenbrande.com>