Administrator
Published on 2024-05-16 / 16 Visits
0
0

SMS PDU编解码

概念

PDU是一种短信协议数据单元(Protocol Data Unit)的编码方式,用于在移动通信网络中传输短信。PDU编码将短信分为消息头和消息体两部分,消息头包含了短信的元数据信息,如发送方号码、接收方号码、短信中心号码等,消息体则包含了短信的实际内容。

内容编码

短信的内容编码方法有3种,对应使用的字母表编码格式为:

  • 7 位

  • 8 位

  • 16 位 UCS2

这几种格式对中文的支持不一致:

SMS(Short Message Service)短信服务在早期设计中,使用的是7位或8位的字符集来编码文本信息,这通常是基于GSM标准的字母表,称为GSM 7-bit default alphabet或GSM 8-bit data encoding scheme。这些字符集确实不支持直接发送中文等非拉丁字符。

GSM 7-bit默认字母表包含拉丁字母、数字和一些特殊符号,总共不超过128个字符。而8位编码通常用于发送二进制数据或使用扩展的字符集,但即便如此,它也不包含中文字符。

为了支持中文和其他非拉丁字符,需要使用UCS-2编码(一个16位的编码方案),这可以容纳更多的字符,包括中文、日文、韩文等。使用UCS-2编码时,每条短信可以容纳的字符数会减半,因为每个字符现在需要两个字节来表示。

当发送包含中文的短信时,手机或发送系统会自动将短信内容转换为UCS-2编码,以便能够正确传输中文字符。接收方手机在接收到短信后,会识别出短信是使用UCS-2编码的,并正确地将其转换为中文显示给用户。

因此,虽然GSM标准的7位/8位字母表不支持中文,但通过使用UCS-2编码,SMS仍然可以支持中文短信的发送和接收。

工具

16位编解码工具:SMSPDU.exe

7位/8位/16位编解码工具:PDUspy.exe

C语言编码代码参考(7位方式)

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

typedef enum
{
    PA_WMS_MO_PP_SMS_TYPE_NOT_CONCATENATED = 0,
    PA_WMS_MO_PP_SMS_TYPE_CONCATENATED = 1
} pa_sms_mo_pp_sms_type;

typedef enum {
  PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT          = 0,            /**<  Message format default GSM 7-bit. */
  PA_WMS_MO_PP_SMS_ENCODING_TYPE_BINARY_DATA       = 1,            /**<  Message format binary stream. */
  PA_WMS_MO_PP_SMS_ENCODING_TYPE_TEXT_UCS2         = 2,            /**<  Message format UCS2 text. */
  PA_WMS_MO_PP_SMS_ENCODING_TYPE_UNKNOWN
}pa_sms_mo_pp_sms_encoding_type;

static uint16_t refrence_num = 0x01;
static pa_sms_mo_pp_sms_type concatenated = PA_WMS_MO_PP_SMS_TYPE_NOT_CONCATENATED;

#define SMS_HEAD_LENGTH_LARGE   (0)
#define SMS_MAX_CONTENT_LENGTH  (252)   /**<  Maximum length of SMS content. */
#define SMS_MAX_ADDR_LENGTH     (48)    /**<  Maximum length of SCA addr. */
#define PA_SMS_CORE_MAX_UDL_GSM_LEN_GW                (160)
#define PA_SMS_CORE_MAX_UDL_USC2_LEN_GW               (140)
#define PA_SMS_CORE_MAX_SEGMENT_GSM7_LEN_GW           (153 - 8)
#define PA_SMS_CORE_MAX_SEGMENT_USC2_LEN_GSM          (132)

#define NA    0x20                      /**<  NOT avaliable, show SPACE instead. */

#define LE_OK               (0)
#define LE_FAULT            (-1)
#define LE_UNSUPPORTED      (-2)
#define LE_ERROR            printf
#define LE_INFO             printf
#define LE_DEBUG            printf
#define LE_WARN             printf
#define UTIL_LOG_MSG        printf
#define int32               int
#define le_result_t         int32
#define FALSE               (0)
#define TRUE                (1)

