为什么通过LWIP在STM32F7和Telit GL865 GSM调制解调器之间通过PPPoS会话进行LCP协商?

时间:2018-05-10 14:30:16

标签: stm32 gsm uart lwip ppp

我正在使用Telit GL865 GSM调制解调器和STM32F7核开发板。我让它们通过USART1进行通信。首先,我用AT命令配置调制解调器如下;之后,调制解调器开始发送LCP包,由于我无法处理它们,最后会发生NO CARRIER消息。 我不确定的是,我使用正确的AT命令,因为它与我分享的链接不同: https://github.com/espressif/esp-idf/blob/master/examples/protocols/pppos_client/main/pppos_client_main.c

第二件事是,当我致电pppos_create(&ppp_netif, output_cb, status_cb, 0)时,我发现它实际上并没有致电status_cb(ppp_pcb *pcb, int err_code, void *ctx)

主要代码和必需的回调:

/////////////////////////// Includes
#include "main.h"
#include "stm32f7xx_hal.h"
#include "lwip.h"
#include "usart.h"
#include "gpio.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "string.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 "netif/ppp/pppos.h"
#include "ppp/ppp.h"
#include "lwip/sio.h"
#include "netif/ppp/pppapi.h"
#include "netif/ppp/pppos.h"
#include "netif/ppp/pppoe.h"
#include "netif/ppp/ppp_opts.h"
///////////////////////////////////////////// ///End of Includes
void SystemClock_Config(void);
#define RX_SIZE 64
///////////////////////////////////////////////////////Variables
uint32_t sysTickCounter = 0;
uint8_t sendData[50] = "                    \n";
uint8_t recData;
uint8_t receivedValue, connected = 0;
uint32_t recIndex = 0;
uint8_t recBuffer[RX_SIZE];
uint8_t pppLinkStatusCallbackArray[RX_SIZE];
uint8_t sent,sendMeToPPPoS = 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 = 0;
////////////////////////////////////////////////////////////End of Variables

////////////////////////////////////////////////////////////PPP Functions
static void status_cb(ppp_pcb *pcb, int err_code, void *ctx) {
//this function is never called; however, it must have been called in the main with ppp = pppos_create(&ppp_netif, output_cb, status_cb, 0); 
  struct netif *pppif = ppp_netif(pcb);
  LWIP_UNUSED_ARG(ctx);

  switch(err_code) {
    case PPPERR_NONE: {
      printf("status_cb: Connected\n");
      printf("   our_ipaddr  = %s\r\n", ipaddr_ntoa(&pppif->ip_addr));
      printf("   his_ipaddr  = %s\r\n", ipaddr_ntoa(&pppif->gw));
      printf("   netmask     = %s\r\n", ipaddr_ntoa(&pppif->netmask));
      connected = 1; //
      break;
    }
    case PPPERR_PARAM: {
      printf("status_cb: Invalid parameter\r\n");
      break;
    }
    case PPPERR_OPEN: {
      printf("status_cb: Unable to open PPP session\r\n");
      break;
    }
    case PPPERR_DEVICE: {
      printf("status_cb: Invalid I/O device for PPP\r\n");
      break;
    }
    case PPPERR_ALLOC: {
      printf("status_cb: Unable to allocate resources\r\n");
      break;
    }
    case PPPERR_USER: {
      ppp_free();
      printf("status_cb: User interrupt\r\n");
      break;
    }
    case PPPERR_CONNECT: {
      printf("status_cb: Connection lost\r\n");
      break;
    }
    case PPPERR_AUTHFAIL: {
      printf("status_cb: Failed authentication challenge\r\n");
      break;
    }
    case PPPERR_PROTOCOL: {
      printf("status_cb: Failed to meet protocol\r\n");
      break;
    }
    case PPPERR_PEERDEAD: {
      printf("status_cb: Connection timeout\r\n");
      break;
    }
    case PPPERR_IDLETIMEOUT: {
      printf("status_cb: Idle Timeout\r\n");
      break;
    }
    case PPPERR_CONNECTTIME: {
      printf("status_cb: Max connect time reached\r\n");
      break;
    }
    case PPPERR_LOOPBACK: {
      printf("status_cb: Loopback detected\r\n");
      break;
    }
    default: {
      printf("status_cb: Unknown error code %d\r\n", err_code);
      break;
    }
  }
  ppp_connect(pcb, 30);//Try to reconnect in 30 seconds
}

static u32_t output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
{
    //UART output callback for PPPoS
    return HAL_UART_Transmit_IT(&huart1, data, sizeof(data));
}
/////////////////////////////////////////////////////////End of PPP Functions

