@@ -260,9 +260,8 @@ static void on_frame_send(void *user_data, const uint8_t *data, int len)
260260
261261///////////////////////////////////////////////////////////////////////////////
262262
263- int tiny_fd_init (tiny_fd_handle_t * handle , tiny_fd_init_t * init )
263+ static int __tiny_fd_init_validate (tiny_fd_handle_t * handle , tiny_fd_init_t * init , uint8_t peers_count )
264264{
265- const uint8_t peers_count = init -> peers_count == 0 ? 1 : init -> peers_count ;
266265 * handle = NULL ;
267266 if ( (0 == init -> on_read_cb ) || (0 == init -> buffer ) || (0 == init -> buffer_size ) )
268267 {
@@ -273,7 +272,7 @@ int tiny_fd_init(tiny_fd_handle_t *handle, tiny_fd_init_t *init)
273272 {
274273 int size = tiny_fd_buffer_size_by_mtu_ex (peers_count , 0 , init -> window_frames , init -> crc_type , 1 );
275274 init -> mtu = (init -> buffer_size - size ) / (init -> window_frames + 1 );
276- if ( init -> mtu < 1 )
275+ if ( init -> mtu < 2 )
277276 {
278277 LOG (TINY_LOG_CRIT , "Calculated mtu size is zero, no payload transfer is available%s" , "\n" );
279278 return TINY_ERR_OUT_OF_MEMORY ;
@@ -295,6 +294,21 @@ int tiny_fd_init(tiny_fd_handle_t *handle, tiny_fd_init_t *init)
295294 LOG (TINY_LOG_CRIT , "HDLC uses timeouts for ACK, at least retry_timeout, or send_timeout must be specified%s" , "\n" );
296295 return TINY_ERR_INVALID_DATA ;
297296 }
297+ return TINY_SUCCESS ;
298+ }
299+
300+ ///////////////////////////////////////////////////////////////////////////////
301+
302+ int tiny_fd_init (tiny_fd_handle_t * handle , tiny_fd_init_t * init )
303+ {
304+ const uint8_t peers_count = init -> peers_count == 0 ? 1 : init -> peers_count ;
305+ {
306+ int result = __tiny_fd_init_validate (handle , init , peers_count );
307+ if (result != TINY_SUCCESS )
308+ {
309+ return result ;
310+ }
311+ }
298312 memset (init -> buffer , 0 , init -> buffer_size );
299313
300314 /* Lets locate main FD protocol data at the beginning of specified buffer.
@@ -815,20 +829,27 @@ int tiny_fd_buffer_size_by_mtu(int mtu, int window)
815829
816830int tiny_fd_buffer_size_by_mtu_ex (uint8_t peers_count , int mtu , int tx_window , hdlc_crc_t crc_type , int rx_window )
817831{
832+ // If peers_count is not specified, then we assume that there is only one peer
818833 if ( !peers_count )
819834 {
820835 peers_count = 1 ;
821836 }
822- // Alignment requirements are already satisfied by hdlc_ll_get_buf_size_ex() subfunction call
823- return sizeof (tiny_fd_data_t ) + TINY_ALIGN_STRUCT_VALUE - 1 +
824- peers_count * sizeof (tiny_fd_peer_info_t ) +
825- // RX side
826- hdlc_ll_get_buf_size_ex (mtu + sizeof (tiny_frame_header_t ), crc_type , rx_window ) +
837+ // Aligned size to keep protocol related state machine information
838+ int header_size = sizeof (tiny_fd_data_t ) + TINY_ALIGN_STRUCT_VALUE - 1 +
839+ peers_count * sizeof (tiny_fd_peer_info_t );
840+ // Buffer size to hold HDLC low level protocol RX data
841+ int hdlc_level_rx_size = hdlc_ll_get_buf_size_ex (mtu + sizeof (tiny_frame_header_t ), crc_type , rx_window );
842+ // minimum size of i-frame including header and payload
843+ int i_frame_tx_size = (sizeof (tiny_fd_frame_info_t * ) + sizeof (tiny_fd_frame_info_t ) + mtu -
844+ sizeof (((tiny_fd_frame_info_t * )0 )-> payload ));
845+ // minimum size of s-frame/u-frame including header and 2-bytes payload
846+ // this size includes internal library overhead, introduced by the pointer to the frame info
847+ int u_s_frame_tx_size = (sizeof (tiny_fd_frame_info_t * ) + sizeof (tiny_fd_frame_info_t ));
848+ return header_size +
849+ hdlc_level_rx_size +
827850 // TX side
828- (sizeof (tiny_fd_frame_info_t * ) + sizeof (tiny_fd_frame_info_t ) + mtu -
829- sizeof (((tiny_fd_frame_info_t * )0 )-> payload )) *
830- tx_window +
831- (sizeof (tiny_fd_frame_info_t * ) + sizeof (tiny_fd_frame_info_t )) * TINY_FD_U_QUEUE_MAX_SIZE ;
851+ i_frame_tx_size * tx_window +
852+ u_s_frame_tx_size * TINY_FD_U_QUEUE_MAX_SIZE ;
832853}
833854
834855///////////////////////////////////////////////////////////////////////////////
@@ -850,18 +871,20 @@ int tiny_fd_get_mtu(tiny_fd_handle_t handle)
850871int tiny_fd_send_to (tiny_fd_handle_t handle , uint8_t address , const void * data , int len , uint32_t timeout )
851872{
852873 const uint8_t * ptr = (const uint8_t * )data ;
853- int left = len ;
854- while ( left > 0 )
874+ int left_bytes = len ;
875+ while ( left_bytes > 0 )
855876 {
856- int size = left < tiny_fd_queue_get_mtu ( & handle -> frames .i_queue ) ? left : tiny_fd_queue_get_mtu ( & handle -> frames .i_queue );
877+ int size = left_bytes < tiny_fd_queue_get_mtu ( & handle -> frames .i_queue ) ?
878+ left_bytes : tiny_fd_queue_get_mtu ( & handle -> frames .i_queue );
857879 int result = tiny_fd_send_packet_to (handle , address , ptr , size , timeout );
858880 if ( result != TINY_SUCCESS )
859881 {
860882 break ;
861883 }
862- left -= result ;
884+ ptr += size ;
885+ left_bytes -= size ;
863886 }
864- return left ;
887+ return len - left_bytes ;
865888}
866889
867890///////////////////////////////////////////////////////////////////////////////
0 commit comments