Skip to content

Commit bd72512

Browse files
committed
Fixed issue with Cosm sketch
1 parent abd9db1 commit bd72512

File tree

6 files changed

+63
-15
lines changed

6 files changed

+63
-15
lines changed

binary/wifiHD.elf

1.21 KB
Binary file not shown.

wifiHD/Release/wifiHD.elf

1.21 KB
Binary file not shown.

wifiHD/src/ard_tcp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
298298
err_t err) {
299299
struct ttcp* ttcp = arg;
300300

301-
INFO_TCP("pcb:%p pbuf: %p err:%d\n", pcb, p, err);
302301
if (err == ERR_OK && p != NULL) {
302+
INFO_TCP("pcb:%p pbuf: %p err:%d len:%d\n", pcb, p, err, p->tot_len);
303303
DATA_LED_ON();
304304
/* for print_stats() */
305305
ttcp->recved += p->tot_len;

wifiHD/src/ard_utils.c

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "ard_utils.h"
1212
#include "debug.h"
1313

14-
#define MAX_PBUF_STORED 50
14+
#define MAX_PBUF_STORED 30
1515

1616
tData pBufStore[MAX_PBUF_STORED][MAX_SOCK_NUM];
1717

@@ -31,12 +31,20 @@ void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
3131
if (q == NULL)
3232
return;
3333

34+
if (pBufStore[headBuf][sock].data != NULL)
35+
{
36+
WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf][sock].data, headBuf);
37+
// to avoid memory leak free the oldest buffer
38+
freetDataIdx(headBuf, sock);
39+
}
40+
3441
u8_t* p = (u8_t*)calloc(q->tot_len,sizeof(u8_t));
3542
if(p != NULL) {
3643
if (pbuf_copy_partial(q, p, q->tot_len,0) != q->tot_len) {
37-
free(p);
38-
p = NULL;
39-
return;
44+
WARN("pbuf_copy_partial failed: src:%p, dst:%p, len:%d\n", q, p, q->tot_len);
45+
free(p);
46+
p = NULL;
47+
return;
4048
}
4149

4250
pBufStore[headBuf][sock].data = p;
@@ -48,7 +56,7 @@ void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
4856
if (headBuf == MAX_PBUF_STORED)
4957
headBuf = 0;
5058
if (headBuf == tailBuf)
51-
WARN("Overwriting data!");
59+
WARN("Overwriting data [%d-%d]!\n", headBuf, tailBuf);
5260
INFO_UTIL("Insert: %p:%d-%d [%d,%d]\n", p, q->tot_len, p[0], headBuf, tailBuf);
5361
}
5462
}
@@ -61,22 +69,52 @@ tData* get_pBuf(uint8_t sock)
6169
if (IS_BUF_AVAIL())
6270
{
6371
tData* p = &(pBufStore[tailBuf][sock]);
64-
INFO_UTIL("%p [%d,%d]\n", p, headBuf, tailBuf);
72+
INFO_UTIL_VER("%p [%d,%d]\n", p, headBuf, tailBuf);
6573
return p;
6674
}
6775
return NULL;
6876
}
6977

70-
void freetData(void * buf)
78+
void freetData(void * buf, uint8_t sock)
7179
{
7280
if (buf==NULL)
81+
{
82+
WARN("Buf == NULL!");
7383
return;
84+
}
85+
86+
pBufStore[tailBuf][sock].data = NULL;
87+
pBufStore[tailBuf][sock].len = 0;
88+
pBufStore[tailBuf][sock].idx = 0;
89+
pBufStore[tailBuf][sock].pcb = 0;
7490

7591
if (++tailBuf == MAX_PBUF_STORED)
7692
tailBuf = 0;
93+
INFO_UTIL("%p [%d,%d]\n", buf, headBuf, tailBuf);
94+
free(buf);
95+
}
96+
97+
void freetDataIdx(uint8_t idxBuf, uint8_t sock)
98+
{
99+
if (idxBuf >=MAX_PBUF_STORED)
100+
{
101+
WARN("idxBuf out of range: %d\n", idxBuf);
102+
return;
103+
}
104+
105+
void * buf = pBufStore[idxBuf][sock].data;
106+
107+
INFO_UTIL("%p idx:%d\n", buf, idxBuf);
108+
77109
free(buf);
110+
111+
pBufStore[idxBuf][sock].data = 0;
112+
pBufStore[idxBuf][sock].len = 0;
113+
pBufStore[idxBuf][sock].idx = 0;
114+
pBufStore[idxBuf][sock].pcb = 0;
78115
}
79116

