projects
/
bertos.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
USB: coding style fixes (structure naming)
[bertos.git]
/
bertos
/
net
/
xmodem.c
diff --git
a/bertos/net/xmodem.c
b/bertos/net/xmodem.c
index d6653a49d6c37d635a87af142685063599e20e23..fbf43386d151d757d690d14b4563c12438ff241b 100644
(file)
--- a/
bertos/net/xmodem.c
+++ b/
bertos/net/xmodem.c
@@
-27,9
+27,10
@@
* the GNU General Public License.
*
* Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
* the GNU General Public License.
*
* Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
- * Copyright 1999, 2001 Bern
ardo Innocenti <bernie@develer.com
>
+ * Copyright 1999, 2001 Bern
ie Innocenti <bernie@codewiz.org
>
*
* -->
*
* -->
+ *
* \brief X-Modem serial transmission protocol (implementation)
*
* Supports the CRC-16 and 1K-blocks variants of the standard.
* \brief X-Modem serial transmission protocol (implementation)
*
* Supports the CRC-16 and 1K-blocks variants of the standard.
@@
-37,22
+38,25
@@
*
* \todo Break xmodem_send() and xmodem_recv() in smaller functions.
*
*
* \todo Break xmodem_send() and xmodem_recv() in smaller functions.
*
- * \version $Id$
- * \author Bernardo Innocenti <bernie@develer.com>
+ * \author Bernie Innocenti <bernie@codewiz.org>
* \author Francesco Sacchi <batt@develer.com>
*/
#include "xmodem.h"
* \author Francesco Sacchi <batt@develer.com>
*/
#include "xmodem.h"
-#include <appconfig.h>
-#include <string.h> /* for memset() */
-#include <drv/ser.h>
-#include <algo/crc.h>
+#include "cfg/cfg_xmodem.h"
+
#include <cfg/debug.h>
#include <cfg/debug.h>
+// Define log settings for cfg/log.h
+#define LOG_LEVEL CONFIG_XMODEM_LOG_LEVEL
+#define LOG_FORMAT CONFIG_XMODEM_LOG_FORMAT
+#include <cfg/log.h>
+#include <algo/crc.h>
+#include <string.h> /* for memset() */
/**
* \name Protocol control codes
/**
* \name Protocol control codes
@@
-67,9
+71,6
@@
#define XM_CAN 0x18 /**< CANcel transmission */
/*\}*/
#define XM_CAN 0x18 /**< CANcel transmission */
/*\}*/
-#define XM_MAXRETRIES 15 /**< Max retries before giving up */
-#define XM_MAXCRCRETRIES 7 /**< Max retries before switching to BCC */
-
#if CONFIG_XMODEM_1KCRC == 1
#define XM_BUFSIZE 1024 /**< 1024 bytes of block buffer */
#else
#if CONFIG_XMODEM_1KCRC == 1
#define XM_BUFSIZE 1024 /**< 1024 bytes of block buffer */
#else
@@
-81,12
+82,12
@@
/**
* \brief Receive a file using the XModem protocol.
*
/**
* \brief Receive a file using the XModem protocol.
*
- * \param
port Serial port
to use for transfer
+ * \param
ch Channel
to use for transfer
* \param fd Destination file
*
* \note This function allocates a large amount of stack (\see XM_BUFSIZE).
*/
* \param fd Destination file
*
* \note This function allocates a large amount of stack (\see XM_BUFSIZE).
*/
-bool xmodem_recv(
struct KFileSerial *port
, KFile *fd)
+bool xmodem_recv(
KFile *ch
, KFile *fd)
{
char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
int c, i, blocksize;
{
char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
int c, i, blocksize;
@@
-98,18
+99,18
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
bool usecrc = true;
bool usecrc = true;
-
XMODEM_PROGRESS
("Starting Transfer...\n");
+
LOG_INFO
("Starting Transfer...\n");
purge = true;
purge = true;
-
ser_clearerr(port
);
+
kfile_clearerr(ch
);
/* Send initial NAK to start transmission */
for(;;)
{
if (XMODEM_CHECK_ABORT)
{
/* Send initial NAK to start transmission */
for(;;)
{
if (XMODEM_CHECK_ABORT)
{
- kfile_putc(XM_CAN,
&port->fd
);
- kfile_putc(XM_CAN,
&port->fd
);
-
XMODEM_PROGRESS
("Transfer aborted\n");
+ kfile_putc(XM_CAN,
ch
);
+ kfile_putc(XM_CAN,
ch
);
+
LOG_INFO
("Transfer aborted\n");
return false;
}
return false;
}
@@
-121,41
+122,43
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
{
purge = false;
{
purge = false;
- if (kfile_error(&port->fd))
- XMODEM_PROGRESS("Retries %d\n", retries);
+ if (kfile_error(ch))
+ {
+ LOG_ERR("Retries %d\n", retries);
+ }
-
ser_resync(port
, 200);
+
kfile_resync(ch
, 200);
retries++;
retries++;
- if (retries >=
X
M_MAXRETRIES)
+ if (retries >=
CONFIG_XMODE
M_MAXRETRIES)
{
{
- kfile_putc(XM_CAN,
&port->fd
);
- kfile_putc(XM_CAN,
&port->fd
);
-
XMODEM_PROGRESS
("Transfer aborted\n");
+ kfile_putc(XM_CAN,
ch
);
+ kfile_putc(XM_CAN,
ch
);
+
LOG_INFO
("Transfer aborted\n");
return false;
}
/* Transmission start? */
if (blocknr == 0)
{
return false;
}
/* Transmission start? */
if (blocknr == 0)
{
- if (retries <
X
M_MAXCRCRETRIES)
+ if (retries <
CONFIG_XMODE
M_MAXCRCRETRIES)
{
{
-
XMODEM_PROGRESS
("Request Tx (CRC)\n");
- kfile_putc(XM_C,
&port->fd
);
+
LOG_INFO
("Request Tx (CRC)\n");
+ kfile_putc(XM_C,
ch
);
}
else
{
/* Give up with CRC and fall back to checksum */
usecrc = false;
}
else
{
/* Give up with CRC and fall back to checksum */
usecrc = false;
-
XMODEM_PROGRESS
("Request Tx (BCC)\n");
- kfile_putc(XM_NAK,
&port->fd
);
+
LOG_INFO
("Request Tx (BCC)\n");
+ kfile_putc(XM_NAK,
ch
);
}
}
else
}
}
else
- kfile_putc(XM_NAK,
&port->fd
);
+ kfile_putc(XM_NAK,
ch
);
}
}
- switch (kfile_getc(
&port->fd
))
+ switch (kfile_getc(
ch
))
{
#if XM_BUFSIZE >= 1024
case XM_STX: /* Start of header (1024-byte block) */
{
#if XM_BUFSIZE >= 1024
case XM_STX: /* Start of header (1024-byte block) */
@@
-169,27
+172,31
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
getblock:
/* Get block number */
getblock:
/* Get block number */
- c = kfile_getc(
&port->fd
);
+ c = kfile_getc(
ch
);
/* Check complemented block number */
/* Check complemented block number */
- if ((~c & 0xff) != kfile_getc(
&port->fd
))
+ if ((~c & 0xff) != kfile_getc(
ch
))
{
{
-
XMODEM_PROGRESS
("Bad blk (%d)\n", c);
+
LOG_WARN
("Bad blk (%d)\n", c);
purge = true;
break;
}
/* Determine which block is being sent */
if (c == (blocknr & 0xff))
purge = true;
break;
}
/* Determine which block is being sent */
if (c == (blocknr & 0xff))
+ {
/* Last block repeated */
/* Last block repeated */
- XMODEM_PROGRESS("Repeat blk %d\n", blocknr);
+ LOG_INFO("Repeat blk %d\n", blocknr);
+ }
else if (c == ((blocknr + 1) & 0xff))
else if (c == ((blocknr + 1) & 0xff))
+ {
/* Next block */
/* Next block */
- XMODEM_PROGRESS("Recv blk %d\n", ++blocknr);
+ LOG_INFO("Recv blk %d\n", ++blocknr);
+ }
else
{
/* Sync lost */
else
{
/* Sync lost */
-
XMODEM_PROGRESS
("Sync lost (%d/%d)\n", c, blocknr);
+
LOG_WARN
("Sync lost (%d/%d)\n", c, blocknr);
purge = true;
break;
}
purge = true;
break;
}
@@
-199,7
+206,7
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
crc = 0;
for (i = 0; i < blocksize; i++)
{
crc = 0;
for (i = 0; i < blocksize; i++)
{
- if ((c = kfile_getc(
&port->fd
)) == EOF)
+ if ((c = kfile_getc(
ch
)) == EOF)
{
purge = true;
break;
{
purge = true;
break;
@@
-219,7
+226,7
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
break;
/* Get the checksum byte or the CRC-16 MSB */
break;
/* Get the checksum byte or the CRC-16 MSB */
- if ((c = kfile_getc(
&port->fd
)) == EOF)
+ if ((c = kfile_getc(
ch
)) == EOF)
{
purge = true;
break;
{
purge = true;
break;
@@
-230,7
+237,7
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
crc = UPDCRC16(c, crc);
/* Get CRC-16 LSB */
crc = UPDCRC16(c, crc);
/* Get CRC-16 LSB */
- if ((c = kfile_getc(
&port->fd
)) == EOF)
+ if ((c = kfile_getc(
ch
)) == EOF)
{
purge = true;
break;
{
purge = true;
break;
@@
-240,7
+247,7
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
if (crc)
{
if (crc)
{
-
XMODEM_PROGRESS
("Bad CRC: %04x\n", crc);
+
LOG_ERR
("Bad CRC: %04x\n", crc);
purge = true;
break;
}
purge = true;
break;
}
@@
-248,7
+255,7
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
/* Compare the checksum */
else if (c != checksum)
{
/* Compare the checksum */
else if (c != checksum)
{
-
XMODEM_PROGRESS
("Bad sum: %04x/%04x\n", checksum, c);
+
LOG_ERR
("Bad sum: %04x/%04x\n", checksum, c);
purge = true;
break;
}
purge = true;
break;
}
@@
-261,25
+268,25
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
if (last_block_done < blocknr)
{
/* Call user function to flush the buffer */
if (last_block_done < blocknr)
{
/* Call user function to flush the buffer */
- if (kfile_write(fd, block_buffer, blocksize))
+ if (kfile_write(fd, block_buffer, blocksize)
== (size_t)blocksize
)
{
/* Acknowledge block and clear error counter */
{
/* Acknowledge block and clear error counter */
- kfile_putc(XM_ACK,
&port->fd
);
+ kfile_putc(XM_ACK,
ch
);
retries = 0;
last_block_done = blocknr;
}
else
{
/* User callback failed: abort transfer immediately */
retries = 0;
last_block_done = blocknr;
}
else
{
/* User callback failed: abort transfer immediately */
- retries =
X
M_MAXRETRIES;
+ retries =
CONFIG_XMODE
M_MAXRETRIES;
purge = true;
}
}
break;
case XM_EOT: /* End of transmission */
purge = true;
}
}
break;
case XM_EOT: /* End of transmission */
- kfile_putc
har(XM_ACK, &port->fd
);
-
XMODEM_PROGRESS
("Transfer completed\n");
+ kfile_putc
(XM_ACK, ch
);
+
LOG_INFO
("Transfer completed\n");
return true;
case EOF: /* Timeout or serial error */
return true;
case EOF: /* Timeout or serial error */
@@
-287,7
+294,7
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
break;
default:
break;
default:
-
XMODEM_PROGRESS
("Skipping garbage\n");
+
LOG_INFO
("Skipping garbage\n");
purge = true;
break;
}
purge = true;
break;
}
@@
-300,13
+307,13
@@
bool xmodem_recv(struct KFileSerial *port, KFile *fd)
/**
* \brief Transmit some data using the XModem protocol.
*
/**
* \brief Transmit some data using the XModem protocol.
*
- * \param
port Serial port
to use for transfer
+ * \param
ch Channel
to use for transfer
* \param fd Source file
*
* \note This function allocates a large amount of stack for
* the XModem transfer buffer (\see XM_BUFSIZE).
*/
* \param fd Source file
*
* \note This function allocates a large amount of stack for
* the XModem transfer buffer (\see XM_BUFSIZE).
*/
-bool xmodem_send(
struct KFileSerial *port
, KFile *fd)
+bool xmodem_send(
KFile *ch
, KFile *fd)
{
char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
size_t size = -1;
{
char block_buffer[XM_BUFSIZE]; /* Buffer to hold a block of data */
size_t size = -1;
@@
-324,9
+331,8
@@
bool xmodem_send(struct KFileSerial *port, KFile *fd)
*/
size = kfile_read(fd, block_buffer, XM_BUFSIZE);
*/
size = kfile_read(fd, block_buffer, XM_BUFSIZE);
- kfile_clearerr(&port->fd);
- ser_purge(port);
- XMODEM_PROGRESS("Wait remote host\n");
+ kfile_clearerr(ch);
+ LOG_INFO("Wait remote host\n");
for(;;)
{
for(;;)
{
@@
-336,21
+342,23
@@
bool xmodem_send(struct KFileSerial *port, KFile *fd)
if (XMODEM_CHECK_ABORT)
return false;
if (XMODEM_CHECK_ABORT)
return false;
- switch (c = kfile_getc(
&port->fd
))
+ switch (c = kfile_getc(
ch
))
{
case XM_NAK:
{
case XM_NAK:
-
XMODEM_PROGRESS
("Resend blk %d\n", blocknr);
+
LOG_INFO
("Resend blk %d\n", blocknr);
proceed = true;
break;
case XM_C:
if (c == XM_C)
{
proceed = true;
break;
case XM_C:
if (c == XM_C)
{
-
XMODEM_PROGRESS
("Tx start (CRC)\n");
+
LOG_INFO
("Tx start (CRC)\n");
usecrc = true;
}
else
usecrc = true;
}
else
- XMODEM_PROGRESS("Tx start (BCC)\n");
+ {
+ LOG_INFO("Tx start (BCC)\n");
+ }
proceed = true;
break;
proceed = true;
break;
@@
-362,26
+370,26
@@
bool xmodem_send(struct KFileSerial *port, KFile *fd)
/* Call user function to read in one block */
size = kfile_read(fd, block_buffer, XM_BUFSIZE);
/* Call user function to read in one block */
size = kfile_read(fd, block_buffer, XM_BUFSIZE);
-
XMODEM_PROGRESS
("Send blk %d\n", blocknr);
+
LOG_INFO
("Send blk %d\n", blocknr);
blocknr++;
retries = 0;
proceed = true;
break;
case EOF:
blocknr++;
retries = 0;
proceed = true;
break;
case EOF:
- kfile_clearerr(
&port->fd
);
+ kfile_clearerr(
ch
);
retries++;
retries++;
-
XMODEM_PROGRESS
("Retries %d\n", retries);
- if (retries <=
X
M_MAXRETRIES)
+
LOG_INFO
("Retries %d\n", retries);
+ if (retries <=
CONFIG_XMODE
M_MAXRETRIES)
break;
/* falling through! */
case XM_CAN:
break;
/* falling through! */
case XM_CAN:
-
XMODEM_PROGRESS
("Transfer aborted\n");
+
LOG_INFO
("Transfer aborted\n");
return false;
default:
return false;
default:
-
XMODEM_PROGRESS
("Skipping garbage\n");
+
LOG_INFO
("Skipping garbage\n");
break;
}
}
break;
}
}
@@
-389,7
+397,7
@@
bool xmodem_send(struct KFileSerial *port, KFile *fd)
if (!size)
{
if (!size)
{
- kfile_putc(XM_EOT,
&port->fd
);
+ kfile_putc(XM_EOT,
ch
);
continue;
}
continue;
}
@@
-398,19
+406,19
@@
bool xmodem_send(struct KFileSerial *port, KFile *fd)
/* Send block header (STX, blocknr, ~blocknr) */
#if XM_BUFSIZE == 128
/* Send block header (STX, blocknr, ~blocknr) */
#if XM_BUFSIZE == 128
- kfile_putc(XM_SOH,
&port->fd
);
+ kfile_putc(XM_SOH,
ch
);
#else
#else
- kfile_putc(XM_STX,
&port->fd
);
+ kfile_putc(XM_STX,
ch
);
#endif
#endif
- kfile_putc(blocknr & 0xFF,
&port->fd
);
- kfile_putc(~blocknr & 0xFF,
&port->fd
);
+ kfile_putc(blocknr & 0xFF,
ch
);
+ kfile_putc(~blocknr & 0xFF,
ch
);
/* Send block and compute its CRC/checksum */
sum = 0;
crc = 0;
for (i = 0; i < XM_BUFSIZE; i++)
{
/* Send block and compute its CRC/checksum */
sum = 0;
crc = 0;
for (i = 0; i < XM_BUFSIZE; i++)
{
- kfile_putc(block_buffer[i],
&port->fd
);
+ kfile_putc(block_buffer[i],
ch
);
crc = UPDCRC16(block_buffer[i], crc);
sum += block_buffer[i];
}
crc = UPDCRC16(block_buffer[i], crc);
sum += block_buffer[i];
}
@@
-418,13
+426,11
@@
bool xmodem_send(struct KFileSerial *port, KFile *fd)
/* Send CRC/Checksum */
if (usecrc)
{
/* Send CRC/Checksum */
if (usecrc)
{
- crc = UPDCRC16(0, crc);
- crc = UPDCRC16(0, crc);
- ser_putchar(crc >> 8, port);
- ser_putchar(crc & 0xFF, port);
+ kfile_putc(crc >> 8, ch);
+ kfile_putc(crc & 0xFF, ch);
}
else
}
else
- kfile_putc(sum,
&port->fd
);
+ kfile_putc(sum,
ch
);
}
}
#endif
}
}
#endif