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 
64 class Hdlc
65 {
66 public:
72  Hdlc(void *buffer, int bufferSize)
73  : m_buffer(buffer)
74  , m_bufferSize(bufferSize)
75  {
76  }
77 
78  virtual ~Hdlc() = default;
79 
88  void begin(write_block_cb_t writecb, read_block_cb_t readcb);
89 
96  void begin();
97 
101  void end();
102 
111  int write(const char *buf, int size);
112 
121  int write(const IPacket &pkt);
122 
129  int run_rx(const void *data, int len);
130 
137  int run_tx(void *data, int max_len);
138 
144  void disableCrc();
145 
151  void enableCrc(hdlc_crc_t crc);
152 
160  bool enableCheckSum();
161 
169  bool enableCrc16();
170 
179  bool enableCrc32();
180 
185  void setReceiveCallback(void (*on_receive)(IPacket &pkt) = nullptr)
186  {
187  m_onReceive = on_receive;
188  };
189 
194  void setSendCallback(void (*on_send)(IPacket &pkt) = nullptr)
195  {
196  m_onSend = on_send;
197  };
198 
199 protected:
206  virtual void onReceive(uint8_t *pdata, int size)
207  {
208  IPacket pkt((char *)pdata, size);
209  pkt.m_len = size;
210  if ( m_onReceive )
211  m_onReceive(pkt);
212  }
213 
220  virtual void onSend(const uint8_t *pdata, int size)
221  {
222  IPacket pkt((char *)pdata, size);
223  pkt.m_len = size;
224  if ( m_onSend )
225  m_onSend(pkt);
226  }
227 
228 private:
230  hdlc_handle_t m_handle = nullptr;
231 
232  hdlc_struct_t m_data{};
233 
234  void *m_buffer = nullptr;
235 
236  int m_bufferSize = 0;
237 
238  hdlc_crc_t m_crc = HDLC_CRC_DEFAULT;
239 
241  void (*m_onReceive)(IPacket &pkt) = nullptr;
242 
244  void (*m_onSend)(IPacket &pkt) = nullptr;
245 
247  static int onReceiveInternal(void *handle, void *pdata, int size);
248 
250  static int onSendInternal(void *handle, const void *pdata, int size);
251 };
252 
257 } // 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:194
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:206
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:72
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:60
virtual void onSend(const uint8_t *pdata, int size)
Method called by hdlc protocol upon sending next frame.
Definition: TinyProtocolHdlc.h:220
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 encapsulates HDLC low-level framing functionality.
Definition: TinyProtocolHdlc.h:64
void setReceiveCallback(void(*on_receive)(IPacket &pkt)=nullptr)
Sets receive callback for incoming messages.
Definition: TinyProtocolHdlc.h:185