forked from electricbubble/gidevice
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
screenshot.go
118 lines (95 loc) · 2.25 KB
/
screenshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package giDevice
import (
"bytes"
"errors"
"fmt"
"github.com/SonicCloudOrg/sonic-gidevice/pkg/libimobiledevice"
)
var _ Screenshot = (*screenshot)(nil)
func newScreenshot(client *libimobiledevice.ScreenshotClient) *screenshot {
return &screenshot{
client: client,
exchanged: false,
}
}
type screenshot struct {
client *libimobiledevice.ScreenshotClient
exchanged bool
}
func (s *screenshot) Take() (raw *bytes.Buffer, err error) {
if err = s.exchange(); err != nil {
return nil, err
}
// link service
req := []interface{}{
"DLMessageProcessMessage",
map[string]interface{}{
"MessageType": "ScreenShotRequest",
},
}
var pkt libimobiledevice.Packet
if pkt, err = s.client.NewBinaryPacket(req); err != nil {
return nil, err
}
if err = s.client.SendPacket(pkt); err != nil {
return nil, err
}
var respPkt libimobiledevice.Packet
if respPkt, err = s.client.ReceivePacket(); err != nil {
return nil, err
}
var resp []interface{}
if err = respPkt.Unmarshal(&resp); err != nil {
return nil, err
}
if resp[0].(string) != "DLMessageProcessMessage" {
return nil, fmt.Errorf("message device not ready %s %s", resp[3], resp[4])
}
raw = new(bytes.Buffer)
screen := resp[1].(map[string]interface{})
var data []byte
ok := false
if data, ok = screen["ScreenShotData"].([]byte); !ok {
return nil, errors.New("`ScreenShotData` not ready")
}
if _, err = raw.Write(data); err != nil {
return nil, err
}
return
}
func (s *screenshot) exchange() (err error) {
if s.exchanged {
return
}
var respPkt libimobiledevice.Packet
if respPkt, err = s.client.ReceivePacket(); err != nil {
return err
}
var resp []interface{}
if err = respPkt.Unmarshal(&resp); err != nil {
return err
}
req := []interface{}{
"DLMessageVersionExchange",
"DLVersionsOk",
resp[1],
}
var pkt libimobiledevice.Packet
if pkt, err = s.client.NewBinaryPacket(req); err != nil {
return err
}
if err = s.client.SendPacket(pkt); err != nil {
return err
}
if respPkt, err = s.client.ReceivePacket(); err != nil {
return err
}
if err = respPkt.Unmarshal(&resp); err != nil {
return err
}
if resp[3].(string) != "DLMessageDeviceReady" {
return fmt.Errorf("message device not ready %s", resp[3])
}
s.exchanged = true
return
}