/////////////////////////////////////////////////////////////////////////Main
int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_USART1_UART_Init();
    MX_LWIP_Init();
    HAL_UART_Receive_IT(&huart1, &recData, 1);
    SysTick_Config(SystemCoreClock/1000); 

    HAL_Delay(300);//delay 300ms
    memset(recBuffer, 0, sizeof(recBuffer));//UART receive buffer
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT\r\n", strlen("AT\r\n"));//send this AT command over UART to GSM module
    HAL_Delay(300);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT&K0\r\n", strlen("AT&K0\r\n"));
    HAL_Delay(300);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SCFG=3,1,300,600,300,10\r\n", strlen("AT#SCFG=3,1,300,600,300,10\r\n"));
    HAL_Delay(300);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CGDCONT=1,\"IP\",\"mgbs\",\"0.0.0.0\",0,0\r\n", strlen("AT+CGDCONT=1,\"IP\",\"mgbs\",\"0.0.0.0\",0,0\r\n"));
    HAL_Delay(300);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CGSN\r\n", strlen("AT+CGSN\r\n"));
    HAL_Delay(300);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#ICMP=2\r\n", strlen("AT#ICMP=2\r\n"));
    HAL_Delay(300);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GPPPCFG=\"000.000.000.000\",25,1\r\n", strlen("AT#GPPPCFG=\"000.000.000.000\",25,1\r\n"));
    HAL_Delay(300);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GPPPCFGEXT=0\r\n", strlen("AT#GPPPCFGEXT=0\r\n"));
    HAL_Delay(300);

    HAL_Delay(3000);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GAUTH=0\r\n", strlen("AT#GAUTH=0\r\n"));
    HAL_Delay(3000);

    HAL_Delay(300);
    memset(recBuffer, 0, sizeof(recBuffer));
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"ATDT*99#\r\n", strlen("AATDT*99#\r\n"));

    ppp = pppos_create(&ppp_netif, output_cb, status_cb, 0);
    ppp_set_auth(ppp, PPPAUTHTYPE_NONE, "", "");
    ppp_set_default(ppp);

    u16_t holdoff = 0;
    err_t err = ppp_connect(ppp, holdoff);
    while(err!=ERR_OK)
    {
        HAL_Delay(100);
    }

    while (1)
    {
    }
}

/////////////////////////////////////////////////////////////UART callbacks

//////////////Fill the buffer with UART data unless 300ms past
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
    if (UartHandle->Instance == USART1)
    {
        //HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);/* Toggle LED2 on: Transfer in reception process is correct */
        sysTickCounter = 0;//reset the timeout if data is received --> for more info, go to systick_handler
        HAL_UART_Receive_IT(&huart1, &recData, 1);
        if(recIndex >= RX_SIZE){
            recIndex = 0;
        }   
        recBuffer[recIndex++] = recData;
    }
}
/////////////////////////////////////////////End of UART callbacks

//////////////below; store the received LCP packages from UART buffer to pppos_input by 
//////////////dicriminating the LCP packages with starting and ending indicators 0x7E.
void SysTick_Handler(void)
{
    sysTickCounter++;
    if(sysTickCounter == 300){   //Check the UART buffer over 300ms
        sysTickCounter = 0;
        recIndex = 0;

        for (int i = 0; i < RX_SIZE; i++){
            if(recBuffer[i] == 0x7E){
                if(lcpStart == 0){
                    lcpStart = 1;
                }else if(lcpStart == 1){
                    lcpStart = 0;
                }
            }
            if(lcpStart == 1){
                lcpBuffer[lcpBufferIndex] = recBuffer[i];
                lcpBufferIndex++;
            }
            if(lcpStart == 0){
                if(lcpStartOld == 1){
                    quantityOfPackages++;

                    lcpBuffer[lcpBufferIndex] = 0x7E;
                    length = lcpBufferIndex;
                    lcpBufferIndex = 0;

                    pppos_input(ppp, lcpBuffer, length);
                    memset(recBuffer, 0, sizeof(recBuffer));
                    memset(lcpBuffer, 0, sizeof(lcpBuffer));
                }
            }
            lcpStartOld = lcpStart;
        }
    }
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
}

我希望PPPoS处理LwIP的静态IP分配。但是,我甚至无法处理来自GSM模块的LCP消息,因为我无法通过pppos_create(&ppp_netif, output_cb, status_cb, 0);正确创建PPPoS。

调制解调器发送的所有内容包括LCP如下:

AT
AT

OK

AT&K0
AT&K0

OK
AT#SCFG=3,1,300,600,300,10
AT#SCFG=3,1,300,600,300,10

OK
AT+CGDCONT=1,"IP","mgbs","0.0.0.0",0,0
AT+CGDCONT=1,"IP","mgbs","0.0.0.0",0,0

OK
AT+CGSN
AT+CGSN

356308046338494

OK
AT#ICMP=2
AT#ICMP=2

OK
AT#GPPPCFG="000.000.000.000",25,1
AT#GPPPCFG="000.000.000.000",25,1

OK
AT#GPPPCFGEXT=0
AT#GPPPCFGEXT=0

OK
ATD*99***1
ATD*99***1

NO CARRIER
ATDT*99#
ATDT*99#

CONNECT

~ÿ}#À!}!}!} }0}"}&} } } } }%}&þQ}-} z'~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&à[}-} ã9~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&Ôe}-} H‘~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&Ío}-} ðØ~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&½y}-} ùÑ~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&­ƒ}-} }6í~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&¡}-} 9j~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&š—}-} 1}0~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&¡¡}-} ¡Ì~
~ÿ}#À!}!}!} }0}"}&} } } } }%}&Ÿ«}-} k]~
~ÿ}#À!}%}!} }*} } } } } } }*ø~
~ÿ}#À!}%}!} }*} } } } } } }*ø~
~ÿ}#À!}%}!} }*} } } } } } }*ø~

NO CARRIER

0 个答案:

没有答案