#include "ymodem.h" #include "tick.h" #include "serial.h" struct serial_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); struct serial *uart; void *user; }; static int do_getc(struct ymodem_xfer *xfer, uint8_t *c, int timeout) { struct serial_ymodem *modem = (struct serial_ymodem *)xfer; uint64_t end = millis() + timeout; int n; do { n = serial_recv_buffered(modem->uart, c, 1); } while ((n != 1) && (millis() < end)); return n == 1 ? 0 : -1; } static int do_putc(struct ymodem_xfer *xfer, uint8_t c) { struct serial_ymodem *modem = (struct serial_ymodem *)xfer; int nr; do { nr = serial_send_buffered(modem->uart, &c, 1); } while (nr != 1); return 0; } static int on_info(struct ymodem_xfer *xfer, const char *name, unsigned int size) { struct serial_ymodem *modem = (struct serial_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 serial_ymodem *modem = (struct serial_ymodem *)xfer; if (modem->on_data) return modem->on_data(modem->user, data, size); return -1; } int serial_ymodem_recv(struct serial *uart, void *user, int (*_on_info)(void *user, const char *name, unsigned int size), int (*_on_data)(void *user, const void *data, unsigned int len)) { struct serial_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, .uart = uart, .user = user }; return ymodem_recv(&modem.xfer); }