• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenLightingProject / ola / 11783254300

11 Nov 2024 05:23PM UTC coverage: 45.767% (+0.09%) from 45.677%
11783254300

push

github

web-flow
Merge pull request #1978 from DMXControl/add_nodle_r4s

Add DMXControl Projects e.V. Nodle R4S

7798 of 17938 branches covered (43.47%)

0 of 1 new or added line in 1 file covered. (0.0%)

721 existing lines in 11 files now uncovered.

22344 of 48821 relevant lines covered (45.77%)

64.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/common/rdm/MessageSerializer.cpp
1
/*
2
 * This library is free software; you can redistribute it and/or
3
 * modify it under the terms of the GNU Lesser General Public
4
 * License as published by the Free Software Foundation; either
5
 * version 2.1 of the License, or (at your option) any later version.
6
 *
7
 * This library is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 * Lesser General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public
13
 * License along with this library; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
 *
16
 * MessageSerializer.cpp
17
 * Prints the test representation of a Message.
18
 * Copyright (C) 2011 Simon Newton
19
 */
20

21
#include <ola/messaging/Message.h>
22
#include <ola/network/NetworkUtils.h>
23
#include <ola/rdm/MessageSerializer.h>
24
#include <string.h>
25
#include <algorithm>
26

27
namespace ola {
28
namespace rdm {
29

30
MessageSerializer::MessageSerializer(unsigned int initial_size)
10✔
31
    : m_data(NULL),
10✔
32
      m_offset(0),
10✔
33
      m_buffer_size(0),
10✔
34
      m_initial_buffer_size(initial_size) {
10✔
35
}
10✔
36

37
MessageSerializer::~MessageSerializer() {
20✔
38
  if (m_data) {
20✔
39
    delete[] m_data;
20✔
40
  }
41
}
20✔
42

43

44
/**
45
 * Serialize a message and return a pointer to the message in memory
46
 */
47
const uint8_t *MessageSerializer::SerializeMessage(
11✔
48
    const ola::messaging::Message *message,
49
    unsigned int *length) {
50

51
  if (!m_data) {
11✔
52
    m_buffer_size = m_initial_buffer_size;
10✔
53
    m_data = new uint8_t[m_buffer_size];
10✔
54
  }
55
  m_offset = 0;
11✔
56

57
  message->Accept(this);
11✔
58

59
  *length = m_offset;
11✔
60
  return m_data;
11✔
61
}
62

63

64
void MessageSerializer::Visit(
9✔
65
    const ola::messaging::BoolMessageField *message) {
66
  CheckForFreeSpace(message->GetDescriptor()->MaxSize());
9✔
67
  m_data[m_offset++] = message->Value();
9✔
68
}
9✔
69

70

71
void MessageSerializer::Visit(
1✔
72
    const ola::messaging::IPV4MessageField *message) {
73
  CheckForFreeSpace(message->GetDescriptor()->MaxSize());
1✔
74
  uint32_t data = message->Value().AsInt();
1✔
75
  memcpy(m_data + m_offset, reinterpret_cast<uint8_t*>(&data), sizeof(data));
1✔
76
  m_offset += sizeof(data);
1✔
77
}
1✔
78

79

80
void MessageSerializer::Visit(
1✔
81
    const ola::messaging::IPV6MessageField *message) {
82
  unsigned int size = message->GetDescriptor()->MaxSize();
1✔
83
  CheckForFreeSpace(size);
1✔
84
  message->Value().Pack(m_data + m_offset, size);
1✔
85
  m_offset += size;
1✔
86
}
1✔
87

88

89
void MessageSerializer::Visit(
1✔
90
    const ola::messaging::MACMessageField *message) {
91
  unsigned int size = message->GetDescriptor()->MaxSize();
1✔
92
  CheckForFreeSpace(size);
1✔
93
  message->Value().Pack(m_data + m_offset, size);
1✔
94
  m_offset += size;
1✔
95
}
1✔
96

97

98
void MessageSerializer::Visit(
1✔
99
    const ola::messaging::UIDMessageField *message) {
100
  unsigned int size = message->GetDescriptor()->MaxSize();
1✔
101
  CheckForFreeSpace(size);
1✔
102
  message->Value().Pack(m_data + m_offset, size);
1✔
103
  m_offset += size;
1✔
104
}
1✔
105

106

107
void MessageSerializer::Visit(
5✔
108
    const ola::messaging::StringMessageField *message) {
109
  unsigned int size = std::min(
5✔
110
      static_cast<unsigned int>(message->Value().size()),
5✔
111
      message->GetDescriptor()->MaxSize());
5✔
112
  unsigned int used_size = std::max(
5✔
113
      size,
114
      message->GetDescriptor()->MinSize());
5✔
115
  CheckForFreeSpace(size);
5✔
116
  memcpy(m_data + m_offset, message->Value().c_str(), size);
5✔
117
  memset(m_data + m_offset + size, 0, used_size - size);
5✔
118
  m_offset += used_size;
5✔
119
}
5✔
120

121

122
void MessageSerializer::Visit(
17✔
123
    const ola::messaging::BasicMessageField<uint8_t> *message) {
124
  IntVisit(message);
17✔
125
}
17✔
126

127

128
void MessageSerializer::Visit(
11✔
129
    const ola::messaging::BasicMessageField<uint16_t> *message) {
130
  IntVisit(message);
11✔
131
}
11✔
132

133

134
void MessageSerializer::Visit(
9✔
135
    const ola::messaging::BasicMessageField<uint32_t> *message) {
136
  IntVisit(message);
9✔
137
}
9✔
138

139

140
void MessageSerializer::Visit(
2✔
141
    const ola::messaging::BasicMessageField<uint64_t> *message) {
142
  IntVisit(message);
2✔
143
}
2✔
144

145

146
void MessageSerializer::Visit(
2✔
147
    const ola::messaging::BasicMessageField<int8_t> *message) {
148
  IntVisit(message);
2✔
149
}
2✔
150

151

152
void MessageSerializer::Visit(
4✔
153
    const ola::messaging::BasicMessageField<int16_t> *message) {
154
  IntVisit(message);
4✔
155
}
4✔
156

157

158
void MessageSerializer::Visit(
2✔
159
    const ola::messaging::BasicMessageField<int32_t> *message) {
160
  IntVisit(message);
2✔
161
}
2✔
162

163

164
void MessageSerializer::Visit(
2✔
165
    const ola::messaging::BasicMessageField<int64_t> *message) {
166
  IntVisit(message);
2✔
167
}
2✔
168

169

170
void MessageSerializer::Visit(
13✔
171
    const ola::messaging::GroupMessageField *message) {
172
  (void) message;
13✔
173
}
13✔
174

175

176
void MessageSerializer::PostVisit(
13✔
177
    const ola::messaging::GroupMessageField *message) {
178
  (void) message;
13✔
179
}
13✔
180

181

182

183
/**
184
 * Check that there is at least required_size bytes of space left, if not
185
 * expand the memory so the new data can fit.
186
 */
187
void MessageSerializer::CheckForFreeSpace(unsigned int required_size) {
67✔
188
  if (m_buffer_size - m_offset > required_size) {
67✔
189
    return;
190
  }
191

UNCOV
192
  uint8_t *old_buffer = m_data;
×
UNCOV
193
  m_data = new uint8_t[2 * m_buffer_size];
×
UNCOV
194
  memcpy(m_data, old_buffer, m_offset);
×
UNCOV
195
  delete[] old_buffer;
×
196
}
197

198

199
/**
200
 * Serialize an integer value, converting to little endian if needed
201
 */
202
template <typename int_type>
203
void MessageSerializer::IntVisit(
98✔
204
    const ola::messaging::BasicMessageField<int_type> *message) {
205
  CheckForFreeSpace(sizeof(int_type));
98✔
206
  int_type value;
207
  if (message->GetDescriptor()->IsLittleEndian()) {
98✔
208
    value = ola::network::HostToLittleEndian(
16✔
209
        static_cast<int_type>(message->Value()));
8✔
210
  } else {
211
    value = ola::network::HostToNetwork(
82✔
212
        static_cast<int_type>(message->Value()));
60✔
213
  }
214

215

216
  uint8_t *ptr = reinterpret_cast<uint8_t*>(&value);
98✔
217
  memcpy(m_data + m_offset, ptr, sizeof(int_type));
98✔
218
  m_offset += sizeof(int_type);
98✔
219
}
98✔
220
}  // namespace rdm
221
}  // namespace ola
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc