Skip to content

Commit a4ddc3d

Browse files
committed
fix: Monitor read must expect bytes not string. impr: Monitor internal buffer thread protected
1 parent 9f48f56 commit a4ddc3d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/monitor.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ class BridgeMonitor: public Stream {
1919
private:
2020
BridgeClass& bridge;
2121
RingBufferN<BufferSize> temp_buffer;
22+
struct k_mutex monitor_mutex;
2223
bool is_connected = false;
2324

2425
public:
2526
BridgeMonitor(BridgeClass& bridge): bridge(bridge) {}
2627

2728
bool begin() {
2829
return bridge.call(MON_CONNECTED_METHOD, is_connected);
30+
k_mutex_init(&monitor_mutex);
2931
}
3032

3133
bool isConnected() const {
@@ -39,23 +41,30 @@ class BridgeMonitor: public Stream {
3941
}
4042

4143
int read(uint8_t* buffer, size_t size) {
44+
k_mutex_lock(&monitor_mutex, K_FOREVER);
4245
int i = 0;
4346
while (temp_buffer.available() && i < size) {
4447
buffer[i++] = temp_buffer.read_char();
4548
}
49+
k_mutex_unlock(&monitor_mutex);
4650
return i;
4751
}
4852

4953
int available() override {
54+
k_mutex_lock(&monitor_mutex, K_FOREVER);
5055
int size = temp_buffer.availableForStore();
5156
if (size > 0) _read(size);
52-
return temp_buffer.available();
57+
int available = temp_buffer.available();
58+
k_mutex_unlock(&monitor_mutex);
59+
return available;
5360
}
5461

5562
int peek() override {
63+
k_mutex_lock(&monitor_mutex, K_FOREVER);
5664
if (temp_buffer.available()) {
5765
return temp_buffer.peek();
5866
}
67+
k_mutex_unlock(&monitor_mutex);
5968
}
6069

6170
size_t write(uint8_t c) override {
@@ -105,20 +114,21 @@ class BridgeMonitor: public Stream {
105114

106115
if (size == 0) return 0;
107116

108-
MsgPack::str_t message;
117+
MsgPack::arr_t<uint8_t> message;
109118
bool ret = bridge.call(MON_READ_METHOD, message, size);
110119

120+
k_mutex_lock(&monitor_mutex, K_FOREVER);
111121
if (ret) {
112-
for (size_t i = 0; i < message.length(); ++i) {
113-
temp_buffer.store_char(message[i]);
122+
for (size_t i = 0; i < message.size(); ++i) {
123+
temp_buffer.store_char(static_cast<char>(message[i]));
114124
}
115-
return message.length();
125+
return message.size();
116126
}
117127

118128
// if (bridge.lastError.code > NO_ERR) {
119129
// is_connected = false;
120130
// }
121-
131+
k_mutex_unlock(&monitor_mutex);
122132
return 0;
123133

124134
}

0 commit comments

Comments
 (0)