You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
#include <unistd.h>
|
|
#include "ymodem.h"
|
|
#include "dwt.h"
|
|
|
|
struct stdio_ymodem {
|
|
struct ymodem_xfer xfer;
|
|
int (*on_info)(void *user, const char *name, unsigned int size);
|
|
int (*on_data)(void *user, const void *name, unsigned int size);
|
|
void *user;
|
|
};
|
|
|
|
static int do_getc(struct ymodem_xfer *xfer, uint8_t *c, int timeout)
|
|
{
|
|
dwt_clear();
|
|
do {
|
|
if (read(STDIN_FILENO, c, 1) == 1) {
|
|
return 0;
|
|
}
|
|
} while (dwt_get_millis() < timeout);
|
|
|
|
return -1;
|
|
}
|
|
|
|
static int do_putc(struct ymodem_xfer *xfer, uint8_t c)
|
|
{
|
|
int nr;
|
|
|
|
do {
|
|
nr = write(STDOUT_FILENO, &c, 1);
|
|
} while (nr != 1);
|
|
return 0;
|
|
}
|
|
|
|
static int on_info(struct ymodem_xfer *xfer, const char *name, unsigned int size)
|
|
{
|
|
struct stdio_ymodem *modem = (struct stdio_ymodem *)xfer;
|
|
|
|
if (modem->on_info) {
|
|
return modem->on_info(modem->user, name, size);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int on_data(struct ymodem_xfer *xfer, const void *data, unsigned int size)
|
|
{
|
|
struct stdio_ymodem *modem = (struct stdio_ymodem *)xfer;
|
|
|
|
if (modem->on_data)
|
|
return modem->on_data(modem->user, data, size);
|
|
return -1;
|
|
}
|
|
|
|
int stdio_ymodem_recv(void *user,
|
|
int (*_on_info)(void *user, const char *name, unsigned int size),
|
|
int (*_on_data)(void *user, const void *data, unsigned int len))
|
|
{
|
|
int err, en;
|
|
struct stdio_ymodem modem = {
|
|
.xfer = {
|
|
.getc = do_getc,
|
|
.putc = do_putc,
|
|
.on_info = on_info,
|
|
.on_data = on_data,
|
|
},
|
|
.on_info = _on_info,
|
|
.on_data = _on_data,
|
|
.user = user
|
|
};
|
|
|
|
if (!(en = dwt_is_enabled())) {
|
|
dwt_init();
|
|
}
|
|
err = ymodem_recv(&modem.xfer);
|
|
if (!en) {
|
|
dwt_close();
|
|
}
|
|
return (err);
|
|
}
|
|
|
|
|