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