BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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