Dependents: SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1
if/net/ipaddr.h@1:e52ec2a24c6a, 2010-05-24 (annotated)
- Committer:
- donatien
- Date:
- Mon May 24 10:48:38 2010 +0000
- Revision:
- 1:e52ec2a24c6a
- Parent:
- 0:3717b703f76d
- Child:
- 2:2dafdd154a9e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 1:e52ec2a24c6a | 1 | |
donatien | 1:e52ec2a24c6a | 2 | /* |
donatien | 1:e52ec2a24c6a | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [dot] gmail [dot] com) |
donatien | 1:e52ec2a24c6a | 4 | |
donatien | 1:e52ec2a24c6a | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 1:e52ec2a24c6a | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 1:e52ec2a24c6a | 7 | in the Software without restriction, including without limitation the rights |
donatien | 1:e52ec2a24c6a | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 1:e52ec2a24c6a | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 1:e52ec2a24c6a | 10 | furnished to do so, subject to the following conditions: |
donatien | 1:e52ec2a24c6a | 11 | |
donatien | 1:e52ec2a24c6a | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 1:e52ec2a24c6a | 13 | all copies or substantial portions of the Software. |
donatien | 1:e52ec2a24c6a | 14 | |
donatien | 1:e52ec2a24c6a | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 1:e52ec2a24c6a | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 1:e52ec2a24c6a | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 1:e52ec2a24c6a | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 1:e52ec2a24c6a | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 1:e52ec2a24c6a | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 1:e52ec2a24c6a | 21 | THE SOFTWARE. |
donatien | 1:e52ec2a24c6a | 22 | */ |
donatien | 1:e52ec2a24c6a | 23 | |
donatien | 0:3717b703f76d | 24 | #ifndef IPADDR_H |
donatien | 0:3717b703f76d | 25 | #define IPADDR_H |
donatien | 0:3717b703f76d | 26 | |
donatien | 0:3717b703f76d | 27 | #include "mbed.h" |
donatien | 0:3717b703f76d | 28 | |
donatien | 0:3717b703f76d | 29 | #include "netCfg.h" |
donatien | 0:3717b703f76d | 30 | #if NET_LWIP_STACK |
donatien | 0:3717b703f76d | 31 | #include "lwip/ip_addr.h" |
donatien | 0:3717b703f76d | 32 | #endif |
donatien | 0:3717b703f76d | 33 | class IpAddr; |
donatien | 0:3717b703f76d | 34 | |
donatien | 0:3717b703f76d | 35 | class IpAddr //Basically a C++ frontend to ip_addr_t |
donatien | 0:3717b703f76d | 36 | { |
donatien | 0:3717b703f76d | 37 | public: |
donatien | 0:3717b703f76d | 38 | #if NET_LWIP_STACK |
donatien | 0:3717b703f76d | 39 | IpAddr(ip_addr_t* pIp) |
donatien | 0:3717b703f76d | 40 | { |
donatien | 0:3717b703f76d | 41 | *((uint32_t*)m_ip) = pIp->addr; |
donatien | 0:3717b703f76d | 42 | } |
donatien | 0:3717b703f76d | 43 | #endif |
donatien | 0:3717b703f76d | 44 | |
donatien | 0:3717b703f76d | 45 | IpAddr(uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3) |
donatien | 0:3717b703f76d | 46 | { |
donatien | 0:3717b703f76d | 47 | //We are in LE |
donatien | 0:3717b703f76d | 48 | m_ip[0] = ip0; |
donatien | 0:3717b703f76d | 49 | m_ip[1] = ip1; |
donatien | 0:3717b703f76d | 50 | m_ip[2] = ip2; |
donatien | 0:3717b703f76d | 51 | m_ip[3] = ip3; |
donatien | 0:3717b703f76d | 52 | } |
donatien | 0:3717b703f76d | 53 | |
donatien | 0:3717b703f76d | 54 | IpAddr() |
donatien | 0:3717b703f76d | 55 | { |
donatien | 0:3717b703f76d | 56 | m_ip[0] = 0; |
donatien | 0:3717b703f76d | 57 | m_ip[1] = 0; |
donatien | 0:3717b703f76d | 58 | m_ip[2] = 0; |
donatien | 0:3717b703f76d | 59 | m_ip[3] = 0; |
donatien | 0:3717b703f76d | 60 | } |
donatien | 0:3717b703f76d | 61 | |
donatien | 0:3717b703f76d | 62 | #if NET_LWIP_STACK |
donatien | 0:3717b703f76d | 63 | ip_addr_t getStruct() const |
donatien | 0:3717b703f76d | 64 | { |
donatien | 0:3717b703f76d | 65 | ip_addr_t ip_struct; |
donatien | 0:3717b703f76d | 66 | ip_struct.addr = *((uint32_t*)m_ip); |
donatien | 0:3717b703f76d | 67 | return ip_struct; |
donatien | 0:3717b703f76d | 68 | } |
donatien | 0:3717b703f76d | 69 | #endif |
donatien | 0:3717b703f76d | 70 | |
donatien | 0:3717b703f76d | 71 | uint8_t operator[](unsigned int i) const |
donatien | 0:3717b703f76d | 72 | { |
donatien | 0:3717b703f76d | 73 | uint8_t null = 0; |
donatien | 0:3717b703f76d | 74 | if( i > 3 ) |
donatien | 0:3717b703f76d | 75 | return null; |
donatien | 0:3717b703f76d | 76 | return m_ip[i]; |
donatien | 0:3717b703f76d | 77 | } |
donatien | 0:3717b703f76d | 78 | |
donatien | 0:3717b703f76d | 79 | bool isEq(const IpAddr& b) const |
donatien | 0:3717b703f76d | 80 | { |
donatien | 0:3717b703f76d | 81 | return (*((uint32_t*)m_ip) == *((uint32_t*)(b.m_ip))); |
donatien | 0:3717b703f76d | 82 | } |
donatien | 0:3717b703f76d | 83 | |
donatien | 0:3717b703f76d | 84 | bool operator==(const IpAddr& b) const |
donatien | 0:3717b703f76d | 85 | { |
donatien | 0:3717b703f76d | 86 | return isEq(b); |
donatien | 0:3717b703f76d | 87 | } |
donatien | 0:3717b703f76d | 88 | |
donatien | 0:3717b703f76d | 89 | bool operator!=(const IpAddr& b) const |
donatien | 0:3717b703f76d | 90 | { |
donatien | 0:3717b703f76d | 91 | return !(operator==(b)); |
donatien | 0:3717b703f76d | 92 | } |
donatien | 0:3717b703f76d | 93 | |
donatien | 0:3717b703f76d | 94 | bool isNull() const |
donatien | 0:3717b703f76d | 95 | { |
donatien | 0:3717b703f76d | 96 | return (*((uint32_t*)m_ip) == 0); |
donatien | 0:3717b703f76d | 97 | } |
donatien | 0:3717b703f76d | 98 | |
donatien | 0:3717b703f76d | 99 | private: |
donatien | 0:3717b703f76d | 100 | uint8_t m_ip[4]; |
donatien | 0:3717b703f76d | 101 | }; |
donatien | 0:3717b703f76d | 102 | |
donatien | 0:3717b703f76d | 103 | #endif |