1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
26
27
28
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
59
63
64
69
74
75
81
86
87
93
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
126
127
128
129
130
131
132
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
203
204
209
210
211
212
213
217
218
219
220
221
222
223
227
228
229
234
235
236
237
243
244
245
246
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
421
427
428
429
/* ... */
#include "tftpserver.h"
#include "flash_if.h"
#include <string.h>
#include <stdio.h>
#include "main.h"
#include "lcd_log.h"
6 includes
#ifdef USE_IAP_TFTP
static uint32_t Flash_Write_Address;
static struct udp_pcb *UDPpcb;
static __IO uint32_t total_count=0;
Private variables
static void IAP_wrq_recv_callback(void *_args, struct udp_pcb *upcb, struct pbuf *pkt_buf,
const ip_addr_t *addr, u16_t port);
static int IAP_tftp_process_write(struct udp_pcb *upcb, const ip_addr_t *to, int to_port);
static void IAP_tftp_recv_callback(void *arg, struct udp_pcb *Upcb, struct pbuf *pkt_buf,
const ip_addr_t *addr, u16_t port);
static void IAP_tftp_cleanup_wr(struct udp_pcb *upcb, tftp_connection_args *args);
static tftp_opcode IAP_tftp_decode_op(char *buf);
static u16_t IAP_tftp_extract_block(char *buf);
static void IAP_tftp_set_opcode(char *buffer, tftp_opcode opcode);
static void IAP_tftp_set_block(char* packet, u16_t block);
static err_t IAP_tftp_send_ack_packet(struct udp_pcb *upcb, const ip_addr_t *to, int to_port, int block);
Private function prototypes
/* ... */
static tftp_opcode IAP_tftp_decode_op(char *buf)
{
return (tftp_opcode)(buf[1]);
}{ ... }
/* ... */
static u16_t IAP_tftp_extract_block(char *buf)
{
u16_t *b = (u16_t*)buf;
return ntohs(b[1]);
}{ ... }
/* ... */
static void IAP_tftp_set_opcode(char *buffer, tftp_opcode opcode)
{
buffer[0] = 0;
buffer[1] = (u8_t)opcode;
}{ ... }
/* ... */
static void IAP_tftp_set_block(char* packet, u16_t block)
{
u16_t *p = (u16_t *)packet;
p[1] = htons(block);
}{ ... }
/* ... */
static err_t IAP_tftp_send_ack_packet(struct udp_pcb *upcb, const ip_addr_t *to, int to_port, int block)
{
err_t err;
struct pbuf *pkt_buf;
char packet[TFTP_ACK_PKT_LEN];
memset(packet, 0, TFTP_ACK_PKT_LEN *sizeof(char));
IAP_tftp_set_opcode(packet, TFTP_ACK);
/* ... */
IAP_tftp_set_block(packet, block);
pkt_buf = pbuf_alloc(PBUF_TRANSPORT, TFTP_ACK_PKT_LEN, PBUF_POOL);
if (!pkt_buf)
{
#ifdef USE_LCD
LCD_ErrLog("Can not allocate pbuf\n");
#endif
return ERR_MEM;
...}
memcpy(pkt_buf->payload, packet, TFTP_ACK_PKT_LEN);
err = udp_sendto(upcb, pkt_buf, to, to_port);
pbuf_free(pkt_buf);
return err;
}{ ... }
/* ... */
static void IAP_wrq_recv_callback(void *_args, struct udp_pcb *upcb, struct pbuf *pkt_buf, const ip_addr_t *addr, u16_t port)
{
tftp_connection_args *args = (tftp_connection_args *)_args;
uint32_t data_buffer[128];
uint16_t count=0;
#ifdef USE_LCD
char message[40];
#endif
if (pkt_buf->len != pkt_buf->tot_len)
{
#ifdef USE_LCD
LCD_ErrLog("Invalid data length\n");
#endif
return;
}if (pkt_buf->len != pkt_buf->tot_len) { ... }
if ((pkt_buf->len > TFTP_DATA_PKT_HDR_LEN) &&
(IAP_tftp_extract_block(pkt_buf->payload) == (args->block + 1)))
{
pbuf_copy_partial(pkt_buf, data_buffer, pkt_buf->len - TFTP_DATA_PKT_HDR_LEN,
TFTP_DATA_PKT_HDR_LEN);
total_count += pkt_buf->len - TFTP_DATA_PKT_HDR_LEN;
count = (pkt_buf->len - TFTP_DATA_PKT_HDR_LEN)/4;
if (((pkt_buf->len - TFTP_DATA_PKT_HDR_LEN)%4)!=0)
count++;
FLASH_If_Write(&Flash_Write_Address, data_buffer ,count);
args->block++;
(args->tot_bytes) += (pkt_buf->len - TFTP_DATA_PKT_HDR_LEN);
/* ... */
}if ((pkt_buf->len > TFTP_DATA_PKT_HDR_LEN) && (IAP_tftp_extract_block(pkt_buf->payload) == (args->block + 1))) { ... }
else if (IAP_tftp_extract_block(pkt_buf->payload) == (args->block + 1))
{
args->block++;
}else if (IAP_tftp_extract_block(pkt_buf->payload) == (args->block + 1)) { ... }
IAP_tftp_send_ack_packet(upcb, addr, port, args->block);
/* ... */
if (pkt_buf->len < TFTP_DATA_PKT_LEN_MAX)
{
IAP_tftp_cleanup_wr(upcb, args);
pbuf_free(pkt_buf);
#ifdef USE_LCD
sprintf(message, "%d bytes ",(int)total_count);
LCD_UsrLog("Tot bytes Received:, %s\n", message);
LCD_UsrLog(" State: Prog Finished \n");
LCD_UsrLog("Reset the board \n");/* ... */
#endif
}if (pkt_buf->len < TFTP_DATA_PKT_LEN_MAX) { ... }
else
{
pbuf_free(pkt_buf);
return;
}else { ... }
}{ ... }
/* ... */
static int IAP_tftp_process_write(struct udp_pcb *upcb, const ip_addr_t *to, int to_port)
{
tftp_connection_args *args = NULL;
/* ... */
args = mem_malloc(sizeof *args);
if (!args)
{
#ifdef USE_LCD
LCD_ErrLog("Memory error \n");
#endif
IAP_tftp_cleanup_wr(upcb, args);
return 0;
}if (!args) { ... }
args->op = TFTP_WRQ;
args->to_ip.addr = to->addr;
args->to_port = to_port;
args->block = 0;
args->tot_bytes = 0;
udp_recv(upcb, IAP_wrq_recv_callback, args);
total_count =0;
FLASH_If_Init();
FLASH_If_Erase(USER_FLASH_FIRST_PAGE_ADDRESS);
Flash_Write_Address = USER_FLASH_FIRST_PAGE_ADDRESS;
IAP_tftp_send_ack_packet(upcb, to, to_port, args->block);
#ifdef USE_LCD
LCD_UsrLog(" State: Programming... \n");
#endif
return 0;
}{ ... }
/* ... */
static void IAP_tftp_recv_callback(void *arg, struct udp_pcb *upcb, struct pbuf *pkt_buf,
const ip_addr_t *addr, u16_t port)
{
tftp_opcode op;
struct udp_pcb *upcb_tftp_data;
err_t err;
#ifdef USE_LCD
uint32_t i;
char filename[40],message[46], *ptr;/* ... */
#endif
upcb_tftp_data = udp_new();
if (!upcb_tftp_data)
{
#ifdef USE_LCD
LCD_ErrLog("Can not create pcb \n");
#endif
return;
}if (!upcb_tftp_data) { ... }
/* ... */
err = udp_bind(upcb_tftp_data, IP_ADDR_ANY, 0);
if (err != ERR_OK)
{
#ifdef USE_LCD
LCD_ErrLog("Can not create pcb \n");
#endif
return;
}if (err != ERR_OK) { ... }
op = IAP_tftp_decode_op(pkt_buf->payload);
if (op != TFTP_WRQ)
{
#ifdef USE_LCD
LCD_ErrLog("Bad TFTP opcode \n");
#endif
udp_remove(upcb_tftp_data);
}if (op != TFTP_WRQ) { ... }
else
{
#ifdef USE_LCD
ptr = pkt_buf->payload;
ptr = ptr +2;
i= 0;
while (*(ptr+i)!=0x0)
{
i++;
}while (*(ptr+i)!=0x0) { ... }
strncpy(filename, ptr, i+1);
LCD_UsrLog("IAP using TFTP \n");
sprintf(message, "File: %s",filename);
LCD_UsrLog("%s\n", message);
LCD_UsrLog(" State: Erasing...\n");/* ... */
#endif
IAP_tftp_process_write(upcb_tftp_data, addr, port);
}else { ... }
pbuf_free(pkt_buf);
}{ ... }
/* ... */
static void IAP_tftp_cleanup_wr(struct udp_pcb *upcb, tftp_connection_args *args)
{
mem_free(args);
udp_disconnect(upcb);
udp_remove(upcb);
udp_recv(UDPpcb, IAP_tftp_recv_callback, NULL);
}{ ... }
Private functions
/* ... */
void IAP_tftpd_init(void)
{
err_t err;
unsigned port = 69;
UDPpcb = udp_new();
if (!UDPpcb)
{
#ifdef USE_LCD
LCD_ErrLog("Can not create pcb \n");
#endif
return;
}if (!UDPpcb) { ... }
err = udp_bind(UDPpcb, IP_ADDR_ANY, port);
if (err == ERR_OK)
{
udp_recv(UDPpcb, IAP_tftp_recv_callback, NULL);
}if (err == ERR_OK) { ... }
else
{
#ifdef USE_LCD
LCD_ErrLog("Can not create pcb \n");
#endif
}else { ... }
}{ ... }
Global functions#endif/* ... */