Skip to content

Commit c9cb8f8

Browse files
author
Mika Leppänen
committed
Created netsocket classes for PPP service and PPP interface
PPP service encapsulates the PPP protocol. PPP interface can be used as helper class to bind PPP protocol with network stack (similar to EMAC and L3IP interface). Added PPP interface to onboard network stack class.
1 parent a1e3a5d commit c9cb8f8

File tree

5 files changed

+546
-0
lines changed

5 files changed

+546
-0
lines changed

features/netsocket/NetworkInterface.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class WiFiInterface;
3636
class MeshInterface;
3737
class CellularInterface;
3838
class EMACInterface;
39+
class PPPInterface;
3940

4041
/** Common interface that is shared between network devices.
4142
*
@@ -338,6 +339,14 @@ class NetworkInterface: public DNS {
338339
return 0;
339340
}
340341

342+
/** Return pointer to a PPPInterface.
343+
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
344+
*/
345+
virtual PPPInterface *pppInterface()
346+
{
347+
return 0;
348+
}
349+
341350
/** Return pointer to a CellularInterface.
342351
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
343352
* @deprecated CellularBase migrated to CellularInterface - use cellularInterface()

features/netsocket/OnboardNetworkStack.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "NetworkStack.h"
2323
#include "EMAC.h"
2424
#include "L3IP.h"
25+
#include "PPP.h"
2526

2627
/**
2728
* mbed OS API for onboard IP stack abstraction
@@ -165,11 +166,21 @@ class OnboardNetworkStack : public NetworkStack {
165166
return NSAPI_ERROR_OK;
166167
};
167168

169+
virtual nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, Interface **interface_out)
170+
{
171+
return NSAPI_ERROR_UNSUPPORTED;
172+
};
173+
168174
virtual nsapi_error_t remove_l3ip_interface(Interface **interface_out)
169175
{
170176
return NSAPI_ERROR_OK;
171177
};
172178

179+
virtual nsapi_error_t remove_ppp_interface(Interface **interface_out)
180+
{
181+
return NSAPI_ERROR_UNSUPPORTED;
182+
};
183+
173184
virtual void set_default_interface(OnboardNetworkStack::Interface *interface)
174185
{
175186
}

features/netsocket/PPP.h

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef PPP_H_
19+
#define PPP_H_
20+
21+
#include <stdbool.h>
22+
#include "Callback.h"
23+
#include "FileHandle.h"
24+
#include "NetStackMemoryManager.h"
25+
26+
class PPP {
27+
public:
28+
29+
/** Return the default on-board PPP
30+
*
31+
* Returns the default on-board PPP - this will be target-specific, and
32+
* may not be available on all targets.
33+
*/
34+
static PPP &get_default_instance();
35+
36+
virtual ~PPP() {};
37+
38+
/**
39+
* Callback to be registered with PPP interface and to be called for received packets
40+
*
41+
* @param buf Received data
42+
*/
43+
//typedef void (*ppp_link_input_fn)(void *data, net_stack_mem_buf_t *buf);
44+
typedef mbed::Callback<void (net_stack_mem_buf_t *buf)> ppp_link_input_cb_t;
45+
46+
/**
47+
* Callback to be registered with PPP interface and to be called for link status changes
48+
*
49+
* @param up Link status
50+
*/
51+
//typedef void (*ppp_link_state_change_fn)(void *data, bool up);
52+
typedef mbed::Callback<void (bool up)> ppp_link_state_change_cb_t;
53+
54+
/**
55+
* Return maximum transmission unit
56+
*
57+
* @return MTU in bytes
58+
*/
59+
virtual uint32_t get_mtu_size() = 0;
60+
61+
/**
62+
* Gets memory buffer alignment preference
63+
*
64+
* Gets preferred memory buffer alignment of the cellular device.
65+
* @return Memory alignment requirement in bytes
66+
*/
67+
virtual uint32_t get_align_preference() const = 0;
68+
69+
/**
70+
* Return interface name
71+
*
72+
* @param name Pointer to where the name should be written
73+
* @param size Maximum number of characters to copy
74+
*/
75+
virtual void get_ifname(char *name, uint8_t size) const = 0;
76+
77+
/**
78+
* Sends the packet over the link
79+
*
80+
* That cannot be called from an interrupt context.
81+
*
82+
* @param buf Packet to be sent
83+
* @return True if the packet was sent, false otherwise
84+
*/
85+
virtual bool link_out(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack) = 0;
86+
87+
/**
88+
* Initializes the PPP
89+
*
90+
* @return True on success, False in case of an error.
91+
*/
92+
virtual bool power_up() = 0;
93+
94+
/**
95+
* Deinitializes the PPP
96+
*
97+
*/
98+
virtual void power_down() = 0;
99+
100+
/**
101+
* Sets a callback that needs to be called for packets received for that interface
102+
*
103+
* @param input_cb Function to be register as a callback
104+
*/
105+
virtual void set_link_input_cb(ppp_link_input_cb_t input_cb) = 0;
106+
107+
/**
108+
* Sets a callback that needs to be called on link status changes for given interface
109+
*
110+
* @param state_cb Function to be register as a callback
111+
*/
112+
virtual void set_link_state_cb(ppp_link_state_change_cb_t state_cb) = 0;
113+
114+
/** Sets memory manager that is used to handle memory buffers
115+
*
116+
* @param mem_mngr Pointer to memory manager
117+
*/
118+
virtual void set_memory_manager(NetStackMemoryManager &mem_mngr) = 0;
119+
120+
/** Sets file stream used to communicate with modem
121+
*
122+
* @param stream Pointer to file handle
123+
*/
124+
virtual void set_stream(mbed::FileHandle *stream) = 0;
125+
126+
/** Sets IP protocol versions of IP stack
127+
*
128+
* @param ip_stack IP protocol version
129+
*/
130+
virtual void set_ip_stack(nsapi_ip_stack_t ip_stack) = 0;
131+
132+
/** Sets user name and password for PPP protocol
133+
*
134+
* @param uname User name
135+
* @param password Password
136+
*/
137+
virtual void set_credentials(const char *uname, const char *password) = 0;
138+
139+
/** Gets local IP address
140+
*
141+
* @param version IP address version
142+
* @return IP address
143+
*/
144+
virtual const nsapi_addr_t *get_ip_address(nsapi_version_t version) = 0;
145+
146+
/** Get the local network mask.
147+
*
148+
* @return Local network mask or null if no network mask has been received.
149+
*/
150+
virtual const nsapi_addr_t *get_netmask() = 0;
151+
152+
/** Get the local gateway.
153+
*
154+
* @return Local gateway or null if no network mask has been received.
155+
*/
156+
virtual const nsapi_addr_t *get_gateway() = 0;
157+
158+
/** Gets DNS server address
159+
*
160+
* @param index Server index
161+
*/
162+
virtual const nsapi_addr_t *get_dns_server(uint8_t index) = 0;
163+
};
164+
165+
#endif /* PPP_H_ */

