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 
67 class IFd
68 {
69 public:
70  friend class FdD;
76  IFd(void *buffer, int bufferSize)
77  : m_buffer((uint8_t *)buffer)
78  , m_bufferSize(bufferSize)
79  {
80  }
81 
82  virtual ~IFd() = default;
83 
91  void begin();
92 
96  void end();
97 
106  int write(const char *buf, int size);
107 
116  int write(const IPacket &pkt);
117 
126  int run_rx(const void *data, int len);
127 
134  int run_rx(read_block_cb_t read_func);
135 
141  int run_tx(write_block_cb_t write_func);
142 
150  int run_tx(void *data, int max_size);
151 
157  void disableCrc();
158 
164  void enableCrc(hdlc_crc_t crc);
165 
173  bool enableCheckSum();
174 
182  bool enableCrc16();
183 
192  bool enableCrc32();
193 
198  void setReceiveCallback(void (*on_receive)(void *userData, uint8_t addr, IPacket &pkt) = nullptr)
199  {
200  m_onReceive = on_receive;
201  };
202 
207  void setSendCallback(void (*on_send)(void *userData, uint8_t addr, IPacket &pkt) = nullptr)
208  {
209  m_onSend = on_send;
210  };
211 
216  void setConnectEventCallback(void (*on_connect)(void *userData, uint8_t addr, bool connected) = nullptr)
217  {
218  m_onConnectEvent = on_connect;
219  }
220 
227  void setWindowSize(uint8_t window)
228  {
229  m_window = window;
230  }
231 
236  void setSendTimeout(uint16_t timeout)
237  {
238  m_sendTimeout = timeout;
239  }
240 
245  void setUserData(void *userData)
246  {
247  m_userData = userData;
248  }
249 
254  {
255  return m_handle;
256  }
257 
261  int getStatus()
262  {
263  return tiny_fd_get_status(m_handle);
264  }
265 
266 protected:
274  virtual void onReceive(uint8_t addr, uint8_t *pdata, int size)
275  {
276  IPacket pkt((char *)pdata, size);
277  pkt.m_len = size;
278  if ( m_onReceive )
279  m_onReceive(m_userData, addr, pkt);
280  }
281 
289  virtual void onSend(uint8_t addr, const uint8_t *pdata, int size)
290  {
291  IPacket pkt((char *)pdata, size);
292  pkt.m_len = size;
293  if ( m_onSend )
294  m_onSend(m_userData, addr, pkt);
295  }
296 
303  virtual void onConnectEvent(uint8_t addr, bool connected)
304  {
305  if ( m_onConnectEvent )
306  m_onConnectEvent(m_userData, addr, connected);
307  }
308 
309 private:
311  tiny_fd_handle_t m_handle = nullptr;
312 
314  uint8_t *m_buffer = nullptr;
315 
316  hdlc_crc_t m_crc = HDLC_CRC_DEFAULT;
317 
319  int m_bufferSize = 0;
320 
322  uint16_t m_sendTimeout = 0;
323 
325  uint8_t m_window = 3;
326 
328  void (*m_onReceive)(void *userData, uint8_t addr, IPacket &pkt) = nullptr;
329 
331  void (*m_onSend)(void *userData, uint8_t addr, IPacket &pkt) = nullptr;
332 
334  void (*m_onConnectEvent)(void *userData, uint8_t addr, bool connected) = nullptr;
335 
337  void *m_userData = nullptr;
338 
340  static void onReceiveInternal(void *handle, uint8_t addr, uint8_t *pdata, int size);
341 
343  static void onSendInternal(void *handle, uint8_t addr, const uint8_t *pdata, int size);
344 
346  static void onConnectEventInternal(void *handle, uint8_t addr, bool connected);
347 };
348 
354 template <int S> class Fd: public IFd
355 {
356 public:
357  Fd()
358  : IFd(m_data, S)
359  {
360  }
361 
362 private:
363  TINY_ALIGNED_STRUCT uint8_t m_data[S]{};
364 };
365 
371 class FdD: public IFd
372 {
373 public:
378  explicit FdD(int size)
379  : IFd(reinterpret_cast<uint8_t *>(new uintptr_t[(size + TINY_ALIGN_STRUCT_VALUE - 1) / TINY_ALIGN_STRUCT_VALUE]), size)
380  , m_allocated(reinterpret_cast<uintptr_t *>(m_buffer))
381  {
382  }
383 
384  ~FdD()
385  {
386  end();
387  delete[] m_allocated;
388  }
389 
390 private:
391  uintptr_t *m_allocated = nullptr;
392 };
393 
398 } // namespace tinyproto
void begin()
Initializes protocol internal variables.
Definition: TinyProtocolFd.cpp:59
Definition: tiny_fd_int.h:103
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:216
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:274
This is class, which allocates buffers statically.
Definition: TinyProtocolFd.h:354
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:253
FdD(int size)
Creates instance of Full duplex protocol with dynamically allocated buffer.
Definition: TinyProtocolFd.h:378
void disableCrc()
Disable CRC field in the protocol.
Definition: TinyProtocolFd.cpp:138
IFd class encapsulates Full Duplex Protocol functionality.
Definition: TinyProtocolFd.h:67
void setWindowSize(uint8_t window)
Sets desired window size.
Definition: TinyProtocolFd.h:227
void setReceiveCallback(void(*on_receive)(void *userData, uint8_t addr, IPacket &pkt)=nullptr)
Sets receive callback for incoming messages.
Definition: TinyProtocolFd.h:198
void setSendCallback(void(*on_send)(void *userData, uint8_t addr, IPacket &pkt)=nullptr)
Sets send callback for outgoing messages.
Definition: TinyProtocolFd.h:207
IFd(void *buffer, int bufferSize)
Initializes IFd object.
Definition: TinyProtocolFd.h:76
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:371
void setUserData(void *userData)
Sets user data to pass to callbacks.
Definition: TinyProtocolFd.h:245
virtual void onSend(uint8_t addr, const uint8_t *pdata, int size)
Method called by hdlc protocol upon sending next frame.
Definition: TinyProtocolFd.h:289
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:261
void setSendTimeout(uint16_t timeout)
Sets send timeout in milliseconds.
Definition: TinyProtocolFd.h:236
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:303
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:997