Nanostack changes for PPP service

Created PPP interface for PPP service. Re-used the ethernet tasklet
and PHY driver structure for PPP.
pull/10974/head
Mika Leppänen 2019-06-18 13:41:46 +03:00
parent 3864e12172
commit a2df462f65
13 changed files with 620 additions and 11 deletions

View File

@ -34,7 +34,10 @@ public:
void get_mac_address(uint8_t *buf) const
{
interface_phy.get_mac_address(buf);
NanostackMACPhy *phy = interface_phy.nanostack_mac_phy();
if (phy) {
phy->get_mac_address(buf);
}
}
/**

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2019 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NANOSTACKPPPINTERFACE_H
#define NANOSTACKPPPINTERFACE_H
#include "MeshInterfaceNanostack.h"
#include "PPPInterface.h"
#include "NanostackPPPPhy.h"
class Nanostack::PPPInterface : public Nanostack::Interface {
public:
virtual nsapi_error_t bringup(bool dhcp, const char *ip,
const char *netmask, const char *gw,
nsapi_ip_stack_t stack = DEFAULT_STACK,
bool blocking = true);
virtual nsapi_error_t bringdown();
typedef mbed::Callback<void (uint8_t up, int8_t device_id)> link_state_cb_t;
virtual void set_link_state_changed_callback(link_state_cb_t link_state_cb);
private:
friend class Nanostack;
PPPInterface(NanostackPhy &phy) : Interface(phy), link_state_up(false), enet_tasklet_connected(false) {}
nsapi_error_t initialize();
void link_state_changed(bool up);
nsapi_error_t connect_enet_tasklet();
link_state_cb_t link_state_cb;
bool link_state_up;
bool enet_tasklet_connected;
};
#endif

View File

@ -0,0 +1,386 @@
/*
* Copyright (c) 2019 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "NanostackPPPInterface.h"
#include "NanostackPPPPhy.h"
#include "PPP.h"
#include "nsdynmemLIB.h"
#include "arm_hal_phy.h"
#include "mbed_interface.h"
#include "mesh_system.h"
#include "callback_handler.h"
#include "enet_tasklet.h"
#include "ip6string.h"
class PPPPhy : public NanostackPPPPhy {
public:
PPPPhy(NanostackMemoryManager &mem, PPP &m);
virtual int8_t phy_register();
virtual void phy_power_on();
virtual void phy_power_off();
virtual void get_iid64(uint8_t *iid64);
virtual uint16_t get_mtu();
virtual void set_link_state_change_cb(link_state_change_cb_t cb);
int8_t tx(uint8_t *data_ptr, uint16_t data_len, uint8_t tx_handle, data_protocol_e data_flow);
void ppp_phy_rx(net_stack_mem_buf_t *mem);
void link_state_cb(bool up);
private:
NanostackMemoryManager &memory_manager;
PPP &ppp;
uint8_t iid64[8];
link_state_change_cb_t link_state_change_cb;
bool active;
bool powered_up;
int8_t device_id;
phy_device_driver_s phy;
};
nsapi_error_t Nanostack::PPPInterface::initialize()
{
nanostack_lock();
if (register_phy() < 0) {
nanostack_unlock();
return NSAPI_ERROR_DEVICE_ERROR;
}
nanostack_unlock();
return NSAPI_ERROR_OK;
}
nsapi_error_t Nanostack::PPPInterface::bringup(bool dhcp, const char *ip,
const char *netmask, const char *gw,
nsapi_ip_stack_t stack, bool blocking)
{
if (stack == IPV4_STACK) {
return NSAPI_ERROR_UNSUPPORTED;
}
_blocking = blocking;
if (link_state_up) {
connect_enet_tasklet();
}
if (blocking) {
uint8_t retries = 10;
while (_connect_status != NSAPI_STATUS_GLOBAL_UP) {
int32_t count = connect_semaphore.try_acquire_for(3000);
if (count <= 0 && retries-- == 0) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of...
}
// Not up until global up
if (_connect_status == NSAPI_STATUS_LOCAL_UP) {
_connect_status = NSAPI_STATUS_CONNECTING;
}
}
}
return NSAPI_ERROR_OK;
}
nsapi_error_t Nanostack::PPPInterface::connect_enet_tasklet()
{
if (enet_tasklet_connected) {
return NSAPI_ERROR_OK;
}
enet_tasklet_connected = true;
nanostack_lock();
if (interface_id < 0) {
enet_tasklet_init();
__mesh_handler_set_callback(this);
interface_id = enet_tasklet_ppp_network_init(_device_id);
}
int8_t status = -1;
if (interface_id >= 0) {
status = enet_tasklet_connect(&__mesh_handler_c_callback, interface_id);
}
nanostack_unlock();
if (status == -1) {
return NSAPI_ERROR_DEVICE_ERROR;
} else if (status == -2) {
return NSAPI_ERROR_NO_MEMORY;
} else if (status == -3) {
return NSAPI_ERROR_ALREADY;
} else if (status != 0) {
return NSAPI_ERROR_DEVICE_ERROR;
}
return NSAPI_ERROR_OK;
}
nsapi_error_t Nanostack::PPPInterface::bringdown()
{
nanostack_lock();
int8_t status = enet_tasklet_disconnect(true);
enet_tasklet_connected = false;
nanostack_unlock();
if (status == -1) {
return NSAPI_ERROR_DEVICE_ERROR;
} else if (status == -2) {
return NSAPI_ERROR_NO_MEMORY;
} else if (status == -3) {
return NSAPI_ERROR_ALREADY;
} else if (status != 0) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (_blocking) {
int32_t count = disconnect_semaphore.try_acquire_for(30000);
if (count <= 0) {
return NSAPI_ERROR_TIMEOUT;
}
}
return NSAPI_ERROR_OK;
}
void Nanostack::PPPInterface::link_state_changed(bool up)
{
link_state_up = up;
// If application callback is set
if (link_state_cb) {
link_state_cb(up, _device_id);
} else {
if (up) {
connect_enet_tasklet();
}
}
}
void Nanostack::PPPInterface::set_link_state_changed_callback(link_state_cb_t new_link_state_cb)
{
link_state_cb = new_link_state_cb;
}
// GAH! no handles on the callback. Force a single interface
static PPPPhy *single_phy;
extern "C"
{
static int8_t ppp_phy_interface_state_control(phy_interface_state_e, uint8_t)
{
return -1;
}
static int8_t ppp_phy_tx(uint8_t *data_ptr, uint16_t data_len, uint8_t tx_handle, data_protocol_e data_flow)
{
return single_phy->tx(data_ptr, data_len, tx_handle, data_flow);
}
PPPPhy::PPPPhy(NanostackMemoryManager &mem, PPP &m) : memory_manager(mem), ppp(m), link_state_change_cb(NULL), active(false), powered_up(false), device_id(-1)
{
}
void PPPPhy::ppp_phy_rx(net_stack_mem_buf_t *mem)
{
if (!active) {
memory_manager.free(mem);
return;
}
const uint8_t *ptr = NULL;
uint8_t *tmpbuf = NULL;
uint32_t total_len;
if (memory_manager.get_next(mem) == NULL) {
// Easy contiguous case
ptr = static_cast<const uint8_t *>(memory_manager.get_ptr(mem));
total_len = memory_manager.get_len(mem);
} else {
// Nanostack can't accept chunked data - make temporary contiguous copy
total_len = memory_manager.get_total_len(mem);
ptr = tmpbuf = static_cast<uint8_t *>(ns_dyn_mem_temporary_alloc(total_len));
if (tmpbuf) {
memory_manager.copy_from_buf(tmpbuf, total_len, mem);
}
}
if (ptr && phy.phy_rx_cb) {
phy.phy_rx_cb(ptr, total_len, 0xff, 0, device_id);
}
ns_dyn_mem_free(tmpbuf);
memory_manager.free(mem);
}
} // extern "C"
int8_t PPPPhy::tx(uint8_t *data_ptr, uint16_t data_len, uint8_t tx_handle, data_protocol_e data_flow)
{
if (!active) {
return -1;
}
net_stack_mem_buf_t *mem = memory_manager.alloc_pool(data_len, 0);
if (!mem) {
return -1;
}
memory_manager.copy_to_buf(mem, data_ptr, data_len);
// They take ownership - their responsibility to free
ppp.link_out(mem, IPV6_STACK);
return 0;
}
int8_t PPPPhy::phy_register()
{
active = true;
if (device_id < 0) {
phy.PHY_MAC = iid64;
phy.address_write = NULL;
phy.driver_description = const_cast<char *>("PPP");
phy.link_type = PHY_LINK_PPP;
phy.phy_MTU = 0;
phy.phy_header_length = 0;
phy.phy_tail_length = 0;
phy.state_control = ppp_phy_interface_state_control;
phy.tx = ppp_phy_tx;
phy.phy_rx_cb = NULL;
phy.phy_tx_done_cb = NULL;
ppp.set_memory_manager(memory_manager);
ppp.set_link_input_cb(mbed::callback(this, &PPPPhy::ppp_phy_rx));
if (link_state_change_cb) {
ppp.set_link_state_cb(mbed::callback(this, &PPPPhy::link_state_cb));
}
device_id = arm_net_phy_register(&phy);
if (device_id < 0) {
return -1;
}
}
return device_id;
}
void PPPPhy::phy_power_on()
{
if (!powered_up) {
if (!ppp.power_up()) {
return;
}
phy.phy_MTU = get_mtu();
powered_up = true;
active = true;
}
}
void PPPPhy::phy_power_off()
{
if (powered_up) {
ppp.power_down();
powered_up = false;
}
active = false;
}
void PPPPhy::set_link_state_change_cb(link_state_change_cb_t cb)
{
link_state_change_cb = cb;
}
void PPPPhy::link_state_cb(bool up)
{
// Read negotiated parameters from PPP
if (up) {
get_iid64(iid64);
}
// Call upper level callback if set
if (link_state_change_cb) {
link_state_change_cb(up);
}
}
void PPPPhy::get_iid64(uint8_t *iid64)
{
if (!iid64) {
return;
}
// Read link local IPv6 address
const nsapi_addr_t *ipv6_ll_addr = ppp.get_ip_address(NSAPI_IPv6);
if (ipv6_ll_addr) {
memcpy(iid64, &ipv6_ll_addr->bytes[8], 8);
}
}
uint16_t PPPPhy::get_mtu()
{
return ppp.get_mtu_size();
}
nsapi_error_t Nanostack::add_ppp_interface(PPP &ppp, bool default_if, Nanostack::PPPInterface **interface_out)
{
if (!single_phy) {
single_phy = new (std::nothrow) PPPPhy(this->memory_manager, ppp);
if (!single_phy) {
return NSAPI_ERROR_NO_MEMORY;
}
}
static Nanostack::PPPInterface *interface = NULL;
if (interface == NULL) {
interface = new (std::nothrow) Nanostack::PPPInterface(*single_phy);
if (!interface) {
return NSAPI_ERROR_NO_MEMORY;
}
single_phy->set_link_state_change_cb(mbed::callback(interface, &Nanostack::PPPInterface::link_state_changed));
}
interface->initialize();
single_phy->phy_power_on();
*interface_out = interface;
return NSAPI_ERROR_OK;
}
nsapi_error_t Nanostack::add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out)
{
Nanostack::PPPInterface *interface;
nsapi_error_t err = add_ppp_interface(ppp, default_if, &interface);
*interface_out = interface;
return err;
}
nsapi_error_t Nanostack::remove_ppp_interface(OnboardNetworkStack::Interface **interface_out)
{
single_phy->phy_power_off();
return NSAPI_ERROR_OK;
}

View File

@ -335,6 +335,41 @@ int8_t enet_tasklet_network_init(int8_t device_id)
return tasklet_data_ptr->network_interface_id;
}
/* For now, to enable to compile without Nanostack interface changes, enables Nanostack PPP
interface only if default stack is Nanostack and interface cellular. After Nanostack
with updated interface call arm_nwk_interface_ppp_init() is integrated to mbed-os, this
flagging can be removed.
*/
#define NANOSTACK 0x11991199
#define CELLULAR 0x22992299
#if MBED_CONF_NSAPI_DEFAULT_STACK == NANOSTACK && MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == CELLULAR
#define ENABLE_NANOSTACK_PPP
#endif
#undef NANOSTACK
#undef CELLULAR
int8_t enet_tasklet_ppp_network_init(int8_t device_id)
{
#ifdef ENABLE_NANOSTACK_PPP
if (tasklet_data_ptr->network_interface_id != -1) {
tr_debug("Interface already at active state\n");
return tasklet_data_ptr->network_interface_id;
}
if (!eth_mac_api) {
eth_mac_api = ethernet_mac_create(device_id);
}
tasklet_data_ptr->network_interface_id = arm_nwk_interface_ppp_init(eth_mac_api, "ppp0");
tr_debug("interface ID: %d", tasklet_data_ptr->network_interface_id);
arm_nwk_interface_configure_ipv6_bootstrap_set(
tasklet_data_ptr->network_interface_id, NET_IPV6_BOOTSTRAP_AUTONOMOUS, NULL);
return tasklet_data_ptr->network_interface_id;
#else
return -1;
#endif
}
void enet_tasklet_link_state_changed(bool up)
{
if (up) {

View File

@ -36,6 +36,14 @@ void enet_tasklet_init(void);
*/
int8_t enet_tasklet_network_init(int8_t device_id);
/*
* \brief Create PPP network interface.
*
* \param device_id Registered physical device.
* \return interface ID used to communication with this interface.
*/
int8_t enet_tasklet_ppp_network_init(int8_t device_id);
/*
* \brief Connect to Ethernet network.
*

View File

@ -37,6 +37,7 @@ public:
class LoWPANNDInterface;
class ThreadInterface;
class WisunInterface;
class PPPInterface;
/* Implement OnboardNetworkStack method */
virtual nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out);
@ -44,6 +45,13 @@ public:
/* Local variant with stronger typing and manual address specification */
nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, Nanostack::EthernetInterface **interface_out, const uint8_t *mac_addr = NULL);
virtual nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out);
/* Local variant with stronger typing and manual address specification */
nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, Nanostack::PPPInterface **interface_out);
nsapi_error_t remove_ppp_interface(OnboardNetworkStack::Interface **interface_out);
protected:
Nanostack();

