fix(lwip): Fix UDP sync send error

This commit is contained in:
Dong Heng
2018-08-28 13:34:48 +08:00
parent 4edc6f083b
commit c5d20f94f3
3 changed files with 44 additions and 34 deletions

View File

@@ -22,7 +22,7 @@
#include "esp_log.h" #include "esp_log.h"
#if ESP_UDP #if ESP_UDP && LWIP_NETIF_TX_SINGLE_PBUF
#define UDP_SYNC_MAX MEMP_NUM_NETCONN #define UDP_SYNC_MAX MEMP_NUM_NETCONN
#define UDP_SYNC_RETRY_MAX CONFIG_ESP_UDP_SYNC_RETRY_MAX #define UDP_SYNC_RETRY_MAX CONFIG_ESP_UDP_SYNC_RETRY_MAX
@@ -40,15 +40,16 @@
typedef struct udp_sync { typedef struct udp_sync {
struct api_msg *msg; struct api_msg *msg;
int ret; struct netif *netif;
int retry; int8_t ret;
uint8_t retry;
} udp_sync_t; } udp_sync_t;
static const char *TAG = "udp_sync"; static const char *TAG = "udp_sync";
static size_t s_udp_sync_num; static size_t s_udp_sync_num;
static udp_sync_t s_udp_sync[UDP_SYNC_MAX]; static udp_sync_t s_udp_sync[UDP_SYNC_MAX];
static bool s_register_locked;
static struct api_msg *s_cur_msg; static struct api_msg *s_cur_msg;
/* /*
@@ -57,7 +58,6 @@ static struct api_msg *s_cur_msg;
void udp_sync_init(void) void udp_sync_init(void)
{ {
memset(s_udp_sync, 0, sizeof(s_udp_sync)); memset(s_udp_sync, 0, sizeof(s_udp_sync));
s_register_locked = false;
s_udp_sync_num = 0; s_udp_sync_num = 0;
} }
@@ -68,18 +68,13 @@ void udp_sync_regitser(void *in_msg)
{ {
s_cur_msg = in_msg; s_cur_msg = in_msg;
if (s_register_locked == true)
return ;
struct api_msg *msg = (struct api_msg *)in_msg; struct api_msg *msg = (struct api_msg *)in_msg;
int s = msg->conn->socket; int s = msg->conn->socket;
if (s < 0 || s >= UDP_SYNC_MAX) { if (s < 0 || s >= UDP_SYNC_MAX) {
ESP_LOGE(TAG, "UDP sync register error, socket is %d", s); ESP_LOGE(TAG, "UDP sync register error, socket is %d", s);
return ;
} else if (s_udp_sync[s].msg) { } else if (s_udp_sync[s].msg) {
ESP_LOGE(TAG, "UDP sync register error, msg is %p", s_udp_sync[s].msg); ESP_LOGE(TAG, "UDP sync register error, msg is %p", s_udp_sync[s].msg);
return ;
} }
s_udp_sync_num++; s_udp_sync_num++;
@@ -88,6 +83,25 @@ void udp_sync_regitser(void *in_msg)
s_udp_sync[s].msg = msg; s_udp_sync[s].msg = msg;
} }
static void _udp_sync_ack_ret(int s, struct api_msg *msg)
{
/* Only cache when low-level has no buffer to send packet */
if (s_udp_sync[s].ret != ERR_MEM || s_udp_sync[s].retry >= UDP_SYNC_RETRY_MAX) {
ESP_LOGD(TAG, "UDP sync ret %d retry %d", s_udp_sync[s].ret, s_udp_sync[s].retry);
s_udp_sync[s].msg = NULL;
s_udp_sync[s].retry = 0;
s_udp_sync[s].ret = ERR_OK;
s_udp_sync_num--;
TCPIP_APIMSG_ACK(msg);
} else {
s_udp_sync[s].retry++;
ESP_LOGD(TAG, "UDP sync ack error, errno %d", s_udp_sync[s].ret);
}
}
/* /*
* @brief ack the message * @brief ack the message
*/ */
@@ -98,26 +112,11 @@ void udp_sync_ack(void *in_msg)
if (s < 0 || s >= UDP_SYNC_MAX) { if (s < 0 || s >= UDP_SYNC_MAX) {
ESP_LOGE(TAG, "UDP sync ack error, socket is %d", s); ESP_LOGE(TAG, "UDP sync ack error, socket is %d", s);
return ;
} else if (!s_udp_sync[s].msg) { } else if (!s_udp_sync[s].msg) {
ESP_LOGE(TAG, "UDP sync ack error, msg is NULL"); ESP_LOGE(TAG, "UDP sync ack error, msg is NULL");
return ;
} }
/* Only cache when low-level has no buffer to send packet */ _udp_sync_ack_ret(s, msg);
if (s_udp_sync[s].ret != ERR_MEM || s_udp_sync[s].retry >= UDP_SYNC_RETRY_MAX) {
s_udp_sync[s].msg = NULL;
s_udp_sync[s].retry = 0;
s_udp_sync[s].ret = ERR_OK;
s_udp_sync_num--;
ESP_LOGD(TAG, "UDP sync ret %d", s_udp_sync[s].ret);
TCPIP_APIMSG_ACK(msg);
} else {
s_udp_sync[s].retry++;
ESP_LOGD(TAG, "UDP sync ack error, errno %d", s_udp_sync[s].ret);
}
s_cur_msg = NULL; s_cur_msg = NULL;
} }
@@ -125,7 +124,7 @@ void udp_sync_ack(void *in_msg)
/* /*
* @brief set the current message send result * @brief set the current message send result
*/ */
void udp_sync_set_ret(int ret) void udp_sync_set_ret(void *netif, int ret)
{ {
/* Only poll and regitser can set current message */ /* Only poll and regitser can set current message */
if (!s_cur_msg) { if (!s_cur_msg) {
@@ -139,15 +138,28 @@ void udp_sync_set_ret(int ret)
if (s < 0 || s >= UDP_SYNC_MAX) { if (s < 0 || s >= UDP_SYNC_MAX) {
ESP_LOGE(TAG, "UDP sync ack error, socket is %d", s); ESP_LOGE(TAG, "UDP sync ack error, socket is %d", s);
return ;
} else if (!s_udp_sync[s].msg) { } else if (!s_udp_sync[s].msg) {
ESP_LOGE(TAG, "UDP sync ack error, msg is NULL"); ESP_LOGE(TAG, "UDP sync ack error, msg is NULL");
return ;
} }
s_udp_sync[s].netif = netif;
s_udp_sync[s].ret = ret; s_udp_sync[s].ret = ret;
} }
static void udp_sync_send(struct api_msg *msg)
{
struct pbuf *p = msg->msg.b->p;
int s = msg->conn->socket;
struct netif *netif = s_udp_sync[s].netif;
s_cur_msg = msg;
netif->linkoutput(netif, p);
_udp_sync_ack_ret(s, msg);
s_cur_msg = NULL;
}
/* /*
* @brief process the sync * @brief process the sync
*/ */
@@ -156,19 +168,17 @@ void udp_sync_proc(void)
if (!s_udp_sync_num) if (!s_udp_sync_num)
return ; return ;
s_register_locked = true;
for (int i = 0; i < UDP_SYNC_MAX; i++) { for (int i = 0; i < UDP_SYNC_MAX; i++) {
if (!s_udp_sync[i].msg) if (!s_udp_sync[i].msg)
continue; continue;
lwip_netconn_do_send(s_udp_sync[i].msg); udp_sync_send(s_udp_sync[i].msg);
#if 0 #if 0
//Todo: Add this later //Todo: Add this later
if (s_udp_sync[i].ret != ERR_OK) if (s_udp_sync[i].ret != ERR_OK)
break; break;
#endif #endif
} }
s_register_locked = false;
} }
#endif /* ESP_UDP */ #endif /* ESP_UDP */

View File

@@ -43,7 +43,7 @@ void udp_sync_ack(void *in_msg);
* *
* @param ret current message send result * @param ret current message send result
*/ */
void udp_sync_set_ret(int ret); void udp_sync_set_ret(void *netif, int ret);
/* /*
* @brief process the sync * @brief process the sync

View File

@@ -303,7 +303,7 @@ static int8_t low_level_output(struct netif* netif, struct pbuf* p)
*/ */
err = esp_aio_sendto(&aio, NULL, 0); err = esp_aio_sendto(&aio, NULL, 0);
#if ESP_UDP #if ESP_UDP
udp_sync_set_ret(err); udp_sync_set_ret(netif, err);
#endif #endif
if (err != ERR_OK) { if (err != ERR_OK) {
if (err == ERR_MEM){ if (err == ERR_MEM){