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.
mbed-os/features/nanostack/nanostack-interface/NanostackInterface.h@0:6ad07c9019fd, 2018-05-30 (annotated)
- Committer:
- Bethory
- Date:
- Wed May 30 00:01:50 2018 +0000
- Revision:
- 0:6ad07c9019fd
Codigo de tales para todos los pasculaes
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| Bethory | 0:6ad07c9019fd | 1 | /* | 
| Bethory | 0:6ad07c9019fd | 2 | * Copyright (c) 2016-2017, Arm Limited and affiliates. | 
| Bethory | 0:6ad07c9019fd | 3 | * SPDX-License-Identifier: Apache-2.0 | 
| Bethory | 0:6ad07c9019fd | 4 | * | 
| Bethory | 0:6ad07c9019fd | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| Bethory | 0:6ad07c9019fd | 6 | * you may not use this file except in compliance with the License. | 
| Bethory | 0:6ad07c9019fd | 7 | * You may obtain a copy of the License at | 
| Bethory | 0:6ad07c9019fd | 8 | * | 
| Bethory | 0:6ad07c9019fd | 9 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| Bethory | 0:6ad07c9019fd | 10 | * | 
| Bethory | 0:6ad07c9019fd | 11 | * Unless required by applicable law or agreed to in writing, software | 
| Bethory | 0:6ad07c9019fd | 12 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| Bethory | 0:6ad07c9019fd | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| Bethory | 0:6ad07c9019fd | 14 | * See the License for the specific language governing permissions and | 
| Bethory | 0:6ad07c9019fd | 15 | * limitations under the License. | 
| Bethory | 0:6ad07c9019fd | 16 | */ | 
| Bethory | 0:6ad07c9019fd | 17 | |
| Bethory | 0:6ad07c9019fd | 18 | #ifndef NANOSTACK_INTERFACE_H_ | 
| Bethory | 0:6ad07c9019fd | 19 | #define NANOSTACK_INTERFACE_H_ | 
| Bethory | 0:6ad07c9019fd | 20 | |
| Bethory | 0:6ad07c9019fd | 21 | #include "mbed.h" | 
| Bethory | 0:6ad07c9019fd | 22 | #include "NetworkStack.h" | 
| Bethory | 0:6ad07c9019fd | 23 | #include "MeshInterface.h" | 
| Bethory | 0:6ad07c9019fd | 24 | // Include here for backward compatibility | 
| Bethory | 0:6ad07c9019fd | 25 | #include "LoWPANNDInterface.h" | 
| Bethory | 0:6ad07c9019fd | 26 | #include "ThreadInterface.h" | 
| Bethory | 0:6ad07c9019fd | 27 | #include "NanostackEthernetInterface.h" | 
| Bethory | 0:6ad07c9019fd | 28 | #include "MeshInterfaceNanostack.h" | 
| Bethory | 0:6ad07c9019fd | 29 | |
| Bethory | 0:6ad07c9019fd | 30 | struct ns_address; | 
| Bethory | 0:6ad07c9019fd | 31 | |
| Bethory | 0:6ad07c9019fd | 32 | /** Network interface class for Nanostack */ | 
| Bethory | 0:6ad07c9019fd | 33 | class NanostackInterface : public NetworkStack { | 
| Bethory | 0:6ad07c9019fd | 34 | public: | 
| Bethory | 0:6ad07c9019fd | 35 | static NanostackInterface *get_stack(); | 
| Bethory | 0:6ad07c9019fd | 36 | |
| Bethory | 0:6ad07c9019fd | 37 | protected: | 
| Bethory | 0:6ad07c9019fd | 38 | |
| Bethory | 0:6ad07c9019fd | 39 | /** Get the local IP address | 
| Bethory | 0:6ad07c9019fd | 40 | * | 
| Bethory | 0:6ad07c9019fd | 41 | * @return Null-terminated representation of the local IP address | 
| Bethory | 0:6ad07c9019fd | 42 | * or null if not yet connected | 
| Bethory | 0:6ad07c9019fd | 43 | */ | 
| Bethory | 0:6ad07c9019fd | 44 | virtual const char *get_ip_address(); | 
| Bethory | 0:6ad07c9019fd | 45 | |
| Bethory | 0:6ad07c9019fd | 46 | /** Opens a socket | 
| Bethory | 0:6ad07c9019fd | 47 | * | 
| Bethory | 0:6ad07c9019fd | 48 | * Creates a network socket and stores it in the specified handle. | 
| Bethory | 0:6ad07c9019fd | 49 | * The handle must be passed to following calls on the socket. | 
| Bethory | 0:6ad07c9019fd | 50 | * | 
| Bethory | 0:6ad07c9019fd | 51 | * A stack may have a finite number of sockets, in this case | 
| Bethory | 0:6ad07c9019fd | 52 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. | 
| Bethory | 0:6ad07c9019fd | 53 | * | 
| Bethory | 0:6ad07c9019fd | 54 | * @param handle Destination for the handle to a newly created socket | 
| Bethory | 0:6ad07c9019fd | 55 | * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP | 
| Bethory | 0:6ad07c9019fd | 56 | * @return 0 on success, negative error code on failure | 
| Bethory | 0:6ad07c9019fd | 57 | */ | 
| Bethory | 0:6ad07c9019fd | 58 | virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto); | 
| Bethory | 0:6ad07c9019fd | 59 | |
| Bethory | 0:6ad07c9019fd | 60 | /** Close the socket | 
| Bethory | 0:6ad07c9019fd | 61 | * | 
| Bethory | 0:6ad07c9019fd | 62 | * Closes any open connection and deallocates any memory associated | 
| Bethory | 0:6ad07c9019fd | 63 | * with the socket. | 
| Bethory | 0:6ad07c9019fd | 64 | * | 
| Bethory | 0:6ad07c9019fd | 65 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 66 | * @return 0 on success, negative error code on failure | 
| Bethory | 0:6ad07c9019fd | 67 | */ | 
| Bethory | 0:6ad07c9019fd | 68 | virtual nsapi_error_t socket_close(void *handle); | 
| Bethory | 0:6ad07c9019fd | 69 | |
| Bethory | 0:6ad07c9019fd | 70 | /** Bind a specific address to a socket | 
| Bethory | 0:6ad07c9019fd | 71 | * | 
| Bethory | 0:6ad07c9019fd | 72 | * Binding a socket specifies the address and port on which to recieve | 
| Bethory | 0:6ad07c9019fd | 73 | * data. If the IP address is zeroed, only the port is bound. | 
| Bethory | 0:6ad07c9019fd | 74 | * | 
| Bethory | 0:6ad07c9019fd | 75 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 76 | * @param address Local address to bind | 
| Bethory | 0:6ad07c9019fd | 77 | * @return 0 on success, negative error code on failure. | 
| Bethory | 0:6ad07c9019fd | 78 | */ | 
| Bethory | 0:6ad07c9019fd | 79 | virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address); | 
| Bethory | 0:6ad07c9019fd | 80 | |
| Bethory | 0:6ad07c9019fd | 81 | /** Listen for connections on a TCP socket | 
| Bethory | 0:6ad07c9019fd | 82 | * | 
| Bethory | 0:6ad07c9019fd | 83 | * Marks the socket as a passive socket that can be used to accept | 
| Bethory | 0:6ad07c9019fd | 84 | * incoming connections. | 
| Bethory | 0:6ad07c9019fd | 85 | * | 
| Bethory | 0:6ad07c9019fd | 86 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 87 | * @param backlog Number of pending connections that can be queued | 
| Bethory | 0:6ad07c9019fd | 88 | * simultaneously | 
| Bethory | 0:6ad07c9019fd | 89 | * @return 0 on success, negative error code on failure | 
| Bethory | 0:6ad07c9019fd | 90 | */ | 
| Bethory | 0:6ad07c9019fd | 91 | virtual nsapi_error_t socket_listen(void *handle, int backlog); | 
| Bethory | 0:6ad07c9019fd | 92 | |
| Bethory | 0:6ad07c9019fd | 93 | /** Connects TCP socket to a remote host | 
| Bethory | 0:6ad07c9019fd | 94 | * | 
| Bethory | 0:6ad07c9019fd | 95 | * Initiates a connection to a remote server specified by the | 
| Bethory | 0:6ad07c9019fd | 96 | * indicated address. | 
| Bethory | 0:6ad07c9019fd | 97 | * | 
| Bethory | 0:6ad07c9019fd | 98 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 99 | * @param address The SocketAddress of the remote host | 
| Bethory | 0:6ad07c9019fd | 100 | * @return 0 on success, negative error code on failure | 
| Bethory | 0:6ad07c9019fd | 101 | */ | 
| Bethory | 0:6ad07c9019fd | 102 | virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address); | 
| Bethory | 0:6ad07c9019fd | 103 | |
| Bethory | 0:6ad07c9019fd | 104 | /** Accepts a connection on a TCP socket | 
| Bethory | 0:6ad07c9019fd | 105 | * | 
| Bethory | 0:6ad07c9019fd | 106 | * The server socket must be bound and set to listen for connections. | 
| Bethory | 0:6ad07c9019fd | 107 | * On a new connection, creates a network socket and stores it in the | 
| Bethory | 0:6ad07c9019fd | 108 | * specified handle. The handle must be passed to following calls on | 
| Bethory | 0:6ad07c9019fd | 109 | * the socket. | 
| Bethory | 0:6ad07c9019fd | 110 | * | 
| Bethory | 0:6ad07c9019fd | 111 | * A stack may have a finite number of sockets, in this case | 
| Bethory | 0:6ad07c9019fd | 112 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. | 
| Bethory | 0:6ad07c9019fd | 113 | * | 
| Bethory | 0:6ad07c9019fd | 114 | * This call is non-blocking. If accept would block, | 
| Bethory | 0:6ad07c9019fd | 115 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. | 
| Bethory | 0:6ad07c9019fd | 116 | * | 
| Bethory | 0:6ad07c9019fd | 117 | * @param server Socket handle to server to accept from | 
| Bethory | 0:6ad07c9019fd | 118 | * @param handle Destination for a handle to the newly created socket | 
| Bethory | 0:6ad07c9019fd | 119 | * @param address Destination for the remote address or NULL | 
| Bethory | 0:6ad07c9019fd | 120 | * @return 0 on success, negative error code on failure | 
| Bethory | 0:6ad07c9019fd | 121 | */ | 
| Bethory | 0:6ad07c9019fd | 122 | virtual nsapi_error_t socket_accept(void *handle, void **server, SocketAddress *address); | 
| Bethory | 0:6ad07c9019fd | 123 | |
| Bethory | 0:6ad07c9019fd | 124 | /** Send data over a TCP socket | 
| Bethory | 0:6ad07c9019fd | 125 | * | 
| Bethory | 0:6ad07c9019fd | 126 | * The socket must be connected to a remote host. Returns the number of | 
| Bethory | 0:6ad07c9019fd | 127 | * bytes sent from the buffer. | 
| Bethory | 0:6ad07c9019fd | 128 | * | 
| Bethory | 0:6ad07c9019fd | 129 | * This call is non-blocking. If send would block, | 
| Bethory | 0:6ad07c9019fd | 130 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. | 
| Bethory | 0:6ad07c9019fd | 131 | * | 
| Bethory | 0:6ad07c9019fd | 132 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 133 | * @param data Buffer of data to send to the host | 
| Bethory | 0:6ad07c9019fd | 134 | * @param size Size of the buffer in bytes | 
| Bethory | 0:6ad07c9019fd | 135 | * @return Number of sent bytes on success, negative error | 
| Bethory | 0:6ad07c9019fd | 136 | * code on failure | 
| Bethory | 0:6ad07c9019fd | 137 | */ | 
| Bethory | 0:6ad07c9019fd | 138 | virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, nsapi_size_t size); | 
| Bethory | 0:6ad07c9019fd | 139 | |
| Bethory | 0:6ad07c9019fd | 140 | /** Receive data over a TCP socket | 
| Bethory | 0:6ad07c9019fd | 141 | * | 
| Bethory | 0:6ad07c9019fd | 142 | * The socket must be connected to a remote host. Returns the number of | 
| Bethory | 0:6ad07c9019fd | 143 | * bytes received into the buffer. | 
| Bethory | 0:6ad07c9019fd | 144 | * | 
| Bethory | 0:6ad07c9019fd | 145 | * This call is non-blocking. If recv would block, | 
| Bethory | 0:6ad07c9019fd | 146 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. | 
| Bethory | 0:6ad07c9019fd | 147 | * | 
| Bethory | 0:6ad07c9019fd | 148 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 149 | * @param data Destination buffer for data received from the host | 
| Bethory | 0:6ad07c9019fd | 150 | * @param size Size of the buffer in bytes | 
| Bethory | 0:6ad07c9019fd | 151 | * @return Number of received bytes on success, negative error | 
| Bethory | 0:6ad07c9019fd | 152 | * code on failure | 
| Bethory | 0:6ad07c9019fd | 153 | */ | 
| Bethory | 0:6ad07c9019fd | 154 | virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, nsapi_size_t size); | 
| Bethory | 0:6ad07c9019fd | 155 | |
| Bethory | 0:6ad07c9019fd | 156 | /** Send a packet over a UDP socket | 
| Bethory | 0:6ad07c9019fd | 157 | * | 
| Bethory | 0:6ad07c9019fd | 158 | * Sends data to the specified address. Returns the number of bytes | 
| Bethory | 0:6ad07c9019fd | 159 | * sent from the buffer. | 
| Bethory | 0:6ad07c9019fd | 160 | * | 
| Bethory | 0:6ad07c9019fd | 161 | * This call is non-blocking. If sendto would block, | 
| Bethory | 0:6ad07c9019fd | 162 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. | 
| Bethory | 0:6ad07c9019fd | 163 | * | 
| Bethory | 0:6ad07c9019fd | 164 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 165 | * @param address The SocketAddress of the remote host | 
| Bethory | 0:6ad07c9019fd | 166 | * @param data Buffer of data to send to the host | 
| Bethory | 0:6ad07c9019fd | 167 | * @param size Size of the buffer in bytes | 
| Bethory | 0:6ad07c9019fd | 168 | * @return Number of sent bytes on success, negative error | 
| Bethory | 0:6ad07c9019fd | 169 | * code on failure | 
| Bethory | 0:6ad07c9019fd | 170 | */ | 
| Bethory | 0:6ad07c9019fd | 171 | virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, nsapi_size_t size); | 
| Bethory | 0:6ad07c9019fd | 172 | |
| Bethory | 0:6ad07c9019fd | 173 | /** Receive a packet over a UDP socket | 
| Bethory | 0:6ad07c9019fd | 174 | * | 
| Bethory | 0:6ad07c9019fd | 175 | * Receives data and stores the source address in address if address | 
| Bethory | 0:6ad07c9019fd | 176 | * is not NULL. Returns the number of bytes received into the buffer. | 
| Bethory | 0:6ad07c9019fd | 177 | * | 
| Bethory | 0:6ad07c9019fd | 178 | * This call is non-blocking. If recvfrom would block, | 
| Bethory | 0:6ad07c9019fd | 179 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. | 
| Bethory | 0:6ad07c9019fd | 180 | * | 
| Bethory | 0:6ad07c9019fd | 181 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 182 | * @param address Destination for the source address or NULL | 
| Bethory | 0:6ad07c9019fd | 183 | * @param buffer Destination buffer for data received from the host | 
| Bethory | 0:6ad07c9019fd | 184 | * @param size Size of the buffer in bytes | 
| Bethory | 0:6ad07c9019fd | 185 | * @return Number of received bytes on success, negative error | 
| Bethory | 0:6ad07c9019fd | 186 | * code on failure | 
| Bethory | 0:6ad07c9019fd | 187 | */ | 
| Bethory | 0:6ad07c9019fd | 188 | virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, nsapi_size_t size); | 
| Bethory | 0:6ad07c9019fd | 189 | |
| Bethory | 0:6ad07c9019fd | 190 | /** Register a callback on state change of the socket | 
| Bethory | 0:6ad07c9019fd | 191 | * | 
| Bethory | 0:6ad07c9019fd | 192 | * The specified callback will be called on state changes such as when | 
| Bethory | 0:6ad07c9019fd | 193 | * the socket can recv/send/accept successfully and on when an error | 
| Bethory | 0:6ad07c9019fd | 194 | * occurs. The callback may also be called spuriously without reason. | 
| Bethory | 0:6ad07c9019fd | 195 | * | 
| Bethory | 0:6ad07c9019fd | 196 | * The callback may be called in an interrupt context and should not | 
| Bethory | 0:6ad07c9019fd | 197 | * perform expensive operations such as recv/send calls. | 
| Bethory | 0:6ad07c9019fd | 198 | * | 
| Bethory | 0:6ad07c9019fd | 199 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 200 | * @param callback Function to call on state change | 
| Bethory | 0:6ad07c9019fd | 201 | * @param data Argument to pass to callback | 
| Bethory | 0:6ad07c9019fd | 202 | */ | 
| Bethory | 0:6ad07c9019fd | 203 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); | 
| Bethory | 0:6ad07c9019fd | 204 | |
| Bethory | 0:6ad07c9019fd | 205 | /* Set stack-specific socket options | 
| Bethory | 0:6ad07c9019fd | 206 | * | 
| Bethory | 0:6ad07c9019fd | 207 | * The setsockopt allow an application to pass stack-specific hints | 
| Bethory | 0:6ad07c9019fd | 208 | * to the underlying stack. For unsupported options, | 
| Bethory | 0:6ad07c9019fd | 209 | * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. | 
| Bethory | 0:6ad07c9019fd | 210 | * | 
| Bethory | 0:6ad07c9019fd | 211 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 212 | * @param level Stack-specific protocol level | 
| Bethory | 0:6ad07c9019fd | 213 | * @param optname Stack-specific option identifier | 
| Bethory | 0:6ad07c9019fd | 214 | * @param optval Option value | 
| Bethory | 0:6ad07c9019fd | 215 | * @param optlen Length of the option value | 
| Bethory | 0:6ad07c9019fd | 216 | * @return 0 on success, negative error code on failure | 
| Bethory | 0:6ad07c9019fd | 217 | */ | 
| Bethory | 0:6ad07c9019fd | 218 | virtual nsapi_error_t setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen); | 
| Bethory | 0:6ad07c9019fd | 219 | |
| Bethory | 0:6ad07c9019fd | 220 | /* Get stack-specific socket options | 
| Bethory | 0:6ad07c9019fd | 221 | * | 
| Bethory | 0:6ad07c9019fd | 222 | * The getstackopt allow an application to retrieve stack-specific hints | 
| Bethory | 0:6ad07c9019fd | 223 | * from the underlying stack. For unsupported options, | 
| Bethory | 0:6ad07c9019fd | 224 | * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. | 
| Bethory | 0:6ad07c9019fd | 225 | * | 
| Bethory | 0:6ad07c9019fd | 226 | * @param handle Socket handle | 
| Bethory | 0:6ad07c9019fd | 227 | * @param level Stack-specific protocol level | 
| Bethory | 0:6ad07c9019fd | 228 | * @param optname Stack-specific option identifier | 
| Bethory | 0:6ad07c9019fd | 229 | * @param optval Destination for option value | 
| Bethory | 0:6ad07c9019fd | 230 | * @param optlen Length of the option value | 
| Bethory | 0:6ad07c9019fd | 231 | * @return 0 on success, negative error code on failure | 
| Bethory | 0:6ad07c9019fd | 232 | */ | 
| Bethory | 0:6ad07c9019fd | 233 | virtual nsapi_error_t getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen); | 
| Bethory | 0:6ad07c9019fd | 234 | |
| Bethory | 0:6ad07c9019fd | 235 | private: | 
| Bethory | 0:6ad07c9019fd | 236 | nsapi_size_or_error_t do_sendto(void *handle, const struct ns_address *address, const void *data, nsapi_size_t size); | 
| Bethory | 0:6ad07c9019fd | 237 | char text_ip_address[40]; | 
| Bethory | 0:6ad07c9019fd | 238 | static NanostackInterface * _ns_interface; | 
| Bethory | 0:6ad07c9019fd | 239 | }; | 
| Bethory | 0:6ad07c9019fd | 240 | |
| Bethory | 0:6ad07c9019fd | 241 | nsapi_error_t map_mesh_error(mesh_error_t err); | 
| Bethory | 0:6ad07c9019fd | 242 | |
| Bethory | 0:6ad07c9019fd | 243 | #endif /* NANOSTACK_INTERFACE_H_ */ |