Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603630
  • 博文数量: 99
  • 博客积分: 5128
  • 博客等级: 大校
  • 技术积分: 1538
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-27 19:40
文章分类

全部博文(99)

文章存档

2012年(3)

2011年(5)

2010年(4)

2009年(31)

2008年(56)

分类: LINUX

2009-09-02 17:38:04

/*
 * vivi/deriver/serialxmodem.c:
 * - an implementation of the xmodem protocol from the spec.
 *
 * Based on bootldr/xmodem.c (C) 2000 Compaq Computer Coporation.
 *
 * Copyright (C) 2001 MIZI Research, Inc.
 *
 *
 * 1999-01-xx: Edwin Foo
 * - Initial code
 *
 * 2001-10-04: Janghoon Lyu
 * - Modified a little bit.
 *
 * 2002-07-04: Janghoon Lyu
 */


#include "config.h"
#include "machine.h"
#include "serial.h"
#include "priv_data.h"
#include <types.h>

/* XMODEM parameters */
#define BLOCK_SIZE    128    /* size of transmit blocks */
#define RETRIES        20    /* maximum number of RETRIES */

/* Line control codes */
#define SOH        0x01    /* start of header */
#define ACK        0x06    /* Acknowledge */
#define NAK        0x15    /* Negative acknowledge */
#define CAN        0x18    /* Cancel */
#define EOT        0x04    /* end of text */

#define GET_BYTE_TIMEOUT 10000000

/* global error variable */
char *xmodem_errtxt = NULL;
int get_byte_err = 0;
__u8 volatile rbuffer[BLOCK_SIZE];

/* prototypes of helper functions */
int get_record(void);
__u8 get_byte(void);

enum
{
    SAC_SEND_NAK = 0,
    SAC_SENT_NAK = 1,
    SAC_PAST_START_NAK = 2
};

static volatile int seen_a_char = SAC_SEND_NAK;

static int one_nak = 0;
static unsigned long xmodem_timeout = GET_BYTE_TIMEOUT;

char debugbuf[4096];
int db_idx = 0;

void bufputs(char *s)
{
    size_t len = strlen(s) + 1;

    if (len + db_idx > sizeof(debugbuf))
        len = sizeof(debugbuf) - db_idx;

    if (len) {
        memcpy(&debugbuf[db_idx], s, len);
        db_idx += len;
    }
}

void reset_debugbuf(void)
{
    memset(debugbuf, 0x2a, sizeof(debugbuf));
    db_idx = 0;
}

__u32 xmodem_receive(char *dldaddr, size_t len)
{
    char ochr;
    int r = 0, rx_block_num = 0, error_count = 0;
    __u32 foffset = 0;
    int i;
    int ret;

    xmodem_errtxt = NULL;
    seen_a_char = 0;

    one_nak = get_param_value("xmodem_one_nak", &ret);
    if (ret) one_nak = 0;

    xmodem_timeout = get_param_value("xmodem_initial_timeout", &ret);
    if (ret) xmodem_timeout = GET_BYTE_TIMEOUT;

    rx_block_num = 1; // 一共接收到了多少个包

    error_count = RETRIES; // 重试次数 20次


    do {
        // rx_block_num & 255 的意思是求出当前的包的编号,因为每次最多

        // 是 256个包而已0-255,并且 rx_block_num 应该是指向下一个应该

        // 接收到的正确的包的编号,如果相等,表示今次接收的包是正确的

        if ((r = get_record()) == (rx_block_num & 255)) {
            error_count = RETRIES; // 重置重试次数

            for (i = 0; i <BLOCK_SIZE; i++) // 将一个包复制到指定的地址

                *(__u8 *)(dldaddr+foffset+i) = rbuffer[i];
            xmodem_errtxt = "RX PACKET"; // 错误信息,没有错误,接收一个包完毕

            rx_block_num++; // 接收到的总包数加1

            ochr = ACK; // 回应 ACK 信号

            foffset += BLOCK_SIZE; // 写入的目标地址的偏移,相应加上一个block

        } else {
            switch (r) { // 否则,处理错误代码

                // NAK 信号的意思是请求发送方重新发一个包

                // ACK 信号解释为正确接收

                // 下面可以看出,超时,错误的checksum

            case -1: /* TIMEOUT */
                xmodem_errtxt = "TIMEOUT";
                ochr = NAK;
                break;
            case -2: /* Bad block */
                xmodem_errtxt = "BAD BLOCK#";
                /* eat teh rest of the block */
                get_byte_err = 0;
                while (get_byte_err != -1) get_byte();
                ochr = NAK;
                break;
            case -3: /* Bad checksum */
                xmodem_errtxt = "BAD CHKSUM";
                ochr = NAK;
                break;
            case -4: /* End of file */
                xmodem_errtxt = "DONE";
                ochr = ACK;
                break;
            case -5: /* Cancel */
                xmodem_errtxt = "ABORTED";
                ochr = ACK;
                break;
            default: /* Block out of sequence */
                xmodem_errtxt = "WRONG BLK";
                ochr = NAK;
            }
            error_count--; // 错误加一

        }
        putc(ochr); // 发送应答信号,很简单,putc就是接收-->发送端的信号了

    } while ((r > -3) && error_count);
    // r>-3表示 DONE 和 ABORTED 之外的情况,也就是说,没有完成或者没有中途退出

    // 的话就一直循环,但是如果错误技术过多的话也停止传输。


    if ((!error_count) || (r != -4)) { // 表示出错了

        foffset = 0;    /* indicate failure to caller */
        /*printk("x-modem error: %s\n", xmodem_errtxt); */
    }

    return foffset; // 返回最后指向的指针,应该是文件的结尾处表示正确,0表示错误

}

