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.
esp32-driver/ESP32Stack.h@0:8f8e8f3cbd1c, 2018-06-21 (annotated)
- Committer:
- mayur098
- Date:
- Thu Jun 21 17:50:21 2018 +0000
- Revision:
- 0:8f8e8f3cbd1c
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mayur098 | 0:8f8e8f3cbd1c | 1 | /* ESP32 implementation of NetworkInterfaceAPI |
mayur098 | 0:8f8e8f3cbd1c | 2 | * Copyright (c) 2015 ARM Limited |
mayur098 | 0:8f8e8f3cbd1c | 3 | * Copyright (c) 2017 Renesas Electronics Corporation |
mayur098 | 0:8f8e8f3cbd1c | 4 | * |
mayur098 | 0:8f8e8f3cbd1c | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mayur098 | 0:8f8e8f3cbd1c | 6 | * you may not use this file except in compliance with the License. |
mayur098 | 0:8f8e8f3cbd1c | 7 | * You may obtain a copy of the License at |
mayur098 | 0:8f8e8f3cbd1c | 8 | * |
mayur098 | 0:8f8e8f3cbd1c | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mayur098 | 0:8f8e8f3cbd1c | 10 | * |
mayur098 | 0:8f8e8f3cbd1c | 11 | * Unless required by applicable law or agreed to in writing, software |
mayur098 | 0:8f8e8f3cbd1c | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mayur098 | 0:8f8e8f3cbd1c | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mayur098 | 0:8f8e8f3cbd1c | 14 | * See the License for the specific language governing permissions and |
mayur098 | 0:8f8e8f3cbd1c | 15 | * limitations under the License. |
mayur098 | 0:8f8e8f3cbd1c | 16 | */ |
mayur098 | 0:8f8e8f3cbd1c | 17 | |
mayur098 | 0:8f8e8f3cbd1c | 18 | #ifndef ESP32_STACK_H |
mayur098 | 0:8f8e8f3cbd1c | 19 | #define ESP32_STACK_H |
mayur098 | 0:8f8e8f3cbd1c | 20 | |
mayur098 | 0:8f8e8f3cbd1c | 21 | #include "mbed.h" |
mayur098 | 0:8f8e8f3cbd1c | 22 | #include "ESP32.h" |
mayur098 | 0:8f8e8f3cbd1c | 23 | |
mayur098 | 0:8f8e8f3cbd1c | 24 | /** ESP32Stack class |
mayur098 | 0:8f8e8f3cbd1c | 25 | * Implementation of the NetworkStack for the ESP32 |
mayur098 | 0:8f8e8f3cbd1c | 26 | */ |
mayur098 | 0:8f8e8f3cbd1c | 27 | class ESP32Stack : public NetworkStack |
mayur098 | 0:8f8e8f3cbd1c | 28 | { |
mayur098 | 0:8f8e8f3cbd1c | 29 | protected: |
mayur098 | 0:8f8e8f3cbd1c | 30 | /** ESP32Stack lifetime |
mayur098 | 0:8f8e8f3cbd1c | 31 | * @param tx TX pin |
mayur098 | 0:8f8e8f3cbd1c | 32 | * @param rx RX pin |
mayur098 | 0:8f8e8f3cbd1c | 33 | * @param debug Enable debugging |
mayur098 | 0:8f8e8f3cbd1c | 34 | * @param rts RTS pin |
mayur098 | 0:8f8e8f3cbd1c | 35 | * @param cts CTS pin |
mayur098 | 0:8f8e8f3cbd1c | 36 | * @param baudrate The baudrate of the serial port. |
mayur098 | 0:8f8e8f3cbd1c | 37 | */ |
mayur098 | 0:8f8e8f3cbd1c | 38 | ESP32Stack(PinName en, PinName io0, PinName tx, PinName rx, bool debug, |
mayur098 | 0:8f8e8f3cbd1c | 39 | PinName rts, PinName cts, int baudrate); |
mayur098 | 0:8f8e8f3cbd1c | 40 | |
mayur098 | 0:8f8e8f3cbd1c | 41 | protected: |
mayur098 | 0:8f8e8f3cbd1c | 42 | /** Open a socket |
mayur098 | 0:8f8e8f3cbd1c | 43 | * @param handle Handle in which to store new socket |
mayur098 | 0:8f8e8f3cbd1c | 44 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
mayur098 | 0:8f8e8f3cbd1c | 45 | * @return 0 on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 46 | */ |
mayur098 | 0:8f8e8f3cbd1c | 47 | virtual int socket_open(void **handle, nsapi_protocol_t proto); |
mayur098 | 0:8f8e8f3cbd1c | 48 | |
mayur098 | 0:8f8e8f3cbd1c | 49 | /** Close the socket |
mayur098 | 0:8f8e8f3cbd1c | 50 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 51 | * @return 0 on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 52 | * @note On failure, any memory associated with the socket must still |
mayur098 | 0:8f8e8f3cbd1c | 53 | * be cleaned up |
mayur098 | 0:8f8e8f3cbd1c | 54 | */ |
mayur098 | 0:8f8e8f3cbd1c | 55 | virtual int socket_close(void *handle); |
mayur098 | 0:8f8e8f3cbd1c | 56 | |
mayur098 | 0:8f8e8f3cbd1c | 57 | /** Bind a server socket to a specific port |
mayur098 | 0:8f8e8f3cbd1c | 58 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 59 | * @param address Local address to listen for incoming connections on |
mayur098 | 0:8f8e8f3cbd1c | 60 | * @return 0 on success, negative on failure. |
mayur098 | 0:8f8e8f3cbd1c | 61 | */ |
mayur098 | 0:8f8e8f3cbd1c | 62 | virtual int socket_bind(void *handle, const SocketAddress &address); |
mayur098 | 0:8f8e8f3cbd1c | 63 | |
mayur098 | 0:8f8e8f3cbd1c | 64 | /** Start listening for incoming connections |
mayur098 | 0:8f8e8f3cbd1c | 65 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 66 | * @param backlog Number of pending connections that can be queued up at any |
mayur098 | 0:8f8e8f3cbd1c | 67 | * one time [Default: 1] |
mayur098 | 0:8f8e8f3cbd1c | 68 | * @return 0 on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 69 | */ |
mayur098 | 0:8f8e8f3cbd1c | 70 | virtual int socket_listen(void *handle, int backlog); |
mayur098 | 0:8f8e8f3cbd1c | 71 | |
mayur098 | 0:8f8e8f3cbd1c | 72 | /** Connects this TCP socket to the server |
mayur098 | 0:8f8e8f3cbd1c | 73 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 74 | * @param address SocketAddress to connect to |
mayur098 | 0:8f8e8f3cbd1c | 75 | * @return 0 on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 76 | */ |
mayur098 | 0:8f8e8f3cbd1c | 77 | virtual int socket_connect(void *handle, const SocketAddress &address); |
mayur098 | 0:8f8e8f3cbd1c | 78 | |
mayur098 | 0:8f8e8f3cbd1c | 79 | /** Accept a new connection. |
mayur098 | 0:8f8e8f3cbd1c | 80 | * @param handle Handle in which to store new socket |
mayur098 | 0:8f8e8f3cbd1c | 81 | * @param server Socket handle to server to accept from |
mayur098 | 0:8f8e8f3cbd1c | 82 | * @return 0 on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 83 | * @note This call is not-blocking, if this call would block, must |
mayur098 | 0:8f8e8f3cbd1c | 84 | * immediately return NSAPI_ERROR_WOULD_WAIT |
mayur098 | 0:8f8e8f3cbd1c | 85 | */ |
mayur098 | 0:8f8e8f3cbd1c | 86 | virtual int socket_accept(void *handle, void **socket, SocketAddress *address); |
mayur098 | 0:8f8e8f3cbd1c | 87 | |
mayur098 | 0:8f8e8f3cbd1c | 88 | /** Send data to the remote host |
mayur098 | 0:8f8e8f3cbd1c | 89 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 90 | * @param data The buffer to send to the host |
mayur098 | 0:8f8e8f3cbd1c | 91 | * @param size The length of the buffer to send |
mayur098 | 0:8f8e8f3cbd1c | 92 | * @return Number of written bytes on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 93 | * @note This call is not-blocking, if this call would block, must |
mayur098 | 0:8f8e8f3cbd1c | 94 | * immediately return NSAPI_ERROR_WOULD_WAIT |
mayur098 | 0:8f8e8f3cbd1c | 95 | */ |
mayur098 | 0:8f8e8f3cbd1c | 96 | virtual int socket_send(void *handle, const void *data, unsigned size); |
mayur098 | 0:8f8e8f3cbd1c | 97 | |
mayur098 | 0:8f8e8f3cbd1c | 98 | /** Receive data from the remote host |
mayur098 | 0:8f8e8f3cbd1c | 99 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 100 | * @param data The buffer in which to store the data received from the host |
mayur098 | 0:8f8e8f3cbd1c | 101 | * @param size The maximum length of the buffer |
mayur098 | 0:8f8e8f3cbd1c | 102 | * @return Number of received bytes on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 103 | * @note This call is not-blocking, if this call would block, must |
mayur098 | 0:8f8e8f3cbd1c | 104 | * immediately return NSAPI_ERROR_WOULD_WAIT |
mayur098 | 0:8f8e8f3cbd1c | 105 | */ |
mayur098 | 0:8f8e8f3cbd1c | 106 | virtual int socket_recv(void *handle, void *data, unsigned size); |
mayur098 | 0:8f8e8f3cbd1c | 107 | |
mayur098 | 0:8f8e8f3cbd1c | 108 | /** Send a packet to a remote endpoint |
mayur098 | 0:8f8e8f3cbd1c | 109 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 110 | * @param address The remote SocketAddress |
mayur098 | 0:8f8e8f3cbd1c | 111 | * @param data The packet to be sent |
mayur098 | 0:8f8e8f3cbd1c | 112 | * @param size The length of the packet to be sent |
mayur098 | 0:8f8e8f3cbd1c | 113 | * @return The number of written bytes on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 114 | * @note This call is not-blocking, if this call would block, must |
mayur098 | 0:8f8e8f3cbd1c | 115 | * immediately return NSAPI_ERROR_WOULD_WAIT |
mayur098 | 0:8f8e8f3cbd1c | 116 | */ |
mayur098 | 0:8f8e8f3cbd1c | 117 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); |
mayur098 | 0:8f8e8f3cbd1c | 118 | |
mayur098 | 0:8f8e8f3cbd1c | 119 | /** Receive a packet from a remote endpoint |
mayur098 | 0:8f8e8f3cbd1c | 120 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 121 | * @param address Destination for the remote SocketAddress or null |
mayur098 | 0:8f8e8f3cbd1c | 122 | * @param buffer The buffer for storing the incoming packet data |
mayur098 | 0:8f8e8f3cbd1c | 123 | * If a packet is too long to fit in the supplied buffer, |
mayur098 | 0:8f8e8f3cbd1c | 124 | * excess bytes are discarded |
mayur098 | 0:8f8e8f3cbd1c | 125 | * @param size The length of the buffer |
mayur098 | 0:8f8e8f3cbd1c | 126 | * @return The number of received bytes on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 127 | * @note This call is not-blocking, if this call would block, must |
mayur098 | 0:8f8e8f3cbd1c | 128 | * immediately return NSAPI_ERROR_WOULD_WAIT |
mayur098 | 0:8f8e8f3cbd1c | 129 | */ |
mayur098 | 0:8f8e8f3cbd1c | 130 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); |
mayur098 | 0:8f8e8f3cbd1c | 131 | |
mayur098 | 0:8f8e8f3cbd1c | 132 | /** Register a callback on state change of the socket |
mayur098 | 0:8f8e8f3cbd1c | 133 | * @param handle Socket handle |
mayur098 | 0:8f8e8f3cbd1c | 134 | * @param callback Function to call on state change |
mayur098 | 0:8f8e8f3cbd1c | 135 | * @param data Argument to pass to callback |
mayur098 | 0:8f8e8f3cbd1c | 136 | * @note Callback may be called in an interrupt context. |
mayur098 | 0:8f8e8f3cbd1c | 137 | */ |
mayur098 | 0:8f8e8f3cbd1c | 138 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
mayur098 | 0:8f8e8f3cbd1c | 139 | |
mayur098 | 0:8f8e8f3cbd1c | 140 | protected: |
mayur098 | 0:8f8e8f3cbd1c | 141 | ESP32 *_esp; |
mayur098 | 0:8f8e8f3cbd1c | 142 | }; |
mayur098 | 0:8f8e8f3cbd1c | 143 | |
mayur098 | 0:8f8e8f3cbd1c | 144 | #endif |