pytest_base_mac_address.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: CC0-1.0
  3. import pytest
  4. from pytest_embedded import Dut
  5. @pytest.mark.supported_targets
  6. @pytest.mark.generic
  7. def test_base_mac_address(dut: Dut) -> None:
  8. def get_hex_r(num_bytes: int) -> str:
  9. return r', '.join((r'0x([0-9a-f]{1,2})',) * num_bytes)
  10. hex_r = get_hex_r(6)
  11. dut.expect_exact('BASE_MAC: Base MAC Address read from EFUSE BLK0')
  12. mac_m = dut.expect(r'BASE_MAC: Using "' + hex_r + r'" as base MAC address', timeout=5).groups()
  13. def get_expected_mac_string(increment: int, target: str) -> str:
  14. '''
  15. Return the string representation of the MAC address mac_m with the last octet incremented.
  16. mac_m is an array of strings in hexa-decimal format without the '0x' prefix.
  17. '''
  18. # as a result of some esp32s2 chips burned with one MAC address by mistake,
  19. # there are some MAC address are reserved for this bug fix.
  20. # related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
  21. # reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
  22. if target == 'esp32s2' and increment == 1:
  23. hex_string = ''.join([m.decode('utf8').rjust(2, '0') for m in mac_m])
  24. mac_bytes = int(hex_string, 16)
  25. if mac_bytes >= int('7cdfa1003000', 16) and mac_bytes <= int('7cdfa1005fff', 16):
  26. mac_bytes += int('1d000', 16)
  27. hex_string = f'{mac_bytes:x}'
  28. # Format the new string to match the expected output from the app (includes stripping leading zeroes)
  29. return ', '.join('0x{}'.format(hex_string[i:i + 2].lstrip('0')) for i in range(0, len(hex_string), 2))
  30. return ', '.join(['0x{}'.format(m.decode('utf8')) for m in mac_m[:-1]] + [hex((int(mac_m[-1], 16) + increment) & 0xFF)])
  31. sdkconfig = dut.app.sdkconfig
  32. if sdkconfig.get('ESP_WIFI_ENABLED'):
  33. dut.expect_exact('WIFI_STA MAC: ' + get_expected_mac_string(0, dut.target), timeout=2)
  34. dut.expect_exact('SoftAP MAC: ' + get_expected_mac_string(1, dut.target))
  35. if dut.target != 'esp32s2' and dut.target != 'esp32h2':
  36. if sdkconfig.get('ESP_MAC_ADDR_UNIVERSE_BT'):
  37. dut.expect_exact('BT MAC: ' + get_expected_mac_string(2, dut.target))
  38. dut.expect_exact('Ethernet MAC: ' + get_expected_mac_string(3, dut.target))
  39. dut.expect_exact('New Ethernet MAC: ' + get_expected_mac_string(6, dut.target))
  40. elif dut.target == 'esp32h2':
  41. dut.expect_exact('BT MAC: ' + get_expected_mac_string(0, dut.target))
  42. dut.expect_exact('New Ethernet MAC: ' + get_expected_mac_string(6, dut.target))
  43. if sdkconfig.get('SOC_IEEE802154_SUPPORTED'):
  44. mac_ext_m = dut.expect(r'MAC_EXT: ' + get_hex_r(2), timeout=5).groups()
  45. mac_ext = ['0x{}'.format(m.decode('utf8')) for m in mac_ext_m]
  46. mac = ['0x{}'.format(m.decode('utf8')) for m in mac_m]
  47. expected_eui64 = f'{mac[0]}, {mac[1]}, {mac[2]}, {mac_ext[0]}, {mac_ext[1]}, {mac[3]}, {mac[4]}, {mac[5]}'
  48. dut.expect_exact(r'IEEE802154: ' + expected_eui64, timeout=5)