1
0
mirror of https://github.com/UzixLS/picocom.git synced 2025-07-19 07:21:18 +03:00

Small refactoring in term.c for bsd improvements

Introduced `#define HAS_CUSTOM_BAUD` for platforms which have appropriate implementation.

Moving that `#if defined(__linux__) && defined(USE_CUSTOM_BAUD)` ... stuff out of `term.c`and put it to `termios2.h` / `custbaud_bsd.h` headers.
Moving that platform specific `#ifdefs` to the platform specific headers helps removing duplicate complex `#ifdef` constructs.
The both platform specific headers then do `#define HAS_CUSTOM_BAUD` to signal that custom baudrate support is implemented for the target platform.
I'd tested all above mentioned platforms with both USE_CUSTOM_BAUD unset and set to check if that refactoring don't raises unexpected issues.
This commit is contained in:
Joe Merten
2018-01-15 19:47:46 +01:00
parent ab140a283b
commit f9e0b4c8a2
6 changed files with 38 additions and 30 deletions

View File

@ -38,12 +38,8 @@
* - Have not tested with more recent macOS or Ftdi driver until now.
*/
/* Note that this code might also work with OpenBSD, NetBSD et cetera, but I have not tested.
* That's why I only check for `defined (__FreeBSD__)` here.
*/
#if (defined (__FreeBSD__) || defined(__APPLE__)) && defined(USE_CUSTOM_BAUD)
#include "custbaud_bsd.h"
#ifdef HAS_CUSTOM_BAUD
#include <string.h>
#include <stdio.h>
#include <errno.h>
@ -54,9 +50,10 @@
#include "term.h"
/***************************************************************************/
/* As we can see in FreeBSD and macOS termios.h all the baudrate constants are transparent,
* like B115200=115200. There is no need for any integer <-> code translation.
* So we can pass any baudrate we want directly to / from cfsetospeed() & co.
/* As we can see in BSD and macOS termios.h all the baudrate constants are
* transparent, like B115200=115200. There is no need for any integer <-> code
* translation. So we can pass any baudrate we want directly to / from
* cfsetospeed() & co.
*/
int cfsetospeed_custom(struct termios *tiop, int speed) {
return cfsetospeed(tiop, speed);
@ -142,4 +139,4 @@ int tcsetattr_custom(int fd, int optional_actions, const struct termios *tiop) {
/***************************************************************************/
#endif /* FreeBSD || __APPLE__ && USE_CUSTOM_BAUD */
#endif /* HAS_CUSTOM_BAUD */

View File

@ -6,6 +6,8 @@
* by Joe Merten (https://github.com/JoeMerten www.jme.de)
*
* ATTENTION: BSD and macOS specific stuff!
* This header will `#define HAS_CUSTOM_BAUD` in case of custom baud rate
* implementation is available for the current target platform.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -26,6 +28,13 @@
#ifndef CUSTBAUD_BSD_H
#define CUSTBAUD_BSD_H
/* Note that this code might also work other BSD variants, but I have only
* tested with those listed below.
*/
#if (defined (__FreeBSD__) || defined(__APPLE__)) && defined(USE_CUSTOM_BAUD)
#define HAS_CUSTOM_BAUD
#include <termios.h>
/***************************************************************************/
@ -36,7 +45,7 @@
* tcsetattr_custom()), we want to provide the values here to have them
* available for term_baud_up()/down().
*
* FreeBSD termios.h has 460k and 921k but misses e.g. 500k and >=1M.
* FreeBSD 11.0 termios.h has 460k and 921k but misses e.g. 500k and >=1M.
*/
#if defined(HIGH_BAUD)
@ -97,4 +106,5 @@ int tcsetattr_custom(int fd, int optional_actions, const struct termios *tiop);
/***************************************************************************/
#endif /* __FreeBSD__ || __APPLE__ && USE_CUSTOM_BAUD */
#endif /* CUSTBAUD_BSD_H */

View File

@ -1601,7 +1601,7 @@ show_usage(char *name)
printf(" LINENOISE is enabled\n");
printf(" HISTFILE is: %s\n", HISTFILE);
#endif
#ifdef USE_CUSTOM_BAUD
#if defined(USE_CUSTOM_BAUD) && defined(HAS_CUSTOM_BAUD)
printf(" USE_CUSTOM_BAUD is enabled\n");
#endif

18
term.c
View File

@ -60,15 +60,11 @@
#include <sys/ioctl.h>
#endif
#if defined(__linux__) && defined(USE_CUSTOM_BAUD)
/* only works for linux, recent kernels */
#include "termios2.h"
#endif
#if (defined (__FreeBSD__) || defined(__APPLE__)) && defined(USE_CUSTOM_BAUD)
/* only for FreeBSD and macOS (Tiger and above) */
/* only for some BSD and macOS (Tiger and above) */
#include "custbaud_bsd.h"
#endif
/* Time to wait for UART to clear after a drain (in usec). */
#define DRAIN_DELAY 200000
@ -309,7 +305,7 @@ Bspeed(speed_t code)
int
term_baud_ok(int baud)
{
#ifndef USE_CUSTOM_BAUD
#ifndef HAS_CUSTOM_BAUD
return (Bcode(baud) != BNONE) ? 1 : 0;
#else
return (baud >= 0);
@ -801,7 +797,7 @@ term_set_baudrate (int fd, int baudrate)
}
cfsetispeed(&tio, B0);
} else {
#ifdef USE_CUSTOM_BAUD
#ifdef HAS_CUSTOM_BAUD
r = cfsetospeed_custom(&tio, baudrate);
if ( r < 0 ) {
term_errno = TERM_ESETOSPEED;
@ -809,11 +805,11 @@ term_set_baudrate (int fd, int baudrate)
break;
}
cfsetispeed(&tio, B0);
#else /* ! defined USE_CUSTOM_BAUD */
#else /* ! defined HAS_CUSTOM_BAUD */
term_errno = TERM_EBAUD;
rval = -1;
break;
#endif /* of USE_CUSTOM_BAUD */
#endif /* of HAS_CUSTOM_BAUD */
}
term.nexttermios[i] = tio;
@ -840,7 +836,7 @@ term_get_baudrate (int fd, int *ispeed)
if ( ispeed ) {
code = cfgetispeed(&term.currtermios[i]);
*ispeed = Bspeed(code);
#ifdef USE_CUSTOM_BAUD
#ifdef HAS_CUSTOM_BAUD
if ( *ispeed < 0 ) {
*ispeed = cfgetispeed_custom(&term.currtermios[i]);
}
@ -849,7 +845,7 @@ term_get_baudrate (int fd, int *ispeed)
code = cfgetospeed(&term.currtermios[i]);
ospeed = Bspeed(code);
if ( ospeed < 0 ) {
#ifdef USE_CUSTOM_BAUD
#ifdef HAS_CUSTOM_BAUD
ospeed = cfgetospeed_custom(&term.currtermios[i]);
if ( ospeed < 0 ) {
term_errno = TERM_EGETSPEED;

View File

@ -23,13 +23,8 @@
* USA
*/
#if defined(__linux__) && defined(USE_CUSTOM_BAUD)
#include <linux/version.h>
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
#error "This code requires Linux kernel > 2.6!"
#endif
#include "termios2.h"
#ifdef HAS_CUSTOM_BAUD
#include <string.h>
#include <unistd.h>
#include <errno.h>
@ -183,7 +178,7 @@ cf2setispeed_custom(struct termios *tios, int speed)
/***************************************************************************/
#endif /* __linux__ && USE_CUSTOM_BAUD */
#endif /* HAS_CUSTOM_BAUD */
/*
* Local Variables:

View File

@ -6,6 +6,8 @@
* by Nick Patavalis (npat@efault.net)
*
* ATTENTION: Linux-specific kludge!
* This header will `#define HAS_CUSTOM_BAUD` in case of custom baud rate
* implementation is available for the current target platform.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -26,6 +28,12 @@
#ifndef TERMIOS2_H
#define TERMIOS2_H
#if defined(__linux__) && defined(USE_CUSTOM_BAUD)
#include <linux/version.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
#define HAS_CUSTOM_BAUD
#include <termios.h>
/* Replace termios functions, with termios2 functions */
@ -71,6 +79,8 @@ int cf2setospeed_custom(struct termios *tios, int speed);
/***************************************************************************/
#endif /* of LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) */
#endif /* of defined(__linux__) && defined(USE_CUSTOM_BAUD) */
#endif /* of TERMIOS2_H */
/***************************************************************************/