Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Class MacAddress similar to IPAddress #6667

Closed
wants to merge 12 commits into from

Conversation

mrengineer7777
Copy link
Collaborator

@mrengineer7777 mrengineer7777 commented Apr 29, 2022

Description of Change

This adds new classes MacAddress and MacAddress8. MacAddress allows for better handling of MAC and BSSID addresses. MacAddress8 is for 8-byte EUI-64 as noted in esp_mac.h.

Test scenarios

Tested on Arduino Feather ESP32.

[env]
monitor_speed = 115200
build_flags = 

[env:fthr-esp32ard]
platform = https://github.com/tasmota/platform-espressif32/releases/download/v2.0.2.3/platform-espressif32-2.0.2.3.zip
framework = arduino
board = featheresp32

RC1

No new changes. Bugfixes accepted.

TESTING

See test project here: https://github.com/mrengineer7777/MA_Test.

Related links

Closes #6658.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 29, 2022

Unit Test Results

0 files  0 suites   0s ⏱️
0 tests 0 ✔️ 0 💤 0

Results for commit 5a0a7d5.

♻️ This comment has been updated with latest results.

@SuGlider SuGlider self-requested a review May 1, 2022 00:08
@SuGlider
Copy link
Collaborator

SuGlider commented May 1, 2022

@mrengineer7777 - Let me know when it is ready.

@SuGlider SuGlider self-assigned this May 1, 2022
@mrengineer7777
Copy link
Collaborator Author

@SuGlider Tested working. Test project here: https://github.com/mrengineer7777/MA_Test.
Can you help me with integration into the project? I don't know the proper places to declare the new files. Open to suggestions.

@mrengineer7777 mrengineer7777 marked this pull request as ready for review May 2, 2022 19:15
@mrengineer7777
Copy link
Collaborator Author

Looks like I'll need to update CMakeLists.txt if we are happy with the file locations.

@SuGlider
Copy link
Collaborator

SuGlider commented May 2, 2022

Looks like I'll need to update CMakeLists.txt if we are happy with the file locations.

Yes, it's just a matter of adding it. CMake/CI needs all files in place. Thanks!

@mrengineer7777
Copy link
Collaborator Author

@SuGlider Updated. Success.

@SuGlider
Copy link
Collaborator

SuGlider commented May 2, 2022

@mrengineer7777 Thanks!

I have an initial suggestion:

@SuGlider
Copy link
Collaborator

SuGlider commented May 2, 2022

@mrengineer7777 - Talking to @me-no-dev, he made some nice suggestions also:

1- MacAddress6 should be named just MacAddress, as it is the default size for our SoCs and mostly known. MacAddress8 seems to make a lot of sense with IPv6.
2- @me-no-dev said that you may want to also submit a PR to Arduino main GitHub, so in the future all Arduino platforms around the world will look the same regarding this class. That would be awesome!

I also would suggest some adding to the code - in the review.

Copy link
Collaborator

@SuGlider SuGlider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good! Just a few methods to make it closer to IPAddress class :-)

Same changing suggestions from Mac6 to Mac8.

@VojtechBartoska VojtechBartoska added the Area: BT&Wifi BT & Wifi related issues label May 3, 2022
@mrengineer7777
Copy link
Collaborator Author

@SuGlider I have also updated the test project

@mrengineer7777
Copy link
Collaborator Author

Just learned how to squash commits using GitHub Desktop :)

mrengineer7777 added a commit to mrengineer7777/ArduinoCore-API that referenced this pull request May 3, 2022
Adding New Classes MacAddress, MacAddress8 in the style of IPAddress.  Note I didn't include Printable, as I don't understand how that works.  If someone else wants to add it later, they are welcome to do so.  This is my first time contributing to this repository, so please feel free to point out any mistakes.

