aboutsummaryrefslogtreecommitdiff
path: root/firmwares/wifishield/wifiHD/src/ard_utils.c
blob: 39c19dd4e8f555f5ced164d98eb847bafb29eba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * ard_utils.c
 *
 *  Created on: Jul 4, 2010
 *      Author: mlf by Metodo2 srl
 */
//#define _APP_DEBUG_

#include "lwip/pbuf.h"
#include "wifi_spi.h"
#include "ard_utils.h"
#include "debug.h"
#include "ard_spi.h"
#include "ard_tcp.h"

#define MAX_PBUF_STORED	30

tData pBufStore[MAX_PBUF_STORED][MAX_SOCK_NUM];

unsigned char headBuf[MAX_SOCK_NUM] = {0};
unsigned char tailBuf[MAX_SOCK_NUM] = {0};

#define IS_BUF_AVAIL(x) (tailBuf[x] != headBuf[x])
#define IS_BUF_EMPTY(x) ((tailBuf[x] == 0) && (headBuf[x] == 0))

void init_pBuf()
{
	memset(pBufStore, 0, sizeof(pBufStore));
}

uint8_t* insertBuf(uint8_t sock, uint8_t* buf, uint16_t len)
{
	DUMP(buf,len);
	if (sock>= MAX_SOCK_NUM)
	{
		WARN("Sock out of range: sock=%d", sock);
		return NULL;
	}		
	if (pBufStore[headBuf[sock]][sock].data != NULL)
	{
		WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf[sock]][sock].data, headBuf[sock]);
		// to avoid memory leak free the oldest buffer
		freetDataIdx(headBuf[sock], sock);
	}

	u8_t* p = (u8_t*)calloc(len,sizeof(u8_t));
    if(p != NULL) {
    	memcpy(p, buf, len);

    	pBufStore[headBuf[sock]][sock].data = p;
    	pBufStore[headBuf[sock]][sock].len = len;
    	pBufStore[headBuf[sock]][sock].idx = 0;
    	pBufStore[headBuf[sock]][sock].pcb = getTTCP(sock, TTCP_MODE_TRANSMIT);
    	headBuf[sock]++;

    	if (headBuf[sock] == MAX_PBUF_STORED)
    		headBuf[sock] = 0;
    	if (headBuf[sock] == tailBuf[sock])
    	{
    		WARN("Avoid to Overwrite data [%d-%d]!\n", headBuf[sock], tailBuf[sock]);
    		if (headBuf[sock] != 0)
    			--headBuf[sock];
    		else
    			headBuf[sock] = MAX_PBUF_STORED-1;
    	}
    	INFO_UTIL("Insert[%d]: %p:%d-%d [%d,%d]\n", sock, p, len, p[0], headBuf[sock], tailBuf[sock]);
    }
    return p;
}


uint16_t calcMergeLen(uint8_t sock)
{
	uint16_t len = 0;

	unsigned char index = tailBuf[sock];
	do {
	if (pBufStore[index][sock].data != NULL)
	{
		len += pBufStore[index][sock].len;
	}
	++index;
	if (index == MAX_PBUF_STORED)
		index = 0;
	}while (index!=headBuf[sock]);
	return len;
}

uint16_t clearBuf(uint8_t sock)
{
	uint16_t len = 0;

	unsigned char index = tailBuf[sock];
	do {
	if (pBufStore[index][sock].data != NULL)
	{
		freetDataIdx(index,sock);
	}
	++index;
	if (index == MAX_PBUF_STORED)
		index = 0;
	}while (index!=headBuf[sock]);
	tailBuf[sock]=index;
	return len;
}

uint8_t* mergeBuf(uint8_t sock, uint8_t** buf, uint16_t* _len)
{
	uint16_t len = calcMergeLen(sock);
	uint8_t* p = (u8_t*)calloc(len,sizeof(u8_t));
	uint8_t* _p = p;
	if(p != NULL) {
		unsigned char index = tailBuf[sock];
		do {
		if (pBufStore[index][sock].data != NULL)
		{
			memcpy(p, pBufStore[index][sock].data, pBufStore[index][sock].len);
			p += pBufStore[index][sock].len;
		}
		++index;
		if (index == MAX_PBUF_STORED)
			index = 0;
		}while (index!=headBuf[sock]);
	}
	DUMP(_p,len);
	if (buf != NULL)
		*buf = _p;
	if (_len != NULL)
		*_len = len;
	return _p;
}

