[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lwip-users] PPPoS LwIP Raw API Server does not call accept callback but
From: |
sarp |
Subject: |
[lwip-users] PPPoS LwIP Raw API Server does not call accept callback but clients can connect. |
Date: |
Sat, 2 Jun 2018 02:59:06 -0700 (MST) |
I am using Telit GL865 GSM module and implemented PPPoS.
Then I used the LwIP_TCP_Echo_Server example and successfully initialized
the server.
On the client side, it seems that they can connect to my server but,
my server does not call tcp_echoserver_accept callback function.
Server state stucks in the LISTEN state.
I am using free RTOS but doing things in one thread.
Here is the code:
* Includes
------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
/* USER CODE BEGIN Includes */
#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 "pppapi.h"
#include <lwip/sockets.h>
#include "usart.h"
#include "test.h"
#define RX_SIZE 4096
#define SERVER_PORT 502
/* USER CODE END Includes */
/* Variables
-----------------------------------------------------------------*/
osThreadId defaultTaskHandle;
osThreadId serverTaskHandle;
/* USER CODE BEGIN Variables */
uint32_t sysTickCounter, runTimeCnt, recIndex = 0;
uint8_t recData, called, connectionStatus, send, dataReceived, gsmInitEnd
=0;
uint8_t recBuffer[RX_SIZE];
ppp_pcb *ppp;
struct netif ppp_netif;
static struct tcp_pcb *tcp_echoserver_pcb;
/* ECHO protocol states */
enum tcp_echoserver_states{
ES_NONE = 0,
ES_ACCEPTED,
ES_RECEIVED,
ES_CLOSING
};
struct tcp_echoserver_struct{
u8_t state; /* current connection state */
u8_t retries;
struct tcp_pcb *pcb; /* pointer on the current tcp_pcb */
struct pbuf *p; /* pointer on the received/to be transmitted pbuf
*/
};
/* USER CODE END Variables */
/* Function prototypes
-------------------------------------------------------*/
void StartDefaultTask(void const * argument);
void startServerTask(void const * argument);
extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* USER CODE BEGIN FunctionPrototypes */
void gsmInitAT(void);
static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t
err);
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct
pbuf *p, err_t err);
static void tcp_echoserver_error(void *arg, err_t err);
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb);
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t
len);
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct
tcp_echoserver_struct *es);
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct
tcp_echoserver_struct *es);
void tcp_echoserver_init(void){
/* create new tcp pcb */
tcp_echoserver_pcb = tcp_new();
if (tcp_echoserver_pcb != NULL)
{
err_t err;
/* bind echo_pcb to port 502 (ECHO protocol) */
err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY , SERVER_PORT);//(const
ip_addr_t *)SERVER_IP
if (err == ERR_OK)
{
/* start tcp listening for echo_pcb */
tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);
/* initialize LwIP tcp_accept callback function */
tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept);
}
else
{
/* deallocate the pcb */
memp_free(MEMP_TCP_PCB, tcp_echoserver_pcb);
}
}
}
static void status_cb(ppp_pcb *pcb, int err_code, void *ctx) {
struct netif *pppif = ppp_netif(pcb);
called = 1;
//LWIP_UNUSED_ARG(ctx);
switch(err_code) {
case PPPERR_NONE: {
connectionStatus = 1;
printf("status_cb: Connected\n");
printf(" our_ipaddr = %s\r\n", ipaddr_ntoa(&pppif->ip_addr));
printf(" gatewayaddr = %s\r\n", ipaddr_ntoa(&pppif->gw));
printf(" netmask = %s\r\n", ipaddr_ntoa(&pppif->netmask));
break;
}
case PPPERR_PARAM: {
connectionStatus = 2;
printf("status_cb: Invalid parameter\r\n");
break;
}
case PPPERR_OPEN: {
connectionStatus = 3;
printf("status_cb: Unable to open PPP session\r\n");
break;
}
case PPPERR_DEVICE: {
connectionStatus = 4;
printf("status_cb: Invalid I/O device for PPP\r\n");
break;
}
case PPPERR_ALLOC: {
connectionStatus = 5;
printf("status_cb: Unable to allocate resources\r\n");
break;
}
case PPPERR_USER: {
connectionStatus = 6;
ppp_free(pcb);
printf("status_cb: User interrupt\r\n");
break;
}
case PPPERR_CONNECT: {
connectionStatus = 7;
printf("status_cb: Connection lost\r\n");
break;
}
case PPPERR_AUTHFAIL: {
connectionStatus = 8;
printf("status_cb: Failed authentication challenge\r\n");
break;
}
case PPPERR_PROTOCOL: {
connectionStatus = 9;
printf("status_cb: Failed to meet protocol\r\n");
break;
}
case PPPERR_PEERDEAD: {
connectionStatus = 10;
printf("status_cb: Connection timeout\r\n");
break;
}
case PPPERR_IDLETIMEOUT: {
connectionStatus = 11;
printf("status_cb: Idle Timeout\r\n");
break;
}
case PPPERR_CONNECTTIME: {
connectionStatus = 12;
printf("status_cb: Max connect time reached\r\n");
break;
}
case PPPERR_LOOPBACK: {
connectionStatus = 13;
printf("status_cb: Loopback detected\r\n");
break;
}
default: {
connectionStatus = 14;
printf("status_cb: Unknown error code %d\r\n", err_code);
break;
}
}
}
static u32_t output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx){
printf("sent: %s with length: %d\r\n", data, len);
return HAL_UART_Transmit_IT(&huart1, data, len);
}
/* USER CODE END FunctionPrototypes */
/* 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);
/* definition and creation of serverTask */
osThreadDef(serverTask, startServerTask, osPriorityNormal, 0, 1024);
serverTaskHandle = osThreadCreate(osThread(serverTask), NULL);
}
/* StartDefaultTask function */
void StartDefaultTask(void const * argument)
{
/* init code for LWIP */
MX_LWIP_Init();
/* USER CODE BEGIN StartDefaultTask */
HAL_UART_Receive_IT(&huart1, &recData, 1);
gsmInitAT();
//Now init 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);
pppapi_connect(ppp,0);
for(;;)
{
if(gsmInitEnd){
if(send){
send = 0;
pppos_input_tcpip(ppp, recBuffer, recIndex);
printf("received: %s with length: %d\r\n", recBuffer,
recIndex);
recIndex = 0;
memset(recBuffer, 0, sizeof(recBuffer));
runTimeCnt++;
}
if(connectionStatus == 1){
osDelay(2000);
tcp_echoserver_init();
connectionStatus = 2;
}
}
}
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Application */
void gsmInitAT(void){
HAL_Delay(500);
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT\r\n", strlen("AT\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT&K0\r\n",
strlen("AT&K0\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
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(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SLED=2,2,2\r\n",
strlen("AT#SLED=2,2,2\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SLEDSAV\r\n",
strlen("AT#SLEDSAV\r\n"));
HAL_Delay(500);
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\r\n",
strlen("AT+CGDCONT=1,\"IP\",\"mgbs\",\"0.0.0.0\",0,0\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CGSN\r\n",
strlen("AT+CGSN\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#ICMP=2\r\n",
strlen("AT#ICMP=2\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CSQ\r\n",
strlen("AT+CSQ\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CCLK?\r\n",
strlen("AT+CCLK?\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
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(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GPPPCFGEXT=0\r\n",
strlen("AT#GPPPCFGEXT=0\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GAUTH=0\r\n",
strlen("AT#GAUTH=0\r\n"));
HAL_Delay(500);
memset(recBuffer, 0, sizeof(recBuffer));
recIndex = 0;
HAL_UART_Transmit_IT(&huart1, (uint8_t*)"ATDT*99***1#\r\n",
strlen("ATDT*99***1#\r\n"));
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle){
if (UartHandle->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1, &recData, 1);
if(recIndex >= RX_SIZE){
recIndex = 0;
}
dataReceived = 1;
sysTickCounter = 0;
send = 0;
if(recData == 0x7E)
{
if(gsmInitEnd == 0){
recIndex = 0;
memset(recBuffer, 0, sizeof(recBuffer));
}
gsmInitEnd = 1;
}
recBuffer[recIndex++] = recData;
}
}
static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t
err){
connectionStatus = 17;
err_t ret_err;
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(err);
/* set priority for the newly accepted tcp connection newpcb */
tcp_setprio(newpcb, TCP_PRIO_MIN);
/* allocate structure es to maintain tcp connection informations */
es = (struct tcp_echoserver_struct *)mem_malloc(sizeof(struct
tcp_echoserver_struct));
if (es != NULL)
{
es->state = ES_ACCEPTED;
es->pcb = newpcb;
es->retries = 0;
es->p = NULL;
/* pass newly allocated es structure as argument to newpcb */
tcp_arg(newpcb, es);
/* initialize lwip tcp_recv callback function for newpcb */
tcp_recv(newpcb, tcp_echoserver_recv);
/* initialize lwip tcp_err callback function for newpcb */
tcp_err(newpcb, tcp_echoserver_error);
/* initialize LwIP tcp_sent callback function
*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
tcp_sent(newpcb, tcp_echoserver_sent);
/* initialize lwip tcp_poll callback function for newpcb */
tcp_poll(newpcb, tcp_echoserver_poll, 4);
ret_err = ERR_OK;
}
else
{
/* close tcp connection */
tcp_echoserver_connection_close(newpcb, es);
/* return memory error */
ret_err = ERR_MEM;
}
return ret_err;
}
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct
pbuf *p, err_t err){
struct tcp_echoserver_struct *es;
err_t ret_err;
LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct tcp_echoserver_struct *)arg;
/* if we receive an empty tcp frame from client => close connection */
if (p == NULL)
{
/* remote host closed connection */
es->state = ES_CLOSING;
if(es->p == NULL)
{
/* we're done sending, close connection */
tcp_echoserver_connection_close(tpcb, es);
}
else
{
/* we're not done yet */
/* acknowledge received packet */
tcp_sent(tpcb, tcp_echoserver_sent);
/* send remaining data*/
tcp_echoserver_send(tpcb, es);
}
ret_err = ERR_OK;
}
/* else : a non empty frame was received from client but for some reason
err != ERR_OK */
else if(err != ERR_OK)
{
/* free received pbuf*/
if (p != NULL)
{
es->p = NULL;
pbuf_free(p);
}
ret_err = err;
}
else if(es->state == ES_ACCEPTED)
{
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
/* store reference to incoming pbuf (chain) */
es->p = p;
/* initialize LwIP tcp_sent callback function */
tcp_sent(tpcb, tcp_echoserver_sent);
/* send back the received data (echo) */
tcp_echoserver_send(tpcb, es);
ret_err = ERR_OK;
}
else if (es->state == ES_RECEIVED)
{
/* more data received from client and previous data has been already
sent*/
if(es->p == NULL)
{
es->p = p;
/* send back received data */
tcp_echoserver_send(tpcb, es);
}
else
{
struct pbuf *ptr;
/* chain pbufs to the end of what we recv'ed previously */
ptr = es->p;
pbuf_chain(ptr,p);
}
ret_err = ERR_OK;
}
else if(es->state == ES_CLOSING)
{
/* odd case, remote side closing twice, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
else
{
/* unkown es->state, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}
static void tcp_echoserver_error(void *arg, err_t err){
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(err);
es = (struct tcp_echoserver_struct *)arg;
if (es != NULL)
{
/* free es structure */
mem_free(es);
}
}
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb){
err_t ret_err;
struct tcp_echoserver_struct *es;
es = (struct tcp_echoserver_struct *)arg;
if (es != NULL)
{
if (es->p != NULL)
{
tcp_sent(tpcb, tcp_echoserver_sent);
/* there is a remaining pbuf (chain) , try to send data */
tcp_echoserver_send(tpcb, es);
}
else
{
/* no remaining pbuf (chain) */
if(es->state == ES_CLOSING)
{
/* close tcp connection */
tcp_echoserver_connection_close(tpcb, es);
}
}
ret_err = ERR_OK;
}
else
{
/* nothing to be done */
tcp_abort(tpcb);
ret_err = ERR_ABRT;
}
return ret_err;
}
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t
len){
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(len);
es = (struct tcp_echoserver_struct *)arg;
es->retries = 0;
if(es->p != NULL)
{
/* still got pbufs to send */
tcp_sent(tpcb, tcp_echoserver_sent);
tcp_echoserver_send(tpcb, es);
}
else
{
/* if no more data to send and client closed connection*/
if(es->state == ES_CLOSING)
tcp_echoserver_connection_close(tpcb, es);
}
return ERR_OK;
}
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct
tcp_echoserver_struct *es){
struct pbuf *ptr;
err_t wr_err = ERR_OK;
while ((wr_err == ERR_OK) &&
(es->p != NULL) &&
(es->p->len <= tcp_sndbuf(tpcb)))
{
/* get pointer on pbuf from es structure */
ptr = es->p;
/* enqueue data for transmission */
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
// wr_err = tcp_write(tpcb, buffer_send, sizeof(buffer_send),
1);////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (wr_err == ERR_OK)
{
u16_t plen;
u8_t freed;
plen = ptr->len;
/* continue with next pbuf in chain (if any) */
es->p = ptr->next;
if(es->p != NULL)
{
/* increment reference count for es->p */
pbuf_ref(es->p);
}
/* chop first pbuf from chain */
do
{
/* try hard to free pbuf */
freed = pbuf_free(ptr);
}
while(freed == 0);
/* we can read more data now */
tcp_recved(tpcb, plen);
}
else if(wr_err == ERR_MEM)
{
/* we are low on memory, try later / harder, defer to poll */
es->p = ptr;
}
else
{
/* other problem ?? */
}
}
}
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct
tcp_echoserver_struct *es){
/* remove all callbacks */
tcp_arg(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_recv(tpcb, NULL);
tcp_err(tpcb, NULL);
tcp_poll(tpcb, NULL, 0);
/* delete es structure */
if (es != NULL)
{
mem_free(es);
}
/* close tcp connection */
tcp_close(tpcb);
}
/* USER CODE END Application */
--
Sent from: http://lwip.100.n7.nabble.com/lwip-users-f3.html
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [lwip-users] PPPoS LwIP Raw API Server does not call accept callback but clients can connect.,
sarp <=