Skip to content

Commit

Permalink
Better control of when the sim is ready to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Oct 1, 2024
1 parent 82d3cd0 commit 717afcd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
11 changes: 5 additions & 6 deletions cmd/modemd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/modemd/modem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions cmd/modemd/modemController.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type ModemController struct {
RetryFindModemInterval time.Duration
MaxOffDuration time.Duration
MinConnDuration time.Duration
ATReady bool

lastOnRequestTime time.Time
lastSuccessfulPing time.Time
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/modemd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 717afcd

Please sign in to comment.