@geastack/host 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +25 -0
- package/audio_runtime.cpp +807 -0
- package/bluetooth.cpp +125 -0
- package/geolocation.cpp +43 -0
- package/host/apps.cpp +20 -0
- package/host/audio.cpp +240 -0
- package/host/ble.cpp +239 -0
- package/host/camera.cpp +193 -0
- package/host/display.cpp +230 -0
- package/host/fetch.cpp +1060 -0
- package/host/geolocation.cpp +31 -0
- package/host/gpio.cpp +55 -0
- package/host/http.cpp +349 -0
- package/host/image.cpp +335 -0
- package/host/imu.cpp +51 -0
- package/host/input.cpp +12 -0
- package/host/media.cpp +579 -0
- package/host/memory.cpp +91 -0
- package/host/rtc.cpp +212 -0
- package/host/rtc_esp.cpp +291 -0
- package/host/tile_loader.cpp +478 -0
- package/host/timers.cpp +327 -0
- package/host/touch.cpp +25 -0
- package/host/websocket.cpp +279 -0
- package/host/wifi.cpp +99 -0
- package/include/gea/embedded-host.h +38 -0
- package/include/host/apps.h +15 -0
- package/include/host/audio.h +158 -0
- package/include/host/backends.h +455 -0
- package/include/host/battery.h +25 -0
- package/include/host/camera.h +236 -0
- package/include/host/clock.h +45 -0
- package/include/host/device_control.h +23 -0
- package/include/host/display.h +266 -0
- package/include/host/display_orientation.h +291 -0
- package/include/host/fetch.h +310 -0
- package/include/host/gpio.h +36 -0
- package/include/host/http.h +161 -0
- package/include/host/image.h +144 -0
- package/include/host/input.h +14 -0
- package/include/host/media.h +165 -0
- package/include/host/memory.h +44 -0
- package/include/host/navigator.h +283 -0
- package/include/host/notify.h +41 -0
- package/include/host/profiler.h +49 -0
- package/include/host/rtc.h +174 -0
- package/include/host/storage.h +141 -0
- package/include/host/tile_loader.h +59 -0
- package/include/host/timers.h +41 -0
- package/include/host/touch.h +20 -0
- package/include/host/websocket.h +179 -0
- package/include/host/window.h +31 -0
- package/input.cpp +68 -0
- package/package.json +27 -0
- package/services/app_state.cpp +19 -0
- package/services/battery_service.cpp +19 -0
- package/services/bluetooth_service.cpp +37 -0
- package/services/diagnostics.cpp +233 -0
- package/services/diagnostics_connection.cpp +231 -0
- package/services/diagnostics_internal.h +15 -0
- package/services/diagnostics_server.cpp +135 -0
- package/services/mirror.cpp +364 -0
- package/services/network_services.cpp +62 -0
- package/services/runtime_log.cpp +23 -0
- package/services/settings_surface.h +221 -0
- package/wifi.cpp +185 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "services/diagnostics.h"
|
|
3
|
+
|
|
4
|
+
#include "diagnostics_internal.h"
|
|
5
|
+
#include "memory.h"
|
|
6
|
+
#include "services/diagnostics_platform.h"
|
|
7
|
+
|
|
8
|
+
#include <cstdarg>
|
|
9
|
+
#include <cstddef>
|
|
10
|
+
#include <cstdio>
|
|
11
|
+
#include <mutex>
|
|
12
|
+
|
|
13
|
+
namespace gea::framework::services {
|
|
14
|
+
|
|
15
|
+
namespace {
|
|
16
|
+
|
|
17
|
+
// SPIRAM-backed ring that buffers log bytes between the producer (any task
|
|
18
|
+
// calling ESP_LOG) and the diagnostics network client, which drains it in
|
|
19
|
+
// 96-byte chunks per poll (see DiagnosticsConnection). It must comfortably hold
|
|
20
|
+
// the largest single burst — the ~1.5 KB per-frame perf line, written under one
|
|
21
|
+
// lock — plus slack for concurrent logs, or the client sees only the line's
|
|
22
|
+
// tail. 8 KB holds several perf lines of backlog at negligible SPIRAM cost.
|
|
23
|
+
constexpr std::size_t kLogRingSize = 8192;
|
|
24
|
+
constexpr int kDiagnosticsPort = 8081;
|
|
25
|
+
|
|
26
|
+
class DiagnosticsMemory {
|
|
27
|
+
public:
|
|
28
|
+
static void *allocate(std::size_t size)
|
|
29
|
+
{
|
|
30
|
+
return gea::framework::memory::Allocator::allocatePreferSpiram(size);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
class LogRing {
|
|
35
|
+
public:
|
|
36
|
+
bool begin()
|
|
37
|
+
{
|
|
38
|
+
std::lock_guard<std::mutex> guard(mutex_);
|
|
39
|
+
if (!data_) {
|
|
40
|
+
data_ = static_cast<char *>(DiagnosticsMemory::allocate(kLogRingSize));
|
|
41
|
+
if (!data_) {
|
|
42
|
+
std::fputs("Diagnostics log ring allocation failed; continuing without ring replay\n", stdout);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
head_ = 0;
|
|
47
|
+
used_ = 0;
|
|
48
|
+
total_ = 0;
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
void write(const char *data, std::size_t len)
|
|
53
|
+
{
|
|
54
|
+
if (!data || len == 0) return;
|
|
55
|
+
std::lock_guard<std::mutex> guard(mutex_);
|
|
56
|
+
if (!data_) return;
|
|
57
|
+
for (std::size_t i = 0; i < len; i++) {
|
|
58
|
+
data_[head_] = data[i];
|
|
59
|
+
head_ = (head_ + 1) % kLogRingSize;
|
|
60
|
+
if (used_ < kLogRingSize) used_++;
|
|
61
|
+
total_++;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
std::size_t oldestTotal()
|
|
66
|
+
{
|
|
67
|
+
std::lock_guard<std::mutex> guard(mutex_);
|
|
68
|
+
return oldestTotalLocked();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
std::size_t latestTotal()
|
|
72
|
+
{
|
|
73
|
+
std::lock_guard<std::mutex> guard(mutex_);
|
|
74
|
+
return total_;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
int copySince(std::size_t *cursor, char *dst, int cap)
|
|
78
|
+
{
|
|
79
|
+
if (!cursor || !dst || cap <= 0) return 0;
|
|
80
|
+
std::lock_guard<std::mutex> guard(mutex_);
|
|
81
|
+
if (!data_) return 0;
|
|
82
|
+
std::size_t oldest = oldestTotalLocked();
|
|
83
|
+
if (*cursor < oldest) *cursor = oldest;
|
|
84
|
+
std::size_t available = (total_ > *cursor) ? (total_ - *cursor) : 0;
|
|
85
|
+
if (available == 0) return 0;
|
|
86
|
+
std::size_t count = available > static_cast<std::size_t>(cap)
|
|
87
|
+
? static_cast<std::size_t>(cap)
|
|
88
|
+
: available;
|
|
89
|
+
for (std::size_t i = 0; i < count; i++) {
|
|
90
|
+
std::size_t absolute = *cursor + i;
|
|
91
|
+
dst[i] = data_[absolute % kLogRingSize];
|
|
92
|
+
}
|
|
93
|
+
*cursor += count;
|
|
94
|
+
return static_cast<int>(count);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private:
|
|
98
|
+
std::size_t oldestTotalLocked() const
|
|
99
|
+
{
|
|
100
|
+
return (total_ > used_) ? (total_ - used_) : 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
std::mutex mutex_;
|
|
104
|
+
char *data_ = nullptr;
|
|
105
|
+
std::size_t head_ = 0;
|
|
106
|
+
std::size_t used_ = 0;
|
|
107
|
+
std::size_t total_ = 0;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
LogRing &logRing()
|
|
111
|
+
{
|
|
112
|
+
static LogRing ring;
|
|
113
|
+
return ring;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
} // namespace
|
|
117
|
+
|
|
118
|
+
namespace diagnostics_internal {
|
|
119
|
+
|
|
120
|
+
bool beginLogRing()
|
|
121
|
+
{
|
|
122
|
+
return logRing().begin();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
void writeLog(const char *data, std::size_t len)
|
|
126
|
+
{
|
|
127
|
+
logRing().write(data, len);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
std::size_t oldestLogTotal()
|
|
131
|
+
{
|
|
132
|
+
return logRing().oldestTotal();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
std::size_t latestLogTotal()
|
|
136
|
+
{
|
|
137
|
+
return logRing().latestTotal();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
int copyLogSince(std::size_t *cursor, char *dst, int cap)
|
|
141
|
+
{
|
|
142
|
+
return logRing().copySince(cursor, dst, cap);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
} // namespace diagnostics_internal
|
|
146
|
+
|
|
147
|
+
bool DiagnosticsServer::begin()
|
|
148
|
+
{
|
|
149
|
+
return diagnostics_internal::beginLogRing();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
int DiagnosticsServer::port()
|
|
153
|
+
{
|
|
154
|
+
return kDiagnosticsPort;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
namespace {
|
|
158
|
+
|
|
159
|
+
// Format `fmt`/`args` and tee the result to the USB console (stdout) and the
|
|
160
|
+
// diagnostics log ring (streamed to a network client). The common short line
|
|
161
|
+
// formats into a stack buffer; anything longer — notably the ~1.5 KB per-frame
|
|
162
|
+
// perf line — re-formats into a heap buffer so the whole line, including its
|
|
163
|
+
// trailing newline, is emitted instead of being clipped at 255 bytes (which
|
|
164
|
+
// dropped the newline and ran consecutive log lines together). Returns the
|
|
165
|
+
// untruncated formatted length, per the esp_log_set_vprintf contract.
|
|
166
|
+
int formatAndEmitLog(const char *fmt, std::va_list args)
|
|
167
|
+
{
|
|
168
|
+
char stackBuf[256];
|
|
169
|
+
std::va_list argsCopy;
|
|
170
|
+
va_copy(argsCopy, args);
|
|
171
|
+
const int len = std::vsnprintf(stackBuf, sizeof(stackBuf), fmt, args);
|
|
172
|
+
if (len <= 0) {
|
|
173
|
+
va_end(argsCopy);
|
|
174
|
+
return len;
|
|
175
|
+
}
|
|
176
|
+
if (len < static_cast<int>(sizeof(stackBuf))) {
|
|
177
|
+
std::fwrite(stackBuf, 1, static_cast<std::size_t>(len), stdout);
|
|
178
|
+
diagnostics_internal::writeLog(stackBuf, static_cast<std::size_t>(len));
|
|
179
|
+
va_end(argsCopy);
|
|
180
|
+
return len;
|
|
181
|
+
}
|
|
182
|
+
const std::size_t size = static_cast<std::size_t>(len) + 1;
|
|
183
|
+
char *heapBuf = static_cast<char *>(gea::framework::memory::Allocator::allocatePreferSpiram(size));
|
|
184
|
+
if (heapBuf) {
|
|
185
|
+
std::vsnprintf(heapBuf, size, fmt, argsCopy);
|
|
186
|
+
std::fwrite(heapBuf, 1, static_cast<std::size_t>(len), stdout);
|
|
187
|
+
diagnostics_internal::writeLog(heapBuf, static_cast<std::size_t>(len));
|
|
188
|
+
gea::framework::memory::Allocator::free(heapBuf);
|
|
189
|
+
} else {
|
|
190
|
+
// Allocation failed: emit the clipped stack buffer rather than nothing.
|
|
191
|
+
std::fwrite(stackBuf, 1, sizeof(stackBuf) - 1, stdout);
|
|
192
|
+
diagnostics_internal::writeLog(stackBuf, sizeof(stackBuf) - 1);
|
|
193
|
+
}
|
|
194
|
+
va_end(argsCopy);
|
|
195
|
+
return len;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
} // namespace
|
|
199
|
+
|
|
200
|
+
int DiagnosticsServer::vprint(const char *fmt, std::va_list args)
|
|
201
|
+
{
|
|
202
|
+
return formatAndEmitLog(fmt, args);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
void DiagnosticsServer::print(const char *fmt, ...)
|
|
206
|
+
{
|
|
207
|
+
std::va_list args;
|
|
208
|
+
va_start(args, fmt);
|
|
209
|
+
formatAndEmitLog(fmt, args);
|
|
210
|
+
va_end(args);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
void DiagnosticsServer::start()
|
|
214
|
+
{
|
|
215
|
+
diagnostics_internal::startServer();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
void HeapProbe::log(const char *stage)
|
|
219
|
+
{
|
|
220
|
+
diagnosticsPlatform().logHeapProbe(stage);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
void StackProbe::logCurrentTask(const char *stage)
|
|
224
|
+
{
|
|
225
|
+
diagnosticsPlatform().logCurrentTaskStackProbe(stage);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
void HeapTaskSummary::log(const char *stage)
|
|
229
|
+
{
|
|
230
|
+
diagnosticsPlatform().logHeapTaskSummary(stage);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
} // namespace gea::framework::services
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "services/diagnostics.h"
|
|
3
|
+
|
|
4
|
+
#include "diagnostics_internal.h"
|
|
5
|
+
#include "services/mirror.h"
|
|
6
|
+
|
|
7
|
+
#include <cstddef>
|
|
8
|
+
#include <cstdint>
|
|
9
|
+
#include <cstring>
|
|
10
|
+
|
|
11
|
+
namespace gea::framework::services {
|
|
12
|
+
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
constexpr int kFrameHeaderSize = 4;
|
|
16
|
+
constexpr int kMaxFramePayload = 1024;
|
|
17
|
+
constexpr int kMaxFrameSize = kFrameHeaderSize + kMaxFramePayload;
|
|
18
|
+
constexpr int kChannelLog = 1;
|
|
19
|
+
constexpr int kBackpressureDelayMs = 250;
|
|
20
|
+
constexpr int kBackpressureLogIntervalMs = 5000;
|
|
21
|
+
|
|
22
|
+
class PendingFrame : public DiagnosticsFrame {
|
|
23
|
+
public:
|
|
24
|
+
bool hasPending() const { return len_ > offset_; }
|
|
25
|
+
bool empty() const override { return len_ == 0; }
|
|
26
|
+
int payloadCapacity() const override { return kMaxFramePayload; }
|
|
27
|
+
|
|
28
|
+
void clear()
|
|
29
|
+
{
|
|
30
|
+
len_ = 0;
|
|
31
|
+
offset_ = 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
unsigned char *payload() override
|
|
35
|
+
{
|
|
36
|
+
return data_ + kFrameHeaderSize;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
bool queue(int channel, int type, const unsigned char *payload, int payload_len) override
|
|
40
|
+
{
|
|
41
|
+
if (payload_len < 0) payload_len = 0;
|
|
42
|
+
if (payload_len > kMaxFramePayload) payload_len = kMaxFramePayload;
|
|
43
|
+
data_[0] = static_cast<unsigned char>(channel & 0xFF);
|
|
44
|
+
data_[1] = static_cast<unsigned char>(type & 0xFF);
|
|
45
|
+
data_[2] = static_cast<unsigned char>(payload_len & 0xFF);
|
|
46
|
+
data_[3] = static_cast<unsigned char>((payload_len >> 8) & 0xFF);
|
|
47
|
+
if (payload && payload_len > 0 && payload != data_ + kFrameHeaderSize) {
|
|
48
|
+
std::memmove(data_ + kFrameHeaderSize, payload, static_cast<std::size_t>(payload_len));
|
|
49
|
+
}
|
|
50
|
+
len_ = kFrameHeaderSize + payload_len;
|
|
51
|
+
offset_ = 0;
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
int flushTo(DiagnosticsOutput &output, int *err_out)
|
|
56
|
+
{
|
|
57
|
+
if (err_out) *err_out = 0;
|
|
58
|
+
if (len_ <= 0) return 1;
|
|
59
|
+
while (offset_ < len_) {
|
|
60
|
+
int written = output.write(data_ + offset_, len_ - offset_, err_out);
|
|
61
|
+
if (written < 0) return -1;
|
|
62
|
+
if (written == 0) return 0;
|
|
63
|
+
offset_ += written;
|
|
64
|
+
}
|
|
65
|
+
return 1;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private:
|
|
69
|
+
unsigned char data_[kMaxFrameSize] = {};
|
|
70
|
+
int len_ = 0;
|
|
71
|
+
int offset_ = 0;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
} // namespace
|
|
75
|
+
|
|
76
|
+
class DiagnosticsConnection::Impl {
|
|
77
|
+
public:
|
|
78
|
+
void reset()
|
|
79
|
+
{
|
|
80
|
+
mirror_.reset();
|
|
81
|
+
pending_.clear();
|
|
82
|
+
logCursor_ = diagnostics_internal::oldestLogTotal();
|
|
83
|
+
lastBackpressureLogAt_ = 0;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
bool hasPendingOutput() const
|
|
87
|
+
{
|
|
88
|
+
return pending_.hasPending();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
bool wantsImmediatePoll(std::int64_t now_us) const
|
|
92
|
+
{
|
|
93
|
+
if (pending_.hasPending()) return true;
|
|
94
|
+
#ifndef GEA_EMBEDDED_MIRROR_DISABLED
|
|
95
|
+
return mirror_.ready(now_us);
|
|
96
|
+
#else
|
|
97
|
+
(void)now_us;
|
|
98
|
+
return false;
|
|
99
|
+
#endif
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
int pollIntervalMs() const
|
|
103
|
+
{
|
|
104
|
+
return MirrorTransport::pollIntervalMs();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
int backpressureDelayMs() const
|
|
108
|
+
{
|
|
109
|
+
return kBackpressureDelayMs;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
void handleIncoming(const char *data, int len)
|
|
113
|
+
{
|
|
114
|
+
if (!data || len <= 0) return;
|
|
115
|
+
#ifndef GEA_EMBEDDED_MIRROR_DISABLED
|
|
116
|
+
for (int i = 0; i < len; i++) {
|
|
117
|
+
mirror_.handleCommand(data[i]);
|
|
118
|
+
}
|
|
119
|
+
#else
|
|
120
|
+
(void)data;
|
|
121
|
+
(void)len;
|
|
122
|
+
#endif
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
DiagnosticsSendStatus flush(DiagnosticsOutput &output, std::int64_t now_us, int *err_out)
|
|
126
|
+
{
|
|
127
|
+
if (!pending_.hasPending()) return DiagnosticsSendStatus::Idle;
|
|
128
|
+
int send_result = pending_.flushTo(output, err_out);
|
|
129
|
+
if (send_result > 0) {
|
|
130
|
+
pending_.clear();
|
|
131
|
+
return DiagnosticsSendStatus::Sent;
|
|
132
|
+
}
|
|
133
|
+
if (send_result == 0) {
|
|
134
|
+
int err = err_out ? *err_out : 0;
|
|
135
|
+
if (now_us - lastBackpressureLogAt_ >=
|
|
136
|
+
static_cast<std::int64_t>(kBackpressureLogIntervalMs) * 1000) {
|
|
137
|
+
DiagnosticsServer::print("Diagnostics send backpressure: errno=%d\n", err);
|
|
138
|
+
lastBackpressureLogAt_ = now_us;
|
|
139
|
+
}
|
|
140
|
+
return DiagnosticsSendStatus::Backpressure;
|
|
141
|
+
}
|
|
142
|
+
return DiagnosticsSendStatus::Closed;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
void prepareNextFrame(std::int64_t now_us)
|
|
146
|
+
{
|
|
147
|
+
if (pending_.hasPending()) return;
|
|
148
|
+
|
|
149
|
+
int forward_logs = 1;
|
|
150
|
+
#ifndef GEA_EMBEDDED_MIRROR_DISABLED
|
|
151
|
+
if (mirror_.suppressesLogForwarding()) forward_logs = 0;
|
|
152
|
+
#endif
|
|
153
|
+
if (forward_logs) {
|
|
154
|
+
char log_chunk[96];
|
|
155
|
+
int log_len = diagnostics_internal::copyLogSince(&logCursor_, log_chunk, sizeof(log_chunk));
|
|
156
|
+
if (log_len > 0) {
|
|
157
|
+
pending_.queue(kChannelLog, 1, reinterpret_cast<const unsigned char *>(log_chunk), log_len);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
logCursor_ = diagnostics_internal::latestLogTotal();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#ifndef GEA_EMBEDDED_MIRROR_DISABLED
|
|
165
|
+
mirror_.syncCurrentApp();
|
|
166
|
+
if (!mirror_.ensureBuffer(pending_)) return;
|
|
167
|
+
mirror_.beginIfDue(now_us, pending_);
|
|
168
|
+
mirror_.appendBatch(now_us, pending_);
|
|
169
|
+
#else
|
|
170
|
+
(void)now_us;
|
|
171
|
+
#endif
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private:
|
|
175
|
+
MirrorTransport mirror_;
|
|
176
|
+
PendingFrame pending_;
|
|
177
|
+
std::size_t logCursor_ = 0;
|
|
178
|
+
std::int64_t lastBackpressureLogAt_ = 0;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
DiagnosticsConnection::DiagnosticsConnection()
|
|
182
|
+
: impl_(new Impl())
|
|
183
|
+
{
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
DiagnosticsConnection::~DiagnosticsConnection()
|
|
187
|
+
{
|
|
188
|
+
delete impl_;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
void DiagnosticsConnection::reset()
|
|
192
|
+
{
|
|
193
|
+
impl_->reset();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
bool DiagnosticsConnection::hasPendingOutput() const
|
|
197
|
+
{
|
|
198
|
+
return impl_->hasPendingOutput();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
bool DiagnosticsConnection::wantsImmediatePoll(std::int64_t now_us) const
|
|
202
|
+
{
|
|
203
|
+
return impl_->wantsImmediatePoll(now_us);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
int DiagnosticsConnection::pollIntervalMs() const
|
|
207
|
+
{
|
|
208
|
+
return impl_->pollIntervalMs();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
int DiagnosticsConnection::backpressureDelayMs() const
|
|
212
|
+
{
|
|
213
|
+
return impl_->backpressureDelayMs();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
void DiagnosticsConnection::handleIncoming(const char *data, int len)
|
|
217
|
+
{
|
|
218
|
+
impl_->handleIncoming(data, len);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
DiagnosticsSendStatus DiagnosticsConnection::flush(DiagnosticsOutput &output, std::int64_t now_us, int *err_out)
|
|
222
|
+
{
|
|
223
|
+
return impl_->flush(output, now_us, err_out);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
void DiagnosticsConnection::prepareNextFrame(std::int64_t now_us)
|
|
227
|
+
{
|
|
228
|
+
impl_->prepareNextFrame(now_us);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
} // namespace gea::framework::services
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstddef>
|
|
5
|
+
|
|
6
|
+
namespace gea::framework::services::diagnostics_internal {
|
|
7
|
+
|
|
8
|
+
bool beginLogRing();
|
|
9
|
+
void writeLog(const char *data, std::size_t len);
|
|
10
|
+
std::size_t oldestLogTotal();
|
|
11
|
+
std::size_t latestLogTotal();
|
|
12
|
+
int copyLogSince(std::size_t *cursor, char *dst, int cap);
|
|
13
|
+
void startServer();
|
|
14
|
+
|
|
15
|
+
} // namespace gea::framework::services::diagnostics_internal
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "services/diagnostics.h"
|
|
3
|
+
|
|
4
|
+
#include "diagnostics_internal.h"
|
|
5
|
+
#include "services/diagnostics_platform.h"
|
|
6
|
+
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
|
|
9
|
+
namespace gea::framework::services {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
class DiagnosticsRuntime {
|
|
14
|
+
public:
|
|
15
|
+
static DiagnosticsRuntime &instance()
|
|
16
|
+
{
|
|
17
|
+
static DiagnosticsRuntime runtime;
|
|
18
|
+
return runtime;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
void start()
|
|
22
|
+
{
|
|
23
|
+
if (started_) return;
|
|
24
|
+
if (!DiagnosticsServer::begin()) return;
|
|
25
|
+
DiagnosticsPlatform &platform = diagnosticsPlatform();
|
|
26
|
+
platform.installLogSink(DiagnosticsServer::vprint);
|
|
27
|
+
started_ = true;
|
|
28
|
+
platform.startTask(&DiagnosticsRuntime::taskMain, this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private:
|
|
32
|
+
DiagnosticsRuntime() = default;
|
|
33
|
+
|
|
34
|
+
void run()
|
|
35
|
+
{
|
|
36
|
+
DiagnosticsPlatform &platform = diagnosticsPlatform();
|
|
37
|
+
DiagnosticsListener &listener = platform.listener();
|
|
38
|
+
if (!listener.listen(DiagnosticsServer::port())) {
|
|
39
|
+
platform.logError("Diagnostics server failed on port %d", DiagnosticsServer::port());
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
platform.logInfo("Diagnostics transport listening on port %d", DiagnosticsServer::port());
|
|
44
|
+
|
|
45
|
+
DiagnosticsPeer *peer = nullptr;
|
|
46
|
+
DiagnosticsConnection connection;
|
|
47
|
+
|
|
48
|
+
while (true) {
|
|
49
|
+
std::int64_t now_us = platform.nowUs();
|
|
50
|
+
bool immediate = peer && connection.wantsImmediatePoll(now_us);
|
|
51
|
+
int timeout_ms = immediate ? 0 : connection.pollIntervalMs();
|
|
52
|
+
int wait_errno = 0;
|
|
53
|
+
int events = listener.wait(peer, peer && connection.hasPendingOutput(), timeout_ms, &wait_errno);
|
|
54
|
+
if (events < 0) {
|
|
55
|
+
platform.logWarn("Diagnostics wait failed: errno=%d", wait_errno);
|
|
56
|
+
platform.sleepMs(connection.pollIntervalMs());
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (events & kDiagnosticsWaitListenerReadable) {
|
|
61
|
+
DiagnosticsPeer *next_peer = listener.accept();
|
|
62
|
+
if (next_peer) {
|
|
63
|
+
if (peer) {
|
|
64
|
+
peer->close("replaced by a new connection", 0);
|
|
65
|
+
delete peer;
|
|
66
|
+
}
|
|
67
|
+
peer = next_peer;
|
|
68
|
+
connection.reset();
|
|
69
|
+
platform.logInfo("Diagnostics client connected");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (peer && (events & kDiagnosticsWaitPeerReadable)) {
|
|
74
|
+
char incoming[16];
|
|
75
|
+
int read_errno = 0;
|
|
76
|
+
int received = peer->read(incoming, sizeof(incoming), &read_errno);
|
|
77
|
+
if (received == kDiagnosticsIoWouldBlock) {
|
|
78
|
+
// No payload after select; keep the peer and continue polling.
|
|
79
|
+
} else if (received == 0) {
|
|
80
|
+
peer->close("disconnected", 0);
|
|
81
|
+
delete peer;
|
|
82
|
+
peer = nullptr;
|
|
83
|
+
connection.reset();
|
|
84
|
+
} else if (received < 0) {
|
|
85
|
+
peer->close("closed after receive error", read_errno);
|
|
86
|
+
delete peer;
|
|
87
|
+
peer = nullptr;
|
|
88
|
+
connection.reset();
|
|
89
|
+
} else {
|
|
90
|
+
connection.handleIncoming(incoming, received);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!peer) continue;
|
|
95
|
+
|
|
96
|
+
if (connection.hasPendingOutput()) {
|
|
97
|
+
int send_errno = 0;
|
|
98
|
+
DiagnosticsSendStatus send_status = connection.flush(*peer, platform.nowUs(), &send_errno);
|
|
99
|
+
if (send_status == DiagnosticsSendStatus::Backpressure) {
|
|
100
|
+
platform.sleepMs(connection.backpressureDelayMs());
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (send_status == DiagnosticsSendStatus::Closed) {
|
|
104
|
+
peer->close("closed after send error", send_errno);
|
|
105
|
+
delete peer;
|
|
106
|
+
peer = nullptr;
|
|
107
|
+
connection.reset();
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
connection.prepareNextFrame(platform.nowUs());
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static void taskMain(void *context)
|
|
117
|
+
{
|
|
118
|
+
static_cast<DiagnosticsRuntime *>(context)->run();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
bool started_ = false;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
} // namespace
|
|
125
|
+
|
|
126
|
+
namespace diagnostics_internal {
|
|
127
|
+
|
|
128
|
+
void startServer()
|
|
129
|
+
{
|
|
130
|
+
DiagnosticsRuntime::instance().start();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
} // namespace diagnostics_internal
|
|
134
|
+
|
|
135
|
+
} // namespace gea::framework::services
|