Extract RX sanity checker into separate function
[elmcan.git] / module / elmcan.c
index 0eb4fe866398cb3f8ac01c65c3f45e5eb9562bf7..239970d04fd32fbc044536cdabc75771cf58126d 100644 (file)
@@ -194,7 +194,11 @@ static void elm327_send(struct elmcan *elm, const void *buf, size_t len)
 }
 
 
-/* Take the ELM327 out of almost any state and back into command mode. */
+/* Take the ELM327 out of almost any state and back into command mode.
+ * We send ELM327_MAGIC_CHAR which will either abort any running
+ * operation, or be echoed back to us in case we're already in command
+ * mode.
+ */
 static void elm327_kick_into_cmd_mode(struct elmcan *elm)
 {
        if (elm->state != ELM_GETMAGICCHAR && elm->state != ELM_GETPROMPT) {
@@ -945,6 +949,23 @@ static void put_elm(struct elmcan *elm)
 }
 
 
+static bool elmcan_is_valid_rx_char(char c)
+{
+       return (accept_flaky_uart
+               || isdigit(c)
+               || isupper(c)
+               || ELM327_MAGIC_CHAR == c
+               || ELM327_READY_CHAR == c
+               || '<' == c
+               || 'a' == c
+               || 'b' == c
+               || 'v' == c
+               || '.' == c
+               || '?' == c
+               || '\r' == c
+               || ' ' == c);
+}
+
 /* Handle incoming ELM327 ASCII data.
  * This will not be re-entered while running, but other ldisc
  * functions may be called in parallel.
@@ -981,19 +1002,7 @@ static void elmcan_ldisc_rx(struct tty_struct *tty,
                        /* Check for stray characters on the UART line.
                         * Likely caused by bad hardware.
                         */
-                       if (!accept_flaky_uart
-                           && !isdigit(*cp)
-                           && !isupper(*cp)
-                           && ELM327_MAGIC_CHAR != *cp
-                           && ELM327_READY_CHAR != *cp
-                           && '<' != *cp
-                           && 'a' != *cp
-                           && 'b' != *cp
-                           && 'v' != *cp
-                           && '.' != *cp
-                           && '?' != *cp
-                           && '\r' != *cp
-                           && ' ' != *cp) {
+                       if (!elmcan_is_valid_rx_char(*cp)) {
                                netdev_err(elm->dev,
                                           "Received illegal character %02x.\n",
                                           *cp);
@@ -1254,12 +1263,6 @@ static struct tty_ldisc_ops elmcan_ldisc = {
 
 
 
-
-
- /************************************************************************
-  *            Module init/exit                                *
-  ************************************************************************/
-
 static int __init elmcan_init(void)
 {
        int status;