/*
 * Read a record in the XMODEM protocol, return the block number
 * (0-255) if successful, or one of the following return codes:
 *     -1 = Bad byte
 *     -2 = Bad block number
 *     -3 = Bad block checksum
 *     -4 = End of file
 *     -5 = Canceled by remote
 *
 * 读取一个块,每个块的格式为(根据 xmodem协议)
 *
 *  | | | | | |
 *  | SOH | 信息包序号 | 信息包序号的补码 | 数据区段 | 算术校验和 |
 *  |_____|________ _ |________________ |________ |__________ |
 *
 *
 */

int get_record(void)
{
    int c, block_num = 0;
    int i;
    __u32 check_sum;

    /* clear the buffer,接收缓冲,存128字节的数据区段 */
    for (i = 0; i < BLOCK_SIZE; i++)
        rbuffer[i] = 0x00;

    check_sum = 0;
    i = -2;
    // 首先读一个字节,判断是否有出错

    c = get_byte();
    if (get_byte_err)
        return -1;

    switch (c) {
    case SOH:    /* 如果接收的是头信号的话 Receive packet */
        for (;;) { // 一直循环到 128字节读取完毕

            c = get_byte(); // 读一个字节判断有没有错误

            if (get_byte_err)
                return -1;

            switch (i) {
            case -2: // 如果 i 为 -2 表示现在接收的是信息包的序号

                block_num = c; // 保存起来以便返回

                break;
            case -1:
#if 0
#ifdef CHECK_NEGATED_SECTNUM
                if (c != (-block_num -1))
                    return -2;
#endif
#endif
                break;
            case BLOCK_SIZE: // 如果现在计数到达 128表示这是一个CHECKSUM

                // 因为包的编号是 0-127一共128个,那么128编号的包为校验和

                if ((check_sum & 0xff) != c) // 将接收到的校验和和自己计算的比较

                    return -3; // 不对就返回错误

                else
                    return block_num; // 正确,返回包的编号

                break;
            default: // 默认的话就是一个块里面的每个字节

                rbuffer[i] = c; // 存放数据

                check_sum += c; // checksum只是很简单的相加

            }
            i++;
        }
    case EOT:    /* end of file encountered */
        return -4;
    case CAN:    /* cancel protocol */
        return -5;
    default:
        return -5;
    }
}

/* get_byte should use one of the timer's for a CPU clock independent timeout */
__u8 get_byte()
{
    int c, ret;

again:
    c = 0;
    get_byte_err = 0; /* reset errno */
    c = awaitkey(xmodem_timeout, &get_byte_err); // 从UART读取一个字节,超时时间为 xmodem_timeout


    if (get_byte_err) {
        if (seen_a_char == SAC_SEND_NAK || !one_nak) {
            bufputs("timeout nak");
            putc(NAK);    /* make the sender go */
        }

        if (seen_a_char < SAC_PAST_START_NAK) {
            bufputs("goto again");
            seen_a_char = SAC_SENT_NAK;

            xmodem_timeout = get_param_value("xmodem_timeout", &ret);
            if (ret) xmodem_timeout = GET_BYTE_TIMEOUT;
            goto again;
        }
    }

    if (get_byte_err == 0)
        seen_a_char = SAC_PAST_START_NAK;

    return (c);
}

阅读(1799) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~