Renesas / esp32-driver
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP32Stack.h Source File

ESP32Stack.h

00001 /* ESP32 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  * Copyright (c) 2017 Renesas Electronics Corporation
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 ESP32_STACK_H
00019 #define ESP32_STACK_H
00020 
00021 #include "mbed.h"
00022 #include "ESP32.h"
00023 
00024 /** ESP32Stack class
00025  *  Implementation of the NetworkStack for the ESP32
00026  */
00027 class ESP32Stack : public NetworkStack
00028 {
00029 protected:
00030     /** ESP32Stack lifetime
00031      * @param en        EN pin
00032      * @param io0       IO0 pin
00033      * @param tx        TX pin
00034      * @param rx        RX pin
00035      * @param debug     Enable debugging
00036      * @param rts       RTS pin
00037      * @param cts       CTS pin
00038      * @param baudrate  The baudrate of the serial port.
00039      */
00040     ESP32Stack(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
00041                PinName rts, PinName cts, int baudrate);
00042 
00043 protected:
00044     /** Open a socket
00045      *  @param handle       Handle in which to store new socket
00046      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00047      *  @return             0 on success, negative on failure
00048      */
00049     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00050 
00051     /** Close the socket
00052      *  @param handle       Socket handle
00053      *  @return             0 on success, negative on failure
00054      *  @note On failure, any memory associated with the socket must still
00055      *        be cleaned up
00056      */
00057     virtual int socket_close(void *handle);
00058 
00059     /** Bind a server socket to a specific port
00060      *  @param handle       Socket handle
00061      *  @param address      Local address to listen for incoming connections on
00062      *  @return             0 on success, negative on failure.
00063      */
00064     virtual int socket_bind(void *handle, const SocketAddress &address);
00065 
00066     /** Start listening for incoming connections
00067      *  @param handle       Socket handle
00068      *  @param backlog      Number of pending connections that can be queued up at any
00069      *                      one time [Default: 1]
00070      *  @return             0 on success, negative on failure
00071      */
00072     virtual int socket_listen(void *handle, int backlog);
00073 
00074     /** Connects this TCP socket to the server
00075      *  @param handle       Socket handle
00076      *  @param address      SocketAddress to connect to
00077      *  @return             0 on success, negative on failure
00078      */
00079     virtual int socket_connect(void *handle, const SocketAddress &address);
00080 
00081     /** Accept a new connection.
00082      *  @param handle       Handle in which to store new socket
00083      *  @param server       Socket handle to server to accept from
00084      *  @return             0 on success, negative on failure
00085      *  @note This call is not-blocking, if this call would block, must
00086      *        immediately return NSAPI_ERROR_WOULD_WAIT
00087      */
00088     virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
00089 
00090     /** Send data to the remote host
00091      *  @param handle       Socket handle
00092      *  @param data         The buffer to send to the host
00093      *  @param size         The length of the buffer to send
00094      *  @return             Number of written bytes on success, negative on failure
00095      *  @note This call is not-blocking, if this call would block, must
00096      *        immediately return NSAPI_ERROR_WOULD_WAIT
00097      */
00098     virtual int socket_send(void *handle, const void *data, unsigned size);
00099 
00100     /** Receive data from the remote host
00101      *  @param handle       Socket handle
00102      *  @param data         The buffer in which to store the data received from the host
00103      *  @param size         The maximum length of the buffer
00104      *  @return             Number of received bytes on success, negative on failure
00105      *  @note This call is not-blocking, if this call would block, must
00106      *        immediately return NSAPI_ERROR_WOULD_WAIT
00107      */
00108     virtual int socket_recv(void *handle, void *data, unsigned size);
00109 
00110     /** Send a packet to a remote endpoint
00111      *  @param handle       Socket handle
00112      *  @param address      The remote SocketAddress
00113      *  @param data         The packet to be sent
00114      *  @param size         The length of the packet to be sent
00115      *  @return             The number of written bytes on success, negative on failure
00116      *  @note This call is not-blocking, if this call would block, must
00117      *        immediately return NSAPI_ERROR_WOULD_WAIT
00118      */
00119     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00120 
00121     /** Receive a packet from a remote endpoint
00122      *  @param handle       Socket handle
00123      *  @param address      Destination for the remote SocketAddress or null
00124      *  @param buffer       The buffer for storing the incoming packet data
00125      *                      If a packet is too long to fit in the supplied buffer,
00126      *                      excess bytes are discarded
00127      *  @param size         The length of the buffer
00128      *  @return             The number of received bytes on success, negative on failure
00129      *  @note This call is not-blocking, if this call would block, must
00130      *        immediately return NSAPI_ERROR_WOULD_WAIT
00131      */
00132     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00133 
00134     /** Register a callback on state change of the socket
00135      *  @param handle       Socket handle
00136      *  @param callback     Function to call on state change
00137      *  @param data         Argument to pass to callback
00138      *  @note Callback may be called in an interrupt context.
00139      */
00140     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00141 
00142     /** Set stack-specific socket options
00143      *  The setsockopt allow an application to pass stack-specific hints
00144      *  to the underlying stack. For unsupported options,
00145      *  NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
00146      *
00147      *  @param handle   Socket handle
00148      *  @param level    Stack-specific protocol level
00149      *  @param optname  Stack-specific option identifier
00150      *  @param optval   Option value
00151      *  @param optlen   Length of the option value
00152      *  @return         0 on success, negative error code on failure
00153      */
00154     virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
00155             int optname, const void *optval, unsigned optlen);
00156 
00157     /** Get stack-specific socket options
00158      *  The getstackopt allow an application to retrieve stack-specific hints
00159      *  from the underlying stack. For unsupported options,
00160      *  NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
00161      *
00162      *  @param handle   Socket handle
00163      *  @param level    Stack-specific protocol level
00164      *  @param optname  Stack-specific option identifier
00165      *  @param optval   Destination for option value
00166      *  @param optlen   Length of the option value
00167      *  @return         0 on success, negative error code on failure
00168      */
00169     virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level,
00170             int optname, void *optval, unsigned *optlen);
00171 
00172 protected:
00173     ESP32 *_esp;
00174     uint16_t _local_ports[ESP32::SOCKET_COUNT];
00175 };
00176 
00177 #endif