adds getMACAddress() like getIPAddress(). Initializes in constructor.
Dependencies: Socket lwip-eth lwip-sys lwip
Dependents: EtherTest2 SmartLight
Fork of EthernetInterface by
Revision 26:132037eec2ba, committed 2012-09-01
- Comitter:
- jonathonfletcher
- Date:
- Sat Sep 01 11:15:56 2012 +0000
- Parent:
- 25:a0ee3ae75cfa
- Commit message:
- add getMACAddress() in the style of getIPAddress(). Initialize the MAC string from the constructor.
Changed in this revision
EthernetInterface.cpp | Show annotated file Show diff for this revision Revisions of this file |
EthernetInterface.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r a0ee3ae75cfa -r 132037eec2ba EthernetInterface.cpp --- a/EthernetInterface.cpp Mon Aug 13 09:38:08 2012 +0000 +++ b/EthernetInterface.cpp Sat Sep 01 11:15:56 2012 +0000 @@ -35,34 +35,48 @@ static Semaphore netif_inited(0); static char ip_addr[16]; -static bool connected; +static char mac_addr[18]; +static bool connected = false; static bool use_dhcp = false; -static void tcpip_init_done(void *arg) { +static void tcpip_init_done(void *arg) +{ tcpip_inited.release(); } -static void netif_status_callback(struct netif *netif) { + +static void netif_status_callback(struct netif *netif) +{ strcpy(ip_addr, inet_ntoa(netif->ip_addr)); connected = netif_is_up(netif) ? true : false; netif_inited.release(); } -static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { +static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) +{ tcpip_init(tcpip_init_done, NULL); tcpip_inited.wait(); - + memset((void*) &lpcNetif, 0, sizeof(lpcNetif)); netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input); netif_set_default(&lpcNetif); netif_set_status_callback(&lpcNetif, netif_status_callback); } -int EthernetInterface::init() { +EthernetInterface::EthernetInterface() +{ + char mac[6]; + mbed_mac_address(mac); + snprintf(mac_addr, 18, "%02x:%02x:%02x:%02x:%02x:%02x\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} + +int EthernetInterface::init() +{ use_dhcp = true; init_netif(NULL, NULL, NULL); return 0; } -int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) { +int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) +{ use_dhcp = false; ip_addr_t ip_n, mask_n, gateway_n; inet_aton(ip, &ip_n); @@ -72,34 +86,42 @@ return 0; } -int EthernetInterface::connect(unsigned int timeout_ms) { +int EthernetInterface::connect(unsigned int timeout_ms) +{ NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); NVIC_EnableIRQ(ENET_IRQn); - + if (use_dhcp) { dhcp_start(&lpcNetif); } else { netif_set_up(&lpcNetif); } - + // -1: error, 0: timeout int inited = netif_inited.wait(timeout_ms); return (inited > 0) ? (0) : (-1); } -int EthernetInterface::disconnect() { +int EthernetInterface::disconnect() +{ if (use_dhcp) { dhcp_release(&lpcNetif); dhcp_stop(&lpcNetif); } else { netif_set_down(&lpcNetif); } - + NVIC_DisableIRQ(ENET_IRQn); - + return 0; } -char* EthernetInterface::getIPAddress() { +char* EthernetInterface::getIPAddress() +{ return (connected) ? (ip_addr) : (NULL); } + +char* EthernetInterface::getMACAddress() +{ + return mac_addr; +}
diff -r a0ee3ae75cfa -r 132037eec2ba EthernetInterface.h --- a/EthernetInterface.h Mon Aug 13 09:38:08 2012 +0000 +++ b/EthernetInterface.h Sat Sep 01 11:15:56 2012 +0000 @@ -16,7 +16,7 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - + #ifndef ETHERNETINTERFACE_H_ #define ETHERNETINTERFACE_H_ @@ -27,40 +27,49 @@ #include "rtos.h" #include "lwip/netif.h" - /** Interface using Ethernet to connect to an IP-based network - * - */ -class EthernetInterface { +/** Interface using Ethernet to connect to an IP-based network +* +*/ +class EthernetInterface +{ public: - /** Initialize the interface with DHCP. - * Initialize the interface and configure it to use DHCP (no connection at this point). - * \return 0 on success, a negative number on failure - */ - static int init(); //With DHCP + + /* + * Constructor + */ + EthernetInterface(); + + /** Initialize the interface with DHCP. + * Initialize the interface and configure it to use DHCP (no connection at this point). + * \return 0 on success, a negative number on failure + */ + static int init(); //With DHCP - /** Initialize the interface with a static IP address. - * Initialize the interface and configure it with the following static configuration (no connection at this point). - * \param ip the IP address to use - * \param mask the IP address mask - * \param gateway the gateway to use - * \return 0 on success, a negative number on failure - */ - static int init(const char* ip, const char* mask, const char* gateway); + /** Initialize the interface with a static IP address. + * Initialize the interface and configure it with the following static configuration (no connection at this point). + * \param ip the IP address to use + * \param mask the IP address mask + * \param gateway the gateway to use + * \return 0 on success, a negative number on failure + */ + static int init(const char* ip, const char* mask, const char* gateway); - /** Connect - * Bring the interface up, start DHCP if needed. - * \param timeout_ms timeout in ms (default: (12)s). - * \return 0 on success, a negative number on failure - */ - static int connect(unsigned int timeout_ms=12000); - - /** Disconnect - * Bring the interface down - * \return 0 on success, a negative number on failure - */ - static int disconnect(); - - static char* getIPAddress(); + /** Connect + * Bring the interface up, start DHCP if needed. + * \param timeout_ms timeout in ms (default: (12)s). + * \return 0 on success, a negative number on failure + */ + static int connect(unsigned int timeout_ms=12000); + + /** Disconnect + * Bring the interface down + * \return 0 on success, a negative number on failure + */ + static int disconnect(); + + static char* getIPAddress(); + + static char* getMACAddress(); }; #include "TCPSocketConnection.h"