Skip to content

Commit 3ee452d

Browse files
committed
Added logger unit test
1 parent 72f5f25 commit 3ee452d

4 files changed

Lines changed: 99 additions & 13 deletions

File tree

src/hal/tiny_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ extern "C"
224224

225225
/**
226226
* on_connect_event_cb_t is a callback function, which is called every time connection is established or interrupted.
227-
* @param handle handle of Tiny.
227+
* @param udata user data
228228
* @param address remote client id (only for master device).
229229
* @param connected event occured.
230230
* @return None.
231231
*/
232-
typedef void (*on_connect_event_cb_t)(void *handle, uint8_t address, bool connected);
232+
typedef void (*on_connect_event_cb_t)(void *udata, uint8_t address, bool connected);
233233

234234
#define EVENT_BITS_ALL 0xFF ///< All bits supported by tiny HAL events
235235
#define EVENT_BITS_CLEAR 1 ///< Flag, used in tiny_events_wait()

src/proto/fd/tiny_fd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ extern "C"
134134
* this callback is called for every frame, regardless of whether it is sent or received: I-frames, S-frames or U-frames.
135135
* It provides detailed information about the frame, including its type, subtype, sequence numbers and data.
136136
*
137+
* @param udata user data, passed during Tiny Full Duplex initialization.
137138
* @param handle handle of Tiny.
138139
* @param direction direction of the frame, can be TINY_FD_FRAME_DIRECTION_IN or TINY_FD_FRAME_DIRECTION_OUT.
139140
* @param frame_type type of the frame, can be TINY_FD_FRAME_TYPE_I, TINY_FD_FRAME_TYPE_S or TINY_FD_FRAME_TYPE_U.
@@ -145,7 +146,8 @@ extern "C"
145146
* @param data pointer to the frame data.
146147
* @param len length of the frame data.
147148
*/
148-
typedef void (*tiny_fd_log_frame_cb_t)(tiny_fd_handle_t handle,
149+
typedef void (*tiny_fd_log_frame_cb_t)(void *udata,
150+
tiny_fd_handle_t handle,
149151
tiny_fd_frame_direction_t direction,
150152
tiny_fd_frame_type_t frame_type,
151153
tiny_fd_frame_subtype_t frame_subtype,

src/proto/fd/tiny_fd_proto_logger.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ static uint8_t __get_awaiting_sequence(uint8_t control)
9292
#if defined(TINY_FD_DEBUG) && defined(TINY_FILE_LOGGING)
9393
///////////////////////////////////////////////////////////////////////////////
9494

95-
static const char *__get_frame_type_str(uint8_t control)
95+
static const char __get_frame_type_str(uint8_t control)
9696
{
9797
tiny_fd_frame_type_t type = __get_frame_type(control);
9898
switch (type)
9999
{
100-
case TINY_FD_FRAME_TYPE_I: return "I";
101-
case TINY_FD_FRAME_TYPE_S: return "S";
102-
case TINY_FD_FRAME_TYPE_U: return "U";
103-
default: return "U";
100+
case TINY_FD_FRAME_TYPE_I: return 'I';
101+
case TINY_FD_FRAME_TYPE_S: return 'S';
102+
case TINY_FD_FRAME_TYPE_U: return 'U';
103+
default: return 'U';
104104
}
105105
}
106106

@@ -143,7 +143,9 @@ void __tiny_fd_log_frame(tiny_fd_handle_t handle,
143143
return;
144144
}
145145
if (handle->log_frame_cb) {
146-
handle->log_frame_cb(handle, direction,
146+
handle->log_frame_cb(handle->user_data,
147+
handle,
148+
direction,
147149
__get_frame_type(data[1]),
148150
__get_frame_subtype(data[1]),
149151
__get_frame_sequence(data[1]),

unittest/tiny_fd_tests.cpp

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ TEST_GROUP(TINY_FD)
4545
init.pdata = this;
4646
init.on_connect_event_cb = __onConnect;
4747
init.on_read_cb = onRead;
48+
init.on_send_cb = onSend;
49+
init.log_frame_cb = logFrame;
4850
init.buffer = inBuffer.data();
4951
init.buffer_size = inBuffer.size();
5052
init.window_frames = 7;
@@ -61,29 +63,53 @@ TEST_GROUP(TINY_FD)
6163
void teardown()
6264
{
6365
tiny_fd_close(handle);
66+
logFrameFunc = nullptr;
6467
}
6568

6669
void onConnect(uint8_t, bool status) { connected = status; }
6770
void onRead(uint8_t, uint8_t *, int) { }
71+
void onSend(uint8_t, const uint8_t *, int) { }
6872

69-
static void __onConnect(void *handle, uint8_t address, bool connected)
73+
static void __onConnect(void *udata, uint8_t address, bool connected)
7074
{
7175
// get the instance of the test class
72-
auto *self = static_cast<TEST_GROUP_CppUTestGroupTINY_FD *>(handle);
76+
auto *self = static_cast<TEST_GROUP_CppUTestGroupTINY_FD *>(udata);
7377
self->onConnect(address, connected);
7478
}
7579

76-
static void onRead(void *handle, uint8_t address, uint8_t *buf, int len)
80+
static void onRead(void *udata, uint8_t address, uint8_t *buf, int len)
7781
{
7882
// get the instance of the test class
79-
auto *self = static_cast<TEST_GROUP_CppUTestGroupTINY_FD *>(handle);
83+
auto *self = static_cast<TEST_GROUP_CppUTestGroupTINY_FD *>(udata);
8084
self->onRead(address, buf, len);
8185
}
8286

87+
static void onSend(void *udata, uint8_t address, const uint8_t *buf, int len)
88+
{
89+
// get the instance of the test class
90+
auto *self = static_cast<TEST_GROUP_CppUTestGroupTINY_FD *>(udata);
91+
self->onSend(address, buf, len);
92+
}
93+
94+
static void logFrame(void *udata, tiny_fd_handle_t handle, tiny_fd_frame_direction_t direction,
95+
tiny_fd_frame_type_t frame_type, tiny_fd_frame_subtype_t frame_subtype,
96+
uint8_t ns, uint8_t nr, const uint8_t *data, int len)
97+
{
98+
// get the instance of the test class
99+
auto *self = static_cast<TEST_GROUP_CppUTestGroupTINY_FD *>(udata);
100+
if (!self->logFrameFunc) {
101+
return; // No logging function set
102+
}
103+
self->logFrameFunc(handle, direction, frame_type, frame_subtype, ns, nr, data, len);
104+
}
105+
83106
tiny_fd_handle_t handle = nullptr;
84107
bool connected = false;
85108
std::array<uint8_t, 1024> inBuffer{};
86109
std::array<uint8_t, 1024> outBuffer{};
110+
std::function<void(tiny_fd_handle_t, tiny_fd_frame_direction_t,
111+
tiny_fd_frame_type_t, tiny_fd_frame_subtype_t, uint8_t, uint8_t,
112+
const uint8_t *, int)> logFrameFunc = nullptr;
87113

88114
void establishConnection()
89115
{
@@ -246,3 +272,59 @@ TEST(TINY_FD, ABM_CheckMtuAPI)
246272
CHECK(mtu > 0); // MTU should be greater than 0
247273
CHECK_EQUAL(34, mtu); // Assuming the MTU is 34 bytes according to protocol test configuration
248274
}
275+
276+
TEST(TINY_FD, ABM_CheckLoggerFunction)
277+
{
278+
int counter = 0;
279+
// Check logger function
280+
auto log_frame_func = [&counter](tiny_fd_handle_t handle,
281+
tiny_fd_frame_direction_t direction,
282+
tiny_fd_frame_type_t frame_type,
283+
tiny_fd_frame_subtype_t frame_subtype,
284+
uint8_t ns,
285+
uint8_t nr,
286+
const uint8_t *data,
287+
int len) {
288+
switch (counter) {
289+
case 0: // SABM frame
290+
CHECK_EQUAL(TINY_FD_FRAME_TYPE_U, frame_type);
291+
CHECK_EQUAL(TINY_FD_FRAME_SUBTYPE_SABM, frame_subtype);
292+
CHECK_EQUAL(0x00, ns);
293+
CHECK_EQUAL(0x00, nr);
294+
CHECK_EQUAL(2, len);
295+
CHECK_EQUAL(0x03, data[0]); // Address field
296+
CHECK_EQUAL(0x2F, data[1]); // SABM packet
297+
break;
298+
case 1: // UA frame
299+
CHECK_EQUAL(TINY_FD_FRAME_TYPE_U, frame_type);
300+
CHECK_EQUAL(TINY_FD_FRAME_SUBTYPE_UA, frame_subtype);
301+
CHECK_EQUAL(0x00, ns);
302+
CHECK_EQUAL(0x00, nr);
303+
CHECK_EQUAL(2, len);
304+
CHECK_EQUAL(0x01, data[0]); // Address field
305+
CHECK_EQUAL(0x73, data[1]); // UA packet
306+
break;
307+
case 2: // I-frame
308+
CHECK_EQUAL(TINY_FD_FRAME_TYPE_I, frame_type);
309+
CHECK_EQUAL(TINY_FD_FRAME_SUBTYPE_RR, frame_subtype); // Should be RR frame
310+
CHECK_EQUAL(0x00, ns); // N(S) = 0
311+
CHECK_EQUAL(0x00, nr); // N(R) = 1
312+
CHECK_EQUAL(3, len);
313+
CHECK_EQUAL(0x03, data[0]); // Address field
314+
CHECK_EQUAL(0x00, data[1]); // Data byte
315+
CHECK_EQUAL(0x11, data[2]); // Data byte
316+
break;
317+
default:
318+
FAIL("Unexpected frame logged");
319+
break;
320+
}
321+
counter++;
322+
};
323+
logFrameFunc = log_frame_func; // Set the logging function
324+
establishConnection(); // This will trigger the logging function for SABM and UA frames
325+
CHECK_EQUAL(2, counter); // We should have logged 2 frames: SABM and UA
326+
// Now we can send I-frames
327+
auto read_result = tiny_fd_on_rx_data(handle, (uint8_t *)"\x7E\x03\x00\x11\x7E", 5); // I-frame in order
328+
CHECK_EQUAL(TINY_SUCCESS, read_result);
329+
CHECK_EQUAL(3, counter); // We should have logged 3 frames now
330+
}

0 commit comments

Comments
 (0)