Skip to content

Commit

Permalink
chore: update modem manager to handle modem additions and removals
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed Jul 28, 2024
1 parent 9a6f65e commit 9b43539
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
36 changes: 21 additions & 15 deletions internal/pkg/modem/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"
"time"

"github.com/godbus/dbus/v5"
"github.com/maltegrosse/go-modemmanager"
)

Expand All @@ -21,9 +22,9 @@ type Modem struct {
}

type Manager struct {
mmgr modemmanager.ModemManager
modems map[string]*Modem
reboot chan struct{}
mmgr modemmanager.ModemManager
modems map[string]*Modem
rebootSignal chan struct{}
}

var instance *Manager
Expand All @@ -34,9 +35,9 @@ func NewManager() (*Manager, error) {
return nil, err
}
instance = &Manager{
mmgr: mmgr,
modems: make(map[string]*Modem),
reboot: make(chan struct{}, 1),
mmgr: mmgr,
modems: make(map[string]*Modem),
rebootSignal: make(chan struct{}, 1),
}
go instance.watch()
return instance, nil
Expand All @@ -61,8 +62,8 @@ func (m *Manager) watchModems() error {
if err != nil {
return err
}

modemAdded := false
shouldReboot := false
currentModems := make(map[string]dbus.ObjectPath)
for _, mm := range modems {
state, err := mm.GetState()
if err != nil {
Expand All @@ -77,22 +78,27 @@ func (m *Manager) watchModems() error {
if err != nil {
return err
}
currentModems[modemId] = mm.GetObjectPath()
if exist, ok := m.modems[modemId]; ok {
if exist.modem.GetObjectPath() == mm.GetObjectPath() {
continue
}
}
slog.Info("new modem added", "modemId", modemId, "objectPath", mm.GetObjectPath())
modemAdded = true
nm := &Modem{
modem: mm,
}
shouldReboot = true
nm := &Modem{modem: mm}
nm.IsEuicc, nm.Eid = nm.detectEuicc()
m.modems[modemId] = nm
}
// If the modem is not in the list, add it, and send a signal to reboot the subscriber
if modemAdded {
m.reboot <- struct{}{}
for modemId, modem := range m.modems {
if _, ok := currentModems[modemId]; !ok {
slog.Info("modem removed", "modemId", modemId, "objectPath", modem.modem.GetObjectPath())
delete(m.modems, modemId)
shouldReboot = true
}
}
if shouldReboot {
m.rebootSignal <- struct{}{}
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/modem/modem.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (m *Modem) Restart() error {
slog.Error("failed to power on sim", "error", err, "result", string(result))
return err
}
// Some older modems require disabling and enabling the modem to take effect.
if err := m.modem.Disable(); err != nil {
slog.Error("failed to disable modem", "error", err)
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/modem/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Subscriber:
go m.messagingSubscriber(modem, stopChan, subscriber)
}

<-m.reboot
<-m.rebootSignal
slog.Info("got reboot signal, restarting messaging subscriber")
for _, stopChan := range stopChans {
stopChan <- struct{}{}
Expand Down

0 comments on commit 9b43539

Please sign in to comment.