Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
ppp_nsapi.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2019 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 <errno.h> 00018 #include "platform/FileHandle.h" 00019 #include "mbed_trace.h" 00020 #define TRACE_GROUP "PPPNS" 00021 #include "nsapi_ppp.h" 00022 #include "OnboardNetworkStack.h" 00023 #include "netsocket/PPPInterface.h" 00024 00025 #define IPADDR_STRLEN_MAX 46 00026 00027 #if NSAPI_PPP_AVAILABLE 00028 00029 namespace mbed { 00030 00031 void nsapi_ppp_close(); 00032 00033 // Just one interface for now 00034 static FileHandle *my_stream; 00035 static OnboardNetworkStack::Interface *my_interface; 00036 static bool blocking_connect = true; 00037 static const char *login; 00038 static const char *pwd; 00039 static PPP *ppp_service = NULL; 00040 static OnboardNetworkStack *stack; 00041 00042 nsapi_error_t nsapi_ppp_set_blocking(bool blocking) 00043 { 00044 blocking_connect = blocking; 00045 return NSAPI_ERROR_OK ; 00046 } 00047 00048 nsapi_error_t nsapi_ppp_connect(FileHandle *stream, Callback<void(nsapi_event_t, intptr_t)> cb, const char *uname, const char *password, const nsapi_ip_stack_t ip_stack) 00049 { 00050 if (my_stream) { 00051 return NSAPI_ERROR_PARAMETER ; 00052 } 00053 00054 my_stream = stream; 00055 stream->set_blocking(false); 00056 login = uname; 00057 pwd = password; 00058 00059 nsapi_error_t retcode; 00060 00061 if (!my_interface) { 00062 ppp_service = &PPP::get_default_instance(); 00063 stack = &OnboardNetworkStack::get_default_instance(); 00064 00065 ppp_service->set_stream(stream); 00066 ppp_service->set_ip_stack(ip_stack); 00067 ppp_service->set_credentials(uname, password); 00068 00069 retcode = stack->add_ppp_interface(*ppp_service, true, &my_interface); 00070 00071 if (retcode != NSAPI_ERROR_OK ) { 00072 my_interface = NULL; 00073 nsapi_ppp_close(); 00074 return retcode; 00075 } 00076 00077 if (cb) { 00078 my_interface->attach(cb); 00079 } 00080 } 00081 00082 retcode = my_interface->bringup(false, NULL, NULL, NULL, ip_stack, blocking_connect); 00083 00084 if (retcode != NSAPI_ERROR_OK ) { 00085 nsapi_ppp_close(); 00086 } 00087 00088 return retcode; 00089 } 00090 00091 nsapi_error_t nsapi_ppp_disconnect(FileHandle *stream) 00092 { 00093 if (my_stream != stream) { 00094 return NSAPI_ERROR_NO_CONNECTION ; 00095 } 00096 00097 nsapi_error_t retcode = my_interface->bringdown(); 00098 00099 nsapi_ppp_close(); 00100 00101 return retcode; 00102 } 00103 00104 void nsapi_ppp_close() 00105 { 00106 if (my_interface) { 00107 stack->remove_ppp_interface(&my_interface); 00108 my_interface = NULL; 00109 } 00110 00111 stack = NULL; 00112 00113 if (ppp_service) { 00114 ppp_service->set_stream(NULL); 00115 ppp_service->set_ip_stack(DEFAULT_STACK); 00116 ppp_service->set_credentials(NULL, NULL); 00117 ppp_service = NULL; 00118 } 00119 00120 /* Detach callbacks, and put handle back to default blocking mode */ 00121 my_stream->sigio(NULL); 00122 my_stream->set_blocking(true); 00123 my_stream = NULL; 00124 } 00125 00126 NetworkStack *nsapi_ppp_get_stack() 00127 { 00128 return &OnboardNetworkStack::get_default_instance(); 00129 } 00130 00131 const char *nsapi_ppp_get_ip_addr(FileHandle *stream) 00132 { 00133 static char ip_addr[IPADDR_STRLEN_MAX]; 00134 00135 if (stream == my_stream) { 00136 if (my_interface->get_ip_address (ip_addr, IPADDR_STRLEN_MAX)) { 00137 return ip_addr; 00138 } 00139 } 00140 00141 return NULL; 00142 } 00143 const char *nsapi_ppp_get_netmask(FileHandle *stream) 00144 { 00145 #if !PPP_IPV4_SUPPORT 00146 return NULL; 00147 #endif 00148 00149 static char netmask[IPADDR_STRLEN_MAX]; 00150 if (stream == my_stream) { 00151 if (my_interface->get_netmask (netmask, IPADDR_STRLEN_MAX)) { 00152 return netmask; 00153 } 00154 } 00155 00156 return NULL; 00157 } 00158 const char *nsapi_ppp_get_gw_addr(FileHandle *stream) 00159 { 00160 #if !PPP_IPV4_SUPPORT 00161 return NULL; 00162 #endif 00163 00164 static char gwaddr[IPADDR_STRLEN_MAX]; 00165 if (stream == my_stream) { 00166 if (my_interface->get_gateway (gwaddr, IPADDR_STRLEN_MAX)) { 00167 return gwaddr; 00168 } 00169 } 00170 00171 return NULL; 00172 } 00173 00174 } // namespace mbed 00175 00176 #endif
Generated on Tue Jul 12 2022 13:54:42 by
