Merge pull request #167 from grissiom/ymodem

components/utilities: add ymodem
This commit is contained in:
Bernard Xiong committed 2013-10-09 08:33:02 -07:00
commit 69bbd35f38
6 files changed
+704

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('Utilities', src, depend = ['RT_USING_RYM'], CPPPATH = CPPPATH)
Return('group')
File diff suppressed because it is too large. Load diff
+135
View File
@@ -0,0 +1,135 @@
#ifndef __YMODEM_H__
#define __YMODEM_H__
/*
* File : ymodem.h
* COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd
*
* Change Logs:
* Date Author Notes
* 2013-04-14 Grissiom initial implementation
*/
#include "rtthread.h"
/* The word "RYM" is stand for "Real-YModem". */
enum rym_code {
RYM_CODE_NONE = 0x00,
RYM_CODE_SOH = 0x01,
RYM_CODE_STX = 0x02,
RYM_CODE_EOT = 0x04,
RYM_CODE_ACK = 0x06,
RYM_CODE_NAK = 0x15,
RYM_CODE_CAN = 0x18,
RYM_CODE_C = 0x43,
};
/* RYM error code
*
* We use the rt_err_t to return error values. We take use of current error
* codes available in RTT and append ourselves.
*/
/* timeout on handshake */
#define RYM_ERR_TMO 0x70
/* wrong code, wrong SOH, STX etc. */
#define RYM_ERR_CODE 0x71
/* wrong sequence number */
#define RYM_ERR_SEQ 0x72
/* wrong CRC checksum */
#define RYM_ERR_CRC 0x73
/* not enough data received */
#define RYM_ERR_DSZ 0x74
/* the transmission is aborted by user */
#define RYM_ERR_CAN 0x75
/* how many ticks wait for chars between packet. */
#ifndef RYM_WAIT_CHR_TICK
#define RYM_WAIT_CHR_TICK (RT_TICK_PER_SECOND * 3)
#endif
/* how many ticks wait for between packet. */
#ifndef RYM_WAIT_PKG_TICK
#define RYM_WAIT_PKG_TICK (RT_TICK_PER_SECOND * 3)
#endif
/* how many ticks between two handshake code. */
#ifndef RYM_CHD_INTV_TICK
#define RYM_CHD_INTV_TICK (RT_TICK_PER_SECOND / 4)
#endif
enum rym_stage {
RYM_STAGE_NONE,
/* set when C is send */
RYM_STAGE_ESTABLISHING,
/* set when we've got the packet 0 and sent ACK and second C */
RYM_STAGE_ESTABLISHED,
/* set when the sender respond to our second C */
RYM_STAGE_TRANSMITTING,
/* set when the sender send a EOT */
RYM_STAGE_FINISHING,
/* set when transmission is really finished, i.e., after the NAK, C, final
* NULL packet stuff. */
RYM_STAGE_FINISHED,
};
struct rym_ctx;
/* when receiving files, the buf will be the data received from ymodem protocol
* and the len is the data size.
*
* TODO:
* When sending files, the len is the buf size in RYM. The callback need to
* fill the buf with data to send. Returning RYM_CODE_EOT will terminate the
* transfer and the buf will be discarded. Any other return values will cause
* the transfer continue.
*/
typedef enum rym_code (*rym_callback)(struct rym_ctx *ctx, rt_uint8_t *buf, rt_size_t len);
/* Currently RYM only support one transfer session(ctx) for simplicity.
*
* In case we could support multiple sessions in The future, the first argument
* of APIs are (struct rym_ctx*).
*/
struct rym_ctx
{
rym_callback on_data;
rym_callback on_begin;
rym_callback on_end;
/* When error happened, user need to check this to get when the error has
* happened. */
enum rym_stage stage;
/* user could get the error content through this */
rt_uint8_t *buf;
struct rt_semaphore sem;
rt_device_t dev;
};
/** recv a file on device dev with ymodem session ctx.
*
* If an error happens, you can get where it is failed from ctx->stage.
*
* @param on_begin The callback will be invoked when the first packet arrived.
* This packet often contain file names and the size of the file, if the sender
* support it. So if you want to save the data to a file, you may need to
* create the file on need. It is the on_begin's responsibility to parse the
* data content. The on_begin can be NULL, in which case the transmission will
* continue without any side-effects.
*
* @param on_data The callback will be invoked on the packets received. The
* callback should save the data to the destination. The return value will be
* sent to the sender and in turn, only RYM_{ACK,CAN} is valid. When on_data is
* NULL, RYM will barely send ACK on every packet and have no side-effects.
*
* @param on_end The callback will be invoked when one transmission is
* finished. The data should be 128 bytes of NULL. You can do some cleaning job
* in this callback such as closing the file. The return value of this callback
* is ignored. As above, this parameter can be NULL if you don't need such
* function.
*
* @param handshake_timeout the timeout when hand shaking. The unit is in
* second.
*/
rt_err_t rym_recv_on_device(struct rym_ctx *ctx, rt_device_t dev,
rym_callback on_begin, rym_callback on_data, rym_callback on_end,
int handshake_timeout);
#endif
+59
View File
@@ -0,0 +1,59 @@
#include <ymodem.h>
static rt_device_t _odev;
static enum rym_code _rym_echo_data(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
rt_device_write(_odev, 0, buf, len);
return RYM_CODE_ACK;
}
rt_err_t rym_cat_to_dev(rt_device_t idev, rt_device_t odev)
{
struct rym_ctx rctx;
rt_err_t res;
_odev = odev;
rt_kprintf("entering RYM mode\n");
odev->flag &= ~RT_DEVICE_FLAG_STREAM;
res = rt_device_open(odev, 0);
if (res != RT_EOK)
{
rt_kprintf("open output device error: 0x%x", -res);
return res;
}
res = rym_recv_on_device(&rctx, idev,
RT_NULL, _rym_echo_data, RT_NULL, 1000);
rt_device_close(_odev);
rt_kprintf("leaving RYM mode with code %X\n", res);
return res;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
void rym_cat_vcom(void)
{
extern rt_err_t rym_cat_to_dev(rt_device_t idev, rt_device_t odev);
rt_device_t idev, odev;
rt_thread_delay(RT_TICK_PER_SECOND*10);
idev = rt_device_find("uart1");
if (!idev)
{
rt_kprintf("could not find idev\n");
}
odev = rt_device_find("vcom");
if (!odev)
{
rt_kprintf("could not find odev\n");
}
rym_cat_to_dev(idev, odev);
}
FINSH_FUNCTION_EXPORT(rym_cat_vcom, test the YModem);
#endif
+28
View File
@@ -0,0 +1,28 @@
#include <ymodem.h>
static enum rym_code _rym_dummy_write(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
return RYM_CODE_ACK;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
rt_err_t rym_null(char *devname)
{
struct rym_ctx rctx;
rt_device_t dev = rt_device_find(devname);
if (!dev)
{
rt_kprintf("could not find device %s\n", devname);
return -1;
}
return rym_recv_on_device(&rctx, dev,
RT_NULL, _rym_dummy_write, RT_NULL, 1000);
}
FINSH_FUNCTION_EXPORT(rym_null, dump data to null);
#endif
+113
View File
@@ -0,0 +1,113 @@
#include <rtthread.h>
#include <ymodem.h>
#include <dfs_posix.h>
#include <stdlib.h>
#include <board.h>
struct custom_ctx
{
struct rym_ctx parent;
int fd;
int flen;
char fpath[256];
};
static enum rym_code _rym_bg(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx*)ctx;
cctx->fpath[0] = '/';
/* the buf should be the file name */
strcpy(&(cctx->fpath[1]), buf);
cctx->fd = open(cctx->fpath, O_CREAT | O_WRONLY | O_TRUNC, 0);
if (cctx->fd < 0)
{
rt_err_t err = rt_get_errno();
rt_kprintf("error creating file: %d\n", err);
rt_kprintf("abort transmission\n");
return RYM_CODE_CAN;
}
cctx->flen = atoi(buf+strlen(buf)+1);
if (cctx->flen == 0)
cctx->flen = -1;
return RYM_CODE_ACK;
}
static enum rym_code _rym_tof(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx*)ctx;
RT_ASSERT(cctx->fd >= 0);
if (cctx->flen == -1)
{
write(cctx->fd, buf, len);
}
else
{
int wlen = len > cctx->flen ? cctx->flen : len;
write(cctx->fd, buf, wlen);
cctx->flen -= wlen;
}
return RYM_CODE_ACK;
}
static enum rym_code _rym_end(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx*)ctx;
RT_ASSERT(cctx->fd >= 0);
close(cctx->fd);
cctx->fd = -1;
return RYM_CODE_ACK;
}
rt_err_t rym_write_to_file(rt_device_t idev)
{
rt_err_t res;
struct custom_ctx *ctx = rt_malloc(sizeof(*ctx));
RT_ASSERT(idev);
rt_kprintf("entering RYM mode\n");
res = rym_recv_on_device(&ctx->parent, idev,
_rym_bg, _rym_tof, _rym_end, 1000);
/* there is no Ymodem traffic on the line so print out info. */
rt_kprintf("leaving RYM mode with code %d\n", res);
rt_kprintf("file %s has been created.\n", ctx->fpath);
rt_free(ctx);
return res;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
rt_err_t ry(char *dname)
{
rt_err_t res;
rt_device_t dev = rt_device_find(dname);
if (!dev)
{
rt_kprintf("could not find device:%s\n", dname);
return -RT_ERROR;
}
res = rym_write_to_file(dev);
return res;
}
FINSH_FUNCTION_EXPORT(ry, receive files by ymodem protocol);
#endif