PDU-7Bit转码

#include 
#include 
#include 
#include 
#include 
#include 

/*#include 
#include 
*/
#include 

#include "sw_cell_type.h"
#include "cellular_err.h"
#include "cellular_net.h"
#include "cellular_msg.h"
#include "cellular_sms.h"
#include "cellular_atcmd.h"


#ifndef ICONV_CONST
# define ICONV_CONST const
#endif

#define SMS_LIST_SIZE  135*1024

int g_mem_type;
sms_t list_head;
char sms_center[PH_NUM_LEN]="";


int gsmEncode7bit(unsigned char* pDst,const char* pSrc)
{
	int nSrc;        // 源字符串的计数值
	int nDst;        // 目标编码串的计数值
	int nChar;       // 当前正在处理的组内字符字节的序号,范围是0-7
	unsigned char nLeft;    // 上一字节残余的数据
	int nSrcLength=strlen(pSrc);



	// 计数值初始化
	nSrc = 0;
	nDst = 0;

	// 将源串每8个字节分为一组,压缩成7个字节
	// 循环该处理过程,直至源串被处理完
	// 如果分组不到8字节,也能正确处理
	while(nSrc> nChar;
			// 修改目标串的指针和计数值 pDst++;
			//printf("%c",*pDst);
			pDst++;  nDst++;

		}
		// 修改源串的指针和计数值
		pSrc++; nSrc++;
	}

	//Nleft还有剩余,需要一个自己保留。
	nChar = nSrc & 7;
	if(nChar != 0)
	{
		*pDst=nLeft;
		nDst++; 
		pDst++;
	}
	*pDst='\0';

	// 返回目标串长度
	return nDst;
}


int sms_mem_type( int mem_type)
{
    if( mem_type == SMS_ON_MEM)
    {
        send_cmd( AT_SMS_ON_MEM, NULL, 0);
        return 0;
    }
    else if( mem_type == SMS_ON_SIM)
    {
        send_cmd( AT_SMS_ON_SIM, NULL, 0);
        return 0;
    }
    else
    {
        return -1;
    }
}

sms_t *find_lsms_head( sms_t *sms, sms_t *head)
{
    while( head != NULL)
    {
        if( head->lsms_mark == sms->lsms_mark)
        {
            return head;
        }
        head = head->next;
    }

    return head;
}

int add_to_list( sms_t *sms)
{
    sms_t *head = list_head.next;
    sms_t *last = &list_head;
    sms_t *temp = NULL;

    if( sms == NULL)
    {
        return -1;
    }   

    temp = ( sms_t *)malloc( sizeof(sms_t));
    if( temp == NULL)
    {
        return -1;
    }
    memset( temp , 0, sizeof( sms_t));
    memcpy( temp, sms, sizeof( sms_t));
    
    //printf("sms_mark = %d, ser_num = %d\n", sms->lsms_mark, sms->lsms_sernum);
    //printf("temp_mark = %d, temp_num = %d\n", temp->lsms_mark, temp->lsms_sernum);

    if( temp->lsms_mark != 0)
    {
        //printf("mark = %d \n", sms->lsms_mark);
        head = find_lsms_head( sms, head);
        /*if( head == NULL)
        {
            temp->next = list_head.next;
            list_head.next = temp;
            return 0;
        }
        */
        if( head != NULL)
        {
            while( head != NULL && head->lsms_mark == temp->lsms_mark)
            {
                if( head->lsms_sernum > temp->lsms_sernum)
                {
                    break;
                }
                last = head;
                head = head->next;
            }
            temp->next = last->next;
            last->next = temp;
            return 0;
        }
    }
    
    head = list_head.next;
    last = &list_head;
    while( head != NULL)
    {
        if( strcmp( temp->date_time, head->date_time) > 0)
        {
            break;
        }
        last = head;
        head = head->next;
    }
    last->next = temp;
    temp->next = head;
    

    return 0;
    
}

/*!
 * 对字符串进行语言编码转换
 * param from  原始编码,比如"GB2312",的按照iconv支持的写
 * param to      转换的目的编码
 * param save  转换后的数据保存到这个指针里,需要在外部分配内存
 * param savelen 存储转换后数据的内存大小
 * param src      原始需要转换的字符串
 * param srclen    原始字符串长度
 * */
