Skip to content

Commit

Permalink
fix: fix 'no more responses from usbmuxd after the last device discon…
Browse files Browse the repository at this point in the history
…nects' problem
  • Loading branch information
sam80180 committed Jan 6, 2024
1 parent e639c1c commit d2c1795
Show file tree
Hide file tree
Showing 5 changed files with 1,707 additions and 97 deletions.
51 changes: 28 additions & 23 deletions cmd/devmode/enable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devmode

import (
"context"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -30,40 +31,44 @@ var devmodeEnableCmd = &cobra.Command{
bIsDeviceOnline := true
wg := new(sync.WaitGroup)
wg.Add(1)
shutDownFun, errListen := util.UsbmuxListen(func(gidevice *giDevice.Device, device *entity.Device, e error, cancelFunc func()) {
var deviceIdMap = make(map[int]string)
shutDownFun := util.UsbmuxListen(func(gidevice *giDevice.Device, device *entity.Device, e error, cancelFunc context.CancelFunc) {
if e != nil {
logrus.Warnf("Error: %+v", e)
}
if device == nil {
return
}
funcDone := func() {
cancelFunc()
bIsDeviceOnline = true
logrus.Infof("Device %s is online.", udid)
wg.Done()
if len(device.SerialNumber) > 0 {
deviceIdMap[device.DeviceID] = device.SerialNumber
} else {
device.SerialNumber = deviceIdMap[device.DeviceID]
delete(deviceIdMap, device.DeviceID)
}
if device.Status == "offline" {
bIsDeviceOnline = false
logrus.Infof("Device %s is offline.", udid)
} else if !bIsDeviceOnline && device.Status == "online" {
if device.SerialNumber == udid {
funcDone()
return
}
detail, _ := entity.GetDetail(*gidevice)
if detail != nil && detail.UniqueDeviceID == udid {
funcDone()
if device.SerialNumber == udid {
logrus.Infof("Device %s is %s.", device.SerialNumber, device.Status)
if device.Status == "offline" {
bIsDeviceOnline = false
} else if !bIsDeviceOnline && device.Status == "online" { // transition from offline to online
if cancelFunc != nil {
cancelFunc()
}
bIsDeviceOnline = true
wg.Done()
return
}
} else {
logrus.Debugf("Device %s is %s.", device.SerialNumber, device.Status)
}
})
if errListen != nil {
return errListen
}
go func() {
go (func(cancelFunc *context.CancelFunc) { // timer to cancel listening
time.Sleep(time.Duration(intEnableWaitTimeout) * time.Second)
logrus.Warnf("Timeout waiting for device %s to reboot.", udid)
shutDownFun()
if cancelFunc != nil {
(*cancelFunc)()
}
wg.Done()
}()
})(&shutDownFun)
wg.Wait()
if bIsDeviceOnline && bAutoConfirm {
bPreCheckIOSVer = false
Expand Down
63 changes: 26 additions & 37 deletions cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package cmd

import (
"encoding/json"
"context"
"fmt"
"os"
"os/signal"
"sync"

giDevice "github.com/SonicCloudOrg/sonic-gidevice"
"github.com/SonicCloudOrg/sonic-ios-bridge/src/entity"
"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -34,47 +34,36 @@ var listenCmd = &cobra.Command{
Short: "Listener for devices status",
Long: "Listener for devices status",
RunE: func(cmd *cobra.Command, args []string) error {
usbMuxClient, err := giDevice.NewUsbmux()
if err != nil {
return util.NewErrorPrint(util.ErrConnect, "usbMux", err)
}
model := make(chan giDevice.Device)
shutDownFun, err2 := usbMuxClient.Listen(model)
if err2 != nil {
return util.NewErrorPrint(util.ErrSendCommand, "listen", err2)
}

shutDown := make(chan os.Signal, 1)
signal.Notify(shutDown, os.Interrupt, os.Kill)
var deviceIdMap = make(map[int]string)
for {
select {
case d := <-model:
deviceByte, _ := json.Marshal(d.Properties())
device := &entity.Device{}
json.Unmarshal(deviceByte, device)
if len(device.SerialNumber) > 0 {
deviceIdMap[device.DeviceID] = device.SerialNumber
} else {
device.SerialNumber = deviceIdMap[device.DeviceID]
delete(deviceIdMap, device.DeviceID)
}
device.Status = device.GetStatus()
if device.Status == "online" && isDetail {
detail, err1 := entity.GetDetail(d)
wg := new(sync.WaitGroup)
wg.Add(1)
util.UsbmuxListen(func(gidevice *giDevice.Device, device *entity.Device, e error, cancelFunc context.CancelFunc) {
if e != nil {
logrus.Warnf("Error: %+v", e)
}
if device == nil {
return
}
if len(device.SerialNumber) > 0 {
deviceIdMap[device.DeviceID] = device.SerialNumber
} else {
device.SerialNumber = deviceIdMap[device.DeviceID]
delete(deviceIdMap, device.DeviceID)
}
if device.Status == "online" {
if isDetail {
detail, err1 := entity.GetDetail(*gidevice)
if err1 != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err1)
logrus.Warnf("Error: %+v", err1)
} else {
device.DeviceDetail = *detail
}
}
data := util.ResultData(device)
fmt.Println(util.Format(data, isFormat, isDetail))
case <-shutDown:
shutDownFun()
return nil
}
}
data := util.ResultData(device)
fmt.Println(util.Format(data, isFormat, isDetail))
})
wg.Wait()
return nil
},
}
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ require (
github.com/Masterminds/semver v1.5.0
github.com/SonicCloudOrg/sonic-gidevice v0.7.7
github.com/SonicCloudOrg/sonic-ios-webkit-adapter v0.0.7
github.com/cenkalti/backoff/v4 v4.2.1
github.com/gin-gonic/gin v1.8.1
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-envparse v0.1.0
github.com/mitchellh/mapstructure v1.5.0
github.com/quintans/toolkit v0.3.5
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.4.0
Expand All @@ -32,7 +34,7 @@ require (
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -43,10 +45,10 @@ require (
github.com/tidwall/sjson v1.2.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/yezihack/e v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit d2c1795

Please sign in to comment.