Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PPPInterface.cpp Source File

PPPInterface.cpp

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