Adjust code style to comply with clang-format rules

This commit is contained in:
yabu76 2025-08-14 13:50:58 +09:00
parent 102657af58
commit 4aa36e6e91
9 changed files with 285 additions and 159 deletions

View file

@ -3,3 +3,7 @@ IndentWidth: 4
AllowShortFunctionsOnASingleLine: None AllowShortFunctionsOnASingleLine: None
KeepEmptyLinesAtTheStartOfBlocks: false KeepEmptyLinesAtTheStartOfBlocks: false
BreakBeforeBraces: Allman BreakBeforeBraces: Allman
IndentCaseLabels: true
ColumnLimit: 144
SortIncludes: false
SkipMacroDefinitionBody: true

View file

@ -68,7 +68,7 @@ int main(int argc, char *argv[])
/* Configure input terminal */ /* Configure input terminal */
if (isatty(fileno(stdin))) if (isatty(fileno(stdin)))
{ {
stdin_configure(); stdin_configure();
} }
else else
{ {
@ -110,7 +110,8 @@ int main(int argc, char *argv[])
if (interactive_mode) if (interactive_mode)
{ {
tio_printf("Press ctrl-%c q to quit", option.prefix_key); tio_printf("Press ctrl-%c q to quit", option.prefix_key);
} else }
else
{ {
tio_printf("Non-interactive mode enabled"); tio_printf("Non-interactive mode enabled");
tio_printf("Press ctrl-c to quit"); tio_printf("Press ctrl-c to quit");

View file

@ -170,6 +170,7 @@ bool match_patterns(const char *string, const char *patterns)
pattern = strtok(patterns_copy, ","); pattern = strtok(patterns_copy, ",");
while (pattern != NULL) while (pattern != NULL)
{ {
// clang-format off
// Check if the string matches the current pattern // Check if the string matches the current pattern
#ifdef FNM_EXTMATCH #ifdef FNM_EXTMATCH
if (fnmatch(pattern, string, FNM_EXTMATCH) == 0) if (fnmatch(pattern, string, FNM_EXTMATCH) == 0)
@ -180,6 +181,7 @@ bool match_patterns(const char *string, const char *patterns)
free(patterns_copy); free(patterns_copy);
return true; return true;
} }
// clang-format on
// Move to the next pattern // Move to the next pattern
pattern = strtok(NULL, ","); pattern = strtok(NULL, ",");

View file

@ -61,6 +61,7 @@ enum opt_t
OPT_EXEC, OPT_EXEC,
}; };
// clang-format off
/* Default options */ /* Default options */
struct option_t option = struct option_t option =
{ {
@ -125,6 +126,7 @@ struct option_t option =
.map_i_msb2lsb = false, .map_i_msb2lsb = false,
.map_o_ign_cr = false, .map_o_ign_cr = false,
}; };
// clang-format on
void option_print_help(char *argv[]) void option_print_help(char *argv[])
{ {
@ -833,12 +835,14 @@ void options_print()
tio_printf(" Output line delay: %d", option.output_line_delay); tio_printf(" Output line delay: %d", option.output_line_delay);
tio_printf(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect)); tio_printf(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect));
tio_printf(" Automatic reconnect: %s", option.no_reconnect ? "true" : "false"); tio_printf(" Automatic reconnect: %s", option.no_reconnect ? "true" : "false");
// clang-format off
tio_printf(" Pulse duration: DTR=%d RTS=%d CTS=%d DSR=%d DCD=%d RI=%d", option.dtr_pulse_duration, tio_printf(" Pulse duration: DTR=%d RTS=%d CTS=%d DSR=%d DCD=%d RI=%d", option.dtr_pulse_duration,
option.rts_pulse_duration, option.rts_pulse_duration,
option.cts_pulse_duration, option.cts_pulse_duration,
option.dsr_pulse_duration, option.dsr_pulse_duration,
option.dcd_pulse_duration, option.dcd_pulse_duration,
option.ri_pulse_duration); option.ri_pulse_duration);
// clang-format on
tio_printf(" Input mode: %s", option_input_mode_to_string(option.input_mode)); tio_printf(" Input mode: %s", option_input_mode_to_string(option.input_mode));
tio_printf(" Output mode: %s", option_output_mode_to_string(option.output_mode)); tio_printf(" Output mode: %s", option_output_mode_to_string(option.output_mode));
tio_printf(" Alert: %s", option_alert_state_to_string(option.alert)); tio_printf(" Alert: %s", option_alert_state_to_string(option.alert));
@ -887,8 +891,9 @@ void options_parse(int argc, char *argv[])
option.vt100 = true; option.vt100 = true;
} }
while (1) while (true)
{ {
// clang-format off
static struct option long_options[] = static struct option long_options[] =
{ {
{"baudrate", required_argument, 0, 'b' }, {"baudrate", required_argument, 0, 'b' },
@ -932,6 +937,7 @@ void options_parse(int argc, char *argv[])
{"complete-profiles", no_argument, 0, OPT_COMPLETE_PROFILES }, {"complete-profiles", no_argument, 0, OPT_COMPLETE_PROFILES },
{0, 0, 0, 0 } {0, 0, 0, 0 }
}; };
// clang-format on
/* getopt_long stores the option index here */ /* getopt_long stores the option index here */
int option_index = 0; int option_index = 0;
@ -1131,7 +1137,7 @@ void options_parse(int argc, char *argv[])
/* Assume first non-option is the target (tty device, profile, tid) */ /* Assume first non-option is the target (tty device, profile, tid) */
if (strcmp(option.target, "")) if (strcmp(option.target, ""))
{ {
optind++; optind++;
} }
else if (optind < argc) else if (optind < argc)
{ {
@ -1176,6 +1182,7 @@ void options_parse_final(int argc, char *argv[])
#ifdef __CYGWIN__ #ifdef __CYGWIN__
unsigned char portnum; unsigned char portnum;
char *tty_win; char *tty_win;
// clang-format off
if ( ((strncmp("COM", option.target, 3) == 0) if ( ((strncmp("COM", option.target, 3) == 0)
|| (strncmp("com", option.target, 3) == 0) ) || (strncmp("com", option.target, 3) == 0) )
&& (sscanf(option.target + 3, "%hhu", &portnum) == 1) && (sscanf(option.target + 3, "%hhu", &portnum) == 1)
@ -1184,5 +1191,6 @@ void options_parse_final(int argc, char *argv[])
asprintf(&tty_win, "/dev/ttyS%hhu", portnum - 1); asprintf(&tty_win, "/dev/ttyS%hhu", portnum - 1);
option.target = tty_win; option.target = tty_win;
} }
// clang-format on
#endif #endif
} }

View file

@ -90,7 +90,7 @@ static void readline_input_cr(void)
rl_history_count++; rl_history_count++;
} }
else else
{ {
free(rl_history[0]); free(rl_history[0]);
memmove(&rl_history[0], &rl_history[1], (RL_HISTORY_MAX - 1) * sizeof(char*)); memmove(&rl_history[0], &rl_history[1], (RL_HISTORY_MAX - 1) * sizeof(char*));
rl_history[RL_HISTORY_MAX - 1] = strndup(rl_line, rl_line_length); rl_history[RL_HISTORY_MAX - 1] = strndup(rl_line, rl_line_length);

View file

@ -45,6 +45,7 @@
static int device_fd; static int device_fd;
// clang-format off
static char script_init[] = static char script_init[] =
"tio.set = function(arg)\n" "tio.set = function(arg)\n"
" local dtr = arg.DTR or -1\n" " local dtr = arg.DTR or -1\n"
@ -71,6 +72,7 @@ static char script_init[] =
"end\n" "end\n"
"tio.alwaysecho = true\n" "tio.alwaysecho = true\n"
"setmetatable(tio, tio)\n"; "setmetatable(tio, tio)\n";
// clang-format on
static bool alwaysecho(lua_State *L) static bool alwaysecho(lua_State *L)
{ {
@ -100,7 +102,9 @@ static int api_echo(lua_State *L)
log_printf("\n[%s] %s", pTimeStampNow, str); log_printf("\n[%s] %s", pTimeStampNow, str);
} }
} }
} else { }
else
{
for (size_t i=0; i<len; i++) for (size_t i=0; i<len; i++)
{ {
putchar(str[i]); putchar(str[i]);
@ -255,7 +259,8 @@ static int api_write(lua_State *L)
ssize_t ret; ssize_t ret;
int attempts = 100; int attempts = 100;
do { do
{
ret = write(device_fd, string, len); ret = write(device_fd, string, len);
if (ret < 0) if (ret < 0)
return luaL_error(L, "%s", strerror(errno)); return luaL_error(L, "%s", strerror(errno));
@ -319,7 +324,8 @@ static int api_read(lua_State *L)
} }
// lua: string = tio.readline(timeout) // lua: string = tio.readline(timeout)
static int api_readline(lua_State *L) { static int api_readline(lua_State *L)
{
int timeout = lua_tointeger(L, 1); //ms int timeout = lua_tointeger(L, 1); //ms
luaL_Buffer b; luaL_Buffer b;
char ch; char ch;
@ -331,7 +337,8 @@ static int api_readline(lua_State *L) {
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
luaL_prepbuffer(&b); luaL_prepbuffer(&b);
while (true) { while (true)
{
int ret = read_poll(device_fd, &ch, 1, timeout); int ret = read_poll(device_fd, &ch, 1, timeout);
if (ret < 0) if (ret < 0)
@ -440,6 +447,7 @@ static void script_file_run(lua_State *L, const char *filename)
} }
} }
// clang-format off
static const struct luaL_Reg tio_lib[] = static const struct luaL_Reg tio_lib[] =
{ {
{ "echo", api_echo}, { "echo", api_echo},
@ -453,6 +461,7 @@ static const struct luaL_Reg tio_lib[] =
{ "ttysearch", api_ttysearch}, { "ttysearch", api_ttysearch},
{NULL, NULL} {NULL, NULL}
}; };
// clang-format on
static void script_load(lua_State *L) static void script_load(lua_State *L)
{ {

View file

@ -147,6 +147,7 @@ typedef enum
SUBCOMMAND_MAP, SUBCOMMAND_MAP,
} sub_command_t; } sub_command_t;
// clang-format off
const char random_array[] = const char random_array[] =
{ {
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x20, 0x28, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x20, 0x28, 0x0A, 0x20,
@ -160,6 +161,7 @@ const char random_array[] =
0x66, 0x65, 0x65, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6B, 0x21, 0x0A, 0x20, 0x0A, 0x66, 0x65, 0x65, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6B, 0x21, 0x0A, 0x20, 0x0A,
0x00 0x00
}; };
// clang-format on
bool interactive_mode = true; bool interactive_mode = true;
@ -212,7 +214,7 @@ inline static bool is_valid_hex(char c)
inline static unsigned char char_to_nibble(char c) inline static unsigned char char_to_nibble(char c)
{ {
if(c >= '0' && c <= '9') if (c >= '0' && c <= '9')
{ {
return c - '0'; return c - '0';
} }
@ -331,7 +333,7 @@ void *tty_stdin_input_thread(void *arg)
pthread_mutex_unlock(&mutex_input_ready); pthread_mutex_unlock(&mutex_input_ready);
// Input loop for stdin // Input loop for stdin
while (1) while (true)
{ {
/* Input from stdin ready */ /* Input from stdin ready */
byte_count = read(STDIN_FILENO, input_buffer, BUFSIZ); byte_count = read(STDIN_FILENO, input_buffer, BUFSIZ);
@ -360,7 +362,8 @@ void *tty_stdin_input_thread(void *arg)
for (int i = 0; i<byte_count; i++) for (int i = 0; i<byte_count; i++)
{ {
// first do key hit check for xmodem abort // first do key hit check for xmodem abort
if (!key_hit) { if (!key_hit)
{
key_hit = input_buffer[i]; key_hit = input_buffer[i];
byte_count--; byte_count--;
memcpy(input_buffer+i, input_buffer+i+1, byte_count-i); memcpy(input_buffer+i, input_buffer+i+1, byte_count-i);
@ -383,7 +386,7 @@ void *tty_stdin_input_thread(void *arg)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case KEY_SHIFT_F: case KEY_SHIFT_F:
tio_printf("Flushed data I/O buffers") tio_printf("Flushed data I/O buffers");
tcflush(device_fd, TCIOFLUSH); tcflush(device_fd, TCIOFLUSH);
break; break;
default: default:
@ -414,7 +417,8 @@ void tty_input_thread_create(void)
{ {
pthread_mutex_lock(&mutex_input_ready); pthread_mutex_lock(&mutex_input_ready);
if (pthread_create(&thread, NULL, tty_stdin_input_thread, NULL) != 0) { if (pthread_create(&thread, NULL, tty_stdin_input_thread, NULL) != 0)
{
tio_error_printf("pthread_create() error"); tio_error_printf("pthread_create() error");
exit(1); exit(1);
} }
@ -631,6 +635,7 @@ void tty_output_mode_set(output_mode_t mode)
static void mappings_print(void) static void mappings_print(void)
{ {
// clang-format off
if (option.map_i_cr_nl || option.map_ign_cr || option.map_i_ff_escc || if (option.map_i_cr_nl || option.map_ign_cr || option.map_i_ff_escc ||
option.map_i_nl_cr || option.map_i_nl_crnl || option.map_i_cr_crnl || option.map_i_nl_cr || option.map_i_nl_crnl || option.map_i_cr_crnl ||
option.map_o_cr_nl || option.map_o_del_bs || option.map_o_nl_crnl || option.map_o_cr_nl || option.map_o_del_bs || option.map_o_nl_crnl ||
@ -656,6 +661,7 @@ static void mappings_print(void)
{ {
tio_printf(" Mappings: none"); tio_printf(" Mappings: none");
} }
// clang-format on
} }
void handle_command_sequence(char input_char, char *output_char, bool *forward) void handle_command_sequence(char input_char, char *output_char, bool *forward)
@ -1011,6 +1017,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
break; break;
case KEY_M: case KEY_M:
// clang-format off
/* Change mapping of characters on input or output */ /* Change mapping of characters on input or output */
tio_printf("Please enter which mapping to set or unset:"); tio_printf("Please enter which mapping to set or unset:");
tio_printf(" (0) ICRNL: %s mapping CR to NL on input (unless IGNCR is set)", tio_printf(" (0) ICRNL: %s mapping CR to NL on input (unless IGNCR is set)",
@ -1039,6 +1046,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
option.map_o_nulbrk ? "Unset" : "Set"); option.map_o_nulbrk ? "Unset" : "Set");
tio_printf(" (c) OIGNCR: %s ignoring CR on output", tio_printf(" (c) OIGNCR: %s ignoring CR on output",
option.map_o_ign_cr ? "Unset" : "Set"); option.map_o_ign_cr ? "Unset" : "Set");
// clang-format on
// Process next input character as sub command // Process next input character as sub command
sub_command = SUBCOMMAND_MAP; sub_command = SUBCOMMAND_MAP;
@ -1050,7 +1058,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
case KEY_R: case KEY_R:
/* Run script */ /* Run script */
tio_printf("Run Lua script") tio_printf("Run Lua script");
tio_printf_raw("Enter file name: "); tio_printf_raw("Enter file name: ");
if (tio_readln()) if (tio_readln())
{ {
@ -1126,12 +1134,13 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
case KEY_Y: case KEY_Y:
tio_printf("Send file with YMODEM"); tio_printf("Send file with YMODEM");
tio_printf_raw("Enter file name: "); tio_printf_raw("Enter file name: ");
if (tio_readln()) { if (tio_readln())
int ret; {
int ret;
tio_printf("Sending file '%s' ", line); tio_printf("Sending file '%s' ", line);
tio_printf("Press any key to abort transfer"); tio_printf("Press any key to abort transfer");
ret = xymodem_send(device_fd, line, YMODEM); ret = xymodem_send(device_fd, line, YMODEM);
tio_printf("%s", ret < 0 ? "Aborted" : "Done"); tio_printf("%s", ret < 0 ? "Aborted" : "Done");
} }
break; break;
@ -1194,9 +1203,9 @@ void stdout_restore(void)
// If terminal is vt100 // If terminal is vt100
if (option.vt100) if (option.vt100)
{ {
// Disable DEC Special Graphics character set just in case it was randomly // Disable DEC Special Graphics character set just in case it was randomly
// enabled by noise from serial device. // enabled by noise from serial device.
putchar('\017'); putchar('\017');
} }
} }
@ -2044,7 +2053,8 @@ GList *tty_search_for_serial_devices(void)
} }
/* Populate device info */ /* Populate device info */
*device_info = (device_t) { *device_info = (device_t)
{
.path = devicePath, .path = devicePath,
.tid = g_strdup(tid), .tid = g_strdup(tid),
.uptime = uptime, .uptime = uptime,

View file

@ -40,7 +40,8 @@
#define min(a, b) ((a) < (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b))
struct xpacket_1k { struct xpacket_1k
{
uint8_t type; uint8_t type;
uint8_t seq; uint8_t seq;
uint8_t nseq; uint8_t nseq;
@ -49,7 +50,8 @@ struct xpacket_1k {
uint8_t crc_lo; uint8_t crc_lo;
} __attribute__((packed)); } __attribute__((packed));
struct xpacket { struct xpacket
{
uint8_t type; uint8_t type;
uint8_t seq; uint8_t seq;
uint8_t nseq; uint8_t nseq;
@ -63,7 +65,8 @@ static uint16_t crc16(const uint8_t *data, uint16_t size)
{ {
uint16_t crc, s; uint16_t crc, s;
for (crc = 0; size > 0; size--) { for (crc = 0; size > 0; size--)
{
s = *data++ ^ (crc >> 8); s = *data++ ^ (crc >> 8);
s ^= (s >> 4); s ^= (s >> 4);
crc = (crc << 8) ^ s ^ (s << 5) ^ (s << 12); crc = (crc << 8) ^ s ^ (s << 5) ^ (s << 12);
@ -81,16 +84,19 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
/* Drain pending characters from serial line. Insist on the /* Drain pending characters from serial line. Insist on the
* last drained character being 'C'. * last drained character being 'C'.
*/ */
while(1) { while (true)
{
if (key_hit) if (key_hit)
return -1; return -1;
rc = read_poll(sio, &resp, 1, 50); rc = read_poll(sio, &resp, 1, 50);
if (rc == 0) { if (rc == 0)
{
if (resp == 'C') break; if (resp == 'C') break;
if (resp == CAN) return ERR; if (resp == CAN) return ERR;
continue; continue;
} }
else if (rc < 0) { else if (rc < 0)
{
tio_error_print("Read sync from serial failed"); tio_error_print("Read sync from serial failed");
return ERR; return ERR;
} }
@ -100,7 +106,8 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
packet.seq = seq; packet.seq = seq;
packet.type = STX; packet.type = STX;
while (len) { while (len)
{
size_t sz, z = 0; size_t sz, z = 0;
char *from, status; char *from, status;
@ -116,11 +123,14 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
/* Send packet */ /* Send packet */
from = (char *) &packet; from = (char *) &packet;
sz = sizeof(packet); sz = sizeof(packet);
while (sz) { while (sz)
{
if (key_hit) if (key_hit)
return ERR; return ERR;
if ((rc = write(sio, from, sz)) < 0 ) { if ((rc = write(sio, from, sz)) < 0 )
if (errno == EWOULDBLOCK) { {
if (errno == EWOULDBLOCK)
{
usleep(1000); usleep(1000);
continue; continue;
} }
@ -138,30 +148,36 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
if (seq == 0 && packet.data[0] == 0) resp = ACK; if (seq == 0 && packet.data[0] == 0) resp = ACK;
/* Read receiver response, timeout 1 s */ /* Read receiver response, timeout 1 s */
for(int n=0; n < 20; n++) { for (int n = 0; n < 20; n++)
{
if (key_hit) if (key_hit)
return ERR; return ERR;
rc = read_poll(sio, &resp, 1, 50); rc = read_poll(sio, &resp, 1, 50);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Read ack/nak from serial failed"); tio_error_print("Read ack/nak from serial failed");
return ERR; return ERR;
} else if(rc > 0) { }
else if (rc > 0)
{
break; break;
} }
} }
/* Update "progress bar" */ /* Update "progress bar" */
switch (resp) { switch (resp)
case NAK: status = 'N'; break; {
case ACK: status = '.'; break; case NAK: status = 'N'; break;
case 'C': status = 'C'; break; case ACK: status = '.'; break;
case CAN: status = '!'; return ERR; case 'C': status = 'C'; break;
default: status = '?'; case CAN: status = '!'; return ERR;
default: status = '?';
} }
write(STDOUT_FILENO, &status, 1); write(STDOUT_FILENO, &status, 1);
/* Move to next block after ACK */ /* Move to next block after ACK */
if (resp == ACK) { if (resp == ACK)
{
packet.seq++; packet.seq++;
len -= z; len -= z;
buf += z; buf += z;
@ -169,23 +185,29 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
} }
/* Send EOT at 1 Hz until ACK or CAN received */ /* Send EOT at 1 Hz until ACK or CAN received */
while (seq) { while (seq)
{
if (key_hit) if (key_hit)
return ERR; return ERR;
if (write(sio, EOT_STR, 1) < 0) { if (write(sio, EOT_STR, 1) < 0)
{
tio_error_print("Write EOT to serial failed"); tio_error_print("Write EOT to serial failed");
return ERR; return ERR;
} }
write(STDOUT_FILENO, "|", 1); write(STDOUT_FILENO, "|", 1);
/* 1s timeout */ /* 1s timeout */
rc = read_poll(sio, &resp, 1, 1000); rc = read_poll(sio, &resp, 1, 1000);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Read from serial failed"); tio_error_print("Read from serial failed");
return ERR; return ERR;
} else if(rc == 0) { }
else if (rc == 0)
{
continue; continue;
} }
if (resp == ACK || resp == CAN) { if (resp == ACK || resp == CAN)
{
write(STDOUT_FILENO, "\r\n", 2); write(STDOUT_FILENO, "\r\n", 2);
return (resp == ACK) ? OK : ERR; return (resp == ACK) ? OK : ERR;
} }
@ -203,16 +225,19 @@ static int xmodem(int sio, const void *data, size_t len)
/* Drain pending characters from serial line. Insist on the /* Drain pending characters from serial line. Insist on the
* last drained character being 'C'. * last drained character being 'C'.
*/ */
while(1) { while (true)
{
if (key_hit) if (key_hit)
return -1; return -1;
rc = read_poll(sio, &resp, 1, 50); rc = read_poll(sio, &resp, 1, 50);
if (rc == 0) { if (rc == 0)
{
if (resp == 'C') break; if (resp == 'C') break;
if (resp == CAN) return ERR; if (resp == CAN) return ERR;
continue; continue;
} }
else if (rc < 0) { else if (rc < 0)
{
tio_error_print("Read sync from serial failed"); tio_error_print("Read sync from serial failed");
return ERR; return ERR;
} }
@ -222,7 +247,8 @@ static int xmodem(int sio, const void *data, size_t len)
packet.seq = 1; packet.seq = 1;
packet.type = SOH; packet.type = SOH;
while (len) { while (len)
{
size_t sz, z = 0; size_t sz, z = 0;
char *from, status; char *from, status;
@ -238,11 +264,14 @@ static int xmodem(int sio, const void *data, size_t len)
/* Send packet */ /* Send packet */
from = (char *) &packet; from = (char *) &packet;
sz = sizeof(packet); sz = sizeof(packet);
while (sz) { while (sz)
{
if (key_hit) if (key_hit)
return ERR; return ERR;
if ((rc = write(sio, from, sz)) < 0 ) { if ((rc = write(sio, from, sz)) < 0 )
if (errno == EWOULDBLOCK) { {
if (errno == EWOULDBLOCK)
{
usleep(1000); usleep(1000);
continue; continue;
} }
@ -257,30 +286,36 @@ static int xmodem(int sio, const void *data, size_t len)
resp = 0; resp = 0;
/* Read receiver response, timeout 1 s */ /* Read receiver response, timeout 1 s */
for(int n=0; n < 20; n++) { for (int n = 0; n < 20; n++)
{
if (key_hit) if (key_hit)
return ERR; return ERR;
rc = read_poll(sio, &resp, 1, 50); rc = read_poll(sio, &resp, 1, 50);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Read ack/nak from serial failed"); tio_error_print("Read ack/nak from serial failed");
return ERR; return ERR;
} else if(rc > 0) { }
else if (rc > 0)
{
break; break;
} }
} }
/* Update "progress bar" */ /* Update "progress bar" */
switch (resp) { switch (resp)
case NAK: status = 'N'; break; {
case ACK: status = '.'; break; case NAK: status = 'N'; break;
case 'C': status = 'C'; break; case ACK: status = '.'; break;
case CAN: status = '!'; return ERR; case 'C': status = 'C'; break;
default: status = '?'; case CAN: status = '!'; return ERR;
default: status = '?';
} }
write(STDOUT_FILENO, &status, 1); write(STDOUT_FILENO, &status, 1);
/* Move to next block after ACK */ /* Move to next block after ACK */
if (resp == ACK) { if (resp == ACK)
{
packet.seq++; packet.seq++;
len -= z; len -= z;
buf += z; buf += z;
@ -288,23 +323,29 @@ static int xmodem(int sio, const void *data, size_t len)
} }
/* Send EOT at 1 Hz until ACK or CAN received */ /* Send EOT at 1 Hz until ACK or CAN received */
while (1) { while (true)
{
if (key_hit) if (key_hit)
return ERR; return ERR;
if (write(sio, EOT_STR, 1) < 0) { if (write(sio, EOT_STR, 1) < 0)
{
tio_error_print("Write EOT to serial failed"); tio_error_print("Write EOT to serial failed");
return ERR; return ERR;
} }
write(STDOUT_FILENO, "|", 1); write(STDOUT_FILENO, "|", 1);
/* 1s timeout */ /* 1s timeout */
rc = read_poll(sio, &resp, 1, 1000); rc = read_poll(sio, &resp, 1, 1000);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Read from serial failed"); tio_error_print("Read from serial failed");
return ERR; return ERR;
} else if(rc == 0) { }
else if (rc == 0)
{
continue; continue;
} }
if (resp == ACK || resp == CAN) { if (resp == ACK || resp == CAN)
{
write(STDOUT_FILENO, "\r\n", 2); write(STDOUT_FILENO, "\r\n", 2);
return (resp == ACK) ? OK : ERR; return (resp == ACK) ? OK : ERR;
} }
@ -325,8 +366,10 @@ int start_receive(int sio)
seconds. If nothing is received in that time then return false to indicate seconds. If nothing is received in that time then return false to indicate
that the transfer did not start. */ that the transfer did not start. */
rc = write(sio, "C", 1); rc = write(sio, "C", 1);
if (rc < 0) { if (rc < 0)
if (errno == EWOULDBLOCK) { {
if (errno == EWOULDBLOCK)
{
usleep(1000); usleep(1000);
continue; continue;
} }
@ -353,7 +396,7 @@ int start_receive(int sio)
return rc; return rc;
} }
uint16_t update_CRC(uint16_t crc, char data_char) uint16_t update_CRC(uint16_t crc, char data_char)
{ {
uint8_t data = data_char; uint8_t data = data_char;
crc = crc ^ ((uint16_t)data << 8); crc = crc ^ ((uint16_t)data << 8);
@ -385,19 +428,25 @@ int receive_packet(int sio, struct xpacket packet, int fd)
/* Read seq bytes*/ /* Read seq bytes*/
rc = read_poll(sio, &rxSeq1, 1, 3000); rc = read_poll(sio, &rxSeq1, 1, 3000);
if (rc == 0) { if (rc == 0)
{
tio_error_print("Timeout waiting for first seq byte"); tio_error_print("Timeout waiting for first seq byte");
return ERR; return ERR;
} else if (rc < 0) { }
tio_error_print("Error reading first seq byte") else if (rc < 0)
{
tio_error_print("Error reading first seq byte");
return ERR_FATAL; return ERR_FATAL;
} }
rc = read_poll(sio, &rxSeq2, 1, 3000); rc = read_poll(sio, &rxSeq2, 1, 3000);
if (rc == 0) { if (rc == 0)
{
tio_error_print("Timeout waiting for second seq byte"); tio_error_print("Timeout waiting for second seq byte");
return ERR; return ERR;
} else if (rc < 0) { }
tio_error_print("Error reading second seq byte") else if (rc < 0)
{
tio_error_print("Error reading second seq byte");
return ERR_FATAL; return ERR_FATAL;
} }
if (key_hit) if (key_hit)
@ -407,20 +456,24 @@ int receive_packet(int sio, struct xpacket packet, int fd)
for (unsigned ix = 0; (ix < sizeof(packet.data)); ix++) for (unsigned ix = 0; (ix < sizeof(packet.data)); ix++)
{ {
rc = read_poll(sio, &resp, 1, 3000); rc = read_poll(sio, &resp, 1, 3000);
/* If the read times out or fails then fail this packet. */ /* If the read times out or fails then fail this packet. */
if (rc == 0) if (rc == 0)
{ {
tio_error_print("Timeout waiting for next packet char"); tio_error_print("Timeout waiting for next packet char");
rc = write(sio, CAN_STR, 1); rc = write(sio, CAN_STR, 1);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Write cancel packet to serial failed"); tio_error_print("Write cancel packet to serial failed");
return ERR_FATAL; return ERR_FATAL;
} }
return ERR; return ERR;
} else if (rc < 0) { }
tio_error_print("Error reading next packet char") else if (rc < 0)
{
tio_error_print("Error reading next packet char");
rc = write(sio, CAN_STR, 1); rc = write(sio, CAN_STR, 1);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Write cancel packet to serial failed"); tio_error_print("Write cancel packet to serial failed");
} }
return ERR_FATAL; return ERR_FATAL;
@ -433,11 +486,14 @@ int receive_packet(int sio, struct xpacket packet, int fd)
/* Read CRC */ /* Read CRC */
rc = read_poll(sio, &resp, 1, 3000); rc = read_poll(sio, &resp, 1, 3000);
if (rc == 0) { if (rc == 0)
{
tio_error_print("Timeout waiting for first CRC byte"); tio_error_print("Timeout waiting for first CRC byte");
return ERR; return ERR;
} else if (rc < 0) { }
tio_error_print("Error reading first CRC byte") else if (rc < 0)
{
tio_error_print("Error reading first CRC byte");
return ERR_FATAL; return ERR_FATAL;
} }
@ -446,20 +502,23 @@ int receive_packet(int sio, struct xpacket packet, int fd)
rxCrc = uresp16 << 8; rxCrc = uresp16 << 8;
rc = read_poll(sio, &resp, 1, 3000); rc = read_poll(sio, &resp, 1, 3000);
if (rc == 0) { if (rc == 0)
{
tio_error_print("Timeout waiting for second CRC byte"); tio_error_print("Timeout waiting for second CRC byte");
return ERR; return ERR;
} else if (rc < 0) { }
tio_error_print("Error reading second CRC byte") else if (rc < 0)
{
tio_error_print("Error reading second CRC byte");
return ERR_FATAL; return ERR_FATAL;
} }
uresp = resp; uresp = resp;
uresp16 = uresp; uresp16 = uresp;
rxCrc |= uresp16; rxCrc |= uresp16;
if (key_hit) if (key_hit)
return USER_CAN; return USER_CAN;
/* At this point in the code, there should not be anything in the receive buffer /* At this point in the code, there should not be anything in the receive buffer
because the sender has just sent a complete packet and is waiting on a response. */ because the sender has just sent a complete packet and is waiting on a response. */
@ -469,7 +528,8 @@ int receive_packet(int sio, struct xpacket packet, int fd)
tio_error_print("%s", strerror(errno)); tio_error_print("%s", strerror(errno));
tio_error_print("Poll check error after packet finish"); tio_error_print("Poll check error after packet finish");
rc = write(sio, CAN_STR, 1); rc = write(sio, CAN_STR, 1);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Write cancel packet to serial failed"); tio_error_print("Write cancel packet to serial failed");
} }
return ERR_FATAL; return ERR_FATAL;
@ -494,7 +554,8 @@ int receive_packet(int sio, struct xpacket packet, int fd)
{ {
/* Resend of previously processed packet. */ /* Resend of previously processed packet. */
rc = write(sio, ACK_STR, 1); rc = write(sio, ACK_STR, 1);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Write acknowlegdement packet to serial failed"); tio_error_print("Write acknowlegdement packet to serial failed");
return ERR_FATAL; return ERR_FATAL;
} }
@ -520,7 +581,8 @@ int receive_packet(int sio, struct xpacket packet, int fd)
{ {
tio_error_print("Problem writing to file"); tio_error_print("Problem writing to file");
rc = write(sio, CAN_STR, 1); rc = write(sio, CAN_STR, 1);
if (rc < 0) { if (rc < 0)
{
tio_error_print("Write cancel packet to serial failed"); tio_error_print("Write cancel packet to serial failed");
} }
return ERR_FATAL; return ERR_FATAL;
@ -545,16 +607,20 @@ int xmodem_receive(int sio, int fd)
char status; char status;
/* Drain pending characters from serial line.*/ /* Drain pending characters from serial line.*/
while(1) { while (true)
{
if (key_hit) if (key_hit)
return -1; return -1;
rc = read_poll(sio, &resp, 1, 50); rc = read_poll(sio, &resp, 1, 50);
if (rc == 0) { if (rc == 0)
{
if (resp == CAN) return ERR; if (resp == CAN) return ERR;
break; break;
} }
else if (rc < 0) { else if (rc < 0)
if (rc != USER_CAN) { {
if (rc != USER_CAN)
{
tio_error_print("Read sync from serial failed"); tio_error_print("Read sync from serial failed");
} }
return ERR; return ERR;
@ -571,79 +637,95 @@ int xmodem_receive(int sio, int fd)
{ {
tio_error_print("Timeout waiting for transfer to start"); tio_error_print("Timeout waiting for transfer to start");
return ERR; return ERR;
} else if (rc < 0) { }
else if (rc < 0)
{
tio_error_print("Error starting XMODEM receive"); tio_error_print("Error starting XMODEM receive");
return ERR; return ERR;
} }
while (!complete) { while (!complete)
{
/* Poll for 1 new byte for 3 seconds */ /* Poll for 1 new byte for 3 seconds */
rc = read_poll(sio, &resp, 1, 3000); rc = read_poll(sio, &resp, 1, 3000);
if (rc == 0) { if (rc == 0)
{
tio_error_print("Timeout waiting for start of next packet"); tio_error_print("Timeout waiting for start of next packet");
return ERR; return ERR;
} else if (rc < 0) { }
tio_error_print("Error reading start of next packet") else if (rc < 0)
{
tio_error_print("Error reading start of next packet");
return ERR; return ERR;
} }
if (key_hit) if (key_hit)
return USER_CAN; return USER_CAN;
switch(resp) switch (resp)
{ {
case SOH: case SOH:
/* Start of a packet */ /* Start of a packet */
rc = receive_packet(sio, packet, fd); rc = receive_packet(sio, packet, fd);
if (rc == OK) { if (rc == OK)
packet.seq++; {
status = '.'; packet.seq++;
} else if (rc == ERR) { status = '.';
rc = write(sio, NAK_STR, 1); }
if (rc < 0) { else if (rc == ERR)
tio_error_print("Writing not acknowledge packet to serial failed"); {
rc = write(sio, NAK_STR, 1);
if (rc < 0)
{
tio_error_print("Writing not acknowledge packet to serial failed");
return ERR;
}
status = 'N';
}
else if (rc == ERR_FATAL)
{
tio_error_print("Receive cancelled due to fatal error");
return ERR; return ERR;
} }
status = 'N'; else if (rc == USER_CAN)
} else if (rc == ERR_FATAL) { {
tio_error_print("Receive cancelled due to fatal error"); rc = write(sio, CAN_STR, 1);
return ERR; if (rc < 0)
} else if (rc == USER_CAN) { {
rc = write(sio, CAN_STR, 1); tio_error_print("Writing cancel to serial failed");
if (rc < 0) { return ERR;
tio_error_print("Writing cancel to serial failed"); }
return ERR; return USER_CAN;
} }
return USER_CAN; else if (rc == RX_IGNORE)
} else if (rc == RX_IGNORE) { {
status = ':'; status = ':';
} }
break; break;
case EOT: case EOT:
/* End of Transfer */ /* End of Transfer */
rc = write(sio, ACK_STR, 1); rc = write(sio, ACK_STR, 1);
if (rc < 0) if (rc < 0)
{ {
tio_error_print("Write acknowlegdement packet to serial failed"); tio_error_print("Write acknowlegdement packet to serial failed");
return ERR; return ERR;
} }
complete = true; complete = true;
status = '\0'; status = '\0';
write(STDOUT_FILENO, "|\r\n", 3); write(STDOUT_FILENO, "|\r\n", 3);
break; break;
case CAN: case CAN:
/* Cancel from sender */ /* Cancel from sender */
tio_error_print("Transmission cancelled from sender"); tio_error_print("Transmission cancelled from sender");
return ERR; return ERR;
break; break;
default: default:
tio_error_print("Unexpected character received waiting for next packet"); tio_error_print("Unexpected character received waiting for next packet");
return ERR; return ERR;
break; break;
} }
/* Update "progress bar" */ /* Update "progress bar" */
write(STDOUT_FILENO, &status, 1); write(STDOUT_FILENO, &status, 1);
@ -660,14 +742,16 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode)
/* Open file, map into memory */ /* Open file, map into memory */
fd = open(filename, O_RDONLY); fd = open(filename, O_RDONLY);
if (fd < 0) { if (fd < 0)
{
tio_error_print("Could not open file"); tio_error_print("Could not open file");
return ERR; return ERR;
} }
fstat(fd, &stat); fstat(fd, &stat);
len = stat.st_size; len = stat.st_size;
buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
if (!buf) { if (!buf)
{
close(fd); close(fd);
tio_error_print("Could not mmap file"); tio_error_print("Could not mmap file");
return ERR; return ERR;
@ -675,15 +759,19 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode)
/* Do transfer */ /* Do transfer */
key_hit = 0; key_hit = 0;
if (mode == XMODEM_1K) { if (mode == XMODEM_1K)
{
rc = xmodem_1k(sio, buf, len, 1); rc = xmodem_1k(sio, buf, len, 1);
} }
else if (mode == XMODEM_CRC) { else if (mode == XMODEM_CRC)
{
rc = xmodem(sio, buf, len); rc = xmodem(sio, buf, len);
} }
else { else
{
/* Ymodem: hdr + file + fin */ /* Ymodem: hdr + file + fin */
while(1) { while (true)
{
char hdr[1024], *p; char hdr[1024], *p;
rc = -1; rc = -1;
@ -694,7 +782,7 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode)
if (xmodem_1k(sio, hdr, p - hdr, 0) < 0) break; /* hdr with metadata */ if (xmodem_1k(sio, hdr, p - hdr, 0) < 0) break; /* hdr with metadata */
if (xmodem_1k(sio, buf, len, 1) < 0) break; /* xmodem file */ if (xmodem_1k(sio, buf, len, 1) < 0) break; /* xmodem file */
if (xmodem_1k(sio, "", 1, 0) < 0) break; /* empty hdr = fin */ if (xmodem_1k(sio, "", 1, 0) < 0) break; /* empty hdr = fin */
rc = 0; break; rc = 0; break;
} }
} }
key_hit = 0xff; key_hit = 0xff;
@ -712,21 +800,25 @@ int xymodem_receive(int sio, const char *filename, modem_mode_t mode)
/* Create new file */ /* Create new file */
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (fd < 0) { if (fd < 0)
{
tio_error_print("Could not open file"); tio_error_print("Could not open file");
return ERR; return ERR;
} }
/* Do transfer */ /* Do transfer */
key_hit = 0; key_hit = 0;
if (mode == XMODEM_1K) { if (mode == XMODEM_1K)
{
tio_error_print("Not supported"); tio_error_print("Not supported");
rc = -1; rc = -1;
} }
else if (mode == XMODEM_CRC) { else if (mode == XMODEM_CRC)
{
rc = xmodem_receive(sio, fd); rc = xmodem_receive(sio, fd);
} }
else { else
{
tio_error_print("Not supported"); tio_error_print("Not supported");
rc = -1; rc = -1;
} }

View file

@ -21,7 +21,8 @@
#pragma once #pragma once
typedef enum { typedef enum
{
XMODEM_1K, XMODEM_1K,
XMODEM_CRC, XMODEM_CRC,
YMODEM, YMODEM,
@ -30,5 +31,4 @@ typedef enum {
extern char key_hit; extern char key_hit;
int xymodem_send(int sio, const char *filename, modem_mode_t mode); int xymodem_send(int sio, const char *filename, modem_mode_t mode);
int xymodem_receive(int sio, const char *filename, modem_mode_t mode); int xymodem_receive(int sio, const char *filename, modem_mode_t mode);