From 41a78ae1db8a32ebfef01e1a3a3561ef9480f64f Mon Sep 17 00:00:00 2001 From: Jiahui Zhu Date: Thu, 19 Nov 2026 13:30:37 +0800 Subject: [PATCH] fix(esim): accept ML307C bare logical channel responses --- internal/modem/at_parse.go | 22 ++++++++++++++++++++-- internal/modem/at_parse_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/internal/modem/at_parse.go b/internal/modem/at_parse.go index 226f7b7b..1dda7a9a 100644 --- a/internal/modem/at_parse.go +++ b/internal/modem/at_parse.go @@ -548,11 +548,11 @@ func ParseUSBNetMode(resp string) (int, error) { } // parseCCHO 解析 AT+CCHO 响应,提取逻辑通道号 -// 响应格式: +CCHO: +// 响应格式: +CCHO: 。ML307C 返回单行裸通道号。 func parseCCHO(resp string) (int, bool) { line, ok := findLineWithPrefix(resp, "+CCHO:") if !ok { - return -1, false + return parseBareCCHO(resp) } parts := strings.Split(line, ":") if len(parts) < 2 { @@ -566,6 +566,24 @@ func parseCCHO(resp string) (int, bool) { return -1, false } +func parseBareCCHO(resp string) (int, bool) { + var channel int + for _, line := range splitLines(resp) { + if line == "" || line == "OK" { + continue + } + value, err := strconv.ParseUint(line, 10, 8) + if channel != 0 || err != nil || value == 0 { + return -1, false + } + channel = int(value) + } + if channel == 0 { + return -1, false + } + return channel, true +} + // parseCGLA 解析 AT+CGLA 响应,提取 APDU 响应 // 响应格式: +CGLA: ,"" func parseCGLA(resp string) (string, bool) { diff --git a/internal/modem/at_parse_test.go b/internal/modem/at_parse_test.go index eb2f397d..87c3694a 100644 --- a/internal/modem/at_parse_test.go +++ b/internal/modem/at_parse_test.go @@ -214,3 +214,30 @@ func TestParseCOPSOperator(t *testing.T) { }) } } + +func TestParseCCHO(t *testing.T) { + tests := []struct { + name string + resp string + channel int + }{ + {name: "prefixed", resp: "\r\n+CCHO: 2\r\nOK\r\n", channel: 2}, + {name: "ML307C", resp: "\r\n2\r\nOK\r\n", channel: 2}, + {name: "terminal stripped", resp: "2", channel: 2}, + {name: "only OK", resp: "OK", channel: -1}, + {name: "bare followed by ERROR", resp: "2\nERROR", channel: -1}, + {name: "command rejected", resp: "+CME ERROR: 3", channel: -1}, + {name: "multiple numbers", resp: "2\n3\nOK", channel: -1}, + {name: "number with suffix", resp: "2,3\nOK", channel: -1}, + {name: "basic channel", resp: "0\nOK", channel: -1}, + {name: "out of range", resp: "258\nOK", channel: -1}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + channel, ok := parseCCHO(tt.resp) + if channel != tt.channel || ok != (tt.channel > 0) { + t.Fatalf("parseCCHO(%q) = (%d, %t), want (%d, %t)", tt.resp, channel, ok, tt.channel, tt.channel > 0) + } + }) + } +}