@@ -46,26 +46,6 @@ static void on_frame_send(void *user_data, const uint8_t *data, int len);
4646// Helper functions
4747///////////////////////////////////////////////////////////////////////////////
4848
49- static uint8_t __switch_to_next_peer (tiny_fd_handle_t handle )
50- {
51- const uint8_t start_peer = handle -> next_peer ;
52- do
53- {
54- if ( ++ handle -> next_peer >= handle -> peers_count )
55- {
56- handle -> next_peer = 0 ;
57- }
58- if ( handle -> peers [ handle -> next_peer ].addr != HDLC_INVALID_PEER_INDEX )
59- {
60- break ;
61- }
62- } while ( start_peer != handle -> next_peer );
63- LOG (TINY_LOG_INFO , "[%p] Switching to peer [%02X]\n" , handle , handle -> next_peer );
64- return start_peer != handle -> next_peer ;
65- }
66-
67- ///////////////////////////////////////////////////////////////////////////////
68-
6949#if 0
7050static inline uint8_t __number_of_awaiting_tx_i_frames (tiny_fd_handle_t handle , uint8_t peer )
7151{
@@ -208,15 +188,24 @@ static void on_frame_read(void *user_data, uint8_t *data, int len)
208188 {
209189 LOG (TINY_LOG_WRN , "[%p] Unknown hdlc frame received\n" , handle );
210190 }
211- if ( control & HDLC_P_BIT )
191+ // Check that if we are in NRM mode then we have something to send
192+ if ( handle -> mode == TINY_FD_MODE_NRM )
212193 {
213- // Check that if we are in NRM mode then we have something to send
214- if ( handle -> mode == TINY_FD_MODE_NRM )
194+ if ( control & ( HDLC_P_BIT | HDLC_F_BIT ) )
215195 {
216196 LOG (TINY_LOG_INFO , "[%p] [CAPTURED MARKER]\n" , handle );
197+ // Cool! Now we have marker again, and we can send
198+ tiny_events_set ( & handle -> events , FD_EVENT_HAS_MARKER );
199+ // For primary station
200+ // 1. Switch to the next peer
201+ // 2. If no I-frames to send, then put RR S-frame to the queue with poll bit set
202+ // 3. If I-frames to send, then send I-frames (1 or multiple)
203+ // 3.1. The last one should have final bit set
204+ // For secondary station
205+ // 1. If no I-frames to send, then put RR S-frame to the queue with poll bit set
206+ // 2. If I-frames to send, then send I-frames (1 or multiple)
207+ // 2.1. The last one should have final bit set
217208 }
218- // Cool! Now we have marker again, and we can send
219- tiny_events_set ( & handle -> events , FD_EVENT_HAS_MARKER );
220209 }
221210 tiny_mutex_unlock (& handle -> frames .mutex );
222211}
@@ -247,23 +236,28 @@ static void on_frame_send(void *user_data, const uint8_t *data, int len)
247236 tiny_fd_queue_free_by_header ( & handle -> frames .s_queue , data );
248237 }
249238 // Clear send flag and clear marker if final was transferred. For ABM mode the marker is never cleared
250- uint8_t flags = FD_EVENT_TX_SENDING ;
251- if ( ( control & HDLC_F_BIT ) && handle -> mode == TINY_FD_MODE_NRM )
239+ uint8_t flags_to_clear = FD_EVENT_TX_SENDING ;
240+ if ( handle -> mode == TINY_FD_MODE_NRM )
252241 {
253242 // Let's talk to the next station if we are primary
254243 // Of course, we could switch to the next peer upon receving response
255244 // from the peer we provided the marker to... But what? What if
256245 // remote peer never responds to us. So, having switch procedure
257246 // in this callback simplifies things
258- if ( __is_primary_station ( handle ) )
247+ if ( __is_primary_station ( handle ) && ( control & HDLC_P_BIT ) )
259248 {
260249 __switch_to_next_peer ( handle );
250+ LOG (TINY_LOG_INFO , "[%p] [RELEASED MARKER]\n" , handle );
251+ flags_to_clear |= FD_EVENT_HAS_MARKER ;
252+ }
253+ else if ( !__is_primary_station ( handle ) && (control & HDLC_F_BIT ) )
254+ {
255+ LOG (TINY_LOG_INFO , "[%p] [RELEASED MARKER]\n" , handle );
256+ flags_to_clear |= FD_EVENT_HAS_MARKER ;
261257 }
262- LOG (TINY_LOG_INFO , "[%p] [RELEASED MARKER]\n" , handle );
263- flags |= FD_EVENT_HAS_MARKER ;
264258 }
265- tiny_events_clear ( & handle -> events , flags );
266- tiny_mutex_unlock (& handle -> frames .mutex );
259+ tiny_events_clear ( & handle -> events , flags_to_clear );
260+ tiny_mutex_unlock ( & handle -> frames .mutex );
267261}
268262
269263///////////////////////////////////////////////////////////////////////////////
@@ -421,6 +415,7 @@ int tiny_fd_init(tiny_fd_handle_t *handle, tiny_fd_init_t *init)
421415
422416 tiny_mutex_create (& protocol -> frames .mutex );
423417 tiny_events_create (& protocol -> events );
418+ // Primary station has marker by default
424419 tiny_events_set ( & protocol -> events , FD_EVENT_QUEUE_HAS_FREE_SLOTS |
425420 (__is_primary_station ( protocol ) ? FD_EVENT_HAS_MARKER : 0 ) );
426421 * handle = protocol ;
@@ -629,7 +624,12 @@ static void tiny_fd_disconnected_check_idle_timeout(tiny_fd_handle_t handle, uin
629624
630625int tiny_fd_get_tx_data (tiny_fd_handle_t handle , void * data , int len , uint32_t timeout )
631626{
627+ // TODO: !!!!!
628+ // We cannot generate data forever, and we should return result as soon as possible
629+ // Now this is not true for NRM mode, as we can generate data forever until
630+ // passed buffer is filled up.
632631 bool repeat = true;
632+ bool wait_point_passed = false;
633633 int result = 0 ;
634634 // TODO: Check for correct mutex usage here. Some fields are not protected
635635 const uint8_t peer = handle -> next_peer ;
@@ -656,6 +656,11 @@ int tiny_fd_get_tx_data(tiny_fd_handle_t handle, void *data, int len, uint32_t t
656656 {
657657 tiny_fd_disconnected_check_idle_timeout (handle , peer );
658658 }
659+ // TODO: This point cannot be reached twice!!!!
660+ if (wait_point_passed ) {
661+ break ;
662+ }
663+ wait_point_passed = true;
659664 // Since no send operation is in progress, check if we have something to send
660665 // Check if the station has marker to send FIRST (That means, we are allowed to send anything still)
661666 if ( tiny_events_wait (& handle -> events , FD_EVENT_HAS_MARKER , EVENT_BITS_LEAVE , timeout ) )
@@ -689,7 +694,7 @@ int tiny_fd_get_tx_data(tiny_fd_handle_t handle, void *data, int len, uint32_t t
689694 {
690695 if ( __time_passed_since_last_marker_seen (handle ) >= handle -> retry_timeout )
691696 {
692- // Return marker back as remote station not responding
697+ // Return marker back to primary station as remote station not responding
693698 LOG (TINY_LOG_CRIT , "[%p] RETURN MARKER BACK\n" , handle );
694699 tiny_events_set ( & handle -> events , FD_EVENT_HAS_MARKER );
695700 }
@@ -699,7 +704,9 @@ int tiny_fd_get_tx_data(tiny_fd_handle_t handle, void *data, int len, uint32_t t
699704 }
700705 }
701706 }
702- result += generated_data ;
707+ if (result >= 0 ) {
708+ result += generated_data ;
709+ }
703710 if ( !generated_data )
704711 {
705712 if ( !repeat )
@@ -964,6 +971,7 @@ int tiny_fd_register_peer(tiny_fd_handle_t handle, uint8_t address)
964971 tiny_mutex_lock (& handle -> frames .mutex );
965972 if ( __address_field_to_peer ( handle , address ) != HDLC_INVALID_PEER_INDEX )
966973 {
974+ // Peer already registered
967975 tiny_mutex_unlock (& handle -> frames .mutex );
968976 return TINY_ERR_FAILED ;
969977 }
0 commit comments