Tiny protocol  0.9.3
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 
134 #ifdef HAVE_SERIALUSB
135 
140  inline void beginToSerialUSB()
141  {
142  begin([](void *p, const void *b, int s)->int { return SerialUSB.write((const char *)b, s); },
143  [](void *p, void *b, int s)->int { return SerialUSB.readBytes((char *)b, s); });
144  }
145 #endif
146 
147 #endif
148 
152  void end();
153 
162  int write(char* buf, int size);
163 
172  int write(IPacket &pkt);
173 
182  int run_rx(uint16_t timeout = 0);
183 
188  int run_tx(uint16_t timeout = 0);
189 
195  void disableCrc();
196 
202  void enableCrc(hdlc_crc_t crc);
203 
211  bool enableCheckSum();
212 
220  bool enableCrc16();
221 
230  bool enableCrc32();
231 
236  void setReceiveCallback(void (*on_receive)(IPacket &pkt) = nullptr) { m_onReceive = on_receive; };
237 
242  void setSendCallback(void (*on_send)(IPacket &pkt) = nullptr) { m_onSend = on_send; };
243 
250  void setWindowSize(uint8_t window) { m_window = window; }
251 
256  void setSendTimeout(uint16_t timeout) { m_sendTimeout = timeout; }
257 
258 protected:
265  virtual void onReceive(uint8_t *pdata, int size)
266  {
267  IPacket pkt((char *)pdata, size);
268  pkt.m_len = size;
269  if ( m_onReceive ) m_onReceive( pkt );
270  }
271 
278  virtual void onSend(uint8_t *pdata, int size)
279  {
280  IPacket pkt((char *)pdata, size);
281  pkt.m_len = size;
282  if ( m_onSend ) m_onSend( pkt );
283  }
284 
285 private:
287  tiny_fd_handle_t m_handle = nullptr;
288 
290  uint8_t *m_buffer = nullptr;
291 
293 
295  int m_bufferSize = 0;
296 
298  uint16_t m_sendTimeout = 0;
299 
301  uint8_t m_window = 3;
302 
304  void (*m_onReceive)(IPacket &pkt) = nullptr;
305 
307  void (*m_onSend)(IPacket &pkt) = nullptr;
308 
310  static void onReceiveInternal(void *handle, uint16_t uid, uint8_t *pdata, int size);
311 
313  static void onSendInternal(void *handle, uint16_t uid, uint8_t *pdata, int size);
314 };
315 
319 template <int S>
320 class ProtoFd: public IProtoFd
321 {
322 public:
323  ProtoFd(): IProtoFd( m_data, S ) {}
324 private:
325  uint8_t m_data[S];
326 };
327 
333 class ProtoFdD: public IProtoFd
334 {
335 public:
340  ProtoFdD( int size ): IProtoFd( new uint8_t[size], size ) { }
341  ~ProtoFdD() { delete[] m_buffer; }
342 private:
343 };
344 
349 } // Tiny namespace
350 
int run_tx(uint16_t timeout=0)
hdlc_crc_t
Definition: tiny_hdlc.h:51
Tiny protocol Arduino API.
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:53
virtual void onSend(uint8_t *pdata, int size)
Definition: TinyProtocolFd.h:278
Definition: TinyProtocolFd.h:320
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:236
virtual void onReceive(uint8_t *pdata, int size)
Definition: TinyProtocolFd.h:265
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:250
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:333
Tiny Protocol Full Duplex API.
void beginToSerial1()
Definition: TinyProtocolFd.h:100
ProtoFdD(int size)
Definition: TinyProtocolFd.h:340
void setSendCallback(void(*on_send)(IPacket &pkt)=nullptr)
Definition: TinyProtocolFd.h:242
void setSendTimeout(uint16_t timeout)
Definition: TinyProtocolFd.h:256
void begin(write_block_cb_t writecb, read_block_cb_t readcb)
int write(char *buf, int size)