Skip to content

Commit ca45b0e

Browse files
fix : Add IP address extraction to provisioning response
-Extract device IP from UDP packet source address -Follow official Espressif EsptouchForAndroid implementation -Add IP address field to ProvisioningResponse -Handle IPv4 address extraction with error handling -Fixes missing IP address that was previously not being captured from the EspTouchV2 provisioning response packets. **Contact** : vitthalphad7620@gmail.com
1 parent 6afe449 commit ca45b0e

1 file changed

Lines changed: 29 additions & 33 deletions

File tree

lib/src/protocols/esptouch_v2.dart

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,28 @@ class EspTouchV2 extends Protocol {
1414
@override
1515
List<int> get ports => [18266, 28266, 38266, 48266];
1616

17-
/// Receive data and returns response with device BSSID and IP address
18-
///
19-
/// Implementation follows official Espressif EsptouchForAndroid:
20-
/// - BSSID is extracted from response packet bytes 1-6
21-
/// - IP address is extracted from UDP packet source address
22-
///
23-
/// Throws [InvalidProvisioningResponseDataException] if received data is not valid
2417
@override
2518
ProvisioningResponse receive(Uint8List data, InternetAddress? sourceAddress) {
2619
if (data.length < 7) {
2720
throw InvalidProvisioningResponseDataException(
28-
"Invalid data ($data). Length should be at least 7 elements");
21+
"Invalid data ($data). Length should be at least 7 elements",
22+
);
2923
}
3024

31-
// Extract BSSID from bytes 1-6
32-
final response = ProvisioningResponse(Uint8List(6)..setAll(0, data.skip(1).take(6)));
25+
final response = ProvisioningResponse(
26+
Uint8List(6)..setAll(0, data.skip(1).take(6)),
27+
);
3328

3429
// Extract IP from UDP packet source address (Official Espressif method)
35-
if (sourceAddress != null && sourceAddress.type == InternetAddressType.IPv4) {
30+
// Implementation follows official Espressif EsptouchForAndroid:
31+
if (sourceAddress != null &&
32+
sourceAddress.type == InternetAddressType.IPv4) {
3633
try {
3734
final ipBytes = sourceAddress.rawAddress;
3835
if (ipBytes.length == 4) {
3936
response.ipAddress = Uint8List.fromList(ipBytes);
4037
}
41-
} catch (e) {
42-
//
43-
}
38+
} catch (e) {}
4439
}
4540

4641
return response;
@@ -102,8 +97,10 @@ class EspTouchV2 extends Protocol {
10297
plainData.addAll(request.reservedData!);
10398
}
10499

105-
final encryptedData =
106-
encrypt(Int8List.fromList(plainData), request.encryptionKey!);
100+
final encryptedData = encrypt(
101+
Int8List.fromList(plainData),
102+
request.encryptionKey!,
103+
);
107104

108105
plainData.clear();
109106

@@ -131,8 +128,10 @@ class EspTouchV2 extends Protocol {
131128
dataTmp.addAll(request.reservedData!);
132129

133130
if (_isPasswordEncoded || _isReservedDataEncoded) {
134-
final padding =
135-
_padding(_reservedPaddingFactor, request.reservedData);
131+
final padding = _padding(
132+
_reservedPaddingFactor,
133+
request.reservedData,
134+
);
136135
_reservedDataPaddingLength = padding.length;
137136
dataTmp.addAll(padding);
138137
}
@@ -174,8 +173,9 @@ class EspTouchV2 extends Protocol {
174173
}
175174

176175
final buf = Int8List(6);
177-
final read =
178-
Int8List.fromList(_buffer.skip(offset).take(expectLength).toList());
176+
final read = Int8List.fromList(
177+
_buffer.skip(offset).take(expectLength).toList(),
178+
);
179179
buf.setAll(0, read);
180180
if (read.isEmpty) {
181181
break;
@@ -212,7 +212,8 @@ class EspTouchV2 extends Protocol {
212212
} else {
213213
isReservedDataEncoded = isEncoded(request.reservedData!);
214214
headTmp.add(
215-
request.reservedData!.length | (_isReservedDataEncoded ? 0x80 : 0));
215+
request.reservedData!.length | (_isReservedDataEncoded ? 0x80 : 0),
216+
);
216217
}
217218

218219
headTmp.add(crc(request.bssid));
@@ -238,25 +239,20 @@ class EspTouchV2 extends Protocol {
238239
}
239240

240241
void _createBlocksFor6Bytes(
241-
Int8List buf, int sequence, int crc, bool tailIsCrc) {
242+
Int8List buf,
243+
int sequence,
244+
int crc,
245+
bool tailIsCrc,
246+
) {
242247
if (sequence == -1) {
243248
// first sequence
244249
final syncBlock = _syncBlock();
245250

246-
blocks.addAll([
247-
syncBlock,
248-
0,
249-
syncBlock,
250-
0,
251-
]);
251+
blocks.addAll([syncBlock, 0, syncBlock, 0]);
252252
} else {
253253
final seqBlock = _seqBlock(sequence);
254254

255-
blocks.addAll([
256-
seqBlock,
257-
seqBlock,
258-
seqBlock,
259-
]);
255+
blocks.addAll([seqBlock, seqBlock, seqBlock]);
260256
}
261257

262258
for (int bit = 0; bit < (tailIsCrc ? 7 : 8); bit++) {

0 commit comments

Comments
 (0)