PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IPAddress.cpp 1.7KB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #if ARDUINO >= 100
  2. #include "Arduino.h"
  3. #include "IPAddress.h"
  4. IPAddress::IPAddress()
  5. {
  6. _address.dword = 0;
  7. }
  8. IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
  9. {
  10. _address.bytes[0] = first_octet;
  11. _address.bytes[1] = second_octet;
  12. _address.bytes[2] = third_octet;
  13. _address.bytes[3] = fourth_octet;
  14. }
  15. IPAddress::IPAddress(uint32_t address)
  16. {
  17. _address.dword = address;
  18. }
  19. IPAddress::IPAddress(const uint8_t *address)
  20. {
  21. memcpy(_address.bytes, address, sizeof(_address));
  22. }
  23. IPAddress& IPAddress::operator=(const uint8_t *address)
  24. {
  25. memcpy(_address.bytes, address, sizeof(_address.bytes));
  26. return *this;
  27. }
  28. IPAddress& IPAddress::operator=(uint32_t address)
  29. {
  30. _address.dword = address;
  31. return *this;
  32. }
  33. bool IPAddress::operator==(const uint8_t* addr)
  34. {
  35. return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
  36. }
  37. size_t IPAddress::printTo(Print& p) const
  38. {
  39. for (int i= 0; i < 3; i++) {
  40. p.print(_address.bytes[i], DEC);
  41. p.print('.');
  42. }
  43. p.print(_address.bytes[3], DEC);
  44. return 4;
  45. }
  46. bool IPAddress::fromString(const char *address)
  47. {
  48. uint16_t acc = 0; // Accumulator
  49. uint8_t dots = 0;
  50. while (*address) {
  51. char c = *address++;
  52. if (c >= '0' && c <= '9') {
  53. acc = acc * 10 + (c - '0');
  54. if (acc > 255) {
  55. // Value out of [0..255] range
  56. return false;
  57. }
  58. } else if (c == '.') {
  59. if (dots == 3) {
  60. // Too much dots (there must be 3 dots)
  61. return false;
  62. }
  63. _address.bytes[dots++] = acc;
  64. acc = 0;
  65. } else {
  66. // Invalid char
  67. return false;
  68. }
  69. }
  70. if (dots != 3) {
  71. // Too few dots (there must be 3 dots)
  72. return false;
  73. }
  74. _address.bytes[3] = acc;
  75. return true;
  76. }
  77. #endif