|
| 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 | +#include <Arduino.h> |
| 21 | +#include <IPAddress.h> |
| 22 | + |
| 23 | +IPAddress::IPAddress() |
| 24 | +{ |
| 25 | + _address.dword = 0; |
| 26 | +} |
| 27 | + |
| 28 | +IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) |
| 29 | +{ |
| 30 | + _address.bytes[0] = first_octet; |
| 31 | + _address.bytes[1] = second_octet; |
| 32 | + _address.bytes[2] = third_octet; |
| 33 | + _address.bytes[3] = fourth_octet; |
| 34 | +} |
| 35 | + |
| 36 | +IPAddress::IPAddress(uint32_t address) |
| 37 | +{ |
| 38 | + _address.dword = address; |
| 39 | +} |
| 40 | + |
| 41 | +IPAddress::IPAddress(const uint8_t *address) |
| 42 | +{ |
| 43 | + memcpy(_address.bytes, address, sizeof(_address.bytes)); |
| 44 | +} |
| 45 | + |
| 46 | +bool IPAddress::fromString(const char *address) |
| 47 | +{ |
| 48 | + // TODO: add support for "a", "a.b", "a.b.c" formats |
| 49 | + |
| 50 | + uint16_t acc = 0; // Accumulator |
| 51 | + uint8_t dots = 0; |
| 52 | + |
| 53 | + while (*address) |
| 54 | + { |
| 55 | + char c = *address++; |
| 56 | + if (c >= '0' && c <= '9') |
| 57 | + { |
| 58 | + acc = acc * 10 + (c - '0'); |
| 59 | + if (acc > 255) { |
| 60 | + // Value out of [0..255] range |
| 61 | + return false; |
| 62 | + } |
| 63 | + } |
| 64 | + else if (c == '.') |
| 65 | + { |
| 66 | + if (dots == 3) { |
| 67 | + // Too much dots (there must be 3 dots) |
| 68 | + return false; |
| 69 | + } |
| 70 | + _address.bytes[dots++] = acc; |
| 71 | + acc = 0; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + // Invalid char |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (dots != 3) { |
| 81 | + // Too few dots (there must be 3 dots) |
| 82 | + return false; |
| 83 | + } |
| 84 | + _address.bytes[3] = acc; |
| 85 | + return true; |
| 86 | +} |
| 87 | + |
| 88 | +IPAddress& IPAddress::operator=(const uint8_t *address) |
| 89 | +{ |
| 90 | + memcpy(_address.bytes, address, sizeof(_address.bytes)); |
| 91 | + return *this; |
| 92 | +} |
| 93 | + |
| 94 | +IPAddress& IPAddress::operator=(uint32_t address) |
| 95 | +{ |
| 96 | + _address.dword = address; |
| 97 | + return *this; |
| 98 | +} |
| 99 | + |
| 100 | +bool IPAddress::operator==(const uint8_t* addr) const |
| 101 | +{ |
| 102 | + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; |
| 103 | +} |
| 104 | + |
| 105 | +size_t IPAddress::printTo(Print& p) const |
| 106 | +{ |
| 107 | + size_t n = 0; |
| 108 | + for (int i =0; i < 3; i++) |
| 109 | + { |
| 110 | + n += p.print(_address.bytes[i], DEC); |
| 111 | + n += p.print('.'); |
| 112 | + } |
| 113 | + n += p.print(_address.bytes[3], DEC); |
| 114 | + return n; |
| 115 | +} |
| 116 | + |
0 commit comments