Initial check-ing of (non-working) Zmodem receive logic

This commit is contained in:
Gregory Nutt 2013-07-14 11:05:23 -06:00
parent b614666243
commit 4d439eda39
6 changed files with 1867 additions and 95 deletions

View File

@ -49,7 +49,7 @@ STACKSIZE = 1536
ASRCS =
CSRCS = sz_main.c zm_send.c
CSRCS += rz_main.c
CSRCS += rz_main.c zm_receive.c
CSRCS += zm_state.c zm_proto.c zm_watchdog.c zm_utils.c zm_dumpbuffer.c
AOBJS = $(ASRCS:.S=$(OBJEXT))

View File

@ -1,5 +1,5 @@
/****************************************************************************
* system/xmodem/rz_main.c
* system/zmodem/rz_main.c
*
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
@ -40,28 +40,113 @@
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <libgen.h>
#include <time.h>
#include <errno.h>
#include <apps/zmodem.h>
/****************************************************************************
* Definitions
* Private Functions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static void show_usage(FAR const char *progname, int errcode)
{
fprintf(stderr, "USAGE: %s [OPTIONS]\n",
progname);
fprintf(stderr, "\nWhere OPTIONS include the following:\n");
fprintf(stderr, "\t-d <device>: Communication device to use. Default: %s\n",
CONFIG_SYSTEM_ZMODEM_DEVNAME);
fprintf(stderr, "\t-h: Show this text and exit\n");
exit(errcode);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: rz_main
****************************************************************************/
int rz_main(int argc, char *argv[])
int rz_main(int argc, FAR char **argv)
{
printf("Not yet implemented!!\n");
return 0;
}
ZMRHANDLE handle;
FAR const char *devname = CONFIG_SYSTEM_ZMODEM_DEVNAME;
int exitcode = EXIT_FAILURE;
int option;
int ret;
int fd;
/* Parse input parameters */
while ((option = getopt(argc, argv, ":d:h")) != ERROR)
{
switch (option)
{
case 'd':
devname = optarg;
break;
case 'h':
show_usage(argv[0], EXIT_SUCCESS);
break;
case ':':
fprintf(stderr, "ERROR: Missing required argument\n");
show_usage(argv[0], EXIT_FAILURE);
break;
default:
case '?':
fprintf(stderr, "ERROR: Unrecognized option\n");
show_usage(argv[0], EXIT_FAILURE);
break;
}
}
/* Nothing else is expected on the command line */
if (optind < argc)
{
printf("ERROR: Too many command line arguments\n");
show_usage(argv[0], EXIT_FAILURE);
}
/* Open the device for read/write access */
fd = open(devname, O_RDWR);
if (fd < 0)
{
fprintf(stderr, "ERROR: Failed to open %s\n", devname);
goto errout;
}
/* Get the Zmodem handle */
handle = zmr_initialize(fd);
if (!handle)
{
fprintf(stderr, "ERROR: Failed to get Zmodem handle\n");
goto errout_with_device;
}
/* And begin reception of files */
ret = zmr_receive(handle);
if (ret < 0)
{
fprintf(stderr, "File reception failed: %d\n", ret);
goto errout_with_zmodem;
}
exitcode = EXIT_SUCCESS;
errout_with_zmodem:
(void)zmr_release(handle);
errout_with_device:
(void)close(fd);
errout:
return exitcode;
}

View File

