tinyproto
TinyProtocol.h
Go to the documentation of this file.
1 /*
2  Copyright 2016-2025 (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 
37 #pragma once
38 
39 #include "TinyPacket.h"
40 #include "TinyLightProtocol.h"
41 #include "TinyProtocolHdlc.h"
42 #include "TinyProtocolFd.h"
43 #include "link/TinyLinkLayer.h"
44 #include "link/TinySerialFdLink.h"
45 #include "link/TinySerialHdlcLink.h"
46 
47 #include "hal/tiny_types.h"
48 
49 #include <stdint.h>
50 #include <limits.h>
51 
52 #if CONFIG_TINYHAL_THREAD_SUPPORT == 1
53 #include <thread>
54 #endif
55 
56 namespace tinyproto
57 {
58 
59 class Proto
60 {
61 public:
62  explicit Proto(bool multithread = false);
63 
64  ~Proto();
65 
66  void setLink(ILinkLayer &link);
67 
68  ILinkLayer &getLink();
69 
70  bool begin();
71 
72  bool begin(int poolBuffers);
73 
74  bool send(const IPacket &message, uint32_t timeout);
75 
76  IPacket *read(uint32_t timeout);
77 
78  void end();
79 
80  void release(IPacket *message);
81 
82  void addRxPool(IPacket &message);
83 
84  void setRxCallback(void (*onRx)(Proto &, IPacket &));
85 
86  void setTxCallback(void (*onTx)(Proto &, IPacket &));
87 
88 #if CONFIG_TINYHAL_THREAD_SUPPORT == 1
89  void setTxDelay( uint32_t delay );
90 
91  int getLostRxFrames();
92 #endif
93 
94 private:
95  ILinkLayer *m_link = nullptr;
96  void (*m_onRx)(Proto &, IPacket &) = nullptr;
97  void (*m_onTx)(Proto &, IPacket &) = nullptr;
98  bool m_multithread = false;
99  bool m_terminate = true;
100  IPacket *m_pool = nullptr;
101  IPacket *m_queue = nullptr;
102  IPacket *m_last = nullptr;
103 #if CONFIG_TINYHAL_THREAD_SUPPORT == 1
104  std::thread *m_sendThread = nullptr;
105  std::thread *m_readThread = nullptr;
106  uint32_t m_txDelay = 0;
107  int m_lostRxFrames = 0;
108 #endif
109 
110  tiny_events_t m_events{};
111 
112  tiny_mutex_t m_mutex{};
113 
114  void onRead(uint8_t addr, uint8_t *buf, int len);
115 
116  void onSend(uint8_t addr, const uint8_t *buf, int len);
117 
118  static void onReadCb(void *udata, uint8_t addr, uint8_t *buf, int len);
119 
120  static void onSendCb(void *udata, uint8_t addr, const uint8_t *buf, int len);
121 
122 #if CONFIG_TINYHAL_THREAD_SUPPORT == 1
123  void runTx();
124 
125  void runRx();
126 #endif
127 
128 // void printCount(const char *, IPacket *queue);
129 };
130 
132 
133 #if defined(ARDUINO)
134 
135 class SerialFdProto: public Proto
136 {
137 public:
138  explicit SerialFdProto(HardwareSerial &port);
139 
140  ArduinoSerialFdLink &getLink();
141 
142 private:
143  ArduinoSerialFdLink m_layer;
144 };
145 
146 class SerialHdlcProto: public Proto
147 {
148 public:
149  explicit SerialHdlcProto(HardwareSerial &port);
150 
151  ArduinoSerialHdlcLink &getLink();
152 
153 private:
154  ArduinoSerialHdlcLink m_layer;
155 };
156 
157 #else
158 
159 class SerialFdProto: public Proto
160 {
161 public:
162  explicit SerialFdProto(char *dev, bool multithread = false);
163 
164  SerialFdLink &getLink();
165 
166 private:
167  SerialFdLink m_layer;
168 };
169 
170 class SerialHdlcProto: public Proto
171 {
172 public:
173  explicit SerialHdlcProto(char *dev, bool multithread = false);
174 
175  SerialHdlcLink &getLink();
176 
177 private:
178  SerialHdlcLink m_layer;
179 };
180 
181 #endif
182 
183 } // namespace tinyproto
Definition: TinyProtocol.h:170
This is Tiny protocol implementation for microcontrollers.
This is Tiny protocol implementation for microcontrollers.
Describes packet entity and provides API methods to manipulate the packet.
Definition: TinyPacket.h:56
This is Tiny protocol implementation for microcontrollers.
Definition: TinyProtocol.h:159
This is basic class for C++ Link Layer objects.
Definition: TinyLinkLayer.h:43
This is Tiny light protocol implementation for microcontrollers.
This is Tiny HAL implementation for microcontrollers.
Definition: TinySerial.cpp:22
Events group type used by Tiny Protocol implementation.
Definition: cpp_hal.h:60
Definition: TinyProtocol.h:59