Murata Type YD Wi-Fi driver

Dependents:   easy-connect-type-yd

Committer:
MACRUM
Date:
Wed Jul 12 10:49:10 2017 +0000
Revision:
0:35a2186cf186
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:35a2186cf186 1 /* Murata Type-YD implementation of NetworkInterfaceAPI
MACRUM 0:35a2186cf186 2 * Copyright (c) 2017 Murata Manufacturing Co., LTD.
MACRUM 0:35a2186cf186 3 *
MACRUM 0:35a2186cf186 4 * Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:35a2186cf186 5 * you may not use this file except in compliance with the License.
MACRUM 0:35a2186cf186 6 * You may obtain a copy of the License at
MACRUM 0:35a2186cf186 7 *
MACRUM 0:35a2186cf186 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:35a2186cf186 9 *
MACRUM 0:35a2186cf186 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:35a2186cf186 11 * distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:35a2186cf186 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:35a2186cf186 13 * See the License for the specific language governing permissions and
MACRUM 0:35a2186cf186 14 * limitations under the License.
MACRUM 0:35a2186cf186 15 */
MACRUM 0:35a2186cf186 16
MACRUM 0:35a2186cf186 17 #ifndef TYPEYD_INTERFACE_H
MACRUM 0:35a2186cf186 18 #define TYPEYD_INTERFACE_H
MACRUM 0:35a2186cf186 19
MACRUM 0:35a2186cf186 20 #include "mbed.h"
MACRUM 0:35a2186cf186 21 #include "SNICInterface/SNIC_WifiInterface.h"
MACRUM 0:35a2186cf186 22
MACRUM 0:35a2186cf186 23 /** TypeYDInterface class
MACRUM 0:35a2186cf186 24 * Implementation of the NetworkStack for the Type-YD
MACRUM 0:35a2186cf186 25 */
MACRUM 0:35a2186cf186 26 class TypeYDInterface : public NetworkStack, public WiFiInterface
MACRUM 0:35a2186cf186 27 {
MACRUM 0:35a2186cf186 28 public:
MACRUM 0:35a2186cf186 29 /** TypeYDInterface lifetime
MACRUM 0:35a2186cf186 30 * @param tx TX pin
MACRUM 0:35a2186cf186 31 * @param rx RX pin
MACRUM 0:35a2186cf186 32 * @param debug Enable debugging
MACRUM 0:35a2186cf186 33 */
MACRUM 0:35a2186cf186 34 TypeYDInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 115200);
MACRUM 0:35a2186cf186 35
MACRUM 0:35a2186cf186 36 /** Initialize TypeYD wifi module
MACRUM 0:35a2186cf186 37 *
MACRUM 0:35a2186cf186 38 * Attempts to start up Type YD module
MACRUM 0:35a2186cf186 39 *
MACRUM 0:35a2186cf186 40 * @return 0 on success, negative error code on failure
MACRUM 0:35a2186cf186 41 */
MACRUM 0:35a2186cf186 42 int init();
MACRUM 0:35a2186cf186 43
MACRUM 0:35a2186cf186 44 /** Get firmware version
MACRUM 0:35a2186cf186 45 *
MACRUM 0:35a2186cf186 46 * Attempts to get Type-YD firmware version
MACRUM 0:35a2186cf186 47 *
MACRUM 0:35a2186cf186 48 * @param version Buffer stored firmware version
MACRUM 0:35a2186cf186 49 * @param length Length of version buffer
MACRUM 0:35a2186cf186 50 * @return 0 on success, negative error code on failure
MACRUM 0:35a2186cf186 51 */
MACRUM 0:35a2186cf186 52 int getFWVersion(unsigned char *version, int length);
MACRUM 0:35a2186cf186 53
MACRUM 0:35a2186cf186 54 /** Start the interface
MACRUM 0:35a2186cf186 55 *
MACRUM 0:35a2186cf186 56 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
MACRUM 0:35a2186cf186 57 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
MACRUM 0:35a2186cf186 58 *
MACRUM 0:35a2186cf186 59 * @return 0 on success, negative error code on failure
MACRUM 0:35a2186cf186 60 */
MACRUM 0:35a2186cf186 61 virtual int connect();
MACRUM 0:35a2186cf186 62
MACRUM 0:35a2186cf186 63 /** Start the interface
MACRUM 0:35a2186cf186 64 *
MACRUM 0:35a2186cf186 65 * Attempts to connect to a WiFi network.
MACRUM 0:35a2186cf186 66 *
MACRUM 0:35a2186cf186 67 * @param ssid Name of the network to connect to
MACRUM 0:35a2186cf186 68 * @param pass Security passphrase to connect to the network
MACRUM 0:35a2186cf186 69 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
MACRUM 0:35a2186cf186 70 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
MACRUM 0:35a2186cf186 71 * @return 0 on success, or error code on failure
MACRUM 0:35a2186cf186 72 */
MACRUM 0:35a2186cf186 73 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
MACRUM 0:35a2186cf186 74 uint8_t channel = 0);
MACRUM 0:35a2186cf186 75
MACRUM 0:35a2186cf186 76 /** Set the WiFi network credentials
MACRUM 0:35a2186cf186 77 *
MACRUM 0:35a2186cf186 78 * @param ssid Name of the network to connect to
MACRUM 0:35a2186cf186 79 * @param pass Security passphrase to connect to the network
MACRUM 0:35a2186cf186 80 * @param security Type of encryption for connection
MACRUM 0:35a2186cf186 81 * (defaults to NSAPI_SECURITY_NONE)
MACRUM 0:35a2186cf186 82 * @return 0 on success, or error code on failure
MACRUM 0:35a2186cf186 83 */
MACRUM 0:35a2186cf186 84 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
MACRUM 0:35a2186cf186 85
MACRUM 0:35a2186cf186 86 /** Set the WiFi network channel - NOT SUPPORTED
MACRUM 0:35a2186cf186 87 *
MACRUM 0:35a2186cf186 88 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
MACRUM 0:35a2186cf186 89 *
MACRUM 0:35a2186cf186 90 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
MACRUM 0:35a2186cf186 91 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
MACRUM 0:35a2186cf186 92 */
MACRUM 0:35a2186cf186 93 virtual int set_channel(uint8_t channel);
MACRUM 0:35a2186cf186 94
MACRUM 0:35a2186cf186 95 /** Stop the interface
MACRUM 0:35a2186cf186 96 * @return 0 on success, negative on failure
MACRUM 0:35a2186cf186 97 */
MACRUM 0:35a2186cf186 98 virtual int disconnect();
MACRUM 0:35a2186cf186 99
MACRUM 0:35a2186cf186 100 /** Get the internally stored IP address
MACRUM 0:35a2186cf186 101 * @return IP address of the interface or null if not yet connected
MACRUM 0:35a2186cf186 102 */
MACRUM 0:35a2186cf186 103 virtual const char *get_ip_address();
MACRUM 0:35a2186cf186 104
MACRUM 0:35a2186cf186 105 /** Get the internally stored MAC address
MACRUM 0:35a2186cf186 106 * @return MAC address of the interface
MACRUM 0:35a2186cf186 107 */
MACRUM 0:35a2186cf186 108 virtual const char *get_mac_address();
MACRUM 0:35a2186cf186 109
MACRUM 0:35a2186cf186 110 /** Get the local gateway
MACRUM 0:35a2186cf186 111 *
MACRUM 0:35a2186cf186 112 * @return Null-terminated representation of the local gateway
MACRUM 0:35a2186cf186 113 * or null if no network mask has been recieved
MACRUM 0:35a2186cf186 114 */
MACRUM 0:35a2186cf186 115 virtual const char *get_gateway();
MACRUM 0:35a2186cf186 116
MACRUM 0:35a2186cf186 117 /** Get the local network mask
MACRUM 0:35a2186cf186 118 *
MACRUM 0:35a2186cf186 119 * @return Null-terminated representation of the local network mask
MACRUM 0:35a2186cf186 120 * or null if no network mask has been recieved
MACRUM 0:35a2186cf186 121 */
MACRUM 0:35a2186cf186 122 virtual const char *get_netmask();
MACRUM 0:35a2186cf186 123
MACRUM 0:35a2186cf186 124 /** Gets the current radio signal strength for active connection
MACRUM 0:35a2186cf186 125 *
MACRUM 0:35a2186cf186 126 * @return Connection strength in dBm (negative value)
MACRUM 0:35a2186cf186 127 */
MACRUM 0:35a2186cf186 128 virtual int8_t get_rssi();
MACRUM 0:35a2186cf186 129
MACRUM 0:35a2186cf186 130 /** Scan for available networks
MACRUM 0:35a2186cf186 131 *
MACRUM 0:35a2186cf186 132 * This function will block.
MACRUM 0:35a2186cf186 133 *
MACRUM 0:35a2186cf186 134 * @param ap Pointer to allocated array to store discovered AP
MACRUM 0:35a2186cf186 135 * @param count Size of allocated @a res array, or 0 to only count available AP
MACRUM 0:35a2186cf186 136 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
MACRUM 0:35a2186cf186 137 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
MACRUM 0:35a2186cf186 138 * see @a nsapi_error
MACRUM 0:35a2186cf186 139 */
MACRUM 0:35a2186cf186 140 virtual int scan(WiFiAccessPoint *res, unsigned count);
MACRUM 0:35a2186cf186 141
MACRUM 0:35a2186cf186 142 /** Translates a hostname to an IP address with specific version
MACRUM 0:35a2186cf186 143 *
MACRUM 0:35a2186cf186 144 * The hostname may be either a domain name or an IP address. If the
MACRUM 0:35a2186cf186 145 * hostname is an IP address, no network transactions will be performed.
MACRUM 0:35a2186cf186 146 *
MACRUM 0:35a2186cf186 147 * If no stack-specific DNS resolution is provided, the hostname
MACRUM 0:35a2186cf186 148 * will be resolve using a UDP socket on the stack.
MACRUM 0:35a2186cf186 149 *
MACRUM 0:35a2186cf186 150 * @param address Destination for the host SocketAddress
MACRUM 0:35a2186cf186 151 * @param host Hostname to resolve
MACRUM 0:35a2186cf186 152 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
MACRUM 0:35a2186cf186 153 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
MACRUM 0:35a2186cf186 154 * @return 0 on success, negative error code on failure
MACRUM 0:35a2186cf186 155 */
MACRUM 0:35a2186cf186 156 //using NetworkInterface::gethostbyname;
MACRUM 0:35a2186cf186 157 nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version);
MACRUM 0:35a2186cf186 158
MACRUM 0:35a2186cf186 159 /** Add a domain name server to list of servers to query
MACRUM 0:35a2186cf186 160 *
MACRUM 0:35a2186cf186 161 * @param addr Destination for the host address
MACRUM 0:35a2186cf186 162 * @return 0 on success, negative error code on failure
MACRUM 0:35a2186cf186 163 */
MACRUM 0:35a2186cf186 164 using NetworkInterface::add_dns_server;
MACRUM 0:35a2186cf186 165
MACRUM 0:35a2186cf186 166 protected:
MACRUM 0:35a2186cf186 167 /** Open a socket
MACRUM 0:35a2186cf186 168 * @param handle Handle in which to store new socket
MACRUM 0:35a2186cf186 169 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
MACRUM 0:35a2186cf186 170 * @return 0 on success, negative on failure
MACRUM 0:35a2186cf186 171 */
MACRUM 0:35a2186cf186 172 virtual int socket_open(void **handle, nsapi_protocol_t proto);
MACRUM 0:35a2186cf186 173
MACRUM 0:35a2186cf186 174 /** Close the socket
MACRUM 0:35a2186cf186 175 * @param handle Socket handle
MACRUM 0:35a2186cf186 176 * @return 0 on success, negative on failure
MACRUM 0:35a2186cf186 177 * @note On failure, any memory associated with the socket must still
MACRUM 0:35a2186cf186 178 * be cleaned up
MACRUM 0:35a2186cf186 179 */
MACRUM 0:35a2186cf186 180 virtual int socket_close(void *handle);
MACRUM 0:35a2186cf186 181
MACRUM 0:35a2186cf186 182 /** Bind a server socket to a specific port
MACRUM 0:35a2186cf186 183 * @param handle Socket handle
MACRUM 0:35a2186cf186 184 * @param address Local address to listen for incoming connections on
MACRUM 0:35a2186cf186 185 * @return 0 on success, negative on failure.
MACRUM 0:35a2186cf186 186 */
MACRUM 0:35a2186cf186 187 virtual int socket_bind(void *handle, const SocketAddress &address);
MACRUM 0:35a2186cf186 188
MACRUM 0:35a2186cf186 189 /** Start listening for incoming connections
MACRUM 0:35a2186cf186 190 * @param handle Socket handle
MACRUM 0:35a2186cf186 191 * @param backlog Number of pending connections that can be queued up at any
MACRUM 0:35a2186cf186 192 * one time [Default: 1]
MACRUM 0:35a2186cf186 193 * @return 0 on success, negative on failure
MACRUM 0:35a2186cf186 194 */
MACRUM 0:35a2186cf186 195 virtual int socket_listen(void *handle, int backlog);
MACRUM 0:35a2186cf186 196
MACRUM 0:35a2186cf186 197 /** Connects this TCP socket to the server
MACRUM 0:35a2186cf186 198 * @param handle Socket handle
MACRUM 0:35a2186cf186 199 * @param address SocketAddress to connect to
MACRUM 0:35a2186cf186 200 * @return 0 on success, negative on failure
MACRUM 0:35a2186cf186 201 */
MACRUM 0:35a2186cf186 202 virtual int socket_connect(void *handle, const SocketAddress &address);
MACRUM 0:35a2186cf186 203
MACRUM 0:35a2186cf186 204 /** Accept a new connection.
MACRUM 0:35a2186cf186 205 * @param handle Handle in which to store new socket
MACRUM 0:35a2186cf186 206 * @param server Socket handle to server to accept from
MACRUM 0:35a2186cf186 207 * @return 0 on success, negative on failure
MACRUM 0:35a2186cf186 208 * @note This call is not-blocking, if this call would block, must
MACRUM 0:35a2186cf186 209 * immediately return NSAPI_ERROR_WOULD_WAIT
MACRUM 0:35a2186cf186 210 */
MACRUM 0:35a2186cf186 211 virtual int socket_accept(void *handle, void **client_socket, SocketAddress *address);
MACRUM 0:35a2186cf186 212
MACRUM 0:35a2186cf186 213 /** Send data to the remote host
MACRUM 0:35a2186cf186 214 * @param handle Socket handle
MACRUM 0:35a2186cf186 215 * @param data The buffer to send to the host
MACRUM 0:35a2186cf186 216 * @param size The length of the buffer to send
MACRUM 0:35a2186cf186 217 * @return Number of written bytes on success, negative on failure
MACRUM 0:35a2186cf186 218 * @note This call is not-blocking, if this call would block, must
MACRUM 0:35a2186cf186 219 * immediately return NSAPI_ERROR_WOULD_WAIT
MACRUM 0:35a2186cf186 220 */
MACRUM 0:35a2186cf186 221 virtual int socket_send(void *handle, const void *data, unsigned size);
MACRUM 0:35a2186cf186 222
MACRUM 0:35a2186cf186 223 /** Receive data from the remote host
MACRUM 0:35a2186cf186 224 * @param handle Socket handle
MACRUM 0:35a2186cf186 225 * @param data The buffer in which to store the data received from the host
MACRUM 0:35a2186cf186 226 * @param size The maximum length of the buffer
MACRUM 0:35a2186cf186 227 * @return Number of received bytes on success, negative on failure
MACRUM 0:35a2186cf186 228 * @note This call is not-blocking, if this call would block, must
MACRUM 0:35a2186cf186 229 * immediately return NSAPI_ERROR_WOULD_WAIT
MACRUM 0:35a2186cf186 230 */
MACRUM 0:35a2186cf186 231 virtual int socket_recv(void *handle, void *data, unsigned size);
MACRUM 0:35a2186cf186 232
MACRUM 0:35a2186cf186 233 /** Send a packet to a remote endpoint
MACRUM 0:35a2186cf186 234 * @param handle Socket handle
MACRUM 0:35a2186cf186 235 * @param address The remote SocketAddress
MACRUM 0:35a2186cf186 236 * @param data The packet to be sent
MACRUM 0:35a2186cf186 237 * @param size The length of the packet to be sent
MACRUM 0:35a2186cf186 238 * @return The number of written bytes on success, negative on failure
MACRUM 0:35a2186cf186 239 * @note This call is not-blocking, if this call would block, must
MACRUM 0:35a2186cf186 240 * immediately return NSAPI_ERROR_WOULD_WAIT
MACRUM 0:35a2186cf186 241 */
MACRUM 0:35a2186cf186 242 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
MACRUM 0:35a2186cf186 243
MACRUM 0:35a2186cf186 244 /** Receive a packet from a remote endpoint
MACRUM 0:35a2186cf186 245 * @param handle Socket handle
MACRUM 0:35a2186cf186 246 * @param address Destination for the remote SocketAddress or null
MACRUM 0:35a2186cf186 247 * @param buffer The buffer for storing the incoming packet data
MACRUM 0:35a2186cf186 248 * If a packet is too long to fit in the supplied buffer,
MACRUM 0:35a2186cf186 249 * excess bytes are discarded
MACRUM 0:35a2186cf186 250 * @param size The length of the buffer
MACRUM 0:35a2186cf186 251 * @return The number of received bytes on success, negative on failure
MACRUM 0:35a2186cf186 252 * @note This call is not-blocking, if this call would block, must
MACRUM 0:35a2186cf186 253 * immediately return NSAPI_ERROR_WOULD_WAIT
MACRUM 0:35a2186cf186 254 */
MACRUM 0:35a2186cf186 255 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
MACRUM 0:35a2186cf186 256
MACRUM 0:35a2186cf186 257 /** Register a callback on state change of the socket
MACRUM 0:35a2186cf186 258 * @param handle Socket handle
MACRUM 0:35a2186cf186 259 * @param callback Function to call on state change
MACRUM 0:35a2186cf186 260 * @param data Argument to pass to callback
MACRUM 0:35a2186cf186 261 * @note Callback may be called in an interrupt context.
MACRUM 0:35a2186cf186 262 */
MACRUM 0:35a2186cf186 263 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
MACRUM 0:35a2186cf186 264
MACRUM 0:35a2186cf186 265 /** Provide access to the NetworkStack object
MACRUM 0:35a2186cf186 266 *
MACRUM 0:35a2186cf186 267 * @return The underlying NetworkStack object
MACRUM 0:35a2186cf186 268 */
MACRUM 0:35a2186cf186 269 virtual NetworkStack *get_stack()
MACRUM 0:35a2186cf186 270 {
MACRUM 0:35a2186cf186 271 return this;
MACRUM 0:35a2186cf186 272 }
MACRUM 0:35a2186cf186 273
MACRUM 0:35a2186cf186 274 private:
MACRUM 0:35a2186cf186 275 C_SNIC_WifiInterface _snic;
MACRUM 0:35a2186cf186 276
MACRUM 0:35a2186cf186 277 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
MACRUM 0:35a2186cf186 278 E_SECURITY ap_esec;
MACRUM 0:35a2186cf186 279 uint8_t ap_ch;
MACRUM 0:35a2186cf186 280 char ap_pass[65]; /* The longest allowed passphrase */
MACRUM 0:35a2186cf186 281
MACRUM 0:35a2186cf186 282 char mac_address[6];
MACRUM 0:35a2186cf186 283 char str_macaddr[3*6]; /* 00:00:00:00:00:00 */
MACRUM 0:35a2186cf186 284 };
MACRUM 0:35a2186cf186 285
MACRUM 0:35a2186cf186 286 #endif
MACRUM 0:35a2186cf186 287