Skip to content

Commit da3bba1

Browse files
Merge pull request #19 from TNCT-Mechatech/release-v1.1.0beta
Release v1.1.0beta
2 parents 1a13681 + d94ebfd commit da3bba1

File tree

8 files changed

+100
-76
lines changed

8 files changed

+100
-76
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## リリースバージョン
88

9-
* **1.0.0-beta**
9+
* **1.1.0-beta**
1010

1111
## 導入
1212
### Linux

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SerialBridge
2-
version=1.0.0
2+
version=1.1.0
33
author=TNCT-Mechatech
44
maintainer=TNCT-Mechatech
55
sentence=Serial communication between nodes.

src/CobsSerial.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class CobsSerial
109109
{
110110
public:
111111

112-
CobsSerial(SerialDev *dev);
112+
CobsSerial(SerialDev *dev, const unsigned int buff_size=RX_BUFFER_SIZE);
113113

114114
int read(uint8_t *data);
115115

@@ -122,6 +122,8 @@ class CobsSerial
122122
};
123123

124124
private:
125+
const unsigned int _buff_size;
126+
125127
SerialDev *_dev;
126128

127129
ring_queue<uint8_t> _rx_buffer;

src/Message.hpp

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -39,112 +39,93 @@
3939

4040
#include <stdint.h>
4141
#include <stdlib.h>
42+
#include <string.h>
4243

43-
/** SerialBridge's namespace */
44+
/**
45+
* @namespace sb
46+
* @brief Namespace to indicate SerialBridge messages.
47+
*/
4448
namespace sb
4549
{
4650

4751
/**
48-
* @brief sb::Message interface class.
52+
* @brief sb::MessageInterface interface class.
4953
* @details
5054
* This is for abstracting various sb::Messages so that they can be handled
5155
* with a unified interface when used within SerialBridge.
5256
*/
53-
class _Message
57+
class MessageInterface
5458
{
5559
public:
56-
/**
57-
* @brief Returns a pointer to the current packet.
58-
* @return uint8_t* Pointer indicating the packet array.
59-
*/
60-
virtual uint8_t *ptr() = 0;
60+
uint8_t *ptr();
61+
62+
void packing(uint8_t id);
63+
void unpacking();
6164

6265
/**
63-
* @brief Stores the data structure in a packet.
64-
* This function is used internally by SerialBridge when sending.
65-
* @param[in] id Message ID assigned when sending.
66-
* @return None
66+
* @brief Returns the length of the current packet.
67+
* @return int Current packet array length.
6768
*/
68-
virtual void packing(uint8_t id) = 0;
69+
virtual int size() = 0;
6970

71+
protected:
7072
/**
71-
* @brief Extract the data structure from the packet.
72-
* This function is used internally by SerialBridge on reception.
73-
* @return None
73+
* @brief A function that returns a data structure.
74+
* @return *void Pointer to the data structure of the derived class.
7475
*/
75-
virtual void unpacking() = 0;
76+
virtual void *_data_ptr() = 0;
7677

7778
/**
78-
* @brief Returns the length of the current packet.
79-
* @return int Current packet array length.
79+
* @brief The length of the packet used to identify the data.
8080
*/
81-
virtual int size() = 0;
81+
enum{
82+
CTRL_DATA_LEN = 2,
83+
};
84+
85+
uint8_t *_all_packet;
8286
};
8387

8488
/**
8589
* @brief A class that stores the data processed by the SerialBridge class.
8690
* @tparam DataStruct Specify the type of data structure you want to handle with sb::Message.
8791
*/
8892
template <class DataStruct>
89-
class Message : public _Message
93+
class Message : public MessageInterface
9094
{
9195
public:
92-
93-
/** Data is passed using this member structure. */
94-
DataStruct data;
95-
9696
/**
97-
* @deprecated The following functions are used for internal processing
98-
* and do not support direct user calls.
97+
* @brief Data is exchanged through this member variable.
98+
* You can specify any structure type here with template parameters.
9999
*/
100-
virtual uint8_t *ptr()
101-
{
102-
return _all_packet;
103-
}
100+
DataStruct data;
104101

105102
/**
106-
* @deprecated The following functions are used for internal processing
107-
* and do not support direct user calls.
103+
* @brief Message class constructor.
108104
*/
109-
virtual void packing(uint8_t id)
105+
Message()
110106
{
111-
_all_packet[0] = id;
112-
_Packet_t tmp;
113-
tmp.data = data;
114-
115-
uint32_t sum = 0;
116-
for(int i = 0; i < size()-1; i++){
117-
if(i >= 1)
118-
_all_packet[i] = tmp.all[i-1];
119-
sum += _all_packet[i];
120-
}
121-
_all_packet[size()-1] = (uint8_t)(sum & 0xFF);
107+
_all_packet = new uint8_t[sizeof(DataStruct) + CTRL_DATA_LEN];
122108
}
123109

124110
/**
125-
* @deprecated The following functions are used for internal processing
126-
* and do not support direct user calls.
111+
* @brief Message class destructor.
127112
*/
128-
virtual void unpacking()
113+
~Message()
129114
{
130-
_Packet_t tmp;
131-
for(int i = 0; i < (int)sizeof(DataStruct); i++){
132-
tmp.all[i] = _all_packet[i+1];
133-
}
134-
data = tmp.data;
115+
delete[] _all_packet;
135116
}
136117

137118
virtual int size()
138119
{
139-
return sizeof(DataStruct) + 2;
120+
return sizeof(DataStruct) + CTRL_DATA_LEN;
140121
}
122+
141123
private:
142-
typedef union{
143-
uint8_t all[sizeof(DataStruct)];
144-
DataStruct data;
145-
}_Packet_t;
146124

147-
uint8_t _all_packet[sizeof(DataStruct)+2];
125+
virtual void *_data_ptr()
126+
{
127+
return &data;
128+
}
148129
};
149130

150131
};

