cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong values getting assigned when same structure assignment is done.

pradeepwagre
Associate III

I am developing an TCP/IP Server Client application on my NUCLEO-F207ZG  development board.I am implementing a function to send ADC values on Ethernet.

void Client_Data_Send(void)
{
char temp_buff[TEMP_BUFF_SIZE];
volatile uint32_t buff_len;
volatile uint16_t Temperature = 0;
struct pbuf *p = NULL;

Temperature = TEMP;

buff_len = sprintf(temp_buff,"DHT11 Sensor value from TCP Client : %u \n\r",Temperature);

/*allocate pbuf from ram*/
p = (pbuf_alloc(PBUF_TRANSPORT,buff_len,PBUF_POOL));

(tcp_client->p)= (p);

if(tcp_client->p != NULL)
{
/*copy the data to send into the allocated pbuf*/
pbuf_take( tcp_client->p ,(char *)temp_buff,buff_len);

/*send data*/
tcp_client_send(tx_pcb, tcp_client);

/*free mem*/
pbuf_free( tcp_client->p);
}

}

Above is the function I implemented.

At line (tcp_client->p)= (p); when I debuged I came to know that values are not getting assinged.

Below are the screen shot.

pradeepwagre_0-1715354230350.png

 

pradeepwagre_1-1715354230596.png

 

since wrong values for all members getting assigned I am facing Hard fault issue. Is it structure alignment issue? I am using LwIP library for Tcp/Ip application.

1 ACCEPTED SOLUTION

Accepted Solutions
pradeepwagre
Associate III

Thank you for replying,I added this and then the similar structure assignment issue got solved.

pradeepwagre_0-1715782240650.png

 

View solution in original post

9 REPLIES 9
Bob S
Principal

pbuf_alloc() can return NULL when there are no buffers available.  You don't check for that.  Try increasing the number of pbufs.

can you elaborate "increase number of pbufs"?Sorry I am a bit new to LwIP library.

Not sure which image you're complaining about. The resolution of the Yellow screen shot makes it unreadable.

What does the black one represent vs the yellow one. Problem is poorly expressed.

>>Sorry I am a bit new to LwIP library.

Perhaps read the documentation and review a broader group of examples.

Is there a #define for buffer counts? Does the buffer need to be persistent?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Appologise for given lack of information.

Buffer count is 100.

After executing the below statement

p = (pbuf_alloc(PBUF_TRANSPORT,buff_len,PBUF_POOL));

Yellow image is shown by debugger which is expected value,basically it's showing string length which is to be sent on ethernet.

pradeepwagre_0-1715360942174.png

 

But after that

(tcp_client->p)= (p); Is making problem.

The black screen image is showing garbage values even after executing it,I am complaining below black image.

 

pradeepwagre_1-1715361234307.png

typedef struct
{
uint8_t retries;
uint8_t state;
struct pbuf *p;
struct tcp_pcb *pcb;

}Tcp_Client_Type;

Above is my structure defined.

 

I think my initial post was not on the right track.  The code does indeed check for NULL in the pbuf pointer.  But to answer your follow-up question, there are a couple of #define values related to this.  PBUF_POOL_SIZE is probably the one you want IF you wanted to change that.  But I don't think that is your issue.

The first image you posted is too low-res to read.  It looks like it is showing the contents of "p" and might be OK if I read the fuzzy pixels right.  The second shows "tcp_client", I presume BEFORE it gets "p" assigned to it.  That looks odd, specially the pointers.  Where is "tcp_client" declared/defined?  It looks to be a global variable in the code you posted above.

Standard debugging applies here.  Does this fail on the 1st time through?  20th time?  Only after hours of running?  What do your other functions do with "tcp_client"?  And as a good programming practice, after you call pbuf_free() set tcp_client->p = NULL to help with future debugging and make it obvious if you try to use tcp_client->p again without allocating a new buffer.

This issue happens at first attempt.

yes tcp_client is global variable.

/*
* tcp_client.c
*
* Created on: 05-May-2024
* Author: prade
*/
#include "tcp_client.h"
#define TEMP_BUFF_SIZE 100


