@@ -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