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