Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

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), 
00024     _ip_address(), 
00025     _netmask(), 
00026     _gateway(),
00027     _connection_status_cb(NULL),
00028     _connect_status(NSAPI_STATUS_DISCONNECTED )
00029 {
00030 }
00031 
00032 
00033 nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00034 {
00035     _dhcp = false;
00036 
00037     strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
00038     _ip_address[sizeof(_ip_address) - 1] = '\0';
00039     strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
00040     _netmask[sizeof(_netmask) - 1] = '\0';
00041     strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
00042     _gateway[sizeof(_gateway) - 1] = '\0';
00043 
00044     return NSAPI_ERROR_OK ;
00045 }
00046 
00047 nsapi_error_t EthernetInterface::set_dhcp(bool dhcp)
00048 {
00049     _dhcp = dhcp;
00050     return NSAPI_ERROR_OK ;
00051 }
00052 
00053 nsapi_error_t EthernetInterface::connect()
00054 {
00055     return mbed_lwip_bringup_2(_dhcp, false,
00056             _ip_address[0] ? _ip_address : 0,
00057             _netmask[0] ? _netmask : 0,
00058             _gateway[0] ? _gateway : 0,
00059             DEFAULT_STACK);
00060 }
00061 
00062 nsapi_error_t EthernetInterface::disconnect()
00063 {
00064     return mbed_lwip_bringdown_2(false);
00065 }
00066 
00067 const char *EthernetInterface::get_mac_address()
00068 {
00069     return mbed_lwip_get_mac_address();
00070 }
00071 
00072 const char *EthernetInterface::get_ip_address()
00073 {
00074     if (mbed_lwip_get_ip_address(_ip_address, sizeof _ip_address)) {
00075         return _ip_address;
00076     }
00077 
00078     return NULL;
00079 }
00080 
00081 const char *EthernetInterface::get_netmask()
00082 {
00083     if (mbed_lwip_get_netmask(_netmask, sizeof _netmask)) {
00084         return _netmask;
00085     }
00086 
00087     return 0;
00088 }
00089 
00090 const char *EthernetInterface::get_gateway()
00091 {
00092     if (mbed_lwip_get_gateway(_gateway, sizeof _gateway)) {
00093         return _gateway;
00094     }
00095 
00096     return 0;
00097 }
00098 
00099 NetworkStack *EthernetInterface::get_stack()
00100 {
00101     return nsapi_create_stack(&lwip_stack);
00102 }
00103 
00104 void EthernetInterface::attach(
00105     Callback<void(nsapi_event_t, intptr_t)> status_cb)
00106 {
00107     _connection_status_cb = status_cb;
00108     mbed_lwip_attach(netif_status_cb, this);
00109 }
00110 
00111 nsapi_connection_status_t EthernetInterface::get_connection_status() const
00112 {
00113     return _connect_status;
00114 }
00115 
00116 void EthernetInterface::netif_status_cb(void *ethernet_if_ptr, 
00117     nsapi_event_t reason, intptr_t parameter) 
00118 {
00119     EthernetInterface *eth_ptr = static_cast<EthernetInterface*>(ethernet_if_ptr);
00120     eth_ptr->_connect_status = (nsapi_connection_status_t)parameter;
00121     if (eth_ptr->_connection_status_cb)
00122     {
00123         eth_ptr->_connection_status_cb(reason, parameter);
00124     }   
00125 }
00126 
00127 nsapi_error_t EthernetInterface::set_blocking(bool blocking)
00128 {
00129     mbed_lwip_set_blocking(blocking);
00130     return NSAPI_ERROR_OK ;
00131 }