Skip to content

Commit 4368f75

Browse files
committed
impr: bridge cleanup and get_last_client_error method
fix: monitor trying to connect even if bridge begin fails + monitor._read is private mod: implementation is Arduino only, better mux usage
1 parent f179f14 commit 4368f75

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/bridge.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class BridgeClass {
3535
HardwareSerial* serial_ptr = nullptr;
3636
ITransport* transport = nullptr;
3737

38-
struct k_mutex read_mutex;
39-
struct k_mutex write_mutex;
38+
struct k_mutex read_mutex{};
39+
struct k_mutex write_mutex{};
4040

41-
k_tid_t upd_tid;
42-
k_thread_stack_t *upd_stack_area;
43-
struct k_thread upd_thread_data;
41+
k_tid_t upd_tid{};
42+
k_thread_stack_t *upd_stack_area{};
43+
struct k_thread upd_thread_data{};
4444

4545
bool started = false;
4646

@@ -50,7 +50,7 @@ class BridgeClass {
5050
serial_ptr = &serial;
5151
}
5252

53-
explicit operator bool() const {
53+
operator bool() const {
5454
return started;
5555
}
5656

@@ -133,7 +133,7 @@ class BridgeClass {
133133
}
134134

135135
template<typename RType, typename... Args>
136-
bool call(const MsgPack::str_t method, RType& result, Args&&... args) {
136+
bool call(const MsgPack::str_t& method, RType& result, Args&&... args) {
137137

138138
uint32_t msg_id_wait;
139139

@@ -182,6 +182,10 @@ class BridgeClass {
182182
return static_cast<uint8_t>(client->lastError.code);
183183
}
184184

185+
RpcError& get_last_client_error() const {
186+
return client->lastError;
187+
}
188+
185189
private:
186190

187191
void update_safe() {

src/monitor.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
template<size_t BufferSize=DEFAULT_MONITOR_BUF_SIZE>
2828
class BridgeMonitor: public Stream {
2929

30-
private:
3130
BridgeClass* bridge;
3231
RingBufferN<BufferSize> temp_buffer;
33-
struct k_mutex monitor_mutex;
32+
struct k_mutex monitor_mutex{};
3433
bool is_connected = false;
3534

3635
public:
@@ -40,10 +39,12 @@ class BridgeMonitor: public Stream {
4039

4140
bool begin() {
4241
k_mutex_init(&monitor_mutex);
43-
if (!(*bridge)) {
44-
bridge->begin();
42+
43+
bool bridge_started = (*bridge);
44+
if (!bridge_started) {
45+
bridge_started = bridge->begin();
4546
}
46-
return bridge->call(MON_CONNECTED_METHOD, is_connected);
47+
return bridge_started && bridge->call(MON_CONNECTED_METHOD, is_connected);
4748
}
4849

4950
explicit operator bool() const {
@@ -91,18 +92,14 @@ class BridgeMonitor: public Stream {
9192

9293
size_t write(const uint8_t* buffer, size_t size) override {
9394

94-
MsgPack::str_t send_buffer;
95+
String send_buffer;
9596

9697
for (size_t i = 0; i < size; ++i) {
97-
#ifdef ARDUINO
9898
send_buffer += static_cast<char>(buffer[i]);
99-
#else
100-
send_buffer.push_back(static_cast<char>(buffer[i]));
101-
#endif
10299
}
103100

104101
size_t written;
105-
bool ret = bridge->call(MON_WRITE_METHOD, written, send_buffer);
102+
const bool ret = bridge->call(MON_WRITE_METHOD, written, send_buffer);
106103
if (ret) {
107104
return written;
108105
}
@@ -119,14 +116,16 @@ class BridgeMonitor: public Stream {
119116
return (ok && res);
120117
}
121118

119+
private:
122120
void _read(size_t size) {
123121

124122
if (size == 0) return;
125123

124+
k_mutex_lock(&monitor_mutex, K_FOREVER);
125+
126126
MsgPack::arr_t<uint8_t> message;
127127
bool ret = bridge->call(MON_READ_METHOD, message, size);
128128

129-
k_mutex_lock(&monitor_mutex, K_FOREVER);
130129
if (ret) {
131130
for (size_t i = 0; i < message.size(); ++i) {
132131
temp_buffer.store_char(static_cast<char>(message[i]));

0 commit comments

Comments
 (0)