features/netsocket/PPPInterface.cpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "PPPInterface.h"
19+
20+
using namespace mbed;
21+
22+
/* Interface implementation */
23+
PPPInterface::PPPInterface(PPP &ppp, OnboardNetworkStack &stack) :
24+
_ppp(ppp),
25+
_stack(stack),
26+
_interface(NULL),
27+
_blocking(true),
28+
_ip_address(),
29+
_netmask(),
30+
_gateway(),
31+
_stream(NULL),
32+
_ip_stack(DEFAULT_STACK),
33+
_uname(NULL),
34+
_password(NULL)
35+
{
36+
}
37+
38+
PPPInterface::~PPPInterface()
39+
{
40+
_stack.remove_ppp_interface(&_interface);
41+
}
42+
43+
nsapi_error_t PPPInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
44+
{
45+
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
46+
_ip_address[sizeof(_ip_address) - 1] = '\0';
47+
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
48+
_netmask[sizeof(_netmask) - 1] = '\0';
49+
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
50+
_gateway[sizeof(_gateway) - 1] = '\0';
51+
52+
return NSAPI_ERROR_OK;
53+
}
54+
55+
nsapi_error_t PPPInterface::connect()
56+
{
57+
_ppp.set_stream(_stream);
58+
_ppp.set_ip_stack(_ip_stack);
59+
_ppp.set_credentials(_uname, _password);
60+
61+
if (!_interface) {
62+
nsapi_error_t err = _stack.add_ppp_interface(_ppp, true, &_interface);
63+
if (err != NSAPI_ERROR_OK) {
64+
_interface = NULL;
65+
return err;
66+
}
67+
_interface->attach(_connection_status_cb);
68+
}
69+
70+
return _interface->bringup(false,
71+
_ip_address[0] ? _ip_address : 0,
72+
_netmask[0] ? _netmask : 0,
73+
_gateway[0] ? _gateway : 0,
74+
_ip_stack,
75+
_blocking);
76+
}
77+
78+
nsapi_error_t PPPInterface::disconnect()
79+
{
80+
if (_interface) {
81+
return _interface->bringdown();
82+
}
83+
return NSAPI_ERROR_OK;
84+
}
85+
86+
const char *PPPInterface::get_ip_address()
87+
{
88+
if (_interface && _interface->get_ip_address(_ip_address, sizeof(_ip_address))) {
89+
return _ip_address;
90+
}
91+
92+
return NULL;
93+
}
94+
95+
const char *PPPInterface::get_netmask()
96+
{
97+
if (_interface && _interface->get_netmask(_netmask, sizeof(_netmask))) {
98+
return _netmask;
99+
}
100+
101+
return 0;
102+
}
103+
104+
const char *PPPInterface::get_gateway()
105+
{
106+
if (_interface && _interface->get_gateway(_gateway, sizeof(_gateway))) {
107+
return _gateway;
108+
}
109+
110+
return 0;
111+
}
112+
113+
char *PPPInterface::get_interface_name(char *interface_name)
114+
{
115+
if (_interface) {
116+
return _interface->get_interface_name(interface_name);
117+
}
118+
119+
return NULL;
120+
}
121+
122+
void PPPInterface::set_as_default()
123+
{
124+
if (_interface) {
125+
_stack.set_default_interface(_interface);
126+
}
127+
}
128+
129+
void PPPInterface::set_stream(mbed::FileHandle *stream)
130+
{
131+
_stream = stream;
132+
}
133+
134+
void PPPInterface::set_ip_stack(nsapi_ip_stack_t ip_stack)
135+
{
136+
137+
_ip_stack = ip_stack;
138+
}
139+
140+
void PPPInterface::set_credentials(const char *uname, const char *password)
141+
{
142+
_uname = uname;
143+
_password = password;
144+
}
145+
146+
NetworkStack *PPPInterface::get_stack()
147+
{
148+
return &_stack;
149+
}
150+
151+
void PPPInterface::attach(
152+
mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
153+
{
154+
_connection_status_cb = status_cb;
155+
if (_interface) {
156+
_interface->attach(status_cb);
157+
}
158+
}
159+
160+
nsapi_connection_status_t PPPInterface::get_connection_status() const
161+
{
162+
if (_interface) {
163+
return _interface->get_connection_status();
164+
} else {
165+
return NSAPI_STATUS_DISCONNECTED;
166+
}
167+
}
168+
169+
nsapi_error_t PPPInterface::set_blocking(bool blocking)
170+
{
171+
_blocking = blocking;
172+
return NSAPI_ERROR_OK;
173+
}
174+

0 commit comments

Comments
 (0)