[136] | 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 | memset(_address, 0, sizeof(_address));
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
|
---|
| 29 | {
|
---|
| 30 | _address[0] = first_octet;
|
---|
| 31 | _address[1] = second_octet;
|
---|
| 32 | _address[2] = third_octet;
|
---|
| 33 | _address[3] = fourth_octet;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | IPAddress::IPAddress(uint32_t address)
|
---|
| 37 | {
|
---|
| 38 | memcpy(_address, &address, sizeof(_address));
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | IPAddress::IPAddress(const uint8_t *address)
|
---|
| 42 | {
|
---|
| 43 | memcpy(_address, address, sizeof(_address));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | IPAddress& IPAddress::operator=(const uint8_t *address)
|
---|
| 47 | {
|
---|
| 48 | memcpy(_address, address, sizeof(_address));
|
---|
| 49 | return *this;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | IPAddress& IPAddress::operator=(uint32_t address)
|
---|
| 53 | {
|
---|
| 54 | memcpy(_address, (const uint8_t *)&address, sizeof(_address));
|
---|
| 55 | return *this;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | bool IPAddress::operator==(const uint8_t* addr) const
|
---|
| 59 | {
|
---|
| 60 | return memcmp(addr, _address, sizeof(_address)) == 0;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | size_t IPAddress::printTo(Print& p) const
|
---|
| 64 | {
|
---|
| 65 | size_t n = 0;
|
---|
| 66 | for (int i =0; i < 3; i++)
|
---|
| 67 | {
|
---|
| 68 | n += p.print(_address[i], DEC);
|
---|
| 69 | n += p.print('.');
|
---|
| 70 | }
|
---|
| 71 | n += p.print(_address[3], DEC);
|
---|
| 72 | return n;
|
---|
| 73 | }
|
---|
| 74 |
|
---|