forked from haowanxing/esptouch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esptouchTask.go
226 lines (214 loc) · 6.37 KB
/
esptouchTask.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package esptouch
import (
"errors"
"fmt"
"github.com/haowanxing/esptouch-go/protocol"
"github.com/haowanxing/esptouch-go/task"
"github.com/haowanxing/esptouch-go/utils/byteutil"
"log"
"net"
"time"
)
type EsptouchTask struct {
parameter *task.EsptouchParameter
apSsid []byte
apPassword []byte
apBssid []byte
udpClient *net.UDPConn
mEsptouchResultList []IEsptouchResult
mBssidTaskSucCountMap map[string]int
mIsInterrupt bool
mIsExecuted bool
mIsSuc bool
}
func NewEsptouchTask(apSsid, apPassword, apBssid []byte) (*EsptouchTask, error) {
if apSsid == nil || len(apSsid) == 0 {
return nil, errors.New("SSID can't be empty")
}
if apBssid == nil || len(apBssid) != 6 {
return nil, errors.New("BSSID is empty or length is not 6")
}
if apPassword == nil {
apPassword = []byte("")
}
mParameter := task.NewEsptouchParameter()
conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: nil,
Port: mParameter.GetPortListening(),
})
if err != nil {
return nil, err
}
return &EsptouchTask{
parameter: mParameter,
apSsid: apSsid,
apPassword: apPassword,
apBssid: apBssid,
udpClient: conn,
mEsptouchResultList: make([]IEsptouchResult, 0),
mBssidTaskSucCountMap: make(map[string]int),
}, nil
}
func (p *EsptouchTask) checkTaskValid() {
if p.mIsExecuted {
panic("the Esptouch task could be executed only once")
}
p.mIsExecuted = true
}
func (p *EsptouchTask) putEsptouchResult(isSuc bool, bssid string, ip net.IP) {
var count int
if c, ok := p.mBssidTaskSucCountMap[bssid]; ok {
count = c
}
count++
p.mBssidTaskSucCountMap[bssid] = count
if !(count >= p.parameter.GetThresholdSucBroadcastCount()) {
return
}
var isExist = false
for _, esptouchResultInList := range p.mEsptouchResultList {
if esptouchResultInList.GetBssid() == bssid {
isExist = true
break
}
}
if !isExist {
esptouchResult := NewEsptouchResult(isSuc, bssid, ip)
p.mEsptouchResultList = append(p.mEsptouchResultList, esptouchResult)
}
}
func (p *EsptouchTask) listenAsync(expectDataLen int) {
var startTime = time.Now()
var receiveBytes = make([]byte, expectDataLen)
var receiveOneByte byte = 0
for {
if len(p.mEsptouchResultList) >= p.parameter.GetExpectTaskResultCount() || p.mIsInterrupt {
break
}
var expectOneByte = byte(len(p.apSsid) + len(p.apPassword) + 9)
n, _, err := p.udpClient.ReadFromUDP(receiveBytes)
if err != nil {
log.Println("read udp err", err, n)
}
if n > 0 {
receiveOneByte = receiveBytes[0]
}
if receiveOneByte == expectOneByte {
consume := time.Now().Sub(startTime).Milliseconds()
timeout := p.parameter.GetWaitUdpTotalMillisecond() - consume
if timeout < 0 {
break
} else {
if len(receiveBytes) > 1 {
var bssid = byteutil.ParseBssid(receiveBytes, p.parameter.GetEsptouchResultOneLen(), p.parameter.GetEsptouchResultMacLen())
var inetAddress = byteutil.ParseInetAddr(receiveBytes, p.parameter.GetEsptouchResultOneLen()+p.parameter.GetEsptouchResultMacLen(), p.parameter.GetEsptouchResultIpLen())
//log.Printf("[Success] Bssid: %s, IP: %s", bssid, inetAddress)
p.putEsptouchResult(true, bssid, inetAddress)
}
}
}
}
p.mIsSuc = len(p.mEsptouchResultList) >= p.parameter.GetExpectTaskResultCount()
p.interrupt()
}
func (p *EsptouchTask) getEsptouchResultList() []IEsptouchResult {
if len(p.mEsptouchResultList) == 0 {
esptouchResultFail := NewEsptouchResult(false, "", nil)
p.mEsptouchResultList = append(p.mEsptouchResultList, esptouchResultFail)
}
return p.mEsptouchResultList
}
func (p *EsptouchTask) execute(generator *protocol.EsptouchGenerator) bool {
startTime := time.Now().UnixNano() / 1e6
currentTime := startTime
lastTime := currentTime - p.parameter.GetTimeoutTotalCodeMillisecond()
gc := generator.GetGCBytes2()
dc := generator.GetDCBytes2()
index := 0
for {
if !p.mIsInterrupt {
if currentTime-lastTime >= p.parameter.GetTimeoutTotalCodeMillisecond() {
for {
if !p.mIsInterrupt && time.Now().UnixNano()/1e6-currentTime < p.parameter.GetTimeoutGuideCodeMillisecond() {
p.sendData(gc, 0, int64(len(gc)), p.parameter.GetIntervalGuideCodeMillisecond())
} else {
index = 0
break
}
if time.Now().UnixNano()/1e6-startTime > p.parameter.GetWaitUdpSendingMillisecond() {
fmt.Println("Wait udp end.")
break
}
}
lastTime = currentTime
} else {
p.sendData(dc, int64(index), 3, p.parameter.GetIntervalDataCodeMillisecond())
index = (index + 3) % len(dc)
}
currentTime = time.Now().UnixNano() / 1e6
if currentTime-startTime > p.parameter.GetWaitUdpSendingMillisecond() {
log.Println("UDP Send Timeout.")
break
}
} else {
break
}
}
return p.mIsSuc
}
func (p *EsptouchTask) sendData(data [][]byte, offset, count int64, interval int64) {
ip := net.ParseIP(p.parameter.GetTargetHostname())
for i := offset; i < offset+count; i++ {
if len(data[i]) == 0 {
continue
}
_, _ = p.udpClient.WriteToUDP(data[i], &net.UDPAddr{
IP: ip,
Port: p.parameter.GetTargetPort(),
})
time.Sleep(time.Millisecond * time.Duration(interval))
}
}
func (p *EsptouchTask) interrupt() {
if !p.mIsInterrupt {
p.mIsInterrupt = true
}
}
func (p *EsptouchTask) Interrupt() {
p.interrupt()
}
func (p *EsptouchTask) ExecuteForResults(expectTaskResultCount int) []IEsptouchResult {
p.checkTaskValid()
p.parameter.SetExpectTaskResultCount(expectTaskResultCount)
var ipAddress net.IP
netInterfaces, _ := net.Interfaces()
for _, v := range netInterfaces {
if (v.Flags & net.FlagUp) != 0 {
addrs, _ := v.Addrs()
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ipAddress = ipnet.IP
}
}
}
}
}
generator := protocol.NewEsptouchGenerator(p.apSsid, p.apBssid, p.apPassword, ipAddress)
go p.listenAsync(p.parameter.GetEsptouchResultTotalLen())
var isSuc = false
for i := 0; i < p.parameter.GetTotalRepeatTime(); i++ {
isSuc = p.execute(generator)
if isSuc {
return p.getEsptouchResultList()
}
}
if !p.mIsInterrupt {
time.Sleep(time.Millisecond * time.Duration(p.parameter.GetWaitUdpReceivingMillisecond()))
}
p.interrupt()
return p.getEsptouchResultList()
}
func (p *EsptouchTask) SetPackageBroadcast(broadcast bool) {
p.parameter.SetBroadcast(broadcast)
}