diff --git a/cmd/modemd/main.go b/cmd/modemd/main.go index 492e8d4..bc52ff2 100644 --- a/cmd/modemd/main.go +++ b/cmd/modemd/main.go @@ -158,13 +158,13 @@ func runMain() error { for i := 0; i < 20; i++ { _, err := mc.RunATCommand("AT") if err == nil { - mc.ATReady = true + mc.Modem.ATReady = true log.Println("AT command responding.") break } time.Sleep(3 * time.Second) } - if !mc.ATReady { + if !mc.Modem.ATReady { log.Println("Making noModemATCommandResponse event.") eventclient.AddEvent(eventclient.Event{ Timestamp: time.Now(), @@ -173,24 +173,23 @@ func runMain() error { return errors.New("failed to get AT command response") } time.Sleep(5 * time.Second) // Wait a little bit longer or else might get AT ERRORS - mc.ATReady = true + mc.Modem.ATReady = true if err := mc.DisableGPS(); err != nil { return err } // ========== Checking SIM card. ============= log.Println("Checking SIM card.") - simReady := false for retries := 5; retries > 0; retries-- { simStatus, err := mc.CheckSimCard() if err == nil && simStatus == "READY" { - simReady = true + mc.Modem.SimReady = true break } log.Printf("SIM card not ready. Will cycle power %d more time(s) to find SIM card", retries) time.Sleep(5 * time.Second) } - if !simReady { + if !mc.Modem.SimReady { mc.failedToFindSimCard = true makeModemEvent("noModemSimCard", &mc) continue diff --git a/cmd/modemd/modem.go b/cmd/modemd/modem.go index 55afef8..be2e595 100644 --- a/cmd/modemd/modem.go +++ b/cmd/modemd/modem.go @@ -13,6 +13,8 @@ type Modem struct { Name string Netdev string VendorProduct string + ATReady bool + SimReady bool } // NewModem return a new modem from the config diff --git a/cmd/modemd/modemController.go b/cmd/modemd/modemController.go index 0b44f57..2b67c45 100644 --- a/cmd/modemd/modemController.go +++ b/cmd/modemd/modemController.go @@ -55,7 +55,6 @@ type ModemController struct { RetryFindModemInterval time.Duration MaxOffDuration time.Duration MinConnDuration time.Duration - ATReady bool lastOnRequestTime time.Time lastSuccessfulPing time.Time @@ -258,15 +257,17 @@ func (mc *ModemController) GetStatus() (map[string]interface{}, error) { status["timestamp"] = time.Now().Format(time.RFC1123Z) status["powered"] = mc.IsPowered status["onOffReason"] = mc.onOffReason - status["ATReady"] = mc.ATReady if mc.Modem != nil { + // Set details for modem modem := make(map[string]interface{}) modem["name"] = mc.Modem.Name modem["netdev"] = mc.Modem.Netdev modem["vendor"] = mc.Modem.VendorProduct + modem["atReady"] = mc.Modem.ATReady + modem["simReady"] = mc.Modem.SimReady modem["connectedTime"] = mc.connectedTime.Format(time.RFC1123Z) - if mc.ATReady { + if mc.Modem.ATReady { modem["voltage"] = valueOrErrorStr(mc.readVoltage()) modem["temp"] = valueOrErrorStr(mc.readTemp()) modem["manufacturer"] = valueOrErrorStr(mc.getManufacturer()) @@ -276,7 +277,8 @@ func (mc *ModemController) GetStatus() (map[string]interface{}, error) { } status["modem"] = modem - if mc.ATReady { + // Set details for signal + if mc.Modem.ATReady { signal := make(map[string]interface{}) signal["strength"] = valueOrErrorStr(mc.signalStrength()) signal["band"] = valueOrErrorStr(mc.readBand()) @@ -289,7 +291,10 @@ func (mc *ModemController) GetStatus() (map[string]interface{}, error) { signal["accessTechnology"] = accessTechnology } status["signal"] = signal + } + // Set details for SIM card + if mc.Modem.SimReady { simCard := make(map[string]interface{}) simCard["simCardStatus"] = valueOrErrorStr(mc.CheckSimCard()) simCard["ICCID"] = valueOrErrorStr(mc.readSimICCID()) @@ -711,7 +716,9 @@ func (mc *ModemController) SetModemPower(on bool) error { } } else { _, _ = mc.RunATCommand("AT+CPOF") - mc.ATReady = false + if mc.Modem != nil { + mc.Modem.ATReady = false + } log.Println("Triggering modem shutdown.") if err := pinEn.Out(gpio.Low); err != nil { return fmt.Errorf("failed to set modem power pin low: %v", err) diff --git a/cmd/modemd/service.go b/cmd/modemd/service.go index 481fb61..8c4a397 100644 --- a/cmd/modemd/service.go +++ b/cmd/modemd/service.go @@ -118,7 +118,7 @@ func (s service) SetAPN(apn string) *dbus.Error { } func (s service) RunATCommand(atCommand string) (string, string, *dbus.Error) { - if !s.mc.ATReady { + if s.mc.Modem != nil && !s.mc.Modem.ATReady { return "", "", makeDbusError("RunATCommand", errors.New("modem not ready for AT commands")) } totalOut, out, err := s.mc.RunATCommandTotalOutput(atCommand)