@ -183,6 +183,7 @@
#define ZME_COMMAND ZCOMMAND /* Command, from sending program */
#define ZME_STDERR ZSTDERR /* Output this message to stderr */
#define ZME_OO 252 /* Received OO, termining the receiver */
#define ZME_DATARCVD 253 /* Data received */
#define ZME_TIMEOUT 254 /* Timeout */
#define ZME_ERROR 255 /* Protocol error */
@ -198,6 +199,7 @@
#define ZM_FLAG_WAIT (1 << 6) /* Next send should wait */
#define ZM_FLAG_APPEND (1 << 7) /* Append to the existing file */
#define ZM_FLAG_TIMEOUT (1 << 8) /* A timeout has been detected */
#define ZM_FLAG_OO (1 << 9) /* "OO" may be received */
/* The Zmodem parser success/error return code definitions:
*
@ -241,8 +243,7 @@ enum parser_state_e
{
PSTATE_IDLE = 0, /* Nothing in progress */
PSTATE_HEADER, /* Parsing a header following ZPAD ZDLE */
PSTATE_DATA, /* Sending data */
PSTATE_FINISH /* Waiting for termination handshake */
PSTATE_DATA, /* Sending/receiving data */
};
/* PSTATE_IDLE substates */
@ -250,8 +251,8 @@ enum parser_state_e
enum pidle_substate_e
{
PIDLE_ZPAD = 0, /* Waiting for ZPAD */
PIDLE_ZDLE, /* Waiting for ZDLE */
PIDLE_ZDLEE /* Waiting for ZDLEE */
PIDLE_ZDLE, /* ZPAD received, waiting for ZDLE */
PIDLE_OO /* First 'O' received, waiting for second 'O' of "OO" */
};
/* PSTATE_HEADER substates */
@ -271,14 +272,6 @@ enum pdata_substate_e
PDATA_CRC /* Have the packet type, accumulating the CRC */
};
/* PSTATE_FINISH substates */
enum pfinish_substate_e
{
PFINISH_1STO = 0, /* Waiting for first 'O' */
PFINISH_2NDO /* Waiting for second 'O' */
};
/* This type describes the method to perform actions at the time of
* a state transition.
*/