src/SerialBridge.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class SerialBridge
2626
public:
2727
typedef uint8_t frame_id;
2828

29-
SerialBridge(SerialDev *dev);
29+
SerialBridge(SerialDev *dev, const unsigned int buff_size=CobsSerial::RX_BUFFER_SIZE);
3030

3131
~SerialBridge();
3232

33-
int add_frame(frame_id id, sb::_Message *str);
33+
int add_frame(frame_id id, sb::MessageInterface *str);
3434
int rm_frame(frame_id id);
3535

3636
int read();
@@ -46,10 +46,12 @@ class SerialBridge
4646
private:
4747
int _id_2_order(frame_id id);
4848

49-
sb::_Message *_str[STRUCT_MAX_NUM];
49+
sb::MessageInterface *_str[STRUCT_MAX_NUM];
5050
frame_id _id_list[STRUCT_MAX_NUM];
5151

5252
CobsSerial *_dev;
53+
54+
const unsigned int _buff_size;
5355
};
5456

5557
#endif //#ifndef _SERIAL_BRIDGE_HPP_

src/source/CobsSerial.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
/**
1313
* @brief CobsSerial class constructor.
1414
* @param[in] dev (SerialDev class pointer) An argument that indicates a serial device class object.
15+
* @param[in] buff_size Receive buffer size.(bytes)
1516
*/
16-
CobsSerial::CobsSerial(SerialDev *dev)
17-
: _rx_buffer(RX_BUFFER_SIZE)
17+
CobsSerial::CobsSerial(SerialDev *dev, const unsigned int buff_size)
18+
: _buff_size(buff_size), _rx_buffer(buff_size)
1819
{
1920
_dev = dev;
2021
_got_packet = false;
@@ -32,7 +33,7 @@ CobsSerial::CobsSerial(SerialDev *dev)
3233
int CobsSerial::read(uint8_t *data)
3334
{
3435
int cnt = 0;
35-
uint8_t tmp[RX_BUFFER_SIZE];
36+
uint8_t tmp[_buff_size];
3637

3738
if(!_data_begin && _got_packet){
3839
uint8_t val = 1;

src/source/Message.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,46 @@
11
/**
22
* @file Message.cpp
3-
* @brief dummy file
3+
* @brief The data format handled by SerialBridge is defined here.
44
* @author Taiyou Komazawa
55
* @date 2021/8/18
6-
* @note Currently, it is a dummy file for makefile to recognize the header file.
76
*/
87

98
#include "../Message.hpp"
9+
10+
/**
11+
* @brief Returns a pointer to the current packet.
12+
* @return uint8_t* Pointer indicating the packet array.
13+
*/
14+
uint8_t* sb::MessageInterface::ptr()
15+
{
16+
return _all_packet;
17+
}
18+
19+
/**
20+
* @brief Stores the data structure in a packet.
21+
* This function is used internally by SerialBridge when sending.
22+
* @param[in] id Message ID assigned when sending.
23+
* @return None
24+
*/
25+
void sb::MessageInterface::packing(uint8_t id)
26+
{
27+
_all_packet[0] = id;
28+
29+
uint32_t sum = 0;
30+
memcpy(_all_packet+1, _data_ptr(), size()-CTRL_DATA_LEN);
31+
for (int i = 0; i < size() - 1; i++)
32+
{
33+
sum += _all_packet[i];
34+
}
35+
_all_packet[size()-1] = (uint8_t)(sum & 0xFF);
36+
}
37+
38+
/**
39+
* @brief Extract the data structure from the packet.
40+
* This function is used internally by SerialBridge on reception.
41+
* @return None
42+
*/
43+
void sb::MessageInterface::unpacking()
44+
{
45+
memcpy(_data_ptr(), _all_packet+1, size()-CTRL_DATA_LEN);
46+
}

src/source/SerialBridge.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
/**
1111
* @brief SerialBridge class constructor.
1212
* @param[in] dev (SerialDev class pointer) An argument that indicates a serial device class object.
13+
* @param[in] buff_size Receive buffer size.(bytes)
1314
*/
14-
SerialBridge::SerialBridge(SerialDev *dev)
15-
: _id_list()
15+
SerialBridge::SerialBridge(SerialDev *dev, const unsigned int buff_size)
16+
: _id_list(), _buff_size(buff_size)
1617
{
17-
_dev = new CobsSerial(dev);
18+
_dev = new CobsSerial(dev, _buff_size);
1819
}
1920

2021
/**
@@ -30,13 +31,13 @@ SerialBridge::~SerialBridge()
3031
* @param[in] id (Message identification number) A number to identify the message.
3132
* The number can be specified from 0x00 to 0xFF, and up to 256 types of messages can be added.
3233
* However, you cannot add more messages than specified by SerialBridge::STRUCT_MAX_NUM.
33-
* @param[in] str (sb::_Message class pointer) A data structure object used to send and receive data.
34+
* @param[in] str (sb::MessageInterface class pointer) A data structure object used to send and receive data.
3435
* @return int Whether the message frame was added successfully.
3536
* @retval 0 : Success.
3637
* @retval -1 : Failure. The argument str specified was NULL.
3738
* @retval -2 : Failure. The number of registered message frames has reached the upper limit.
3839
*/
39-
int SerialBridge::add_frame(SerialBridge::frame_id id, sb::_Message *str)
40+
int SerialBridge::add_frame(SerialBridge::frame_id id, sb::MessageInterface *str)
4041
{
4142
if(str == NULL)
4243
return -1;
@@ -93,7 +94,7 @@ int SerialBridge::rm_frame(frame_id id)
9394
*/
9495
int SerialBridge::read()
9596
{
96-
uint8_t tmp[CobsSerial::RX_BUFFER_SIZE] = {};
97+
uint8_t tmp[_buff_size] = {};
9798
int len = _dev->read(tmp) - 1;
9899
if(len > 0){
99100
int order = _id_2_order(tmp[0]);

0 commit comments

Comments
 (0)