[Bugfix][T108][bug-view-1987/1990]Resolve the issue of not receiving 8-bit text messages

Only Configure: No
Affected branch: mbtk_source_ds
Affected module: sms
Is it affected on IC: only ASR
Self-test: yes
Doc Update: no

Change-Id: I7f18f77102ccefc90d2e7d8bb4f85441bdee3650
diff --git a/mbtk/libmbtk_lib/ril/mbtk_pdu_sms.c b/mbtk/libmbtk_lib/ril/mbtk_pdu_sms.c
index 3b1b481..20cf88e 100755
--- a/mbtk/libmbtk_lib/ril/mbtk_pdu_sms.c
+++ b/mbtk/libmbtk_lib/ril/mbtk_pdu_sms.c
@@ -16,7 +16,7 @@
 

 #define SUB_STR_SIZE 512

 char temp[SUB_STR_SIZE];

-

+#define MIN(a, b) ((a) < (b) ? (a) : (b))

 // some constant

 

 //长短信信息元素参考号

@@ -2613,36 +2613,69 @@
                 break;

 

             case MDAPI_SMS_CHARSET_GSM_8BIT:

-                sms->msg_content = ptr;

-                content_size = strlen(sms->msg_content);

-                #if 0

-                if( 1 == udh ) {

-                    data_len -= (udh_len + 1);

+                kal_int32 utf8_len = 0;

+                kal_uint8* binary_data = NULL;

+                kal_char* utf8_buffer = NULL;

+            

+                // 分配二进制缓冲区

+                binary_data = malloc(data_len);

+                if (!binary_data) {

+                    LOGE("Memory allocation failed for 8bit data");

+                    return MDAPI_RET_ERROR;

                 }

-                if (data_len >= *record_size - 2) {

-                    data_len = *record_size - 2;

-                }

-

-                sms->msg_content[0] = '\0';

-                for (i = 0 ; i < data_len ; i++) {

-                    kal_uint8 tmp[2] = {0, 0};

-

-                    tmp[0] = internal_mdapi_hex2int(ptr);

-                    ptr += 2;

-

-                    buf_len += kal_ext_ascii_to_utf8((UTF8 *)(sms->msg_content + buf_len), tmp, *record_size - buf_len);

-                    if (*record_size - buf_len < 0) {

+            

+                // 将十六进制PDU转换为二进制数据

+                kal_int32 bin_index = 0;

+                for (i = 0; i < data_len; i++) {

+                    kal_int32 hex_val = internal_mdapi_hex2int(ptr);

+                    if (hex_val < 0 || hex_val > 255) {

+                        free(binary_data);

+                        LOGE("Invalid hex value in 8bit PDU at position %d", i);

                         return MDAPI_RET_ERROR;

                     }

+                    binary_data[bin_index++] = (kal_uint8)hex_val;

+                    ptr += 2;

                 }

-

-                *(sms->msg_content + buf_len++) = '\0';

-                *(sms->msg_content + buf_len++) = '\0';

-

-                *record_size = buf_len;

-

-                //LOGE(KAL_LOG_INFO, LOG_TAG, "is utf8 = [%d]", is_utf8_sequence((UTF8 *)sms->msg_content, (UTF8 *)sms->msg_content + data_len - 2));

-                #endif

+            

+                // 分配UTF-8缓冲区(最大可能大小)

+                utf8_buffer = malloc(data_len * 3 + 1);

+                if (!utf8_buffer) {

+                    free(binary_data);

+                    LOGE("Memory allocation failed for UTF-8 buffer");

+                    return MDAPI_RET_ERROR;

+                }

+            

+                // 转换二进制数据到UTF-8

+                kal_int32 buf_pos = 0;

+                for (i = 0; i < data_len; i++) {

+                    kal_uint8 tmp[2] = {binary_data[i], 0};

+                    kal_int32 converted = kal_ext_ascii_to_utf8(

+                        (UTF8*)(utf8_buffer + buf_pos),

+                        tmp,

+                        (data_len * 3) - buf_pos

+                    );

+                    

+                    if (converted <= 0) {

+                        LOGE("Character conversion failed at position %d", i);

+                        break;

+                    }

+                    buf_pos += converted;

+                }

+                utf8_buffer[buf_pos] = '\0';

+                utf8_len = buf_pos;

+            

+                // 复制到输出缓冲区

+                kal_int32 copy_size = MIN(utf8_len, *record_size - 1);

+                memcpy(sms->msg_content, utf8_buffer, copy_size);

+                sms->msg_content[copy_size] = '\0';

+                *record_size = copy_size;

+            

+                // 清理临时内存

+                free(binary_data);

+                free(utf8_buffer);

+                

+                LOGE("8bit msg decoded: len=%d, content=%.*s", 

+                    copy_size, copy_size > 50 ? 50 : copy_size, sms->msg_content);

                 break;

 

             case MDAPI_SMS_CHARSET_UCS2:

@@ -2710,7 +2743,7 @@
     /*lei modify for gsw 2022/5/11*/

     if(status == MDAPI_RET_SUCCESS) {

         memcpy(da_num, record.phone_number, strlen(record.phone_number));

-        memcpy(msg, record.msg_content, strlen(record.msg_content));

+        memcpy(msg, record.msg_content, out_len);

         *charset = record.charset;

         memcpy(date, record.time, strlen(record.time));