payload property

  1. @override
Uint8List? get payload
override

The payload data of the record.

Implementation

@override
Uint8List? get payload {
  if (ssid == null) {
    throw ArgumentError('SSID is required');
  }

  // Open networks don't require a password
  if (authenticationType != WifiAuthenticationType.open &&
      (networkKey == null || networkKey!.isEmpty)) {
    throw ArgumentError(
      'Network key is required for secured networks',
    );
  }

  final List<Uint8List> credentialData = [];

  // Network Index
  credentialData.add(
    _encodeTLV(_attrNetworkIndex, Uint8List.fromList([networkIndex])),
  );

  // SSID
  final ssidBytes = utf8.encode(ssid!);
  credentialData.add(_encodeTLV(_attrSsid, Uint8List.fromList(ssidBytes)));

  // Authentication Type
  final authTypeBytes = Uint8List(2);
  authTypeBytes[0] = (authenticationType.wscValue >> 8) & 0xFF;
  authTypeBytes[1] = authenticationType.wscValue & 0xFF;
  credentialData.add(_encodeTLV(_attrAuthType, authTypeBytes));

  // Encryption Type
  final encTypeBytes = Uint8List(2);
  encTypeBytes[0] = (encryptionType.wscValue >> 8) & 0xFF;
  encTypeBytes[1] = encryptionType.wscValue & 0xFF;
  credentialData.add(_encodeTLV(_attrEncryptionType, encTypeBytes));

  // Network Key (if not open)
  if (authenticationType != WifiAuthenticationType.open &&
      networkKey != null &&
      networkKey!.isNotEmpty) {
    final keyBytes = utf8.encode(networkKey!);
    credentialData.add(
      _encodeTLV(_attrNetworkKey, Uint8List.fromList(keyBytes)),
    );
  }

  // MAC Address (optional)
  if (macAddress != null) {
    final macBytes = _macAddressToBytes(macAddress)!;
    credentialData.add(_encodeTLV(_attrMacAddress, macBytes));
  }

  // Combine all credential data
  final allCredentialData = <int>[];
  for (var data in credentialData) {
    allCredentialData.addAll(data);
  }

  // Create credential container with length
  final credentialLength = Uint8List(2);
  credentialLength[0] = (allCredentialData.length >> 8) & 0xFF;
  credentialLength[1] = allCredentialData.length & 0xFF;

  final credentialType = Uint8List(2);
  credentialType[0] = (_attrCredential >> 8) & 0xFF;
  credentialType[1] = _attrCredential & 0xFF;

  return Uint8List.fromList([
    ...credentialType,
    ...credentialLength,
    ...allCredentialData,
  ]);
}
  1. @override
set payload (Uint8List? payload)
override

The payload data of the record.

Implementation

@override
set payload(Uint8List? payload) {
  if (payload == null || payload.isEmpty) {
    throw ArgumentError('Payload cannot be null or empty');
  }

  final stream = ByteStream(payload);

  // Read credential container
  final credentialType = stream.readInt(2);
  if (credentialType != _attrCredential) {
    throw ArgumentError(
      'Invalid WiFi record format: expected credential container',
    );
  }

  final credentialLength = stream.readInt(2);
  final endPosition = stream.readLength + credentialLength;

  // Parse attributes
  while (stream.readLength < endPosition && !stream.isEnd()) {
    final attrType = stream.readInt(2);
    final attrLength = stream.readInt(2);
    final attrValue = stream.readBytes(attrLength);

    switch (attrType) {
      case _attrSsid:
        ssid = utf8.decode(attrValue);
        break;
      case _attrNetworkKey:
        networkKey = utf8.decode(attrValue);
        break;
      case _attrAuthType:
        final authValue = (attrValue[0] << 8) | attrValue[1];
        authenticationType = WifiAuthenticationType.fromWscValue(authValue);
        break;
      case _attrEncryptionType:
        final encValue = (attrValue[0] << 8) | attrValue[1];
        encryptionType = WifiEncryptionType.fromWscValue(encValue);
        break;
      case _attrMacAddress:
        if (attrValue.length == 6) {
          macAddress = _macAddressToString(attrValue);
        }
        break;
      case _attrNetworkIndex:
        if (attrValue.isNotEmpty) {
          networkIndex = attrValue[0];
        }
        break;
    }
  }
}