www.pudn.com > Linuxserial.zip > ssf.c


/*
 * File         : ssf.c
 * Date         : 2002-04-20
 * Author       : yfy001
 * Description  : transmit a file via a serial port
 * Copyright (C) 2001, 2002  yfy001
 */

#include               // printf
#include               // open
#include              // bzero
#include              // exit
#include           // times
#include              // read, write, close
#include            // lstat

#include "types.h"
#include "tty.h"
#include "args.h"

static pkginfo_t pkginfo = {
    "ssf",                      // program
    "serutils",                 // package
    "0.0.6",                    // version
    "yfy001",                // author
    0                           // reserved
};

static portinfo_t portinfo = {
    0,                          // print prompt after receiving
    115200,                     // baudrate: 115200
    8,                          // databit: 8
    0,                          // debug: off
    0,                          // echo: off
    1,                          // flow control: hardware
    0,                          // tty: /dev/ttyS0
    0,                          // parity: none
    1,                          // stopbit: 1
    0                           // reserved
};

static args_t args = { &pkginfo, &portinfo };
p_args_t p_args = &args;

static int SendAllFiles(char **pathname);

int main(int argc, char *argv[])
{
    int retval = 0;

    args_parse(argc, argv, 1);
    atexit(tty_close);
    // open comport 
    retval = tty_open(p_args->p_portinfo);
    if (retval) {
        fprintf(stderr, "Make sure /dev/ttyS%d "
                "not in use or you have enough privilege.\n\n",
                p_args->p_portinfo->tty);
        exit(retval);
    }
    SendAllFiles(&argv[optind]);
    exit(0);
}

static int SendAllFiles(char **pathname)
{
    int oldtick, newtick;
    int systick;
    float kbps = 0, time = 0;
    int byteSend = 0;
    struct stat stat;
    int num = 0;
    oldtick = times(NULL);      // get system tick number 
    // send files in char **pathname via serial port 
    while (*pathname != NULL) {
        printf("\nsenging file %s, echo mode: %s\n",
               *pathname, p_args->p_portinfo->echo == 0 ? "off" : "on");
        if (-1 == tty_send_file(*pathname, p_args->p_portinfo->echo)) {
            fflush(stdout);
            fflush(stderr);
            fprintf(stderr, "\ncannot send file %s\n", *pathname);
            return (-1);
        }
        lstat(*pathname, &stat);
        num++;
        byteSend += stat.st_size;
        pathname++;
    }

    newtick = times(NULL);
    systick = sysconf(_SC_CLK_TCK);     // get system clock tick per second 
    time = (float) (newtick - oldtick) / systick;
    if (time) {
        kbps = (float) byteSend / time / 1000;
        printf("\n%d files, %d bytes sent in %.2fs, %.2fKbps\n",
               num, byteSend, time, kbps);
    }
    return (0);
}