Marco Romano

NAME

Net::UCP - Perl extension for EMI - UCP Protocol.

SYNOPSIS

    use Net::UCP;

    $emi = Net::UCP->new(
                         SMSC_HOST   => 'smsc.somedomain.tld', 
                         SMSC_PORT   => 3024, 
                         SENDER_TEXT => 'My Self 123', 
                         SRC_HOST=   => 'my.host.tld', 
                         SRC_PORT    => '1666',
                         FAKE => 0,
                         );

DESCRIPTION

This module implements a Client Interface to the EMI - UCP Interface specification, This Protocol can be used to comunicate with an SMSC (Short Message Service Centre)

Usually the Network connection is based on TCP/IP or X.25.

You will of course be required to have a valid login at the SMSC to use their services. (Unless there is an SMSC which provides their services for free. Please, let me know about any such service provider.) If you want to help my project send me info about some SMSC account.

A Net::UCP object must be created with the new() constructor. Once this has been done, all commands are accessed via method calls on the object.

If you have a good know-how about EMI/UCP or if you have patience to read specification you can use this module in raw mode. See RAW MODE for more informations.

EXAMPLE

    use Encode;   #you need it to convert text to GSM 03.38 Default Alphabet. See DATA CODING section
    use Net::UCP;
    
    ($recipient,$text,$sender) = @ARGV;
     
    my ($acknowledge, $error_number, $error_text);

    $emi = Net::UCP->new(SMSC_HOST   => 'smsc.somedomain.tld',
                         SMSC_PORT   => 3024,
                         SENDER_TEXT => 'MyApp',
                         SRC_HOST    => '10.10.10.21', #optional see below
                         SRC_PORT    => '1666',        #optional see below
                         WARN        => 1,
                         FAKE        => 0
                         ) or die("Failed to create SMSC object");
    
    $emi->open_link() or die($!);

    ($acknowledge,$error_number,$error_text) = $emi->login(
                                                           SMSC_ID    => 'your_account_id',
                                                           SMSC_PW    => 'your password',
                                                           SHORT_CODE => 'your Auth Code',
                                                           OTON       => '5',        #optional
                                                           ONPI       => '1',        #optional 
                                                           VERS       => '0100',     #optional
                                                           );
    
    die ("Login to SMSC failed. Error nbr: $error_number, Error txt: $error_text\n") unless($acknowledge);
    
    ($acknowledge,$error_number,$error_text) = $emi->send_sms(
                                                              RECIPIENT      => $recipient, #mand.
                                                              MESSAGE_TEXT   => $text,      #opt
                                                              SENDER_TEXT    => $sender,    #opt
                                                              );
    
    die("Sending SMS failed. Error nbr: $error_number, Error txt: $error_text\n") unless($acknowledge);
    
    $emi->close_link();

CONSTRUCTOR

new()

The parameters may be given in arbitrary order.

SMSC_HOST=> Mandatory. The hostname or ip-address of the SMSC.

SMSC_PORT=> Optional. The TCP/IP port number of your SMSC. If omitted, port number 3024 will be used by default.

SMSC_HOST=> Optional. Your ip appdress.

SRC_PORT=> Optional. The TCP/IP source port number. You need to set it if you want to use auth. method based on AC.

SENDER_TEXT=> Optional. The text that will appear in the receivers mobile phone, identifying you as a sender. If omitted, the text 'Net::UCP' will be used by default. You will probably want to provide a more meaningful text than that.

TIMEOUT=> Optional. A timeout, given in seconds, to wait for an acknowledgement of an SMS message transmission. The value must be numeric, positive and within the range of 0 (zero) to 60. Failing this, or if the parameter is omitted, the default timeout of 15 seconds will be applied. The value of this parameter will be used in all calls to the send_sms() method. If the SMSC does not respond with an ACK or a NACK during this period, the send_sms() method will return a NACK to the caller. If the value of 0 (zero) is given, no timeout will occur but the send_sms() method will wait indefinitively for a response. Note that the value given to the constructor can temporarily be overruled in the call to the send_sms() method. As a final note, please remember that not all systems have implemented the alarm() call, which is used to create a timeout. On such systems, this module will still do a blocking call when reading data from the SMSC.

