Skip to content

Commit d0e11c9

Browse files
committed
Restore ubuntu build, add Shelly.GetDebugInfo
1 parent 3bfd30c commit d0e11c9

File tree

6 files changed

+101
-36
lines changed

6 files changed

+101
-36
lines changed

mos.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
author: 'Deomid "rojer" Ryabkov'
22
description: A HomeKit firmware for Shelly switches
3-
version: 2.0.8
3+
version: 2.0.10
44
platform: esp8266
55

66
libs_version: latest
@@ -59,7 +59,6 @@ libs:
5959
- origin: https://github.com/mongoose-os-libs/mqtt
6060
- origin: https://github.com/mongoose-os-libs/rpc-service-config
6161
- origin: https://github.com/mongoose-os-libs/rpc-service-fs
62-
- origin: https://github.com/mongoose-os-libs/rpc-service-ota
6362
- origin: https://github.com/mongoose-os-libs/rpc-uart
6463
- origin: https://github.com/mongoose-os-libs/http-server
6564
- origin: https://github.com/mongoose-os-libs/rpc-mqtt

src/shelly_debug.cpp

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,87 @@
1717

1818
#include "shelly_debug.h"
1919

20+
#include "mgos.h"
2021
#include "mgos_http_server.h"
2122

2223
#include "HAPAccessoryServer+Internal.h"
24+
#include "HAPPlatformTCPStreamManager+Init.h"
2325

2426
static HAPPlatformKeyValueStoreRef s_kvs;
27+
static HAPPlatformTCPStreamManagerRef s_tcpm;
2528

