Allwize Library
SerialSniffer.h
1 /*
2 
3 Serial Sniffer
4 
5 Copyright (C) 2018 by Xose PĂ©rez <xose dot perez at gmail dot com>
6 
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 */
21 
22 #pragma once
23 
24 #include <stdlib.h>
25 #include <Stream.h>
26 
27 class SerialSniffer : public Stream {
28 
29  public:
30 
31  SerialSniffer(Stream & stream, Stream & debug) : _stream(stream), _debug(debug) {}
32 
33  virtual size_t write(uint8_t ch) {
34  _debug.print("w ");
35  _debug.print(ch, HEX);
36  _debug.println();
37  delay(1);
38  return _stream.write(ch);
39  }
40 
41  virtual int read() {
42  int ch = _stream.read();
43  if (ch >= 0) {
44  _debug.print("r ");
45  _debug.print(ch, HEX);
46  _debug.println();
47  delay(1);
48  }
49  return ch;
50  }
51 
52  virtual int available() {
53  return _stream.available();
54  }
55 
56  virtual int peek() {
57  return _stream.peek();
58  }
59 
60  virtual void flush() {
61  _stream.flush();
62  }
63 
64  private:
65 
66  Stream & _stream;
67  Stream & _debug;
68 
69 
70 };