WARN=> Optional. If this parameter is given and if it evaluates to true, then any warnings and errors will be written to STDERR. If omitted, or if the parameter evaluates to false, then nothing is written to STDERR. It is strongly recommended to turn on warnings during the development of an application using the Net::UCP module. When development is finished, the developer may chose to not require warnings but to handle all error situations completely in the main application by checking the return values from Net::UCP.

The constructor returns undef if mandatory information is missing or invalid parameter values are detected. In this case, the object is discarded (out of scope) by the Perl interpreter and you cannot call any methods on the object handle.

Any errors detected will be printed on STDERR if the WARN=> parameter evaluates to true.

Test the return value from the constructor!

METHODS

Open the communication link to the SMSC. In reality, this opens up a socket to the SMSC. Be aware that this is not an authenticated login but that the login() method must also be called before any SMS messagescan be sent to the SMSC if you will use an Authentication based on Login and Password.

open_link() is useful since the main application can verify that it's at all possible to communicate with the SMSC. (Think: getting through a firewall.)

This method takes no parameters since it will use the data given in the constructor parameters.

Any errors detected will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

open_link() returns true on success and undef in case something went wrong.

login()

You are able to use authentication based on Operation 60 of EMI Protocol. Authenticates against the SMSC with the given SMSC-id and password.

or

Directly through Operation 51. Authenticates against the SMSC with the given SHORT_CODE (AC parameter).

If the open_link() method has not explicitly been called by the main application, the login() method will do it before trying to authenticate with the SMSC.

The parameters may be given in arbitrary order.

SHORT_CODE=> A valid Authentication Code Mandatory for Auth based on OP 51.

SMSC_ID=> A string which should be a valid account ID at the SMSC.

SMSC_PW=> A valid password at the SMSC.

Optional Parameters (Beta):

OTON=> Originator Type of Number

it could be :

              1 = International Number (Starts with the country code) 
              2 = National Number (Default value is omitted)
              6 = Abbreviated Number (short number alias)              
              

ONPI=> Originator Numbering Plan Id

it could be :

              1 = E.164 address (default value if omitted)
              3 = X.121 address
              5 = Private (TCP/IP address/abbreviated number address)

STYP=> Subtype of Operation

              1 = add item to mo-list
              2 = remove item from mo-list
              3 = verify item mo-list
              4 = add item to mt-list
              5 = remove item from mt-list
              6 = verify item mt-list

VERS=> (Test) default value is 0100

Any errors detected will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

Return values:

In void context, login() will always return undef. login() will return a true value if you will use it only to set Authentication Code.

In scalar context, login() will return true for success, false for transmission failure and undef for application related errors. Application related errors may be for instance that a mandatory parameter is missing. All such errors will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

In array context, login() will return three values: ($acknowledge, $error_number, $error_text); where $acknowledge holds the same value as when the method is called in scalar context (i.e. true, false or undef), $error_number contains a numerical error code from the SMSC and $error_text contains a (relatively) explanatory text about the error.

Be aware that both $error_number and $error_text are provided in a response from the SMSC, which means that the data quality of these entities depends on how well the SMSC has implemented the protocol.

If $acknowledge is undef, then $error_number will be set to 0 (zero) and $error_text will contain a zero length string.

It is strongly recommended to call login() in an array context, since this provides for an improved error handling in the main application.

send_sms()
EXAMPLE
    $binary_message  = "024A3A7125CD7DD1A1A5CD7DB1BDD994040045225D04985585D85D84106906985D84984A85585D85D84104D";
    $binary_message .= "04104D85D0690410A24824C49A6289B09D093126986A800";

    $emi->send_sms(
                   RECIPIENT      => '391232345678', 
                   MESSAGE_TEXT   => 'A Message', 
                   SENDER_TEXT    => 'Marco', 
                   FLASH          => 1,
                   UDH            => '050415811581',
                   MESSAGE_BYNARY => $binary_message,
                   TIMEOUT        => 5 
                   );

Submits the SMS message to the SMSC (Operation 51) and waits for an SMSC acknowledge.

The parameters may be given in arbitrary order.

RECIPIENT=> Mandatory.

This is the phone number of the recipient in international format with leading a '+' or '00'.