117+
80118
void ack_recved(void* pcb, int len);
81119

82120
bool isAvailTcpDataByte(uint8_t sock)
@@ -85,10 +123,10 @@ bool isAvailTcpDataByte(uint8_t sock)
85123

86124
if (p != NULL)
87125
{
88-
INFO_UTIL("check:%d %d %p\n",p->idx, p->len, p->data);
126+
INFO_UTIL_VER("check:%d %d %p\n",p->idx, p->len, p->data);
89127
if (p->idx == p->len)
90128
{
91-
freetData(p->data);
129+
freetData(p->data, sock);
92130
ack_recved(p->pcb, p->len);
93131
INFO_UTIL("Free %p other buf %d tail:%d head:%d\n",
94132
p->data, IS_BUF_AVAIL(), tailBuf, headBuf);
@@ -116,12 +154,12 @@ bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek)
116154
*payload = buf[p->idx];
117155
else
118156
*payload = buf[p->idx++];
119-
INFO_UTIL("get:%d %p %d\n",p->idx, p->data, *payload);
157+
INFO_UTIL_VER("get:%d %p %d\n",p->idx, p->data, *payload);
120158
return true;
121159
}else{
122160
//dealloc current buffer
123161
INFO_UTIL("Free %p\n", p->data);
124-
freetData(p->data);
162+
freetData(p->data, sock);
125163
ack_recved(p->pcb, p->len);
126164
}
127165
}
@@ -147,7 +185,7 @@ bool freeTcpData(uint8_t sock)
147185
p = get_pBuf(sock);
148186
if (p != NULL)
149187
{
150-
freetData(p->data);
188+
freetData(p->data, sock);
151189
ack_recved(p->pcb, p->len);
152190
return true;
153191
}

wifiHD/src/ard_utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#define LED2_DN() gpio_clr_gpio_pin(LED2_GPIO)
2626
#define LED2_TL() gpio_tgl_gpio_pin(LED2_GPIO)
2727

28-
#define _DEBUG_
2928
#ifdef _DEBUG_
3029
#define SIGN0_UP LED0_UP
3130
#define SIGN0_DN LED0_DN
@@ -232,7 +231,9 @@ void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb);
232231

233232
tData* get_pBuf(uint8_t sock);
234233

235-
void freetData(void * buf);
234+
void freetData(void * buf, uint8_t sock);
235+
236+
void freetDataIdx(uint8_t idxBuf, uint8_t sock);
236237

237238
bool isBufAvail();
238239

wifiHD/src/debug.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,21 @@ if ((enableDebug & INFO_SPI_FLAG)&&(verboseDebug & INFO_SPI_FLAG)) \
5555
#define INFO_UTIL(msg, args...) do { \
5656
if (enableDebug & INFO_UTIL_FLAG) printk("I-[%s] " msg , __func__ , ##args ); \
5757
} while (0)
58+
59+
#define INFO_UTIL_VER(msg, args...) do { \
60+
if ((enableDebug & INFO_UTIL_FLAG)&&(verboseDebug & INFO_UTIL_FLAG)) \
61+
printk("I-[%s] " msg , __func__ , ##args ); \
62+
} while (0)
63+
64+
5865
#else
5966
#define INFO_INIT(msg, args...) do {}while(0);
6067
#define INFO_TCP(msg, args...) do {}while(0);
6168
#define INFO_TCP_VER(msg, args...) do { }while(0);
6269
#define INFO_SPI(msg, args...) do {}while(0);
70+
#define INFO_SPI_VER(msg, args...) do { }while(0);
6371
#define INFO_UTIL(msg, args...) do {}while(0);
72+
#define INFO_UTIL_VER(msg, args...) do { }while(0);
6473
#endif
6574

6675
#ifdef _APP_DEBUG_

0 commit comments

Comments
 (0)