@@ -62,6 +62,8 @@ int csp_if_cblk_tx(csp_iface_t * iface, uint16_t via, csp_packet_t *packet, int
6262 uint16_t frame_length = packet -> frame_length ;
6363 uint8_t * frame_begin = packet -> frame_begin ;
6464
65+ ifdata -> cblk_tx_lock (iface );
66+
6567 if (param_get_uint8 (& tx_encrypt )) {
6668 frame_length = crypto_encrypt (ifdata -> packet_enc , packet -> frame_begin , packet -> frame_length );
6769 frame_begin = & ifdata -> packet_enc [CRYPTO_PREAMP ];
@@ -76,6 +78,7 @@ int csp_if_cblk_tx(csp_iface_t * iface, uint16_t via, csp_packet_t *packet, int
7678
7779 cblk_frame_t * tx_ccsds_buf = ifdata -> cblk_tx_buffer_get (iface );
7880 if (tx_ccsds_buf == NULL ) {
81+ ifdata -> cblk_tx_unlock (iface );
7982 csp_buffer_free (packet );
8083 return CSP_ERR_NOBUFS ;
8184 }
@@ -96,11 +99,13 @@ int csp_if_cblk_tx(csp_iface_t * iface, uint16_t via, csp_packet_t *packet, int
9699 bytes_remain -= segment_len ;
97100
98101 if (ifdata -> cblk_tx_send (iface , tx_ccsds_buf ) < 0 ) {
102+ ifdata -> cblk_tx_unlock (iface );
99103 csp_buffer_free (packet );
100104 return CSP_ERR_NOBUFS ;
101105 }
102106 }
103107
108+ ifdata -> cblk_tx_unlock (iface );
104109 csp_buffer_free (packet );
105110
106111 return CSP_ERR_NONE ;
@@ -112,7 +117,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
112117
113118 uint16_t frame_length = be16toh (frame -> hdr .data_length );
114119
115- if (_cblk_rx_debug >= 1 ) {
120+ if (_cblk_rx_debug >= 3 ) {
116121 printf ("RX %p chain %u CCSDS header: %u %u %u\n" , frame , group , frame -> hdr .csp_packet_idx , frame -> hdr .ccsds_frame_idx , frame_length );
117122 }
118123
@@ -126,7 +131,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
126131 } else if (ifdata -> rx_packet_idx == frame -> hdr .csp_packet_idx && ifdata -> rx_frame_idx == frame -> hdr .ccsds_frame_idx ) {
127132
128133 /* We already handled this frame */
129- if (_cblk_rx_debug >= 1 ) printf ("Discarding dublicated frame\n" );
134+ if (_cblk_rx_debug >= 2 ) printf ("Discarding dublicated frame\n" );
130135 return CSP_ERR_NONE ;
131136
132137 } else if (frame -> hdr .ccsds_frame_idx == 0 ) {
@@ -139,7 +144,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
139144
140145 /* We are missing part of the received CSP frame */
141146 if (_cblk_rx_debug >= 1 ) {
142- printf ("Part of CSP frame is missing: Received part %d of %d , expected part %d of %d \n" ,
147+ printf ("Part of CSP frame is missing: Received part %" PRIu8 " of %" PRIu8 " , expected part %" PRIu8 " of %" PRIu8 " \n" ,
143148 frame -> hdr .ccsds_frame_idx , frame -> hdr .csp_packet_idx , ifdata -> rx_frame_idx + 1 , ifdata -> rx_packet_idx );
144149 }
145150 iface -> frame ++ ;
@@ -166,7 +171,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
166171
167172 if (frame -> hdr .nacl_crypto_key > 0 ) {
168173
169- if (_cblk_rx_debug >= 3 ) {
174+ if (_cblk_rx_debug >= 4 ) {
170175 csp_hex_dump ("-rx_enc" , & ifdata -> packet_dec [CRYPTO_PREAMP ], frame_length );
171176 }
172177
@@ -192,7 +197,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
192197 rx_packet -> frame_length = frame_length ;
193198 }
194199
195- if (_cblk_rx_debug >= 3 ) {
200+ if (_cblk_rx_debug >= 5 ) {
196201 csp_hex_dump ("-rx_dec" , rx_packet -> frame_begin , rx_packet -> frame_length );
197202 }
198203
@@ -203,24 +208,25 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
203208 return CSP_ERR_INVAL ;
204209 }
205210
206- if (_cblk_rx_debug >= 2 ) {
211+ if (_cblk_rx_debug >= 4 ) {
207212 csp_hex_dump ("packet" , rx_packet -> data , rx_packet -> length );
208213 }
209214
210215 csp_qfifo_write (rx_packet , iface , NULL );
211- /* We have succesfully transmitted a full packet,
212- reset our internal index counters for the next ones */
213- ifdata -> rx_packet_idx = -1 ;
214- ifdata -> rx_frame_idx = -1 ;
215216 return CSP_ERR_NONE ;
216217}
217218
218219void csp_if_cblk_init (csp_iface_t * iface ) {
219220
220221 csp_cblk_interface_data_t * ifdata = iface -> interface_data ;
221222
222- ifdata -> rx_frame_idx = -1 ;
223- ifdata -> rx_packet_idx = -1 ;
223+ ifdata -> rx_frame_idx = UINT8_MAX ;
224+ ifdata -> rx_packet_idx = UINT8_MAX ;
225+
226+ if (ifdata -> cblk_tx_lock == NULL || ifdata -> cblk_tx_unlock == NULL ) {
227+ printf ("csp_if_cblk_init: lock function pointers must be set!\n" );
228+ return ;
229+ }
224230
225231 iface -> nexthop = csp_if_cblk_tx ;
226232}
0 commit comments