EMW3162 driver

Committer:
Maggie17
Date:
Tue Nov 01 12:43:30 2016 +0000
Revision:
0:65ba09002ce8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Maggie17 0:65ba09002ce8 1 /* ESP8266 implementation of NetworkInterfaceAPI
Maggie17 0:65ba09002ce8 2 * Copyright (c) 2015 ARM Limited
Maggie17 0:65ba09002ce8 3 *
Maggie17 0:65ba09002ce8 4 * Licensed under the Apache License, Version 2.0 (the "License");
Maggie17 0:65ba09002ce8 5 * you may not use this file except in compliance with the License.
Maggie17 0:65ba09002ce8 6 * You may obtain a copy of the License at
Maggie17 0:65ba09002ce8 7 *
Maggie17 0:65ba09002ce8 8 * http://www.apache.org/licenses/LICENSE-2.0
Maggie17 0:65ba09002ce8 9 *
Maggie17 0:65ba09002ce8 10 * Unless required by applicable law or agreed to in writing, software
Maggie17 0:65ba09002ce8 11 * distributed under the License is distributed on an "AS IS" BASIS,
Maggie17 0:65ba09002ce8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Maggie17 0:65ba09002ce8 13 * See the License for the specific language governing permissions and
Maggie17 0:65ba09002ce8 14 * limitations under the License.
Maggie17 0:65ba09002ce8 15 */
Maggie17 0:65ba09002ce8 16
Maggie17 0:65ba09002ce8 17 #ifndef ESP8266_INTERFACE_H
Maggie17 0:65ba09002ce8 18 #define ESP8266_INTERFACE_H
Maggie17 0:65ba09002ce8 19
Maggie17 0:65ba09002ce8 20 #include "network-socket/NetworkStack.h"
Maggie17 0:65ba09002ce8 21 #include "network-socket/WiFiInterface.h"
Maggie17 0:65ba09002ce8 22 #include "EMW3162.h"
Maggie17 0:65ba09002ce8 23
Maggie17 0:65ba09002ce8 24
Maggie17 0:65ba09002ce8 25 #define ESP8266_SOCKET_COUNT 5
Maggie17 0:65ba09002ce8 26
Maggie17 0:65ba09002ce8 27 /** ESP8266Interface class
Maggie17 0:65ba09002ce8 28 * Implementation of the NetworkStack for the ESP8266
Maggie17 0:65ba09002ce8 29 */
Maggie17 0:65ba09002ce8 30 class ESP8266Interface : public NetworkStack, public WiFiInterface
Maggie17 0:65ba09002ce8 31 {
Maggie17 0:65ba09002ce8 32 public:
Maggie17 0:65ba09002ce8 33 /** ESP8266Interface lifetime
Maggie17 0:65ba09002ce8 34 * @param tx TX pin
Maggie17 0:65ba09002ce8 35 * @param rx RX pin
Maggie17 0:65ba09002ce8 36 * @param debug Enable debugging
Maggie17 0:65ba09002ce8 37 */
Maggie17 0:65ba09002ce8 38 ESP8266Interface(PinName tx, PinName rx, bool debug = true);
Maggie17 0:65ba09002ce8 39
Maggie17 0:65ba09002ce8 40 /** Start the interface
Maggie17 0:65ba09002ce8 41 *
Maggie17 0:65ba09002ce8 42 * Attempts to connect to a WiFi network. If passphrase is invalid,
Maggie17 0:65ba09002ce8 43 * NSAPI_ERROR_AUTH_ERROR is returned.
Maggie17 0:65ba09002ce8 44 *
Maggie17 0:65ba09002ce8 45 * @param ssid Name of the network to connect to
Maggie17 0:65ba09002ce8 46 * @param pass Security passphrase to connect to the network
Maggie17 0:65ba09002ce8 47 * @param security Type of encryption for connection
Maggie17 0:65ba09002ce8 48 * @return 0 on success, negative error code on failure
Maggie17 0:65ba09002ce8 49 */
Maggie17 0:65ba09002ce8 50 virtual int connect(
Maggie17 0:65ba09002ce8 51 const char *ssid,
Maggie17 0:65ba09002ce8 52 const char *pass,
Maggie17 0:65ba09002ce8 53 nsapi_security_t security = NSAPI_SECURITY_NONE);
Maggie17 0:65ba09002ce8 54
Maggie17 0:65ba09002ce8 55 /** Stop the interface
Maggie17 0:65ba09002ce8 56 * @return 0 on success, negative on failure
Maggie17 0:65ba09002ce8 57 */
Maggie17 0:65ba09002ce8 58 virtual int disconnect();
Maggie17 0:65ba09002ce8 59
Maggie17 0:65ba09002ce8 60 /** Get the internally stored IP address
Maggie17 0:65ba09002ce8 61 * @return IP address of the interface or null if not yet connected
Maggie17 0:65ba09002ce8 62 */
Maggie17 0:65ba09002ce8 63 virtual const char *get_ip_address();
Maggie17 0:65ba09002ce8 64
Maggie17 0:65ba09002ce8 65 /** Get the internally stored MAC address
Maggie17 0:65ba09002ce8 66 * @return MAC address of the interface
Maggie17 0:65ba09002ce8 67 */
Maggie17 0:65ba09002ce8 68 virtual const char *get_mac_address();
Maggie17 0:65ba09002ce8 69
Maggie17 0:65ba09002ce8 70 protected:
Maggie17 0:65ba09002ce8 71 /** Open a socket
Maggie17 0:65ba09002ce8 72 * @param handle Handle in which to store new socket
Maggie17 0:65ba09002ce8 73 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
Maggie17 0:65ba09002ce8 74 * @return 0 on success, negative on failure
Maggie17 0:65ba09002ce8 75 */
Maggie17 0:65ba09002ce8 76 virtual int socket_open(void **handle, nsapi_protocol_t proto);
Maggie17 0:65ba09002ce8 77
Maggie17 0:65ba09002ce8 78 /** Close the socket
Maggie17 0:65ba09002ce8 79 * @param handle Socket handle
Maggie17 0:65ba09002ce8 80 * @return 0 on success, negative on failure
Maggie17 0:65ba09002ce8 81 * @note On failure, any memory associated with the socket must still
Maggie17 0:65ba09002ce8 82 * be cleaned up
Maggie17 0:65ba09002ce8 83 */
Maggie17 0:65ba09002ce8 84 virtual int socket_close(void *handle);
Maggie17 0:65ba09002ce8 85
Maggie17 0:65ba09002ce8 86 /** Bind a server socket to a specific port
Maggie17 0:65ba09002ce8 87 * @param handle Socket handle
Maggie17 0:65ba09002ce8 88 * @param address Local address to listen for incoming connections on
Maggie17 0:65ba09002ce8 89 * @return 0 on success, negative on failure.
Maggie17 0:65ba09002ce8 90 */
Maggie17 0:65ba09002ce8 91 virtual int socket_bind(void *handle, const SocketAddress &address);
Maggie17 0:65ba09002ce8 92
Maggie17 0:65ba09002ce8 93 /** Start listening for incoming connections
Maggie17 0:65ba09002ce8 94 * @param handle Socket handle
Maggie17 0:65ba09002ce8 95 * @param backlog Number of pending connections that can be queued up at any
Maggie17 0:65ba09002ce8 96 * one time [Default: 1]
Maggie17 0:65ba09002ce8 97 * @return 0 on success, negative on failure
Maggie17 0:65ba09002ce8 98 */
Maggie17 0:65ba09002ce8 99 virtual int socket_listen(void *handle, int backlog);
Maggie17 0:65ba09002ce8 100
Maggie17 0:65ba09002ce8 101 /** Connects this TCP socket to the server
Maggie17 0:65ba09002ce8 102 * @param handle Socket handle
Maggie17 0:65ba09002ce8 103 * @param address SocketAddress to connect to
Maggie17 0:65ba09002ce8 104 * @return 0 on success, negative on failure
Maggie17 0:65ba09002ce8 105 */
Maggie17 0:65ba09002ce8 106 virtual int socket_connect(void *handle, const SocketAddress &address);
Maggie17 0:65ba09002ce8 107
Maggie17 0:65ba09002ce8 108 /** Accept a new connection.
Maggie17 0:65ba09002ce8 109 * @param handle Handle in which to store new socket
Maggie17 0:65ba09002ce8 110 * @param server Socket handle to server to accept from
Maggie17 0:65ba09002ce8 111 * @return 0 on success, negative on failure
Maggie17 0:65ba09002ce8 112 * @note This call is not-blocking, if this call would block, must
Maggie17 0:65ba09002ce8 113 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:65ba09002ce8 114 */
Maggie17 0:65ba09002ce8 115 virtual int socket_accept(void **handle, void *server);
Maggie17 0:65ba09002ce8 116
Maggie17 0:65ba09002ce8 117 /** Send data to the remote host
Maggie17 0:65ba09002ce8 118 * @param handle Socket handle
Maggie17 0:65ba09002ce8 119 * @param data The buffer to send to the host
Maggie17 0:65ba09002ce8 120 * @param size The length of the buffer to send
Maggie17 0:65ba09002ce8 121 * @return Number of written bytes on success, negative on failure
Maggie17 0:65ba09002ce8 122 * @note This call is not-blocking, if this call would block, must
Maggie17 0:65ba09002ce8 123 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:65ba09002ce8 124 */
Maggie17 0:65ba09002ce8 125 virtual int socket_send(void *handle, const void *data, unsigned size);
Maggie17 0:65ba09002ce8 126
Maggie17 0:65ba09002ce8 127 /** Receive data from the remote host
Maggie17 0:65ba09002ce8 128 * @param handle Socket handle
Maggie17 0:65ba09002ce8 129 * @param data The buffer in which to store the data received from the host
Maggie17 0:65ba09002ce8 130 * @param size The maximum length of the buffer
Maggie17 0:65ba09002ce8 131 * @return Number of received bytes on success, negative on failure
Maggie17 0:65ba09002ce8 132 * @note This call is not-blocking, if this call would block, must
Maggie17 0:65ba09002ce8 133 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:65ba09002ce8 134 */
Maggie17 0:65ba09002ce8 135 virtual int socket_recv(void *handle, void *data, unsigned size);
Maggie17 0:65ba09002ce8 136
Maggie17 0:65ba09002ce8 137 /** Send a packet to a remote endpoint
Maggie17 0:65ba09002ce8 138 * @param handle Socket handle
Maggie17 0:65ba09002ce8 139 * @param address The remote SocketAddress
Maggie17 0:65ba09002ce8 140 * @param data The packet to be sent
Maggie17 0:65ba09002ce8 141 * @param size The length of the packet to be sent
Maggie17 0:65ba09002ce8 142 * @return The number of written bytes on success, negative on failure
Maggie17 0:65ba09002ce8 143 * @note This call is not-blocking, if this call would block, must
Maggie17 0:65ba09002ce8 144 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:65ba09002ce8 145 */
Maggie17 0:65ba09002ce8 146 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
Maggie17 0:65ba09002ce8 147
Maggie17 0:65ba09002ce8 148 /** Receive a packet from a remote endpoint
Maggie17 0:65ba09002ce8 149 * @param handle Socket handle
Maggie17 0:65ba09002ce8 150 * @param address Destination for the remote SocketAddress or null
Maggie17 0:65ba09002ce8 151 * @param buffer The buffer for storing the incoming packet data
Maggie17 0:65ba09002ce8 152 * If a packet is too long to fit in the supplied buffer,
Maggie17 0:65ba09002ce8 153 * excess bytes are discarded
Maggie17 0:65ba09002ce8 154 * @param size The length of the buffer
Maggie17 0:65ba09002ce8 155 * @return The number of received bytes on success, negative on failure
Maggie17 0:65ba09002ce8 156 * @note This call is not-blocking, if this call would block, must
Maggie17 0:65ba09002ce8 157 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:65ba09002ce8 158 */
Maggie17 0:65ba09002ce8 159 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
Maggie17 0:65ba09002ce8 160
Maggie17 0:65ba09002ce8 161 /** Register a callback on state change of the socket
Maggie17 0:65ba09002ce8 162 * @param handle Socket handle
Maggie17 0:65ba09002ce8 163 * @param callback Function to call on state change
Maggie17 0:65ba09002ce8 164 * @param data Argument to pass to callback
Maggie17 0:65ba09002ce8 165 * @note Callback may be called in an interrupt context.
Maggie17 0:65ba09002ce8 166 */
Maggie17 0:65ba09002ce8 167 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
Maggie17 0:65ba09002ce8 168
Maggie17 0:65ba09002ce8 169 /** Provide access to the NetworkStack object
Maggie17 0:65ba09002ce8 170 *
Maggie17 0:65ba09002ce8 171 * @return The underlying NetworkStack object
Maggie17 0:65ba09002ce8 172 */
Maggie17 0:65ba09002ce8 173 virtual NetworkStack *get_stack()
Maggie17 0:65ba09002ce8 174 {
Maggie17 0:65ba09002ce8 175 return this;
Maggie17 0:65ba09002ce8 176 }
Maggie17 0:65ba09002ce8 177
Maggie17 0:65ba09002ce8 178 private:
Maggie17 0:65ba09002ce8 179 ESP8266 _esp;
Maggie17 0:65ba09002ce8 180 bool _ids[ESP8266_SOCKET_COUNT];
Maggie17 0:65ba09002ce8 181
Maggie17 0:65ba09002ce8 182 void event();
Maggie17 0:65ba09002ce8 183 struct {
Maggie17 0:65ba09002ce8 184 void (*callback)(void *);
Maggie17 0:65ba09002ce8 185 void *data;
Maggie17 0:65ba09002ce8 186 } _cbs[ESP8266_SOCKET_COUNT];
Maggie17 0:65ba09002ce8 187 };
Maggie17 0:65ba09002ce8 188
Maggie17 0:65ba09002ce8 189
Maggie17 0:65ba09002ce8 190 #endif