int convert(const char *from, const char *to, char* save, int savelen, char *src, int srclen)
{
    iconv_t cd;
    char   *inbuf = src;
    size_t inbufsize = srclen;
    char *outbuf = save;
    size_t outbufsize = savelen;
    int status = 0;
    //size_t  savesize = 0;
    /*const char* inptr = inbuf;
    size_t      insize = inbufsize;
    char* outptr = outbuf;
    size_t outsize = outbufsize;
    */
    int ret;

    cd = iconv_open(to, from);
    iconv(cd,NULL,NULL,NULL,NULL);
    if (inbufsize == 0) {
        status = -1;
        return -1;
    }
	printf("cover---------------------------%din,: %s, in size = %d,\n\n", __LINE__, inbuf, inbufsize);
    ret = iconv( cd, &inbuf, (size_t)&inbufsize, &outbuf, &outbufsize);

    iconv_close(cd);
	printf("cover---------------------------%din,: %s, in size = %d, outbuf:%s, outsize = %d\n\n", __LINE__, inbuf, inbufsize, outbuf,outbufsize);
    return status;

}


int char_swap( char *data, int len)
{
    char temp;
    int i;

    for( i=0; iindex);
        printf("Date: %s\nFrom: %s\nText:%s", head->date_time, head->ph_num, head->text);
        last = head;
        head = head->next;

        /*while( head != NULL && head->lsms_mark != 0 && last->lsms_mark == head->lsms_mark)
        {   
            //printf("Mark = %d num = %d\n", head->lsms_mark, head->lsms_sernum);
            printf("%s", head->text);
            last = head;
            head = head->next;
        }
        */
        printf("\n\n");
    }

    return 0;
}

int free_sms_list()
{
    sms_t *head = list_head.next;
    sms_t *last = &list_head;
    int i = 0;

    while( head != NULL)
    {
        last = head;
        head = head->next;
        free(last);
        i++;
    }

    list_head.next = NULL;

    //printf(" i = %d freed %d bytes\n", i, i*sizeof(sms_t));
    return 0;

}

/*int sms_to_db()
{
    sms_t *head = list_head.next;

    while( head != NULL)
    {
        insert_sms_db( head);
        head = head->next;
    }

    return 0;
}
*/
sms_t *get_sms_list( int mem_type)
{
    static char sms_list[SMS_LIST_SIZE] = "";
    int sms_size = SMS_LIST_SIZE;
    int ret = 0;

    memset( sms_list, 0, SMS_LIST_SIZE);

    free_sms_list();

    /*if( SMS_ON_MEM == mem_type)
    {
        printf("SMS_ON_MEM\n");
        get_sms_table();
    }
    else if( SMS_ON_SIM == mem_type)
    {
        printf("SMS_ON_SIM\n");
        ret = send_cmd( AT_SMS_LIST, sms_list, sms_size);
        //printf("%s\n", sms_list);
        deal_sms_list( sms_list);
    }
    else
    {
        //printf("SMS_ON_UNKNOW\n");
        return NULL;
    }
    //sms_list_print();
    //printf("%s", sms_list);
    */
    ret = sms_mem_type( mem_type);
    if( ret != 0)
    {
        return NULL;
    }
    ret = send_cmd( AT_SMS_LIST, sms_list, sms_size);
    //printf("%s\n", sms_list);
    deal_sms_list( sms_list);
    //ret = sms_mem_type( g_mem_type);

    return list_head.next;
}

int sms_delete( int mem_type, int index)
{
    char cmd[127] = "";
    int ret;

    /*if( mem_type == SMS_ON_MEM)
    {
        ret = sms_db_del( index);
    }
    else if( mem_type == SMS_ON_SIM)
    {
        if( index == -1)
        {
            sprintf( cmd, "AT+CMGD=0,4");
        }
        else
        {
            sprintf( cmd, "AT+CMGD=%d", index);
        }
        ret = send_cmd( cmd, NULL, 0);
    }
    */
    ret = sms_mem_type( mem_type);
    if( ret != 0)
    {
        return -1;
    }
    if( index == -1)
    {
        sprintf( cmd, "AT+CMGD=0,4\n");
    }
    else
    {
        sprintf( cmd, "AT+CMGD=%d\n", index);
    }
    ret = send_cmd( cmd, NULL, 0);

    return ret;
}