View File

@ -18,10 +18,10 @@
#ifndef NANOSTACK_ETHERNET_PHY_H_
#define NANOSTACK_ETHERNET_PHY_H_
#include "NanostackPhy.h"
#include "NanostackMACPhy.h"
/** Ethernet PHY driver class for Nanostack */
class NanostackEthernetPhy : public NanostackPhy {
class NanostackEthernetPhy : public NanostackMACPhy {
};
#endif /* NANOSTACK_INTERFACE_H_ */

View File

@ -23,6 +23,7 @@
#include "LoWPANNDInterface.h"
#include "ThreadInterface.h"
#include "NanostackEthernetInterface.h"
#include "NanostackPPPInterface.h"
#include "MeshInterfaceNanostack.h"
#include "WisunInterface.h"
#endif /* NANOSTACK_INTERFACE_H_ */

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2019, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NANOSTACK_MACPHY_H_
#define NANOSTACK_MACPHY_H_
#include "NanostackPhy.h"
/** MAC PHY driver class for Nanostack */
class NanostackMACPhy : public NanostackPhy {
public:
/** Read the mac address of this physical interface
*
* Note - some devices do not have a mac address
* in hardware.
*
* @param mac mac address
*/
virtual void get_mac_address(uint8_t *mac) = 0;
/** Set the mac address of this physical interface
*
* @param mac mac address
*/
virtual void set_mac_address(uint8_t *mac) = 0;
/** Provide access to the NanostackMACPhy
*
* @return NanostackMACPhy
*/
virtual NanostackMACPhy *nanostack_mac_phy()
{
return this;
}
};
#endif /* NANOSTACK_MACPHY_H_ */

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2019, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NANOSTACK_PPP_PHY_H_
#define NANOSTACK_PPP_PHY_H_
#include "NanostackPhy.h"
/** PPP PHY driver class for Nanostack */
class NanostackPPPPhy : public NanostackPhy {
public:
/** Link state callback function prototype
*
* @param up link up
*/
typedef mbed::Callback<void (bool up)> link_state_change_cb_t;
/** Set link state callback of this physical interface
*
* @param cb callback
*/
virtual void set_link_state_change_cb(link_state_change_cb_t cb) = 0;
/** Read the iid64 address of this physical interface
*
* @param iid64 iid64 address
*/
virtual void get_iid64(uint8_t *iid64) = 0;
/** Read the mtu of this physical interface
*
* @return mtu
*/
virtual uint16_t get_mtu() = 0;
/** Provide access to the NanostackMACPhy
*
* @return NanostackPPPPhy
*/
virtual NanostackPPPPhy *nanostack_ppp_phy()
{
return this;
}
};
#endif /* NANOSTACK_PPP_PHY_H_ */

View File

@ -18,6 +18,9 @@
#ifndef NANOSTACK_PHY_H_
#define NANOSTACK_PHY_H_
class NanostackMACPhy;
class NanostackPPPPhy;
/** PHY driver class for Nanostack */
class NanostackPhy {
public:
@ -29,17 +32,25 @@ public:
*/
virtual int8_t phy_register() = 0;
/** Read the mac address of this physical interface
/** Return pointer to a NanostackMACPhy.
*
* Note - some devices do not have a mac address
* in hardware.
* @return Pointer to requested phy type or NULL if this
* class doesn't implement the phy.
*/
virtual void get_mac_address(uint8_t *mac) = 0;
virtual NanostackMACPhy *nanostack_mac_phy()
{
return 0;
}
/** Set the mac address of this physical interface
/** Return pointer to a NanostackPPPPhy.
*
* @return Pointer to requested phy type or NULL if this
* class doesn't implement the phy.
*/
virtual void set_mac_address(uint8_t *mac) = 0;
virtual NanostackPPPPhy *nanostack_ppp_phy()
{
return 0;
}
protected:
NanostackPhy() {}

View File

@ -18,10 +18,10 @@
#ifndef NANOSTACK_RF_PHY_H_
#define NANOSTACK_RF_PHY_H_
#include "NanostackPhy.h"
#include "NanostackMACPhy.h"
/** Radio PHY driver class for Nanostack */
class NanostackRfPhy : public NanostackPhy {
class NanostackRfPhy : public NanostackMACPhy {
public:
/** Return the default on-board NanostackRfPhy

View File

@ -95,6 +95,7 @@ typedef enum phy_link_type_e {
PHY_LINK_15_4_SUBGHZ_TYPE, /**< Standard 802.15.4 subGHz radio 868 /915MHz. */
PHY_LINK_TUN, /**< Tunnel interface for Linux TUN, RF network driver over serial bus or just basic application to application data flow. */
PHY_LINK_SLIP, /**< Generic SLIP driver which just forward SLIP payload */
PHY_LINK_PPP, /**< PPP interface */
} phy_link_type_e;
/** Data layers */