1
1
package screencapture
2
2
3
3
import (
4
+ "encoding/binary"
4
5
"errors"
6
+ "fmt"
7
+ "io"
5
8
6
9
"github.com/google/gousb"
7
10
log "github.com/sirupsen/logrus"
@@ -35,15 +38,13 @@ func (usa *UsbAdapter) StartReading(device IosDevice, receiver UsbDataReceiver,
35
38
}
36
39
confignum , _ := usbDevice .ActiveConfigNum ()
37
40
38
- log .Debugf ("Config is active: %d" , confignum )
41
+ log .Debugf ("Config is active: %d, QT config is: %d " , confignum , device . QTConfigIndex )
39
42
40
- config , err := usbDevice .Config (confignum )
43
+ config , err := usbDevice .Config (device . QTConfigIndex )
41
44
if err != nil {
42
45
return errors .New ("Could not retrieve config" )
43
46
}
44
47
45
- sendQTConfigControlRequest (usbDevice )
46
-
47
48
log .Debugf ("QT Config is active: %s" , config .String ())
48
49
49
50
val , err := usbDevice .Control (0x02 , 0x01 , 0 , 0x86 , make ([]byte , 0 ))
@@ -94,22 +95,27 @@ func (usa *UsbAdapter) StartReading(device IosDevice, receiver UsbDataReceiver,
94
95
return err
95
96
}
96
97
log .Debug ("Endpoint claimed" )
97
- log .Infof ("Device '%s' USB connection ready" , device .SerialNumber )
98
+ log .Infof ("Device '%s' USB connection ready, waiting for ping.. " , device .SerialNumber )
98
99
go func () {
99
-
100
- frameExtractor := NewLengthFieldBasedFrameExtractor ()
101
100
for {
102
- buffer := make ([]byte , 65536 )
101
+ buffer := make ([]byte , 4 )
103
102
104
- n , err := stream . Read ( buffer )
103
+ n , err := io . ReadFull ( stream , buffer )
105
104
if err != nil {
106
- log .Error ( "couldn't read bytes " , err )
105
+ log .Errorf ( "Failed reading 4bytes length with err:%s only received: %d " , err , n )
107
106
return
108
107
}
109
- frame , isCompleteFrame := frameExtractor .ExtractFrame (buffer [:n ])
110
- if isCompleteFrame {
111
- receiver .ReceiveData (frame )
108
+ //the 4 bytes header are included in the length, so we need to subtract them
109
+ //here to know how long the payload will be
110
+ length := binary .LittleEndian .Uint32 (buffer ) - 4
111
+ dataBuffer := make ([]byte , length )
112
+
113
+ n , err = io .ReadFull (stream , dataBuffer )
114
+ if err != nil {
115
+ log .Errorf ("Failed reading payload with err:%s only received: %d/%d bytes" , err , n , length )
116
+ return
112
117
}
118
+ receiver .ReceiveData (dataBuffer )
113
119
}
114
120
}()
115
121
@@ -125,6 +131,11 @@ func (usa *UsbAdapter) StartReading(device IosDevice, receiver UsbDataReceiver,
125
131
iface .Close ()
126
132
127
133
sendQTDisableConfigControlRequest (usbDevice )
134
+ log .Debug ("Resetting device config" )
135
+ _ , err = usbDevice .Config (device .UsbMuxConfigIndex )
136
+ if err != nil {
137
+ log .Warn (err )
138
+ }
128
139
129
140
return nil
130
141
}
@@ -148,6 +159,11 @@ func grabInBulk(setting gousb.InterfaceSetting) (int, error) {
148
159
}
149
160
150
161
func grabQuickTimeInterface (config * gousb.Config ) (* gousb.Interface , error ) {
151
- _ , ifaceIndex := findInterfaceForSubclass (config .Desc , QuicktimeSubclass )
162
+ log .Debug ("Looking for quicktime interface.." )
163
+ found , ifaceIndex := findInterfaceForSubclass (config .Desc , QuicktimeSubclass )
164
+ if ! found {
165
+ return nil , fmt .Errorf ("did not find interface %v" , config )
166
+ }
167
+ log .Debugf ("Found Quicktimeinterface: %d" , ifaceIndex )
152
168
return config .Interface (ifaceIndex , 0 )
153
169
}
0 commit comments