@@ -55,12 +55,11 @@ static nsapi_error_t connect_error_code;
55
55
// Just one interface for now
56
56
static FileHandle *my_stream;
57
57
static ppp_pcb *my_ppp_pcb;
58
- static bool ppp_link_up = false ;
59
58
static bool ppp_active = false ;
60
59
static const char *login;
61
60
static const char *pwd;
62
61
static sys_sem_t ppp_close_sem;
63
- static void (*notify_ppp_link_down)( nsapi_error_t ) = 0 ;
62
+ static Callback< void ( nsapi_error_t )> connection_status_cb ;
64
63
65
64
static EventQueue *prepare_event_queue ()
66
65
{
@@ -71,13 +70,9 @@ static EventQueue *prepare_event_queue()
71
70
// Should be trying to get a global shared event queue here!
72
71
// Shouldn't have to be making a private thread!
73
72
74
- // Only need to queue 1 event . new blows on failure.
73
+ // Only need to queue 2 events . new blows on failure.
75
74
event_queue = new EventQueue (2 * EVENTS_EVENT_SIZE, NULL );
76
- #if LWIP_DEBUG
77
- event_thread = new Thread (osPriorityNormal, 900 *2 );
78
- #else
79
- event_thread = new Thread (osPriorityNormal, 900 *1 );
80
- #endif
75
+ event_thread = new Thread (osPriorityNormal, PPP_THREAD_STACK_SIZE);
81
76
82
77
if (event_thread->start (callback (event_queue, &EventQueue::dispatch_forever)) != osOK) {
83
78
delete event_thread;
@@ -130,7 +125,7 @@ static void ppp_link_status(ppp_pcb *pcb, int err_code, void *ctx)
130
125
nsapi_error_t mapped_err_code = NSAPI_ERROR_NO_CONNECTION;
131
126
132
127
switch (err_code) {
133
- case PPPERR_NONE: {
128
+ case PPPERR_NONE:
134
129
mapped_err_code = NSAPI_ERROR_OK;
135
130
tr_info (" status_cb: Connected" );
136
131
#if PPP_IPV4_SUPPORT
@@ -153,81 +148,88 @@ static void ppp_link_status(ppp_pcb *pcb, int err_code, void *ctx)
153
148
tr_debug (" our6_ipaddr = %s" , ip6addr_ntoa (netif_ip6_addr (ppp_netif (pcb), 0 )));
154
149
#endif /* PPP_IPV6_SUPPORT */
155
150
break ;
156
- }
157
- case PPPERR_PARAM: {
151
+
152
+ case PPPERR_PARAM:
158
153
tr_info (" status_cb: Invalid parameter" );
159
154
break ;
160
- }
161
- case PPPERR_OPEN: {
155
+
156
+ case PPPERR_OPEN:
162
157
tr_info (" status_cb: Unable to open PPP session" );
163
158
break ;
164
- }
165
- case PPPERR_DEVICE: {
159
+
160
+ case PPPERR_DEVICE:
166
161
tr_info (" status_cb: Invalid I/O device for PPP" );
167
162
break ;
168
- }
169
- case PPPERR_ALLOC: {
163
+
164
+ case PPPERR_ALLOC:
170
165
tr_info (" status_cb: Unable to allocate resources" );
171
166
break ;
172
- }
173
- case PPPERR_USER: {
167
+
168
+ case PPPERR_USER:
174
169
tr_info (" status_cb: User interrupt" );
175
170
break ;
176
- }
177
- case PPPERR_CONNECT: {
171
+
172
+ case PPPERR_CONNECT:
178
173
tr_info (" status_cb: Connection lost" );
179
174
mapped_err_code = NSAPI_ERROR_CONNECTION_LOST;
180
175
break ;
181
- }
182
- case PPPERR_AUTHFAIL: {
176
+
177
+ case PPPERR_AUTHFAIL:
183
178
tr_info (" status_cb: Failed authentication challenge" );
184
179
mapped_err_code = NSAPI_ERROR_AUTH_FAILURE;
185
180
break ;
186
- }
187
- case PPPERR_PROTOCOL: {
181
+
182
+ case PPPERR_PROTOCOL:
188
183
tr_info (" status_cb: Failed to meet protocol" );
189
184
break ;
190
- }
191
- case PPPERR_PEERDEAD: {
185
+
186
+ case PPPERR_PEERDEAD:
192
187
tr_info (" status_cb: Connection timeout" );
193
188
mapped_err_code = NSAPI_ERROR_CONNECTION_TIMEOUT;
194
189
break ;
195
- }
196
- case PPPERR_IDLETIMEOUT: {
190
+
191
+ case PPPERR_IDLETIMEOUT:
197
192
tr_info (" status_cb: Idle Timeout" );
198
193
break ;
199
- }
200
- case PPPERR_CONNECTTIME: {
194
+
195
+ case PPPERR_CONNECTTIME:
201
196
tr_info (" status_cb: Max connect time reached" );
202
197
break ;
203
- }
204
- case PPPERR_LOOPBACK: {
198
+
199
+ case PPPERR_LOOPBACK:
205
200
tr_info (" status_cb: Loopback detected" );
206
201
break ;
207
- }
208
- default : {
202
+
203
+ default :
209
204
tr_info (" status_cb: Unknown error code %d" , err_code);
210
205
break ;
211
- }
206
+
212
207
}
213
208
214
209
if (err_code == PPPERR_NONE) {
215
- ppp_link_up = true ;
216
210
/* suppress generating a callback event for connection up
217
211
* Because connect() call is blocking, why wait for a callback */
218
212
return ;
219
213
}
220
214
221
215
/* If some error happened, we need to properly shutdown the PPP interface */
222
216
if (ppp_active) {
223
- ppp_link_up = false ;
224
217
ppp_active = false ;
225
218
connect_error_code = mapped_err_code;
226
219
sys_sem_signal (&ppp_close_sem);
227
220
}
228
221
229
222
/* Alright, PPP interface is down, we need to notify upper layer */
230
- notify_ppp_link_down (mapped_err_code);
223
+ if (connection_status_cb) {
224
+ connection_status_cb (mapped_err_code);
225
+ }
226
+ }
227
+
228
+ static void handle_modem_hangup ()
229
+ {
230
+ if (my_ppp_pcb->phase != PPP_PHASE_DEAD) {
231
+ ppp_close (my_ppp_pcb, 1 );
232
+ }
231
233
}
232
234
233
235
#if !PPP_INPROC_IRQ_SAFE
@@ -248,7 +250,8 @@ static void ppp_input()
248
250
fhs.events = POLLIN;
249
251
poll (&fhs, 1 , 0 );
250
252
if (fhs.revents & (POLLHUP|POLLERR|POLLNVAL)) {
251
- goto hup;
253
+ handle_modem_hangup ();
254
+ return ;
252
255
}
253
256
254
257
// Infinite loop, but we assume that we can read faster than the
@@ -259,17 +262,12 @@ static void ppp_input()
259
262
if (len == -EAGAIN) {
260
263
break ;
261
264
} else if (len <= 0 ) {
262
- goto hup;
265
+ handle_modem_hangup ();
266
+ return ;
263
267
}
264
268
pppos_input (my_ppp_pcb, buffer, len);
265
269
}
266
270
return ;
267
-
268
- hup:
269
- if (my_ppp_pcb->phase != PPP_PHASE_DEAD) {
270
- ppp_close (my_ppp_pcb, 1 );
271
- }
272
- return ;
273
271
}
274
272
275
273
static void stream_cb () {
@@ -343,14 +341,14 @@ nsapi_error_t nsapi_ppp_error_code()
343
341
return connect_error_code;
344
342
}
345
343
346
- nsapi_error_t nsapi_ppp_connect (FileHandle *stream, void (*ppp_link_down_cb)( int ) , const char *uname, const char *password)
344
+ nsapi_error_t nsapi_ppp_connect (FileHandle *stream, Callback< void ( nsapi_error_t )> cb , const char *uname, const char *password)
347
345
{
348
346
if (my_stream) {
349
347
return NSAPI_ERROR_PARAMETER;
350
348
}
351
349
my_stream = stream;
352
350
stream->set_blocking (false );
353
- notify_ppp_link_down = ppp_link_down_cb ;
351
+ connection_status_cb = cb ;
354
352
login = uname;
355
353
pwd = password;
356
354
@@ -375,26 +373,45 @@ NetworkStack *nsapi_ppp_get_stack()
375
373
return nsapi_create_stack (&lwip_stack);
376
374
}
377
375
378
- char *nsapi_ppp_get_ip_addr (char *ip_addr, nsapi_size_t buflen )
376
+ const char *nsapi_ppp_get_ip_addr (FileHandle *stream )
379
377
{
380
- if (mbed_lwip_get_ip_address (ip_addr, buflen)) {
381
- return ip_addr;
378
+ static char ip_addr[IPADDR_STRLEN_MAX];
379
+
380
+ if (stream == my_stream) {
381
+
382
+ if (mbed_lwip_get_ip_address (ip_addr, IPADDR_STRLEN_MAX)) {
383
+ return ip_addr;
384
+ }
382
385
}
383
386
384
387
return NULL ;
385
388
}
386
- char *nsapi_ppp_get_netmask (char *netmask, nsapi_size_t buflen )
389
+ const char *nsapi_ppp_get_netmask (FileHandle *stream )
387
390
{
388
- if (mbed_lwip_get_netmask (netmask, buflen)) {
389
- return netmask;
391
+ #if LWIP_IPV6
392
+ return NULL ;
393
+ #endif
394
+
395
+ static char netmask[IPADDR_STRLEN_MAX];
396
+ if (stream == my_stream) {
397
+ if (mbed_lwip_get_netmask (netmask, IPADDR_STRLEN_MAX)) {
398
+ return netmask;
399
+ }
390
400
}
391
401
392
402
return NULL ;
393
403
}
394
- char *nsapi_ppp_get_gw_addr (char *default_gw_addr )
404
+ const char *nsapi_ppp_get_gw_addr (FileHandle *stream )
395
405
{
396
- if (mbed_lwip_get_gateway (default_gw_addr, sizeof default_gw_addr)) {
397
- return default_gw_addr;
406
+ #if LWIP_IPV6
407
+ return NULL ;
408
+ #endif
409
+
410
+ static char gwaddr[IPADDR_STRLEN_MAX];
411
+ if (stream == my_stream) {
412
+ if (mbed_lwip_get_gateway (gwaddr, IPADDR_STRLEN_MAX)) {
413
+ return gwaddr;
414
+ }
398
415
}
399
416
400
417
return NULL ;
0 commit comments