26-
static void shelly_debug_handler(struct mg_connection *nc, int ev,
27-
void *ev_data, void *user_data) {
28-
if (ev != MG_EV_HTTP_REQUEST) return;
29+
void shelly_debug_write_nc(struct mg_connection *nc) {
2930
uint16_t cn;
3031
if (HAPAccessoryServerGetCN(s_kvs, &cn) != kHAPError_None) {
3132
cn = 0;
3233
}
33-
mg_send_response_line(nc, 200,
34-
"Content-Type: text/html\r\n"
35-
"Connection: close\r\n");
36-
mg_printf(nc, "<pre>\r\n");
37-
mg_printf(nc, "Config number: %u\r\n", cn);
34+
HAPPlatformTCPStreamManagerStats tcpm_stats = {};
35+
HAPPlatformTCPStreamManagerGetStats(s_tcpm, &tcpm_stats);
36+
mg_printf(nc,
37+
"App: %s %s %s\r\n"
38+
"Uptime: %.2lf\r\n"
39+
"RAM: %lu free, %lu min free\r\n"
40+
"HAP config number: %u\r\n"
41+
"HAP connection stats: %u/%u/%u\r\n",
42+
MGOS_APP, mgos_sys_ro_vars_get_fw_version(),
43+
mgos_sys_ro_vars_get_fw_id(), mgos_uptime(),
44+
(unsigned long) mgos_get_free_heap_size(),
45+
(unsigned long) mgos_get_min_free_heap_size(), cn,
46+
(unsigned) tcpm_stats.numPendingTCPStreams,
47+
(unsigned) tcpm_stats.numActiveTCPStreams,
48+
(unsigned) tcpm_stats.maxNumTCPStreams);
3849
mg_printf(nc, "HAP connections:\r\n");
39-
time_t now = mg_time();
50+
time_t now_wall = mg_time();
51+
int64_t now_micros = mgos_uptime_micros();
4052
int num_hap_connections = 0;
4153
struct mg_connection *nc2 = NULL;
42-
for (nc2 = mg_next(nc->mgr, NULL); nc2 != NULL; nc2 = mg_next(nc->mgr, nc2)) {
54+
struct mg_mgr *mgr = mgos_get_mgr();
55+
for (nc2 = mg_next(mgr, NULL); nc2 != NULL; nc2 = mg_next(mgr, nc2)) {
4356
if (nc2->listener == NULL ||
4457
ntohs(nc2->listener->sa.sin.sin_port) != 9000) {
4558
continue;
4659
}
4760
char addr[32];
4861
mg_sock_addr_to_str(&nc2->sa, addr, sizeof(addr),
4962
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
50-
mg_printf(nc, " %s last_io %d\r\n", addr, (int) (now - nc2->last_io_time));
63+
int last_io_age = (int) (now_wall - nc2->last_io_time);
64+
int64_t last_read_age = 0;
65+
HAPPlatformTCPStream *ts = (HAPPlatformTCPStream *) nc2->user_data;
66+
if (ts != nullptr) {
67+
last_read_age = now_micros - ts->lastRead;
68+
}
69+
mg_printf(nc, " %s nc %pf %#lx io %d ts %p rd %lld\r\n", addr, nc2,
70+
(unsigned long) nc2->flags, last_io_age, ts,
71+
(long long) (last_read_age / 1000000));
5172
num_hap_connections++;
5273
}
5374
mg_printf(nc, " Total: %d", num_hap_connections);
75+
}
5476

77+
void shelly_get_debug_info(std::string *out) {
78+
struct mg_connection nc = {};
79+
shelly_debug_write_nc(&nc);
80+
*out = std::string(nc.send_mbuf.buf, nc.send_mbuf.len);
81+
mbuf_free(&nc.send_mbuf);
82+
}
83+
84+
static void shelly_debug_handler(struct mg_connection *nc, int ev,
85+
void *ev_data, void *user_data) {
86+
if (ev != MG_EV_HTTP_REQUEST) return;
87+
mg_send_response_line(nc, 200,
88+
"Content-Type: text/html\r\n"
89+
"Connection: close\r\n");
90+
mg_printf(nc, "<pre>\r\n");
91+
shelly_debug_write_nc(nc);
5592
nc->flags |= MG_F_SEND_AND_CLOSE;
5693
(void) ev_data;
5794
(void) user_data;
5895
}
5996

60-
bool shelly_debug_init(HAPPlatformKeyValueStoreRef kvs) {
97+
bool shelly_debug_init(HAPPlatformKeyValueStoreRef kvs,
98+
HAPPlatformTCPStreamManagerRef tcpm) {
6199
s_kvs = kvs;
100+
s_tcpm = tcpm;
62101
mgos_register_http_endpoint("/debug/", shelly_debug_handler, NULL);
63102
return true;
64103
}

src/shelly_debug.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <string>
19+
1820
#include "HAP.h"
1921

20-
bool shelly_debug_init(HAPPlatformKeyValueStoreRef kvs);
22+
void shelly_get_debug_info(std::string *out);
23+
24+
bool shelly_debug_init(HAPPlatformKeyValueStoreRef kvs,
25+
HAPPlatformTCPStreamManagerRef tcpm);

src/shelly_main.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,18 @@ static HAPAccessoryServerOptions s_server_options = {
7272
.ip =
7373
{
7474
.transport = &kHAPAccessoryServerTransport_IP,
75-
.available = 0,
7675
.accessoryServerStorage = &s_ip_storage,
7776
},
7877
.ble =
7978
{
8079
.transport = nullptr,
81-
.available = false,
8280
.accessoryServerStorage = nullptr,
8381
.preferredAdvertisingInterval = 0,
8482
.preferredNotificationDuration = 0,
8583
},
8684
};
8785
static HAPAccessoryServerCallbacks s_callbacks;
88-
static HAPPlatformTCPStreamManager s_tcp_stream_manager;
86+
static HAPPlatformTCPStreamManager s_tcpm;
8987
static HAPPlatformServiceDiscovery s_service_discovery;
9088
static HAPAccessoryServerRef s_server;
9189

@@ -96,7 +94,7 @@ static HAPPlatform s_platform = {
9694
.setupNFC = NULL,
9795
.ip =
9896
{
99-
.tcpStreamManager = &s_tcp_stream_manager,
97+
.tcpStreamManager = &s_tcpm,
10098
.serviceDiscovery = &s_service_discovery,
10199
},
102100
.ble =
@@ -275,7 +273,7 @@ struct mgos_ade7953 *s_ade7953 = NULL;
275273

276274
static void shelly_status_timer_cb(void *arg) {
277275
HAPPlatformTCPStreamManagerStats tcpm_stats = {};
278-
HAPPlatformTCPStreamManagerGetStats(&s_tcp_stream_manager, &tcpm_stats);
276+
HAPPlatformTCPStreamManagerGetStats(&s_tcpm, &tcpm_stats);
279277
LOG(LL_INFO, ("Uptime: %.2lf, conns %u/%u/%u, RAM: %lu, %lu free",
280278
mgos_uptime(), (unsigned) tcpm_stats.numPendingTCPStreams,
281279
(unsigned) tcpm_stats.numActiveTCPStreams,
@@ -302,13 +300,16 @@ static void shelly_status_timer_cb(void *arg) {
302300
check_led(LED_GPIO, LED_ON);
303301
#ifdef MGOS_HAVE_OTA_COMMON
304302
// If committed, set up inactive app slot as location for core dumps.
303+
static bool s_cd_area_set = false;
305304
struct mgos_ota_status ota_status;
306-
if (mgos_ota_is_committed() && mgos_ota_get_status(&ota_status)) {
305+
if (!s_cd_area_set && mgos_ota_is_committed() &&
306+
mgos_ota_get_status(&ota_status)) {
307307
rboot_config bcfg = rboot_get_config();
308308
int cd_slot = (ota_status.partition == 0 ? 1 : 0);
309309
uint32_t cd_addr = bcfg.roms[cd_slot];
310310
uint32_t cd_size = bcfg.roms_sizes[cd_slot];
311311
esp_core_dump_set_flash_area(cd_addr, cd_size);
312+
s_cd_area_set = true;
312313
}
313314
#endif
314315
(void) arg;
@@ -419,7 +420,7 @@ bool shelly_app_init() {
419420
.port = kHAPNetworkPort_Any,
420421
.maxConcurrentTCPStreams = NUM_SESSIONS,
421422
};
422-
HAPPlatformTCPStreamManagerCreate(&s_tcp_stream_manager, &tcpm_opts);
423+
HAPPlatformTCPStreamManagerCreate(&s_tcpm, &tcpm_opts);
423424

424425
// Service discovery.
425426
static const HAPPlatformServiceDiscoveryOptions sd_opts = {};
@@ -486,9 +487,9 @@ bool shelly_app_init() {
486487
mgos_gpio_setup_input(BTN_GPIO, MGOS_GPIO_PULL_UP);
487488
}
488489

489-
shelly_rpc_service_init(&s_server, &s_kvs, &s_tcp_stream_manager);
490+
shelly_rpc_service_init(&s_server, &s_kvs, &s_tcpm);
490491

491-
shelly_debug_init(&s_kvs);
492+
shelly_debug_init(&s_kvs, &s_tcpm);
492493

493494
return true;
494495
}

src/shelly_rpc_service.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "HAPAccessoryServer+Internal.h"
2525

26+
#include "shelly_debug.h"
2627
#include "shelly_sw_service.h"
2728

2829
static HAPAccessoryServerRef *s_server;
@@ -32,8 +33,10 @@ static HAPPlatformTCPStreamManagerRef s_tcpm;
3233
static void shelly_get_info_handler(struct mg_rpc_request_info *ri,
3334
void *cb_arg, struct mg_rpc_frame_info *fi,
3435
struct mg_str args) {
36+
#ifdef MGOS_HAVE_WIFI
3537
const char *ssid = mgos_sys_config_get_wifi_sta_ssid();
3638
const char *pass = mgos_sys_config_get_wifi_sta_pass();
39+
#endif
3740
bool hap_provisioned = !mgos_conf_str_empty(mgos_sys_config_get_hap_salt());
3841
bool hap_paired = HAPAccessoryServerIsPaired(s_server);
3942
#ifdef MGOS_CONFIG_HAVE_SW1
@@ -65,7 +68,9 @@ static void shelly_get_info_handler(struct mg_rpc_request_info *ri,
6568
#endif
6669
"},"
6770
#endif
71+
#ifdef MGOS_HAVE_WIFI
6872
"wifi_en: %B, wifi_ssid: %Q, wifi_pass: %Q, "
73+
#endif
6974
"hap_provisioned: %B, hap_paired: %B, "
7075
"hap_ip_conns_pending: %u, hap_ip_conns_active: %u, "
7176
"hap_ip_conns_max: %u}",
@@ -92,9 +97,11 @@ static void shelly_get_info_handler(struct mg_rpc_request_info *ri,
9297
sw2.apower, sw2.aenergy,
9398
#endif
9499
#endif
100+
#ifdef MGOS_HAVE_WIFI
95101
mgos_sys_config_get_wifi_sta_enable(), (ssid ? ssid : ""),
96-
(pass ? pass : ""), hap_provisioned, hap_paired,
97-
(unsigned) tcpm_stats.numPendingTCPStreams,
102+
(pass ? pass : ""),
103+
#endif
104+
hap_provisioned, hap_paired, (unsigned) tcpm_stats.numPendingTCPStreams,
98105
(unsigned) tcpm_stats.numActiveTCPStreams,
99106
(unsigned) tcpm_stats.maxNumTCPStreams);
100107
(void) cb_arg;
@@ -152,6 +159,18 @@ static void shelly_set_config_handler(struct mg_rpc_request_info *ri,
152159
(void) fi;
153160
}
154161

162+
static void shelly_get_debug_info_handler(struct mg_rpc_request_info *ri,
163+
void *cb_arg,
164+
struct mg_rpc_frame_info *fi,
165+
struct mg_str args) {
166+
std::string res;
167+
shelly_get_debug_info(&res);
168+
mg_rpc_send_responsef(ri, "{info: %Q}", res.c_str());
169+
(void) cb_arg;
170+
(void) args;
171+
(void) fi;
172+
}
173+
155174
bool shelly_rpc_service_init(HAPAccessoryServerRef *server,
156175
HAPPlatformKeyValueStoreRef kvs,
157176
HAPPlatformTCPStreamManagerRef tcpm) {
@@ -163,5 +182,7 @@ bool shelly_rpc_service_init(HAPAccessoryServerRef *server,
163182
mg_rpc_add_handler(mgos_rpc_get_global(), "Shelly.SetConfig",
164183
"{config: %M, reboot: %B}", shelly_set_config_handler,
165184
NULL);
185+
mg_rpc_add_handler(mgos_rpc_get_global(), "Shelly.GetDebugInfo", "",
186+
shelly_get_debug_info_handler, NULL);
166187
return true;
167188
}

0 commit comments

Comments
 (0)