|
| 1 | +/* ODIN-W2 implementation of WiFiInterface |
| 2 | + * Copyright (c) 2016 u-blox Malmö AB |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef ODIN_WIFI_INTERFACE_H |
| 18 | +#define ODIN_WIFI_INTERFACE_H |
| 19 | + |
| 20 | +#include "WiFiInterface.h" |
| 21 | +#include "Callback.h" |
| 22 | +#include "mbed_events.h" |
| 23 | + |
| 24 | +#include "rtos.h" |
| 25 | +#include "emac_api.h" |
| 26 | +#include "nsapi_types.h" |
| 27 | +#include "lwip/netif.h" |
| 28 | + |
| 29 | +typedef Queue<void*, 1> MsgQueue; |
| 30 | + |
| 31 | +class OdinWiFiInterface; |
| 32 | + |
| 33 | +struct PrivContext; |
| 34 | + |
| 35 | +/** OdinWiFiInterface class |
| 36 | + * Implementation of the WiFiInterface for the ODIN-W2 module |
| 37 | + */ |
| 38 | +class OdinWiFiInterface : public WiFiInterface |
| 39 | +{ |
| 40 | +public: |
| 41 | + /** OdinWiFiInterface lifetime |
| 42 | + */ |
| 43 | + OdinWiFiInterface(); |
| 44 | + |
| 45 | + OdinWiFiInterface(bool debug); |
| 46 | + |
| 47 | + ~OdinWiFiInterface(); |
| 48 | + |
| 49 | + /** Set the WiFi network credentials |
| 50 | + * |
| 51 | + * @param ssid Name of the network to connect to |
| 52 | + * @param pass Security passphrase to connect to the network |
| 53 | + * @param security Type of encryption for connection |
| 54 | + * (defaults to NSAPI_SECURITY_NONE) |
| 55 | + * @return 0 on success, or error code on failure |
| 56 | + */ |
| 57 | + virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); |
| 58 | + |
| 59 | + /** Set the WiFi network channel |
| 60 | + * |
| 61 | + * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) |
| 62 | + * @return 0 on success, or error code on failure |
| 63 | + */ |
| 64 | + virtual int set_channel(uint8_t channel); |
| 65 | + |
| 66 | + /** Start the interface |
| 67 | + * |
| 68 | + * Attempts to connect to a WiFi network. |
| 69 | + * |
| 70 | + * @param ssid Name of the network to connect to |
| 71 | + * @param pass Security passphrase to connect to the network |
| 72 | + * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) |
| 73 | + * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) |
| 74 | + * @return 0 on success, or error code on failure |
| 75 | + */ |
| 76 | + virtual int connect(const char *ssid, |
| 77 | + const char *pass, |
| 78 | + nsapi_security_t security = NSAPI_SECURITY_NONE, |
| 79 | + uint8_t channel = 0); |
| 80 | + |
| 81 | + /** Start the interface |
| 82 | + * |
| 83 | + * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. |
| 84 | + * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. |
| 85 | + * |
| 86 | + * @return 0 on success, negative error code on failure |
| 87 | + */ |
| 88 | + virtual int connect(); |
| 89 | + |
| 90 | + /** Stop the interface |
| 91 | + * |
| 92 | + * @return 0 on success, or error code on failure |
| 93 | + */ |
| 94 | + virtual int disconnect(); |
| 95 | + |
| 96 | + /** Get the local MAC address |
| 97 | + * |
| 98 | + * Provided MAC address is intended for info or debug purposes and |
| 99 | + * may not be provided if the underlying network interface does not |
| 100 | + * provide a MAC address |
| 101 | + * |
| 102 | + * @return Null-terminated representation of the local MAC address |
| 103 | + * or null if no MAC address is available |
| 104 | + */ |
| 105 | + virtual const char *get_mac_address(); |
| 106 | + |
| 107 | + /** Get the local IP address |
| 108 | + * |
| 109 | + * @return Null-terminated representation of the local IP address |
| 110 | + * or null if no IP address has been recieved |
| 111 | + */ |
| 112 | + virtual const char *get_ip_address(); |
| 113 | + |
| 114 | + /** Get the local network mask |
| 115 | + * |
| 116 | + * @return Null-terminated representation of the local network mask |
| 117 | + * or null if no network mask has been recieved |
| 118 | + */ |
| 119 | + virtual const char *get_netmask(); |
| 120 | + |
| 121 | + /** Get the local gateway |
| 122 | + * |
| 123 | + * @return Null-terminated representation of the local gateway |
| 124 | + * or null if no network mask has been recieved |
| 125 | + */ |
| 126 | + virtual const char *get_gateway(); |
| 127 | + |
| 128 | + /** Set a static IP address |
| 129 | + * |
| 130 | + * Configures this network interface to use a static IP address. |
| 131 | + * Implicitly disables DHCP, which can be enabled in set_dhcp. |
| 132 | + * Requires that the network is disconnected. |
| 133 | + * |
| 134 | + * @param address Null-terminated representation of the local IP address |
| 135 | + * @param netmask Null-terminated representation of the local network mask |
| 136 | + * @param gateway Null-terminated representation of the local gateway |
| 137 | + * @return 0 on success, negative error code on failure |
| 138 | + */ |
| 139 | + virtual int set_network(const char *ip_address, const char *netmask, const char *gateway); |
| 140 | + |
| 141 | + /** Enable or disable DHCP on the network |
| 142 | + * |
| 143 | + * Enables DHCP on connecting the network. Defaults to enabled unless |
| 144 | + * a static IP address has been assigned. Requires that the network is |
| 145 | + * disconnected. |
| 146 | + * |
| 147 | + * @param dhcp True to enable DHCP |
| 148 | + * @return 0 on success, negative error code on failure |
| 149 | + */ |
| 150 | + virtual int set_dhcp(bool dhcp); |
| 151 | + |
| 152 | + /** Gets the current radio signal strength for active connection |
| 153 | + * |
| 154 | + * @return Connection strength in dBm (negative value), |
| 155 | + * or 0 if measurement impossible |
| 156 | + */ |
| 157 | + virtual int8_t get_rssi(); |
| 158 | + |
| 159 | + /** Scan for available networks |
| 160 | + * |
| 161 | + * If the network interface is set to non-blocking mode, scan will attempt to scan |
| 162 | + * for WiFi networks asynchronously and return NSAPI_ERROR_WOULD_BLOCK. If a callback |
| 163 | + * is attached, the callback will be called when the operation has completed. |
| 164 | + * |
| 165 | + * @param ap Pointer to allocated array to store discovered AP |
| 166 | + * @param count Size of allocated @a res array, or 0 to only count available AP |
| 167 | + * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) |
| 168 | + * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error |
| 169 | + * see @a nsapi_error |
| 170 | + */ |
| 171 | + virtual int scan(WiFiAccessPoint *res, unsigned count); |
| 172 | + |
| 173 | + /** Sets timeout for connection setup. Note that the time for DHCP retrieval is not included. |
| 174 | + * |
| 175 | + * @param timeout Timeout in ms. Use 0 for waiting forever. The timeout might take up to X sec longer than |
| 176 | + * specified since the Wi-Fi driver might need some time to finish and cleanup. |
| 177 | + * @return 0 on success, negative error code on failure |
| 178 | + */ |
| 179 | + virtual void set_timeout(int timeout); |
| 180 | + |
| 181 | + virtual NetworkStack *get_stack(); |
| 182 | + |
| 183 | +protected: |
| 184 | + |
| 185 | +private: |
| 186 | + |
| 187 | + int connect_async(const char *ssid, |
| 188 | + const char *pass, |
| 189 | + nsapi_security_t security = NSAPI_SECURITY_NONE, |
| 190 | + uint8_t channel = 0, |
| 191 | + void *data = NULL, |
| 192 | + unsigned timeout = 0); |
| 193 | + |
| 194 | + bool start(bool debug); |
| 195 | + bool stop(); |
| 196 | + |
| 197 | + char _mac_addr_str[18]; |
| 198 | + // Private context to share between C and C++ calls |
| 199 | + PrivContext* _priv_context; |
| 200 | + const char *_ssid; |
| 201 | + const char *_pass; |
| 202 | + char _ip_address[IPADDR_STRLEN_MAX]; |
| 203 | + char _netmask[IPADDR_STRLEN_MAX]; |
| 204 | + char _gateway[IPADDR_STRLEN_MAX]; |
| 205 | + nsapi_security_t _security; |
| 206 | + uint8_t _channel; |
| 207 | + bool _use_dhcp; |
| 208 | + int _timeout; |
| 209 | + // Event queue when the driver context need to be used |
| 210 | + EventQueue* _odin_event_queue; |
| 211 | + int32_t target_id; |
| 212 | + // Event queue for sending start up and connection events from driver to this class |
| 213 | + MsgQueue _event_queue; |
| 214 | + // Event queue for sending scan events from driver to this class |
| 215 | + MsgQueue _scan_event_queue; |
| 216 | +}; |
| 217 | + |
| 218 | +#endif |
0 commit comments