Fixed issue with Cosm sketch

This commit is contained in:
Mimmo La Fauci 2012-06-07 13:51:48 +02:00
parent abd9db126b
commit bd72512a59
6 changed files with 63 additions and 15 deletions

Binary file not shown.

Binary file not shown.

View File

@ -298,8 +298,8 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
err_t err) {
struct ttcp* ttcp = arg;
INFO_TCP("pcb:%p pbuf: %p err:%d\n", pcb, p, err);
if (err == ERR_OK && p != NULL) {
INFO_TCP("pcb:%p pbuf: %p err:%d len:%d\n", pcb, p, err, p->tot_len);
DATA_LED_ON();
/* for print_stats() */
ttcp->recved += p->tot_len;

View File

@ -11,7 +11,7 @@
#include "ard_utils.h"
#include "debug.h"
#define MAX_PBUF_STORED 50
#define MAX_PBUF_STORED 30
tData pBufStore[MAX_PBUF_STORED][MAX_SOCK_NUM];
@ -31,12 +31,20 @@ void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
if (q == NULL)
return;
if (pBufStore[headBuf][sock].data != NULL)
{
WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf][sock].data, headBuf);
// to avoid memory leak free the oldest buffer
freetDataIdx(headBuf, sock);
}
u8_t* p = (u8_t*)calloc(q->tot_len,sizeof(u8_t));
if(p != NULL) {
if (pbuf_copy_partial(q, p, q->tot_len,0) != q->tot_len) {
free(p);
p = NULL;
return;
WARN("pbuf_copy_partial failed: src:%p, dst:%p, len:%d\n", q, p, q->tot_len);
free(p);
p = NULL;
return;
}
pBufStore[headBuf][sock].data = p;
@ -48,7 +56,7 @@ void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
if (headBuf == MAX_PBUF_STORED)
headBuf = 0;
if (headBuf == tailBuf)
WARN("Overwriting data!");
WARN("Overwriting data [%d-%d]!\n", headBuf, tailBuf);
INFO_UTIL("Insert: %p:%d-%d [%d,%d]\n", p, q->tot_len, p[0], headBuf, tailBuf);
}
}
@ -61,22 +69,52 @@ tData* get_pBuf(uint8_t sock)
if (IS_BUF_AVAIL())
{
tData* p = &(pBufStore[tailBuf][sock]);
INFO_UTIL("%p [%d,%d]\n", p, headBuf, tailBuf);
INFO_UTIL_VER("%p [%d,%d]\n", p, headBuf, tailBuf);
return p;
}
return NULL;
}
void freetData(void * buf)
void freetData(void * buf, uint8_t sock)
{
if (buf==NULL)
{
WARN("Buf == NULL!");
return;
}
pBufStore[tailBuf][sock].data = NULL;
pBufStore[tailBuf][sock].len = 0;
pBufStore[tailBuf][sock].idx = 0;
pBufStore[tailBuf][sock].pcb = 0;
if (++tailBuf == MAX_PBUF_STORED)
tailBuf = 0;
INFO_UTIL("%p [%d,%d]\n", buf, headBuf, tailBuf);
free(buf);
}
void freetDataIdx(uint8_t idxBuf, uint8_t sock)
{
if (idxBuf >=MAX_PBUF_STORED)
{
WARN("idxBuf out of range: %d\n", idxBuf);
return;
}
void * buf = pBufStore[idxBuf][sock].data;
INFO_UTIL("%p idx:%d\n", buf, idxBuf);
free(buf);
pBufStore[idxBuf][sock].data = 0;
pBufStore[idxBuf][sock].len = 0;
pBufStore[idxBuf][sock].idx = 0;
pBufStore[idxBuf][sock].pcb = 0;
}
void ack_recved(void* pcb, int len);
bool isAvailTcpDataByte(uint8_t sock)
@ -85,10 +123,10 @@ bool isAvailTcpDataByte(uint8_t sock)
if (p != NULL)
{
INFO_UTIL("check:%d %d %p\n",p->idx, p->len, p->data);
INFO_UTIL_VER("check:%d %d %p\n",p->idx, p->len, p->data);
if (p->idx == p->len)
{
freetData(p->data);
freetData(p->data, sock);
ack_recved(p->pcb, p->len);
INFO_UTIL("Free %p other buf %d tail:%d head:%d\n",
p->data, IS_BUF_AVAIL(), tailBuf, headBuf);
@ -116,12 +154,12 @@ bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek)
*payload = buf[p->idx];
else
*payload = buf[p->idx++];
INFO_UTIL("get:%d %p %d\n",p->idx, p->data, *payload);
INFO_UTIL_VER("get:%d %p %d\n",p->idx, p->data, *payload);
return true;
}else{
//dealloc current buffer
INFO_UTIL("Free %p\n", p->data);
freetData(p->data);
freetData(p->data, sock);
ack_recved(p->pcb, p->len);
}
}
@ -147,7 +185,7 @@ bool freeTcpData(uint8_t sock)
p = get_pBuf(sock);
if (p != NULL)
{
freetData(p->data);
freetData(p->data, sock);
ack_recved(p->pcb, p->len);
return true;
}

View File

@ -25,7 +25,6 @@
#define LED2_DN() gpio_clr_gpio_pin(LED2_GPIO)
#define LED2_TL() gpio_tgl_gpio_pin(LED2_GPIO)
#define _DEBUG_
#ifdef _DEBUG_
#define SIGN0_UP LED0_UP
#define SIGN0_DN LED0_DN
@ -232,7 +231,9 @@ void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb);
tData* get_pBuf(uint8_t sock);
void freetData(void * buf);
void freetData(void * buf, uint8_t sock);
void freetDataIdx(uint8_t idxBuf, uint8_t sock);
bool isBufAvail();

View File

@ -55,12 +55,21 @@ if ((enableDebug & INFO_SPI_FLAG)&&(verboseDebug & INFO_SPI_FLAG)) \
#define INFO_UTIL(msg, args...) do { \
if (enableDebug & INFO_UTIL_FLAG) printk("I-[%s] " msg , __func__ , ##args ); \
} while (0)
#define INFO_UTIL_VER(msg, args...) do { \
if ((enableDebug & INFO_UTIL_FLAG)&&(verboseDebug & INFO_UTIL_FLAG)) \
printk("I-[%s] " msg , __func__ , ##args ); \
} while (0)
#else
#define INFO_INIT(msg, args...) do {}while(0);
#define INFO_TCP(msg, args...) do {}while(0);
#define INFO_TCP_VER(msg, args...) do { }while(0);
#define INFO_SPI(msg, args...) do {}while(0);
#define INFO_SPI_VER(msg, args...) do { }while(0);
#define INFO_UTIL(msg, args...) do {}while(0);
#define INFO_UTIL_VER(msg, args...) do { }while(0);
#endif
#ifdef _APP_DEBUG_