IPAddress.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. IPAddress.cpp - Base class that provides IPAddress
  3. Copyright (c) 2011 Adrian McEwen. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include <Arduino.h>
  17. #include <IPAddress.h>
  18. IPAddress::IPAddress()
  19. {
  20. _address.dword = 0;
  21. }
  22. IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
  23. {
  24. _address.bytes[0] = first_octet;
  25. _address.bytes[1] = second_octet;
  26. _address.bytes[2] = third_octet;
  27. _address.bytes[3] = fourth_octet;
  28. }
  29. IPAddress::IPAddress(uint32_t address)
  30. {
  31. _address.dword = address;
  32. }
  33. IPAddress::IPAddress(const uint8_t *address)
  34. {
  35. memcpy(_address.bytes, address, sizeof(_address.bytes));
  36. }
  37. IPAddress& IPAddress::operator=(const uint8_t *address)
  38. {
  39. memcpy(_address.bytes, address, sizeof(_address.bytes));
  40. return *this;
  41. }
  42. IPAddress& IPAddress::operator=(uint32_t address)
  43. {
  44. _address.dword = address;
  45. return *this;
  46. }
  47. bool IPAddress::operator==(const uint8_t* addr) const
  48. {
  49. return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
  50. }
  51. size_t IPAddress::printTo(Print& p) const
  52. {
  53. size_t n = 0;
  54. for (int i =0; i < 3; i++)
  55. {
  56. n += p.print(_address.bytes[i], DEC);
  57. n += p.print('.');
  58. }
  59. n += p.print(_address.bytes[3], DEC);
  60. return n;
  61. }