static char iso88591_gsm7_table[] = 
{
/*00 NUL SOH STX ETX EOT ENQ ACK \a \b \t   \n   \v  \f   \r   SO   SI */
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0x0A, NA, NA, 0x0D, NA, NA,
/*10 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM  SUB ESC FS GS  RS  US */
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
/*20 <SP> !    "     #     $    %     &     '    (     )     *    +     ,    -     .    / */
    0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
/*30 0    1     2    3     4    5     6     7    8     9     :    ;     <    =     >    ? */
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
/*40 @    A     B    C     D    E     F     G    H     I     J    K     L    M     N    O */
    0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
/*50 P    Q     R    S     T    U     V     W    X     Y     Z    [     \    ]     ^     _ */
    0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x1B, 0x1B, 0x1B, 0x1B, 0x11,
/*60 `    a    b    c     d    e     f     g    h     i     j    k     l     m    n     o */
    NA, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
/*70 p    q     r    s     t    u     v     w    x     y     z    {     |     }    ~   DEL */
    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x1B, 0x1B, 0x1B, 0x1B, NA,
/*80                                                              */
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
/*90                                                              */
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
/*A0 ?   ¡    ¢    £     ¤    ¥    ¦    §    ¨  ©   ª    «  ¬   SOFT ®  ¯ */
    NA, 0x40, NA, 0x01, 0x24, 0x03, NA, 0x5F, NA, NA, NA, NA, NA, NA, NA, NA,
/*B0 °  ±   ²    ³  ´    µ   ¶  ·    ?  ¹   º   »   ¼   ½   ¾     ¿ */
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0x60,
/*C0 À     Á    Â     Ã     Ä    Å     Æ    Ç     È     É    Ê     Ë    Ì     Í     Î    Ï */
    0x41, 0x41, 0x41, 0x41, 0x5B, 0x0E, 0x1C, 0x09, 0x45, 0x1F, 0x45, 0x45, 0x49, 0x49, 0x49, 0x49,
/*D0 Ð     Ñ     Ò    Ó     Ô    Õ     Ö     ×     Ø    Ù     Ú    Û     Ü     Ý    Þ     ß */
    NA, 0x5D, 0x4F, 0x4F, 0x4F, 0x4F, 0x5C, NA, 0x0B, 0x55, 0x55, 0x55, 0x5E, 0x59, NA, 0x1E,
/*E0 à    á    â     ã     ä    å     æ    ç     è     é    ê     ë    ì     í     î    ï */
    0x7F, 0x61, 0x61, 0x61, 0x7B, 0x0F, 0x1D, 0x09, 0x04, 0x05, 0x65, 0x65, 0x07, 0x69, 0x69, 0x69,
/*F0 ð    ñ    ò    ó     ô    õ     ö    ÷    ø    ù     ú    û     ü     ý    þ    ÿ */
    NA, 0x7D, 0x08, 0x6F, 0x6F, 0x6F, 0x7C, NA, 0x0C, 0x06, 0x75, 0x75, 0x7E, 0x79, NA, 0x79,
};
#undef NA

/*-----------------------------------------------------------------------------------------------*/
/** 
  @brief  Converts ISO8859-1 data to GSM7 data.
  @param[in] ascii     ISO8859-1 data.
  @param[in] ascii_len ISO8859-1 data length.
  @param[out] gsm7 GSM7 data.
  @return Number of GSM7 data converted.
  */
/*-----------------------------------------------------------------------------------------------*/
static int iso88591_to_gsm7(const char *ascii, int ascii_len, char *gsm7)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < ascii_len; i++)
    {
        gsm7[j++] = iso88591_gsm7_table[(uint8_t)ascii[i]];
        if (0x1b == iso88591_gsm7_table[(uint8_t)ascii[i]]) // extension
        {
            switch(ascii[i])
            {
                case '[':
                    gsm7[j++] = 0x3c;
                    break;
                case '\\':
                    gsm7[j++] = 0x2f;
                    break;
                case ']':
                    gsm7[j++] = 0x3e;
                    break;
                case '^':
                    gsm7[j++] = 0x14;
                    break;
                case '{':
                    gsm7[j++] = 0x28;
                    break;
                case '|':
                    gsm7[j++] = 0x40;
                    break;
                case '}':
                    gsm7[j++] = 0x29;
                    break;
                case '~':
                    gsm7[j++] = 0x3d;
                    break;
            }
        }
    }

    return j;
}

int pa_sms_value_from_dtmf(
    int dtmf
)
{
    int c;

    if (dtmf == 10)
        c = '0';
    else
        c = dtmf + 48;

    return c;
}

int pa_sms_retrieve_dtmf_value(
    char c
)
{
    int value;

    //for 0 the dtmf value is 10 (1010 in binary)
    if (c == '0')
    {
        value = 10;
    }
    //for other digits it is the digit itself (c - 48 gives the actual digit)
    else
    {
        value = (c - 48);
    }

    return value;
}

