mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Adjust code style to comply with clang-format rules
This commit is contained in:
parent
102657af58
commit
4aa36e6e91
9 changed files with 285 additions and 159 deletions
|
|
@ -3,3 +3,7 @@ IndentWidth: 4
|
|||
AllowShortFunctionsOnASingleLine: None
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
BreakBeforeBraces: Allman
|
||||
IndentCaseLabels: true
|
||||
ColumnLimit: 144
|
||||
SortIncludes: false
|
||||
SkipMacroDefinitionBody: true
|
||||
|
|
|
|||
|
|
@ -110,7 +110,8 @@ int main(int argc, char *argv[])
|
|||
if (interactive_mode)
|
||||
{
|
||||
tio_printf("Press ctrl-%c q to quit", option.prefix_key);
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
tio_printf("Non-interactive mode enabled");
|
||||
tio_printf("Press ctrl-c to quit");
|
||||
|
|
|
|||
|
|
@ -170,6 +170,7 @@ bool match_patterns(const char *string, const char *patterns)
|
|||
pattern = strtok(patterns_copy, ",");
|
||||
while (pattern != NULL)
|
||||
{
|
||||
// clang-format off
|
||||
// Check if the string matches the current pattern
|
||||
#ifdef FNM_EXTMATCH
|
||||
if (fnmatch(pattern, string, FNM_EXTMATCH) == 0)
|
||||
|
|
@ -180,6 +181,7 @@ bool match_patterns(const char *string, const char *patterns)
|
|||
free(patterns_copy);
|
||||
return true;
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
// Move to the next pattern
|
||||
pattern = strtok(NULL, ",");
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@ enum opt_t
|
|||
OPT_EXEC,
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
/* Default options */
|
||||
struct option_t option =
|
||||
{
|
||||
|
|
@ -125,6 +126,7 @@ struct option_t option =
|
|||
.map_i_msb2lsb = false,
|
||||
.map_o_ign_cr = false,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
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(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect));
|
||||
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,
|
||||
option.rts_pulse_duration,
|
||||
option.cts_pulse_duration,
|
||||
option.dsr_pulse_duration,
|
||||
option.dcd_pulse_duration,
|
||||
option.ri_pulse_duration);
|
||||
// clang-format on
|
||||
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(" Alert: %s", option_alert_state_to_string(option.alert));
|
||||
|
|
@ -887,8 +891,9 @@ void options_parse(int argc, char *argv[])
|
|||
option.vt100 = true;
|
||||
}
|
||||
|
||||
while (1)
|
||||
while (true)
|
||||
{
|
||||
// clang-format off
|
||||
static struct option long_options[] =
|
||||
{
|
||||
{"baudrate", required_argument, 0, 'b' },
|
||||
|
|
@ -932,6 +937,7 @@ void options_parse(int argc, char *argv[])
|
|||
{"complete-profiles", no_argument, 0, OPT_COMPLETE_PROFILES },
|
||||
{0, 0, 0, 0 }
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
/* getopt_long stores the option index here */
|
||||
int option_index = 0;
|
||||
|
|
@ -1176,6 +1182,7 @@ void options_parse_final(int argc, char *argv[])
|
|||
#ifdef __CYGWIN__
|
||||
unsigned char portnum;
|
||||
char *tty_win;
|
||||
// clang-format off
|
||||
if ( ((strncmp("COM", option.target, 3) == 0)
|
||||
|| (strncmp("com", option.target, 3) == 0) )
|
||||
&& (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);
|
||||
option.target = tty_win;
|
||||
}
|
||||
// clang-format on
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
17
src/script.c
17
src/script.c
|
|
@ -45,6 +45,7 @@
|
|||
|
||||
static int device_fd;
|
||||
|
||||
// clang-format off
|
||||
static char script_init[] =
|
||||
"tio.set = function(arg)\n"
|
||||
" local dtr = arg.DTR or -1\n"
|
||||
|
|
@ -71,6 +72,7 @@ static char script_init[] =
|
|||
"end\n"
|
||||
"tio.alwaysecho = true\n"
|
||||
"setmetatable(tio, tio)\n";
|
||||
// clang-format on
|
||||
|
||||
static bool alwaysecho(lua_State *L)
|
||||
{
|
||||
|
|
@ -100,7 +102,9 @@ static int api_echo(lua_State *L)
|
|||
log_printf("\n[%s] %s", pTimeStampNow, str);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i=0; i<len; i++)
|
||||
{
|
||||
putchar(str[i]);
|
||||
|
|
@ -255,7 +259,8 @@ static int api_write(lua_State *L)
|
|||
ssize_t ret;
|
||||
int attempts = 100;
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
ret = write(device_fd, string, len);
|
||||
if (ret < 0)
|
||||
return luaL_error(L, "%s", strerror(errno));
|
||||
|
|
@ -319,7 +324,8 @@ static int api_read(lua_State *L)
|
|||
}
|
||||
|
||||
// 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
|
||||
luaL_Buffer b;
|
||||
char ch;
|
||||
|
|
@ -331,7 +337,8 @@ static int api_readline(lua_State *L) {
|
|||
|
||||
luaL_buffinit(L, &b);
|
||||
luaL_prepbuffer(&b);
|
||||
while (true) {
|
||||
while (true)
|
||||
{
|
||||
int ret = read_poll(device_fd, &ch, 1, timeout);
|
||||
|
||||
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[] =
|
||||
{
|
||||
{ "echo", api_echo},
|
||||
|
|
@ -453,6 +461,7 @@ static const struct luaL_Reg tio_lib[] =
|
|||
{ "ttysearch", api_ttysearch},
|
||||
{NULL, NULL}
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
static void script_load(lua_State *L)
|
||||
{
|
||||
|
|
|
|||
26
src/tty.c
26
src/tty.c
|
|
@ -147,6 +147,7 @@ typedef enum
|
|||
SUBCOMMAND_MAP,
|
||||
} sub_command_t;
|
||||
|
||||
// clang-format off
|
||||
const char random_array[] =
|
||||
{
|
||||
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,
|
||||
0x00
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
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)
|
||||
{
|
||||
if(c >= '0' && c <= '9')
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
|
|
@ -331,7 +333,7 @@ void *tty_stdin_input_thread(void *arg)
|
|||
pthread_mutex_unlock(&mutex_input_ready);
|
||||
|
||||
// Input loop for stdin
|
||||
while (1)
|
||||
while (true)
|
||||
{
|
||||
/* Input from stdin ready */
|
||||
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++)
|
||||
{
|
||||
// first do key hit check for xmodem abort
|
||||
if (!key_hit) {
|
||||
if (!key_hit)
|
||||
{
|
||||
key_hit = input_buffer[i];
|
||||
byte_count--;
|
||||
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);
|
||||
break;
|
||||
case KEY_SHIFT_F:
|
||||
tio_printf("Flushed data I/O buffers")
|
||||
tio_printf("Flushed data I/O buffers");
|
||||
tcflush(device_fd, TCIOFLUSH);
|
||||
break;
|
||||
default:
|
||||
|
|
@ -414,7 +417,8 @@ void tty_input_thread_create(void)
|
|||
{
|
||||
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");
|
||||
exit(1);
|
||||
}
|
||||
|
|
@ -631,6 +635,7 @@ void tty_output_mode_set(output_mode_t mode)
|
|||
|
||||
static void mappings_print(void)
|
||||
{
|
||||
// clang-format off
|
||||
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_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");
|
||||
}
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
case KEY_M:
|
||||
// clang-format off
|
||||
/* Change mapping of characters on input or output */
|
||||
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)",
|
||||
|
|
@ -1039,6 +1046,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
|
|||
option.map_o_nulbrk ? "Unset" : "Set");
|
||||
tio_printf(" (c) OIGNCR: %s ignoring CR on output",
|
||||
option.map_o_ign_cr ? "Unset" : "Set");
|
||||
// clang-format on
|
||||
|
||||
// Process next input character as sub command
|
||||
sub_command = SUBCOMMAND_MAP;
|
||||
|
|
@ -1050,7 +1058,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
|
|||
|
||||
case KEY_R:
|
||||
/* Run script */
|
||||
tio_printf("Run Lua script")
|
||||
tio_printf("Run Lua script");
|
||||
tio_printf_raw("Enter file name: ");
|
||||
if (tio_readln())
|
||||
{
|
||||
|
|
@ -1126,7 +1134,8 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
|
|||
case KEY_Y:
|
||||
tio_printf("Send file with YMODEM");
|
||||
tio_printf_raw("Enter file name: ");
|
||||
if (tio_readln()) {
|
||||
if (tio_readln())
|
||||
{
|
||||
int ret;
|
||||
|
||||
tio_printf("Sending file '%s' ", line);
|
||||
|
|
@ -2044,7 +2053,8 @@ GList *tty_search_for_serial_devices(void)
|
|||
}
|
||||
|
||||
/* Populate device info */
|
||||
*device_info = (device_t) {
|
||||
*device_info = (device_t)
|
||||
{
|
||||
.path = devicePath,
|
||||
.tid = g_strdup(tid),
|
||||
.uptime = uptime,
|
||||
|
|
|
|||
264
src/xymodem.c
264
src/xymodem.c
|
|
@ -40,7 +40,8 @@
|
|||
|
||||
#define min(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
struct xpacket_1k {
|
||||
struct xpacket_1k
|
||||
{
|
||||
uint8_t type;
|
||||
uint8_t seq;
|
||||
uint8_t nseq;
|
||||
|
|
@ -49,7 +50,8 @@ struct xpacket_1k {
|
|||
uint8_t crc_lo;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct xpacket {
|
||||
struct xpacket
|
||||
{
|
||||
uint8_t type;
|
||||
uint8_t seq;
|
||||
uint8_t nseq;
|
||||
|
|
@ -63,7 +65,8 @@ static uint16_t crc16(const uint8_t *data, uint16_t size)
|
|||
{
|
||||
uint16_t crc, s;
|
||||
|
||||
for (crc = 0; size > 0; size--) {
|
||||
for (crc = 0; size > 0; size--)
|
||||
{
|
||||
s = *data++ ^ (crc >> 8);
|
||||
s ^= (s >> 4);
|
||||
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
|
||||
* last drained character being 'C'.
|
||||
*/
|
||||
while(1) {
|
||||
while (true)
|
||||
{
|
||||
if (key_hit)
|
||||
return -1;
|
||||
rc = read_poll(sio, &resp, 1, 50);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
if (resp == 'C') break;
|
||||
if (resp == CAN) return ERR;
|
||||
continue;
|
||||
}
|
||||
else if (rc < 0) {
|
||||
else if (rc < 0)
|
||||
{
|
||||
tio_error_print("Read sync from serial failed");
|
||||
return ERR;
|
||||
}
|
||||
|
|
@ -100,7 +106,8 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
|
|||
packet.seq = seq;
|
||||
packet.type = STX;
|
||||
|
||||
while (len) {
|
||||
while (len)
|
||||
{
|
||||
size_t sz, z = 0;
|
||||
char *from, status;
|
||||
|
||||
|
|
@ -116,11 +123,14 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
|
|||
/* Send packet */
|
||||
from = (char *) &packet;
|
||||
sz = sizeof(packet);
|
||||
while (sz) {
|
||||
while (sz)
|
||||
{
|
||||
if (key_hit)
|
||||
return ERR;
|
||||
if ((rc = write(sio, from, sz)) < 0 ) {
|
||||
if (errno == EWOULDBLOCK) {
|
||||
if ((rc = write(sio, from, sz)) < 0 )
|
||||
{
|
||||
if (errno == EWOULDBLOCK)
|
||||
{
|
||||
usleep(1000);
|
||||
continue;
|
||||
}
|
||||
|
|
@ -138,20 +148,25 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
|
|||
if (seq == 0 && packet.data[0] == 0) resp = ACK;
|
||||
|
||||
/* Read receiver response, timeout 1 s */
|
||||
for(int n=0; n < 20; n++) {
|
||||
for (int n = 0; n < 20; n++)
|
||||
{
|
||||
if (key_hit)
|
||||
return ERR;
|
||||
rc = read_poll(sio, &resp, 1, 50);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Read ack/nak from serial failed");
|
||||
return ERR;
|
||||
} else if(rc > 0) {
|
||||
}
|
||||
else if (rc > 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update "progress bar" */
|
||||
switch (resp) {
|
||||
switch (resp)
|
||||
{
|
||||
case NAK: status = 'N'; break;
|
||||
case ACK: status = '.'; break;
|
||||
case 'C': status = 'C'; break;
|
||||
|
|
@ -161,7 +176,8 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq)
|
|||
write(STDOUT_FILENO, &status, 1);
|
||||
|
||||
/* Move to next block after ACK */
|
||||
if (resp == ACK) {
|
||||
if (resp == ACK)
|
||||
{
|
||||
packet.seq++;
|
||||
len -= 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 */
|
||||
while (seq) {
|
||||
while (seq)
|
||||
{
|
||||
if (key_hit)
|
||||
return ERR;
|
||||
if (write(sio, EOT_STR, 1) < 0) {
|
||||
if (write(sio, EOT_STR, 1) < 0)
|
||||
{
|
||||
tio_error_print("Write EOT to serial failed");
|
||||
return ERR;
|
||||
}
|
||||
write(STDOUT_FILENO, "|", 1);
|
||||
/* 1s timeout */
|
||||
rc = read_poll(sio, &resp, 1, 1000);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Read from serial failed");
|
||||
return ERR;
|
||||
} else if(rc == 0) {
|
||||
}
|
||||
else if (rc == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (resp == ACK || resp == CAN) {
|
||||
if (resp == ACK || resp == CAN)
|
||||
{
|
||||
write(STDOUT_FILENO, "\r\n", 2);
|
||||
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
|
||||
* last drained character being 'C'.
|
||||
*/
|
||||
while(1) {
|
||||
while (true)
|
||||
{
|
||||
if (key_hit)
|
||||
return -1;
|
||||
rc = read_poll(sio, &resp, 1, 50);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
if (resp == 'C') break;
|
||||
if (resp == CAN) return ERR;
|
||||
continue;
|
||||
}
|
||||
else if (rc < 0) {
|
||||
else if (rc < 0)
|
||||
{
|
||||
tio_error_print("Read sync from serial failed");
|
||||
return ERR;
|
||||
}
|
||||
|
|
@ -222,7 +247,8 @@ static int xmodem(int sio, const void *data, size_t len)
|
|||
packet.seq = 1;
|
||||
packet.type = SOH;
|
||||
|
||||
while (len) {
|
||||
while (len)
|
||||
{
|
||||
size_t sz, z = 0;
|
||||
char *from, status;
|
||||
|
||||
|
|
@ -238,11 +264,14 @@ static int xmodem(int sio, const void *data, size_t len)
|
|||
/* Send packet */
|
||||
from = (char *) &packet;
|
||||
sz = sizeof(packet);
|
||||
while (sz) {
|
||||
while (sz)
|
||||
{
|
||||
if (key_hit)
|
||||
return ERR;
|
||||
if ((rc = write(sio, from, sz)) < 0 ) {
|
||||
if (errno == EWOULDBLOCK) {
|
||||
if ((rc = write(sio, from, sz)) < 0 )
|
||||
{
|
||||
if (errno == EWOULDBLOCK)
|
||||
{
|
||||
usleep(1000);
|
||||
continue;
|
||||
}
|
||||
|
|
@ -257,20 +286,25 @@ static int xmodem(int sio, const void *data, size_t len)
|
|||
resp = 0;
|
||||
|
||||
/* Read receiver response, timeout 1 s */
|
||||
for(int n=0; n < 20; n++) {
|
||||
for (int n = 0; n < 20; n++)
|
||||
{
|
||||
if (key_hit)
|
||||
return ERR;
|
||||
rc = read_poll(sio, &resp, 1, 50);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Read ack/nak from serial failed");
|
||||
return ERR;
|
||||
} else if(rc > 0) {
|
||||
}
|
||||
else if (rc > 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update "progress bar" */
|
||||
switch (resp) {
|
||||
switch (resp)
|
||||
{
|
||||
case NAK: status = 'N'; break;
|
||||
case ACK: status = '.'; break;
|
||||
case 'C': status = 'C'; break;
|
||||
|
|
@ -280,7 +314,8 @@ static int xmodem(int sio, const void *data, size_t len)
|
|||
write(STDOUT_FILENO, &status, 1);
|
||||
|
||||
/* Move to next block after ACK */
|
||||
if (resp == ACK) {
|
||||
if (resp == ACK)
|
||||
{
|
||||
packet.seq++;
|
||||
len -= 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 */
|
||||
while (1) {
|
||||
while (true)
|
||||
{
|
||||
if (key_hit)
|
||||
return ERR;
|
||||
if (write(sio, EOT_STR, 1) < 0) {
|
||||
if (write(sio, EOT_STR, 1) < 0)
|
||||
{
|
||||
tio_error_print("Write EOT to serial failed");
|
||||
return ERR;
|
||||
}
|
||||
write(STDOUT_FILENO, "|", 1);
|
||||
/* 1s timeout */
|
||||
rc = read_poll(sio, &resp, 1, 1000);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Read from serial failed");
|
||||
return ERR;
|
||||
} else if(rc == 0) {
|
||||
}
|
||||
else if (rc == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (resp == ACK || resp == CAN) {
|
||||
if (resp == ACK || resp == CAN)
|
||||
{
|
||||
write(STDOUT_FILENO, "\r\n", 2);
|
||||
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
|
||||
that the transfer did not start. */
|
||||
rc = write(sio, "C", 1);
|
||||
if (rc < 0) {
|
||||
if (errno == EWOULDBLOCK) {
|
||||
if (rc < 0)
|
||||
{
|
||||
if (errno == EWOULDBLOCK)
|
||||
{
|
||||
usleep(1000);
|
||||
continue;
|
||||
}
|
||||
|
|
@ -385,19 +428,25 @@ int receive_packet(int sio, struct xpacket packet, int fd)
|
|||
|
||||
/* Read seq bytes*/
|
||||
rc = read_poll(sio, &rxSeq1, 1, 3000);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
tio_error_print("Timeout waiting for first seq byte");
|
||||
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;
|
||||
}
|
||||
rc = read_poll(sio, &rxSeq2, 1, 3000);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
tio_error_print("Timeout waiting for second seq byte");
|
||||
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;
|
||||
}
|
||||
if (key_hit)
|
||||
|
|
@ -412,15 +461,19 @@ int receive_packet(int sio, struct xpacket packet, int fd)
|
|||
{
|
||||
tio_error_print("Timeout waiting for next packet char");
|
||||
rc = write(sio, CAN_STR, 1);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Write cancel packet to serial failed");
|
||||
return ERR_FATAL;
|
||||
}
|
||||
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);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Write cancel packet to serial failed");
|
||||
}
|
||||
return ERR_FATAL;
|
||||
|
|
@ -433,11 +486,14 @@ int receive_packet(int sio, struct xpacket packet, int fd)
|
|||
|
||||
/* Read CRC */
|
||||
rc = read_poll(sio, &resp, 1, 3000);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
tio_error_print("Timeout waiting for first CRC byte");
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -446,11 +502,14 @@ int receive_packet(int sio, struct xpacket packet, int fd)
|
|||
rxCrc = uresp16 << 8;
|
||||
|
||||
rc = read_poll(sio, &resp, 1, 3000);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
tio_error_print("Timeout waiting for second CRC byte");
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -469,7 +528,8 @@ int receive_packet(int sio, struct xpacket packet, int fd)
|
|||
tio_error_print("%s", strerror(errno));
|
||||
tio_error_print("Poll check error after packet finish");
|
||||
rc = write(sio, CAN_STR, 1);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Write cancel packet to serial failed");
|
||||
}
|
||||
return ERR_FATAL;
|
||||
|
|
@ -494,7 +554,8 @@ int receive_packet(int sio, struct xpacket packet, int fd)
|
|||
{
|
||||
/* Resend of previously processed packet. */
|
||||
rc = write(sio, ACK_STR, 1);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Write acknowlegdement packet to serial failed");
|
||||
return ERR_FATAL;
|
||||
}
|
||||
|
|
@ -520,7 +581,8 @@ int receive_packet(int sio, struct xpacket packet, int fd)
|
|||
{
|
||||
tio_error_print("Problem writing to file");
|
||||
rc = write(sio, CAN_STR, 1);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Write cancel packet to serial failed");
|
||||
}
|
||||
return ERR_FATAL;
|
||||
|
|
@ -545,16 +607,20 @@ int xmodem_receive(int sio, int fd)
|
|||
char status;
|
||||
|
||||
/* Drain pending characters from serial line.*/
|
||||
while(1) {
|
||||
while (true)
|
||||
{
|
||||
if (key_hit)
|
||||
return -1;
|
||||
rc = read_poll(sio, &resp, 1, 50);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
if (resp == CAN) return ERR;
|
||||
break;
|
||||
}
|
||||
else if (rc < 0) {
|
||||
if (rc != USER_CAN) {
|
||||
else if (rc < 0)
|
||||
{
|
||||
if (rc != USER_CAN)
|
||||
{
|
||||
tio_error_print("Read sync from serial failed");
|
||||
}
|
||||
return ERR;
|
||||
|
|
@ -571,50 +637,67 @@ int xmodem_receive(int sio, int fd)
|
|||
{
|
||||
tio_error_print("Timeout waiting for transfer to start");
|
||||
return ERR;
|
||||
} else if (rc < 0) {
|
||||
}
|
||||
else if (rc < 0)
|
||||
{
|
||||
tio_error_print("Error starting XMODEM receive");
|
||||
return ERR;
|
||||
}
|
||||
|
||||
while (!complete) {
|
||||
while (!complete)
|
||||
{
|
||||
/* Poll for 1 new byte for 3 seconds */
|
||||
rc = read_poll(sio, &resp, 1, 3000);
|
||||
if (rc == 0) {
|
||||
if (rc == 0)
|
||||
{
|
||||
tio_error_print("Timeout waiting for start of next packet");
|
||||
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;
|
||||
}
|
||||
if (key_hit)
|
||||
return USER_CAN;
|
||||
|
||||
switch(resp)
|
||||
switch (resp)
|
||||
{
|
||||
case SOH:
|
||||
/* Start of a packet */
|
||||
rc = receive_packet(sio, packet, fd);
|
||||
if (rc == OK) {
|
||||
if (rc == OK)
|
||||
{
|
||||
packet.seq++;
|
||||
status = '.';
|
||||
} else if (rc == ERR) {
|
||||
}
|
||||
else if (rc == ERR)
|
||||
{
|
||||
rc = write(sio, NAK_STR, 1);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Writing not acknowledge packet to serial failed");
|
||||
return ERR;
|
||||
}
|
||||
status = 'N';
|
||||
} else if (rc == ERR_FATAL) {
|
||||
}
|
||||
else if (rc == ERR_FATAL)
|
||||
{
|
||||
tio_error_print("Receive cancelled due to fatal error");
|
||||
return ERR;
|
||||
} else if (rc == USER_CAN) {
|
||||
}
|
||||
else if (rc == USER_CAN)
|
||||
{
|
||||
rc = write(sio, CAN_STR, 1);
|
||||
if (rc < 0) {
|
||||
if (rc < 0)
|
||||
{
|
||||
tio_error_print("Writing cancel to serial failed");
|
||||
return ERR;
|
||||
}
|
||||
return USER_CAN;
|
||||
} else if (rc == RX_IGNORE) {
|
||||
}
|
||||
else if (rc == RX_IGNORE)
|
||||
{
|
||||
status = ':';
|
||||
}
|
||||
break;
|
||||
|
|
@ -644,7 +727,6 @@ int xmodem_receive(int sio, int fd)
|
|||
break;
|
||||
}
|
||||
|
||||
|
||||
/* Update "progress bar" */
|
||||
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 */
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
if (fd < 0)
|
||||
{
|
||||
tio_error_print("Could not open file");
|
||||
return ERR;
|
||||
}
|
||||
fstat(fd, &stat);
|
||||
len = stat.st_size;
|
||||
buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (!buf) {
|
||||
if (!buf)
|
||||
{
|
||||
close(fd);
|
||||
tio_error_print("Could not mmap file");
|
||||
return ERR;
|
||||
|
|
@ -675,15 +759,19 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode)
|
|||
|
||||
/* Do transfer */
|
||||
key_hit = 0;
|
||||
if (mode == XMODEM_1K) {
|
||||
if (mode == XMODEM_1K)
|
||||
{
|
||||
rc = xmodem_1k(sio, buf, len, 1);
|
||||
}
|
||||
else if (mode == XMODEM_CRC) {
|
||||
else if (mode == XMODEM_CRC)
|
||||
{
|
||||
rc = xmodem(sio, buf, len);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
/* Ymodem: hdr + file + fin */
|
||||
while(1) {
|
||||
while (true)
|
||||
{
|
||||
char hdr[1024], *p;
|
||||
|
||||
rc = -1;
|
||||
|
|
@ -712,21 +800,25 @@ int xymodem_receive(int sio, const char *filename, modem_mode_t mode)
|
|||
|
||||
/* Create new file */
|
||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
|
||||
if (fd < 0) {
|
||||
if (fd < 0)
|
||||
{
|
||||
tio_error_print("Could not open file");
|
||||
return ERR;
|
||||
}
|
||||
|
||||
/* Do transfer */
|
||||
key_hit = 0;
|
||||
if (mode == XMODEM_1K) {
|
||||
if (mode == XMODEM_1K)
|
||||
{
|
||||
tio_error_print("Not supported");
|
||||
rc = -1;
|
||||
}
|
||||
else if (mode == XMODEM_CRC) {
|
||||
else if (mode == XMODEM_CRC)
|
||||
{
|
||||
rc = xmodem_receive(sio, fd);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
tio_error_print("Not supported");
|
||||
rc = -1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
typedef enum {
|
||||
typedef enum
|
||||
{
|
||||
XMODEM_1K,
|
||||
XMODEM_CRC,
|
||||
YMODEM,
|
||||
|
|
@ -30,5 +31,4 @@ typedef enum {
|
|||
extern char key_hit;
|
||||
|
||||
int xymodem_send(int sio, const char *filename, modem_mode_t mode);
|
||||
|
||||
int xymodem_receive(int sio, const char *filename, modem_mode_t mode);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue