Dependents:   SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ipaddr.h Source File

ipaddr.h

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef IPADDR_H
00025 #define IPADDR_H
00026 
00027 #include "mbed.h"
00028 
00029 #include "netCfg.h"
00030 #if NET_LWIP_STACK
00031 #include "lwip/ip_addr.h"
00032 #endif
00033 class IpAddr;
00034 
00035 class IpAddr //Basically a C++ frontend to ip_addr_t
00036 {
00037 public:
00038   #if NET_LWIP_STACK
00039   IpAddr(ip_addr_t* pIp)
00040   {
00041     *((uint32_t*)m_ip) = pIp->addr;
00042   }
00043   #endif
00044   
00045   IpAddr(uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3)
00046   {
00047     //We are in LE
00048     m_ip[0] = ip0;
00049     m_ip[1] = ip1;
00050     m_ip[2] = ip2;
00051     m_ip[3] = ip3;
00052   }
00053   
00054   IpAddr()
00055   {
00056     m_ip[0] = 0;
00057     m_ip[1] = 0;
00058     m_ip[2] = 0;
00059     m_ip[3] = 0;
00060   }
00061   
00062   #if NET_LWIP_STACK
00063   ip_addr_t getStruct() const
00064   {
00065     ip_addr_t ip_struct;
00066     ip_struct.addr = *((uint32_t*)m_ip);
00067     return ip_struct;
00068   }
00069   #endif
00070   
00071   uint8_t operator[](unsigned int i) const
00072   {
00073     uint8_t null = 0;
00074     if( i > 3 )
00075       return null;
00076     return m_ip[i];
00077   }
00078   
00079   bool isEq(const IpAddr& b) const
00080   {
00081     return (*((uint32_t*)m_ip) == *((uint32_t*)(b.m_ip)));
00082   }
00083   
00084   bool operator==(const IpAddr& b) const
00085   {
00086     return isEq(b);
00087   }
00088   
00089   bool operator!=(const IpAddr& b) const
00090   {
00091     return !(operator==(b));
00092   }
00093   
00094   bool isNull() const
00095   {
00096     return (*((uint32_t*)m_ip) == 0);
00097   }
00098   
00099 private:
00100   uint8_t m_ip[4];
00101 };
00102 
00103 #endif