int t_get_string(char *str_buf, int str_len)
{
    char *ptr;
    char buf[256] = {0};

    if(fgets(buf, sizeof(buf)-1, stdin) == NULL)
    {
        return -1;
    }
    
    if(0 == buf[0])
    {
        return -1;
    }

    if(buf[0] == '\n')
    {
        return 1;
    }
    
    ptr = strchr(buf, '\n');
    if(ptr)
    {
        ptr[0] = 0;
    }
    
    strncpy(str_buf, buf, str_len-1);
    
    return 0;
}

int pa_sms_core_encode_destination_number(
    int is_gw,
    char *destination_number,
    uint8_t * encoded_bytes
)
{
    int encoded_stream_len;
    int temp;

    encoded_stream_len = 0;
    temp = 0;

    if (is_gw)
    {
        UTIL_LOG_MSG("Send SMS destination number encode GW\n");

        //every 2 digits in the number in decimal is swapped to form the encoded byte
        //eg: 8586582450 forms 58 68  85  42 05 in encoded bytes
        while (*destination_number)
        {
            if (!temp)
            {
                //get the first digit
                encoded_bytes[encoded_stream_len] = (*destination_number - 48);
                temp = 1;
            }
            else
            {
                //"or" the first digit with the next digit, 4 bits pushed to left
                encoded_bytes[encoded_stream_len] =
                    ((*destination_number - 48) << 4) | encoded_bytes[encoded_stream_len];
                encoded_stream_len++;
                temp = 0;
            }
            destination_number++;
        }

        if (temp)
        {
            //add an F if a digit is left out i.e if the length of the phone number is odd
            encoded_bytes[encoded_stream_len] = 0xF0 | encoded_bytes[encoded_stream_len];
            encoded_stream_len++;
        }
    }
    else
    {
        UTIL_LOG_MSG(" [line:%d] don't support encode format !\n", __LINE__);
    }

    return encoded_stream_len;
}


int pa_sms_convert_sms_content
(
    pa_sms_mo_pp_sms_encoding_type  format,
    const char                      *src_content_ptr,
    uint32_t                        src_content_size,
    char                            *dst_content_ptr,
    uint32_t                        *dst_content_size_ptr
)
{
    int iRet = LE_FAULT;

    if((NULL == src_content_ptr) || (NULL == dst_content_ptr))
    {
        LE_ERROR("input arg is NULL\n");
        return iRet;
    }

    if (PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT == format)
    {
        int len = iso88591_to_gsm7(src_content_ptr, src_content_size, dst_content_ptr);
        if (len > src_content_size * 2)
        {
            LE_ERROR("input sms content is too long\n");
            return iRet;
        }
        *dst_content_size_ptr = (uint32_t)len;
    }
    else
    {
        memcpy(dst_content_ptr, src_content_ptr, src_content_size);
        *dst_content_size_ptr = src_content_size;
    }
    iRet = LE_OK;
    
    return iRet;
}

