From 04860bf422c1dca365ddc9818d33297351257bf2 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Fri, 7 Dec 2018 05:22:35 +0800 Subject: [PATCH] [spi-hdlc-adapter] make pretty spi-hdlc-adapter (#3341) --- tools/Makefile.am | 1 + tools/spi-hdlc-adapter/spi-hdlc-adapter.c | 742 ++++++++++------------ 2 files changed, 338 insertions(+), 405 deletions(-) diff --git a/tools/Makefile.am b/tools/Makefile.am index 4eeded266..86075d83d 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -50,6 +50,7 @@ endif # OPENTHREAD_BUILD_TOOLS # Always pretty (e.g. for 'make pretty') these subdirectories. PRETTY_SUBDIRS = \ + spi-hdlc-adapter \ $(NULL) include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/tools/spi-hdlc-adapter/spi-hdlc-adapter.c b/tools/spi-hdlc-adapter/spi-hdlc-adapter.c index b5563618a..19cbaaab1 100644 --- a/tools/spi-hdlc-adapter/spi-hdlc-adapter.c +++ b/tools/spi-hdlc-adapter/spi-hdlc-adapter.c @@ -32,23 +32,23 @@ #endif #include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include #include -#include +#include +#include -#include -#include -#include -#include #include +#include +#include +#include +#include #include #include @@ -68,62 +68,69 @@ /* ------------------------------------------------------------------------- */ /* MARK: Macros and Constants */ -#define SPI_HDLC_VERSION "0.07" +#define SPI_HDLC_VERSION "0.07" -#define MAX_FRAME_SIZE 2048 -#define HEADER_LEN 5 -#define SPI_HEADER_RESET_FLAG 0x80 -#define SPI_HEADER_CRC_FLAG 0x40 -#define SPI_HEADER_PATTERN_VALUE 0x02 -#define SPI_HEADER_PATTERN_MASK 0x03 +#define MAX_FRAME_SIZE 2048 +#define HEADER_LEN 5 +#define SPI_HEADER_RESET_FLAG 0x80 +#define SPI_HEADER_CRC_FLAG 0x40 +#define SPI_HEADER_PATTERN_VALUE 0x02 +#define SPI_HEADER_PATTERN_MASK 0x03 -#define EXIT_QUIT 65535 +#define EXIT_QUIT 65535 #ifndef MSEC_PER_SEC -#define MSEC_PER_SEC 1000 +#define MSEC_PER_SEC 1000 #endif #ifndef USEC_PER_MSEC -#define USEC_PER_MSEC 1000 +#define USEC_PER_MSEC 1000 #endif #ifndef USEC_PER_SEC -#define USEC_PER_SEC (USEC_PER_MSEC * MSEC_PER_SEC) +#define USEC_PER_SEC (USEC_PER_MSEC * MSEC_PER_SEC) #endif -#define SPI_POLL_PERIOD_MSEC (MSEC_PER_SEC/30) +#define SPI_POLL_PERIOD_MSEC (MSEC_PER_SEC / 30) -#define IMMEDIATE_RETRY_COUNT 5 -#define FAST_RETRY_COUNT 15 +#define IMMEDIATE_RETRY_COUNT 5 +#define FAST_RETRY_COUNT 15 -#define IMMEDIATE_RETRY_TIMEOUT_MSEC 1 -#define FAST_RETRY_TIMEOUT_MSEC 10 -#define SLOW_RETRY_TIMEOUT_MSEC 33 +#define IMMEDIATE_RETRY_TIMEOUT_MSEC 1 +#define FAST_RETRY_TIMEOUT_MSEC 10 +#define SLOW_RETRY_TIMEOUT_MSEC 33 -#define GPIO_INT_ASSERT_STATE 0 // I̅N̅T̅ is asserted low -#define GPIO_RES_ASSERT_STATE 0 // R̅E̅S̅ is asserted low +#define GPIO_INT_ASSERT_STATE 0 // I̅N̅T̅ is asserted low +#define GPIO_RES_ASSERT_STATE 0 // R̅E̅S̅ is asserted low -#define SPI_RX_ALIGN_ALLOWANCE_MAX 16 +#define SPI_RX_ALIGN_ALLOWANCE_MAX 16 -#define SOCKET_DEBUG_BYTES_PER_LINE 16 +#define SOCKET_DEBUG_BYTES_PER_LINE 16 #ifndef AUTO_PRINT_BACKTRACE -#define AUTO_PRINT_BACKTRACE (HAVE_EXECINFO_H) +#define AUTO_PRINT_BACKTRACE (HAVE_EXECINFO_H) #endif -#define AUTO_PRINT_BACKTRACE_STACK_DEPTH 20 +#define AUTO_PRINT_BACKTRACE_STACK_DEPTH 20 -static const uint8_t kHdlcResetSignal[] = { 0x7E, 0x13, 0x11, 0x7E }; +static const uint8_t kHdlcResetSignal[] = {0x7E, 0x13, 0x11, 0x7E}; static const uint16_t kHdlcCrcCheckValue = 0xf0b8; static const uint16_t kHdlcCrcResetValue = 0xffff; -enum { +enum +{ MODE_STDIO = 0, - MODE_PTY = 1, + MODE_PTY = 1, }; // Ignores return value from function 's' -#define IGNORE_RETURN_VALUE(s) do { if (s){} } while (0) +#define IGNORE_RETURN_VALUE(s) \ + do \ + { \ + if (s) \ + { \ + } \ + } while (0) /* ------------------------------------------------------------------------- */ /* MARK: Global State */ @@ -134,33 +141,33 @@ static int sMode = MODE_PTY; static int sMode = MODE_STDIO; #endif -static const char* sSpiDevPath = NULL; -static const char* sIntGpioDevPath = NULL; -static const char* sResGpioDevPath = NULL; +static const char *sSpiDevPath = NULL; +static const char *sIntGpioDevPath = NULL; +static const char *sResGpioDevPath = NULL; -static int sVerbose = LOG_NOTICE; +static int sVerbose = LOG_NOTICE; -static int sSpiDevFd = -1; -static int sResGpioValueFd = -1; -static int sIntGpioValueFd = -1; +static int sSpiDevFd = -1; +static int sResGpioValueFd = -1; +static int sIntGpioValueFd = -1; -static int sHdlcInputFd = -1; -static int sHdlcOutputFd = -1; +static int sHdlcInputFd = -1; +static int sHdlcOutputFd = -1; -static int sSpiSpeed = 1000000; // in Hz (default: 1MHz) -static uint8_t sSpiMode = 0; -static int sSpiCsDelay = 20; // in microseconds +static int sSpiSpeed = 1000000; // in Hz (default: 1MHz) +static uint8_t sSpiMode = 0; +static int sSpiCsDelay = 20; // in microseconds static uint16_t sSpiRxPayloadSize; -static uint8_t sSpiRxFrameBuffer[MAX_FRAME_SIZE + SPI_RX_ALIGN_ALLOWANCE_MAX]; +static uint8_t sSpiRxFrameBuffer[MAX_FRAME_SIZE + SPI_RX_ALIGN_ALLOWANCE_MAX]; static uint16_t sSpiTxPayloadSize; -static bool sSpiTxIsReady = false; -static int sSpiTxRefusedCount = 0; -static uint8_t sSpiTxFrameBuffer[MAX_FRAME_SIZE + SPI_RX_ALIGN_ALLOWANCE_MAX]; +static bool sSpiTxIsReady = false; +static int sSpiTxRefusedCount = 0; +static uint8_t sSpiTxFrameBuffer[MAX_FRAME_SIZE + SPI_RX_ALIGN_ALLOWANCE_MAX]; static int sSpiRxAlignAllowance = 0; -static int sSpiSmallPacketSize = 32; // in bytes +static int sSpiSmallPacketSize = 32; // in bytes static bool sSlaveDidReset = false; @@ -181,17 +188,17 @@ static sig_t sPreviousHandlerForSIGTERM; /* ------------------------------------------------------------------------- */ /* MARK: Statistics */ -static uint64_t sSlaveResetCount = 0; -static uint64_t sSpiFrameCount = 0; -static uint64_t sSpiValidFrameCount = 0; -static uint64_t sSpiGarbageFrameCount = 0; -static uint64_t sSpiDuplexFrameCount = 0; +static uint64_t sSlaveResetCount = 0; +static uint64_t sSpiFrameCount = 0; +static uint64_t sSpiValidFrameCount = 0; +static uint64_t sSpiGarbageFrameCount = 0; +static uint64_t sSpiDuplexFrameCount = 0; static uint64_t sSpiUnresponsiveFrameCount = 0; -static uint64_t sHdlcRxFrameByteCount = 0; -static uint64_t sHdlcTxFrameByteCount = 0; -static uint64_t sHdlcRxFrameCount = 0; -static uint64_t sHdlcTxFrameCount = 0; -static uint64_t sHdlcRxBadCrcCount = 0; +static uint64_t sHdlcRxFrameByteCount = 0; +static uint64_t sHdlcTxFrameByteCount = 0; +static uint64_t sHdlcRxFrameCount = 0; +static uint64_t sHdlcTxFrameCount = 0; +static uint64_t sHdlcRxBadCrcCount = 0; /* ------------------------------------------------------------------------- */ /* MARK: Signal Handlers */ @@ -204,7 +211,7 @@ static void signal_SIGINT(int sig) // Can't use syslog() because it isn't async signal safe. // So we write to stderr - IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message)-1)); + IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message) - 1)); // Restore the previous handler so that if we end up getting // this signal again we perform the system default action. @@ -223,7 +230,7 @@ static void signal_SIGTERM(int sig) // Can't use syslog() because it isn't async signal safe. // So we write to stderr - IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message)-1)); + IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message) - 1)); // Restore the previous handler so that if we end up getting // this signal again we perform the system default action. @@ -242,7 +249,7 @@ static void signal_SIGHUP(int sig) // Can't use syslog() because it isn't async signal safe. // So we write to stderr - IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message)-1)); + IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message) - 1)); // We don't restore the "previous handler" // because we always want to let the main @@ -262,34 +269,34 @@ static void signal_dumpstats(int sig) static void signal_clearstats(int sig) { - sDumpStats = true; - sSlaveResetCount = 0; - sSpiFrameCount = 0; - sSpiValidFrameCount = 0; - sSpiGarbageFrameCount = 0; - sSpiDuplexFrameCount = 0; + sDumpStats = true; + sSlaveResetCount = 0; + sSpiFrameCount = 0; + sSpiValidFrameCount = 0; + sSpiGarbageFrameCount = 0; + sSpiDuplexFrameCount = 0; sSpiUnresponsiveFrameCount = 0; - sHdlcRxFrameByteCount = 0; - sHdlcTxFrameByteCount = 0; - sHdlcRxFrameCount = 0; - sHdlcTxFrameCount = 0; - sHdlcRxBadCrcCount = 0; + sHdlcRxFrameByteCount = 0; + sHdlcTxFrameByteCount = 0; + sHdlcRxFrameCount = 0; + sHdlcTxFrameCount = 0; + sHdlcRxBadCrcCount = 0; // Ignore signal argument. (void)sig; } #if AUTO_PRINT_BACKTRACE -static void signal_critical(int sig, siginfo_t * info, void * ucontext) +static void signal_critical(int sig, siginfo_t *info, void *ucontext) { // This is the last hurah for this process. // We dump the stack, because that's all we can do. - void *stack_mem[AUTO_PRINT_BACKTRACE_STACK_DEPTH]; - void **stack = stack_mem; - char **stack_symbols; - int stack_depth, i; - ucontext_t *uc = (ucontext_t*)ucontext; + void * stack_mem[AUTO_PRINT_BACKTRACE_STACK_DEPTH]; + void ** stack = stack_mem; + char ** stack_symbols; + int stack_depth, i; + ucontext_t *uc = (ucontext_t *)ucontext; // Shut up compiler warning. (void)uc; @@ -312,11 +319,11 @@ static void signal_critical(int sig, siginfo_t * info, void * ucontext) // Here are are trying to update the pointer in the backtrace // to be the actual location of the fault. #if defined(__x86_64__) - stack[1] = (void *) uc->uc_mcontext.gregs[REG_RIP]; + stack[1] = (void *)uc->uc_mcontext.gregs[REG_RIP]; #elif defined(__i386__) - stack[1] = (void *) uc->uc_mcontext.gregs[REG_EIP]; + stack[1] = (void *)uc->uc_mcontext.gregs[REG_EIP]; #elif defined(__arm__) - stack[1] = (void *) uc->uc_mcontext.arm_ip; + stack[1] = (void *)uc->uc_mcontext.arm_ip; #else #warning TODO: Add this arch to signal_critical #endif @@ -340,7 +347,7 @@ static void signal_critical(int sig, siginfo_t * info, void * ucontext) } #endif // if AUTO_PRINT_BACKTRACE -static void log_debug_buffer(const char* desc, const uint8_t* buffer_ptr, int buffer_len, bool force) +static void log_debug_buffer(const char *desc, const uint8_t *buffer_ptr, int buffer_len, bool force) { int i = 0; @@ -351,15 +358,15 @@ static void log_debug_buffer(const char* desc, const uint8_t* buffer_ptr, int bu while (i < buffer_len) { - int j; - char dump_string[SOCKET_DEBUG_BYTES_PER_LINE*3+1]; + int j; + char dump_string[SOCKET_DEBUG_BYTES_PER_LINE * 3 + 1]; for (j = 0; i < buffer_len && j < SOCKET_DEBUG_BYTES_PER_LINE; i++, j++) { - sprintf(dump_string+j*3, "%02X ", buffer_ptr[i]); + sprintf(dump_string + j * 3, "%02X ", buffer_ptr[i]); } - syslog(force ? LOG_WARNING : LOG_DEBUG, "%s: %s%s", desc, dump_string, (i < buffer_len)?" ...":""); + syslog(force ? LOG_WARNING : LOG_DEBUG, "%s: %s%s", desc, dump_string, (i < buffer_len) ? " ..." : ""); } } @@ -390,18 +397,18 @@ static uint8_t spi_header_get_flag_byte(const uint8_t *header) static uint16_t spi_header_get_accept_len(const uint8_t *header) { - return ( header[1] + (uint16_t)(header[2] << 8) ); + return (header[1] + (uint16_t)(header[2] << 8)); } static uint16_t spi_header_get_data_len(const uint8_t *header) { - return ( header[3] + (uint16_t)(header[4] << 8) ); + return (header[3] + (uint16_t)(header[4] << 8)); } -static uint8_t* get_real_rx_frame_start(void) +static uint8_t *get_real_rx_frame_start(void) { - uint8_t* ret = sSpiRxFrameBuffer; - int i = 0; + uint8_t *ret = sSpiRxFrameBuffer; + int i = 0; for (i = 0; i < sSpiRxAlignAllowance; i++) { @@ -416,34 +423,31 @@ static uint8_t* get_real_rx_frame_start(void) } static int do_spi_xfer(int len) - { +{ int ret; - struct spi_ioc_transfer xfer[2] = - { - { // This part is the delay between C̅S̅ being - // asserted and the SPI clock starting. This - // is not supported by all Linux SPI drivers. - .tx_buf = 0, - .rx_buf = 0, - .len = 0, - .delay_usecs = (uint16_t)sSpiCsDelay, - .speed_hz = (uint32_t)sSpiSpeed, - .bits_per_word = 8, - .cs_change = false, - }, - { // This part is the actual SPI transfer. - .tx_buf = (unsigned long)sSpiTxFrameBuffer, - .rx_buf = (unsigned long)sSpiRxFrameBuffer, - .len = (uint32_t)(len + HEADER_LEN + sSpiRxAlignAllowance), - .delay_usecs = 0, - .speed_hz = (uint32_t)sSpiSpeed, - .bits_per_word = 8, - .cs_change = false, - } - }; - - + struct spi_ioc_transfer xfer[2] = {{ + // This part is the delay between C̅S̅ being + // asserted and the SPI clock starting. This + // is not supported by all Linux SPI drivers. + .tx_buf = 0, + .rx_buf = 0, + .len = 0, + .delay_usecs = (uint16_t)sSpiCsDelay, + .speed_hz = (uint32_t)sSpiSpeed, + .bits_per_word = 8, + .cs_change = false, + }, + { + // This part is the actual SPI transfer. + .tx_buf = (unsigned long)sSpiTxFrameBuffer, + .rx_buf = (unsigned long)sSpiRxFrameBuffer, + .len = (uint32_t)(len + HEADER_LEN + sSpiRxAlignAllowance), + .delay_usecs = 0, + .speed_hz = (uint32_t)sSpiSpeed, + .bits_per_word = 8, + .cs_change = false, + }}; if (sSpiCsDelay > 0) { @@ -469,36 +473,30 @@ static int do_spi_xfer(int len) return ret; } -static void debug_spi_header(const char* hint, bool force) +static void debug_spi_header(const char *hint, bool force) { if (force || (sVerbose >= LOG_DEBUG)) { - const uint8_t* spiRxFrameBuffer = get_real_rx_frame_start(); + const uint8_t *spiRxFrameBuffer = get_real_rx_frame_start(); - syslog(force ? LOG_WARNING : LOG_DEBUG, "%s-TX: H:%02X ACCEPT:%d DATA:%0d\n", - hint, - spi_header_get_flag_byte(sSpiTxFrameBuffer), - spi_header_get_accept_len(sSpiTxFrameBuffer), - spi_header_get_data_len(sSpiTxFrameBuffer) - ); + syslog(force ? LOG_WARNING : LOG_DEBUG, "%s-TX: H:%02X ACCEPT:%d DATA:%0d\n", hint, + spi_header_get_flag_byte(sSpiTxFrameBuffer), spi_header_get_accept_len(sSpiTxFrameBuffer), + spi_header_get_data_len(sSpiTxFrameBuffer)); - syslog(force ? LOG_WARNING : LOG_DEBUG, "%s-RX: H:%02X ACCEPT:%d DATA:%0d\n", - hint, - spi_header_get_flag_byte(spiRxFrameBuffer), - spi_header_get_accept_len(spiRxFrameBuffer), - spi_header_get_data_len(spiRxFrameBuffer) - ); + syslog(force ? LOG_WARNING : LOG_DEBUG, "%s-RX: H:%02X ACCEPT:%d DATA:%0d\n", hint, + spi_header_get_flag_byte(spiRxFrameBuffer), spi_header_get_accept_len(spiRxFrameBuffer), + spi_header_get_data_len(spiRxFrameBuffer)); } } static int push_pull_spi(void) { - int ret; - uint16_t spi_xfer_bytes = 0; - const uint8_t* spiRxFrameBuffer = NULL; - uint8_t slave_header; - uint16_t slave_max_rx; - int successful_exchanges = 0; + int ret; + uint16_t spi_xfer_bytes = 0; + const uint8_t *spiRxFrameBuffer = NULL; + uint8_t slave_header; + uint16_t slave_max_rx; + int successful_exchanges = 0; static uint16_t slave_data_len; @@ -512,7 +510,7 @@ static int push_pull_spi(void) { // Set the reset flag to indicate to our slave that we // are coming up from scratch. - spi_header_set_flag_byte(sSpiTxFrameBuffer, SPI_HEADER_RESET_FLAG|SPI_HEADER_PATTERN_VALUE); + spi_header_set_flag_byte(sSpiTxFrameBuffer, SPI_HEADER_RESET_FLAG | SPI_HEADER_PATTERN_VALUE); } else { @@ -575,9 +573,8 @@ static int push_pull_spi(void) // Print out a helpful error message for // a common error. - if ( (sSpiCsDelay != 0) - && (errno == EINVAL) - ) { + if ((sSpiCsDelay != 0) && (errno == EINVAL)) + { syslog(LOG_ERR, "SPI ioctl failed with EINVAL. Try adding `--spi-cs-delay=0` to command line arguments."); } goto bail; @@ -592,82 +589,68 @@ static int push_pull_spi(void) if ((slave_header == 0xFF) || (slave_header == 0x00)) { - if ( (slave_header == spiRxFrameBuffer[1]) - && (slave_header == spiRxFrameBuffer[2]) - && (slave_header == spiRxFrameBuffer[3]) - && (slave_header == spiRxFrameBuffer[4]) - ) { + if ((slave_header == spiRxFrameBuffer[1]) && (slave_header == spiRxFrameBuffer[2]) && + (slave_header == spiRxFrameBuffer[3]) && (slave_header == spiRxFrameBuffer[4])) + { // Device is off or in a bad state. // In some cases may be induced by flow control. - syslog(slave_data_len == 0 ? LOG_DEBUG : LOG_WARNING, "Slave did not respond to frame. (Header was all 0x%02X)", slave_header); + syslog(slave_data_len == 0 ? LOG_DEBUG : LOG_WARNING, + "Slave did not respond to frame. (Header was all 0x%02X)", slave_header); sSpiUnresponsiveFrameCount++; } else { // Header is full of garbage - syslog( - LOG_WARNING, - "Garbage in header : %02X %02X %02X %02X %02X", - spiRxFrameBuffer[0], - spiRxFrameBuffer[1], - spiRxFrameBuffer[2], - spiRxFrameBuffer[3], - spiRxFrameBuffer[4] - ); + syslog(LOG_WARNING, "Garbage in header : %02X %02X %02X %02X %02X", spiRxFrameBuffer[0], + spiRxFrameBuffer[1], spiRxFrameBuffer[2], spiRxFrameBuffer[3], spiRxFrameBuffer[4]); sSpiGarbageFrameCount++; if (sVerbose < LOG_DEBUG) { - log_debug_buffer("SPI-TX", sSpiTxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, true); - log_debug_buffer("SPI-RX", sSpiRxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, true); + log_debug_buffer("SPI-TX", sSpiTxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, + true); + log_debug_buffer("SPI-RX", sSpiRxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, + true); } } sSpiTxRefusedCount++; goto bail; } - slave_max_rx = spi_header_get_accept_len(spiRxFrameBuffer); + slave_max_rx = spi_header_get_accept_len(spiRxFrameBuffer); slave_data_len = spi_header_get_data_len(spiRxFrameBuffer); - if ( ((slave_header & SPI_HEADER_PATTERN_MASK) != SPI_HEADER_PATTERN_VALUE) - || (slave_max_rx > MAX_FRAME_SIZE) - || (slave_data_len > MAX_FRAME_SIZE) - ) + if (((slave_header & SPI_HEADER_PATTERN_MASK) != SPI_HEADER_PATTERN_VALUE) || (slave_max_rx > MAX_FRAME_SIZE) || + (slave_data_len > MAX_FRAME_SIZE)) { sSpiGarbageFrameCount++; sSpiTxRefusedCount++; slave_data_len = 0; - syslog( - LOG_WARNING, - "Garbage in header : %02X %02X %02X %02X %02X", - spiRxFrameBuffer[0], - spiRxFrameBuffer[1], - spiRxFrameBuffer[2], - spiRxFrameBuffer[3], - spiRxFrameBuffer[4] - ); + syslog(LOG_WARNING, "Garbage in header : %02X %02X %02X %02X %02X", spiRxFrameBuffer[0], spiRxFrameBuffer[1], + spiRxFrameBuffer[2], spiRxFrameBuffer[3], spiRxFrameBuffer[4]); if (sVerbose < LOG_DEBUG) { - log_debug_buffer("SPI-TX", sSpiTxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, true); - log_debug_buffer("SPI-RX", sSpiRxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, true); + log_debug_buffer("SPI-TX", sSpiTxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, + true); + log_debug_buffer("SPI-RX", sSpiRxFrameBuffer, (int)spi_xfer_bytes + HEADER_LEN + sSpiRxAlignAllowance, + true); } goto bail; } sSpiValidFrameCount++; - if ( (slave_header & SPI_HEADER_RESET_FLAG) == SPI_HEADER_RESET_FLAG) + if ((slave_header & SPI_HEADER_RESET_FLAG) == SPI_HEADER_RESET_FLAG) { sSlaveResetCount++; syslog(LOG_NOTICE, "Slave did reset (%llu resets so far)", (unsigned long long)sSlaveResetCount); sSlaveDidReset = true; - sDumpStats = true; + sDumpStats = true; } // Handle received packet, if any. - if ( (sSpiRxPayloadSize == 0) - && (slave_data_len != 0) - && (slave_data_len <= spi_header_get_accept_len(sSpiTxFrameBuffer)) - ) { + if ((sSpiRxPayloadSize == 0) && (slave_data_len != 0) && + (slave_data_len <= spi_header_get_accept_len(sSpiTxFrameBuffer))) + { // We have received a packet. Set sSpiRxPayloadSize so that // the packet will eventually get queued up by push_hdlc(). sSpiRxPayloadSize = slave_data_len; @@ -678,16 +661,15 @@ static int push_pull_spi(void) } // Handle transmitted packet, if any. - if ( sSpiTxIsReady - && (sSpiTxPayloadSize == spi_header_get_data_len(sSpiTxFrameBuffer)) - ) { + if (sSpiTxIsReady && (sSpiTxPayloadSize == spi_header_get_data_len(sSpiTxFrameBuffer))) + { if (spi_header_get_data_len(sSpiTxFrameBuffer) <= slave_max_rx) { // Our outbound packet has been successfully transmitted. Clear // sSpiTxPayloadSize and sSpiTxIsReady so that pull_hdlc() can // pull another packet for us to send. - sSpiTxIsReady = false; - sSpiTxPayloadSize = 0; + sSpiTxIsReady = false; + sSpiTxPayloadSize = 0; sSpiTxRefusedCount = 0; successful_exchanges++; } @@ -719,12 +701,12 @@ static bool check_and_clear_interrupt(void) { if (sIntGpioValueFd >= 0) { - char value[5] = ""; + char value[5] = ""; ssize_t len; lseek(sIntGpioValueFd, 0, SEEK_SET); - len = read(sIntGpioValueFd, value, sizeof(value)-1); + len = read(sIntGpioValueFd, value, sizeof(value) - 1); if (len < 0) { @@ -742,12 +724,12 @@ static bool check_and_clear_interrupt(void) /* ------------------------------------------------------------------------- */ /* MARK: HDLC Transfer Functions */ -#define HDLC_BYTE_FLAG 0x7E -#define HDLC_BYTE_ESC 0x7D -#define HDLC_BYTE_XON 0x11 -#define HDLC_BYTE_XOFF 0x13 -#define HDLC_BYTE_SPECIAL 0xF8 -#define HDLC_ESCAPE_XFORM 0x20 +#define HDLC_BYTE_FLAG 0x7E +#define HDLC_BYTE_ESC 0x7D +#define HDLC_BYTE_XON 0x11 +#define HDLC_BYTE_XOFF 0x13 +#define HDLC_BYTE_SPECIAL 0xF8 +#define HDLC_ESCAPE_XFORM 0x20 static uint16_t hdlc_crc16(uint16_t aFcs, uint8_t aByte) { @@ -755,41 +737,26 @@ static uint16_t hdlc_crc16(uint16_t aFcs, uint8_t aByte) // CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT // width=16 poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 check=0x2189 name="KERMIT" // http://reveng.sourceforge.net/crc-catalogue/16.htm#crc.cat.kermit - static const uint16_t sFcsTable[256] = - { - 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, - 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, - 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, - 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, - 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, - 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, - 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, - 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, - 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, - 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, - 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, - 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, - 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, - 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, - 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, - 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, - 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, - 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, - 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, - 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, - 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, - 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, - 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, - 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, - 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, - 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, - 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, - 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, - 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, - 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, - 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, - 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 - }; + static const uint16_t sFcsTable[256] = { + 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, + 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52, + 0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, + 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, + 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, + 0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e, + 0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f, + 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, + 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, + 0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, + 0x4e64, 0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948, + 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, + 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, + 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 0xc60c, 0xd785, 0xe51e, 0xf497, + 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 0xd68d, 0xc704, + 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, + 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, + 0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, + 0x3de3, 0x2c6a, 0x1ef1, 0x0f78}; return (aFcs >> 8) ^ sFcsTable[(aFcs ^ aByte) & 0xff]; #else // CRC-16/CCITT-FALSE, same CRC as 802.15.4 @@ -806,7 +773,7 @@ static uint16_t hdlc_crc16(uint16_t aFcs, uint8_t aByte) static bool hdlc_byte_needs_escape(uint8_t byte) { - switch(byte) + switch (byte) { case HDLC_BYTE_SPECIAL: case HDLC_BYTE_ESC: @@ -822,9 +789,9 @@ static bool hdlc_byte_needs_escape(uint8_t byte) static int push_hdlc(void) { - int ret = 0; - const uint8_t* spiRxFrameBuffer = get_real_rx_frame_start(); - static uint8_t escaped_frame_buffer[MAX_FRAME_SIZE*2]; + int ret = 0; + const uint8_t * spiRxFrameBuffer = get_real_rx_frame_start(); + static uint8_t escaped_frame_buffer[MAX_FRAME_SIZE * 2]; static uint16_t unescaped_frame_len; static uint16_t escaped_frame_len; static uint16_t escaped_frame_sent; @@ -836,7 +803,7 @@ static int push_hdlc(void) // Indicate an MCU reset. memcpy(escaped_frame_buffer, kHdlcResetSignal, sizeof(kHdlcResetSignal)); escaped_frame_len = sizeof(kHdlcResetSignal); - sSlaveDidReset = false; + sSlaveDidReset = false; // Set this to zero, since this isn't a real frame. unescaped_frame_len = 0; @@ -844,7 +811,7 @@ static int push_hdlc(void) else if (sSpiRxPayloadSize != 0) { // Escape the frame. - uint8_t c; + uint8_t c; uint16_t fcs = kHdlcCrcResetValue; uint16_t i; @@ -852,7 +819,7 @@ static int push_hdlc(void) for (i = 0; i < sSpiRxPayloadSize; i++) { - c = spiRxFrameBuffer[i + HEADER_LEN]; + c = spiRxFrameBuffer[i + HEADER_LEN]; fcs = hdlc_crc16(fcs, c); if (hdlc_byte_needs_escape(c)) { @@ -890,9 +857,8 @@ static int push_hdlc(void) } escaped_frame_buffer[escaped_frame_len++] = HDLC_BYTE_FLAG; - escaped_frame_sent = 0; - sSpiRxPayloadSize = 0; - + escaped_frame_sent = 0; + sSpiRxPayloadSize = 0; } else { @@ -901,11 +867,7 @@ static int push_hdlc(void) } } - ret = (int)write( - sHdlcOutputFd, - escaped_frame_buffer + escaped_frame_sent, - escaped_frame_len - escaped_frame_sent - ); + ret = (int)write(sHdlcOutputFd, escaped_frame_buffer + escaped_frame_sent, escaped_frame_len - escaped_frame_sent); if (ret < 0) { @@ -941,9 +903,9 @@ bail: static int pull_hdlc(void) { - int ret = 0; + int ret = 0; static uint16_t fcs; - static bool unescape_next_byte = false; + static bool unescape_next_byte = false; if (!sSpiTxIsReady) { @@ -954,27 +916,25 @@ static int pull_hdlc(void) { syslog(LOG_WARNING, "HDLC frame was too big"); unescape_next_byte = false; - sSpiTxPayloadSize = 0; - fcs = kHdlcCrcResetValue; - + sSpiTxPayloadSize = 0; + fcs = kHdlcCrcResetValue; } else if (byte == HDLC_BYTE_FLAG) { if (sSpiTxPayloadSize <= 2) { unescape_next_byte = false; - sSpiTxPayloadSize = 0; - fcs = kHdlcCrcResetValue; + sSpiTxPayloadSize = 0; + fcs = kHdlcCrcResetValue; continue; - } else if (fcs != kHdlcCrcCheckValue) { syslog(LOG_WARNING, "HDLC frame with bad CRC (LEN:%d, FCS:0x%04X)", sSpiTxPayloadSize, fcs); sHdlcRxBadCrcCount++; unescape_next_byte = false; - sSpiTxPayloadSize = 0; - fcs = kHdlcCrcResetValue; + sSpiTxPayloadSize = 0; + fcs = kHdlcCrcResetValue; continue; } @@ -990,29 +950,26 @@ static int pull_hdlc(void) // Clean up for the next frame unescape_next_byte = false; - fcs = kHdlcCrcResetValue; + fcs = kHdlcCrcResetValue; break; - } else if (byte == HDLC_BYTE_ESC) { unescape_next_byte = true; continue; - } else if (hdlc_byte_needs_escape(byte)) { // Skip all other control codes. continue; - } else if (unescape_next_byte) { - byte = byte ^ HDLC_ESCAPE_XFORM; + byte = byte ^ HDLC_ESCAPE_XFORM; unescape_next_byte = false; } - fcs = hdlc_crc16(fcs, byte); + fcs = hdlc_crc16(fcs, byte); sSpiTxFrameBuffer[HEADER_LEN + sSpiTxPayloadSize++] = byte; } } @@ -1030,20 +987,17 @@ static int pull_hdlc(void) } } - return ret < 0 - ? ret - : 0; + return ret < 0 ? ret : 0; } - /* ------------------------------------------------------------------------- */ /* MARK: Raw Transfer Functions */ static int push_raw(void) { - int ret = 0; - const uint8_t* spiRxFrameBuffer = get_real_rx_frame_start(); - static uint8_t raw_frame_buffer[MAX_FRAME_SIZE]; + int ret = 0; + const uint8_t * spiRxFrameBuffer = get_real_rx_frame_start(); + static uint8_t raw_frame_buffer[MAX_FRAME_SIZE]; static uint16_t raw_frame_len; static uint16_t raw_frame_sent; @@ -1062,8 +1016,8 @@ static int push_raw(void) // Read the frame into raw_frame_buffer assert(sSpiRxPayloadSize <= sizeof(raw_frame_buffer)); memcpy(raw_frame_buffer, &spiRxFrameBuffer[HEADER_LEN], sSpiRxPayloadSize); - raw_frame_len = sSpiRxPayloadSize; - raw_frame_sent = 0; + raw_frame_len = sSpiRxPayloadSize; + raw_frame_sent = 0; sSpiRxPayloadSize = 0; } else @@ -1073,11 +1027,7 @@ static int push_raw(void) } } - ret = (int)write( - sHdlcOutputFd, - raw_frame_buffer + raw_frame_sent, - raw_frame_len - raw_frame_sent - ); + ret = (int)write(sHdlcOutputFd, raw_frame_buffer + raw_frame_sent, raw_frame_len - raw_frame_sent); if (ret < 0) { @@ -1130,12 +1080,11 @@ static int pull_raw(void) perror("pull_raw:read"); syslog(LOG_ERR, "pull_raw:read: errno=%d (%s)", errno, strerror(errno)); } - } else if (ret > 0) { sSpiTxPayloadSize = (uint16_t)ret; - sSpiTxIsReady = true; + sSpiTxIsReady = true; // Increment counters for statistics sHdlcRxFrameCount++; @@ -1143,12 +1092,9 @@ static int pull_raw(void) } } - return ret < 0 - ? ret - : 0; + return ret < 0 ? ret : 0; } - /* ------------------------------------------------------------------------- */ /* MARK: Setup Functions */ @@ -1156,9 +1102,7 @@ static bool update_spi_mode(int x) { sSpiMode = (uint8_t)x; - if ( (sSpiDevFd >= 0) - && (ioctl(sSpiDevFd, SPI_IOC_WR_MODE, &sSpiMode) < 0) - ) + if ((sSpiDevFd >= 0) && (ioctl(sSpiDevFd, SPI_IOC_WR_MODE, &sSpiMode) < 0)) { perror("ioctl(SPI_IOC_WR_MODE)"); return false; @@ -1171,9 +1115,7 @@ static bool update_spi_speed(int x) { sSpiSpeed = x; - if ( (sSpiDevFd >= 0) - && (ioctl(sSpiDevFd, SPI_IOC_WR_MAX_SPEED_HZ, &sSpiSpeed) < 0) - ) + if ((sSpiDevFd >= 0) && (ioctl(sSpiDevFd, SPI_IOC_WR_MAX_SPEED_HZ, &sSpiSpeed) < 0)) { perror("ioctl(SPI_IOC_WR_MAX_SPEED_HZ)"); return false; @@ -1182,12 +1124,11 @@ static bool update_spi_speed(int x) return true; } - -static bool setup_spi_dev(const char* path) +static bool setup_spi_dev(const char *path) { - int fd = -1; + int fd = -1; const uint8_t spi_word_bits = 8; - int ret; + int ret; sSpiDevPath = path; fd = open(path, O_RDWR); @@ -1229,7 +1170,7 @@ static bool setup_spi_dev(const char* path) } sSpiDevFd = fd; - fd = -1; + fd = -1; bail: if (fd >= 0) @@ -1239,12 +1180,12 @@ bail: return sSpiDevFd >= 0; } -static bool setup_res_gpio(const char* path) +static bool setup_res_gpio(const char *path) { - int setup_fd = -1; - char* dir_path = NULL; - char* value_path = NULL; - int len; + int setup_fd = -1; + char *dir_path = NULL; + char *value_path = NULL; + int len; sResGpioDevPath = path; @@ -1301,7 +1242,7 @@ static void trigger_reset(void) { if (sResGpioValueFd >= 0) { - char str[] = { '0' + GPIO_RES_ASSERT_STATE, '\n' }; + char str[] = {'0' + GPIO_RES_ASSERT_STATE, '\n'}; lseek(sResGpioValueFd, 0, SEEK_SET); if (write(sResGpioValueFd, str, sizeof(str)) == -1) @@ -1324,13 +1265,13 @@ static void trigger_reset(void) } } -static bool setup_int_gpio(const char* path) +static bool setup_int_gpio(const char *path) { - char* edge_path = NULL; - char* dir_path = NULL; - char* value_path = NULL; + char * edge_path = NULL; + char * dir_path = NULL; + char * value_path = NULL; ssize_t len; - int setup_fd = -1; + int setup_fd = -1; sIntGpioValueFd = -1; @@ -1418,7 +1359,6 @@ bail: return sIntGpioValueFd >= 0; } - /* ------------------------------------------------------------------------- */ /* MARK: Help */ @@ -1431,98 +1371,97 @@ static void print_version(void) static void print_help(void) { print_version(); - const char* help = - "\n" - "Syntax:\n" - "\n" - " spi-hdlc [options] \n" - "\n" - "Options:\n" - "\n" - " --stdio ...................... Use `stdin` and `stdout` for HDLC input and\n" - " output. Useful when directly started by the\n" - " program that will be using it.\n" + const char *help = "\n" + "Syntax:\n" + "\n" + " spi-hdlc [options] \n" + "\n" + "Options:\n" + "\n" + " --stdio ...................... Use `stdin` and `stdout` for HDLC input and\n" + " output. Useful when directly started by the\n" + " program that will be using it.\n" #if HAVE_OPENPTY - " --pty ........................ Create a pseudoterminal for HDLC input and\n" - " output. The path of the newly-created PTY\n" - " will be written to `stdout`, followed by a\n" - " newline.\n" + " --pty ........................ Create a pseudoterminal for HDLC input and\n" + " output. The path of the newly-created PTY\n" + " will be written to `stdout`, followed by a\n" + " newline.\n" #endif // HAVE_OPENPTY - " --raw ........................ Do not encode/decode packets using HDLC.\n" - " Instead, write whole, raw frames to the\n" - " specified input and output FDs. This is useful\n" - " for emulating a serial port, or when datagram-\n" - " based sockets are supplied for stdin and\n" - " stdout` (when used with --stdio).\n" - " --mtu=[MTU] .................. Specify the MTU. Currently only used in raw mode.\n" - " Default and maximum value is 2043.\n" - " -i/--gpio-int[=gpio-path] .... Specify a path to the Linux sysfs-exported\n" - " GPIO directory for the `I̅N̅T̅` pin. If not\n" - " specified, `spi-hdlc` will fall back to\n" - " polling, which is inefficient.\n" - " -r/--gpio-reset[=gpio-path] .. Specify a path to the Linux sysfs-exported\n" - " GPIO directory for the `R̅E̅S̅` pin.\n" - " --spi-mode[=mode] ............ Specify the SPI mode to use (0-3).\n" - " --spi-speed[=hertz] .......... Specify the SPI speed in hertz.\n" - " --spi-cs-delay[=usec] ........ Specify the delay after C̅S̅ assertion, in µsec\n" - " --spi-align-allowance[=n] .... Specify the maximum number of 0xFF bytes to\n" - " clip from start of MISO frame. Max value is 16.\n" - " --spi-small-packet=[n] ....... Specify the smallest packet we can receive\n" - " in a single transaction(larger packets will\n" - " require two transactions). Default value is 32.\n" - " -v/--verbose ................. Increase debug verbosity. (Repeatable)\n" - " -h/-?/--help ................. Print out usage information and exit.\n" - "\n"; + " --raw ........................ Do not encode/decode packets using HDLC.\n" + " Instead, write whole, raw frames to the\n" + " specified input and output FDs. This is useful\n" + " for emulating a serial port, or when datagram-\n" + " based sockets are supplied for stdin and\n" + " stdout` (when used with --stdio).\n" + " --mtu=[MTU] .................. Specify the MTU. Currently only used in raw mode.\n" + " Default and maximum value is 2043.\n" + " -i/--gpio-int[=gpio-path] .... Specify a path to the Linux sysfs-exported\n" + " GPIO directory for the `I̅N̅T̅` pin. If not\n" + " specified, `spi-hdlc` will fall back to\n" + " polling, which is inefficient.\n" + " -r/--gpio-reset[=gpio-path] .. Specify a path to the Linux sysfs-exported\n" + " GPIO directory for the `R̅E̅S̅` pin.\n" + " --spi-mode[=mode] ............ Specify the SPI mode to use (0-3).\n" + " --spi-speed[=hertz] .......... Specify the SPI speed in hertz.\n" + " --spi-cs-delay[=usec] ........ Specify the delay after C̅S̅ assertion, in µsec\n" + " --spi-align-allowance[=n] .... Specify the maximum number of 0xFF bytes to\n" + " clip from start of MISO frame. Max value is 16.\n" + " --spi-small-packet=[n] ....... Specify the smallest packet we can receive\n" + " in a single transaction(larger packets will\n" + " require two transactions). Default value is 32.\n" + " -v/--verbose ................. Increase debug verbosity. (Repeatable)\n" + " -h/-?/--help ................. Print out usage information and exit.\n" + "\n"; printf("%s", help); } - /* ------------------------------------------------------------------------- */ /* MARK: Main Loop */ int main(int argc, char *argv[]) { - int i = 0; - char prog[32]; - static fd_set read_set; - static fd_set write_set; - static fd_set error_set; + int i = 0; + char prog[32]; + static fd_set read_set; + static fd_set write_set; + static fd_set error_set; struct timeval timeout; - int max_fd = -1; - bool did_print_rate_limit_log = false; + int max_fd = -1; + bool did_print_rate_limit_log = false; #if AUTO_PRINT_BACKTRACE struct sigaction sigact; #endif // if AUTO_PRINT_BACKTRACE - enum { - ARG_SPI_MODE = 1001, - ARG_SPI_SPEED = 1002, - ARG_VERBOSE = 1003, - ARG_SPI_CS_DELAY = 1004, + enum + { + ARG_SPI_MODE = 1001, + ARG_SPI_SPEED = 1002, + ARG_VERBOSE = 1003, + ARG_SPI_CS_DELAY = 1004, ARG_SPI_ALIGN_ALLOWANCE = 1005, - ARG_RAW = 1006, - ARG_MTU = 1007, - ARG_SPI_SMALL_PACKET = 1008, + ARG_RAW = 1006, + ARG_MTU = 1007, + ARG_SPI_SMALL_PACKET = 1008, }; static struct option options[] = { - { "stdio", no_argument, &sMode, MODE_STDIO }, - { "pty", no_argument, &sMode, MODE_PTY }, - { "gpio-int", required_argument, NULL, 'i' }, - { "gpio-res", required_argument, NULL, 'r' }, - { "verbose", optional_argument, NULL, ARG_VERBOSE }, - { "version", no_argument, NULL, 'V' }, - { "raw", no_argument, NULL, ARG_RAW }, - { "mtu", required_argument, NULL, ARG_MTU }, - { "help", no_argument, NULL, 'h' }, - { "spi-mode", required_argument, NULL, ARG_SPI_MODE }, - { "spi-speed", required_argument, NULL, ARG_SPI_SPEED }, - { "spi-cs-delay",required_argument,NULL, ARG_SPI_CS_DELAY }, - { "spi-align-allowance", required_argument, NULL, ARG_SPI_ALIGN_ALLOWANCE }, - { "spi-small-packet", required_argument, NULL, ARG_SPI_SMALL_PACKET }, - { NULL, 0, NULL, 0 }, + {"stdio", no_argument, &sMode, MODE_STDIO}, + {"pty", no_argument, &sMode, MODE_PTY}, + {"gpio-int", required_argument, NULL, 'i'}, + {"gpio-res", required_argument, NULL, 'r'}, + {"verbose", optional_argument, NULL, ARG_VERBOSE}, + {"version", no_argument, NULL, 'V'}, + {"raw", no_argument, NULL, ARG_RAW}, + {"mtu", required_argument, NULL, ARG_MTU}, + {"help", no_argument, NULL, 'h'}, + {"spi-mode", required_argument, NULL, ARG_SPI_MODE}, + {"spi-speed", required_argument, NULL, ARG_SPI_SPEED}, + {"spi-cs-delay", required_argument, NULL, ARG_SPI_CS_DELAY}, + {"spi-align-allowance", required_argument, NULL, ARG_SPI_ALIGN_ALLOWANCE}, + {"spi-small-packet", required_argument, NULL, ARG_SPI_SMALL_PACKET}, + {NULL, 0, NULL, 0}, }; strncpy(prog, argv[0], sizeof(prog) - 1); @@ -1534,11 +1473,10 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - // ======================================================================== // INITIALIZATION - sPreviousHandlerForSIGINT = signal(SIGINT, &signal_SIGINT); + sPreviousHandlerForSIGINT = signal(SIGINT, &signal_SIGINT); sPreviousHandlerForSIGTERM = signal(SIGTERM, &signal_SIGTERM); signal(SIGHUP, &signal_SIGHUP); signal(SIGUSR1, &signal_dumpstats); @@ -1546,7 +1484,7 @@ int main(int argc, char *argv[]) #if AUTO_PRINT_BACKTRACE sigact.sa_sigaction = &signal_critical; - sigact.sa_flags = SA_RESTART | SA_SIGINFO | SA_NOCLDWAIT; + sigact.sa_flags = SA_RESTART | SA_SIGINFO | SA_NOCLDWAIT; sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL); sigaction(SIGBUS, &sigact, (struct sigaction *)NULL); @@ -1581,7 +1519,7 @@ int main(int argc, char *argv[]) break; case ARG_SPI_ALIGN_ALLOWANCE: - errno = 0; + errno = 0; sSpiRxAlignAllowance = atoi(optarg); if (errno != 0 || (sSpiRxAlignAllowance < 0)) @@ -1592,7 +1530,8 @@ int main(int argc, char *argv[]) if (sSpiRxAlignAllowance > SPI_RX_ALIGN_ALLOWANCE_MAX) { - syslog(LOG_WARNING, "Reducing SPI RX Align Allowance from %s to %d", optarg, SPI_RX_ALIGN_ALLOWANCE_MAX); + syslog(LOG_WARNING, "Reducing SPI RX Align Allowance from %s to %d", optarg, + SPI_RX_ALIGN_ALLOWANCE_MAX); sSpiRxAlignAllowance = SPI_RX_ALIGN_ALLOWANCE_MAX; } @@ -1618,7 +1557,8 @@ int main(int argc, char *argv[]) sSpiSmallPacketSize = atoi(optarg); if (sSpiSmallPacketSize > MAX_FRAME_SIZE - HEADER_LEN) { - syslog(LOG_WARNING, "Reducing SPI small-packet size from %s to %d", optarg, MAX_FRAME_SIZE - HEADER_LEN); + syslog(LOG_WARNING, "Reducing SPI small-packet size from %s to %d", optarg, + MAX_FRAME_SIZE - HEADER_LEN); sSpiSmallPacketSize = MAX_FRAME_SIZE - HEADER_LEN; } if (sSpiSmallPacketSize < 0) @@ -1648,7 +1588,8 @@ int main(int argc, char *argv[]) sMTU = atoi(optarg); if (sMTU > MAX_FRAME_SIZE - HEADER_LEN) { - syslog(LOG_ERR, "Specified MTU of %d is too large, maximum is %d bytes.", sMTU, MAX_FRAME_SIZE - HEADER_LEN); + syslog(LOG_ERR, "Specified MTU of %d is too large, maximum is %d bytes.", sMTU, + MAX_FRAME_SIZE - HEADER_LEN); exit(EXIT_FAILURE); } if (sMTU < 1) @@ -1698,7 +1639,7 @@ int main(int argc, char *argv[]) } } - syslog(LOG_NOTICE,"spi-hdlc-adapter " SPI_HDLC_VERSION " (" __TIME__ " " __DATE__ ")\n"); + syslog(LOG_NOTICE, "spi-hdlc-adapter " SPI_HDLC_VERSION " (" __TIME__ " " __DATE__ ")\n"); argc -= optind; argv += optind; @@ -1732,18 +1673,17 @@ int main(int argc, char *argv[]) if (sMode == MODE_STDIO) { - sHdlcInputFd = dup(STDIN_FILENO); + sHdlcInputFd = dup(STDIN_FILENO); sHdlcOutputFd = dup(STDOUT_FILENO); close(STDIN_FILENO); close(STDOUT_FILENO); - } else if (sMode == MODE_PTY) { #if HAVE_OPENPTY static int pty_slave_fd = -1; - char pty_name[1024]; + char pty_name[1024]; sRet = openpty(&sHdlcInputFd, &pty_slave_fd, pty_name, NULL, NULL); if (sRet != 0) @@ -1765,7 +1705,6 @@ int main(int argc, char *argv[]) goto bail; #endif // else HAVE_OPENPTY - } else { @@ -1823,7 +1762,6 @@ int main(int argc, char *argv[]) if (!sSpiTxIsReady) { FD_SET(sHdlcInputFd, &read_set); - } else { @@ -1838,7 +1776,6 @@ int main(int argc, char *argv[]) // for that to clear out before we can do anything // else. FD_SET(sHdlcOutputFd, &write_set); - } else if (sIntGpioValueFd >= 0) { @@ -1858,7 +1795,6 @@ int main(int argc, char *argv[]) // set. FD_SET(sIntGpioValueFd, &error_set); } - } else if (timeout_ms > SPI_POLL_PERIOD_MSEC) { @@ -1899,10 +1835,8 @@ int main(int argc, char *argv[]) timeout_ms = min_timeout; } - if ( sSpiTxIsReady - && !did_print_rate_limit_log - && (sSpiTxRefusedCount > 1) - ) { + if (sSpiTxIsReady && !did_print_rate_limit_log && (sSpiTxRefusedCount > 1)) + { // To avoid printing out this message over and over, // we only print it out once the refused count is at // two or higher when we actually have something to @@ -1939,7 +1873,7 @@ int main(int argc, char *argv[]) } // Calculate the timeout value. - timeout.tv_sec = timeout_ms / MSEC_PER_SEC; + timeout.tv_sec = timeout_ms / MSEC_PER_SEC; timeout.tv_usec = (timeout_ms % MSEC_PER_SEC) * USEC_PER_MSEC; // Wait for something to happen. @@ -1987,9 +1921,8 @@ int main(int argc, char *argv[]) // Service the SPI port if we can receive // a packet or we have a packet to be sent. - if ( (sSpiRxPayloadSize == 0) - && (sSpiTxIsReady || check_and_clear_interrupt()) - ) { + if ((sSpiRxPayloadSize == 0) && (sSpiTxIsReady || check_and_clear_interrupt())) + { // We guard this with the above check because we don't // want to overwrite any previously received (but not // yet pushed out) frames. @@ -2000,7 +1933,6 @@ int main(int argc, char *argv[]) } } - // ======================================================================== // SHUTDOWN