Finalizing the public API for Cellular

* state machine corrections
* adding various standard API methods
* Addition/revision/enhancement of the nsapi_ppp glue layer
* Turning off debug by default
pull/4119/head
Hasnain Virk 2017-03-13 13:34:49 +02:00
parent f602c936ff
commit 01088647b9
7 changed files with 130 additions and 30 deletions

View File

@ -7,6 +7,9 @@
#include "cmsis_os.h"
#include "mbed_interface.h"
// Check for Ethernet HAL being present
#ifdef ETH_SUCCESS
#define RECV_TASK_PRI (osPriorityHigh)
#define PHY_TASK_PRI (osPriorityLow)
#define PHY_TASK_WAIT (200)
@ -512,3 +515,6 @@ void mbed_default_mac_address(char *mac) {
return;
}
#endif //ETH_SUCCESS

View File

@ -510,7 +510,9 @@ nsapi_error_t mbed_lwip_bringup(bool dhcp, bool ppp, const char *ip, const char
}
netif_inited = true;
netif_is_ppp = ppp;
if (ppp) {
netif_is_ppp = ppp;
}
netif_set_default(&lwip_netif);
netif_set_link_callback(&lwip_netif, mbed_lwip_netif_link_irq);
@ -565,6 +567,9 @@ nsapi_error_t mbed_lwip_bringup(bool dhcp, bool ppp, const char *ip, const char
if (!netif_is_link_up(&lwip_netif)) {
if (sys_arch_sem_wait(&lwip_netif_linked, 15000) == SYS_ARCH_TIMEOUT) {
if (ppp) {
ppp_lwip_disconnect();
}
return NSAPI_ERROR_NO_CONNECTION;
}
}
@ -588,6 +593,9 @@ nsapi_error_t mbed_lwip_bringup(bool dhcp, bool ppp, const char *ip, const char
// If doesn't have address
if (!mbed_lwip_get_ip_addr(true, &lwip_netif)) {
if (sys_arch_sem_wait(&lwip_netif_has_addr, 15000) == SYS_ARCH_TIMEOUT) {
if (ppp) {
ppp_lwip_disconnect();
}
return NSAPI_ERROR_DHCP_FAILURE;
}
}

View File

@ -50,15 +50,17 @@ using events::EventQueue;
static EventQueue *event_queue;
static Thread *event_thread;
static volatile bool event_queued;
static nsapi_error_t connect_error_code;
// Just one interface for now
static FileHandle *my_stream;
static ppp_pcb *my_ppp_pcb;
static bool ppp_link_up = false;
static bool ppp_active = false;
static const char *login;
static const char *pwd;
static sys_sem_t ppp_close_sem;
static void (*notify_ppp_link_down)(void) = 0;
static void (*notify_ppp_link_down)(nsapi_error_t) = 0;
static EventQueue *prepare_event_queue()
{
@ -118,15 +120,18 @@ static u32_t ppp_output(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
len -= ret;
}
tr_debug("> %ld\n", (long) written);
// /tr_debug("> %ld bytes of data written\n", (long) written);
return written;
}
static void ppp_link_status(ppp_pcb *pcb, int err_code, void *ctx)
{
nsapi_error_t mapped_err_code = NSAPI_ERROR_NO_CONNECTION;
switch(err_code) {
case PPPERR_NONE: {
mapped_err_code = NSAPI_ERROR_OK;
tr_info("status_cb: Connected");
#if PPP_IPV4_SUPPORT
tr_debug(" our_ipaddr = %s", ipaddr_ntoa(&ppp_netif(pcb)->ip_addr));
@ -171,10 +176,12 @@ static void ppp_link_status(ppp_pcb *pcb, int err_code, void *ctx)
}
case PPPERR_CONNECT: {
tr_info("status_cb: Connection lost");
mapped_err_code = NSAPI_ERROR_CONNECTION_LOST;
break;
}
case PPPERR_AUTHFAIL: {
tr_info("status_cb: Failed authentication challenge");
mapped_err_code = NSAPI_ERROR_AUTH_FAILURE;
break;
}
case PPPERR_PROTOCOL: {
@ -183,6 +190,7 @@ static void ppp_link_status(ppp_pcb *pcb, int err_code, void *ctx)
}
case PPPERR_PEERDEAD: {
tr_info("status_cb: Connection timeout");
mapped_err_code = NSAPI_ERROR_CONNECTION_TIMEOUT;
break;
}
case PPPERR_IDLETIMEOUT: {
@ -211,13 +219,15 @@ static void ppp_link_status(ppp_pcb *pcb, int err_code, void *ctx)
}
/* If some error happened, we need to properly shutdown the PPP interface */
if (ppp_link_up) {
if (ppp_active) {
ppp_link_up = false;
ppp_active = false;
connect_error_code = mapped_err_code;
sys_sem_signal(&ppp_close_sem);
}
/* Alright, PPP interface is down, we need to notify upper layer */
notify_ppp_link_down();
notify_ppp_link_down(mapped_err_code);
}
#if !PPP_INPROC_IRQ_SAFE
@ -276,11 +286,13 @@ extern "C" err_t ppp_lwip_connect()
#if PPP_AUTH_SUPPORT
ppp_set_auth(my_ppp_pcb, PPPAUTHTYPE_ANY, login, pwd);
#endif //PPP_AUTH_SUPPORT
ppp_active = true;
err_t ret = ppp_connect(my_ppp_pcb, 0);
// lwIP's ppp.txt says input must not be called until after connect
if (ret == ERR_OK) {
my_stream->sigio(callback(stream_cb));
} else {
ppp_active = false;
}
return ret;
}
@ -326,7 +338,12 @@ extern "C" nsapi_error_t ppp_lwip_if_init(struct netif *netif)
return NSAPI_ERROR_OK;
}
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, void (*ppp_link_down_cb)(void), const char *uname, const char *password)
nsapi_error_t nsapi_ppp_error_code()
{
return connect_error_code;
}
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, void (*ppp_link_down_cb)(int), const char *uname, const char *password)
{
if (my_stream) {
return NSAPI_ERROR_PARAMETER;
@ -339,9 +356,14 @@ nsapi_error_t nsapi_ppp_connect(FileHandle *stream, void (*ppp_link_down_cb)(voi
// mustn't start calling input until after connect -
// attach deferred until ppp_lwip_connect, called from mbed_lwip_bringup
return mbed_lwip_bringup(false, true, NULL, NULL, NULL);
}
nsapi_error_t retcode = mbed_lwip_bringup(false, true, NULL, NULL, NULL);
if (retcode != NSAPI_ERROR_OK && connect_error_code != NSAPI_ERROR_OK) {
return connect_error_code;
} else {
return retcode;
}
}
nsapi_error_t nsapi_ppp_disconnect(FileHandle *stream)
{
@ -353,6 +375,31 @@ NetworkStack *nsapi_ppp_get_stack()
return nsapi_create_stack(&lwip_stack);
}
char *nsapi_ppp_get_ip_addr(char *ip_addr, nsapi_size_t buflen)
{
if (mbed_lwip_get_ip_address(ip_addr, buflen)) {
return ip_addr;
}
return NULL;
}
char *nsapi_ppp_get_netmask(char *netmask, nsapi_size_t buflen)
{
if (mbed_lwip_get_netmask(netmask, buflen)) {
return netmask;
}
return NULL;
}
char *nsapi_ppp_get_gw_addr(char *default_gw_addr)
{
if (mbed_lwip_get_gateway(default_gw_addr, sizeof default_gw_addr)) {
return default_gw_addr;
}
return NULL;
}
#endif /* LWIP_PPP_API */
} // namespace mbed

View File

@ -23,6 +23,8 @@
* Common interface that is shared between Cellular interfaces
*/
class CellularInterface: public NetworkInterface {
public:
/** CellularInterface lifetime
*/
//virtual ~CellularInterface() {};
@ -36,14 +38,14 @@ class CellularInterface: public NetworkInterface {
* @param pwd optionally, password
*/
virtual void set_credentials(const char *apn,
const char *uname = 0,
const char *pwd = 0) = 0;
const char *uname = 0,
const char *pwd = 0) = 0;
/** Set the pin code for SIM card
*
* @param sim_pin PIN for the SIM card
*/
virtual void set_SIM_pin(const char *sim_pin) = 0;
virtual void set_sim_pin(const char *sim_pin) = 0;
/** Start the interface
*
@ -73,6 +75,36 @@ class CellularInterface: public NetworkInterface {
* @return 0 on success, or error code on failure
*/
virtual nsapi_error_t disconnect() = 0;
/** Check if the connection is currently established or not
*
* @return true/false If the cellular module have successfully acquired a carrier and is
* connected to an external packet data network using PPP, isConnected()
* API returns true and false otherwise.
*/
virtual bool is_connected() = 0;
/** Get the local IP address
*
* @return Null-terminated representation of the local IP address
* or null if no IP address has been recieved
*/
virtual const char *get_ip_address() = 0;
/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
*/
virtual const char *get_netmask() = 0;
/** Get the local gateways
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
*/
virtual const char *get_gateway() = 0;
};
#endif

View File

@ -24,8 +24,11 @@
namespace mbed {
NetworkStack *nsapi_ppp_get_stack();
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, void(*link_down_cb)(void)=0, const char *uname=0, const char *pwd=0);
nsapi_error_t nsapi_ppp_connect(FileHandle *stream, void(*link_down_cb)(int)=0, const char *uname=0, const char *pwd=0);
nsapi_error_t nsapi_ppp_disconnect(FileHandle *stream);
char *nsapi_ppp_get_ip_addr(char *ip_addr, nsapi_size_t buflen);
char *nsapi_ppp_get_netmask(char *netmask, nsapi_size_t buflen);
char *nsapi_ppp_get_gw_addr(char *gateway_addr, nsapi_size_t buflen);
} //namespace mbed
#endif /* NSAPI_PPP_H_ */

