mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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