Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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