Fork for fixes

Committer:
hudakz
Date:
Tue Aug 27 15:01:10 2019 +0000
Revision:
9:a156d3de5647
Child:
10:e4ddab81e6a8
Mbed OS 5 support added and API modified.

Who changed what in which revision?

UserRevisionLine numberNew 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
hudakz 9:a156d3de5647 22 #include <stdio.h>
hudakz 9:a156d3de5647 23 #include "mbed.h"
hudakz 9:a156d3de5647 24 #include "IpAddress.h"
hudakz 9:a156d3de5647 25
hudakz 9:a156d3de5647 26 /**
hudakz 9:a156d3de5647 27 * @brief
hudakz 9:a156d3de5647 28 * @note
hudakz 9:a156d3de5647 29 * @param
hudakz 9:a156d3de5647 30 * @retval
hudakz 9:a156d3de5647 31 */
hudakz 9:a156d3de5647 32 IpAddress::IpAddress(void) {
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 9:a156d3de5647 42 IpAddress::IpAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) {
hudakz 9:a156d3de5647 43 _address[0] = octet1;
hudakz 9:a156d3de5647 44 _address[1] = octet2;
hudakz 9:a156d3de5647 45 _address[2] = octet3;
hudakz 9:a156d3de5647 46 _address[3] = octet4;
hudakz 9:a156d3de5647 47 }
hudakz 9:a156d3de5647 48
hudakz 9:a156d3de5647 49 /**
hudakz 9:a156d3de5647 50 * @brief
hudakz 9:a156d3de5647 51 * @note
hudakz 9:a156d3de5647 52 * @param
hudakz 9:a156d3de5647 53 * @retval
hudakz 9:a156d3de5647 54 */
hudakz 9:a156d3de5647 55 IpAddress::IpAddress(uint32_t address) {
hudakz 9:a156d3de5647 56 memcpy(_address, &address, sizeof(_address));
hudakz 9:a156d3de5647 57 }
hudakz 9:a156d3de5647 58
hudakz 9:a156d3de5647 59 /**
hudakz 9:a156d3de5647 60 * @brief
hudakz 9:a156d3de5647 61 * @note
hudakz 9:a156d3de5647 62 * @param
hudakz 9:a156d3de5647 63 * @retval
hudakz 9:a156d3de5647 64 */
hudakz 9:a156d3de5647 65 IpAddress::IpAddress(const uint8_t address[4]) {
hudakz 9:a156d3de5647 66 memcpy(_address, address, sizeof(_address));
hudakz 9:a156d3de5647 67 }
hudakz 9:a156d3de5647 68
hudakz 9:a156d3de5647 69 /**
hudakz 9:a156d3de5647 70 * @brief
hudakz 9:a156d3de5647 71 * @note
hudakz 9:a156d3de5647 72 * @param
hudakz 9:a156d3de5647 73 * @retval
hudakz 9:a156d3de5647 74 */
hudakz 9:a156d3de5647 75 //IPAddress &IPAddress::operator=(const uint8_t* address) {
hudakz 9:a156d3de5647 76 // memcpy(_address, address, sizeof(_address));
hudakz 9:a156d3de5647 77 // return *this;
hudakz 9:a156d3de5647 78 //}
hudakz 9:a156d3de5647 79
hudakz 9:a156d3de5647 80 /**
hudakz 9:a156d3de5647 81 * @brief
hudakz 9:a156d3de5647 82 * @note
hudakz 9:a156d3de5647 83 * @param
hudakz 9:a156d3de5647 84 * @retval
hudakz 9:a156d3de5647 85 */
hudakz 9:a156d3de5647 86 IpAddress &IpAddress::operator=(uint32_t address) {
hudakz 9:a156d3de5647 87 memcpy(_address, (const uint8_t*) &address, sizeof(_address));
hudakz 9:a156d3de5647 88 return *this;
hudakz 9:a156d3de5647 89 }
hudakz 9:a156d3de5647 90
hudakz 9:a156d3de5647 91 /**
hudakz 9:a156d3de5647 92 * @brief
hudakz 9:a156d3de5647 93 * @note
hudakz 9:a156d3de5647 94 * @param
hudakz 9:a156d3de5647 95 * @retval
hudakz 9:a156d3de5647 96 */
hudakz 9:a156d3de5647 97 bool IpAddress::operator==(const uint8_t* addr) const
hudakz 9:a156d3de5647 98 {
hudakz 9:a156d3de5647 99 return memcmp(addr, _address, sizeof(_address)) == 0;
hudakz 9:a156d3de5647 100 }
hudakz 9:a156d3de5647 101
hudakz 9:a156d3de5647 102 /**
hudakz 9:a156d3de5647 103 * @brief Returns IP Address as string of char
hudakz 9:a156d3de5647 104 * @note
hudakz 9:a156d3de5647 105 * @param
hudakz 9:a156d3de5647 106 * @retval
hudakz 9:a156d3de5647 107 */
hudakz 9:a156d3de5647 108 const char* IpAddress::toString(char* buf) {
hudakz 9:a156d3de5647 109 uint8_t i = 0;
hudakz 9:a156d3de5647 110 uint8_t j = 0;
hudakz 9:a156d3de5647 111
hudakz 9:a156d3de5647 112 for (i = 0; i < 3; i++) {
hudakz 9:a156d3de5647 113 j += sprintf(&buf[j], "%d", _address[i]);
hudakz 9:a156d3de5647 114 buf[j++] = '.';
hudakz 9:a156d3de5647 115 }
hudakz 9:a156d3de5647 116
hudakz 9:a156d3de5647 117 j += sprintf(&buf[j], "%d", _address[i]);
hudakz 9:a156d3de5647 118 buf[j] = '\0';
hudakz 9:a156d3de5647 119 return buf;
hudakz 9:a156d3de5647 120 }
hudakz 9:a156d3de5647 121