tinyproto
TinyHdlcLinkLayer.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 "TinyLinkLayer.h"
32 #include "proto/hdlc/low_level/hdlc.h"
33 
34 #include "hal/tiny_types.h"
35 
36 #include <stdint.h>
37 
38 namespace tinyproto
39 {
40 
42 {
43 public:
44  IHdlcLinkLayer(void *buffer, int size);
45 
46  ~IHdlcLinkLayer();
47 
48  bool begin(on_frame_read_cb_t onReadCb, on_frame_send_cb_t onSendCb, void *udata) override;
49 
50  void end() override;
51 
52  bool put(void *buf, int size, uint32_t timeout) override;
53 
54  void flushTx() override;
55 
56  hdlc_crc_t getCrc()
57  {
58  return m_crc;
59  }
60 
61  void setCrc( hdlc_crc_t crc )
62  {
63  m_crc = crc;
64  }
65 
66  void setBuffer(void *buffer, int size)
67  {
68  m_buffer = reinterpret_cast<uint8_t *>(buffer);
69  m_bufferSize = size;
70  }
71 
72 protected:
73 
74  int parseData(const uint8_t *data, int size);
75 
76  int getData(uint8_t *data, int size);
77 
78 private:
79  hdlc_ll_handle_t m_handle = nullptr;
80  void *m_udata = nullptr;
81  on_frame_read_cb_t m_onReadCb = nullptr;
82  on_frame_send_cb_t m_onSendCb = nullptr;
83  tiny_mutex_t m_sendMutex{};
84  tiny_events_t m_events{};
85  void *m_tempBuffer = nullptr;
86 
87  uint8_t *m_buffer = nullptr;
88  int m_bufferSize = 0;
89  hdlc_crc_t m_crc = HDLC_CRC_8;
90  bool m_flushFlag = false;
91 
92  static void onSend(void *udata, const uint8_t *data, int len);
93  static void onRead(void *udata, uint8_t *data, int len);
94 };
95 
96 } // namespace tinyproto
Definition: TinyHdlcLinkLayer.h:41
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
Structure describes configuration of lowest HDLC level Initialize this structure by 0 before passing ...
Definition: hdlc_int.h:65
This is basic class for C++ Link Layer objects.
Definition: TinyLinkLayer.h:43
This is Tiny HAL implementation for microcontrollers.
void flushTx() override
Flush tx operation if possible.
Definition: TinyHdlcLinkLayer.cpp:111
bool put(void *buf, int size, uint32_t timeout) override
Puts new data for sending over the link layer.
Definition: TinyHdlcLinkLayer.cpp:82
void end() override
Stops link layer protocol.
Definition: TinyHdlcLinkLayer.cpp:75
bool begin(on_frame_read_cb_t onReadCb, on_frame_send_cb_t onSendCb, void *udata) override
The method initializes the link layer protocol, and connects custom callbacks to link layer...
Definition: TinyHdlcLinkLayer.cpp:55
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
Events group type used by Tiny Protocol implementation.
Definition: cpp_hal.h:60