From 5dfa9e4a00db4102387ae6dc13395849e42bc679 Mon Sep 17 00:00:00 2001 From: cam Date: Mon, 13 May 2024 23:33:59 +1200 Subject: [PATCH] Added dbus call to set APN --- cmd/modemd/modemController.go | 33 +++++++++++++++++++++++++++++++++ cmd/modemd/service.go | 10 ++++++++++ 2 files changed, 43 insertions(+) diff --git a/cmd/modemd/modemController.go b/cmd/modemd/modemController.go index ea0c005..5cc2b66 100644 --- a/cmd/modemd/modemController.go +++ b/cmd/modemd/modemController.go @@ -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{}) @@ -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") diff --git a/cmd/modemd/service.go b/cmd/modemd/service.go index 8a1df6b..3f47566 100644 --- a/cmd/modemd/service.go +++ b/cmd/modemd/service.go @@ -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()