err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err);
err_t tcp_client_receive(void *arg, struct tcp_pcb *tpcb,struct pbuf *p, err_t err);
err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb);
err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb,u16_t len);
void tcp_client_handle (struct tcp_pcb *tpcb,Tcp_Client_Type *es);


extern volatile uint16_t TEMP;
extern UART_HandleTypeDef huart3;


Tcp_Client_Type *tcp_client = NULL;
struct tcp_pcb *tx_pcb = NULL;
struct pbuf *p = NULL;
void tcp_client_init(void)
{
struct tcp_pcb *tpcb = NULL;

ip_addr_t server_ip_addr;

/*Create new tcp pcb*/
tpcb = tcp_new();

/*Connect to Server*/
IP_ADDR4(&server_ip_addr,192,168,0,104);

tcp_connect(tpcb, &server_ip_addr, SERVER_PORT, tcp_client_connected);
}


err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
err_t ret;

Tcp_Client_Type * x_tcp_client;

x_tcp_client = (Tcp_Client_Type *)mem_malloc(sizeof(Tcp_Client_Type));

if(x_tcp_client != NULL)
{
x_tcp_client->state = STATE_CONNECTED;
x_tcp_client->pcb = tpcb;
x_tcp_client->retries = 0;
x_tcp_client->p = NULL;

/*Pass newly initialized Tcp_Client_Type as argument to the control block*/
tcp_arg(tpcb,x_tcp_client);

/*Register the recv callback function*/
tcp_recv(tpcb,tcp_client_receive);

/*Register the tcp poll callback structure*/
tcp_poll(tpcb,tcp_client_poll,0);

/*Regsiter tcp send call back function*/
tcp_sent(tpcb,tcp_client_sent);

/* handle the TCP data */
tcp_client_handle(tpcb, x_tcp_client);

HAL_UART_Transmit (&huart3,(const uint8_t*)"TRUE",sizeof("TRUE"),15);

HAL_UART_Transmit (&huart3,(const uint8_t*)"\n\r",sizeof("\n\r"),15);

ret= ERR_OK;
}
else
{
HAL_UART_Transmit (&huart3,(const uint8_t*)"FALSE",sizeof("FALSE"),15);

HAL_UART_Transmit (&huart3,(const uint8_t*)"\n\r",sizeof("\n\r"),15);

tcp_close_connection(tpcb,x_tcp_client);

ret = ERR_MEM;
}
return ret;

}



err_t tcp_client_receive(void *arg, struct tcp_pcb *tpcb,struct pbuf *p, err_t err)
{
char *_OFF = "OFF";
char *_ON = "ON" ;
char *rslt;

HAL_UART_Transmit (&huart3,(const uint8_t*)"\n\r",sizeof("\n\r"),15);

HAL_UART_Transmit (&huart3,(const uint8_t*)"Message from TCP Server: %s\n\r",sizeof("Message from TCP Server: %s\n\r"),15);

HAL_UART_Transmit (&huart3,(const uint8_t*)p->payload,sizeof((const uint8_t*)p->payload)+1,15);

HAL_UART_Transmit (&huart3,(const uint8_t*)"\n\r",sizeof("\n\r"),15);

rslt = strstr((char *)p->payload,_ON);

if(rslt)
{
HAL_UART_Transmit (&huart3,(const uint8_t*)("LED ON\n\r"),sizeof("LED ON\n\r"),15);

HAL_GPIO_WritePin(GPIOB,RED_LED_Pin, GPIO_PIN_SET);

}

rslt = strstr((char*)p->payload,_OFF);

if(rslt)
{
HAL_UART_Transmit (&huart3,(const uint8_t*)"LED OFF\n\r",sizeof("LED OFF\n\r"),15);

HAL_GPIO_WritePin(GPIOB,RED_LED_Pin, GPIO_PIN_RESET);

}

pbuf_free(p);

return ERR_OK;
}



