Skip to content

Commit

Permalink
feat: [DiagnosticsRelay] new functions Reboot Shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun authored Mar 14, 2022
1 parent e67ef0a commit ad436fe
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
27 changes: 27 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type device struct {
afc Afc
houseArrest HouseArrest
syslogRelay SyslogRelay
diagnosticsRelay DiagnosticsRelay
crashReportMover CrashReportMover
pcapd Pcapd
}
Expand Down Expand Up @@ -462,6 +463,32 @@ func (d *device) SyslogStop() {
d.syslogRelay.Stop()
}

func (d *device) Reboot() (err error) {
if _, err = d.lockdownService(); err != nil {
return
}
if d.diagnosticsRelay, err = d.lockdown.DiagnosticsRelayService(); err != nil {
return
}
if err = d.diagnosticsRelay.Reboot(); err != nil {
return
}
return
}

func (d *device) Shutdown() (err error) {
if _, err = d.lockdownService(); err != nil {
return
}
if d.diagnosticsRelay, err = d.lockdown.DiagnosticsRelayService(); err != nil {
return
}
if err = d.diagnosticsRelay.Shutdown(); err != nil {
return
}
return
}

func (d *device) PcapdService() (pcapd Pcapd, err error) {
// if d.pcapd != nil {
// return d.pcapd, nil
Expand Down
10 changes: 10 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,13 @@ func Test_device_Syslog(t *testing.T) {
dev.SyslogStop()
time.Sleep(200 * time.Millisecond)
}

func Test_device_Reboot(t *testing.T) {
setupDevice(t)
dev.Reboot()
}

func Test_device_Shutdown(t *testing.T) {
setupDevice(t)
dev.Shutdown()
}
39 changes: 39 additions & 0 deletions diagnosticsrelay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package giDevice

import "github.com/electricbubble/gidevice/pkg/libimobiledevice"

func newDiagnosticsRelay(client *libimobiledevice.DiagnosticsRelayClient) *diagnostics {
return &diagnostics{
client: client,
}
}

type diagnostics struct {
client *libimobiledevice.DiagnosticsRelayClient
}

func (d *diagnostics) Reboot() (err error) {
var pkt libimobiledevice.Packet
if pkt, err = d.client.NewXmlPacket(
d.client.NewBasicRequest("Restart"),
); err != nil {
return
}
if err = d.client.SendPacket(pkt); err != nil {
return err
}
return
}

func (d *diagnostics) Shutdown() (err error) {
var pkt libimobiledevice.Packet
if pkt, err = d.client.NewXmlPacket(
d.client.NewBasicRequest("Shutdown"),
); err != nil {
return
}
if err = d.client.SendPacket(pkt); err != nil {
return err
}
return
}
10 changes: 10 additions & 0 deletions idevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Device interface {
Pcap() (packet <-chan []byte, err error)
PcapStop()

Reboot() error
Shutdown() error

crashReportMoverService() (crashReportMover CrashReportMover, err error)
MoveCrashReport(hostDir string, opts ...CrashReportMoverOption) (err error)

Expand Down Expand Up @@ -96,6 +99,7 @@ type Lockdown interface {
AfcService() (afc Afc, err error)
HouseArrestService() (houseArrest HouseArrest, err error)
SyslogRelayService() (syslogRelay SyslogRelay, err error)
DiagnosticsRelayService() (diagnostics DiagnosticsRelay, err error)
CrashReportMoverService() (crashReportMover CrashReportMover, err error)
}

Expand Down Expand Up @@ -205,6 +209,12 @@ type Pcapd interface {
Packet() <-chan []byte
Stop()
}

type DiagnosticsRelay interface {
Reboot() error
Shutdown() error
}

type CrashReportMover interface {
Move(hostDir string, opts ...CrashReportMoverOption) (err error)
walkDir(dirname string, fn func(path string, info *AfcFileInfo)) (err error)
Expand Down
11 changes: 11 additions & 0 deletions lockdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,17 @@ func (c *lockdown) PcapdService() (pcapd Pcapd, err error) {
return newPcapdClient(pcapdClient), nil
}

func (c *lockdown) DiagnosticsRelayService() (diagnostics DiagnosticsRelay, err error) {
var innerConn InnerConn
if innerConn, err = c._startService(libimobiledevice.DiagnosticsRelayServiceName, nil); err != nil {
return nil, err
}
diagnosticsRelayClient := libimobiledevice.NewDiagnosticsRelayClient(innerConn)
diagnostics = newDiagnosticsRelay(diagnosticsRelayClient)

return
}

func (c *lockdown) CrashReportMoverService() (crashReportMover CrashReportMover, err error) {
var innerConn InnerConn
if innerConn, err = c._startService(libimobiledevice.CrashReportMoverServiceName, nil); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/libimobiledevice/diagnosticsrelay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package libimobiledevice

const (
DiagnosticsRelayServiceName = "com.apple.mobile.diagnostics_relay"
)

type DiagnosticsRelayBasicRequest struct {
Request string `plist:"Request"`
Label string `plist:"Label"`
}

func NewDiagnosticsRelayClient(innerConn InnerConn) *DiagnosticsRelayClient {
return &DiagnosticsRelayClient{
newServicePacketClient(innerConn),
}
}

type DiagnosticsRelayClient struct {
client *servicePacketClient
}

func (c *DiagnosticsRelayClient) InnerConn() InnerConn {
return c.client.innerConn
}

func (c *DiagnosticsRelayClient) NewBasicRequest(relayType string) *DiagnosticsRelayBasicRequest {
return &DiagnosticsRelayBasicRequest{
Request: relayType,
Label: BundleID,
}
}

func (c *DiagnosticsRelayClient) NewXmlPacket(req interface{}) (Packet, error) {
return c.client.NewXmlPacket(req)
}

func (c *DiagnosticsRelayClient) SendPacket(pkt Packet) (err error) {
return c.client.SendPacket(pkt)
}

0 comments on commit ad436fe

Please sign in to comment.