Skip to content

Commit

Permalink
Merge pull request #22 from TheCacophonyProject/set-apn
Browse files Browse the repository at this point in the history
Added dbus call to set APN
  • Loading branch information
CameronRP authored May 13, 2024
2 parents a13028b + 5dfa9e4 commit 80dcf15
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/modemd/modemController.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func (mc *ModemController) GetStatus() (map[string]interface{}, error) {
modem["manufacturer"] = valueOrErrorStr(mc.getManufacturer())
modem["model"] = valueOrErrorStr(mc.getModel())
modem["serial"] = valueOrErrorStr(mc.getSerialNumber())
modem["apn"] = valueOrErrorStr(mc.getAPN())
status["modem"] = modem

signal := make(map[string]interface{})
Expand Down Expand Up @@ -419,6 +420,38 @@ func (mc *ModemController) getSerialNumber() (string, error) {
// 9.1 Overview of AT Commands for SMS Control
// Firmware upgrades?

func (mc *ModemController) getAPN() (string, error) {
out, err := mc.RunATCommand("AT+CGDCONT?")
if err != nil {
return "", err
}
out = strings.TrimPrefix(out, "+CGDCONT:")
out = strings.TrimSpace(out)
parts := strings.Split(out, ",")
if len(parts) < 3 {
return "", fmt.Errorf("invalid CGDCONT format %s", out)
}
apn := parts[2]
apn = strings.TrimPrefix(apn, "\"")
apn = strings.TrimSuffix(apn, "\"")
return apn, nil
}

func (mc *ModemController) setAPN(apn string) error {
_, err := mc.RunATCommand(fmt.Sprintf("AT+CGDCONT=1,\"IP\",\"%s\"", apn))
if err != nil {
return err
}
readAPN, err := mc.getAPN()
if err != nil {
return err
}
if readAPN != apn {
return fmt.Errorf("failed to set APN, APN is '%s' when it was set as '%s'", readAPN, apn)
}
return err
}

func (mc *ModemController) CheckSimCard() (string, error) {
// Enable verbose error messages.
_, err := mc.RunATCommand("AT+CMEE=2")
Expand Down
10 changes: 10 additions & 0 deletions cmd/modemd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func (s service) GetStatus() (map[string]interface{}, *dbus.Error) {
return status, nil
}

func (s service) SetAPN(apn string) *dbus.Error {
log.Println("Setting APN to", apn)
err := s.mc.setAPN(apn)
if err != nil {
log.Println(err)
return makeDbusError("SetAPN", err)
}
return nil
}

/*
func (s service) GPSOn() *dbus.Error {
err := s.mc.EnableGPS()
Expand Down

0 comments on commit 80dcf15

Please sign in to comment.