Data communication through tcp over http port using xmc4800 relax kit

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
Hello,

I am a newbie to network communication and writing an application on xmc4800 relax kit. Here is my requirement "Need to transfer data to webserver from relax kit when there is a request from websever".
This is how I have coded.

-- Create a new tcp connection over http port and listen
-- When there is a request send data using same tcp object.

I have taken up the code from XMC4800 IOT EXAMPLE and httpserver example.

When I debugged it I have got tcp_sent acknowledge but could not see the request coming to webserver (xampp) logs. And when I tried with firefox (without running webserver - kind of peer to peer) I do get
response but it's in different than what I sent. Looks encrypted or encoded.

Here is my code:

static err_t
recv_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
char *data;
char *crlf;
u16_t data_len;

if ((err != ERR_OK) || (p == NULL)) {
/* error or closed by other side? */
if (p != NULL) {
/* Inform TCP that we have taken the data. */
tcp_recved(pcb, p->tot_len);
pbuf_free(p);
}
return ERR_OK;
}

/* Inform TCP that we have taken the data. */
tcp_recved(pcb, p->tot_len);

data = (char *)p->payload;
data_len = p->len;

if(p != NULL) pbuf_free(p);

pcb_send = pcb;

connection_ready=1;

send_data(1,2,3);

return ERR_OK;

}

err_t sent_data(void *arg, struct tcp_pcb *pcb, u16_t len)
{
return ERR_OK;
}

static err_t
poll_data(void *arg, struct tcp_pcb *pcb)
{
return ERR_OK;
}



static err_t
tcp_conn_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
/* Decrease the listen backlog counter */
tcp_accepted((struct tcp_pcb_listen*)arg);
/* Set priority */
tcp_setprio(pcb, TCP_PRIO_MIN);

/* Tell TCP that this is the structure we wish to be passed for our
callbacks. */
tcp_arg(pcb, pcb);

/* Set up the various callback functions */
tcp_recv(pcb, recv_data);
tcp_err(pcb, client_err);
tcp_poll(pcb, poll_data , 4);
tcp_sent(pcb, sent_data);

return ERR_OK;
}

void client_err(void *arg, err_t err)
{
if (err == ERR_RST)
pcb_valid=0;
return;
}

void client_init(void)
{
static struct tcp_pcb *pcb;
struct ip_addr dest;
IP4_ADDR(&dest, SERVER_IP_ADDR0, SERVER_IP_ADDR1, SERVER_IP_ADDR2, SERVER_IP_ADDR3);

/*pcb = tcp_new();
tcp_err(pcb, webServerConnError);
tcp_bind(pcb, IP_ADDR_ANY, SERVER_HTTP_PORT); //server port for incoming connection
tcp_arg(pcb, NULL);
tcp_connect(pcb, &dest, SERVER_HTTP_PORT, webServerConnSuccess); //server port for incoming connection*/


pcb = tcp_new();
LWIP_ASSERT("httpd_init: tcp_new failed", pcb != NULL);
tcp_setprio(pcb, TCP_PRIO_MIN);
/* set SOF_REUSEADDR here to explicitly bind httpd to multiple interfaces */
tcp_bind(pcb, IP_ADDR_ANY, SERVER_HTTP_PORT);
LWIP_ASSERT("httpd_init: tcp_bind failed", err == ERR_OK);
pcb = tcp_listen(pcb);
LWIP_ASSERT("httpd_init: tcp_listen failed", pcb != NULL);
/* initialize callback arg and accept callback */
tcp_arg(pcb, pcb);
tcp_accept(pcb, tcp_conn_accept);
}

void send_data(uint8_t button1, uint8_t button2, uint8_t counter_state)
{
static uint32_t connect_WD=0;
char http_header[1024];

sprintf(http_header,"GET /pushData.php?"
"device=%i&value=%i&button1=%i&button2=%i HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: keep-alive\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"Accept: text/html,application/xhtml+xml,"
"application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
"Accept-Encoding: gzip, deflate, sdch\r\n\r\n"
,DEVICE_ID, counter_state, button1, button2, DOMAIN_NAME);

if ((pcb_send!=0))
{
connect_WD=0;
tcp_write(pcb_send, (char*)&http_header, sizeof(http_header), 0);
tcp_output(pcb_send);
}
else
{
/* Connection watchdog triggered
* --> reset Protocol Control Block
*/
connect_WD++;
if (connect_WD==10)
{
client_close(pcb_send);
pcb_valid=0;
}
}
}