MESSAGE_TEXT=> Optional. A text message to be transmitted.

It is accepted to transfer an empty message, so if this parameter is missing, a space character will be sent (0x20).

FLASH=> Optional. With this parameter you are able to send a Class 0 messages.

              1) Set to 1 Flash Message enabled     (Class 0)
              2) Other value Flash Message disabled (No Class)

MESSAGE_BINARY=> Optional. A binary message to be transmitted.

UDH=> Optional. User Data Header (you need to set UDH to use MESSAGE_BINARY).

First UDH Octet (length) will be internally calculated for this reason you need to omitted it.

SENDER_TEXT=> Optional. The text that will appear in the receivers mobile phone, identifying you as a sender.

    In this version you are able to set:

              1) alphanumeric sender  ( Marco81        )
              2) numeric sender       ( 67166155111    )
              3) international format ( +3934112331112 ) 

This text will temporarily replace the text given to the constructor. If omitted, the text already given to the constructor will be used.

TIMEOUT=> Optional.

A timeout, given in seconds, to wait for an acknowledgement of this SMS message transmission. The value must be numeric, positive and within the range of 0 (zero) to 60. Failing this, or if the parameter is omitted, the timeout established in the new() constructor will be applied. If the SMSC does not respond with an ACK or a NACK during this period, the send_sms() method will return a NACK to the caller. If the value of 0 (zero) is given, no timeout will occur but the send_sms() method will wait indefinitively for a response. On a system that has not implemented the alarm() call, which is used to create a timeout, this module will still do a blocking call when reading data from the SMSC.

Any errors detected will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

Return values:

In void context, send_sms() will always return undef.

In scalar context, send_sms() will return true for success, false for transmission failure and undef for application related errors. Application related errors may be for instance that a mandatory parameter is missing. All such errors will be printed on STDERR if the WARN=> parameter in the constructor evaluates to true.

In array context, send_sms() will return the three values: ($acknowledge, $error_number, $error_text); where $acknowledge holds the same value as when the method is called in scalar context (i.e. true, false or undef), $error_number contains a numerical error code from the SMSC and $error_text contains a (relatively) explanatory text about the error.

Be aware that both $error_number and $error_text are provided in a response from the SMSC, which means that the data quality of these entities depends on how well the SMSC has implemented the protocol.

If $acknowledge is undef, then $error_number will be set to 0 (zero) and $error_text will contain a zero length string.

It is strongly recommended to call send_sms() in array context, since this provides for an improved error handling in the main application.

Note! The fact that the message was successfully transmitted to the SMSC does not guarantee immediate delivery to the recipient or in fact any delivery at all.

About the timeout. Not all Perl systems are able to provide the timeout. The timeout is internally implemented with the alarm() call. If your system has implemented alarm(), then any timeout value provided will be honored. If not, you may provide any value you wish for the timeout, it will still be ignored and reading the ACK from the SMSC will block until everything is read.

If the SMSC fails to send you the full response your application will freeze. If your system does implement alarm() but you do not provide any timeout value, then the default timeout of 15 seconds will be applied.

You may test this on your own system by executing the following on a command line:

perl -e "alarm(0)"

If the response is that alarm() is not implemented, then you're out of luck. You can still use the module, but in case the SMSC doesn't respond as expected your application will wait indefinitively.

send_sms_multipart()

use it as send_sms() but you are able to send messages with text length bigger then 160 characters.

    $emi->send_sms_multipart(
                             RECIPIENT      => '+393291212121',
                             MESSAGE_TEXT   => 'Message text with more then 160 characters',
                             SENDER_TEXT    => 'Marco',
                             );