1684
system/zmodem/zm_receive.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -131,7 +131,7 @@ enum zmodem_state_e
****************************************************************************/
/* Transition actions */
static int zms_rinit(FAR struct zm_state_s *pzm);
static int zms_zrinit(FAR struct zm_state_s *pzm);
static int zms_attention(FAR struct zm_state_s *pzm);
static int zms_challenge(FAR struct zm_state_s *pzm);
static int zms_abort(FAR struct zm_state_s *pzm);
@ -179,7 +179,7 @@ static int zms_sendfile(FAR struct zms_state_s *pzms, FAR const char *filename,
static const struct zm_transition_s g_zms_start[] =
{
{ZME_RINIT, true, ZMS_START, zms_rinit},
{ZME_RINIT, true, ZMS_START, zms_zrinit},
{ZME_SINIT, false, ZMS_START, zms_ignore},
{ZME_CHALLENGE, true, ZMS_START, zms_challenge},
{ZME_ABORT, true, ZMS_FINISH, zms_abort},
@ -195,12 +195,12 @@ static const struct zm_transition_s g_zms_start[] =
* ZSINIT, waiting for ZACK
*/
static const struct zm_transition_s g_zms_init[] =
static const struct zm_transition_s g_zmr_initack[] =
{
{ZME_SINIT, false, ZMS_START, zms_ignore},
{ZME_ACK, true, ZMS_INITACK, zms_initdone},
{ZME_NAK, true, ZMS_INITACK, zms_sendzsinit},
{ZME_RINIT, true, ZMS_INITACK, zms_rinit},
{ZME_RINIT, true, ZMS_INITACK, zms_zrinit},
{ZME_CHALLENGE, true, ZMS_INITACK, zms_challenge},
{ZME_ABORT, true, ZMS_FINISH, zms_abort},
{ZME_FERR, true, ZMS_FINISH, zms_abort},
@ -254,7 +254,7 @@ static const struct zm_transition_s g_zms_crcwait[] =
* interrupt
*/
static const struct zm_transition_s g_zms_streaming[] =
static const struct zm_transition_s g_zmr_sending[] =
{
{ZME_SINIT, false, ZMS_START, zms_attention},
{ZME_ACK, false, ZMS_SENDING, zms_sendack},
@ -353,7 +353,7 @@ static struct zm_transition_s g_zms_command[] =
/* Events handled in state ZMS_MESSAGE - Waiting for stderr data */
static struct zm_transition_s g_zms_stderr[] =
static struct zm_transition_s g_zms_message[] =
{
{ZME_SINIT, false, ZMS_START, zms_ignore},
{ZME_DATARCVD, false, ZMS_MESSAGE, zms_stderrdata},
@ -375,18 +375,18 @@ static struct zm_transition_s g_zms_done[] =
static FAR const struct zm_transition_s * const g_zms_evtable[] =
{
g_zms_start, /* ZMS_START - ZRQINIT sent, waiting for ZRINIT from receiver */
g_zms_init, /* ZMS_INITACK - Received ZRINIT, sent ZSINIT, waiting for ZACK */
g_zms_filewait, /* ZMS_FILEWAIT - Sent file header, waiting for ZRPOS */
g_zms_crcwait, /* ZMS_CRCWAIT - Sent file CRC, waiting for ZRPOS response */
g_zms_streaming, /* ZMS_SENDING - Sending data subpackets, ready for interrupt */
g_zms_sendwait, /* ZMS_SENDWAIT - Waiting for ZACK */
g_zms_senddone, /* ZMS_SENDDONE - File sent, need to send ZEOF */
g_zms_sendeof, /* ZMS_SENDEOF - Sent ZEOF, waiting for ZACK */
g_zms_finish, /* ZMS_FINISH - Sent ZFIN, waiting for ZFIN */
g_zms_command, /* ZMS_COMMAND - Waiting for command data */
g_zms_stderr, /* ZMS_MESSAGE - Waiting for message from receiver */
g_zms_done /* ZMS_DONE - Finished with transfer */
g_zms_start, /* ZMS_START: ZRQINIT sent, waiting for ZRINIT from receiver */
g_zmr_initack, /* ZMS_INITACK: Received ZRINIT, sent ZSINIT, waiting for ZACK */
g_zms_filewait, /* ZMS_FILEWAIT: Sent file header, waiting for ZRPOS */
g_zms_crcwait, /* ZMS_CRCWAIT: Sent file CRC, waiting for ZRPOS response */
g_zmr_sending, /* ZMS_SENDING: Sending data subpackets, ready for interrupt */
g_zms_sendwait, /* ZMS_SENDWAIT: Waiting for ZACK */
g_zms_senddone, /* ZMS_SENDDONE: File sent, need to send ZEOF */
g_zms_sendeof, /* ZMS_SENDEOF: Sent ZEOF, waiting for ZACK */
g_zms_finish, /* ZMS_FINISH: Sent ZFIN, waiting for ZFIN */
g_zms_command, /* ZMS_COMMAND: Waiting for command data */
g_zms_message, /* ZMS_MESSAGE: Waiting for message from receiver */
g_zms_done /* ZMS_DONE: Finished with transfer */
};
/****************************************************************************
@ -394,7 +394,7 @@ static FAR const struct zm_transition_s * const g_zms_evtable[] =
****************************************************************************/
/****************************************************************************
* Name: zms_rinit
* Name: zms_zrinit
*
* Description:
* Received ZRINIT. Usually received while in start state, this can
@ -402,7 +402,7 @@ static FAR const struct zm_transition_s * const g_zms_evtable[] =
*
****************************************************************************/
static int zms_rinit(FAR struct zm_state_s *pzm)
static int zms_zrinit(FAR struct zm_state_s *pzm)
{
FAR struct zms_state_s *pzms = (FAR struct zms_state_s *)pzm;
uint16_t rcaps;
@ -1122,7 +1122,7 @@ static int zms_sendpacket(FAR struct zm_state_s *pzm)
}
}
#ifdef CONFIG_SYSTEM_ZMODEM_RCVSAMPLE
while (pzm->state != ZMS_SENDING && !zm_rcvpending(pzm));
while (pzm->state == ZMS_SENDING && !zm_rcvpending(pzm));
#else
while (0);
#endif
@ -1746,6 +1746,13 @@ int zms_release(ZMSHANDLE handle)
(void)zm_timerrelease(&pzms->cmn);
/* Make sure that the file is closed */
if (pzms->infd)
{
close(pzms->infd);
}
/* And free the Zmodem state structure */
free(pzms);

View File

@ -84,7 +84,6 @@
static int zm_idle(FAR struct zm_state_s *pzm, uint8_t ch);
static int zm_header(FAR struct zm_state_s *pzm, uint8_t ch);
static int zm_data(FAR struct zm_state_s *pzm, uint8_t ch);
static int zm_finish(FAR struct zm_state_s *pzm, uint8_t ch);
/****************************************************************************
* Private Data
@ -126,7 +125,7 @@ static int zm_event(FAR struct zm_state_s *pzm, int event)
}
zmdbg("Transition ZM[R|S]_state %d->%d discard: %d action: %p\n",
pzm->state, ptr->next, ptr->bdiscard, ptr->action);
pzm->state, ptr->next, ptr->bdiscard, ptr->action);
/* Perform the state transition */
@ -308,8 +307,8 @@ static int zm_idle(FAR struct zm_state_s *pzm, uint8_t ch)
case ZDLE:
/* Was the ZDLE preceded by ZPAD[s]? If not, fall through and treat
* as the default case.
/* Was the ZDLE preceded by ZPAD[s]? If not, revert to the PIDLE_ZPAD
* substate.
*/
if (pzm->psubstate == PIDLE_ZDLE)
@ -317,12 +316,59 @@ static int zm_idle(FAR struct zm_state_s *pzm, uint8_t ch)
zmdbg("PSTATE %d:%d->%d.%d\n",
pzm->pstate, pzm->psubstate, PSTATE_HEADER, PHEADER_FORMAT);
pzm->flags &= ~ZM_FLAG_OO;
pzm->pstate = PSTATE_HEADER;
pzm->psubstate = PHEADER_FORMAT;
break;
}
else
{
zmdbg("PSTATE %d:%d->%d.%d\n",
pzm->pstate, pzm->psubstate, pzm->pstate, PIDLE_ZPAD);
/* Unexpected character. Wait for the next ZPAD to get us */
pzm->psubstate = PIDLE_ZPAD;
}
/* O might be the first character of "OO". "OO" might be part of the file
* receiver protocol. After receiving on e file in a group of files, the
* receiver expected either "OO" indicating that all files have been sent,
* or a ZRQINIT header indicating the start of the next file.
*/
case 'O':
/* Is "OO" a possibility in this context? Fall through to the default
* case if not.
*/
if ((pzm->state & ZM_FLAG_OO) != 0)
{
/* Yes... did we receive an 'O' before this one? */
if (pzm->psubstate != PIDLE_OO)
{
/* This is the second 'O' of "OO". the receiver operation is
* finished.
*/
zmdbg("PSTATE %d:%d->%d.%d\n",
pzm->pstate, pzm->psubstate, pzm->pstate, PIDLE_ZPAD);
pzm->psubstate = PIDLE_ZPAD;
return zm_event(pzm, ZME_OO);
}
else
{
/* No... this is the first 'O' that we have seen */
zmdbg("PSTATE %d:%d->%d.%d\n",
pzm->pstate, pzm->psubstate, pzm->pstate, PIDLE_OO);
pzm->psubstate = PIDLE_OO;
}
break;
}
/* Unexpected character. Wait for the next ZPAD to get us back in sync. */
default:
if (pzm->psubstate != PIDLE_ZPAD)
@ -661,45 +707,6 @@ static int zm_data(FAR struct zm_state_s *pzm, uint8_t ch)
return OK;
}
/****************************************************************************
* Name: zm_finish
*
* Description:
* Data has been received in state PSTATE_FINISH. Juse wait for "OO"
*
****************************************************************************/
static int zm_finish(FAR struct zm_state_s *pzm, uint8_t ch)
{
/* Wait for "OO" */
if (ch == 'O')
{
/* Have we seen the second '0'? */
if (pzm->psubstate == PFINISH_2NDO)
{
/* Yes.. then we are finished */
return ZM_XFRDONE;
}
else
{
/* No.. this was the first */
pzm->psubstate = PFINISH_2NDO;
}
}
else
{
/* Reset. We have not seen the first 'O' of the pair */
pzm->psubstate = PFINISH_1STO;
}
return OK;
}
/****************************************************************************
* Name: zm_parse
*
@ -780,10 +787,6 @@ static int zm_parse(FAR struct zm_state_s *pzm, size_t rcvlen)
ret = zm_data(pzm, ch);
break;
case PSTATE_FINISH:
ret = zm_finish(pzm, ch);
break;
/* This should not happen */
default: