blob: ea0cae0a48d93f4c69d92b461419f8aedac5dee3 [file] [log] [blame]
Daniel Petti059be422013-12-14 19:47:42 -08001#ifndef FCR971_INPUT_UART_RECEIVER_H_
2#define FRC971_INPUT_UART_RECEIVER_H_
3
Brian Silverman1662a0e2013-12-19 17:50:01 -08004#include <stdint.h>
Daniel Petti059be422013-12-14 19:47:42 -08005
Brian Silverman53f29182013-12-21 15:16:27 -08006#include <memory>
7
Daniel Petti059be422013-12-14 19:47:42 -08008#define DATA_STRUCT_NAME DataStruct
Brian Silverman1662a0e2013-12-19 17:50:01 -08009#include "cape/data_struct.h"
Daniel Petti059be422013-12-14 19:47:42 -080010#undef DATA_STRUCT_NAME
11
12namespace bbb {
Daniel Petti059be422013-12-14 19:47:42 -080013
Brian Silvermanf6b68842013-12-20 12:34:58 -080014class UartReader {
Brian Silverman1662a0e2013-12-19 17:50:01 -080015 public:
Brian Silvermanf6b68842013-12-20 12:34:58 -080016 UartReader(int32_t baud_rate);
17 ~UartReader();
Brian Silverman1662a0e2013-12-19 17:50:01 -080018
19 // Returns true if it finds one or false if it gets an I/O error first.
20 // packet must be aligned to 4 bytes.
21 bool GetPacket(DataStruct *packet);
22
23 private:
24 // Reads bytes until there are 4 zeros and then fills up buf_.
25 // Returns true if it finds one or false if it gets an I/O error first or the
26 // packet is invalid in some way.
27 bool FindPacket();
28
Brian Silverman53f29182013-12-21 15:16:27 -080029 // Processes a packet currently in buf_ and leaves the result in
30 // unstuffed_data_.
31 // Returns true if it succeeds or false if there was something wrong with the
32 // data.
33 bool ProcessPacket();
34
35 typedef char __attribute__((aligned(4))) AlignedChar;
Brian Silverman1662a0e2013-12-19 17:50:01 -080036
37 const int32_t baud_rate_;
38 AlignedChar *const buf_;
Brian Silverman53f29182013-12-21 15:16:27 -080039 AlignedChar *const unstuffed_data_;
Brian Silverman1662a0e2013-12-19 17:50:01 -080040 const int fd_;
Brian Silverman53f29182013-12-21 15:16:27 -080041
42 // How many bytes of the packet we've read in (or -1 if we don't know where
43 // the packet is).
44 int packet_bytes_ = -1;
Brian Silverman1662a0e2013-12-19 17:50:01 -080045};
46
47} // namespace bbb
Daniel Petti059be422013-12-14 19:47:42 -080048
49#endif