View File

@ -35,22 +35,24 @@ extern "C" {
* @enum nsapi_error_t
*/
enum nsapi_error {
NSAPI_ERROR_OK = 0, /*!< no error */
NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */
NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */
NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */
NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */
NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */
NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */
NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */
NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */
NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */
NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */
NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */
NSAPI_ERROR_OK = 0, /*!< no error */
NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */
NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */
NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */
NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */
NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */
NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */
NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */
NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */
NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */
NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */
NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */
NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */
NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */
};
/** Type used to represent error codes
@ -82,6 +84,8 @@ typedef enum nsapi_security {
NSAPI_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */
NSAPI_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */
NSAPI_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */
NSAPI_SECURITY_PAP = 0x5, /*!< phrase conforms to PPP authentication context */
NSAPI_SECURITY_CHAP = 0x6, /*!< phrase conforms to PPP authentication context */
NSAPI_SECURITY_UNKNOWN = 0xFF, /*!< unknown/unsupported security in scan results */
} nsapi_security_t;

View File

@ -83,7 +83,7 @@ public:
* @param timeout timeout of the connection
* @param debug turns on/off debug output for AT commands
*/
ATParser(FileHandle *fh, const char *output_delimiter="\r", int buffer_size = 256, int timeout = 8000, bool debug = true) :
ATParser(FileHandle *fh, const char *output_delimiter="\r", int buffer_size = 256, int timeout = 8000, bool debug = false) :
_fh(fh),
_buffer_size(buffer_size),
_in_prev(0),