//Add sms contents to PDU data message
int pa_sms_core_encode_message_content
(   int                             is_gw,
    pa_sms_mo_pp_sms_type           concatenated,
    char                            *message_content,
    int                             message_content_len,
    uint8_t                         *encoded_bytes,
    pa_sms_mo_pp_sms_encoding_type  e_enc_type
)
{
    unsigned char str_index;
    int encoded_stream_len;
    unsigned char shift;
    const uint8_t head_length = 6;

    str_index = 0;
    shift = 0;
    encoded_stream_len = 0;
    
    if (encoded_bytes != NULL && message_content != NULL)
    {
        if(PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT == e_enc_type)
        {
            uint8_t k =0;
            if (concatenated == PA_WMS_MO_PP_SMS_TYPE_CONCATENATED)
                k= 7 - (head_length*8%7) ; //padding   


            if (is_gw)
            {

                while (str_index < (message_content_len))
                {
                #if SMS_HEAD_LENGTH_LARGE
                    shift = (str_index + 1) & 0x07;
                #else
                    shift = str_index & 0x07;
                #endif

                    if (concatenated == PA_WMS_MO_PP_SMS_TYPE_CONCATENATED)
                    {
                        if(str_index != message_content_len - 1)
                        {
                            encoded_bytes[encoded_stream_len++] =
                                ((message_content[str_index] << 1) >> shift) | ((message_content[str_index +
                                    1] << 1) << (7 - shift));
                        }
                        else
                        {
                            encoded_bytes[encoded_stream_len++] =
                                ((message_content[str_index] << 1) >> shift);
                        }
                    }
                    else
                    {
                        if(str_index != message_content_len - 1)
                        {
                            encoded_bytes[encoded_stream_len++] = ((message_content[str_index]) >> shift) |
                            ((message_content[str_index + 1]) << (7 - shift));
                        }
                        else
                        {
                            encoded_bytes[encoded_stream_len++] = (message_content[str_index] >> shift);
                        }
                    }

                    if (shift == 6+k)
                    {
                        str_index++;
                    }
                    str_index++;
                }

                if (str_index < message_content_len)
                {
                    //if one more character left out just add the bits left in the 7 bit ascii
                    shift = str_index & 0x07;
                    if (concatenated == PA_WMS_MO_PP_SMS_TYPE_CONCATENATED)
                        encoded_bytes[encoded_stream_len++] =
                            ((message_content[str_index] << 1) >> shift);
                    else
                        encoded_bytes[encoded_stream_len++] = (message_content[str_index] >> shift);
                }

                if ((message_content_len & 0x07) == 0 &&
                    message_content[message_content_len - 1] == 0x0D)
                {
                    encoded_bytes[encoded_stream_len++] = 0x0D;
                }
            }
            else
            {
                UTIL_LOG_MSG(" [line:%d] don't support encode format !\n", __LINE__);
            }
            
        }
        else if((PA_WMS_MO_PP_SMS_ENCODING_TYPE_TEXT_UCS2      == e_enc_type) ||
                (PA_WMS_MO_PP_SMS_ENCODING_TYPE_BINARY_DATA    == e_enc_type))
        {  
            if (is_gw)
            {
                if (concatenated == PA_WMS_MO_PP_SMS_TYPE_CONCATENATED)
                    message_content_len++;

                
                memcpy(&encoded_bytes[encoded_stream_len], message_content, message_content_len);
                encoded_stream_len = message_content_len;  
            }
            else
            {
                UTIL_LOG_MSG(" [line:%d] don't support encode format !\n", __LINE__);
            }
        }
    }

    return encoded_stream_len;
}

