#include "wh_lte_7s4_v2.h"

osThreadId_t WH_LTE_Handle;
const osThreadAttr_t WH_LTE_attributes = {
	.name = "WH_LTE",
	.stack_size = 1024,
	.priority = (osPriority_t)osPriorityBelowNormal,
};

// 事件
osEventFlagsId_t WH_LTE_EventHandle;
const osEventFlagsAttr_t WH_LTE_Event_attributes = {
	.name = "WH_LTE_Event"};

extern UART_HandleTypeDef huart1;
UART_HandleTypeDef *pWHLTEUart = &huart1;



uint8_t WH_LTE_TEST_BUF[8]  ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
// uint8_t WH_LTE_TEST_BUF2[8]  ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
// uint8_t WH_LTE_TRANS_BUF[8]  ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t WH_LTE_Rcv_Buf[1024] = {0};

UART_HELPER_TypeDef *WH_LTE_uart_helper;

WH_LTE_TypeDef wh_lte = {
	WH_LTE_Init,
	WH_LTE_Port,
	WH_LTE_Test,
	WH_LTE_Start,
	WH_LTE_Stop,
	NULL,
	0,	  // state
	0,	  // data_ok
	{0},  // buf
	0,	  // size_received
	0,	  // time base
	1000, // timeout ticks,  1000ms = 1S
	0,	  // event_flag
	0,	  // wavelength_ok
	NULL, // transRecord[3]
	0,0,0,0,0,
	0,
};

int WH_LTE_Init()
{
	WH_LTE_uart_helper = UART_HELPER_Init();

	if (WH_LTE_uart_helper == NULL)
		return -1;

	// TODO 接口
	UART_HELPER_Set_Huart(WH_LTE_uart_helper, pWHLTEUart);
	UART_HELPER_Set_Interface_Type(WH_LTE_uart_helper, Uart_Interface_Max3160_485);

	//  传输
	UART_HELPER_Setup_Trans_mode(WH_LTE_uart_helper, Uart_Trans_DMA);
	// UART_HELPER_Setup_Rcv_mode( WH_LTE_uart_helper, Uart_Trans_IT );
	UART_HELPER_Set_enable_idle(WH_LTE_uart_helper, Uart_IDLE_IT_ENABLE);

	//  回调GPIO 操作 数据操作
	UART_HELPER_Set_trans_GPIO(WH_LTE_uart_helper, WH_LTE_Trans_GPIO); // enbale rcv
	UART_HELPER_Set_trans_cplt_GPIO(WH_LTE_uart_helper, WH_LTE_Trans_Cplt_GPIO);

	UART_HELPER_Set_Callback(WH_LTE_uart_helper, &wh_lte, WH_LTE_CallBack);

	// 设置 Buf, 如果启用消息队列，不启用trans_buf
	UART_HELPER_Set_Rcv_Buf(WH_LTE_uart_helper, WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf));
	UART_HELPER_Set_Trans_Buf(WH_LTE_uart_helper, NULL, 0);

	//  GPIO 操作
	switch (WH_LTE_uart_helper->interface_type)
	{
	case Uart_Interface_Default:
		break;
	case Uart_Interface_485:
		usart6_send_enable();
		break;
	case Uart_Interface_Max3160_232:
		WH_LTE_MAX3160_232();
		break;
	case Uart_Interface_Max3160_485:
		max3160_485_receive_mode();
		break;
	default:
		break;
	}
	wh_lte.state = MODULE_WH_LTE_7S4_INIT_STATE_WAIT;
	wh_lte.timebase_ticks = osKernelGetTickCount();

	// TODO enable trans queue
	// 最大队列数 8, 消息类型， 值传递（内含的buf为指针，如何包含大空间的buf,可选择指针传递 ）
	// wh_lte.transQueue = osMessageQueueNew(8, sizeof(Record_Trans_Rcv_TypeDef), NULL);
}

void WH_LTE_Port()
{
	WH_LTE_Handle = osThreadNew(WH_LTE_Task, NULL, &WH_LTE_attributes);
	// WH_LTE_EventHandle = osEventFlagsNew( &WH_LTE_Event_attributes );
}

int WH_LTE_Test()
{
	return 0;
}


void WH_LTE_Start()
{
	wh_lte.state = MODULE_WH_LTE_7S4_INIT_STATE_START; // 操作状态
}

