Skip to content

Commit

Permalink
Fix receive length
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Sep 17, 2022
1 parent 90a7978 commit 2ade6a9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
16 changes: 8 additions & 8 deletions lwesp/src/include/lwesp/lwesp_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,16 @@ typedef struct lwesp_msg {

struct {
lwesp_conn_t* conn; /*!< Connection handle */
size_t len; /*!< Number of bytes to read */
lwesp_pbuf_p buff; /*!< Buffer handle */
size_t len; /*!< Number of bytes to read from device */
lwesp_pbuf_p buff; /*!< Buffer handle to write received data to */
uint8_t ipd_recv; /*!< Status indicating `+IPD` has been received during `AT+CIPRECVLEN` command.
When this happens, we need to repeat same command */
uint8_t is_last_check; /*!< Status indicating check for data length is at the end of command.
Do nothing after successful command */
uint8_t read; /*!< Set to 1 when in data read mode */
size_t tot_len; /*!< Total length expected for this read operation */
When this happens, we will repeat the length reading again */
uint8_t is_last_check; /*!< When set to `1`, reading has been completed already
and CIPRECVLEN is read again for new status */
uint8_t read; /*!< Set to 1 when in raw data read mode */
size_t tot_len; /*!< Total length expected for this read operation (actual data len received from device) */
size_t buff_ptr; /*!< Buffer pointer to save data to (next character) */
} conn_recv; /*!< Structure to manually read TCP data */
} conn_recv; /*!< Structure to manually read TCP data */

