Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EMACInterface.cpp Source File

EMACInterface.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 "EMACInterface.h"
00018 
00019 using namespace mbed;
00020 
00021 /* Interface implementation */
00022 EMACInterface::EMACInterface(EMAC &emac, OnboardNetworkStack &stack) :
00023     _emac(emac),
00024     _stack(stack),
00025     _interface(NULL),
00026     _dhcp(true),
00027     _blocking(true),
00028     _ip_address(),
00029     _netmask(),
00030     _gateway()
00031 {
00032 }
00033 
00034 nsapi_error_t EMACInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00035 {
00036     _dhcp = false;
00037 
00038     strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
00039     _ip_address[sizeof(_ip_address) - 1] = '\0';
00040     strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
00041     _netmask[sizeof(_netmask) - 1] = '\0';
00042     strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
00043     _gateway[sizeof(_gateway) - 1] = '\0';
00044 
00045     return NSAPI_ERROR_OK ;
00046 }
00047 
00048 nsapi_error_t EMACInterface::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
00049 {
00050     _dhcp = false;
00051 
00052     strncpy(_ip_address, ip_address.get_ip_address() ? ip_address.get_ip_address() : "", sizeof(_ip_address));
00053     _ip_address[sizeof(_ip_address) - 1] = '\0';
00054     strncpy(_netmask, netmask.get_ip_address() ? netmask.get_ip_address() : "", sizeof(_netmask));
00055     _netmask[sizeof(_netmask) - 1] = '\0';
00056     strncpy(_gateway, gateway.get_ip_address() ? gateway.get_ip_address() : "", sizeof(_gateway));
00057     _gateway[sizeof(_gateway) - 1] = '\0';
00058 
00059     return NSAPI_ERROR_OK ;
00060 }
00061 
00062 nsapi_error_t EMACInterface::set_dhcp(bool dhcp)
00063 {
00064     _dhcp = dhcp;
00065     return NSAPI_ERROR_OK ;
00066 }
00067 
00068 nsapi_error_t EMACInterface::connect ()
00069 {
00070     if (!_interface) {
00071         nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
00072         if (err != NSAPI_ERROR_OK ) {
00073             _interface = NULL;
00074             return err;
00075         }
00076         _interface->attach(_connection_status_cb);
00077     }
00078 
00079     return _interface->bringup(_dhcp,
00080                                _ip_address[0] ? _ip_address : 0,
00081                                _netmask[0] ? _netmask : 0,
00082                                _gateway[0] ? _gateway : 0,
00083                                DEFAULT_STACK,
00084                                _blocking);
00085 }
00086 
00087 nsapi_error_t EMACInterface::disconnect ()
00088 {
00089     if (_interface) {
00090         return _interface->bringdown();
00091     }
00092     return NSAPI_ERROR_NO_CONNECTION ;
00093 }
00094 
00095 const char *EMACInterface::get_mac_address ()
00096 {
00097     if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
00098         return _mac_address;
00099     }
00100     return nullptr;
00101 }
00102 
00103 nsapi_error_t EMACInterface::get_ip_address (SocketAddress *address)
00104 {
00105     if (_interface && _interface->get_ip_address (address) == NSAPI_ERROR_OK ) {
00106         strncpy(_ip_address, address->get_ip_address(), sizeof(_ip_address));
00107         return NSAPI_ERROR_OK ;
00108     }
00109 
00110     return NSAPI_ERROR_NO_CONNECTION ;
00111 }
00112 
00113 const char *EMACInterface::get_ip_address ()
00114 {
00115     if (_interface && _interface->get_ip_address (_ip_address, sizeof(_ip_address))) {
00116         return _ip_address;
00117     }
00118     return nullptr;
00119 }
00120 
00121 nsapi_error_t EMACInterface::get_ipv6_link_local_address (SocketAddress *address)
00122 {
00123     if (_interface) {
00124         return _interface->get_ipv6_link_local_address (address);
00125     }
00126 
00127     return NSAPI_ERROR_NO_CONNECTION ;
00128 }
00129 
00130 nsapi_error_t EMACInterface::get_netmask (SocketAddress *address)
00131 {
00132     if (_interface && _interface->get_netmask (address) == NSAPI_ERROR_OK ) {
00133         strncpy(_netmask, address->get_ip_address(), sizeof(_netmask));
00134         return NSAPI_ERROR_OK ;
00135     }
00136 
00137     return NSAPI_ERROR_NO_CONNECTION ;
00138 }
00139 
00140 const char *EMACInterface::get_netmask ()
00141 {
00142     if (_interface && _interface->get_netmask (_netmask, sizeof(_netmask))) {
00143         return _netmask;
00144     }
00145     return nullptr;
00146 }
00147 
00148 nsapi_error_t EMACInterface::get_gateway (SocketAddress *address)
00149 {
00150     if (_interface && _interface->get_gateway (address) == NSAPI_ERROR_OK ) {
00151         strncpy(_gateway, address->get_ip_address(), sizeof(_gateway));
00152         address->set_ip_address(_gateway);
00153         return NSAPI_ERROR_OK ;
00154     }
00155 
00156     return NSAPI_ERROR_NO_CONNECTION ;
00157 }
00158 
00159 const char *EMACInterface::get_gateway ()
00160 {
00161     if (_interface && _interface->get_gateway (_gateway, sizeof(_gateway))) {
00162         return _gateway;
00163     }
00164     return nullptr;
00165 }
00166 
00167 char *EMACInterface::get_interface_name (char *interface_name)
00168 {
00169     if (_interface) {
00170         return _interface->get_interface_name(interface_name);
00171     }
00172 
00173     return NULL;
00174 }
00175 
00176 void EMACInterface::set_as_default ()
00177 {
00178     if (_interface) {
00179         _stack.set_default_interface(_interface);
00180     }
00181 }
00182 
00183 NetworkStack *EMACInterface::get_stack()
00184 {
00185     return &_stack;
00186 }
00187 
00188 void EMACInterface::attach (
00189     mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00190 {
00191     _connection_status_cb = status_cb;
00192     if (_interface) {
00193         _interface->attach(status_cb);
00194     }
00195 }
00196 
00197 nsapi_connection_status_t EMACInterface::get_connection_status () const
00198 {
00199     if (_interface) {
00200         return _interface->get_connection_status();
00201     } else {
00202         return NSAPI_STATUS_DISCONNECTED ;
00203     }
00204 }
00205 
00206 nsapi_error_t EMACInterface::set_blocking (bool blocking)
00207 {
00208     _blocking = blocking;
00209     return NSAPI_ERROR_OK ;
00210 }