Skip to content

Commit 2a4d148

Browse files
committed
Added HAP session debug info
To assist with investigating #129
1 parent ed0ac6f commit 2a4d148

File tree

3 files changed

+67
-27
lines changed

3 files changed

+67
-27
lines changed

src/shelly_debug.cpp

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,32 @@
3232
#include "HAPAccessoryServer+Internal.h"
3333
#include "HAPPlatformTCPStreamManager+Init.h"
3434

35+
#include "shelly_main.hpp"
36+
3537
namespace shelly {
3638

39+
static HAPAccessoryServerRef s_svr;
3740
static HAPPlatformKeyValueStoreRef s_kvs;
3841
static HAPPlatformTCPStreamManagerRef s_tcpm;
3942

43+
struct EnumHAPSessionsContext {
44+
struct mg_connection *nc;
45+
int num_sessions;
46+
};
47+
48+
static void EnumHAPSessions(void *vctx, HAPAccessoryServerRef *svr_,
49+
HAPSessionRef *s, bool *) {
50+
EnumHAPSessionsContext *ctx = (EnumHAPSessionsContext *) vctx;
51+
size_t si = HAPAccessoryServerGetIPSessionIndex(svr_, s);
52+
const HAPAccessoryServer *svr = (const HAPAccessoryServer *) svr_;
53+
const HAPIPSession *is = &svr->ip.storage->sessions[si];
54+
const auto *sd = (const HAPIPSessionDescriptor *) &is->descriptor;
55+
mg_printf(ctx->nc, " %d: s %p ts %p o %d st %d ts %lu\r\n", (int) si, s,
56+
(void *) sd->tcpStream, sd->tcpStreamIsOpen, sd->state,
57+
(unsigned long) sd->stamp);
58+
ctx->num_sessions++;
59+
}
60+
4061
void shelly_debug_write_nc(struct mg_connection *nc) {
4162
uint16_t cn;
4263
if (HAPAccessoryServerGetCN(s_kvs, &cn) != kHAPError_None) {
@@ -48,41 +69,51 @@ void shelly_debug_write_nc(struct mg_connection *nc) {
4869
"App: %s %s %s\r\n"
4970
"Uptime: %.2lf\r\n"
5071
"RAM: %lu free, %lu min free\r\n"
72+
"HAP server port: %d\r\n"
5173
"HAP config number: %u\r\n"
5274
"HAP connection stats: %u/%u/%u\r\n",
5375
MGOS_APP, mgos_sys_ro_vars_get_fw_version(),
5476
mgos_sys_ro_vars_get_fw_id(), mgos_uptime(),
5577
(unsigned long) mgos_get_free_heap_size(),
56-
(unsigned long) mgos_get_min_free_heap_size(), cn,
78+
(unsigned long) mgos_get_min_free_heap_size(),
79+
HAPPlatformTCPStreamManagerGetListenerPort(s_tcpm), cn,
5780
(unsigned) tcpm_stats.numPendingTCPStreams,
5881
(unsigned) tcpm_stats.numActiveTCPStreams,
5982
(unsigned) tcpm_stats.maxNumTCPStreams);
6083
mg_printf(nc, "HAP connections:\r\n");
6184
time_t now_wall = mg_time();
6285
int64_t now_micros = mgos_uptime_micros();
63-
int num_hap_connections = 0;
64-
struct mg_connection *nc2 = NULL;
65-
struct mg_mgr *mgr = mgos_get_mgr();
66-
for (nc2 = mg_next(mgr, NULL); nc2 != NULL; nc2 = mg_next(mgr, nc2)) {
67-
if (nc2->listener == NULL ||
68-
ntohs(nc2->listener->sa.sin.sin_port) != 9000) {
69-
continue;
70-
}
71-
char addr[32];
72-
mg_sock_addr_to_str(&nc2->sa, addr, sizeof(addr),
73-
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
74-
int last_io_age = (int) (now_wall - nc2->last_io_time);
75-
int64_t last_read_age = 0;
76-
HAPPlatformTCPStream *ts = (HAPPlatformTCPStream *) nc2->user_data;
77-
if (ts != nullptr) {
78-
last_read_age = now_micros - ts->lastRead;
86+
{
87+
int num_hap_connections = 0;
88+
struct mg_connection *nc2 = NULL;
89+
struct mg_mgr *mgr = mgos_get_mgr();
90+
for (nc2 = mg_next(mgr, NULL); nc2 != NULL; nc2 = mg_next(mgr, nc2)) {
91+
if (nc2->listener == NULL ||
92+
ntohs(nc2->listener->sa.sin.sin_port) != 9000) {
93+
continue;
94+
}
95+
char addr[32];
96+
mg_sock_addr_to_str(&nc2->sa, addr, sizeof(addr),
97+
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
98+
int last_io_age = (int) (now_wall - nc2->last_io_time);
99+
int64_t last_read_age = 0;
100+
HAPPlatformTCPStream *ts = (HAPPlatformTCPStream *) nc2->user_data;
101+
if (ts != nullptr) {
102+
last_read_age = now_micros - ts->lastRead;
103+
}
104+
mg_printf(nc, " %s nc %pf %#lx io %d ts %p rd %lld\r\n", addr, nc2,
105+
(unsigned long) nc2->flags, last_io_age, ts,
106+
(long long) (last_read_age / 1000000));
107+
num_hap_connections++;
79108
}
80-
mg_printf(nc, " %s nc %pf %#lx io %d ts %p rd %lld\r\n", addr, nc2,
81-
(unsigned long) nc2->flags, last_io_age, ts,
82-
(long long) (last_read_age / 1000000));
83-
num_hap_connections++;
109+
mg_printf(nc, " Total: %d\r\n", num_hap_connections);
110+
}
111+
{
112+
mg_printf(nc, "HAP sessions:\r\n");
113+
EnumHAPSessionsContext ctx = {.nc = nc, .num_sessions = 0};
114+
HAPAccessoryServerEnumerateConnectedSessions(&s_svr, EnumHAPSessions, &ctx);
115+
mg_printf(nc, " Total: %d\r\n", ctx.num_sessions);
84116
}
85-
mg_printf(nc, " Total: %d", num_hap_connections);
86117
}
87118

88119
void GetDebugInfo(std::string *out) {
@@ -236,8 +267,9 @@ static void DebugCoreHandler(struct mg_connection *nc, int ev, void *ev_data,
236267
}
237268
#endif
238269

239-
bool DebugInit(HAPPlatformKeyValueStoreRef kvs,
270+
bool DebugInit(HAPAccessoryServerRef svr, HAPPlatformKeyValueStoreRef kvs,
240271
HAPPlatformTCPStreamManagerRef tcpm) {
272+
s_svr = svr;
241273
s_kvs = kvs;
242274
s_tcpm = tcpm;
243275
mgos_register_http_endpoint("/debug/info", DebugInfoHandler, NULL);

src/shelly_debug.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void GetDebugInfo(std::string *out);
2525

2626
void SetDebugEnable(bool debug_en);
2727

28-
bool DebugInit(HAPPlatformKeyValueStoreRef kvs,
28+
bool DebugInit(HAPAccessoryServerRef svr, HAPPlatformKeyValueStoreRef kvs,
2929
HAPPlatformTCPStreamManagerRef tcpm);
3030

3131
} // namespace shelly

src/shelly_main.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,23 @@ static void CheckLED(int pin, bool led_act) {
455455
}
456456
}
457457

458+
void CountHAPSessions(void *ctx, HAPAccessoryServerRef *, HAPSessionRef *,
459+
bool *) {
460+
(*((int *) ctx))++;
461+
}
462+
458463
static void StatusTimerCB(void *arg) {
459464
static uint8_t s_cnt = 0;
460465
if (++s_cnt % 8 == 0) {
461466
HAPPlatformTCPStreamManagerStats tcpm_stats = {};
462467
HAPPlatformTCPStreamManagerGetStats(&s_tcpm, &tcpm_stats);
463-
LOG(LL_INFO, ("Uptime: %.2lf, conns %u/%u/%u, RAM: %lu, %lu free",
468+
int num_sessions = 0;
469+
HAPAccessoryServerEnumerateConnectedSessions(&s_server, CountHAPSessions,
470+
&num_sessions);
471+
LOG(LL_INFO, ("Uptime: %.2lf, conns %u/%u/%u ns %d, RAM: %lu, %lu free",
464472
mgos_uptime(), (unsigned) tcpm_stats.numPendingTCPStreams,
465473
(unsigned) tcpm_stats.numActiveTCPStreams,
466-
(unsigned) tcpm_stats.maxNumTCPStreams,
474+
(unsigned) tcpm_stats.maxNumTCPStreams, num_sessions,
467475
(unsigned long) mgos_get_heap_size(),
468476
(unsigned long) mgos_get_free_heap_size()));
469477
s_cnt = 0;
@@ -669,7 +677,7 @@ bool InitApp() {
669677

670678
shelly_rpc_service_init(&s_server, &s_kvs, &s_tcpm);
671679

672-
DebugInit(&s_kvs, &s_tcpm);
680+
DebugInit(s_server, &s_kvs, &s_tcpm);
673681

674682
mgos_event_add_handler(MGOS_EVENT_REBOOT, RebootCB, nullptr);
675683
mgos_event_add_handler(MGOS_EVENT_REBOOT_AFTER, RebootCB, nullptr);

0 commit comments

Comments
 (0)