//Encoding for whole SMS data message
int pa_sms_core_encode_gw_sms
(   char                            *destination_number,
    char                            *message_content,
    int                             message_content_len,
    uint8_t                         *encoded_bytes,
    pa_sms_mo_pp_sms_type           concatenated,
    int                             seg_number,
    int                             total_segments,
    pa_sms_mo_pp_sms_encoding_type  e_enc_type
)
{
    int iter_i = 0;

    printf("concatenated:%d seg_number:%d total_segments:%d e_enc_type:%d\n", concatenated, seg_number, total_segments, e_enc_type);
    
    if (destination_number && message_content && encoded_bytes)
    {
        encoded_bytes[iter_i++] = 0x00;         //SCA
        if (concatenated == PA_WMS_MO_PP_SMS_TYPE_CONCATENATED)
        {
            if(seg_number < total_segments)
                encoded_bytes[iter_i++] = 0x41;     // 0"1"00 need to be set to send concatenated SMS
            else
                encoded_bytes[iter_i++] = 0x45;     // 0"1"01 need to be set to send concatenated SMS
        }
        else
            encoded_bytes[iter_i++] = 0x01;     // FO, 0"0"00 is not set for normal text SMS
        encoded_bytes[iter_i++] = 0x00;         //MR

        if ('+' == destination_number[0])       //DA
        {
            destination_number++;
            encoded_bytes[iter_i++] = strlen(destination_number);
            encoded_bytes[iter_i++] = 0x91;     // international format
        }
        else
        {
            encoded_bytes[iter_i++] = strlen(destination_number);
            encoded_bytes[iter_i++] = 0x81;  //unkown number
        }

        iter_i += pa_sms_core_encode_destination_number(TRUE,
            destination_number, &encoded_bytes[iter_i]);

        encoded_bytes[iter_i++] = 0x00; //PID, 
        
        if(PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT == e_enc_type) //TP-DCS P2P msg
        {
            encoded_bytes[iter_i++] = 0x00;//7 bit encoding
        }
        else if(PA_WMS_MO_PP_SMS_ENCODING_TYPE_TEXT_UCS2 == e_enc_type)
        {
            encoded_bytes[iter_i++] = 0x08;//TP-DCS Unicode encoding
        }
        else if(PA_WMS_MO_PP_SMS_ENCODING_TYPE_BINARY_DATA == e_enc_type)
        {
            encoded_bytes[iter_i++] = 0x04;
        }
        //for long SMS
        if (concatenated == PA_WMS_MO_PP_SMS_TYPE_CONCATENATED)
        {        
            if(PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT == e_enc_type)
            {
            #if SMS_HEAD_LENGTH_LARGE
                encoded_bytes[iter_i++] = message_content_len + 7 + 1;     //7 bytes of header and padding are added to the length
            #else
                encoded_bytes[iter_i++] = message_content_len + 6 + 1;     //6 bytes of header and padding are added to the length
            #endif
            }
            else if(    PA_WMS_MO_PP_SMS_ENCODING_TYPE_TEXT_UCS2 == e_enc_type
                    ||  PA_WMS_MO_PP_SMS_ENCODING_TYPE_BINARY_DATA == e_enc_type
                )
            {
                encoded_bytes[iter_i++] = message_content_len + 6;      //6 bytes of header added to the length
            }
            else 
            {
                UTIL_LOG_MSG(" error  e_enc_type:%d\n", e_enc_type);
                return -1;
            }  
            //PDU User data Header
        #if SMS_HEAD_LENGTH_LARGE
            encoded_bytes[iter_i++] = 0x06;
            encoded_bytes[iter_i++] = 0x08;
        #else
            encoded_bytes[iter_i++] = 0x05;
            encoded_bytes[iter_i++] = 0x00;
        #endif
            
            
        #if SMS_HEAD_LENGTH_LARGE
            encoded_bytes[iter_i++] = 0x04;
            encoded_bytes[iter_i++] = refrence_num>>8;
            encoded_bytes[iter_i++] = refrence_num&0xFF;
        #else
            encoded_bytes[iter_i++] = 0x03;
            encoded_bytes[iter_i++] = refrence_num&0xFF;
        #endif
            encoded_bytes[iter_i++] = total_segments;   //total number of segments
            encoded_bytes[iter_i++] = seg_number;       //segment number of the PDU
            
            if(seg_number == total_segments)
            {
            #if SMS_HEAD_LENGTH_LARGE
                if(refrence_num++ == 0xFFFF)
            #else
                if(refrence_num++ == 0xFF)
            #endif
                {
                    refrence_num = 0;
                }
            }
        }
        else
            encoded_bytes[iter_i++] = message_content_len;  //normal text msg length

        iter_i += pa_sms_core_encode_message_content(  TRUE,
                                                        concatenated, 
                                                        message_content, 
                                                        message_content_len, 
                                                        &encoded_bytes[iter_i],
                                                        e_enc_type);
    }

    return iter_i;
}

/*
    pa_sms_mo_pp_sms_encoding_type  format, //Encoding type
    char                            *destination_number,  
    uint32_t                        length,     // Message length
    const char*                     dataPtr     // Message contents


*/

