Skip to content

Commit 007e4ca

Browse files
lexus2kCopilot
andcommitted
Fix C++ wrapper reliability issues, add 19 unit tests
Bug fixes: - Light::write(IPacket): return actual byte count instead of bool (was converting result > 0, destroying error info and always returning 0 or 1) - IFd::run_tx(write_func): break on write returning 0 to prevent infinite loop when device is busy (was spinning forever on len -= 0) - IFd::run_rx/run_tx: increase internal buffer from 4 to 64 bytes for better throughput on high-speed serial connections - IHdlcLinkLayer::getData(): protect m_flushFlag read with m_sendMutex to fix race condition with flushTx() from another thread - ISerialLinkLayer::runTx(): break on write returning 0 (same infinite loop fix as IFd::run_tx) - FdD destructor: store original uintptr_t* pointer to fix undefined behavior from delete[] on mismatched type (was allocating uintptr_t[] but deleting as uint8_t[]) - Proto::end(): add null check for m_link before calling m_link->end() - Proto::getLostRxFrames(): protect counter access with mutex New tests (19 added, 101 total): - CPP_LIGHT (3): WriteReturnsByteCount, WritePacketReturnsByteCount, SendAndReceive - CPP_HDLC (3): WriteAndGenerateTxData, SendAndReceiveFrame, WritePacketInterface - CPP_FD (10): BeginAndEnd, StaticBufferVariant, DynamicAllocationCleanup, WriteReturnsProperValues, RunRxWithDirectData, RunTxGeneratesData, RunTxCallbackBreaksOnZeroWrite, ReceiveCallback, ConnectEventCallback, CrcModes - CPP_PACKET (3): PutAndRead, CopyConstructor, Clear Co-authored-by: Copilot <[email protected]>
1 parent a26fd79 commit 007e4ca

File tree

8 files changed

+485
-12
lines changed

8 files changed

+485
-12
lines changed

Makefile.cpputest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ OBJ_UNIT_TEST = \
2222
unittest/tiny_fd_nrm_tests.o \
2323
unittest/fd_tests.o \
2424
unittest/fd_multidrop_tests.o \
25+
unittest/cpp_wrapper_tests.o \
2526

2627

2728
unittest: $(OBJ_UNIT_TEST) library

src/TinyLightProtocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int Light::read(char *buf, int size)
6464

6565
int Light::write(const IPacket &pkt)
6666
{
67-
return tiny_light_send(&m_data, pkt.m_buf, pkt.m_len) > 0;
67+
return tiny_light_send(&m_data, pkt.m_buf, pkt.m_len);
6868
}
6969

7070
int Light::read(IPacket &pkt)

src/TinyProtocol.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ void Proto::end()
194194
m_readThread = nullptr;
195195
}
196196
#endif
197-
m_link->end();
197+
if ( m_link )
198+
{
199+
m_link->end();
200+
}
198201
return;
199202
}
200203

@@ -314,8 +317,10 @@ void Proto::setTxDelay( uint32_t delay )
314317

315318
int Proto::getLostRxFrames()
316319
{
320+
tiny_mutex_lock( &m_mutex );
317321
int val = m_lostRxFrames;
318322
m_lostRxFrames = 0;
323+
tiny_mutex_unlock( &m_mutex );
319324
return val;
320325
}
321326
#endif

src/TinyProtocolFd.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ int IFd::run_rx(const void *data, int len)
9999

100100
int IFd::run_rx(read_block_cb_t read_func)
101101
{
102-
uint8_t buf[4];
102+
uint8_t buf[64];
103103
int len = read_func(m_userData, buf, sizeof(buf));
104104
if ( len <= 0 )
105105
{
@@ -115,7 +115,7 @@ int IFd::run_tx(void *data, int max_size)
115115

116116
int IFd::run_tx(write_block_cb_t write_func)
117117
{
118-
uint8_t buf[4];
118+
uint8_t buf[64];
119119
int len = tiny_fd_get_tx_data(m_handle, buf, sizeof(buf), 0);
120120
if ( len <= 0 )
121121
{
@@ -125,9 +125,9 @@ int IFd::run_tx(write_block_cb_t write_func)
125125
while ( len )
126126
{
127127
int result = write_func(m_userData, ptr, len);
128-
if ( result < 0 )
128+
if ( result <= 0 )
129129
{
130-
return result;
130+
return result < 0 ? result : TINY_ERR_FAILED;
131131
}
132132
len -= result;
133133
ptr += result;

src/TinyProtocolFd.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,18 @@ class FdD: public IFd
371371
*/
372372
explicit FdD(int size)
373373
: IFd(reinterpret_cast<uint8_t *>(new uintptr_t[(size + TINY_ALIGN_STRUCT_VALUE - 1) / TINY_ALIGN_STRUCT_VALUE]), size)
374+
, m_allocated(reinterpret_cast<uintptr_t *>(m_buffer))
374375
{
375376
}
376377

377378
~FdD()
378379
{
379-
delete[] m_buffer;
380+
end();
381+
delete[] m_allocated;
380382
}
381383

382384
private:
385+
uintptr_t *m_allocated = nullptr;
383386
};
384387

385388
/**

src/link/TinyHdlcLinkLayer.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,19 @@ int IHdlcLinkLayer::getData(uint8_t *data, int size)
124124
{
125125
if ( tiny_events_wait( &m_events, TX_MESSAGE_SENDING, EVENT_BITS_LEAVE, getTimeout() ) )
126126
{
127-
if ( m_flushFlag )
127+
tiny_mutex_lock( &m_sendMutex );
128+
bool flush = m_flushFlag;
129+
if ( flush )
128130
{
129-
// tiny_mutex_lock( &m_sendMutex );
130131
m_flushFlag = false;
131132
hdlc_ll_reset( m_handle, HDLC_LL_RESET_TX_ONLY );
132133
tiny_events_clear( &m_events, TX_MESSAGE_SENDING );
133134
tiny_events_set( &m_events, TX_QUEUE_FREE );
134-
// tiny_events_clear( &m_events, TX_MESSAGE_SENDING );
135-
// tiny_mutex_unlock( &m_sendMutex );
135+
}
136+
tiny_mutex_unlock( &m_sendMutex );
137+
if ( flush )
138+
{
139+
return 0;
136140
}
137141
return hdlc_ll_run_tx(m_handle, data, size);
138142
}

src/link/TinySerialLinkLayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ template <class BASE, int BSIZE> class ISerialLinkLayer: public BASE
8888
while ( len > 0 )
8989
{
9090
int sent = m_serial.write(ptr, len);
91-
if ( sent < 0 )
91+
if ( sent <= 0 )
9292
{
9393
break;
9494
}

0 commit comments

Comments
 (0)