/* TCP/IP based commands */
struct {
Expand Down
32 changes: 15 additions & 17 deletions lwesp/src/lwesp/lwesp_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,8 @@ lwespi_parse_received(lwesp_recv_t* rcv) {

/* Read and process statements starting with '+' character */
if (rcv->data[0] == '+') {
if (!strncmp("+IPD", rcv->data, 4)) { /* Check received network data */
lwesp_conn_p c = lwespi_parse_ipd(rcv->data); /* Parse IPD statement and start receiving network data */
if (!strncmp("+IPD", rcv->data, 4)) { /* Check received network data */
lwesp_conn_p c = lwespi_parse_ipd(rcv->data); /* Parse IPD statement and start receiving network data */

if (CMD_IS_DEF(LWESP_CMD_TCPIP_CIPRECVDATA) && CMD_IS_CUR(LWESP_CMD_TCPIP_CIPRECVLEN)) {
esp.msg->msg.conn_recv.ipd_recv = 1; /* Command repeat, try again later */
Expand Down Expand Up @@ -713,11 +713,10 @@ lwespi_parse_received(lwesp_recv_t* rcv) {
* - Connection is active and
* - Connection is not in closing mode
*/
if (esp.msg->msg.conn_recv.conn->status.f.active
&& !esp.msg->msg.conn_recv.conn->status.f.in_closing) {
if (esp.msg->msg.conn_recv.conn->status.f.active && !esp.msg->msg.conn_recv.conn->status.f.in_closing) {
LWESP_DEBUGW(LWESP_CFG_DBG_IPD | LWESP_DBG_TYPE_TRACE | LWESP_DBG_LVL_WARNING,
esp.msg->msg.conn_recv.buff == NULL,
"[LWESP IPD] No buffer allocated for %d byte(s)\r\n", (int)len);
esp.msg->msg.conn_recv.buff == NULL,
"[LWESP IPD] No buffer allocated for %d byte(s)\r\n", (int)len);

/* Update available data */
if (esp.msg->msg.conn_recv.buff != NULL) {
Expand All @@ -727,10 +726,9 @@ lwespi_parse_received(lwesp_recv_t* rcv) {
} else {
esp.msg->msg.conn_recv.conn->tcp_available_bytes = 0;
esp.msg->msg.conn_recv.conn->tcp_available_bytes = 0;
LWESP_DEBUGF(
LWESP_CFG_DBG_IPD | LWESP_DBG_TYPE_TRACE,
"[LWESP IPD] Connection %u, setting tcp_available_bytes to zero\r\n",
(unsigned)esp.msg->msg.conn_recv.conn->num);
LWESP_DEBUGF(LWESP_CFG_DBG_IPD | LWESP_DBG_TYPE_TRACE,
"[LWESP IPD] Connection %u, setting tcp_available_bytes to zero\r\n",
(unsigned)esp.msg->msg.conn_recv.conn->num);
}
}
} else {
Expand All @@ -739,15 +737,15 @@ lwespi_parse_received(lwesp_recv_t* rcv) {
}
esp.msg->msg.conn_recv.buff = NULL; /* Ignore reading on closed connection */
LWESP_DEBUGF(LWESP_CFG_DBG_IPD | LWESP_DBG_TYPE_TRACE,
"[LWESP IPD] Connection %u closed or in closing. skipping %u byte(s)\r\n",
(unsigned)esp.msg->msg.conn_recv.conn->num, (unsigned)len);
"[LWESP IPD] Connection %u closed or in closing. skipping %u byte(s)\r\n",
(unsigned)esp.msg->msg.conn_recv.conn->num, (unsigned)len);
}
esp.msg->msg.conn_recv.conn->status.f.data_received = 1; /* We have first received data */
} else {
esp.msg->msg.conn_recv.conn->tcp_available_bytes = 0;
LWESP_DEBUGF(LWESP_CFG_DBG_IPD | LWESP_DBG_TYPE_TRACE,
"[LWESP IPD] Connection %u, setting tcp_available_bytes to zero\r\n",
(unsigned)esp.msg->msg.conn_recv.conn->num);
"[LWESP IPD] Connection %u, setting tcp_available_bytes to zero\r\n",
(unsigned)esp.msg->msg.conn_recv.conn->num);
}
} else if (!strncmp("+CIPRECVLEN", rcv->data, 11)) {
lwespi_parse_ciprecvlen(rcv->data); /* Parse CIPRECVLEN statement */
Expand Down Expand Up @@ -1325,7 +1323,7 @@ lwespi_process(const void* data, size_t data_len) {
if (CMD_IS_CUR(LWESP_CMD_TCPIP_CIPRECVDATA) && esp.msg->msg.conn_recv.read) {
size_t len;

if (esp.msg->msg.conn_recv.buff != NULL) { /* Do we have active buffer? */
if (esp.msg->msg.conn_recv.buff != NULL) { /* Do we have active buffer? */
esp.msg->msg.conn_recv.buff->payload[esp.msg->msg.conn_recv.buff_ptr] = ch; /* Save data character */
}
++esp.msg->msg.conn_recv.buff_ptr;
Expand All @@ -1342,8 +1340,8 @@ lwespi_process(const void* data, size_t data_len) {
LWESP_DEBUGF(LWESP_CFG_DBG_IPD | LWESP_DBG_TYPE_TRACE, "[LWESP IPD] Bytes skipped: %d\r\n",
(int)len);
}
d_len -= len; /* Decrease effective length */
d += len; /* Skip remaining length */
d_len -= len; /* Decrease effective length */
d += len; /* Skip remaining length */
esp.msg->msg.conn_recv.buff_ptr += len; /* Forward buffer pointer */
}

Expand Down
2 changes: 1 addition & 1 deletion lwesp/src/system/lwesp_ll_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ configure_uart(uint32_t baudrate) {
* List of COM ports to probe for ESP devices
* This may be different on your computer
*/
static const char* com_port_names[] = {"\\\\.\\COM16", "\\\\.\\COM4", "\\\\.\\COM9", "\\\\.\\COM10"};
static const char* com_port_names[] = {"\\\\.\\COM17", "\\\\.\\COM4", "\\\\.\\COM8", "\\\\.\\COM9", "\\\\.\\COM10"};

/* Try to open one of listed COM ports */
if (!initialized) {
Expand Down
41 changes: 21 additions & 20 deletions snippets/netconn_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
/**
* \brief Host and port settings
*/
#define NETCONN_HOST "example.com"
#define NETCONN_PORT 80
#define NETCONN_HOST "example.com"
#define NETCONN_PORT 80

/**
* \brief Request header to send on successful connection
*/
static const char
request_header[] = ""
"GET / HTTP/1.1\r\n"
"Host: " NETCONN_HOST "\r\n"
"Connection: close\r\n"
"\r\n";
static const char request_header[] = ""
"GET / HTTP/1.1\r\n"
"Host: " NETCONN_HOST "\r\n"
"Connection: close\r\n"
"\r\n";

/**
* \brief Netconn client thread implementation
Expand All @@ -36,7 +35,7 @@ netconn_client_thread(void const* arg) {
lwespr_t res;
lwesp_pbuf_p pbuf;
lwesp_netconn_p client;
lwesp_sys_sem_t* sem = (void*)arg;
lwesp_sys_sem_t* sem = (void*)arg;

/* Make sure we are connected to access point first */
while (!lwesp_sta_has_ip()) {
Expand All @@ -58,13 +57,13 @@ lwesp_sys_sem_t* sem = (void*)arg;
* Function will block thread until we are successfully connected (or not) to server
*/
res = lwesp_netconn_connect(client, NETCONN_HOST, NETCONN_PORT);
if (res == lwespOK) { /* Are we successfully connected? */
if (res == lwespOK) { /* Are we successfully connected? */
printf("Connected to " NETCONN_HOST "\r\n");
res = lwesp_netconn_write(client, request_header, sizeof(request_header) - 1); /* Send data to server */
res = lwesp_netconn_write(client, request_header, sizeof(request_header) - 1); /* Send data to server */
if (res == lwespOK) {
res = lwesp_netconn_flush(client); /* Flush data to output */
res = lwesp_netconn_flush(client); /* Flush data to output */
}
if (res == lwespOK) { /* Were data sent? */
if (res == lwespOK) { /* Were data sent? */
printf("Data were successfully sent to server\r\n");

/*
Expand All @@ -84,23 +83,25 @@ lwesp_sys_sem_t* sem = (void*)arg;
* was closed too early from remote side
*/
res = lwesp_netconn_receive(client, &pbuf);
if (res == lwespCLOSED) { /* Was the connection closed? This can be checked by return status of receive function */
if (res
== lwespCLOSED) { /* Was the connection closed? This can be checked by return status of receive function */
printf("Connection closed by remote side...\r\n");
break;
} else if (res == lwespTIMEOUT) {
printf("Netconn timeout while receiving data. You may try multiple readings before deciding to close manually\r\n");
printf("Netconn timeout while receiving data. You may try multiple readings before deciding to "
"close manually\r\n");
}

if (res == lwespOK && pbuf != NULL) { /* Make sure we have valid packet buffer */
/*
* At this point read and manipulate
* At this point, read and manipulate
* with received buffer and check if you expect more data
*
* After you are done using it, it is important
* you free the memory otherwise memory leaks will appear
* you free the memory, or memory leaks will appear
*/
printf("Received new data packet of %d bytes\r\n", (int)lwesp_pbuf_length(pbuf, 1));
lwesp_pbuf_free(pbuf); /* Free the memory after usage */
lwesp_pbuf_free(pbuf); /* Free the memory after usage */
pbuf = NULL;
}
} while (1);
Expand All @@ -118,12 +119,12 @@ lwesp_sys_sem_t* sem = (void*)arg;
} else {
printf("Cannot connect to remote host %s:%d!\r\n", NETCONN_HOST, NETCONN_PORT);
}
lwesp_netconn_delete(client); /* Delete netconn structure */
lwesp_netconn_delete(client); /* Delete netconn structure */
}

printf("Terminating thread\r\n");
if (lwesp_sys_sem_isvalid(sem)) {
lwesp_sys_sem_release(sem);
}
lwesp_sys_thread_terminate(NULL); /* Terminate current thread */
lwesp_sys_thread_terminate(NULL); /* Terminate current thread */
}

0 comments on commit 2ade6a9

Please sign in to comment.