le_result_t pa_sms_SendMsg
(
    pa_sms_mo_pp_sms_encoding_type  format,
    char                            *destination_number,
    uint32_t                        length,     ///< [IN] The length of the TP data unit in bytes.
    const char*                  dataPtr     ///< [IN] The message.
)
{
    le_result_t iRet = LE_FAULT;
    uint32_t msg_length = 0, i = 0, j = 0, raw_message_len = 0;
    uint32_t segment_gw_send_len = 0, segment_gw_compare_len = 0, total_segments = 0;
    char *sms_content = NULL;
    uint8_t raw_message[SMS_MAX_CONTENT_LENGTH] = {0};
    bool Is_long_sms = FALSE;

    if(destination_number == NULL)
    {
        LE_ERROR("\n input destination_number is NULL \n");
        return iRet;
    }

    if(length > 0)
    {
        sms_content = (char *)malloc(length * 2);
        if(sms_content == NULL)
        {
            LE_ERROR("\n alloc sms_content mem failed \n");
            return iRet;
        }
        memset(sms_content, '\0', length * 2);
    }

    iRet = pa_sms_convert_sms_content(format, (const char *)dataPtr, length, sms_content, &msg_length);
    if(iRet != LE_OK)
    {
        LE_ERROR("\n pa_sms_convert_sms_content failed \n");
        if(sms_content)
        {
            free(sms_content);
        }
        return iRet;
    }
    
    if(format == PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT)
    {
        segment_gw_compare_len = PA_SMS_CORE_MAX_UDL_GSM_LEN_GW;
#if SMS_HEAD_LENGTH_LARGE
        segment_gw_send_len = PA_SMS_CORE_MAX_SEGMENT_GSM7_LEN_GW - 1;
#else
        segment_gw_send_len = PA_SMS_CORE_MAX_SEGMENT_GSM7_LEN_GW;
#endif
    }
    else if(    format == PA_WMS_MO_PP_SMS_ENCODING_TYPE_BINARY_DATA
            ||  format == PA_WMS_MO_PP_SMS_ENCODING_TYPE_TEXT_UCS2)
    {
        segment_gw_compare_len = PA_SMS_CORE_MAX_UDL_USC2_LEN_GW;
        segment_gw_send_len = PA_SMS_CORE_MAX_SEGMENT_USC2_LEN_GSM;
    }
    else
    {
        LE_ERROR("\n unsupport sms format :%d \n",format);
        if(sms_content)
        {
            free(sms_content);
        }
        return iRet;
    }

    total_segments = (msg_length + segment_gw_send_len - 1) / segment_gw_send_len;
    Is_long_sms = (msg_length > segment_gw_compare_len);
    concatenated = (Is_long_sms ? PA_WMS_MO_PP_SMS_TYPE_CONCATENATED : PA_WMS_MO_PP_SMS_TYPE_NOT_CONCATENATED);
    printf("msg_length:%d total_segments:%d Is_long_sms:%d concatenated:%d\n", msg_length, total_segments, Is_long_sms, concatenated);
    for(i = 0; i < total_segments; i++)
    {
        raw_message_len = pa_sms_core_encode_gw_sms(
                    destination_number, 
                    sms_content + i * segment_gw_send_len, 
                    (msg_length > (i+1) * segment_gw_send_len ? segment_gw_send_len : msg_length - i * segment_gw_send_len),
                    raw_message, 
                    concatenated, 
                    (Is_long_sms ? i+1 : 0), 
                    (Is_long_sms ? total_segments : 0),
                    format);

       for(j = 0; j < raw_message_len;)
       {
            if(j < raw_message_len / 8 * 8)
            {
                LE_INFO("%02x %02x %02x %02x %02x %02x %02x %02x\n", 
                    raw_message[j], raw_message[j+1], raw_message[j+2], raw_message[j+3], raw_message[j+4], raw_message[j+5], raw_message[j+6], raw_message[j+7]);
                j += 8;
            }
            else
            {
                LE_INFO("%02x ", raw_message[j]);
                j++;
            }           
       }
       LE_INFO("\n");
       LE_INFO("Directly copy below pdu contents to test:\n");
       for(j = 0; j < raw_message_len;)
       {
            LE_INFO("%02x", raw_message[j]);
            j++;
       }
       LE_INFO("\n");

    }
    if(sms_content)
    {
        free(sms_content);
    }
    return iRet;
}

int main(void)
{
    char c;
    int ret = -1;
    char buf[SMS_MAX_CONTENT_LENGTH] = {0};
    char dest_num[SMS_MAX_ADDR_LENGTH] = {0};
    pa_sms_mo_pp_sms_encoding_type format = PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT;
    int len = 0;
    int v = 0;
    
    printf("please enter phone number: ");
    ret = t_get_string(dest_num,sizeof(dest_num));
    if(ret != 0)
    {    
        printf("Invalid input\n");
        return ret;
    }
//    printf("example binary content: \n");
    printf("please enter SMS content: ");
    switch (format)
    {
        case PA_WMS_MO_PP_SMS_ENCODING_TYPE_GSM_7BIT:
            fgets(buf, SMS_MAX_CONTENT_LENGTH, stdin);
            len = strlen(buf);
            if ('\r' == buf[len-1] || '\n' == buf[len-1])
            {
                buf[len-1] = 0;
                len--;
            }
            break;
        case PA_WMS_MO_PP_SMS_ENCODING_TYPE_BINARY_DATA:
            while (1 == scanf("%x", &v))
            {
                buf[len++] = v;
            }
            ret = t_get_string(&c,2);
            if(ret != 0)
            {    
                printf("invalid input\n");
                return ret;
            }            
            break;
        default:
            return ret;
    }
    printf("raw content size: %d\n", len);

    ret = pa_sms_SendMsg(format, dest_num, len, buf);

    return ret;
}


Comment