Here is data request I sent and response I get in firefox.

sprintf(http_header,"GET /pushData.php?"
"device=%i&value=%i&button1=%i&button2=%i HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: keep-alive\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"Accept: text/html,application/xhtml+xml,"
"application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
"Accept-Encoding: gzip, deflate, sdch\r\n\r\n"
,DEVICE_ID, counter_state, button1, button2, DOMAIN_NAME);

Response :

R0VUIC9wdXNoRGF0YS5waHA/ZGV2aWNlPTAmdmFsdWU9MCZidXR0b24xPTEmYnV0dG9uMj0xIEhUVFAvMS4xDQpIb3N0OiAxOTIuMTY4LjAuNQ0KQ29ubmVjdGlvbjoga2VlcC1hbGl2ZQ0KUHJhZ21hOiBuby1jYWNoZQ0KQ2FjaGUtQ29udHJvbDogbm8tY2FjaGUNCkFjY2VwdDogdGV4dC9odG1sLGFwcGxpY2F0aW9uL3hodG1sK3htbCxhcHBsaWNhdGlvbi94bWw7cT0wLjksaW1hZ2Uvd2VicCwqLyo7cT0wLjgNCkFjY2VwdC1FbmNvZGluZzogZ3ppcCwgZGVmbGF0ZSwgc2RjaA0KDQoAMI3+H/jy/h8wiP4fyQcUAPjy/h8wjf4fAAAUABCF/h+dOQAIIUCA/zCN/h8sjQIA+PL+HzoAAAAAiP4fAAcAADCF/h/48v4fMIj+H/Dy/h/mKgAgOgAAAAAAAABQhf4fvSgACPCI/h/w8v4f8PL+H0Tz/h8sjf4faIX+H3CF/h/48v4fAAAAAPDy/h+Ahf4fiUcACBjz/h8AAAAA1I7+H18tAAQAAAAA1I7+H6CF/h8sjf4fJAcAAHz0406whf4fsIX+H7iF/h8sjf4fNgAA/zCN/h8sjf4f+PL+H/CI/h9XiP4fAAEAAAEAAAAALAAALAAAABjz/h8wiBQAAIb+H+s5AAgAAwAAAwAAAAIQAAAQAgAAaPP+HzCIFAAghv4f6zkACAAAAAAGAAAAMIj+H/8AAAAwhv7/MI3+HyyN/h9I8/4fLI3+HzCI/h/o8hQA/wAAAFCG/h+jjQAIAAAAAAYAAAAsjf4fxI7+H/jyGAIAABuHGFAAAAckAAAkBwAAWO3jTgAAAAAsjf4fAAAZbpDz/h9QEAAAeAIACJCG/h8BiwAIROv+HyyN/h8AABuH3xgBAAAAAABQEAAA+PL+HwAAAACM6/4f1I7+H8iG/h8PagAIABAAABAAAABE6/4fLI3+H0gGAABE6xgCAAAAAMSO/h/whv4f62IACDCI/h/AqAMAMIj+H0Tr/h8Ih/4fw5UACBBQAABQEAAAcP8AAP9wAAAAABuHTuPtWFAAAAAAUAAAjgQAAASOAAAQUAAAUBAAAGTrBQBE6/4AAAAAACyN/h8BACgAeAIACFCH/h89OAAIMIj+H0Tr/h8oAAAAACgAACgAFABk6/4fAQAAADCI/h94h/4f9TEACDCI/h9E6/4f3BIAIDwACABU6/4fROsQAJiH/h85JAAI1P7//zCI/h+oh/4fAAgAAAgAAABU6/4fMIj+H0Tr/h/Ah/4fzyQACOiH/h/5////AAAAABgCAAAAAAAAAAAAAA==

Immediate suggestions would be highly appreciated as I have hard deadlines to finish it off.
0 Likes
0 Replies