emw312 driver

Fork of emw3162-driver by Maggie Mei

Committer:
sarahmarshy
Date:
Thu Jan 12 17:21:34 2017 -0600
Revision:
4:2ab7d68a654c
Parent:
3:0f0c97fee74d
Updated to mbed-os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Maggie17 2:fb6251306b21 1 /* EMW3162 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 2:fb6251306b21 17 #ifndef EMW3162_INTERFACE_H
Maggie17 2:fb6251306b21 18 #define EMW3162_INTERFACE_H
Maggie17 0:2d5090e1ceb4 19
Maggie17 1:3c8bed04849d 20 #include "EMW3162.h"
Maggie17 0:2d5090e1ceb4 21
Maggie17 0:2d5090e1ceb4 22
Maggie17 2:fb6251306b21 23 #define EMW3162_SOCKET_COUNT 5
Maggie17 0:2d5090e1ceb4 24
Maggie17 2:fb6251306b21 25 /** EMW3162Interface class
Maggie17 2:fb6251306b21 26 * Implementation of the NetworkStack for the EMW3162
Maggie17 0:2d5090e1ceb4 27 */
Maggie17 2:fb6251306b21 28 class EMW3162Interface : public NetworkStack, public WiFiInterface
Maggie17 0:2d5090e1ceb4 29 {
Maggie17 0:2d5090e1ceb4 30 public:
Maggie17 2:fb6251306b21 31 /** EMW3162Interface lifetime
Maggie17 0:2d5090e1ceb4 32 * @param tx TX pin
Maggie17 0:2d5090e1ceb4 33 * @param rx RX pin
Maggie17 0:2d5090e1ceb4 34 * @param debug Enable debugging
Maggie17 0:2d5090e1ceb4 35 */
Maggie17 2:fb6251306b21 36 EMW3162Interface(PinName tx, PinName rx, bool debug = false);
Maggie17 0:2d5090e1ceb4 37
Maggie17 0:2d5090e1ceb4 38 /** Start the interface
Maggie17 0:2d5090e1ceb4 39 *
Maggie17 0:2d5090e1ceb4 40 * Attempts to connect to a WiFi network. If passphrase is invalid,
Maggie17 0:2d5090e1ceb4 41 * NSAPI_ERROR_AUTH_ERROR is returned.
Maggie17 0:2d5090e1ceb4 42 *
Maggie17 0:2d5090e1ceb4 43 * @param ssid Name of the network to connect to
Maggie17 0:2d5090e1ceb4 44 * @param pass Security passphrase to connect to the network
Maggie17 0:2d5090e1ceb4 45 * @param security Type of encryption for connection
Maggie17 0:2d5090e1ceb4 46 * @return 0 on success, negative error code on failure
Maggie17 0:2d5090e1ceb4 47 */
sarahmarshy 4:2ab7d68a654c 48 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
sarahmarshy 4:2ab7d68a654c 49 uint8_t channel = 0);
sarahmarshy 4:2ab7d68a654c 50
sarahmarshy 4:2ab7d68a654c 51 /** Start the interface
sarahmarshy 4:2ab7d68a654c 52 *
sarahmarshy 4:2ab7d68a654c 53 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
sarahmarshy 4:2ab7d68a654c 54 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
sarahmarshy 4:2ab7d68a654c 55 *
sarahmarshy 4:2ab7d68a654c 56 * @return 0 on success, negative error code on failure
sarahmarshy 4:2ab7d68a654c 57 */
sarahmarshy 4:2ab7d68a654c 58 virtual int connect();
sarahmarshy 4:2ab7d68a654c 59
sarahmarshy 4:2ab7d68a654c 60 /** Set the WiFi network credentials
sarahmarshy 4:2ab7d68a654c 61 *
sarahmarshy 4:2ab7d68a654c 62 * @param ssid Name of the network to connect to
sarahmarshy 4:2ab7d68a654c 63 * @param pass Security passphrase to connect to the network
sarahmarshy 4:2ab7d68a654c 64 * @param security Type of encryption for connection
sarahmarshy 4:2ab7d68a654c 65 * (defaults to NSAPI_SECURITY_NONE)
sarahmarshy 4:2ab7d68a654c 66 * @return 0 on success, or error code on failure
sarahmarshy 4:2ab7d68a654c 67 */
sarahmarshy 4:2ab7d68a654c 68 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
sarahmarshy 4:2ab7d68a654c 69
sarahmarshy 4:2ab7d68a654c 70 /** Set the WiFi network channel - NOT SUPPORTED
sarahmarshy 4:2ab7d68a654c 71 *
sarahmarshy 4:2ab7d68a654c 72 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
sarahmarshy 4:2ab7d68a654c 73 *
sarahmarshy 4:2ab7d68a654c 74 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
sarahmarshy 4:2ab7d68a654c 75 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
sarahmarshy 4:2ab7d68a654c 76 */
sarahmarshy 4:2ab7d68a654c 77 virtual int set_channel(uint8_t channel);
Maggie17 0:2d5090e1ceb4 78
Maggie17 0:2d5090e1ceb4 79 /** Stop the interface
Maggie17 0:2d5090e1ceb4 80 * @return 0 on success, negative on failure
Maggie17 0:2d5090e1ceb4 81 */
Maggie17 0:2d5090e1ceb4 82 virtual int disconnect();
Maggie17 0:2d5090e1ceb4 83
Maggie17 0:2d5090e1ceb4 84 /** Get the internally stored IP address
Maggie17 0:2d5090e1ceb4 85 * @return IP address of the interface or null if not yet connected
Maggie17 0:2d5090e1ceb4 86 */
Maggie17 0:2d5090e1ceb4 87 virtual const char *get_ip_address();
Maggie17 0:2d5090e1ceb4 88
Maggie17 0:2d5090e1ceb4 89 /** Get the internally stored MAC address
Maggie17 0:2d5090e1ceb4 90 * @return MAC address of the interface
Maggie17 0:2d5090e1ceb4 91 */
Maggie17 0:2d5090e1ceb4 92 virtual const char *get_mac_address();
sarahmarshy 4:2ab7d68a654c 93
sarahmarshy 4:2ab7d68a654c 94 /** Gets the current radio signal strength for active connection
sarahmarshy 4:2ab7d68a654c 95 *
sarahmarshy 4:2ab7d68a654c 96 * @return Connection strength in dBm (negative value)
sarahmarshy 4:2ab7d68a654c 97 */
sarahmarshy 4:2ab7d68a654c 98 virtual int8_t get_rssi();
sarahmarshy 4:2ab7d68a654c 99
sarahmarshy 4:2ab7d68a654c 100 /** Scan for available networks
sarahmarshy 4:2ab7d68a654c 101 *
sarahmarshy 4:2ab7d68a654c 102 * This function will block.
sarahmarshy 4:2ab7d68a654c 103 *
sarahmarshy 4:2ab7d68a654c 104 * @param ap Pointer to allocated array to store discovered AP
sarahmarshy 4:2ab7d68a654c 105 * @param count Size of allocated @a res array, or 0 to only count available AP
sarahmarshy 4:2ab7d68a654c 106 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
sarahmarshy 4:2ab7d68a654c 107 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
sarahmarshy 4:2ab7d68a654c 108 * see @a nsapi_error
sarahmarshy 4:2ab7d68a654c 109 */
sarahmarshy 4:2ab7d68a654c 110 virtual int scan(WiFiAccessPoint *res, unsigned count);
Maggie17 0:2d5090e1ceb4 111
Maggie17 0:2d5090e1ceb4 112 protected:
Maggie17 0:2d5090e1ceb4 113 /** Open a socket
Maggie17 0:2d5090e1ceb4 114 * @param handle Handle in which to store new socket
Maggie17 0:2d5090e1ceb4 115 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
Maggie17 0:2d5090e1ceb4 116 * @return 0 on success, negative on failure
Maggie17 0:2d5090e1ceb4 117 */
Maggie17 0:2d5090e1ceb4 118 virtual int socket_open(void **handle, nsapi_protocol_t proto);
Maggie17 0:2d5090e1ceb4 119
Maggie17 0:2d5090e1ceb4 120 /** Close the socket
Maggie17 0:2d5090e1ceb4 121 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 122 * @return 0 on success, negative on failure
Maggie17 0:2d5090e1ceb4 123 * @note On failure, any memory associated with the socket must still
Maggie17 0:2d5090e1ceb4 124 * be cleaned up
Maggie17 0:2d5090e1ceb4 125 */
Maggie17 0:2d5090e1ceb4 126 virtual int socket_close(void *handle);
Maggie17 0:2d5090e1ceb4 127
Maggie17 0:2d5090e1ceb4 128 /** Bind a server socket to a specific port
Maggie17 0:2d5090e1ceb4 129 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 130 * @param address Local address to listen for incoming connections on
Maggie17 0:2d5090e1ceb4 131 * @return 0 on success, negative on failure.
Maggie17 0:2d5090e1ceb4 132 */
Maggie17 0:2d5090e1ceb4 133 virtual int socket_bind(void *handle, const SocketAddress &address);
Maggie17 0:2d5090e1ceb4 134
Maggie17 0:2d5090e1ceb4 135 /** Start listening for incoming connections
Maggie17 0:2d5090e1ceb4 136 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 137 * @param backlog Number of pending connections that can be queued up at any
Maggie17 0:2d5090e1ceb4 138 * one time [Default: 1]
Maggie17 0:2d5090e1ceb4 139 * @return 0 on success, negative on failure
Maggie17 0:2d5090e1ceb4 140 */
Maggie17 0:2d5090e1ceb4 141 virtual int socket_listen(void *handle, int backlog);
Maggie17 0:2d5090e1ceb4 142
Maggie17 0:2d5090e1ceb4 143 /** Connects this TCP socket to the server
Maggie17 0:2d5090e1ceb4 144 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 145 * @param address SocketAddress to connect to
Maggie17 0:2d5090e1ceb4 146 * @return 0 on success, negative on failure
Maggie17 0:2d5090e1ceb4 147 */
Maggie17 0:2d5090e1ceb4 148 virtual int socket_connect(void *handle, const SocketAddress &address);
Maggie17 0:2d5090e1ceb4 149
Maggie17 0:2d5090e1ceb4 150 /** Accept a new connection.
Maggie17 0:2d5090e1ceb4 151 * @param handle Handle in which to store new socket
Maggie17 0:2d5090e1ceb4 152 * @param server Socket handle to server to accept from
Maggie17 0:2d5090e1ceb4 153 * @return 0 on success, negative on failure
Maggie17 0:2d5090e1ceb4 154 * @note This call is not-blocking, if this call would block, must
Maggie17 0:2d5090e1ceb4 155 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:2d5090e1ceb4 156 */
sarahmarshy 4:2ab7d68a654c 157 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
Maggie17 0:2d5090e1ceb4 158
Maggie17 0:2d5090e1ceb4 159 /** Send data to the remote host
Maggie17 0:2d5090e1ceb4 160 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 161 * @param data The buffer to send to the host
Maggie17 0:2d5090e1ceb4 162 * @param size The length of the buffer to send
Maggie17 0:2d5090e1ceb4 163 * @return Number of written bytes on success, negative on failure
Maggie17 0:2d5090e1ceb4 164 * @note This call is not-blocking, if this call would block, must
Maggie17 0:2d5090e1ceb4 165 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:2d5090e1ceb4 166 */
Maggie17 0:2d5090e1ceb4 167 virtual int socket_send(void *handle, const void *data, unsigned size);
Maggie17 0:2d5090e1ceb4 168
Maggie17 0:2d5090e1ceb4 169 /** Receive data from the remote host
Maggie17 0:2d5090e1ceb4 170 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 171 * @param data The buffer in which to store the data received from the host
Maggie17 0:2d5090e1ceb4 172 * @param size The maximum length of the buffer
Maggie17 0:2d5090e1ceb4 173 * @return Number of received bytes on success, negative on failure
Maggie17 0:2d5090e1ceb4 174 * @note This call is not-blocking, if this call would block, must
Maggie17 0:2d5090e1ceb4 175 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:2d5090e1ceb4 176 */
Maggie17 0:2d5090e1ceb4 177 virtual int socket_recv(void *handle, void *data, unsigned size);
Maggie17 0:2d5090e1ceb4 178
Maggie17 0:2d5090e1ceb4 179 /** Send a packet to a remote endpoint
Maggie17 0:2d5090e1ceb4 180 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 181 * @param address The remote SocketAddress
Maggie17 0:2d5090e1ceb4 182 * @param data The packet to be sent
Maggie17 0:2d5090e1ceb4 183 * @param size The length of the packet to be sent
Maggie17 0:2d5090e1ceb4 184 * @return The number of written bytes on success, negative on failure
Maggie17 0:2d5090e1ceb4 185 * @note This call is not-blocking, if this call would block, must
Maggie17 0:2d5090e1ceb4 186 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:2d5090e1ceb4 187 */
Maggie17 0:2d5090e1ceb4 188 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
Maggie17 0:2d5090e1ceb4 189
Maggie17 0:2d5090e1ceb4 190 /** Receive a packet from a remote endpoint
Maggie17 0:2d5090e1ceb4 191 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 192 * @param address Destination for the remote SocketAddress or null
Maggie17 0:2d5090e1ceb4 193 * @param buffer The buffer for storing the incoming packet data
Maggie17 0:2d5090e1ceb4 194 * If a packet is too long to fit in the supplied buffer,
Maggie17 0:2d5090e1ceb4 195 * excess bytes are discarded
Maggie17 0:2d5090e1ceb4 196 * @param size The length of the buffer
Maggie17 0:2d5090e1ceb4 197 * @return The number of received bytes on success, negative on failure
Maggie17 0:2d5090e1ceb4 198 * @note This call is not-blocking, if this call would block, must
Maggie17 0:2d5090e1ceb4 199 * immediately return NSAPI_ERROR_WOULD_WAIT
Maggie17 0:2d5090e1ceb4 200 */
Maggie17 0:2d5090e1ceb4 201 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
Maggie17 0:2d5090e1ceb4 202
Maggie17 0:2d5090e1ceb4 203 /** Register a callback on state change of the socket
Maggie17 0:2d5090e1ceb4 204 * @param handle Socket handle
Maggie17 0:2d5090e1ceb4 205 * @param callback Function to call on state change
Maggie17 0:2d5090e1ceb4 206 * @param data Argument to pass to callback
Maggie17 0:2d5090e1ceb4 207 * @note Callback may be called in an interrupt context.
Maggie17 0:2d5090e1ceb4 208 */
Maggie17 0:2d5090e1ceb4 209 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
Maggie17 0:2d5090e1ceb4 210
Maggie17 0:2d5090e1ceb4 211 /** Provide access to the NetworkStack object
Maggie17 0:2d5090e1ceb4 212 *
Maggie17 0:2d5090e1ceb4 213 * @return The underlying NetworkStack object
Maggie17 0:2d5090e1ceb4 214 */
Maggie17 0:2d5090e1ceb4 215 virtual NetworkStack *get_stack()
Maggie17 0:2d5090e1ceb4 216 {
Maggie17 0:2d5090e1ceb4 217 return this;
Maggie17 0:2d5090e1ceb4 218 }
Maggie17 0:2d5090e1ceb4 219
Maggie17 0:2d5090e1ceb4 220 private:
Maggie17 2:fb6251306b21 221 EMW3162 _esp;
Maggie17 2:fb6251306b21 222 bool _ids[EMW3162_SOCKET_COUNT];
sarahmarshy 4:2ab7d68a654c 223 char ap_ssid[33];
sarahmarshy 4:2ab7d68a654c 224 char ap_pass[64];
sarahmarshy 4:2ab7d68a654c 225 nsapi_security_t ap_sec;
Maggie17 0:2d5090e1ceb4 226 void event();
Maggie17 0:2d5090e1ceb4 227 struct {
Maggie17 0:2d5090e1ceb4 228 void (*callback)(void *);
Maggie17 0:2d5090e1ceb4 229 void *data;
Maggie17 2:fb6251306b21 230 } _cbs[EMW3162_SOCKET_COUNT];
Maggie17 0:2d5090e1ceb4 231 };
Maggie17 0:2d5090e1ceb4 232
Maggie17 0:2d5090e1ceb4 233
Maggie17 0:2d5090e1ceb4 234 #endif