Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers L3IPInterface.cpp Source File

L3IPInterface.cpp

00001 /*
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "L3IPInterface.h"
00019 
00020 #if MBED_CONF_LWIP_L3IP_ENABLED
00021 #include "LWIPStack.h"
00022 using namespace mbed;
00023 
00024 /* Interface implementation */
00025 L3IPInterface::L3IPInterface(L3IP &l3ip, OnboardNetworkStack &stack) :
00026     _l3ip(l3ip),
00027     _stack(stack),
00028     _interface(NULL),
00029     _dhcp(true),
00030     _blocking(true),
00031     _ip_address(),
00032     _netmask(),
00033     _gateway()
00034 {
00035 }
00036 
00037 L3IPInterface::~ L3IPInterface()
00038 {
00039     _stack.remove_l3ip_interface(&_interface);
00040 }
00041 
00042 nsapi_error_t L3IPInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
00043 {
00044     _dhcp = false;
00045 
00046     strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
00047     _ip_address[sizeof(_ip_address) - 1] = '\0';
00048     strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
00049     _netmask[sizeof(_netmask) - 1] = '\0';
00050     strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
00051     _gateway[sizeof(_gateway) - 1] = '\0';
00052 
00053     return NSAPI_ERROR_OK ;
00054 }
00055 
00056 nsapi_error_t L3IPInterface::set_dhcp(bool dhcp)
00057 {
00058     _dhcp = dhcp;
00059     return NSAPI_ERROR_OK ;
00060 }
00061 
00062 
00063 
00064 nsapi_error_t L3IPInterface::connect()
00065 {
00066     if (!_interface) {
00067         nsapi_error_t err = _stack.add_l3ip_interface(_l3ip, false, &_interface);
00068         if (err != NSAPI_ERROR_OK ) {
00069             _interface = NULL;
00070             return err;
00071         }
00072         _interface->attach(_connection_status_cb);
00073     }
00074 
00075     return _interface->bringup(_dhcp,
00076                                _ip_address[0] ? _ip_address : 0,
00077                                _netmask[0] ? _netmask : 0,
00078                                _gateway[0] ? _gateway : 0,
00079                                DEFAULT_STACK,
00080                                _blocking);
00081 }
00082 
00083 nsapi_error_t L3IPInterface::disconnect()
00084 {
00085     if (_interface) {
00086         return _interface->bringdown();
00087     }
00088     return NSAPI_ERROR_NO_CONNECTION ;
00089 }
00090 
00091 const char *L3IPInterface::get_ip_address()
00092 {
00093     if (_interface && _interface->get_ip_address (_ip_address, sizeof(_ip_address))) {
00094         return _ip_address;
00095     }
00096 
00097     return NULL;
00098 }
00099 
00100 const char *L3IPInterface::get_netmask()
00101 {
00102     if (_interface && _interface->get_netmask (_netmask, sizeof(_netmask))) {
00103         return _netmask;
00104     }
00105 
00106     return 0;
00107 }
00108 
00109 const char *L3IPInterface::get_gateway()
00110 {
00111     if (_interface && _interface->get_gateway (_gateway, sizeof(_gateway))) {
00112         return _gateway;
00113     }
00114 
00115     return 0;
00116 }
00117 
00118 char *L3IPInterface::get_interface_name(char *interface_name)
00119 {
00120     if (_interface) {
00121         return _interface->get_interface_name(interface_name);
00122     }
00123 
00124     return NULL;
00125 }
00126 
00127 void L3IPInterface::set_as_default()
00128 {
00129     if (_interface) {
00130         static_cast<LWIP *>(&_stack)->set_default_interface(_interface);
00131     }
00132 }
00133 
00134 NetworkStack *L3IPInterface::get_stack()
00135 {
00136     return &_stack;
00137 }
00138 
00139 void L3IPInterface::attach(
00140     mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00141 {
00142     _connection_status_cb = status_cb;
00143     if (_interface) {
00144         _interface->attach(status_cb);
00145     }
00146 }
00147 
00148 nsapi_connection_status_t L3IPInterface::get_connection_status() const
00149 {
00150     if (_interface) {
00151         return _interface->get_connection_status();
00152     } else {
00153         return NSAPI_STATUS_DISCONNECTED ;
00154     }
00155 }
00156 
00157 nsapi_error_t L3IPInterface::set_blocking(bool blocking)
00158 {
00159     _blocking = blocking;
00160     return NSAPI_ERROR_OK ;
00161 }
00162 
00163 #endif