anchao commented on code in PR #1627: URL: https://github.com/apache/nuttx-apps/pull/1627#discussion_r1127324803
########## system/ymodem/ym_utils.c: ########## @@ -0,0 +1,113 @@ +/**************************************************************************** + * apps/system/ymodem/ym_utils.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 <sys/time.h> +#include <sys/ioctl.h> +#include "ym_utils.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static long get_current_time(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +ssize_t ymodem_uart_receive(FAR struct ymodem_ctx *ctx, FAR uint8_t *buf, + size_t size, uint32_t timeout) +{ + int uart_fd = *(FAR int *)ctx->priv; + size_t i = 0; + ssize_t ret; + long base; + + base = get_current_time(); + while (get_current_time() - base < timeout && i < size) + { + ret = read(uart_fd, buf + i, size - i); + if (ret >= 0) + { + i += ret; + } + } + + if (i == 0) + { + return -1; + } + + return i; +} + +ssize_t ymodem_uart_send(FAR struct ymodem_ctx *ctx, FAR uint8_t *buf, + size_t size, uint32_t timeout) +{ + int uart_fd = *(FAR int *)ctx->priv; + size_t sendsize = 0; + size_t bufsize = 0; + size_t ret; + int count; + long base; + + ioctl(uart_fd, FIONSPACE, &bufsize); + base = get_current_time(); + while (get_current_time() - base < timeout) + { + if (size < bufsize) + { + bufsize = size; + } + + ret = write(uart_fd, buf + sendsize, bufsize); + if (ret < 0) + { + return ret; + } + + sendsize += ret; + size -= ret; + if (size) + { +wait_send: Review Comment: or maybe it is better to add some delay here? -- 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]
