tinyproto
TinyProtocolHdlc.h
Go to the documentation of this file.
1 /*
2  Copyright 2020,2022 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  GNU General Public License Usage
7 
8  Protocol Library is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Protocol Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
20 
21  Commercial License Usage
22 
23  Licensees holding valid commercial Tiny Protocol licenses may use this file in
24  accordance with the commercial license agreement provided in accordance with
25  the terms contained in a written agreement between you and Alexey Dynda.
26  For further information contact via email on github account.
27 */
28 
36 #pragma once
37 
38 #include "TinyPacket.h"
39 #include "proto/hdlc/high_level/hdlc.h"
40 
41 #ifdef ARDUINO
42 #include <HardwareSerial.h>
43 #else
44 #include <string.h>
45 #endif
46 
47 namespace tinyproto
48 {
49 
62 class Hdlc
63 {
64 public:
70  Hdlc(void *buffer, int bufferSize)
71  : m_buffer(buffer)
72  , m_bufferSize(bufferSize)
73  {
74  }
75 
76  virtual ~Hdlc() = default;
77 
86  void begin(write_block_cb_t writecb, read_block_cb_t readcb);
87 
94  void begin();
95 
99  void end();
100 
109  int write(const char *buf, int size);
110 
119  int write(const IPacket &pkt);
120 
127  int run_rx(const void *data, int len);
128 
135  int run_tx(void *data, int max_len);
136 
142  void disableCrc();
143 
149  void enableCrc(hdlc_crc_t crc);
150 
158  bool enableCheckSum();
159 
167  bool enableCrc16();
168 
177  bool enableCrc32();
178 
183  void setReceiveCallback(void (*on_receive)(IPacket &pkt) = nullptr)
184  {
185  m_onReceive = on_receive;
186  };
187 
192  void setSendCallback(void (*on_send)(IPacket &pkt) = nullptr)
193  {
194  m_onSend = on_send;
195  };
196 
197 protected:
204  virtual void onReceive(uint8_t *pdata, int size)
205  {
206  IPacket pkt((char *)pdata, size);
207  pkt.m_len = size;
208  if ( m_onReceive )
209  m_onReceive(pkt);
210  }
211 
218  virtual void onSend(const uint8_t *pdata, int size)
219  {
220  IPacket pkt((char *)pdata, size);
221  pkt.m_len = size;
222  if ( m_onSend )
223  m_onSend(pkt);
224  }
225 
226 private:
228  hdlc_handle_t m_handle = nullptr;
229 
230  hdlc_struct_t m_data{};
231 
232  void *m_buffer = nullptr;
233 
234  int m_bufferSize = 0;
235 
236  hdlc_crc_t m_crc = HDLC_CRC_DEFAULT;
237 
239  void (*m_onReceive)(IPacket &pkt) = nullptr;
240 
242  void (*m_onSend)(IPacket &pkt) = nullptr;
243 
245  static int onReceiveInternal(void *handle, void *pdata, int size);
246 
248  static int onSendInternal(void *handle, const void *pdata, int size);
249 };
250 
255 } // namespace tinyproto
int run_rx(const void *data, int len)
Processes incoming rx data, specified by a user.
Definition: TinyProtocolHdlc.cpp:92
void setSendCallback(void(*on_send)(IPacket &pkt)=nullptr)
Sets send callback for outgoing messages.
Definition: TinyProtocolHdlc.h:192
This is Tiny protocol implementation for microcontrollers.
void enableCrc(hdlc_crc_t crc)
Enables CRC by specified bit-size.
Definition: TinyProtocolHdlc.cpp:107
Describes packet entity and provides API methods to manipulate the packet.
Definition: TinyPacket.h:56
void disableCrc()
Disable CRC field in the protocol.
Definition: TinyProtocolHdlc.cpp:102
void begin()
Initializes protocol internal variables.
Definition: TinyProtocolHdlc.cpp:70
bool enableCheckSum()
Enables CRC 8-bit field in the protocol.
Definition: TinyProtocolHdlc.cpp:112
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
The function reads data from communication channel.
Definition: tiny_types.h:185
virtual void onReceive(uint8_t *pdata, int size)
Method called by hdlc protocol upon receiving new frame.
Definition: TinyProtocolHdlc.h:204
int write(const char *buf, int size)
Sends data block over communication channel.
Definition: TinyProtocolHdlc.cpp:82
Hdlc(void *buffer, int bufferSize)
Initializes Hdlc object.
Definition: TinyProtocolHdlc.h:70
bool enableCrc32()
Enables CRC 32-bit field in the protocol.
Definition: TinyProtocolHdlc.cpp:124
Structure describes configuration of lowest HDLC level Initialize this structure by 0 before passing ...
Definition: hdlc.h:56
virtual void onSend(const uint8_t *pdata, int size)
Method called by hdlc protocol upon sending next frame.
Definition: TinyProtocolHdlc.h:218
void end()
Resets protocol state.
Definition: TinyProtocolHdlc.cpp:75
bool enableCrc16()
Enables CRC 16-bit field in the protocol.
Definition: TinyProtocolHdlc.cpp:118
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
The function writes data to communication channel port.
Definition: tiny_types.h:174
int run_tx(void *data, int max_len)
Generates data for tx channel.
Definition: TinyProtocolHdlc.cpp:97
Definition: TinySerial.cpp:22
Hdlc class incapsulates hdlc Protocol functionality.
Definition: TinyProtocolHdlc.h:62
void setReceiveCallback(void(*on_receive)(IPacket &pkt)=nullptr)
Sets receive callback for incoming messages.
Definition: TinyProtocolHdlc.h:183