void WH_LTE_Stop()
{
	wh_lte.state = MODULE_WH_LTE_7S4_INIT_STATE_STOP;
	// TODO stop  task？
}

void WH_LTE_Task()
{
	// log_i( " ph tran mode : %d ", wh_lte.->trans_mode );
	// memset(  WH_LTE_RCV_Buf, 0, 2*WH_LTE_Rcv_Buf_Size );  /* ph*3 ph *2 否则内存泄漏 */
	wh_lte.event_flag = 0;
	uint64_t ticks;
	int err_con = 0;
	int st;
	Record_Trans_Rcv_TypeDef msgQueue;  // 消息队列
	int queue_seq = 0;   // 队列的计数

	for (;;)
	{
		switch (wh_lte.state)
		{
            case MODULE_WH_LTE_7S4_INIT_STATE_WAIT:
				osDelay(20);
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_START:
                log_i("MODULE_WH_LTE_7S4_INIT_STATE_START ..  " );
                err_con=0;
                wh_lte.size_received = 0;
                memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                ticks = osKernelGetTickCount();
				WH_LTE_Receive();
                wh_lte_7s4_ppp( &wh_lte  );
 
                wh_lte.state = MODULE_WH_LTE_7S4_INIT_STATE_PPP;
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_PPP:
                log_d(" a .. ok... %d",  wh_lte.size_received);                          
                if( wh_lte.receive_a_flag==1 )
                {

                    wh_lte.error=0;
                    wh_lte.receive_a_flag=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
                    wh_lte_7s4_a(&wh_lte );
                  
                    wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_A;
                }
                if( (osKernelGetTickCount() - ticks) >=1000){
                    log_w( "WH LTE ppp Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte.error = 1;
                        wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_START;
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_A:
                log_i(" INIT_STATE_A ...");
                if( wh_lte.receive_ok_flag==1 )
                {
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();

                    wh_lte_7s4_at_set_workmode( &wh_lte,WH_LTE_7S4_CMD_WORK_MODE_NET );
                    wh_lte.state = MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE;
                }
                if( (osKernelGetTickCount() - ticks) >=1000){
                    log_w( "WH LTE A Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte.error = 1;
                        wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_START;
                }
				osDelay(200);
                break;    
            case MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE:
                log_i(" STATE_SET_WORKMODE ...");
				if(wh_lte.receive_ok_flag==1){
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_set_sockasl(&wh_lte,WH_LTE_7S4_CMD_SOCKASL_LONG);
					err_con=0;
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL;
				}
                if( (osKernelGetTickCount() - ticks) >=1000){
                    log_w( "WH LTE A Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte.error = 1;
                        wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_set_workmode( &wh_lte,WH_LTE_7S4_CMD_WORK_MODE_NET );
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE;
                }
				osDelay(200);
                break;        
            case MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL:
                log_i(" INIT_STATE_SET_SOCKASL .。。 ");
				if(wh_lte.receive_ok_flag==1){
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_set_socka_en(&wh_lte ,WH_LTE_7S4_CMD_STATE_ON);
					err_con=0;
					wh_lte.state=MODULE_WH_LTE_EVENT_SOCKAEN;
				}
                if( (osKernelGetTickCount() - ticks) >=1000){
                    log_w( "WH LTE A Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte.error = 1;
                        wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_set_sockasl(&wh_lte,WH_LTE_7S4_CMD_SOCKASL_LONG);
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL;
                }
                break;        
            case MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN:
				if(wh_lte.receive_ok_flag==1){
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_set_socka(&wh_lte,WH_LTE_7S4_CMD_PROTOCOL_TCP,wh_lte.server_ip_domain,strlen(wh_lte.server_ip_domain),wh_lte.server_port);
					err_con=0;
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA;
				}
                if( (osKernelGetTickCount() - ticks) >=1000){
                    log_w( "WH LTE A Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte.error = 1;
                        wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_set_socka_en(&wh_lte ,WH_LTE_7S4_CMD_STATE_ON);
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN;
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA:
				if(wh_lte.receive_ok_flag==1){
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_get_cclk(&wh_lte);
					// operating_state.err_code&=(~ERROR_CODE_MASK_WH_LTE_7S4);
					err_con=0;
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME;
				}
                if( (osKernelGetTickCount() - ticks) >=1000){
                    log_w( "WH LTE A Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte.error = 1;
                        wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_set_socka(&wh_lte,WH_LTE_7S4_CMD_PROTOCOL_TCP,wh_lte.server_ip_domain,strlen(wh_lte.server_ip_domain),wh_lte.server_port);
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA;
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME:
				if(wh_lte.receive_para_num!=0){
					log_d("Module WH-LTE-7S4 Receive Time");
					// TODO: 设置RTC 时间
					// if(set_time(&time_set_base,wh_lte_7s4.receive_para[0])!=0){
					// 	wh_lte_7s4.receive_para_num=0;
					// 	break;
					// }
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_z(&wh_lte);
					// operating_state.err_code&=(~ERROR_CODE_MASK_WH_LTE_7S4);
					err_con=0;
					// operating_state.flag_write_back=1;
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_REBOOT;
				} 
                if( (osKernelGetTickCount() - ticks) >=1000){
                    log_w( "WH LTE A Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte.error = 1;
                        wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    wh_lte.error=0;
                    wh_lte.receive_ok_flag=0;
                    wh_lte.size_received = 0;
                    memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                    ticks = osKernelGetTickCount();
					WH_LTE_Receive();
					wh_lte_7s4_at_get_cclk(&wh_lte);
					wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME;
                }
                break;  
            case MODULE_WH_LTE_7S4_INIT_STATE_REBOOT:
                //  TODO
                if(wh_let_7s4_get_work_state()==0){
                    log_i("Module WH-LTE-7S4 Init Succeed");
                    // operating_state.flag_error[ERROR_CODE_4G]=0;
                    // operating_state.net_state=1;
                    err_con=0;
                    wh_lte.state=MODULE_WH_LTE_7S4_INIT_STATE_OK;
                }
                if( (osKernelGetTickCount() - ticks) >=20000 ){
                    log_w("Module WH-LTE-7S4 Connect Failed,Retrying...");
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_OK:
                // TODO: 切换传送模式  polling  - DMA
                break;

            
            case MODULE_WH_LTE_7S4_INIT_STATE_ERROR:
                if(err_con!=0){
                    err_con=0;
                    // operating_state.flag_error[ERROR_CODE_4G]=1;
                    log_e("Module WH-LTE-7S4 Init Error");
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_STOP:
                break;
            default:
                break;
        }
		osDelay(20);
	}
}

void WH_LTE_Trans_GPIO(void)
{
	// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, SET);
	// HAL_GPIO_WritePin(NULL,0, SET);
	HAL_GPIO_WritePin(SEL_232_485_GPIO_Port, SEL_232_485_Pin, SET);
	HAL_GPIO_WritePin(HDPLX_GPIO_Port, HDPLX_Pin, RESET);
	HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, SET);
}

void WH_LTE_Trans_Cplt_GPIO(void)
{
	// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, RESET);
	// HAL_GPIO_WritePin(NULL,0, RESET);
	HAL_GPIO_WritePin(SEL_232_485_GPIO_Port, SEL_232_485_Pin, SET);
	HAL_GPIO_WritePin(HDPLX_GPIO_Port, HDPLX_Pin, SET);
	HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, RESET);
}

int WH_LTE_Transmit()
{
	//  TODO 结合队列
	wh_lte.size_received = 0;
	if (WH_LTE_uart_helper->transferring == 0)
	{
		return UART_HELPER_Trans(WH_LTE_uart_helper, WH_LTE_uart_helper->trans_record->buf, WH_LTE_uart_helper->trans_record->size);
	}
	return 0;
}

int WH_LTE_Receive()
{
	return UART_HELPER_Start_Rcv(WH_LTE_uart_helper, WH_LTE_uart_helper->rcv_buf, WH_LTE_uart_helper->rcv_size);
	// return 0;
}

void WH_LTE_Set_Timeout_ms(uint64_t ms_ticks)
{
	wh_lte.timeout_ticks = ms_ticks;
}

int WH_LTE_Validate()
{
	// return CRC16_Check(wh_lte.result_buf ,wh_lte.size_received);
}


int WH_LTE_CallBack(WH_LTE_TypeDef *pWHLTE, uint8_t *buf, uint16_t size)
{
	log_d(" WH_LTE_CallBack  -- state %d   size %d", wh_lte.state, size);
    int state;
    int i=0;
    int j=0;
	if (size == 0)  return 0;
	uint32_t size_tmp=size+wh_lte.receive_size>1024?1024-wh_lte.receive_size:size;
	memcpy(&wh_lte.receive_buf[wh_lte.receive_size],buf,size_tmp);
	wh_lte.receive_size+=size_tmp;	

    while(i<size){
        state=0;
        // uint32_t size_tmp=size+wh_lte.receive_size>1024?1024-wh_lte.receive_size:size;
        // memcpy(&wh_lte.receive_buf[wh_lte.receive_size],buf,size_tmp);
        // wh_lte.receive_size+=size_tmp;
        while(i<size){
            switch(state){
            case 0:
                if(buf[i]=='a'){
                    wh_lte.receive_a_flag=1;
                    state=0;
                    i++;
                    continue;
                }
                if(buf[i]=='\r'){
                    state=1;
                    break;
                }
                if(buf[i]=='+'){
                    state=9;
                    break;
                }
                if(buf[i]=='A'){
                    state=11;
                    break;
                }
                break;
            case 1:
                if(buf[i]=='\n'){
                    state=2;
                    break;
                }
                i--;
                state=0;
                break;
            case 2:
                if(buf[i]=='O'){
                    state=3;
                    break;
                }
                if(buf[i]=='+'){
                    state=6;
                    break;
                }
                i--;
                state=0;
                break;
            case 3:
                if(buf[i]=='K'){
                    state=4;
                    break;
                }
                i--;
                state=0;
                break;
            case 4:
                if(buf[i]=='\r'){
                    state=5;
                    break;
                }
                i--;
                state=0;
                break;
            case 5:
                if(buf[i]=='\n'){
                    wh_lte.receive_ok_flag=1;
                    state=0;
                    i++;
                    continue;
                }
                i--;
                state=0;
                break;
            case 6:
                if(buf[i]==':'){
                    state=7;
                    break;
                }
                break;
            case 7:
                if(buf[i]==','){
                    j=0;
                    wh_lte.receive_para[wh_lte.receive_para_num][j++]='\0';
                    wh_lte.receive_para_num++;
                    break;
                }
                if(buf[i]=='\r')
                {
                    wh_lte.receive_para_num++;
                    state=8;
                    break;
                }
                if(j>=32 || wh_lte.receive_para_num>=8){
                    wh_lte.receive_para_num=0;
                    state=0;
                    break;
                }
                wh_lte.receive_para[wh_lte.receive_para_num][j++]=buf[i];
                break;
            case 8:
                if(buf[i]=='\n')
                {
                    state=0;
                    break;
                }
                wh_lte.receive_para_num=0;
                break;
            case 9:
                if(buf[i]=='o'){
                    state=10;
                    break;
                }
                i--;
                state=0;
                break;
            case 10:
                if(buf[i]=='k'){
                    wh_lte.receive_ok_flag=1;
                    state=0;
                    i++;
                    continue;
                }
                i--;
                state=0;
                break;
            case 11:
                if(buf[i]=='T'){
                    state=2;
                    break;
                }
                i--;
                state=0;
                break;
            }
            i++;
        }
    }
}

/**
 * @brief 获得 Link  Power的电平状态
 * 
 * @param integer 要转换的数据
 * @param str 字符串指针
 * @param length 字符串长度
 * @return unsigned 转换的字符串长度
 */
unsigned wh_let_7s4_get_work_state(void)
{
    unsigned ret = 0;
    // ret=(GPIO_ReadInputDataBit(WH_LTE_7S4_WORK_GPIO,WH_LTE_7S4_WORK_PIN)==SET)<<1;
    // ret|=(GPIO_ReadInputDataBit(WH_LTE_7S4_LINKA_GPIO,WH_LTE_7S4_LINKA_PIN)==SET);
    return ret;
}

/**
 * @brief 将unsigned数据转换为字符串
 * 
 * @param integer 要转换的数据
 * @param str 字符串指针
 * @param length 字符串长度
 * @return unsigned 转换的字符串长度
 */
unsigned wh_lte_convert_u2s(unsigned integer,char *str,unsigned length)
{
    unsigned i;
    unsigned j=length-1;
    while(j<length){
        str[j]=integer%10+'0';
        integer/=10;
        if(integer==0)
            break;
        j--;
    }
    if(integer!=0)
        return 0;
    for(i=0;j<length;i++,j++){
        str[i]=str[j];
    }
    return i;
}