This library originated on arduino-esp32 (espressif/arduino-esp32#6667).  It was suggested that I contribute to the Arduino core as well :)
@SuGlider SuGlider self-requested a review May 5, 2022 13:36
@SuGlider
Copy link
Collaborator

SuGlider commented May 5, 2022

@mrengineer7777 | @me-no-dev

Thanks David for adding this class!
I have added all necessary operators and Printable to the classes in order to make it closer to IPAddress Arduino Class.
Let me know if you are OK with it.

@mrengineer7777
Copy link
Collaborator Author

@SuGlider Thanks for adding Printable. As the library author I prefer to keep the implementation details in the .cpp file. I realize you are copying the format of IPAddress. I also have some questions. I'm a C guy learning c++, so forgive my ignorance.

Should this be return &_mac.bytes[index];? I realize this is the same implementation as IPAddress.h.

    uint8_t& operator[](int index)
    {
        return _mac.bytes[index];
    }

What do these do? Are they needed for PrintTo()? It looks like they return a pointer to the class's private variables, which I consider extremely dangerous.

    operator const uint8_t*() const
    {
        return _mac.bytes;
    }
    operator const uint64_t*() const
    {
        return &_mac.val;
    }

@SuGlider
Copy link
Collaborator

SuGlider commented May 5, 2022

Should this be return &_mac.bytes[index];? I realize this is the same implementation as IPAddress.h.

    uint8_t& operator[](int index)
    {
        return _mac.bytes[index];
    }

uint8_t operator[](int index) const is used in getting operation, when there is no change to the memory, just reading values, for instance,
int8_t a1 = addr[1];

But, just the operator[] can't resolve something like addr[2] = 255;
For that, it's necessary a setting operator, uint8_t& operator[](int index), which returns the reference to the element in the array.

int8_t& is a reference to int8_t, in other words, it returns int8_t* type.

What do these do? Are they needed for PrintTo()? It looks like they return a pointer to the class's private variables, which I consider extremely dangerous.

    operator const uint8_t*() const
    {
        return _mac.bytes;
    }
    operator const uint64_t*() const
    {
        return &_mac.val;
    }

Those are for const type pointer casting... Read Only.
It makes easy for any sort of byte-to-byte manipulation, FancyPrint() or any other sort of data processing.

  const uint8_t *ip_8 = MacAddress(0x00, 0xCD, 0x01, 0xFE, 0xB2, 0xF0);
  const uint32_t *ip_64 = MacAddress(ip_8);

@mrengineer7777
Copy link
Collaborator Author

But, just the operator[] can't resolve something like addr[2] = 255; For that, it's necessary a setting operator, uint8_t& operator[](int index), which returns the reference to the element in the array.

Ah, that's helpful.

Those are for const type pointer casting... Read Only. It makes easy for any sort of byte-to-byte manipulation, FancyPrint() or any other sort of data processing.

  const uint8_t *ip_8 = MacAddress(0x00, 0xCD, 0x01, 0xFE, 0xB2, 0xF0);
  const uint32_t *ip_64 = MacAddress(ip_8);

Since it's read only and improves functionality I'll allow it.

Now that I understand the functionality, I'll update my test project to verify the new operators. I'll post a link when I update, likely tomorrow.

@SuGlider I would like to move the implementation details to the .cpp file. Any objections?

@SuGlider
Copy link
Collaborator

SuGlider commented May 5, 2022

Please fell free to change anything.

@SuGlider SuGlider added this to the 2.1.0 milestone May 6, 2022
@mrengineer7777
Copy link
Collaborator Author

@SuGlider I need help testing the printTo() function. I don't understand how to use it. I'll post the updates I've done so far while I wait for your reply.

@mrengineer7777
Copy link
Collaborator Author

Updated test project. Does not test printTo() yet. https://github.com/mrengineer7777/MA_Test

@SuGlider
Copy link
Collaborator

SuGlider commented May 6, 2022

@SuGlider I need help testing the printTo() function. I don't understand how to use it. I'll post the updates I've done so far while I wait for your reply.

Printable is an Interface to a Print object:
size_t MacAddress::printTo(Print& p)

The common Classes that inherit from Print Class in ESP Arduino are:

  • HardwareSerial, USBCDC, HWCDC
  • BluetoothSerial, plus some USB based classes
  • File, EEPROM, F_Fat, LittleFSFS, SDMMCFS, SPIFSFS, SDFS
  • I2SClass, TwoWire -- For some reason SPIClass isn't part of this list...
  • WiFiClient, WiFiServer, AsyncUDP, AsyncUDPPAcket, AsyncUDPMessage, UDP, WiFiUDP
  • StreamString
  Serial.begin(115200);
  MacAddress addr(0x00, 0xCD, 0x01, 0xFE, 0xB2, 0xF0);

  // it will print the mac address to Serial 
  addr.printTo(Serial);
  Serial.println();           // the line above doesn't send \n

  // it will print the mac address to a String
  StreamString sstr;
  addr.printTo(sstr);
  Serial.println(sstr);
  

@mrengineer7777
Copy link
Collaborator Author

Passes test project. Ready for review.

Chars must be uppercase to match toString() and pass test
@me-no-dev
Copy link
Member

me-no-dev commented Dec 13, 2023

@mrengineer7777 now it's time to finally review properly and merge your PR.
Here are some remarks:

  • Have a look at the official IPAddress class from Arduino Core API: https://github.com/arduino/ArduinoCore-API/blob/master/api/IPAddress.h
  • bool MacAddress::fromCStr(const char *buf); should become bool MacAddress::fromString(const char *buf); and should also allow a new constructor MacAddress::MacAddress(const char *macstr);
  • int MacAddress::toCStr(char *buf); should become int MacAddress::toString(char *buf);
  • The rest looks OK, but we should check int MacAddress:: fromString(const char *buf); for possible pitfalls
  • Also please remove the friend classes for now

@me-no-dev
Copy link
Member

MacAddress8 should probably be supported in MacAddress, just like IPv6 is supported in IPAddress. That will make things much cleaner and robust

@VojtechBartoska VojtechBartoska added the Resolution: Awaiting response Waiting for response of author label Dec 13, 2023
@mrengineer7777
Copy link
Collaborator Author

@me-no-dev I agree on combining MacAddress into one file. Will take some time for me to refactor this.

@me-no-dev
Copy link
Member

Sure thing @mrengineer7777 :) Better be done once correctly, than having to change it later or even worst, having to live with it for the rest of your life 😆

@VojtechBartoska
Copy link
Contributor

any progress on this @mrengineer7777? Thanks a lot in advance for your contribution.

@SuGlider
Copy link
Collaborator

@mrengineer7777 - it sounds like the branch of this PR doesn't allow us to change the code / rebase it.

@VojtechBartoska VojtechBartoska added Status: Pending and removed Resolution: Awaiting response Waiting for response of author labels Jan 30, 2024
@SuGlider
Copy link
Collaborator

SuGlider commented Feb 7, 2024

We may consider it for 3.0.0 instead of 3.0.0-RC1.

@P-R-O-C-H-Y
Copy link
Member

Closing this PR in favour of #9304. Thanks @mrengineer7777

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues Status: In Progress ⚠️ Issue is in progress Type: Feature request Feature request for Arduino ESP32
Projects
Development

Successfully merging this pull request may close these issues.

MacAddress.h
5 participants