mirror of https://github.com/ARMmbed/mbed-os.git
324 lines
11 KiB
C++
324 lines
11 KiB
C++
/*
|
|
* Copyright (c) 2016 ARM Limited. All rights reserved.
|
|
*/
|
|
|
|
#ifndef NANOSTACK_INTERFACE_H_
|
|
#define NANOSTACK_INTERFACE_H_
|
|
|
|
#include "NetworkStack.h"
|
|
#include "MeshInterface.h"
|
|
#include "NanostackRfPhy.h"
|
|
|
|
#include "mbed-mesh-api/Mesh6LoWPAN_ND.h"
|
|
#include "mbed-mesh-api/MeshThread.h"
|
|
|
|
class NanostackInterface : public NetworkStack {
|
|
public:
|
|
static NanostackInterface *get_stack();
|
|
|
|
protected:
|
|
|
|
/** Get the local IP address
|
|
*
|
|
* @return Null-terminated representation of the local IP address
|
|
* or null if not yet connected
|
|
*/
|
|
virtual const char *get_ip_address();
|
|
|
|
/** Opens a socket
|
|
*
|
|
* Creates a network socket and stores it in the specified handle.
|
|
* The handle must be passed to following calls on the socket.
|
|
*
|
|
* A stack may have a finite number of sockets, in this case
|
|
* NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
|
|
*
|
|
* @param handle Destination for the handle to a newly created socket
|
|
* @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
|
|
* @return 0 on success, negative error code on failure
|
|
*/
|
|
virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto);
|
|
|
|
/** Close the socket
|
|
*
|
|
* Closes any open connection and deallocates any memory associated
|
|
* with the socket.
|
|
*
|
|
* @param handle Socket handle
|
|
* @return 0 on success, negative error code on failure
|
|
*/
|
|
virtual nsapi_error_t socket_close(void *handle);
|
|
|
|
/** Bind a specific address to a socket
|
|
*
|
|
* Binding a socket specifies the address and port on which to recieve
|
|
* data. If the IP address is zeroed, only the port is bound.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param address Local address to bind
|
|
* @return 0 on success, negative error code on failure.
|
|
*/
|
|
virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address);
|
|
|
|
/** Listen for connections on a TCP socket
|
|
*
|
|
* Marks the socket as a passive socket that can be used to accept
|
|
* incoming connections.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param backlog Number of pending connections that can be queued
|
|
* simultaneously
|
|
* @return 0 on success, negative error code on failure
|
|
*/
|
|
virtual nsapi_error_t socket_listen(void *handle, int backlog);
|
|
|
|
/** Connects TCP socket to a remote host
|
|
*
|
|
* Initiates a connection to a remote server specified by the
|
|
* indicated address.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param address The SocketAddress of the remote host
|
|
* @return 0 on success, negative error code on failure
|
|
*/
|
|
virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address);
|
|
|
|
/** Accepts a connection on a TCP socket
|
|
*
|
|
* The server socket must be bound and set to listen for connections.
|
|
* On a new connection, creates a network socket and stores it in the
|
|
* specified handle. The handle must be passed to following calls on
|
|
* the socket.
|
|
*
|
|
* A stack may have a finite number of sockets, in this case
|
|
* NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
|
|
*
|
|
* This call is non-blocking. If accept would block,
|
|
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
|
|
*
|
|
* @param server Socket handle to server to accept from
|
|
* @param handle Destination for a handle to the newly created socket
|
|
* @param address Destination for the remote address or NULL
|
|
* @return 0 on success, negative error code on failure
|
|
*/
|
|
virtual nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address);
|
|
|
|
/** Send data over a TCP socket
|
|
*
|
|
* The socket must be connected to a remote host. Returns the number of
|
|
* bytes sent from the buffer.
|
|
*
|
|
* This call is non-blocking. If send would block,
|
|
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param data Buffer of data to send to the host
|
|
* @param size Size of the buffer in bytes
|
|
* @return Number of sent bytes on success, negative error
|
|
* code on failure
|
|
*/
|
|
virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size);
|
|
|
|
/** Receive data over a TCP socket
|
|
*
|
|
* The socket must be connected to a remote host. Returns the number of
|
|
* bytes received into the buffer.
|
|
*
|
|
* This call is non-blocking. If recv would block,
|
|
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param data Destination buffer for data received from the host
|
|
* @param size Size of the buffer in bytes
|
|
* @return Number of received bytes on success, negative error
|
|
* code on failure
|
|
*/
|
|
virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size);
|
|
|
|
/** Send a packet over a UDP socket
|
|
*
|
|
* Sends data to the specified address. Returns the number of bytes
|
|
* sent from the buffer.
|
|
*
|
|
* This call is non-blocking. If sendto would block,
|
|
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param address The SocketAddress of the remote host
|
|
* @param data Buffer of data to send to the host
|
|
* @param size Size of the buffer in bytes
|
|
* @return Number of sent bytes on success, negative error
|
|
* code on failure
|
|
*/
|
|
virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size);
|
|
|
|
/** Receive a packet over a UDP socket
|
|
*
|
|
* Receives data and stores the source address in address if address
|
|
* is not NULL. Returns the number of bytes received into the buffer.
|
|
*
|
|
* This call is non-blocking. If recvfrom would block,
|
|
* NSAPI_ERROR_WOULD_BLOCK is returned immediately.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param address Destination for the source address or NULL
|
|
* @param data Destination buffer for data received from the host
|
|
* @param size Size of the buffer in bytes
|
|
* @return Number of received bytes on success, negative error
|
|
* code on failure
|
|
*/
|
|
virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size);
|
|
|
|
/** Register a callback on state change of the socket
|
|
*
|
|
* The specified callback will be called on state changes such as when
|
|
* the socket can recv/send/accept successfully and on when an error
|
|
* occurs. The callback may also be called spuriously without reason.
|
|
*
|
|
* The callback may be called in an interrupt context and should not
|
|
* perform expensive operations such as recv/send calls.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param callback Function to call on state change
|
|
* @param data Argument to pass to callback
|
|
*/
|
|
virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
|
|
|
|
/* Set stack-specific socket options
|
|
*
|
|
* The setsockopt allow an application to pass stack-specific hints
|
|
* to the underlying stack. For unsupported options,
|
|
* NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param level Stack-specific protocol level
|
|
* @param optname Stack-specific option identifier
|
|
* @param optval Option value
|
|
* @param optlen Length of the option value
|
|
* @return 0 on success, negative error code on failure
|
|
*/
|
|
virtual nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen);
|
|
|
|
/* Get stack-specific socket options
|
|
*
|
|
* The getstackopt allow an application to retrieve stack-specific hints
|
|
* from the underlying stack. For unsupported options,
|
|
* NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
|
|
*
|
|
* @param handle Socket handle
|
|
* @param level Stack-specific protocol level
|
|
* @param optname Stack-specific option identifier
|
|
* @param optval Destination for option value
|
|
* @param optlen Length of the option value
|
|
* @return 0 on success, negative error code on failure
|
|
*/
|
|
virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen);
|
|
|
|
private:
|
|
static NanostackInterface * _ns_interface;
|
|
};
|
|
|
|
class MeshInterfaceNanostack : public MeshInterface {
|
|
public:
|
|
|
|
/** Attach phy and initialize the mesh
|
|
*
|
|
* Initializes a mesh interface on the given phy. Not needed if
|
|
* the phy is passed to the mesh's constructor.
|
|
*
|
|
* @return 0 on success, negative on failure
|
|
*/
|
|
virtual nsapi_error_t initialize(NanostackRfPhy *phy);
|
|
|
|
/** Start the interface
|
|
*
|
|
* @return 0 on success, negative on failure
|
|
*/
|
|
virtual nsapi_error_t connect() = 0;
|
|
|
|
/** Stop the interface
|
|
*
|
|
* @return 0 on success, negative on failure
|
|
*/
|
|
virtual nsapi_error_t disconnect();
|
|
|
|
/** Get the internally stored IP address
|
|
/return IP address of the interface or null if not yet connected
|
|
*/
|
|
virtual const char *get_ip_address();
|
|
|
|
/** Get the internally stored MAC address
|
|
/return MAC address of the interface
|
|
*/
|
|
virtual const char *get_mac_address();
|
|
|
|
protected:
|
|
MeshInterfaceNanostack();
|
|
MeshInterfaceNanostack(NanostackRfPhy *phy);
|
|
nsapi_error_t register_rf();
|
|
nsapi_error_t actual_connect();
|
|
virtual NetworkStack * get_stack(void);
|
|
|
|
void mesh_network_handler(mesh_connection_status_t status);
|
|
NanostackRfPhy *phy;
|
|
AbstractMesh *mesh_api;
|
|
int8_t rf_device_id;
|
|
uint8_t eui64[8];
|
|
char ip_addr_str[40];
|
|
char mac_addr_str[24];
|
|
Semaphore connect_semaphore;
|
|
};
|
|
|
|
class LoWPANNDInterface : public MeshInterfaceNanostack {
|
|
public:
|
|
|
|
/** Create an uninitialized LoWPANNDInterface
|
|
*
|
|
* Must initialize to initialize the mesh on a phy.
|
|
*/
|
|
LoWPANNDInterface() : MeshInterfaceNanostack() {
|
|
|
|
}
|
|
|
|
/** Create an initialized MeshInterface
|
|
*
|
|
*/
|
|
LoWPANNDInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy) {
|
|
|
|
}
|
|
|
|
nsapi_error_t connect();
|
|
protected:
|
|
Mesh6LoWPAN_ND *get_mesh_api() const { return static_cast<Mesh6LoWPAN_ND *>(mesh_api); }
|
|
private:
|
|
|
|
};
|
|
|
|
class ThreadInterface : public MeshInterfaceNanostack {
|
|
public:
|
|
|
|
/** Create an uninitialized LoWPANNDInterface
|
|
*
|
|
* Must initialize to initialize the mesh on a phy.
|
|
*/
|
|
ThreadInterface() : MeshInterfaceNanostack() {
|
|
|
|
}
|
|
|
|
/** Create an initialized MeshInterface
|
|
*
|
|
*/
|
|
ThreadInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy) {
|
|
|
|
}
|
|
|
|
nsapi_error_t connect();
|
|
protected:
|
|
MeshThread *get_mesh_api() const { return static_cast<MeshThread *>(mesh_api); }
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
#endif /* NANOSTACK_INTERFACE_H_ */
|