Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Nanostack.h Source File

Nanostack.h

00001 /*
00002  * Copyright (c) 2016-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef NANOSTACK_H_
00019 #define NANOSTACK_H_
00020 
00021 #include "OnboardNetworkStack.h"
00022 #include "NanostackMemoryManager.h"
00023 #include "MeshInterface.h"
00024 #include "mesh_interface_types.h"
00025 #include "eventOS_event.h"
00026 
00027 struct ns_address;
00028 
00029 class Nanostack : public OnboardNetworkStack, private mbed::NonCopyable<Nanostack> {
00030 public:
00031     static Nanostack &get_instance();
00032 
00033     // Our Nanostack::Interface etc are defined by mbed_mesh_api
00034     class Interface;
00035     class EthernetInterface;
00036     class MeshInterface;
00037     class LoWPANNDInterface;
00038     class ThreadInterface;
00039     class WisunInterface;
00040     class PPPInterface;
00041 
00042     /* Implement OnboardNetworkStack method */
00043     virtual nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out);
00044 
00045     /* Local variant with stronger typing and manual address specification */
00046     nsapi_error_t add_ethernet_interface(EMAC &emac, bool default_if, Nanostack::EthernetInterface **interface_out, const uint8_t *mac_addr = NULL);
00047 
00048     virtual nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out);
00049 
00050     /* Local variant with stronger typing and manual address specification */
00051     nsapi_error_t add_ppp_interface(PPP &ppp, bool default_if, Nanostack::PPPInterface **interface_out);
00052 
00053     nsapi_error_t remove_ppp_interface(OnboardNetworkStack::Interface **interface_out);
00054 
00055 protected:
00056 
00057     Nanostack();
00058 
00059     /** Get the local IP address
00060      *
00061      *  @return         Null-terminated representation of the local IP address
00062      *                  or null if not yet connected
00063      */
00064     virtual const char *get_ip_address();
00065 
00066     /** Opens a socket
00067      *
00068      *  Creates a network socket and stores it in the specified handle.
00069      *  The handle must be passed to following calls on the socket.
00070      *
00071      *  A stack may have a finite number of sockets, in this case
00072      *  NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
00073      *
00074      *  @param handle   Destination for the handle to a newly created socket
00075      *  @param proto    Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
00076      *  @return         0 on success, negative error code on failure
00077      */
00078     virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto);
00079 
00080     /** Close the socket
00081      *
00082      *  Closes any open connection and deallocates any memory associated
00083      *  with the socket.
00084      *
00085      *  @param handle   Socket handle
00086      *  @return         0 on success, negative error code on failure
00087      */
00088     virtual nsapi_error_t socket_close(void *handle);
00089 
00090     /** Bind a specific address to a socket
00091      *
00092      *  Binding a socket specifies the address and port on which to recieve
00093      *  data. If the IP address is zeroed, only the port is bound.
00094      *
00095      *  @param handle   Socket handle
00096      *  @param address  Local address to bind
00097      *  @return         0 on success, negative error code on failure.
00098      */
00099     virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address);
00100 
00101     /** Listen for connections on a TCP socket
00102      *
00103      *  Marks the socket as a passive socket that can be used to accept
00104      *  incoming connections.
00105      *
00106      *  @param handle   Socket handle
00107      *  @param backlog  Number of pending connections that can be queued
00108      *                  simultaneously
00109      *  @return         0 on success, negative error code on failure
00110      */
00111     virtual nsapi_error_t socket_listen(void *handle, int backlog);
00112 
00113     /** Connects TCP socket to a remote host
00114      *
00115      *  Initiates a connection to a remote server specified by the
00116      *  indicated address.
00117      *
00118      *  @param handle   Socket handle
00119      *  @param address  The SocketAddress of the remote host
00120      *  @return         0 on success, negative error code on failure
00121      */
00122     virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address);
00123 
00124     /** Accepts a connection on a TCP socket
00125      *
00126      *  The server socket must be bound and set to listen for connections.
00127      *  On a new connection, creates a network socket and stores it in the
00128      *  specified handle. The handle must be passed to following calls on
00129      *  the socket.
00130      *
00131      *  A stack may have a finite number of sockets, in this case
00132      *  NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
00133      *
00134      *  This call is non-blocking. If accept would block,
00135      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00136      *
00137      *  @param server   Socket handle to server to accept from
00138      *  @param handle   Destination for a handle to the newly created socket
00139      *  @param address  Destination for the remote address or NULL
00140      *  @return         0 on success, negative error code on failure
00141      */
00142     virtual nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address);
00143 
00144     /** Send data over a TCP socket
00145      *
00146      *  The socket must be connected to a remote host. Returns the number of
00147      *  bytes sent from the buffer.
00148      *
00149      *  This call is non-blocking. If send would block,
00150      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00151      *
00152      *  @param handle   Socket handle
00153      *  @param data     Buffer of data to send to the host
00154      *  @param size     Size of the buffer in bytes
00155      *  @return         Number of sent bytes on success, negative error
00156      *                  code on failure
00157      */
00158     virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size);
00159 
00160     /** Receive data over a TCP socket
00161      *
00162      *  The socket must be connected to a remote host. Returns the number of
00163      *  bytes received into the buffer.
00164      *
00165      *  This call is non-blocking. If recv would block,
00166      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00167      *
00168      *  @param handle   Socket handle
00169      *  @param data     Destination buffer for data received from the host
00170      *  @param size     Size of the buffer in bytes
00171      *  @return         Number of received bytes on success, negative error
00172      *                  code on failure
00173      */
00174     virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size);
00175 
00176     /** Send a packet over a UDP socket
00177      *
00178      *  Sends data to the specified address. Returns the number of bytes
00179      *  sent from the buffer.
00180      *
00181      *  This call is non-blocking. If sendto would block,
00182      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00183      *
00184      *  @param handle   Socket handle
00185      *  @param address  The SocketAddress of the remote host
00186      *  @param data     Buffer of data to send to the host
00187      *  @param size     Size of the buffer in bytes
00188      *  @return         Number of sent bytes on success, negative error
00189      *                  code on failure
00190      */
00191     virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size);
00192 
00193     /** Receive a packet over a UDP socket
00194      *
00195      *  Receives data and stores the source address in address if address
00196      *  is not NULL. Returns the number of bytes received into the buffer.
00197      *
00198      *  This call is non-blocking. If recvfrom would block,
00199      *  NSAPI_ERROR_WOULD_BLOCK is returned immediately.
00200      *
00201      *  @param handle   Socket handle
00202      *  @param address  Destination for the source address or NULL
00203      *  @param buffer   Destination buffer for data received from the host
00204      *  @param size     Size of the buffer in bytes
00205      *  @return         Number of received bytes on success, negative error
00206      *                  code on failure
00207      */
00208     virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size);
00209 
00210     /** Register a callback on state change of the socket
00211      *
00212      *  The specified callback will be called on state changes such as when
00213      *  the socket can recv/send/accept successfully and on when an error
00214      *  occurs. The callback may also be called spuriously without reason.
00215      *
00216      *  The callback may be called in an interrupt context and should not
00217      *  perform expensive operations such as recv/send calls.
00218      *
00219      *  @param handle   Socket handle
00220      *  @param callback Function to call on state change
00221      *  @param data     Argument to pass to callback
00222      */
00223     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00224 
00225     /*  Set stack-specific socket options
00226      *
00227      *  The setsockopt allow an application to pass stack-specific hints
00228      *  to the underlying stack. For unsupported options,
00229      *  NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
00230      *
00231      *  @param handle   Socket handle
00232      *  @param level    Stack-specific protocol level
00233      *  @param optname  Stack-specific option identifier
00234      *  @param optval   Option value
00235      *  @param optlen   Length of the option value
00236      *  @return         0 on success, negative error code on failure
00237      */
00238     virtual nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen);
00239 
00240     /*  Get stack-specific socket options
00241      *
00242      *  The getstackopt allow an application to retrieve stack-specific hints
00243      *  from the underlying stack. For unsupported options,
00244      *  NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
00245      *
00246      *  @param handle   Socket handle
00247      *  @param level    Stack-specific protocol level
00248      *  @param optname  Stack-specific option identifier
00249      *  @param optval   Destination for option value
00250      *  @param optlen   Length of the option value
00251      *  @return         0 on success, negative error code on failure
00252      */
00253     virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen);
00254 
00255 private:
00256 
00257     /** Call in callback
00258       *
00259       *  Callback is used to call the call in method of the network stack.
00260       */
00261     typedef mbed::Callback<nsapi_error_t (int delay_ms, mbed::Callback<void()> user_cb)> call_in_callback_cb_t;
00262 
00263     /** Get a call in callback
00264      *
00265      *  Get a call in callback from the network stack context.
00266      *
00267      *  Callback should not take more than 10ms to execute, otherwise it might
00268      *  prevent underlying thread processing. A portable user of the callback
00269      *  should not make calls to network operations due to stack size limitations.
00270      *  The callback should not perform expensive operations such as socket recv/send
00271      *  calls or blocking operations.
00272      *
00273      *  @return         Call in callback
00274      */
00275     virtual call_in_callback_cb_t get_call_in_callback();
00276 
00277     /** Call a callback after a delay
00278      *
00279      *  Call a callback from the network stack context after a delay. If function
00280      *  returns error callback will not be called.
00281      *
00282      *  @param delay    Delay in milliseconds
00283      *  @param func     Callback to be called
00284      *  @return         0 on success, negative error code on failure
00285      */
00286     virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
00287 
00288     struct nanostack_callback {
00289         mbed::Callback<void()> callback;
00290     };
00291 
00292     nsapi_size_or_error_t do_sendto(void *handle, const struct ns_address *address, const void *data, nsapi_size_t size);
00293     static void call_event_tasklet_main(arm_event_s *event);
00294     char text_ip_address[40];
00295     NanostackMemoryManager memory_manager;
00296     int8_t call_event_tasklet;
00297 };
00298 
00299 nsapi_error_t map_mesh_error(mesh_error_t err);
00300 
00301 #endif /* NANOSTACK_H_ */