takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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