anjiahao1 commented on code in PR #1627: URL: https://github.com/apache/nuttx-apps/pull/1627#discussion_r1127326967
########## system/ymodem/rb_main.c: ########## @@ -0,0 +1,195 @@ +/**************************************************************************** + * apps/system/ymodem/rb_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <fcntl.h> +#include <stdio.h> +#include <termios.h> +#include <nuttx/fs/ioctl.h> +#include "ymodem.h" +#include "ym_utils.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ym_fd +{ + int uart_fd; + int file_fd; + char pathname[PATH_MAX]; + size_t file_saved_size; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int handler(FAR struct ymodem_ctx *ctx) +{ + char pathname[PATH_MAX + YMODEM_FILE_NAME_LENGTH]; + FAR struct ym_fd *ym_fd = ctx->priv; + FAR char *filename; + size_t size; + size_t ret; + + if (ctx->packet_type == YMODEM_FILE_RECV_NAME_PKT) + { + if (ym_fd->file_fd != 0) + { + close(ym_fd->file_fd); + } + + if (strlen(ym_fd->pathname) != 0) + { + if (ym_fd->pathname[strlen(ym_fd->pathname) - 1] == '/') + { + sprintf(pathname, "%s%s", ym_fd->pathname, ctx->file_name); + } + else + { + sprintf(pathname, "%s/%s", ym_fd->pathname, ctx->file_name); + } + + filename = pathname; + } + else + { + filename = ctx->file_name; + } + + ym_fd->file_fd = open(filename, O_CREAT | O_RDWR); + if (ym_fd->file_fd < 0) + { + return -1; + } + + ym_fd->file_saved_size = 0; + } + else if (ctx->packet_type == YMODEM_RECV_DATA_PKT) + { + if (ym_fd->file_saved_size + ctx->packet_size > ctx->file_length) + { + size = ctx->file_length - ym_fd->file_saved_size; + } + else + { + size = ctx->packet_size; + } + + ret = write(ym_fd->file_fd, ctx->data, size); + if (ret != size) + { + return -1; + } + + ym_fd->file_saved_size += ret; + } + + return 0; +} + +static void show_usage(FAR const char *progname, int errcode) +{ + fprintf(stderr, "USAGE: %s [OPTIONS]\n", progname); + fprintf(stderr, "\nWhere:\n"); + fprintf(stderr, "\nand OPTIONS include the following:\n"); + fprintf(stderr, + "\t-d <device>: Communication device to use. Default: %s\n", + CONFIG_SYSTEM_YMODEM_DEVNAME); + fprintf(stderr, + "\t-p <path>: Save remote file path. Default: Current path\n"); + exit(errcode); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + FAR const char *devname = CONFIG_SYSTEM_YMODEM_DEVNAME; + struct termios saveterm; + struct ymodem_ctx ctx; + struct termios term; + struct ym_fd ym_fd; + int option; + + memset(&ym_fd, 0, sizeof(struct ym_fd)); + while ((option = getopt(argc, argv, "d:p:h")) != ERROR) + { + switch (option) + { + case 'd': + devname = optarg; + break; + case 'h': + show_usage(argv[0], EXIT_FAILURE); + case 'p': + strncpy(ym_fd.pathname, optarg, PATH_MAX); + break; + default: + case '?': + fprintf(stderr, "ERROR: Unrecognized option\n"); + show_usage(argv[0], EXIT_FAILURE); + break; + } + } + + memset(&ctx, 0, sizeof(struct ymodem_ctx)); + ctx.send = ymodem_uart_send; + ctx.receive = ymodem_uart_receive; + ctx.packet_handler = handler; + ctx.timeout = 200; + ctx.priv = &ym_fd; + + ym_fd.file_fd = 0; + ym_fd.uart_fd = open(devname, O_RDWR | O_NONBLOCK); + if (ym_fd.uart_fd < 0) + { + printf("open\n"); + return 0; + } + + tcgetattr(ym_fd.uart_fd, &saveterm); + tcgetattr(ym_fd.uart_fd, &term); + cfmakeraw(&term); + tcsetattr(ym_fd.uart_fd, TCSANOW, &term); + + sched_lock(); Review Comment: Make sure that other applications will not use/dev/console -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
