takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 /* Interface implementation */
00020 EMACInterface::EMACInterface(EMAC &emac, OnboardNetworkStack &stack) :
00021     _emac(emac),
00022     _stack(stack),
00023     _interface(NULL),
00024     _dhcp(true),
00025     _blocking(true),
00026     _ip_address(),
00027     _netmask(),
00028     _gateway()
00029 {
00030 }
00031 
00032 nsapi_error_t EMACInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00033 {
00034     _dhcp = false;
00035 
00036     strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
00037     _ip_address[sizeof(_ip_address) - 1] = '\0';
00038     strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
00039     _netmask[sizeof(_netmask) - 1] = '\0';
00040     strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
00041     _gateway[sizeof(_gateway) - 1] = '\0';
00042 
00043     return NSAPI_ERROR_OK ;
00044 }
00045 
00046 nsapi_error_t EMACInterface::set_dhcp(bool dhcp)
00047 {
00048     _dhcp = dhcp;
00049     return NSAPI_ERROR_OK ;
00050 }
00051 
00052 nsapi_error_t EMACInterface::connect()
00053 {
00054     if (!_interface) {
00055         nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
00056         if (err != NSAPI_ERROR_OK ) {
00057             _interface = NULL;
00058             return err;
00059         }
00060         _interface->attach(_connection_status_cb);
00061     }
00062 
00063     return _interface->bringup(_dhcp,
00064                                _ip_address[0] ? _ip_address : 0,
00065                                _netmask[0] ? _netmask : 0,
00066                                _gateway[0] ? _gateway : 0,
00067                                DEFAULT_STACK,
00068                                _blocking);
00069 }
00070 
00071 nsapi_error_t EMACInterface::disconnect()
00072 {
00073     if (_interface) {
00074         return _interface->bringdown();
00075     }
00076     return NSAPI_ERROR_NO_CONNECTION ;
00077 }
00078 
00079 const char *EMACInterface::get_mac_address()
00080 {
00081     if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
00082         return _mac_address;
00083     }
00084     return NULL;
00085 }
00086 
00087 const char *EMACInterface::get_ip_address()
00088 {
00089     if (_interface && _interface->get_ip_address(_ip_address, sizeof(_ip_address))) {
00090         return _ip_address;
00091     }
00092 
00093     return NULL;
00094 }
00095 
00096 const char *EMACInterface::get_netmask()
00097 {
00098     if (_interface && _interface->get_netmask(_netmask, sizeof(_netmask))) {
00099         return _netmask;
00100     }
00101 
00102     return 0;
00103 }
00104 
00105 const char *EMACInterface::get_gateway()
00106 {
00107     if (_interface && _interface->get_gateway(_gateway, sizeof(_gateway))) {
00108         return _gateway;
00109     }
00110 
00111     return 0;
00112 }
00113 
00114 NetworkStack *EMACInterface::get_stack()
00115 {
00116     return &_stack;
00117 }
00118 
00119 void EMACInterface::attach(
00120     Callback<void(nsapi_event_t, intptr_t)> status_cb)
00121 {
00122     _connection_status_cb = status_cb;
00123     if (_interface) {
00124         _interface->attach(status_cb);
00125     }
00126 }
00127 
00128 nsapi_connection_status_t EMACInterface::get_connection_status() const
00129 {
00130     if (_interface) {
00131         return _interface->get_connection_status();
00132     } else {
00133         return NSAPI_STATUS_DISCONNECTED ;
00134     }
00135 }
00136 
00137 nsapi_error_t EMACInterface::set_blocking(bool blocking)
00138 {
00139     _blocking = blocking;
00140     return NSAPI_ERROR_OK ;
00141 }