    to the appropriate next state depending on the protocol."
   [{:keys [decode-sm ^ByteBuffer input-buffer] :as channel} a-byte]
   (.put input-buffer ^byte a-byte)
-  (swap! decode-sm assoc :state (if (= (:protocol @decode-sm) :mavlink1)
+  (reset! decode-sm (if (= (.get input-buffer 0) MAVLINK1-START-BYTE)
                               seq-id-state
-                              incompat-flags-state)
-                     :hdr-payload-size (byte-to-long a-byte))
+                              incompat-flags-state))
   nil)
 
 (defn start-state

   Both MAVlink 1.0 and MAVlink 2.0 are supported."
  [{:keys [statistics decode-sm last-values] :as channel} a-byte]
  {:pre [(instance? Byte a-byte)
         (map? @decode-sm)
         (fn? (:state @decode-sm))]}
  (swap! statistics assoc :bytes-received (inc (:bytes-received @statistics)))
  (when-let [message ((:state @decode-sm) channel a-byte)]
    (when-let [message-key (:message-id message)]
      (when-let [msg-ref (message-key last-values)]
        (dosync (ref-set msg-ref message))))

   Ignore every other byte. Go to Payload size state when a start byte is seen;
   don't save the byte regardless. When a start byte is seen, completely reset
   the state."
  [{:keys [decode-sm decode-message-id ^ByteBuffer input-buffer] :as channel} a-byte]
  (when (or (= a-byte MAVLINK1-START-BYTE)
            (= a-byte MAVLINK2-START-BYTE))
    (reset! decode-sm payload-size-state)
    (reset! decode-message-id nil)
    (.clear input-buffer)
    (.put input-buffer ^byte a-byte)
    nil))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