int send_text_sms( char *ph_num, char *text)
{
    int ret = 0;
    char cmd[255]="";
    printf("num = %s, text=%s\n", ph_num, text);
    ret = send_cmd( AT_SMS_TYPE_TEXT, NULL, 0);

    sprintf( cmd, "AT+CMGS=\"%s\"\r", ph_num);
    ret = send_cmd( cmd, NULL, 0);
    memset( cmd, 0, 255);
    sprintf( cmd, "%s\26", text);
    ret = send_cmd( cmd, NULL, 0);
    return ret;
}

int send_pdu_sms( char *ph_num, char *text)
{
	printf("num === %s, txt ==== %s\n", ph_num, text);
    int ret = 0;
    char *cp = text;
    char sms_type = 0;
    char cmd[512] = "";
    int num_len=0;
    //char text_buf[200] = "";
    char *text_buf = NULL;
    //char pdu_text_buf[400] = "";
    char *pdu_text_buf = NULL;
    int text_len = 0;
    char msg_buf[200] = "";
    int msg_len = 0;
    int code_type = 0;
    char ph_num_buf[20]="";
    int num_type = 0x81;
	char center_getnum[64] = {0};
	char center_num[64] = {0};//中心号码

    for(; *cp != '\0'; cp++)
    {
        sms_type |= *cp;
    }
    if( sms_type < 0)
    {
        code_type = 8;
    }
    
    strcpy( ph_num_buf, ph_num);
    ph_num = ph_num_buf;
    printf("num = %s, text=%s\n", ph_num, text);
    ret = send_cmd( AT_SMS_TYPE_PDU, NULL, 0);

	//获取中心号码
	sw_cell_get_center_num(center_getnum);
	int sms_center_len = strlen(center_getnum);
    if( sms_center_len%2 != 0)
    {
        strcat( center_getnum, "F");
        char_swap( center_getnum, sms_center_len + 1);
    }
    else
    {
        char_swap( center_getnum, sms_center_len);
    }
	sprintf(center_num, "91%s", center_getnum);//转换后的中心号码
	//sms_center_len = strlen(sms_center);//中心号码长度
	sms_center_len = strlen(center_num);//中心号码长度
	printf("sms centerd:%s, sms len:%d\n",center_num, sms_center_len);
//	printf("ret =  %d\n", ret);
#if 0
	memset( cmd, 0, 512);
	sprintf( cmd, "AT+CMGS=?\r");
	ret = send_cmd( cmd, NULL, 0);
	if( ret != AT_OK)
	{   
		printf( "semd cmd :%s failed\n", cmd);
		//return -1;
	}
#endif
    if( *ph_num == '+')
    {
        ph_num++;
        num_type = 0x91;
    }

    num_len = strlen(ph_num);
    if( num_len%2 != 0)
    {
        strcat( ph_num, "F");
        char_swap( ph_num, num_len + 1);
    }
    else
    {
        char_swap( ph_num, num_len);
    }

    text_len = strlen( text);
	int actrue_len = text_len;
    int text_buf_size = text_len * 2;
    int pdu_text_buf_size = text_len * 4;

    text_buf = (char *)malloc( text_buf_size);
    pdu_text_buf = (char *)malloc( pdu_text_buf_size);
    if( text_buf == NULL || pdu_text_buf == NULL)
    {
        if( text_buf != NULL)
        {
            free( text_buf);
        }
        if( pdu_text_buf != NULL)
        {
            free( pdu_text_buf);
        }

        return -1;
    }
    memset( text_buf, 0, text_len * 2);
    if( code_type == 8)
    {
        ret = convert( "UTF-8", "UTF-16", text_buf, text_buf_size, text, text_len);
        printf("codec ret = %d\n", ret);
        text_len = strlen( text_buf);
    }  
    else
    {
        strcpy( text_buf, text);
    }

	printf("text buf ==== %s\n",text_buf);
    text = text_buf;
	printf("text ==== %s\n",text);
    char *pdu_text = pdu_text_buf;
    int i;
    memset( pdu_text, 0, pdu_text_buf_size);
	int len = gsmEncode7bit( pdu_text, text);

	for(i=0;i 140)
    {
        int msg_type = 0x40;
        int mark = 0;
        int total = 0;
        int l_num = 0;
        char long_sms_buff[300] = "";
        char *pdu_text_buf_p = pdu_text_buf;
            
        total = text_len/280;
        if( (text_len%280) != 0)
        {
            total++;
        }
        for( i=0; i= 0)
    {
        ret = send_text_sms( ph_num, text);
    }
    else
    {*/
        ret = send_pdu_sms( ph_num, text);
    //}

    return ret;

}

你可能感兴趣的:(PDU-7Bit转码)