uint8_t* insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
{
	if (q == NULL)
		return NULL;

	if (pBufStore[headBuf[sock]][sock].data != NULL)
	{
		WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf[sock]][sock].data, headBuf[sock]);
		// to avoid memory leak free the oldest buffer
		freetDataIdx(headBuf[sock], 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) {
    	  WARN("pbuf_copy_partial failed: src:%p, dst:%p, len:%d\n", q, p, q->tot_len);
    	  free(p);
    	  p = NULL;
    	  return p;
      }

      pBufStore[headBuf[sock]][sock].data = p;
      pBufStore[headBuf[sock]][sock].len = q->tot_len;
      pBufStore[headBuf[sock]][sock].idx = 0;
      pBufStore[headBuf[sock]][sock].pcb = _pcb;
      headBuf[sock]++;

  	  if (headBuf[sock] == MAX_PBUF_STORED)
  		headBuf[sock] = 0;
  	  if (headBuf[sock] == tailBuf[sock])
  	  {
  		  WARN("Avoid to Overwrite data [%d-%d]!\n", headBuf[sock], tailBuf[sock]);
  		  if (headBuf[sock] != 0)
  			  --headBuf[sock];
  		  else
  			  headBuf[sock] = MAX_PBUF_STORED-1;
  	  }
  	  INFO_UTIL("Insert[%d]: %p:%d-%d [%d,%d]\n", sock, p, q->tot_len, p[0], headBuf[sock], tailBuf[sock]);
    }
    return p;
}

void dumpPbuf(uint8_t sock)
{
	unsigned char index = tailBuf[sock];
	printk("headBuf=%d tailBuf=%d\n", headBuf[sock], tailBuf[sock]);
	do {
	if (pBufStore[index][sock].data != NULL)
	{
		printk("Buf: %p Len:%d\n", pBufStore[index][sock].data, pBufStore[index][sock].len);
	}
	++index;
	if (index == MAX_PBUF_STORED)
		index = 0;
	}while (index!=headBuf[sock]);
}

tData* get_pBuf(uint8_t sock)
{
	if (IS_BUF_EMPTY(sock))
		return NULL;

	if (IS_BUF_AVAIL(sock))
	{
		tData* p = &(pBufStore[tailBuf[sock]][sock]);
		INFO_UTIL_VER("%p [%d,%d]\n", p, headBuf[sock], tailBuf[sock]);
		return p;
	}
	return NULL;
}

void freetData(void * buf, uint8_t sock)
{
	if (buf==NULL)
	{
		WARN("Buf == NULL!");
		return;
	}

    pBufStore[tailBuf[sock]][sock].data = NULL;
    pBufStore[tailBuf[sock]][sock].len = 0;
    pBufStore[tailBuf[sock]][sock].idx = 0;
    pBufStore[tailBuf[sock]][sock].pcb = 0;

	if (++tailBuf[sock] == MAX_PBUF_STORED)
		tailBuf[sock] = 0;
	INFO_UTIL("%p [%d,%d]\n", buf, headBuf[sock], tailBuf[sock]);
	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);

void ackAndFreeData(void* pcb, int len, uint8_t sock, uint8_t* data)
{
	INFO_UTIL("Ack pcb:%p len:%d sock:%d data:%p\n", pcb, len, sock, data);
	if (!IS_UDP_SOCK(sock))
		ack_recved(pcb, len);
	if (data != NULL)
		freetData(data, sock);
}


bool isAvailTcpDataByte(uint8_t sock)
{
	tData* p = get_pBuf(sock);

	if (p != NULL)
	{
		INFO_UTIL_VER("check:%d %d %p\n",p->idx, p->len, p->data);
		if (p->idx == p->len)
		{
			INFO_UTIL("Free %p other buf %d tail:%d head:%d\n",
					p->data, IS_BUF_AVAIL(sock), tailBuf[sock], headBuf[sock]);
			ackAndFreeData(p->pcb, p->len, sock, p->data);						
			return (IS_BUF_AVAIL(sock));
		}else{
			return true;
		}
	}
	return false;
}

uint16_t getAvailTcpDataByte(uint8_t sock)
{
	uint16_t len = calcMergeLen(sock);
	INFO_UTIL("Availabled data: %d\n", len);
	return len;
}


bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek)
{
	// ref field in struct pbuf has been used as index pointer for byte data
	tData* p = get_pBuf(sock);

	if (p != NULL)
	{
		if (p->idx < p->len)
		{
		uint8_t* buf = (uint8_t*)p->data;
		if (peek)
			*payload = buf[p->idx];
		else
			*payload = buf[p->idx++];
		INFO_UTIL_VER("get:%d %p %d\n",p->idx, p->data, *payload);
		return true;
		}else{
			ackAndFreeData(p->pcb, p->len, sock, p->data);
		}
	}
	return false;
}

bool getTcpData(uint8_t sock, void** payload, uint16_t* len)
{
	tData* p = NULL;
	p = get_pBuf(sock);
	if (p != NULL)
	{
		*payload = p->data;
		*len = p->len;
		return true;
	}
	return false;
}

bool freeTcpData(uint8_t sock)
{
	tData* p = NULL;
	p = get_pBuf(sock);
	if (p != NULL)
	{
		ackAndFreeData(p->pcb, p->len, sock, p->data);
		return true;
	}
	return false;
}

void freeAllTcpData(uint8_t sock)
{
	tData* p = NULL;
	do{
		p = get_pBuf(sock);
		if (p != NULL)
			freetData(p->data, sock);
	}while(p!=NULL);
}