Tiny protocol  0.11.0
Tiny communication protocol for microcontrollers
TinyProtocolHdlc.h
Go to the documentation of this file.
1 /*
2  Copyright 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/hdlc/tiny_hdlc.h"
31 
32 #ifdef ARDUINO
33 # include <HardwareSerial.h>
34 #else
35 # include <string.h>
36 #endif
37 
38 namespace Tiny {
39 
53 class ProtoHdlc
54 {
55 public:
61  ProtoHdlc(void *buffer, int bufferSize)
62  : m_buffer( buffer )
63  , m_bufferSize( bufferSize )
64  {
65  }
66 
67  virtual ~ProtoHdlc() = default;
68 
77  void begin (write_block_cb_t writecb,
78  read_block_cb_t readcb);
79 
86  void begin();
87 
88 #ifdef ARDUINO
89 
94  inline void beginToSerial()
95  {
96  begin([](void *p, const void *b, int s)->int { return Serial.write((const uint8_t *)b, s); },
97  [](void *p, void *b, int s)->int { return Serial.readBytes((uint8_t *)b, s); });
98  }
99 
100 #ifdef HAVE_HWSERIAL1
101 
106  inline void beginToSerial1()
107  {
108  begin([](void *p, const void *b, int s)->int { return Serial1.write((const uint8_t *)b, s); },
109  [](void *p, void *b, int s)->int { return Serial1.readBytes((uint8_t *)b, s); });
110  }
111 #endif
112 
113 #ifdef HAVE_HWSERIAL2
114 
119  inline void beginToSerial2()
120  {
121  begin([](void *p, const void *b, int s)->int { return Serial2.write((const uint8_t *)b, s); },
122  [](void *p, void *b, int s)->int { return Serial2.readBytes((uint8_t *)b, s); });
123  }
124 #endif
125 
126 #ifdef HAVE_HWSERIAL3
127 
132  inline void beginToSerial3()
133  {
134  begin([](void *p, const void *b, int s)->int { return Serial3.write((const uint8_t *)b, s); },
135  [](void *p, void *b, int s)->int { return Serial3.readBytes((uint8_t *)b, s); });
136  }
137 #endif
138 
139 
140 #ifdef HAVE_SERIALUSB
141 
146  inline void beginToSerialUSB()
147  {
148  begin([](void *p, const void *b, int s)->int { return SerialUSB.write((const char *)b, s); },
149  [](void *p, void *b, int s)->int { return SerialUSB.readBytes((char *)b, s); });
150  }
151 #endif
152 
153 #endif
154 
158  void end();
159 
168  int write(char* buf, int size);
169 
178  int write(IPacket &pkt);
179 
186  int run_rx(const void *data, int len);
187 
194  int run_tx(void *data, int max_len);
195 
201  void disableCrc();
202 
208  void enableCrc(hdlc_crc_t crc);
209 
217  bool enableCheckSum();
218 
226  bool enableCrc16();
227 
236  bool enableCrc32();
237 
242  void setReceiveCallback(void (*on_receive)(IPacket &pkt) = nullptr) { m_onReceive = on_receive; };
243 
248  void setSendCallback(void (*on_send)(IPacket &pkt) = nullptr) { m_onSend = on_send; };
249 
250 protected:
257  virtual void onReceive(uint8_t *pdata, int size)
258  {
259  IPacket pkt((char *)pdata, size);
260  pkt.m_len = size;
261  if ( m_onReceive ) m_onReceive( pkt );
262  }
263 
270  virtual void onSend(const uint8_t *pdata, int size)
271  {
272  IPacket pkt((char *)pdata, size);
273  pkt.m_len = size;
274  if ( m_onSend ) m_onSend( pkt );
275  }
276 
277 private:
279  hdlc_handle_t m_handle = nullptr;
280 
281  hdlc_struct_t m_data{};
282 
283  void *m_buffer = nullptr;
284 
285  int m_bufferSize = 0;
286 
288 
290  void (*m_onReceive)(IPacket &pkt) = nullptr;
291 
293  void (*m_onSend)(IPacket &pkt) = nullptr;
294 
296  static int onReceiveInternal(void *handle, void *pdata, int size);
297 
299  static int onSendInternal(void *handle, const void *pdata, int size);
300 };
301 
306 } // Tiny namespace
307 
hdlc_crc_t
Definition: tiny_hdlc.h:54
Tiny protocol Arduino API.
int run_rx(const void *data, int len)
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:56
void beginToSerial2()
Definition: TinyProtocolHdlc.h:119
void beginToSerial()
Definition: TinyProtocolHdlc.h:94
Definition: TinyLightProtocol.h:39
Definition: TinyProtocolHdlc.h:53
virtual void onSend(const uint8_t *pdata, int size)
Definition: TinyProtocolHdlc.h:270
virtual void onReceive(uint8_t *pdata, int size)
Definition: TinyProtocolHdlc.h:257
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:141
void enableCrc(hdlc_crc_t crc)
int write(char *buf, int size)
void beginToSerial3()
Definition: TinyProtocolHdlc.h:132
ProtoHdlc(void *buffer, int bufferSize)
Definition: TinyProtocolHdlc.h:61
bool enableCheckSum()
Definition: TinyPacket.h:46
Definition: tiny_hdlc.h:68
void setReceiveCallback(void(*on_receive)(IPacket &pkt)=nullptr)
Definition: TinyProtocolHdlc.h:242
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:130
int run_tx(void *data, int max_len)
void beginToSerial1()
Definition: TinyProtocolHdlc.h:106
void setSendCallback(void(*on_send)(IPacket &pkt)=nullptr)
Definition: TinyProtocolHdlc.h:248