Teensy 4.1 core updated for C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

126 linhas
3.7KB

  1. /*
  2. *
  3. * MIT License:
  4. * Copyright (c) 2011 Adrian McEwen
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. * adrianm@mcqn.com 1/1/2011
  24. */
  25. #ifndef IPAddress_h
  26. #define IPAddress_h
  27. #include <Printable.h>
  28. #include <WString.h>
  29. // A class to make it easier to handle and pass around IP addresses
  30. class IPAddress : public Printable {
  31. private:
  32. union {
  33. uint8_t bytes[4]; // IPv4 address
  34. uint32_t dword;
  35. } _address;
  36. // Access the raw byte array containing the address. Because this returns a pointer
  37. // to the internal structure rather than a copy of the address this function should only
  38. // be used when you know that the usage of the returned uint8_t* will be transient and not
  39. // stored.
  40. uint8_t * raw_address() { return _address.bytes; };
  41. public:
  42. // Constructors
  43. IPAddress() {
  44. _address.dword = 0;
  45. }
  46. IPAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) {
  47. _address.bytes[0] = b1;
  48. _address.bytes[1] = b2;
  49. _address.bytes[2] = b3;
  50. _address.bytes[3] = b4;
  51. }
  52. IPAddress(uint32_t address) {
  53. _address.dword = address;
  54. }
  55. IPAddress(const uint8_t *address) {
  56. // TODO: use unaligned read on Cortex-M4
  57. _address.bytes[0] = *address++;
  58. _address.bytes[1] = *address++;
  59. _address.bytes[2] = *address++;
  60. _address.bytes[3] = *address++;
  61. }
  62. bool fromString(const char *address);
  63. bool fromString(const String &address) {
  64. return fromString(address.c_str());
  65. }
  66. // Overloaded cast operator to allow IPAddress objects to be used where a pointer
  67. // to a four-byte uint8_t array is expected
  68. operator uint32_t () {
  69. return _address.dword;
  70. }
  71. bool operator==(const IPAddress& addr) {
  72. return _address.dword == addr._address.dword;
  73. }
  74. bool operator==(const uint8_t* addr) {
  75. // TODO: use unaligned read on Cortex-M4
  76. return (_address.bytes[0] == addr[0]
  77. && _address.bytes[1] == addr[1]
  78. && _address.bytes[2] == addr[2]
  79. && _address.bytes[3] == addr[3]);
  80. }
  81. // Overloaded index operator to allow getting and setting individual octets of the address
  82. uint8_t operator[](int index) const {
  83. return _address.bytes[index];
  84. };
  85. uint8_t& operator[](int index) {
  86. return _address.bytes[index];
  87. };
  88. // Overloaded copy operators to allow initialisation of IPAddress objects from other types
  89. IPAddress& operator=(const uint8_t *address) {
  90. // TODO: use unaligned read on Cortex-M4
  91. _address.bytes[0] = *address++;
  92. _address.bytes[1] = *address++;
  93. _address.bytes[2] = *address++;
  94. _address.bytes[3] = *address++;
  95. return *this;
  96. }
  97. IPAddress& operator=(uint32_t address) {
  98. _address.dword = address;
  99. return *this;
  100. }
  101. virtual size_t printTo(Print& p) const;
  102. friend class EthernetClass;
  103. friend class UDP;
  104. friend class Client;
  105. friend class Server;
  106. friend class DhcpClass;
  107. friend class DNSClient;
  108. };
  109. const IPAddress INADDR_NONE((uint32_t)0);
  110. #endif