Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
NanostackInterface.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_INTERFACE_H_ 00019 #define NANOSTACK_INTERFACE_H_ 00020 00021 #include "mbed.h" 00022 #include "NetworkStack.h" 00023 #include "MeshInterface.h" 00024 // Include here for backward compatibility 00025 #include "LoWPANNDInterface.h" 00026 #include "ThreadInterface.h" 00027 #include "NanostackEthernetInterface.h" 00028 #include "MeshInterfaceNanostack.h" 00029 00030 struct ns_address; 00031 00032 /** Network interface class for Nanostack */ 00033 class NanostackInterface : public NetworkStack { 00034 public: 00035 static NanostackInterface *get_stack(); 00036 00037 protected: 00038 00039 /** Get the local IP address 00040 * 00041 * @return Null-terminated representation of the local IP address 00042 * or null if not yet connected 00043 */ 00044 virtual const char *get_ip_address(); 00045 00046 /** Opens a socket 00047 * 00048 * Creates a network socket and stores it in the specified handle. 00049 * The handle must be passed to following calls on the socket. 00050 * 00051 * A stack may have a finite number of sockets, in this case 00052 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00053 * 00054 * @param handle Destination for the handle to a newly created socket 00055 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP 00056 * @return 0 on success, negative error code on failure 00057 */ 00058 virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto); 00059 00060 /** Close the socket 00061 * 00062 * Closes any open connection and deallocates any memory associated 00063 * with the socket. 00064 * 00065 * @param handle Socket handle 00066 * @return 0 on success, negative error code on failure 00067 */ 00068 virtual nsapi_error_t socket_close(void *handle); 00069 00070 /** Bind a specific address to a socket 00071 * 00072 * Binding a socket specifies the address and port on which to recieve 00073 * data. If the IP address is zeroed, only the port is bound. 00074 * 00075 * @param handle Socket handle 00076 * @param address Local address to bind 00077 * @return 0 on success, negative error code on failure. 00078 */ 00079 virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address); 00080 00081 /** Listen for connections on a TCP socket 00082 * 00083 * Marks the socket as a passive socket that can be used to accept 00084 * incoming connections. 00085 * 00086 * @param handle Socket handle 00087 * @param backlog Number of pending connections that can be queued 00088 * simultaneously 00089 * @return 0 on success, negative error code on failure 00090 */ 00091 virtual nsapi_error_t socket_listen(void *handle, int backlog); 00092 00093 /** Connects TCP socket to a remote host 00094 * 00095 * Initiates a connection to a remote server specified by the 00096 * indicated address. 00097 * 00098 * @param handle Socket handle 00099 * @param address The SocketAddress of the remote host 00100 * @return 0 on success, negative error code on failure 00101 */ 00102 virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address); 00103 00104 /** Accepts a connection on a TCP socket 00105 * 00106 * The server socket must be bound and set to listen for connections. 00107 * On a new connection, creates a network socket and stores it in the 00108 * specified handle. The handle must be passed to following calls on 00109 * the socket. 00110 * 00111 * A stack may have a finite number of sockets, in this case 00112 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00113 * 00114 * This call is non-blocking. If accept would block, 00115 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00116 * 00117 * @param server Socket handle to server to accept from 00118 * @param handle Destination for a handle to the newly created socket 00119 * @param address Destination for the remote address or NULL 00120 * @return 0 on success, negative error code on failure 00121 */ 00122 virtual nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address); 00123 00124 /** Send data over a TCP socket 00125 * 00126 * The socket must be connected to a remote host. Returns the number of 00127 * bytes sent from the buffer. 00128 * 00129 * This call is non-blocking. If send would block, 00130 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00131 * 00132 * @param handle Socket handle 00133 * @param data Buffer of data to send to the host 00134 * @param size Size of the buffer in bytes 00135 * @return Number of sent bytes on success, negative error 00136 * code on failure 00137 */ 00138 virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size); 00139 00140 /** Receive data over a TCP socket 00141 * 00142 * The socket must be connected to a remote host. Returns the number of 00143 * bytes received into the buffer. 00144 * 00145 * This call is non-blocking. If recv would block, 00146 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00147 * 00148 * @param handle Socket handle 00149 * @param data Destination buffer for data received from the host 00150 * @param size Size of the buffer in bytes 00151 * @return Number of received bytes on success, negative error 00152 * code on failure 00153 */ 00154 virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size); 00155 00156 /** Send a packet over a UDP socket 00157 * 00158 * Sends data to the specified address. Returns the number of bytes 00159 * sent from the buffer. 00160 * 00161 * This call is non-blocking. If sendto would block, 00162 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00163 * 00164 * @param handle Socket handle 00165 * @param address The SocketAddress of the remote host 00166 * @param data Buffer of data to send to the host 00167 * @param size Size of the buffer in bytes 00168 * @return Number of sent bytes on success, negative error 00169 * code on failure 00170 */ 00171 virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size); 00172 00173 /** Receive a packet over a UDP socket 00174 * 00175 * Receives data and stores the source address in address if address 00176 * is not NULL. Returns the number of bytes received into the buffer. 00177 * 00178 * This call is non-blocking. If recvfrom would block, 00179 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00180 * 00181 * @param handle Socket handle 00182 * @param address Destination for the source address or NULL 00183 * @param buffer Destination buffer for data received from the host 00184 * @param size Size of the buffer in bytes 00185 * @return Number of received bytes on success, negative error 00186 * code on failure 00187 */ 00188 virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size); 00189 00190 /** Register a callback on state change of the socket 00191 * 00192 * The specified callback will be called on state changes such as when 00193 * the socket can recv/send/accept successfully and on when an error 00194 * occurs. The callback may also be called spuriously without reason. 00195 * 00196 * The callback may be called in an interrupt context and should not 00197 * perform expensive operations such as recv/send calls. 00198 * 00199 * @param handle Socket handle 00200 * @param callback Function to call on state change 00201 * @param data Argument to pass to callback 00202 */ 00203 virtual void socket_attach(void *handle, void (*callback)(void *), void *data); 00204 00205 /* Set stack-specific socket options 00206 * 00207 * The setsockopt allow an application to pass stack-specific hints 00208 * to the underlying stack. For unsupported options, 00209 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00210 * 00211 * @param handle Socket handle 00212 * @param level Stack-specific protocol level 00213 * @param optname Stack-specific option identifier 00214 * @param optval Option value 00215 * @param optlen Length of the option value 00216 * @return 0 on success, negative error code on failure 00217 */ 00218 virtual nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen); 00219 00220 /* Get stack-specific socket options 00221 * 00222 * The getstackopt allow an application to retrieve stack-specific hints 00223 * from the underlying stack. For unsupported options, 00224 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00225 * 00226 * @param handle Socket handle 00227 * @param level Stack-specific protocol level 00228 * @param optname Stack-specific option identifier 00229 * @param optval Destination for option value 00230 * @param optlen Length of the option value 00231 * @return 0 on success, negative error code on failure 00232 */ 00233 virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen); 00234 00235 private: 00236 nsapi_size_or_error_t do_sendto(void *handle, const struct ns_address *address, const void *data, nsapi_size_t size); 00237 char text_ip_address[40]; 00238 static NanostackInterface * _ns_interface; 00239 }; 00240 00241 nsapi_error_t map_mesh_error(mesh_error_t err); 00242 00243 #endif /* NANOSTACK_INTERFACE_H_ */
Generated on Tue Jul 12 2022 14:24:28 by
