tinyproto
TinyProtocolFd.h
Go to the documentation of this file.
1 /*
2  Copyright 2019-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 
36 #pragma once
37 
38 #include "TinyPacket.h"
39 #include "proto/fd/tiny_fd.h"
40 
41 #ifdef ARDUINO
42 #include <HardwareSerial.h>
43 #else
44 #include <string.h>
45 #include <stdlib.h>
46 #endif
47 
48 namespace tinyproto
49 {
50 
63 class IFd
64 {
65 public:
66  friend class FdD;
72  IFd(void *buffer, int bufferSize)
73  : m_buffer((uint8_t *)buffer)
74  , m_bufferSize(bufferSize)
75  {
76  }
77 
78  virtual ~IFd() = default;
79 
87  void begin();
88 
92  void end();
93 
102  int write(const char *buf, int size);
103 
112  int write(const IPacket &pkt);
113 
122  int run_rx(const void *data, int len);
123 
130  int run_rx(read_block_cb_t read_func);
131 
137  int run_tx(write_block_cb_t write_func);
138 
146  int run_tx(void *data, int max_size);
147 
153  void disableCrc();
154 
160  void enableCrc(hdlc_crc_t crc);
161 
169  bool enableCheckSum();
170 
178  bool enableCrc16();
179 
188  bool enableCrc32();
189 
194  void setReceiveCallback(void (*on_receive)(void *userData, uint8_t addr, IPacket &pkt) = nullptr)
195  {
196  m_onReceive = on_receive;
197  };
198 
203  void setSendCallback(void (*on_send)(void *userData, uint8_t addr, IPacket &pkt) = nullptr)
204  {
205  m_onSend = on_send;
206  };
207 
212  void setConnectEventCallback(void (*on_connect)(void *userData, uint8_t addr, bool connected) = nullptr)
213  {
214  m_onConnectEvent = on_connect;
215  }
216 
223  void setWindowSize(uint8_t window)
224  {
225  m_window = window;
226  }
227 
232  void setSendTimeout(uint16_t timeout)
233  {
234  m_sendTimeout = timeout;
235  }
236 
241  void setUserData(void *userData)
242  {
243  m_userData = userData;
244  }
245 
250  {
251  return m_handle;
252  }
253 
257  int getStatus()
258  {
259  return tiny_fd_get_status(m_handle);
260  }
261 
262 protected:
270  virtual void onReceive(uint8_t addr, uint8_t *pdata, int size)
271  {
272  IPacket pkt((char *)pdata, size);
273  pkt.m_len = size;
274  if ( m_onReceive )
275  m_onReceive(m_userData, addr, pkt);
276  }
277 
285  virtual void onSend(uint8_t addr, const uint8_t *pdata, int size)
286  {
287  IPacket pkt((char *)pdata, size);
288  pkt.m_len = size;
289  if ( m_onSend )
290  m_onSend(m_userData, addr, pkt);
291  }
292 
299  virtual void onConnectEvent(uint8_t addr, bool connected)
300  {
301  if ( m_onConnectEvent )
302  m_onConnectEvent(m_userData, addr, connected);
303  }
304 
305 private:
307  tiny_fd_handle_t m_handle = nullptr;
308 
310  uint8_t *m_buffer = nullptr;
311 
312  hdlc_crc_t m_crc = HDLC_CRC_DEFAULT;
313 
315  int m_bufferSize = 0;
316 
318  uint16_t m_sendTimeout = 0;
319 
321  uint8_t m_window = 3;
322 
324  void (*m_onReceive)(void *userData, uint8_t addr, IPacket &pkt) = nullptr;
325 
327  void (*m_onSend)(void *userData, uint8_t addr, IPacket &pkt) = nullptr;
328 
330  void (*m_onConnectEvent)(void *userData, uint8_t addr, bool connected) = nullptr;
331 
333  void *m_userData = nullptr;
334 
336  static void onReceiveInternal(void *handle, uint8_t addr, uint8_t *pdata, int size);
337 
339  static void onSendInternal(void *handle, uint8_t addr, const uint8_t *pdata, int size);
340 
342  static void onConnectEventInternal(void *handle, uint8_t addr, bool connected);
343 };
344 
348 template <int S> class Fd: public IFd
349 {
350 public:
351  Fd()
352  : IFd(m_data, S)
353  {
354  }
355 
356 private:
357  TINY_ALIGNED_STRUCT uint8_t m_data[S]{};
358 };
359 
365 class FdD: public IFd
366 {
367 public:
372  explicit FdD(int size)
373  : IFd(reinterpret_cast<uint8_t *>(new uintptr_t[(size + TINY_ALIGN_STRUCT_VALUE - 1) / TINY_ALIGN_STRUCT_VALUE]), size)
374  {
375  }
376 
377  ~FdD()
378  {
379  delete[] m_buffer;
380  }
381 
382 private:
383 };
384 
389 } // namespace tinyproto
void begin()
Initializes protocol internal variables.
Definition: TinyProtocolFd.cpp:59
Definition: tiny_fd_int.h:114
int run_tx(write_block_cb_t write_func)
Attempts to send out data via write_func function.
Definition: TinyProtocolFd.cpp:116
void setConnectEventCallback(void(*on_connect)(void *userData, uint8_t addr, bool connected)=nullptr)
Sets connect/disconnect callback.
Definition: TinyProtocolFd.h:212
This is Tiny protocol implementation for microcontrollers.
Describes packet entity and provides API methods to manipulate the packet.
Definition: TinyPacket.h:56
#define TINY_ALIGNED_STRUCT
This macro is used internally for aligning the structures.
Definition: tiny_types.h:113
virtual void onReceive(uint8_t addr, uint8_t *pdata, int size)
Method called by hdlc protocol upon receiving new frame.
Definition: TinyProtocolFd.h:270
This is class, which allocates buffers statically.
Definition: TinyProtocolFd.h:348
bool enableCrc16()
Enables CRC 16-bit field in the protocol.
Definition: TinyProtocolFd.cpp:154
void end()
Resets protocol state.
Definition: TinyProtocolFd.cpp:78
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
The function reads data from communication channel.
Definition: tiny_types.h:185
tiny_fd_handle_t getHandle()
Returns low-level handle for full duplex protocol.
Definition: TinyProtocolFd.h:249
FdD(int size)
Creates instance of Full duplex protocol with dynamically allocated buffer.
Definition: TinyProtocolFd.h:372
void disableCrc()
Disable CRC field in the protocol.
Definition: TinyProtocolFd.cpp:138
IFd class incapsulates Full Duplex Protocol functionality.
Definition: TinyProtocolFd.h:63
void setWindowSize(uint8_t window)
Sets desired window size.
Definition: TinyProtocolFd.h:223
void setReceiveCallback(void(*on_receive)(void *userData, uint8_t addr, IPacket &pkt)=nullptr)
Sets receive callback for incoming messages.
Definition: TinyProtocolFd.h:194
void setSendCallback(void(*on_send)(void *userData, uint8_t addr, IPacket &pkt)=nullptr)
Sets send callback for outgoing messages.
Definition: TinyProtocolFd.h:203
IFd(void *buffer, int bufferSize)
Initializes IFd object.
Definition: TinyProtocolFd.h:72
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
This is special class for Full duplex protocol, which allocates buffers dynamically.
Definition: TinyProtocolFd.h:365
void setUserData(void *userData)
Sets user data to pass to callbacks.
Definition: TinyProtocolFd.h:241
virtual void onSend(uint8_t addr, const uint8_t *pdata, int size)
Method called by hdlc protocol upon sending next frame.
Definition: TinyProtocolFd.h:285
Definition: TinySerial.cpp:22
This is Tiny Full-Duplex protocol implementation for microcontrollers.
int write(const char *buf, int size)
Sends data block over communication channel.
Definition: TinyProtocolFd.cpp:85
int getStatus()
Returns status of the protocol.
Definition: TinyProtocolFd.h:257
void setSendTimeout(uint16_t timeout)
Sets send timeout in milliseconds.
Definition: TinyProtocolFd.h:232
bool enableCrc32()
Enables CRC 32-bit field in the protocol.
Definition: TinyProtocolFd.cpp:160
virtual void onConnectEvent(uint8_t addr, bool connected)
Method called by fd protocol when connect/disconnect event takes place.
Definition: TinyProtocolFd.h:299
bool enableCheckSum()
Enables CRC 8-bit field in the protocol.
Definition: TinyProtocolFd.cpp:148
#define TINY_ALIGN_STRUCT_VALUE
This macro is used internally for aligning the structures.
Definition: tiny_types.h:108
void enableCrc(hdlc_crc_t crc)
Enables CRC by specified bit-size.
Definition: TinyProtocolFd.cpp:143
int run_rx(const void *data, int len)
Processes incoming rx data, specified by a user.
Definition: TinyProtocolFd.cpp:95
int tiny_fd_get_status(tiny_fd_handle_t handle)
Returns status of the connection.
Definition: tiny_fd.c:914