( Don't use it in a production environment. Please test it and contact me for improvment and bugs )

logout()

logout() is an alias for close_link(). Whichever method name is used, the very same code will be executed.

What goes up, must also come down. If the main application will continue working on other tasks once the SMS message was sent, it is possible to explicitly close the communications link to the SMSC with this method.

If the Net::UCP object handle (returned by the new() method) goes out of scope in the main application, the link will be implicitly closed and in this case it is not necessary to explicitly close the link.

In reality, this method closes the socket established with the SMSC and does some additional house-keeping. Once the link is closed, a new call to either open_link() or to login() will try to re-establish the communications link (socket) with the SMSC.

returns nothing (void)

RAW MODE

How to use it in "RAW MODE"

 You can use Net::UCP in "raw mode" and it means you can bypass wrapper method like send_sms or login and other stuff 
 like that. You can manage your UCP string calling method make_message() and parse_message() (see below).
 
 For example : to authenticate your client you will be able to create your ucp 60 string calling make_message( op => 60 ... ) 
 and send it to smsc using transmit_msg() method. When SMSC will get back to you with an ucp response you will parse it 
 calling parse_message() which gets back a reference to an HASH with ucp protocol fields as keys and their contents as keys values. 

Parsing Message in Raw Mode

parse_message()

with this method you are able to parse any string get back from SMSC without know what kind of message SMSC has given to you.

it returns a ref to an hash that contains message parsed with message parameters as keys (LOWERCASE) or undef on error. (it's a wrapper of parse_* functions reported below)

Every hash reference get back from parse_message contains "my_checksum" key, its value is checksum recalculated from module, you can use this value to check checksum get back from ucp client.

EXAMPLE
     use Data::Dumper;

     my $smsc_message = "06/00043/R/01/A/01234567890:090196103258/4E";
    
     $ref_msg = $ucp->parse_message($smsc_message);

     print "This is a " . $ref_msg->{type} . " type\n";
     print "OT -> " . $ref_msg->{ot} . "\n\n"; 

     print "\nDUMP\n";
     print Dumper($ref_msg);

Making Message in Raw Mode

make_message()

with this method you are able to make UCP strings (it's a wrapper of functions below) it returns a scalar value with UCP string or undef on error.

EXAMPLE

#we are making a ucp 01 operation type "O".

    my $ucp_string = $upc->make_message(
                                        op => 01,
                                        operation => 1,
                                        adc  => '01234567890',
                                        oadc => '09876543210',
                                        ac   => '',
                                        mt   => 3,
                                        amsg => 'Short Message'
                                       );

    

#we are making a ucp 01 operation type "R". (result)

    my $ucp_string = $ucp->make_message(
                                        op => 01,
                                        result => 1,
                                        trn    => '47',
                                        nack   => 'N',
                                        ec     => '02',
                                        sm     => 'Syntax Error
                                        );

#op 51 submit short message

     $ucp_string = $ucp->make_message(
                                      op => '51',
                                      operation => 1,
                                      adc   => '00393311212',
                                      oadc  => 'ALPHA@NUM',
                                      mt   => 3,
                                      amsg => 'Short Message for NEMUX',
                                      mcls => 1,
                                      otoa => 5039,
                                      );

     #you get back something like that :
     #02/00130/O/51/00393311212/10412614190438AB4D/////////////////3//
     #53686F7274204D65737361676520666F72204E454D5558////1////5039/////C8

     #ready for being sent through transmit_msg() to your SMSC

All Parsing Functions Name

For all operations exist a method parse_[OP_NN]

      Operation 01 -> parse_01();
      Operation 02 -> parse_02();
      Operation 03 -> parse_03();
      Operation 30 -> parse_30();
      Operation 31 -> parse_31();
      Operation 51 -> parse_51();
      Operation 52 -> parse_52();
      Operation 53 -> parse_53();
      Operation 54 -> parse_54();
      Operation 55 -> parse_55();
      Operation 56 -> parse_56();
      Operation 57 -> parse_57();
      Operation 58 -> parse_58();
      Operation 60 -> parse_60();
      Operation 61 -> parse_61();      
     

every functions return a reference to a hash (as seen for parse_message()) or undef on error.

All Making Functions Name

For all operations exist a method make_[OP_NN]

      Operation 01 -> make_01();
      Operation 02 -> make_02();
      Operation 03 -> make_03();
      Operation 30 -> make_30();
      Operation 31 -> make_31();
      Operation 51 -> make_51();
      Operation 52 -> make_52();
      Operation 53 -> make_53();
      Operation 54 -> make_54();
      Operation 55 -> make_55();
      Operation 56 -> make_56();
      Operation 57 -> make_57();
      Operation 58 -> make_58();
      Operation 60 -> make_60();
      Operation 61 -> make_61();

every functions return a scalar value with message string or undef on error. For every function is possible to set as parameters in input the same name of operation's parameters.

EXAMPLE

#make operation

    $ucp_string = $ucp->make_01(
                                operation => 1,
                                adc  => '01234567890',
                                oadc => '09876543210',
                                ac   => '',
                                mt   => 3,
                                amsg => 'Short Message'
                                );

    if ( defined($ucp_string) ) { 
        
        ($ack, $error_code, $error_text) = $ucp->transmit_msg( $ucp_string, 5, 1 ); 
    
    }

SMSC side

    $ucp->make_01(
                  result => 1,
                  trn    => '07',
                  ack    => 'A',
                  sm     => '01234567890:090196103258'
                  );

or... nack

    $ucp->make_01(
                  result => 1,
                  trn    => '47',
                  nack   => 'N',
                  ec     => '02',     
                  sm     => 'Syntax Error'
                  );

another example.. op 02

    $ucp_string = $ucp->make_02(
                                operation => 1,
                                npl   => '3',
                                rads  => '003932412341/00393291111/00393451231',
                                oadc => '123',
                                ac   => '',
                                mt   => 3,
                                amsg => 'Short Message to 3 subscribers'
                                );

Other Methods

remove_ucp_enclosure()

this method REMOVE STX and ETX characters from string received in input and return nothing.

EXAMPLE
    $emi_obj->remove_ucp_enclosure(\$ucp_message);
add_ucp_enclosure()

this method ADD STX and ETX characters to string received in input and return nothing.

EXAMPLE
    $emi_obj->add_ucp_enclosure(\$ucp_message);
wait_in_loop()

It needs three parameters (timeout, action, clear) clear is a boolean value, when clear is equal "1", method removes characters STX and ETX from all messages received.

With wait_in_loop() method you are able to get back every messages from SMSC.

First parameter is timeout in second. Second parameter is a ref to a subroutine. This subroutine will be call when timeout will be caught up.

Second patameter is mandatory and it has no sense if timeout is undefined or less or equal 0. If it isn't set up, wait_in_loop will die leaving a message on standard output when timeout will be caught up.

it will get back a scalar value with a message string or undef value.

EXAMPLE
    my $message;

    sub make_something {
        print "Timeout reached...";
        exit 0;
    }

    $message = $emi->wait_in_loop(
                                  timeout => 30,
                                  action => \&make_something
                                 );

    if (defined($message)) {
        print "I Get Back From SMSC ... " . $message . "\n";
    } else {
        print "No Message from SMSC\n";
    }

without timeout, it waits "in loop" until something get back.

    $message = $emi->wait_in_loop();

with clear equal 1 it removes STX and ETX characters.

    $message = $emi->wait_in_loop(clear => 1);
transmit_msg()

with this method you are able to transmit messages to the SMSC directly, it will get back SMSC response if you need it.

Parameters are 3 : ucp message, timeout in second, boolean value as flag for response.

Retrun value are 3 (in array context) = ack, error_code, error_text. If you don't need response these 3 values will be undef. In a void context, get beck only ack or nack, or undef on transmission problem.

You don't need response in some cases,

    1) you are sending a RESPONSE (Operation "R")
    2) [for example] you are receving messages from SMSC through a child uses wait_in_loop 
       (it could be an idea)
    
EXAMPLE
    $timeout = 10; #ten seconds timeout
    $i_need_response = 1;
 
    ($ack, $error_code, $error_text) = $ucp->transmit_msg($ucp_message, $timeout, $i_need_response);

SMSCfAKE

Description

This module version support second release of SMSCfAKE, with this feature you are able to start a simple smsc that receive messages from any client. It parses and prints out UCP messages. Using action parameter is possible to add a call back that SMSCfAKE will call when it will receive an UCP string. In order to improve the possibility to use it as an application tester i've added another parameter : sending to add another call back that it will call after first call back action

Simple Interaction Diagram :

         Client                                  SMSCfAKE 
           |             send UCP string             |  
           | --------------------------------------> |
           |   send "action" call back return value  |        
           | <-------------------------------------- |
           |   send "sending" call back return value |
           | <-------------------------------------- |
           |                                         |
           |                                         |
           |                                         |

Example :

   1) Client send    : OP 51 type O (submit sms)
   2) SMSCfAKE send  : OP 51 type R (submit sms response)
   3) SMSCfAKE send  : OP 53 type O (delivery notification)

