Tiny protocol  0.11.0
Tiny communication protocol for microcontrollers
TinyProtocolFd.h
Go to the documentation of this file.
1 /*
2  Copyright 2019-2020 (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 
90  void begin();
91 
92 #ifdef ARDUINO
93 
98  inline void beginToSerial()
99  {
100  begin([](void *p, const void *b, int s)->int { return Serial.write((const uint8_t *)b, s); },
101  [](void *p, void *b, int s)->int { return Serial.readBytes((uint8_t *)b, s); });
102  }
103 
104 #ifdef HAVE_HWSERIAL1
105 
110  inline void beginToSerial1()
111  {
112  begin([](void *p, const void *b, int s)->int { return Serial1.write((const uint8_t *)b, s); },
113  [](void *p, void *b, int s)->int { return Serial1.readBytes((uint8_t *)b, s); });
114  }
115 #endif
116 
117 #ifdef HAVE_HWSERIAL2
118 
123  inline void beginToSerial2()
124  {
125  begin([](void *p, const void *b, int s)->int { return Serial2.write((const uint8_t *)b, s); },
126  [](void *p, void *b, int s)->int { return Serial2.readBytes((uint8_t *)b, s); });
127  }
128 #endif
129 
130 #ifdef HAVE_HWSERIAL3
131 
136  inline void beginToSerial3()
137  {
138  begin([](void *p, const void *b, int s)->int { return Serial3.write((const uint8_t *)b, s); },
139  [](void *p, void *b, int s)->int { return Serial3.readBytes((uint8_t *)b, s); });
140  }
141 #endif
142 
143 
144 #ifdef HAVE_SERIALUSB
145 
150  inline void beginToSerialUSB()
151  {
152  begin([](void *p, const void *b, int s)->int { return SerialUSB.write((const char *)b, s); },
153  [](void *p, void *b, int s)->int { return SerialUSB.readBytes((char *)b, s); });
154  }
155 #endif
156 
157 #endif
158 
162  void end();
163 
172  int write(char* buf, int size);
173 
182  int write(IPacket &pkt);
183 
192  int run_rx(uint16_t timeout = 0);
193 
200  int run_rx(const void *data, int len);
201 
206  int run_tx(uint16_t timeout = 0);
207 
215  int run_tx(void *data, int max_size);
216 
222  void disableCrc();
223 
229  void enableCrc(hdlc_crc_t crc);
230 
238  bool enableCheckSum();
239 
247  bool enableCrc16();
248 
257  bool enableCrc32();
258 
263  void setReceiveCallback(void (*on_receive)(IPacket &pkt) = nullptr) { m_onReceive = on_receive; };
264 
269  void setSendCallback(void (*on_send)(IPacket &pkt) = nullptr) { m_onSend = on_send; };
270 
277  void setWindowSize(uint8_t window) { m_window = window; }
278 
283  void setSendTimeout(uint16_t timeout) { m_sendTimeout = timeout; }
284 
285 protected:
292  virtual void onReceive(uint8_t *pdata, int size)
293  {
294  IPacket pkt((char *)pdata, size);
295  pkt.m_len = size;
296  if ( m_onReceive ) m_onReceive( pkt );
297  }
298 
305  virtual void onSend(uint8_t *pdata, int size)
306  {
307  IPacket pkt((char *)pdata, size);
308  pkt.m_len = size;
309  if ( m_onSend ) m_onSend( pkt );
310  }
311 
312 private:
314  tiny_fd_handle_t m_handle = nullptr;
315 
317  uint8_t *m_buffer = nullptr;
318 
320 
322  int m_bufferSize = 0;
323 
325  uint16_t m_sendTimeout = 0;
326 
328  uint8_t m_window = 3;
329 
331  void (*m_onReceive)(IPacket &pkt) = nullptr;
332 
334  void (*m_onSend)(IPacket &pkt) = nullptr;
335 
337  static void onReceiveInternal(void *handle, uint16_t uid, uint8_t *pdata, int size);
338 
340  static void onSendInternal(void *handle, uint16_t uid, uint8_t *pdata, int size);
341 };
342 
346 template <int S>
347 class ProtoFd: public IProtoFd
348 {
349 public:
350  ProtoFd(): IProtoFd( m_data, S ) {}
351 private:
352  uint8_t m_data[S];
353 };
354 
360 class ProtoFdD: public IProtoFd
361 {
362 public:
367  ProtoFdD( int size ): IProtoFd( new uint8_t[size], size ) { }
368  ~ProtoFdD() { delete[] m_buffer; }
369 private:
370 };
371 
376 } // Tiny namespace
377 
int run_tx(uint16_t timeout=0)
hdlc_crc_t
Definition: tiny_hdlc.h:54
Tiny protocol Arduino API.
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:56
virtual void onSend(uint8_t *pdata, int size)
Definition: TinyProtocolFd.h:305
Definition: TinyProtocolFd.h:347
Definition: TinyLightProtocol.h:39
bool enableCheckSum()
void enableCrc(hdlc_crc_t crc)
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:263
virtual void onReceive(uint8_t *pdata, int size)
Definition: TinyProtocolFd.h:292
bool enableCrc32()
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:141
IProtoFd(void *buffer, int bufferSize)
Definition: TinyProtocolFd.h:62
void beginToSerial3()
Definition: TinyProtocolFd.h:136
void setWindowSize(uint8_t window)
Definition: TinyProtocolFd.h:277
bool enableCrc16()
Definition: TinyPacket.h:46
void beginToSerial2()
Definition: TinyProtocolFd.h:123
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:130
void beginToSerial()
Definition: TinyProtocolFd.h:98
Definition: TinyProtocolFd.h:360
Tiny Protocol Full Duplex API.
void beginToSerial1()
Definition: TinyProtocolFd.h:110
ProtoFdD(int size)
Definition: TinyProtocolFd.h:367
void setSendCallback(void(*on_send)(IPacket &pkt)=nullptr)
Definition: TinyProtocolFd.h:269
void setSendTimeout(uint16_t timeout)
Definition: TinyProtocolFd.h:283
int write(char *buf, int size)