123
IpAddress.cpp@11:647d53d146f1, 2019-08-30 (annotated)
- Committer:
- hudakz
- Date:
- Fri Aug 30 08:11:40 2019 +0000
- Revision:
- 11:647d53d146f1
- Parent:
- 10:e4ddab81e6a8
"set_network" method of Mbed style added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 9:a156d3de5647 | 1 | /* |
hudakz | 9:a156d3de5647 | 2 | IPAddress.cpp - Base class that provides IPAddress |
hudakz | 9:a156d3de5647 | 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. |
hudakz | 9:a156d3de5647 | 4 | |
hudakz | 9:a156d3de5647 | 5 | Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> |
hudakz | 9:a156d3de5647 | 6 | |
hudakz | 9:a156d3de5647 | 7 | This library is free software; you can redistribute it and/or |
hudakz | 9:a156d3de5647 | 8 | modify it under the terms of the GNU Lesser General Public |
hudakz | 9:a156d3de5647 | 9 | License as published by the Free Software Foundation; either |
hudakz | 9:a156d3de5647 | 10 | version 2.1 of the License, or (at your option) any later version. |
hudakz | 9:a156d3de5647 | 11 | |
hudakz | 9:a156d3de5647 | 12 | This library is distributed in the hope that it will be useful, |
hudakz | 9:a156d3de5647 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 9:a156d3de5647 | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
hudakz | 9:a156d3de5647 | 15 | Lesser General Public License for more details. |
hudakz | 9:a156d3de5647 | 16 | |
hudakz | 9:a156d3de5647 | 17 | You should have received a copy of the GNU Lesser General Public |
hudakz | 9:a156d3de5647 | 18 | License along with this library; if not, write to the Free Software |
hudakz | 9:a156d3de5647 | 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
hudakz | 9:a156d3de5647 | 20 | */ |
hudakz | 9:a156d3de5647 | 21 | #include <stdio.h> |
hudakz | 9:a156d3de5647 | 22 | #include "mbed.h" |
hudakz | 9:a156d3de5647 | 23 | #include "IpAddress.h" |
hudakz | 9:a156d3de5647 | 24 | |
hudakz | 9:a156d3de5647 | 25 | /** |
hudakz | 9:a156d3de5647 | 26 | * @brief |
hudakz | 9:a156d3de5647 | 27 | * @note |
hudakz | 9:a156d3de5647 | 28 | * @param |
hudakz | 9:a156d3de5647 | 29 | * @retval |
hudakz | 9:a156d3de5647 | 30 | */ |
hudakz | 11:647d53d146f1 | 31 | IpAddress::IpAddress(void) |
hudakz | 11:647d53d146f1 | 32 | { |
hudakz | 9:a156d3de5647 | 33 | memset(_address, 0, sizeof(_address)); |
hudakz | 9:a156d3de5647 | 34 | } |
hudakz | 9:a156d3de5647 | 35 | |
hudakz | 9:a156d3de5647 | 36 | /** |
hudakz | 9:a156d3de5647 | 37 | * @brief |
hudakz | 9:a156d3de5647 | 38 | * @note |
hudakz | 9:a156d3de5647 | 39 | * @param |
hudakz | 9:a156d3de5647 | 40 | * @retval |
hudakz | 9:a156d3de5647 | 41 | */ |
hudakz | 11:647d53d146f1 | 42 | IpAddress::IpAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) |
hudakz | 11:647d53d146f1 | 43 | { |
hudakz | 9:a156d3de5647 | 44 | _address[0] = octet1; |
hudakz | 9:a156d3de5647 | 45 | _address[1] = octet2; |
hudakz | 9:a156d3de5647 | 46 | _address[2] = octet3; |
hudakz | 9:a156d3de5647 | 47 | _address[3] = octet4; |
hudakz | 9:a156d3de5647 | 48 | } |
hudakz | 9:a156d3de5647 | 49 | |
hudakz | 9:a156d3de5647 | 50 | /** |
hudakz | 9:a156d3de5647 | 51 | * @brief |
hudakz | 9:a156d3de5647 | 52 | * @note |
hudakz | 9:a156d3de5647 | 53 | * @param |
hudakz | 9:a156d3de5647 | 54 | * @retval |
hudakz | 9:a156d3de5647 | 55 | */ |
hudakz | 11:647d53d146f1 | 56 | IpAddress::IpAddress(uint32_t address) |
hudakz | 11:647d53d146f1 | 57 | { |
hudakz | 9:a156d3de5647 | 58 | memcpy(_address, &address, sizeof(_address)); |
hudakz | 9:a156d3de5647 | 59 | } |
hudakz | 9:a156d3de5647 | 60 | |
hudakz | 9:a156d3de5647 | 61 | /** |
hudakz | 9:a156d3de5647 | 62 | * @brief |
hudakz | 9:a156d3de5647 | 63 | * @note |
hudakz | 9:a156d3de5647 | 64 | * @param |
hudakz | 9:a156d3de5647 | 65 | * @retval |
hudakz | 9:a156d3de5647 | 66 | */ |
hudakz | 11:647d53d146f1 | 67 | IpAddress::IpAddress(const uint8_t address[4]) |
hudakz | 11:647d53d146f1 | 68 | { |
hudakz | 9:a156d3de5647 | 69 | memcpy(_address, address, sizeof(_address)); |
hudakz | 9:a156d3de5647 | 70 | } |
hudakz | 9:a156d3de5647 | 71 | |
hudakz | 9:a156d3de5647 | 72 | /** |
hudakz | 9:a156d3de5647 | 73 | * @brief |
hudakz | 9:a156d3de5647 | 74 | * @note |
hudakz | 9:a156d3de5647 | 75 | * @param |
hudakz | 9:a156d3de5647 | 76 | * @retval |
hudakz | 9:a156d3de5647 | 77 | */ |
hudakz | 11:647d53d146f1 | 78 | IpAddress::IpAddress(const char* str, size_t len) |
hudakz | 11:647d53d146f1 | 79 | { |
hudakz | 11:647d53d146f1 | 80 | uint8_t pos = 0; |
hudakz | 11:647d53d146f1 | 81 | uint8_t byte; |
hudakz | 11:647d53d146f1 | 82 | uint8_t i = 0; |
hudakz | 11:647d53d146f1 | 83 | |
hudakz | 11:647d53d146f1 | 84 | if (len > 16) |
hudakz | 11:647d53d146f1 | 85 | return; |
hudakz | 11:647d53d146f1 | 86 | |
hudakz | 11:647d53d146f1 | 87 | while (true) { |
hudakz | 11:647d53d146f1 | 88 | if (pos == len || str[pos] < '0' || str[pos] > '9') { |
hudakz | 11:647d53d146f1 | 89 | return; |
hudakz | 11:647d53d146f1 | 90 | } |
hudakz | 11:647d53d146f1 | 91 | |
hudakz | 11:647d53d146f1 | 92 | byte = 0; |
hudakz | 11:647d53d146f1 | 93 | while (pos < len && str[pos] >= '0' && str[pos] <= '9') { |
hudakz | 11:647d53d146f1 | 94 | byte *= 10; |
hudakz | 11:647d53d146f1 | 95 | byte += str[pos++] - '0'; |
hudakz | 11:647d53d146f1 | 96 | } |
hudakz | 11:647d53d146f1 | 97 | |
hudakz | 11:647d53d146f1 | 98 | _address[i++] = byte; |
hudakz | 11:647d53d146f1 | 99 | |
hudakz | 11:647d53d146f1 | 100 | if (i == 4) { |
hudakz | 11:647d53d146f1 | 101 | return; |
hudakz | 11:647d53d146f1 | 102 | } |
hudakz | 11:647d53d146f1 | 103 | |
hudakz | 11:647d53d146f1 | 104 | if (pos == len || str[pos++] != '.') { |
hudakz | 11:647d53d146f1 | 105 | return; |
hudakz | 11:647d53d146f1 | 106 | } |
hudakz | 11:647d53d146f1 | 107 | } |
hudakz | 11:647d53d146f1 | 108 | } |
hudakz | 9:a156d3de5647 | 109 | |
hudakz | 9:a156d3de5647 | 110 | /** |
hudakz | 9:a156d3de5647 | 111 | * @brief |
hudakz | 9:a156d3de5647 | 112 | * @note |
hudakz | 9:a156d3de5647 | 113 | * @param |
hudakz | 9:a156d3de5647 | 114 | * @retval |
hudakz | 9:a156d3de5647 | 115 | */ |
hudakz | 11:647d53d146f1 | 116 | IpAddress &IpAddress::operator=(const uint8_t* address) { |
hudakz | 11:647d53d146f1 | 117 | memcpy(_address, address, sizeof(_address)); |
hudakz | 11:647d53d146f1 | 118 | return *this; |
hudakz | 11:647d53d146f1 | 119 | } |
hudakz | 11:647d53d146f1 | 120 | |
hudakz | 11:647d53d146f1 | 121 | /** |
hudakz | 11:647d53d146f1 | 122 | * @brief |
hudakz | 11:647d53d146f1 | 123 | * @note |
hudakz | 11:647d53d146f1 | 124 | * @param |
hudakz | 11:647d53d146f1 | 125 | * @retval |
hudakz | 11:647d53d146f1 | 126 | */ |
hudakz | 11:647d53d146f1 | 127 | IpAddress &IpAddress::operator=(uint32_t address) |
hudakz | 11:647d53d146f1 | 128 | { |
hudakz | 9:a156d3de5647 | 129 | memcpy(_address, (const uint8_t*) &address, sizeof(_address)); |
hudakz | 9:a156d3de5647 | 130 | return *this; |
hudakz | 9:a156d3de5647 | 131 | } |
hudakz | 9:a156d3de5647 | 132 | |
hudakz | 9:a156d3de5647 | 133 | /** |
hudakz | 9:a156d3de5647 | 134 | * @brief |
hudakz | 9:a156d3de5647 | 135 | * @note |
hudakz | 9:a156d3de5647 | 136 | * @param |
hudakz | 9:a156d3de5647 | 137 | * @retval |
hudakz | 9:a156d3de5647 | 138 | */ |
hudakz | 9:a156d3de5647 | 139 | bool IpAddress::operator==(const uint8_t* addr) const |
hudakz | 9:a156d3de5647 | 140 | { |
hudakz | 9:a156d3de5647 | 141 | return memcmp(addr, _address, sizeof(_address)) == 0; |
hudakz | 9:a156d3de5647 | 142 | } |
hudakz | 9:a156d3de5647 | 143 | |
hudakz | 9:a156d3de5647 | 144 | /** |
hudakz | 9:a156d3de5647 | 145 | * @brief Returns IP Address as string of char |
hudakz | 9:a156d3de5647 | 146 | * @note |
hudakz | 9:a156d3de5647 | 147 | * @param |
hudakz | 9:a156d3de5647 | 148 | * @retval |
hudakz | 9:a156d3de5647 | 149 | */ |
hudakz | 11:647d53d146f1 | 150 | const char* IpAddress::toString(char* buf) |
hudakz | 11:647d53d146f1 | 151 | { |
hudakz | 11:647d53d146f1 | 152 | uint8_t i = 0; |
hudakz | 11:647d53d146f1 | 153 | uint8_t j = 0; |
hudakz | 9:a156d3de5647 | 154 | |
hudakz | 9:a156d3de5647 | 155 | for (i = 0; i < 3; i++) { |
hudakz | 9:a156d3de5647 | 156 | j += sprintf(&buf[j], "%d", _address[i]); |
hudakz | 9:a156d3de5647 | 157 | buf[j++] = '.'; |
hudakz | 9:a156d3de5647 | 158 | } |
hudakz | 9:a156d3de5647 | 159 | |
hudakz | 9:a156d3de5647 | 160 | j += sprintf(&buf[j], "%d", _address[i]); |
hudakz | 9:a156d3de5647 | 161 | buf[j] = '\0'; |
hudakz | 9:a156d3de5647 | 162 | return buf; |
hudakz | 9:a156d3de5647 | 163 | } |