Kaynağa Gözat

fix(rtl): parse uptime from new 'time' output format (tick/sec counters) (#787)

Recent RTLPlayground firmware builds (seen on a Keephome KP-9000 running
v0.1.0-ad9e2ee, built 2026-09-16) answer POST /cmd time with two counters:
"Tick counter: 0x…   Sec Counter: 0x…". The parser only accepted the
legacy plain-hex body, so every poll errored and the host went into
backoff, leaving firmware, uptime and MAC stale in the UI.

parseTimeUptime now accepts both layouts: the Sec Counter value is the
uptime in seconds in the new format; the legacy single-counter body is
unchanged.

Closes #785

Co-authored-by: gnacho <hnacho@proton.me>
Nacho 2 ay önce
ebeveyn
işleme
100aa926d7

+ 34 - 8
server-go/internal/httpapi/rtlconsole.go

@@ -213,12 +213,9 @@ func (c *rtlConsoleCache) fetch(host string) (*rtlConsoleEntry, error) {
 	if cmdResp.StatusCode != http.StatusOK {
 		return nil, fmt.Errorf("/cmd time HTTP %d", cmdResp.StatusCode)
 	}
-	upHex := strings.TrimSpace(string(cmdBody))
-	upHex = strings.TrimPrefix(upHex, "0x")
-	upHex = strings.TrimPrefix(upHex, "0X")
-	upSec, err := strconv.ParseUint(upHex, 16, 32)
+	upSec, err := parseTimeUptime(string(cmdBody))
 	if err != nil {
-		return nil, fmt.Errorf("/cmd time no es un contador hex (%q): %w", strings.TrimSpace(string(cmdBody)), err)
+		return nil, err
 	}
 
 	now := time.Now()
@@ -228,6 +225,35 @@ func (c *rtlConsoleCache) fetch(host string) (*rtlConsoleEntry, error) {
 	}, nil
 }
 
+// parseTimeUptime extrae el uptime en segundos del cuerpo de POST /cmd time.
+// Acepta los dos formatos del firmware RTLPlayground (#785):
+//
+//   - legado: un contador hex único ("0x00028e9d").
+//   - nuevo (firmwares desde 2026-09-16): dos contadores en líneas o una
+//     sola línea, "Tick counter: 0x…   Sec Counter: 0x…" — el uptime es el
+//     Sec Counter (el Tick counter es un reloj distinto, no segundos).
+func parseTimeUptime(body string) (uint64, error) {
+	s := strings.TrimSpace(body)
+	// Formato nuevo: localizar "Sec Counter:" y parsear el hex que sigue.
+	if i := strings.Index(s, "Sec Counter:"); i >= 0 {
+		rest := strings.TrimSpace(s[i+len("Sec Counter:"):])
+		if nl := strings.IndexAny(rest, "\r\n"); nl >= 0 {
+			rest = rest[:nl]
+		}
+		rest = strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(rest, "0x"), "0X"))
+		if sec, err := strconv.ParseUint(rest, 16, 32); err == nil {
+			return sec, nil
+		}
+		return 0, fmt.Errorf("/cmd time: Sec Counter no es hex (%q)", strings.TrimSpace(body))
+	}
+	upHex := strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(s, "0x"), "0X"))
+	upSec, err := strconv.ParseUint(upHex, 16, 32)
+	if err != nil {
+		return 0, fmt.Errorf("/cmd time no es un contador hex (%q): %w", s, err)
+	}
+	return upSec, nil
+}
+
 // attachSystem adjunta la sección System (board + uptime + MAC) al payload de
 // un beacon periódico si hay datos de consola cacheados. Es el punto de
 // inyección: polledFromAgent mapea System.Board→board, SysInfo.Uptime→
@@ -247,9 +273,9 @@ func (s *server) attachRTLConsole(slug, host string, pl *probe.Payload) {
 	b.Model = e.Model
 	b.Release.Description = "RTLPlayground " + e.Firmware
 	sd := &probe.SystemData{
-		Board:      b,
-		SysInfo:    &probe.SysInfo{Uptime: time.Since(e.BootUnix).Seconds()},
-		BridgeMAC:  strings.ToUpper(e.MAC),
+		Board:     b,
+		SysInfo:   &probe.SysInfo{Uptime: time.Since(e.BootUnix).Seconds()},
+		BridgeMAC: strings.ToUpper(e.MAC),
 	}
 	pl.Data.System = sd
 }

+ 31 - 0
server-go/internal/httpapi/rtlconsole_test.go

@@ -164,3 +164,34 @@ func TestRtlConsoleFetchLoginFalloNoAdjunta(t *testing.T) {
 		t.Fatal("fetch debería fallar con login 401")
 	}
 }
+
+// #785: el parser de uptime debe aceptar el formato nuevo (Tick/Sec counter)
+// además del hex plano legado.
+func TestParseTimeUptimeFormats(t *testing.T) {
+	cases := []struct {
+		name string
+		body string
+		want uint64
+	}{
+		{"legado hex con 0x", "0x00028e9d\n", 0x28e9d},
+		{"legado hex sin prefijo", "00028e9d", 0x28e9d},
+		{"nuevo dos líneas (KP-9000 v0.1.0-ad9e2ee)", "  Tick counter: 0x0244599a   Sec Counter: 0x0002e6d0\n", 0x2e6d0},
+		{"nuevo con saltos de línea", "Tick counter: 0x0244599a\nSec Counter: 0x0002e6d0\n", 0x2e6d0},
+		{"nuevo sec counter sin 0x", "Tick counter: 0x99   Sec Counter: 2e6d0", 0x2e6d0},
+	}
+	for _, tc := range cases {
+		got, err := parseTimeUptime(tc.body)
+		if err != nil {
+			t.Fatalf("%s: %v", tc.name, err)
+		}
+		if got != tc.want {
+			t.Fatalf("%s: uptime = %#x, want %#x", tc.name, got, tc.want)
+		}
+	}
+
+	for _, bad := range []string{"", "garbage", "Tick counter: 0x1", "Sec Counter: zz"} {
+		if _, err := parseTimeUptime(bad); err == nil {
+			t.Fatalf("entrada inválida %q no devolvió error", bad)
+		}
+	}
+}