Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * IPAddress.cpp - Base class that provides IPAddress
  3. * Copyright (c) 2011 Adrian McEwen. All right reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. *
  20. * Modified by Marcelo Aquino <marceloaqno@gmail.org> for MySensors use
  21. */
  22. #include <cstdio>
  23. #include <cstring>
  24. #include "IPAddress.h"
  25. IPAddress::IPAddress()
  26. {
  27. _address.dword = 0;
  28. }
  29. IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet,
  30. uint8_t fourth_octet)
  31. {
  32. _address.bytes[0] = first_octet;
  33. _address.bytes[1] = second_octet;
  34. _address.bytes[2] = third_octet;
  35. _address.bytes[3] = fourth_octet;
  36. }
  37. IPAddress::IPAddress(uint32_t address)
  38. {
  39. _address.dword = address;
  40. }
  41. IPAddress::IPAddress(const uint8_t *address)
  42. {
  43. memcpy(_address.bytes, address, sizeof(_address.bytes));
  44. }
  45. bool IPAddress::fromString(const char *address)
  46. {
  47. // TODO: add support for "a", "a.b", "a.b.c" formats
  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. IPAddress& IPAddress::operator=(const uint8_t *address)
  78. {
  79. memcpy(_address.bytes, address, sizeof(_address.bytes));
  80. return *this;
  81. }
  82. IPAddress& IPAddress::operator=(uint32_t address)
  83. {
  84. _address.dword = address;
  85. return *this;
  86. }
  87. bool IPAddress::operator==(const uint8_t* addr) const
  88. {
  89. return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
  90. }
  91. std::string IPAddress::toString()
  92. {
  93. char szRet[16];
  94. sprintf(szRet,"%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2],
  95. _address.bytes[3]);
  96. return std::string(szRet);
  97. }