From 026c1aa3bbd17ee865307d4c337297907ec09202 Mon Sep 17 00:00:00 2001 From: yotia Date: Sun, 4 Aug 2024 15:59:58 +0300 Subject: [PATCH] Fixed error logging. Added more logging of SIM info. --- api/modem/gps/check.go | 6 +++- api/modem/gps/gps.go | 8 +++--- api/modem/gps/nmea.go | 18 +----------- api/modem/internet/ic.go | 4 +-- api/modem/modem.go | 61 ++++++++++++++++++++++++++++++++-------- api/modem/sms/sms.go | 6 ++-- 6 files changed, 65 insertions(+), 38 deletions(-) diff --git a/api/modem/gps/check.go b/api/modem/gps/check.go index 5096f57..b27a403 100644 --- a/api/modem/gps/check.go +++ b/api/modem/gps/check.go @@ -23,7 +23,11 @@ func (st Status) String() string { "ActiveSatelitesCounte: " + string(st.ActiveSatelitesCounte) + "\n" } -var StatusNil = Status{} +var StatusNil = Status{ + GotResponses: false, + FoundSatelitesCount: 0, + ActiveSatelitesCounte: 0, +} func (g *gps) CheckStatus() (Status, error) { // Provides more information about signal and possible problems using NMEA reports diff --git a/api/modem/gps/gps.go b/api/modem/gps/gps.go index c987fb6..2df03c3 100644 --- a/api/modem/gps/gps.go +++ b/api/modem/gps/gps.go @@ -48,7 +48,7 @@ func (g *gps) Update() error { return fmt.Errorf("receive GPS data: %w", err) } if !resp.Check() { - return fmt.Errorf("error response") + return fmt.Errorf("get GPS info: error response: %s", resp) } if err := g.data.decode(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]); err != nil { return fmt.Errorf("decode: %w", err) @@ -77,7 +77,7 @@ func (g *gps) switchGpsMode(on bool) error { return fmt.Errorf("make at ask: %w", err) } if !resp.Check() { - return fmt.Errorf("error response") + return fmt.Errorf("get GPS mode: error response: %s", resp) } ans := strings.Replace(strings.Split(strings.Split(resp.RmFront("+CGPS:").String(), "\n")[0], ",")[0], " ", "", -1) if ans == onStr { @@ -87,10 +87,10 @@ func (g *gps) switchGpsMode(on bool) error { // Modem is not in GPS mode resp, err = g.port.Send("AT+CGPS=" + onStr) if err != nil { - return fmt.Errorf("try to switch to gps: %w", err) + return fmt.Errorf("set GPS mode: %w", err) } if !resp.Check() { - return fmt.Errorf("switch tp GPS failed") + return fmt.Errorf("set GPS mode: error response: %s", resp) } return nil } diff --git a/api/modem/gps/nmea.go b/api/modem/gps/nmea.go index cda5c95..b2d2ff8 100644 --- a/api/modem/gps/nmea.go +++ b/api/modem/gps/nmea.go @@ -54,28 +54,12 @@ func (g *gps) rawCollect(flags nmeaFlags) (string, error) { // Setup gps - // Receive data from all contries' satelites, enable DPO(Dynamic Power Optimization) mode - // if resp, err := g.port.Send("AT+CGNSSMODE=15,1"); err != nil { - // return "", fmt.Errorf("AT+CGNSSMODE= request: %w", err) - // } else { - // if !resp.Check() { - // return "", fmt.Errorf("AT+CGNSSMODE= error response: %s", resp) - // } - // } - // Receive all types of data - // if resp, err := g.port.Send("AT+CGPSNMEA=200191"); err != nil { - // return "", fmt.Errorf("AT+CGPSNMEA= request: %w", err) - // } else { - // if !resp.Check() { - // return "", fmt.Errorf("AT+CGPSNMEA= error response: %s", resp) - // } - // } // Set output rate to 10Hz if resp, err := g.port.Send("AT+CGPSNMEARATE=1"); err != nil { return "", fmt.Errorf("AT+CGPSNMEARATE= request: %w", err) } else { if !resp.Check() { - return "", fmt.Errorf("AT+CGPSNMEARATE= error response: %s", resp) + return "", fmt.Errorf("set output rate: error response: %s", resp) } } diff --git a/api/modem/internet/ic.go b/api/modem/internet/ic.go index 9612478..c006528 100644 --- a/api/modem/internet/ic.go +++ b/api/modem/internet/ic.go @@ -103,7 +103,7 @@ func (c *conn) checkReqs() error { return fmt.Errorf("AT+CPIN? request: %w", err) } if !resp.Check() { - return fmt.Errorf("SIM card is not inserted") + return fmt.Errorf("validate SIM: error response: %s", resp) } return nil } @@ -115,7 +115,7 @@ func (c *conn) ConfigurePPP() error { return fmt.Errorf("AT+CSPN? request: %w", err) } if !resp.Check() { - return fmt.Errorf("failed to check SIM provider") + return fmt.Errorf("get provider: error response: %s", resp) } strs := strings.Split(string(resp), "\"") if len(strs) < 3 { diff --git a/api/modem/modem.go b/api/modem/modem.go index e6d910b..1ae7c48 100644 --- a/api/modem/modem.go +++ b/api/modem/modem.go @@ -250,6 +250,50 @@ func (m *modem) testConsole() { } } +func (m *modem) setupPort() error { + // Reset input + if err := m.port.SerialPort().ResetInputBuffer(); err != nil { + return fmt.Errorf("reset input buffer: %w", err) + } + // Reset output + if err := m.port.SerialPort().ResetOutputBuffer(); err != nil { + return fmt.Errorf("reset output buffer: %w", err) + } + m.port.Send("ATE0") // This shit sometimes enables echo mode... why... when... but it can + + // Turn on errors' describtion + if resp, err := m.port.Send("AT+CMEE=2"); err != nil || !resp.Check() { + if err != nil { + return fmt.Errorf("AT+CMEE=2 request: %w", err) + } + return fmt.Errorf("turn on errors: error response: %s", resp) + } + + // Display other values + m.logger.Println("DEBUG SIM VALUES:") + // ICCID + if resp, err := m.port.Send("AT+CCID"); err != nil || !resp.Check() { + if err != nil { + return fmt.Errorf("AT+CCID request: %w", err) + } + return fmt.Errorf("get ICCID: error response: %s", resp) + } else { + m.logger.Println("ICCID:", strings.Split(resp.String(), "\n")[0]) + } + + // ICCID + if resp, err := m.port.Send("AT+CNUM"); err != nil || !resp.Check() { + if err != nil { + return fmt.Errorf("AT+CNUM request: %w", err) + } + return fmt.Errorf("get CNUM: error response: %s", resp) + } else { + m.logger.Println("CNUM:", strings.Split(resp.String(), "\n")[0]) + } + + return nil +} + func (m *modem) checkPort(portName string) (outErr error) { defer func() { if outErr != nil { // Clear port if there is error @@ -270,16 +314,9 @@ func (m *modem) checkPort(portName string) (outErr error) { } defer m.port.Disconnect() // Do not bother about errors... - // Reset input - if err := m.port.SerialPort().ResetInputBuffer(); err != nil { - return fmt.Errorf("reset input buffer: %w", err) + if err := m.setupPort(); err != nil { + return fmt.Errorf("setup port: %w", err) } - // Reset output - if err := m.port.SerialPort().ResetOutputBuffer(); err != nil { - return fmt.Errorf("reset output buffer: %w", err) - } - m.port.Send("ATE0") // This shit sometimes enables echo mode... why... when... but it can - // m.port.Send("\r\n") // Ping m.logger.Println("Ping...") @@ -295,7 +332,7 @@ func (m *modem) checkPort(portName string) (outErr error) { return fmt.Errorf("get model: %w", err) } if !resp.Check() { - return fmt.Errorf("error response: %s", resp) + return fmt.Errorf("get model: error response: %s", resp) } model := strings.Split(resp.String(), "\n")[0] if err != nil { @@ -341,10 +378,10 @@ SearchLoop: func (m *modem) ping() error { resp, err := m.port.Send("AT") if err != nil { - return err + return fmt.Errorf("AT request: %w", err) } if !resp.Check() { - return fmt.Errorf("connection lost") + return fmt.Errorf("AT request: error response: %s", resp) } return nil } diff --git a/api/modem/sms/sms.go b/api/modem/sms/sms.go index d279d4d..c7bddfb 100644 --- a/api/modem/sms/sms.go +++ b/api/modem/sms/sms.go @@ -33,12 +33,13 @@ func (d *dialer) Init() error { if !d.port.IsConnected() { return fmt.Errorf("serial port is not connected") } + // Ensure text format if resp, err := d.port.Send("AT+CMGF=1"); err != nil || !resp.Check() { if err != nil { - return fmt.Errorf("AT+CMGF=1 request: %w", err) + return fmt.Errorf("set to text format request: %w", err) } - return fmt.Errorf("failed to set SMS format") + return fmt.Errorf("set SIM format: error response: %s", resp) } return nil } @@ -52,6 +53,7 @@ func (d *dialer) Send(number, msg string) error { if at.Resp(resp).Check() { return nil } + d.logger.Println("SEND RESPONSE:", resp) errCode, err := GetError([]byte(resp)) if err != nil { return fmt.Errorf("send sms failed and failed to get error: %w", err)