ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NanostackInterface.h Source File

NanostackInterface.h

00001 /*
00002  * Copyright (c) 2016 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef NANOSTACK_INTERFACE_H_
00018 #define NANOSTACK_INTERFACE_H_
00019 
00020 #include "mbed.h"
00021 #include "NetworkStack.h"
00022 #include "MeshInterface.h"
00023 // Include here for backward compatibility
00024 #include "LoWPANNDInterface.h"
00025 #include "ThreadInterface.h"
00026 #include "NanostackEthernetInterface.h"
00027 #include "MeshInterfaceNanostack.h"
00028 
00029 class NanostackInterface : public NetworkStack {
00030 public:
00031     static NanostackInterface *get_stack();
00032 
00033 protected:
00034 
00035     /** Get the local IP address
00036      *
00037      *  @return         Null-terminated representation of the local IP address
00038      *                  or null if not yet connected
00039      */
00040     virtual const char *get_ip_address();
00041 
00042     /** Opens a socket
00043      *
00044      *  Creates a network socket and stores it in the specified handle.
00045      *  The handle must be passed to following calls on the socket.
00046      *
00047      *  A stack may have a finite number of sockets, in this case
00048      *  NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
00049      *
00050      *  @param handle   Destination for the handle to a newly created socket
00051      *  @param proto    Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
00052      *  @return         0 on success, negative error code on failure
00053      */
00054     virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto);
00055 
00056     /** Close the socket
00057      *
00058      *  Closes any open connection and deallocates any memory associated
00059      *  with the socket.
00060      *
00061      *  @param handle   Socket handle
00062      *  @return         0 on success, negative error code on failure
00063      */
00064     virtual nsapi_error_t socket_close(void *handle);
00065 
00066     /** Bind a specific address to a socket
00067      *
00068      *  Binding a socket specifies the address and port on which to recieve
00069      *  data. If the IP address is zeroed, only the port is bound.
00070      *
00071      *  @param handle   Socket handle
00072      *  @param address  Local address to bind
00073      *  @return         0 on success, negative error code on failure.
00074      */
00075     virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address);
00076 
00077     /** Listen for connections on a TCP socket
00078      *
00079      *  Marks the socket as a passive socket that can be used to accept
00080      *  incoming connections.
00081      *
00082      *  @param handle   Socket handle
00083      *  @param backlog  Number of pending connections that can be queued
00084      *                  simultaneously
00085      *  @return         0 on success, negative error code on failure
00086      */
00087     virtual nsapi_error_t socket_listen(void *handle, int backlog);
00088 
00089     /** Connects TCP socket to a remote host
00090      *
00091      *  Initiates a connection to a remote server specified by the
00092      *  indicated address.
00093      *
00094      *  @param handle   Socket handle
00095      *  @param address  The SocketAddress of the remote host
00096      *  @return         0 on success, negative error code on failure
00097      */
00098     virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address);
00099 
00100     /** Accepts a connection on a TCP socket
00101      *
00102      *  The server socket must be bound and set to listen for connections.
00103      *  On a new connection, creates a network socket and stores it in the
00104      *  specified handle. The handle must be passed to following calls on
00105      *  the socket.
00106      *
00107      *  A stack may have a finite number of sockets, in this case
00108      *  NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
00109      *
00110      *  This call is non-blocking. If accept would block,
00111      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00112      *
00113      *  @param server   Socket handle to server to accept from
00114      *  @param handle   Destination for a handle to the newly created socket
00115      *  @param address  Destination for the remote address or NULL
00116      *  @return         0 on success, negative error code on failure
00117      */
00118     virtual nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address);
00119 
00120     /** Send data over a TCP socket
00121      *
00122      *  The socket must be connected to a remote host. Returns the number of
00123      *  bytes sent from the buffer.
00124      *
00125      *  This call is non-blocking. If send would block,
00126      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00127      *
00128      *  @param handle   Socket handle
00129      *  @param data     Buffer of data to send to the host
00130      *  @param size     Size of the buffer in bytes
00131      *  @return         Number of sent bytes on success, negative error
00132      *                  code on failure
00133      */
00134     virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size);
00135 
00136     /** Receive data over a TCP socket
00137      *
00138      *  The socket must be connected to a remote host. Returns the number of
00139      *  bytes received into the buffer.
00140      *
00141      *  This call is non-blocking. If recv would block,
00142      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00143      *
00144      *  @param handle   Socket handle
00145      *  @param data     Destination buffer for data received from the host
00146      *  @param size     Size of the buffer in bytes
00147      *  @return         Number of received bytes on success, negative error
00148      *                  code on failure
00149      */
00150     virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size);
00151 
00152     /** Send a packet over a UDP socket
00153      *
00154      *  Sends data to the specified address. Returns the number of bytes
00155      *  sent from the buffer.
00156      *
00157      *  This call is non-blocking. If sendto would block,
00158      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00159      *
00160      *  @param handle   Socket handle
00161      *  @param address  The SocketAddress of the remote host
00162      *  @param data     Buffer of data to send to the host
00163      *  @param size     Size of the buffer in bytes
00164      *  @return         Number of sent bytes on success, negative error
00165      *                  code on failure
00166      */
00167     virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size);
00168 
00169     /** Receive a packet over a UDP socket
00170      *
00171      *  Receives data and stores the source address in address if address
00172      *  is not NULL. Returns the number of bytes received into the buffer.
00173      *
00174      *  This call is non-blocking. If recvfrom would block,
00175      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00176      *
00177      *  @param handle   Socket handle
00178      *  @param address  Destination for the source address or NULL
00179      *  @param data     Destination buffer for data received from the host
00180      *  @param size     Size of the buffer in bytes
00181      *  @return         Number of received bytes on success, negative error
00182      *                  code on failure
00183      */
00184     virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size);
00185 
00186     /** Register a callback on state change of the socket
00187      *
00188      *  The specified callback will be called on state changes such as when
00189      *  the socket can recv/send/accept successfully and on when an error
00190      *  occurs. The callback may also be called spuriously without reason.
00191      *
00192      *  The callback may be called in an interrupt context and should not
00193      *  perform expensive operations such as recv/send calls.
00194      *
00195      *  @param handle   Socket handle
00196      *  @param callback Function to call on state change
00197      *  @param data     Argument to pass to callback
00198      */
00199     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00200 
00201     /*  Set stack-specific socket options
00202      *
00203      *  The setsockopt allow an application to pass stack-specific hints
00204      *  to the underlying stack. For unsupported options,
00205      *  NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
00206      *
00207      *  @param handle   Socket handle
00208      *  @param level    Stack-specific protocol level
00209      *  @param optname  Stack-specific option identifier
00210      *  @param optval   Option value
00211      *  @param optlen   Length of the option value
00212      *  @return         0 on success, negative error code on failure
00213      */
00214     virtual nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen);
00215 
00216     /*  Get stack-specific socket options
00217      *
00218      *  The getstackopt allow an application to retrieve stack-specific hints
00219      *  from the underlying stack. For unsupported options,
00220      *  NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
00221      *
00222      *  @param handle   Socket handle
00223      *  @param level    Stack-specific protocol level
00224      *  @param optname  Stack-specific option identifier
00225      *  @param optval   Destination for option value
00226      *  @param optlen   Length of the option value
00227      *  @return         0 on success, negative error code on failure
00228      */
00229     virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen);
00230 
00231 private:
00232     static NanostackInterface * _ns_interface;
00233 };
00234 
00235 nsapi_error_t map_mesh_error(mesh_error_t err);
00236 
00237 #endif /* NANOSTACK_INTERFACE_H_ */