How-to create a simple Fake SMSC

Is possible to create a simple Fake SMSC through method reported below.

create_smsc_fake()

It accepts 8 optional parameters : host, port, listen, output, action, sending, reading_mode, max_len

    1) host : set smsc address (default are 127.0.0.1)
    2) port : set smsc port (default are 6666)
    3) listen : max listen (default 5)
    4) output : set to 1, if you want to see on standard output string UCP got back from client
    5) action : your internal subroutine reference. It must to accept a scalar value as input, its content 
                will be UCP String got back from client. 
                It could be get back a UCP string response into a scalar value. This value will be printed 
                get back to the client.
                Without this parameter smsc fake parse UCP string and will print it on stdout.
    6) sending      : (another internal subroutine useful to send another UCP string to client)
    7) reading_mode : it can be equal to 0 to receive UCP string with new line at the end 
                      it can be equal to 1 to receive UCP string without new line at the end
    8) max_len      : max byte length of ucp string receive in reading_mode 1 default value 1024 (1Kb)
EXAMPLE
   $ucp = Net::UCP->new(FAKE => 1);
   $ucp->create_fake_smsc();

   $ucp = Net::UCP->new(FAKE => 1);

   sub code_ref($) {
       my $ucp_string = shift;
    
       $ucp->parse_message($ucp_string);

       return $ucp_string_respone;
   }
   
   $ucp->create_fake_smsc(
                          host   => 10.10.10.21,
                          port   => 1234,
                          action => \&code_ref
                          );

   $ucp->create_fake_smsc(
                          host         => 10.10.10.21,
                          port         => 5000,
                          listen       => 10,
                          output       => 1,
                          reading_mode => 1,
                          max_len      => 2048,
                          action       => \&ucp_response_call_back,
                          sending      => \&delivery_notification_call_back
                          );

