Tiny protocol  0.9.0
Tiny communication protocol for microcontrollers
TinyProtocolFd.h
Go to the documentation of this file.
1 /*
2  Copyright 2019 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  Protocol Library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Protocol Library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
27 #pragma once
28 
29 #include "TinyPacket.h"
30 #include "proto/fd/tiny_fd.h"
31 
32 #ifdef ARDUINO
33 # include <HardwareSerial.h>
34 #else
35 # include <string.h>
36 #endif
37 
38 namespace Tiny {
39 
53 class IProtoFd
54 {
55 public:
56  friend class ProtoFdD;
62  IProtoFd(void * buffer,
63  int bufferSize)
64  : m_buffer( (uint8_t *)buffer )
65  , m_bufferSize( bufferSize )
66  {
67  }
68 
69  virtual ~IProtoFd() = default;
70 
79  void begin (write_block_cb_t writecb,
80  read_block_cb_t readcb);
81 
82 #ifdef ARDUINO
83 
88  inline void beginToSerial()
89  {
90  begin([](void *p, const void *b, int s)->int { return Serial.write((const uint8_t *)b, s); },
91  [](void *p, void *b, int s)->int { return Serial.readBytes((uint8_t *)b, s); });
92  }
93 
94 #ifdef HAVE_HWSERIAL1
95 
100  inline void beginToSerial1()
101  {
102  begin([](void *p, const void *b, int s)->int { return Serial1.write((const uint8_t *)b, s); },
103  [](void *p, void *b, int s)->int { return Serial1.readBytes((uint8_t *)b, s); });
104  }
105 #endif
106 
107 #ifdef HAVE_HWSERIAL2
108 
113  inline void beginToSerial2()
114  {
115  begin([](void *p, const void *b, int s)->int { return Serial2.write((const uint8_t *)b, s); },
116  [](void *p, void *b, int s)->int { return Serial2.readBytes((uint8_t *)b, s); });
117  }
118 #endif
119 
120 #ifdef HAVE_HWSERIAL3
121 
126  inline void beginToSerial3()
127  {
128  begin([](void *p, const void *b, int s)->int { return Serial3.write((const uint8_t *)b, s); },
129  [](void *p, void *b, int s)->int { return Serial3.readBytes((uint8_t *)b, s); });
130  }
131 #endif
132 
133 #endif
134 
138  void end();
139 
148  int write(char* buf, int size);
149 
158  int write(IPacket &pkt);
159 
168  int run_rx(uint16_t timeout = 0);
169 
174  int run_tx(uint16_t timeout = 0);
175 
181  void disableCrc();
182 
190  bool enableCheckSum();
191 
199  bool enableCrc16();
200 
209  bool enableCrc32();
210 
215  void setReceiveCallback(void (*on_receive)(IPacket &pkt) = nullptr) { m_onReceive = on_receive; };
216 
223  void setWindowSize(uint8_t window) { m_window = window; }
224 
229  void setSendTimeout(uint16_t timeout) { m_sendTimeout = timeout; }
230 
231 protected:
238  virtual void onReceive(uint8_t *pdata, int size)
239  {
240  IPacket pkt((char *)pdata, size);
241  pkt.m_len = size;
242  if ( m_onReceive ) m_onReceive( pkt );
243  }
244 
245 private:
247  tiny_fd_handle_t m_handle = nullptr;
248 
250  uint8_t *m_buffer = nullptr;
251 
253 
255  int m_bufferSize = 0;
256 
258  uint16_t m_sendTimeout = 0;
259 
261  uint8_t m_window = 3;
262 
264  void (*m_onReceive)(IPacket &pkt) = nullptr;
265 
267  static void onReceiveInternal(void *handle, uint16_t uid, uint8_t *pdata, int size);
268 
269 };
270 
274 template <int S>
275 class ProtoFd: public IProtoFd
276 {
277 public:
278  ProtoFd(): IProtoFd( m_data, S ) {}
279 private:
280  uint8_t m_data[S];
281 };
282 
288 class ProtoFdD: public IProtoFd
289 {
290 public:
295  ProtoFdD( int size ): IProtoFd( new uint8_t[size], size ) { }
296  ~ProtoFdD() { delete m_buffer; }
297 private:
298 };
299 
304 } // Tiny namespace
305 
int run_tx(uint16_t timeout=0)
hdlc_crc_t
Definition: tiny_hdlc.h:33
Tiny protocol Arduino API.
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:35
Definition: TinyProtocolFd.h:275
Definition: TinyLightProtocol.h:39
bool enableCheckSum()
struct tiny_fd_data_t * tiny_fd_handle_t
Definition: tiny_fd.h:50
void setReceiveCallback(void(*on_receive)(IPacket &pkt)=nullptr)
Definition: TinyProtocolFd.h:215
virtual void onReceive(uint8_t *pdata, int size)
Definition: TinyProtocolFd.h:238
bool enableCrc32()
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:139
IProtoFd(void *buffer, int bufferSize)
Definition: TinyProtocolFd.h:62
void beginToSerial3()
Definition: TinyProtocolFd.h:126
void setWindowSize(uint8_t window)
Definition: TinyProtocolFd.h:223
bool enableCrc16()
Definition: TinyPacket.h:46
void beginToSerial2()
Definition: TinyProtocolFd.h:113
Definition: TinyProtocolFd.h:53
int run_rx(uint16_t timeout=0)
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:128
void beginToSerial()
Definition: TinyProtocolFd.h:88
Definition: TinyProtocolFd.h:288
Tiny Protocol Full Duplex API.
void beginToSerial1()
Definition: TinyProtocolFd.h:100
ProtoFdD(int size)
Definition: TinyProtocolFd.h:295
void setSendTimeout(uint16_t timeout)
Definition: TinyProtocolFd.h:229
void begin(write_block_cb_t writecb, read_block_cb_t readcb)
int write(char *buf, int size)