tinyproto
TinySerialLinkLayer.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 "link/TinyLinkLayer.h"
32 #include "interface/TinySerial.h"
33 
34 namespace tinyproto
35 {
36 
43 template <class BASE, int BSIZE> class ISerialLinkLayer: public BASE
44 {
45 public:
46  ISerialLinkLayer(char *dev, void *buffer, int size)
47  : BASE(buffer, size)
48  , m_serial(dev)
49  {
50  }
51 
52  bool begin(on_frame_read_cb_t onReadCb, on_frame_send_cb_t onSendCb, void *udata) override
53  {
54  bool result = BASE::begin(onReadCb, onSendCb, udata);
55  m_serial.setTimeout( this->getTimeout() );
56  return result && m_serial.begin(m_speed);
57  }
58 
59  void end() override
60  {
61  m_serial.end();
62  BASE::end();
63  }
64 
65  void runRx() override
66  {
67  uint8_t buf[BSIZE];
68  uint8_t *p = buf;
69 
70  int len = m_serial.readBytes(p, BSIZE);
71  while ( len > 0 )
72  {
73  int temp = BASE::parseData( p, len);
74  if ( temp < 0 )
75  {
76  break;
77  }
78  len -= temp;
79  p += temp;
80  }
81  }
82 
83  void runTx() override
84  {
85  uint8_t buf[BSIZE];
86  int len = BASE::getData( buf, BSIZE );
87  uint8_t *ptr = buf;
88  while ( len > 0 )
89  {
90  int sent = m_serial.write(ptr, len);
91  if ( sent < 0 )
92  {
93  break;
94  }
95  ptr += sent;
96  len -= sent;
97  }
98  }
99 
100  void setSpeed( uint32_t speed )
101  {
102  m_speed = speed;
103  }
104 
105 private:
106  uint32_t m_speed = 115200;
107  tinyproto::Serial m_serial;
108 };
109 
110 } // 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
Definition: TinySerial.h:42
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