DATA CODING

SMSC default alphabet is GSM 03.38 ( ftp://ftp.unicode.org/Public/MAPPINGS/ETSI/GSM0338.TXT )

the easiest way to make this conversion is to use Encode module and its encode method

Encoding Example
  use Encode;
  
  #doing that UCP module will do right 7bit conversion and the result will be : 10412614190438AB4D 
  my $sender = encode('gsm0338', 'ALPHA@NUM');  
  my $amsg_example = encode('gsm0338', 'Short Message for NEMUX by Net::UCP');

  #ucp string will be : 
  #01/00154/O/51/00393201001/10412614190438AB4D/////////////////3//
  #53686F7274204D65737361676520666F72204E454D5558206279204E65743A3A554350\////1////5039/////C7

  $ucp_string = $emi->make_message(
                                   op => '51',
                                   operation => 1,
                                   adc   => '00393201001',
                                   oadc  => $sender,
                                   mt   => 3,
                                   amsg => $amsg_example,
                                   mcls => 1,
                                   otoa => 5039,
                                 );
                
   if ( defined($ucp_string) ) {
       ($acknowledge, $error_number, $error_text) = $emi->transmit_msg( $ucp_string, 10, 1 );
        print $error_text ."\n";
   } else {
       die "Error while making UCP String OP 51\n";
   }
                
   $emi->close_link();

SEE ALSO

IO::Socket, IO::Select, Encode, Net::UCP::Common, Net::UCP::IntTimeout, Net::UCP::TransactionManager

AUTHOR

Marco Romano, <nemux@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2004-2008 by Marco Romano

  This library is free software; you can redistribute it and/or modify
  it under the same terms as Perl itself, either Perl version 5.8.4 or,
  at your option, any later version of Perl 5 you may have available.

4 POD Errors

The following errors were encountered while parsing the POD:

Around line 2390:

'=item' outside of any '=over'

Around line 2417:

You forgot a '=back' before '=head2'

You forgot a '=back' before '=head2'

Around line 2473:

You forgot a '=back' before '=head2'

Around line 2571:

You forgot a '=back' before '=head2'