Skip to content

Commit 74fd9d9

Browse files
Add ESP32 MAC address retrieval in ESPTouch v2
1 parent aa176e6 commit 74fd9d9

4 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/src/protocol.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ abstract class Protocol {
127127
/// Receive data and returns response with device BSSID
128128
///
129129
/// Throws [InvalidProvisioningResponseDataException] if data of received response is invalid
130-
ProvisioningResponse receive(Uint8List data) {
130+
///
131+
/// Added optional [sourceAddress] so derived protocols can extract IP address
132+
/// from the UDP packet source when available.
133+
ProvisioningResponse receive(Uint8List data, [InternetAddress? sourceAddress]) {
131134
if (data.length < 7) {
132135
throw InvalidProvisioningResponseDataException(
133136
"Invalid data ($data). Length should be at least 7 elements");

lib/src/protocols/esptouch.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:io';
12
import 'dart:typed_data';
23

34
import 'package:esp_smartconfig/esp_smartconfig.dart';
@@ -85,8 +86,11 @@ class EspTouch extends Protocol {
8586
/// Receive data and returns response with device BSSID and IP address
8687
///
8788
/// Throws [InvalidProvisioningResponseDataException] if received data is not valid
89+
///
90+
/// Signature updated to match [Protocol.receive] and support optional
91+
/// source address forwarding from [Provisioner].
8892
@override
89-
ProvisioningResponse receive(Uint8List data) {
93+
ProvisioningResponse receive(Uint8List data, [InternetAddress? sourceAddress]) {
9094
final response = super.receive(data);
9195

9296
if (data[0] != _expectedResponseFirstByte) {

lib/src/protocols/esptouch_v2.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class EspTouchV2 extends Protocol {
1818
List<int> get ports => [18266, 28266, 38266, 48266];
1919

2020
@override
21-
ProvisioningResponse receive(Uint8List data, InternetAddress? sourceAddress) {
21+
ProvisioningResponse receive(Uint8List data, [InternetAddress? sourceAddress]) {
22+
// Allow optional source address to be passed through from Provisioner.
2223
if (data.length < 7) {
2324
throw InvalidProvisioningResponseDataException(
2425
"Invalid data ($data). Length should be at least 7 elements",

lib/src/provisioner.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ class Provisioner {
155155
}
156156

157157
try {
158-
final response = protocol.receive(pkg.data);
158+
// Pass the UDP packet source address to protocol.receive so V2
159+
// protocols can extract the device IP from the response.
160+
final response = protocol.receive(pkg.data, pkg.address);
159161

160162
if (protocol.findResponse(response) != null) {
161163
// Same response already received

0 commit comments

Comments
 (0)