tinyproto
TinySerialHdlcLink.h
1 /*
2  Copyright 2016-2024 (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 
29 #pragma once
30 
31 #include "TinySerialLinkLayer.h"
32 #include "TinyHdlcLinkLayer.h"
33 
34 #if defined(ARDUINO)
35 #include "proto/hdlc/low_level/hdlc_int.h"
36 #endif
37 
38 namespace tinyproto
39 {
40 
41 template <int MTU, int BUFFER_SIZE, int BLOCK> class StaticSerialHdlcLink: public ISerialLinkLayer<IHdlcLinkLayer, BLOCK>
42 {
43 public:
44  explicit StaticSerialHdlcLink(char *dev)
45  : ISerialLinkLayer<IHdlcLinkLayer, BLOCK>(dev, this->m_buffer, BUFFER_SIZE)
46  {
47  this->setMtu(MTU);
48  }
49 
50 private:
51  uint8_t m_buffer[BUFFER_SIZE] = {};
52 };
53 
54 
55 #if defined(ARDUINO)
56 
58 template <int MTU, int RX_WINDOW, int BLOCK> using ArduinoStaticSerialHdlcLinkLayer = StaticSerialHdlcLink<MTU, HDLC_BUF_SIZE_EX(MTU, HDLC_CRC_16, RX_WINDOW), BLOCK>;
59 
60 class ArduinoSerialHdlcLink: public ArduinoStaticSerialHdlcLinkLayer<32, 2, 4>
61 {
62 public:
63  explicit ArduinoSerialHdlcLink(HardwareSerial *dev)
64  : ArduinoStaticSerialHdlcLinkLayer<32, 2, 4>(reinterpret_cast<char *>(dev))
65  {
66  }
67 };
68 
69 #endif
70 
71 class SerialHdlcLink: public ISerialLinkLayer<IHdlcLinkLayer, 32>
72 {
73 public:
74  explicit SerialHdlcLink(char *dev)
75  : ISerialLinkLayer<IHdlcLinkLayer, 32>(dev, nullptr, 0)
76  {
77  }
78 
79  ~SerialHdlcLink();
80 
81  bool begin(on_frame_read_cb_t onReadCb, on_frame_send_cb_t onSendCb, void *udata) override;
82 
83  void end() override;
84 
85 private:
86  uint8_t *m_buffer = nullptr;
87 };
88 
89 } // namespace tinyproto
Template class for Serial-based communication for any of TinyProto Links.
Definition: TinySerialLinkLayer.h:43
void(* on_frame_send_cb_t)(void *udata, uint8_t address, const uint8_t *pdata, int size)
on_frame_send_cb_t is a callback function, which is called every time new frame is sent...
Definition: tiny_types.h:223
void(* on_frame_read_cb_t)(void *udata, uint8_t address, uint8_t *pdata, int size)
on_frame_read_cb_t is a callback function, which is called every time new frame is received...
Definition: tiny_types.h:213
Definition: TinySerial.cpp:22
void setMtu(int mtu)
Set protocol mtu (maximum transmission unit) payload.
Definition: TinyLinkLayer.h:126