Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetInterface.cpp Source File

EthernetInterface.cpp

00001 /* LWIP implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "EthernetInterface.h"
00018 #include "lwip_stack.h"
00019 
00020 
00021 /* Interface implementation */
00022 EthernetInterface::EthernetInterface()
00023     : _dhcp(true), _ip_address(), _netmask(), _gateway()
00024 {
00025 }
00026 
00027 nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00028 {
00029     _dhcp = false;
00030     strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
00031     strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
00032     strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
00033     return NSAPI_ERROR_OK ;
00034 }
00035 
00036 nsapi_error_t EthernetInterface::set_dhcp(bool dhcp)
00037 {
00038     _dhcp = dhcp;
00039     return NSAPI_ERROR_OK ;
00040 }
00041 
00042 nsapi_error_t EthernetInterface::connect()
00043 {
00044     return mbed_lwip_bringup(_dhcp,
00045             _ip_address[0] ? _ip_address : 0,
00046             _netmask[0] ? _netmask : 0,
00047             _gateway[0] ? _gateway : 0);
00048 }
00049 
00050 nsapi_error_t EthernetInterface::disconnect()
00051 {
00052     return mbed_lwip_bringdown();
00053 }
00054 
00055 const char *EthernetInterface::get_mac_address()
00056 {
00057     return mbed_lwip_get_mac_address();
00058 }
00059 
00060 const char *EthernetInterface::get_ip_address()
00061 {
00062     if (mbed_lwip_get_ip_address(_ip_address, sizeof _ip_address)) {
00063         return _ip_address;
00064     }
00065 
00066     return NULL;
00067 }
00068 
00069 const char *EthernetInterface::get_netmask()
00070 {
00071     if (mbed_lwip_get_netmask(_netmask, sizeof _netmask)) {
00072         return _netmask;
00073     }
00074 
00075     return 0;
00076 }
00077 
00078 const char *EthernetInterface::get_gateway()
00079 {
00080     if (mbed_lwip_get_gateway(_gateway, sizeof _gateway)) {
00081         return _gateway;
00082     }
00083 
00084     return 0;
00085 }
00086 
00087 NetworkStack *EthernetInterface::get_stack()
00088 {
00089     return nsapi_create_stack(&lwip_stack);
00090 }