为什么PPP over Serial无法回答ping请求?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么PPP over Serial无法回答ping请求?相关的知识,希望对你有一定的参考价值。

我在与STM32F7 MCU和软件STM32CubeMX,Keil的UART通信中使用GSM模块Telit GL865。

当我如下所示通过UART向GSM模块发送AT命令时,我通过GSM模块的内部堆栈获得了IP地址并成功ping它:

AT
AT&K0
AT#SCFG=3,1,300,600,300,10
AT+CGDCONT=1,"IP","mgbs","0.0.0.0",0,0
AT+CGSN
AT#ICMP=2
AT#GPPPCFG="000.000.000.000",25,1
AT#GPPPCFGEXT=0
ATDT*99#
CONNECT

但是,当我开始PPPoS会话时,我无法ping通通过PPP成功建立的IP。

通过PPP堆栈建立的IP,网关和网络掩码地址如下:

IP      = 5.26.61.173
Gateway = 192.168.202.0
Netmask = 255.255.255.255

我查看的第一个有问题的东西是网络掩码地址;但是,LwIP PPPoS对此进行了规定。

其次,建立之后,MCU和GSM模块开始互相发送周期性数据作为"~y ",分别为长度49和53。

这意味着我对printf( )的调试无法向我显示所有定期数据。

第三,最重要,当我对上述GSM的IP执行ping操作时,MCU与GSM之间会以更多的时间发送长度为68的周期性数据"~y ",这意味着ping触发了通信在GSM和MCU之间,但无响应。

我使用基于中断的UART收发,它将开始填充接收到的缓冲区并将其放入pppos_input_tcpip(ppp, recBuffer, recIndex),除非新数据在10ms内到来。

主线程,定时器中断如下:

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "string.h"
#include "main.h"
#include "stm32f7xx_hal.h"
#include "cmsis_os.h"
#include "lwip.h"
#include "usart.h"
#include "gpio.h"
#include <stdio.h>
#include "string.h"
#include <time.h>
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/timeouts.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/init.h"
#include "lwip/tcpip.h"
#include "lwip/netif.h"
#include "lwip/api.h"
#include "lwip/tcp.h"
#include "lwip/udp.h"
#include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/autoip.h"
#include "lwip/etharp.h"
#include "netif/ethernet.h"
#include "lwip/apps/netbiosns.h"
#include "lwip/apps/httpd.h"
#include "lwip/sio.h"
//#include "ppp/ppp.h"
//#include "netif/ppp/pppapi.h"
//#include "netif/ppp/pppos.h"
#include "pppapi.h"
#include <lwip/sockets.h> 
#include "usart.h"
#include "test.h"

#define RX_SIZE 4096
/* USER CODE END Includes */

/* Variables -----------------------------------------------------------------*/
osThreadId defaultTaskHandle;

/* USER CODE BEGIN Variables */
uint8_t lcpBuffer[RX_SIZE];
uint8_t lcpBufferIndex, quantityOfPackages = 0;
uint8_t pppBuffer[4096];
uint32_t sysTickCounter = 0;
uint8_t sendData[50] = "                    
";
uint8_t recData, recDataOlder, recDataOlder2,recDataOlder3,recDataOlder4,recDataOlder5,recDataOlder6;
uint8_t receivedValue,connected,called = 0;
uint32_t recIndex = 0;
uint8_t recBuffer[RX_SIZE];
uint8_t sent, send, connect, connectionStatus, endOfLcpNegotiation, NCPNegotiation = 0;
ppp_pcb *ppp;
struct netif ppp_netif;
u8_t sio_idx = 0;
sio_fd_t ppp_sio;
uint8_t ATconnected,lcpCame,length = 0;
uint8_t lcpStartIndex, lcpStopIndex, lcpStartOld, lcpStart, lcpStop, dataReceived, timeOut, fillThePPPBuffer, gsmInitEnd, sendLCP, sendNCP, socketOpened, socketBind, socketOpen = 0;
uint32_t pppBufferIndex = 0;
ip4_addr_t addr;
int socketStatus;
int socket_fd,accept_fd;
int addr_size,sent_data; char data_buffer[80]; 
struct sockaddr_in sa,ra,isa;
uint32_t runTimeCnt = 0;