err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb)
{
err_t ret;

Tcp_Client_Type * x_tcp_client;

x_tcp_client = (Tcp_Client_Type *)(arg);

if(x_tcp_client != NULL)
{
if(x_tcp_client->p != NULL)
{

}
else
{
if(x_tcp_client->state == STATE_CLOSING)
{
tcp_close_connection(tpcb,x_tcp_client);
}
}

ret = ERR_OK;
}
else
{
tcp_abort(tpcb);

ret = ERR_ABRT;
}

return ret;

}


err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb,u16_t len)
{
Tcp_Client_Type * x_tcp_client;

x_tcp_client = (Tcp_Client_Type *)(arg);

x_tcp_client->retries = 0;

if(x_tcp_client != NULL)
{

}
else
{
if(x_tcp_client->state == STATE_CLOSING)

tcp_close_connection(tpcb,x_tcp_client);
}

return ERR_OK;

}


void tcp_client_send(struct tcp_pcb *tpcb, Tcp_Client_Type * x_tcp_client)
{
struct pbuf *ptr;
err_t err = ERR_OK;
u8_t freed;
/*There is no error
* There is data to send
* Data size is smaller than the size of the send buffer*/

while((err == ERR_OK) && (x_tcp_client->p != NULL) && (x_tcp_client->p->len <= tcp_sndbuf(tpcb)) )
{
ptr = x_tcp_client->p;

err = tcp_write(tpcb, ptr->payload,ptr->len,TCP_WRITE_FLAG_COPY);

if(err == ERR_OK)
{
x_tcp_client->p = ptr->next;

/*There is chained buffer to send*/
if(x_tcp_client->p != NULL)
{
/*Increase reference count*/
pbuf_ref(x_tcp_client->p);
}


do
{

freed = pbuf_free(ptr);

}while(freed == 0);

}
else
{
/*Recover buffer pointer*/
x_tcp_client->p = ptr;

/*Increase retry count*/
x_tcp_client->retries++;
}
}
}

void tcp_close_connection(struct tcp_pcb *tpcb, Tcp_Client_Type *x_tcp_client)
{
tcp_arg(tpcb, NULL);
tcp_sent(tpcb,NULL);
tcp_recv(tpcb,NULL);
tcp_err(tpcb,NULL);
tcp_poll(tpcb,NULL,0);

if(x_tcp_client != NULL )
{
mem_free(x_tcp_client);
}

tcp_close(tpcb);

}

void Client_Data_Send(void)
{
char temp_buff[TEMP_BUFF_SIZE];
volatile uint32_t buff_len;
volatile uint16_t Temperature = 0;

Temperature = TEMP;

buff_len = sprintf(temp_buff,"DHT11 Sensor value from TCP Client : %u \n\r",Temperature);

/*allocate pbuf from ram*/
p = (pbuf_alloc(PBUF_TRANSPORT,buff_len,PBUF_POOL));

//memcpy((struct pbuf*)(tcp_client->p), (struct pbuf*)(p), sizeof(struct pbuf));
tcp_client->p = p;

/*copy the data to send into the allocated pbuf*/
pbuf_take(tcp_client->p ,(char *)temp_buff,buff_len);

/*send data*/
tcp_client_send(tx_pcb, tcp_client);

/*free mem*/
pbuf_free(tcp_client->p);


}


/* Handle the incoming TCP Data */
void tcp_client_handle (struct tcp_pcb *tpcb,Tcp_Client_Type *es)
{
/* get the Remote IP */
ip4_addr_t inIP = tpcb->remote_ip;
uint16_t inPort = tpcb->remote_port;

/* Extract the IP */
char *remIP = ipaddr_ntoa(&inIP);

// esTx->state = es->state;
// esTx->pcb = es->pcb;
// esTx->p = es->p;

tcp_client = es;
tx_pcb = tpcb;
}

I have posted the whole tcp_client.c file

Use the code sample insert tool so that your code is properly formatted for easy reading.

 

Code Snippet.jpg

If you find my answers useful, click the accept button so that way others can see the solution.
pradeepwagre
Associate III

I did it in above comments.

pradeepwagre
Associate III

Thank you for replying,I added this and then the similar structure assignment issue got solved.

pradeepwagre_0-1715782240650.png