Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 'no more updates from usbmuxd after the last device disconnects' problem #74

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/app/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ var launchCmd = &cobra.Command{
func myHelpFunc(cmd *cobra.Command, args []string) {
fmt.Printf(`%s

Usage:
%s -- [arguments [arguments ...]]
Usage:
%s -- [arguments [arguments ...]]

Flags:
%s`, cmd.Long, cmd.UseLine(), cmd.Flags().FlagUsages())
Flags:
%s`, cmd.Long, cmd.UseLine(), cmd.Flags().FlagUsages())
}

func initAppLaunch() {
Expand Down
55 changes: 30 additions & 25 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() {
}, true)
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 != nil {
(*cancelFunc)()
}
wg.Done()
}()
})(&shutDownFun)
wg.Wait()
if bIsDeviceOnline && bAutoConfirm {
bPreCheckIOSVer = false
Expand All @@ -80,7 +85,7 @@ var devmodeEnableCmd = &cobra.Command{

func initDevModeEnableCmd() {
devmodeRootCMD.AddCommand(devmodeEnableCmd)
devmodeEnableCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber")
devmodeEnableCmd.Flags().StringVarP(&udid, "udid", "u", "", "target specific device by UDID")
devmodeEnableCmd.MarkFlagRequired("udid")
devmodeEnableCmd.Flags().BoolVar(&bWaitReboot, "wait", false, "wait for reboot to complete")
devmodeEnableCmd.Flags().IntVar(&intEnableWaitTimeout, "wait-timeout", 60, "wait timeout in seconds")
Expand Down
64 changes: 27 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,37 @@ 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)
}
logrus.Debugf("Device %s is %s", device.SerialNumber, device.Status)
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))
}, true)
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.8
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