void StartDefaultTask(void const * argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

//PPP function
static void status_cb(ppp_pcb *pcb, int err_code, void *ctx) {
    struct netif *pppif = ppp_netif(pcb);
    called = 1;

    switch(err_code) {
        case PPPERR_NONE: {
            connectionStatus = 1;
            printf("status_cb: Connected
");
            printf("   our_ipaddr  = %s
", ipaddr_ntoa(&pppif->ip_addr));
            printf("   gatewayaddr = %s
", ipaddr_ntoa(&pppif->gw));
            printf("   netmask     = %s
", ipaddr_ntoa(&pppif->netmask));
            break;
        }
        case PPPERR_PARAM: {
            connectionStatus = 2;
            printf("status_cb: Invalid parameter
");
            break;
        }
        case PPPERR_OPEN: {
            connectionStatus = 3;
            printf("status_cb: Unable to open PPP session
");
            break;
        }
        case PPPERR_DEVICE: {
            connectionStatus = 4;
            printf("status_cb: Invalid I/O device for PPP
");
            break;
        }
        case PPPERR_ALLOC: {
            connectionStatus = 5;
            printf("status_cb: Unable to allocate resources
");
            break;
        }
        case PPPERR_USER: {
            connectionStatus = 6;
            ppp_free(pcb);
            printf("status_cb: User interrupt
");
            break;
        }
        case PPPERR_CONNECT: {
            connectionStatus = 7;
            printf("status_cb: Connection lost
");
            break;
        }
        case PPPERR_AUTHFAIL: {
            connectionStatus = 8;
            printf("status_cb: Failed authentication challenge
");
            break;
        }
        case PPPERR_PROTOCOL: {
            connectionStatus = 9;
            printf("status_cb: Failed to meet protocol
");
            break;
        }
        case PPPERR_PEERDEAD: {
            connectionStatus = 10;
            printf("status_cb: Connection timeout
");
            break;
        }
        case PPPERR_IDLETIMEOUT: {
            connectionStatus = 11;
            printf("status_cb: Idle Timeout
");
            break;
        }
        case PPPERR_CONNECTTIME: {
            connectionStatus = 12;
            printf("status_cb: Max connect time reached
");
            break;
        }
        case PPPERR_LOOPBACK: {
            connectionStatus = 13;
            printf("status_cb: Loopback detected
");
            break;
        }
        default: {
            connectionStatus = 14;
            printf("status_cb: Unknown error code %d
", err_code);
            break;
        }
    }
}
//PPP function
static u32_t output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx){
    printf("sent:     %s with length: %d
",data,len);
    return HAL_UART_Transmit_IT(&huart1, data, len);
}

/* Init FreeRTOS */
void MX_FREERTOS_Init(void) {
  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 1024);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
}

/* StartDefaultTask function */
void StartDefaultTask(void const * argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();

  /* USER CODE BEGIN StartDefaultTask */
    //SysTick_Config(SystemCoreClock/1000); 
    HAL_UART_Receive_IT(&huart1, &recData, 1);

    // Start of AT commands

    HAL_Delay(1000);    
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT
", strlen("AT
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT&K0
", strlen("AT&K0
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SCFG=3,1,300,600,300,10
", strlen("AT#SCFG=3,1,300,600,300,10
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SLED=2,2,2
", strlen("AT#SLED=2,2,2
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SLEDSAV
", strlen("AT#SLEDSAV
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CGDCONT=1,"IP","mgbs","0.0.0.0",0,0
", strlen("AT+CGDCONT=1,"IP","mgbs","0.0.0.0",0,0
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CGSN
", strlen("AT+CGSN
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#ICMP=2
", strlen("AT#ICMP=2
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CSQ
", strlen("AT+CSQ
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CCLK?
", strlen("AT+CCLK?
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT?
", strlen("AT#SGACT?
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT=1,0
", strlen("AT#SGACT=1,0
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT=1,1
", strlen("AT#SGACT=1,1
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT?
", strlen("AT#SGACT?
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#FRWL=1,"10.0.1.1","255.255.0.0"
", strlen("AT#FRWL=1,"10.0.1.1","255.255.0.0"
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GPPPCFG="000.000.000.000",25,1
", strlen("AT#GPPPCFG="000.000.000.000",25,1
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GPPPCFGEXT=0
", strlen("AT#GPPPCFGEXT=0
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GAUTH=0
", strlen("AT#GAUTH=0
"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"ATDT*99***1#
", strlen("ATDT*99***1#
"));
    //End of AT commands

    //Start PPP
    ppp = pppapi_pppos_create(&ppp_netif, output_cb, status_cb, NULL);
    if (ppp == NULL) {
        printf("Error init pppos");
        return;
    }
    pppapi_set_default(ppp);
    ppp_set_auth(ppp, PPPAUTHTYPE_NONE, "", "");
    pppapi_listen(ppp);


    for(;;)
    {
        //if GSM is initialized, give data to ppp if stops coming after 10ms, 
        //send flag is set by systick(10ms) and gsmInitEnd is set after LCP session starts

        if(gsmInitEnd){
            if(send){
                send = 0;
                pppos_input_tcpip(ppp, recBuffer, recIndex);
                printf("received: %s with length: %d
", recBuffer, recIndex);
                recIndex = 0;
                runTimeCnt++;
                memset(recBuffer, 0, sizeof(recBuffer));
            }
        }
    }
}

// UART rx callback, called over 1 data received
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
    if (UartHandle->Instance == USART1)
    {
        //callback reset
        HAL_UART_Receive_IT(&huart1, &recData, 1);

        //receive buffer overflow check
        if(recIndex >= RX_SIZE){
            recIndex = 0;
        }   

        //3 variables below are externed to send data if stops coming to
        //buffer after 10ms with Systick interrupt later than GSM initialization
        dataReceived = 1;    
        sysTickCounter = 0;
        send = 0;

        //LCP packets start with 0x7E, that means the end of GSM initialization.
        if(recData == 0x7E) 
        {
            if(gsmInitEnd == 0){
                recIndex = 0;
                memset(recBuffer, 0, sizeof(recBuffer));
            }
            gsmInitEnd = 1;
        }
        recBuffer[recIndex++] = recData;
    }
}

Systick处理程序,它检查RX缓冲区并设置发送标志,以在数据在10ms之后停止时将数据发送到ppp:

void SysTick_Handler(void)
{
    if(dataReceived){
        sysTickCounter++;
        if (sysTickCounter >= 10)
        {
            send = 1;
            dataReceived = 0;
            sysTickCounter = 0;
        }
    }
  osSystickHandler();
}

GSM和STM32之间的数据通信对我来说很正常(!),并通过ping请求触发;但是,没有ping响应。

我认为,问题可能与ppp_netifppp_netif(ppp)之间的连接,LwIP堆栈的网络掩码或ping实现...

答案

答案是激活STM32CubeMX的传出和传入ICMP校验和选项...因此,激活LWIP_ICMP模块本身不足以响应ping请求。

另一答案

如果为PPPOS启用软件校验和,则以太网会中断,因为STM32在以太网上的硬件中执行它们-GRRR

以上是关于为什么PPP over Serial无法回答ping请求?的主要内容,如果未能解决你的问题,请参考以下文章

十PPP认证

arduino新手求教怎么连接SD_CardV1.2

无法使用 qtserial 将数据写入 Arduino

PPP 抓包分析

6红外遥控程序

树梅派 配置 pin8 pin10(Tx, Rx) 串口