emw3162 driver for mbed os 5

Fork of emw3162-driver by Maggie Mei

Committer:
Maggie17
Date:
Fri Nov 04 02:14:19 2016 +0000
Revision:
1:3c8bed04849d
Parent:
0:2d5090e1ceb4
Child:
2:fb6251306b21
EMW3162 driver for NSAPI(mbed classic)

Who changed what in which revision?

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