Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* ODIN-W2 implementation of WiFiInterface
lypinator 0:bb348c97df44 2 * Copyright (c) 2016 u-blox Malmö AB
lypinator 0:bb348c97df44 3 *
lypinator 0:bb348c97df44 4 * Licensed under the Apache License, Version 2.0 (the "License");
lypinator 0:bb348c97df44 5 * you may not use this file except in compliance with the License.
lypinator 0:bb348c97df44 6 * You may obtain a copy of the License at
lypinator 0:bb348c97df44 7 *
lypinator 0:bb348c97df44 8 * http://www.apache.org/licenses/LICENSE-2.0
lypinator 0:bb348c97df44 9 *
lypinator 0:bb348c97df44 10 * Unless required by applicable law or agreed to in writing, software
lypinator 0:bb348c97df44 11 * distributed under the License is distributed on an "AS IS" BASIS,
lypinator 0:bb348c97df44 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lypinator 0:bb348c97df44 13 * See the License for the specific language governing permissions and
lypinator 0:bb348c97df44 14 * limitations under the License.
lypinator 0:bb348c97df44 15 */
lypinator 0:bb348c97df44 16
lypinator 0:bb348c97df44 17 #ifndef ODIN_WIFI_INTERFACE_H
lypinator 0:bb348c97df44 18 #define ODIN_WIFI_INTERFACE_H
lypinator 0:bb348c97df44 19
lypinator 0:bb348c97df44 20 #include "WiFiInterface.h"
lypinator 0:bb348c97df44 21 #ifdef DEVICE_WIFI_AP
lypinator 0:bb348c97df44 22 #include "WiFiSoftAPInterface.h"
lypinator 0:bb348c97df44 23 #endif
lypinator 0:bb348c97df44 24
lypinator 0:bb348c97df44 25 #include "mbed.h"
lypinator 0:bb348c97df44 26 #include "netsocket/WiFiAccessPoint.h"
lypinator 0:bb348c97df44 27 #include "netsocket/EMACInterface.h"
lypinator 0:bb348c97df44 28 #include "nsapi_types.h"
lypinator 0:bb348c97df44 29 #include "lwip/netif.h"
lypinator 0:bb348c97df44 30 #include "rtos.h"
lypinator 0:bb348c97df44 31 #include "cb_wlan.h"
lypinator 0:bb348c97df44 32
lypinator 0:bb348c97df44 33 #define ODIN_WIFI_MAX_MAC_ADDR_STR (18)
lypinator 0:bb348c97df44 34 #define ODIN_WIFI_SCAN_CACHE_SIZE (5)
lypinator 0:bb348c97df44 35
lypinator 0:bb348c97df44 36 struct odin_wifi_msg_s;
lypinator 0:bb348c97df44 37 struct user_connect_s;
lypinator 0:bb348c97df44 38 struct user_scan_s;
lypinator 0:bb348c97df44 39 struct user_ap_start_s;
lypinator 0:bb348c97df44 40 struct wlan_status_started_s;
lypinator 0:bb348c97df44 41 struct wlan_status_connected_s;
lypinator 0:bb348c97df44 42 struct wlan_status_connection_failure_s;
lypinator 0:bb348c97df44 43 struct wlan_scan_indication_s;
lypinator 0:bb348c97df44 44
lypinator 0:bb348c97df44 45 /** OdinWiFiInterface class
lypinator 0:bb348c97df44 46 * Implementation of the WiFiInterface for the ODIN-W2 module
lypinator 0:bb348c97df44 47 */
lypinator 0:bb348c97df44 48
lypinator 0:bb348c97df44 49 class OdinWiFiInterface : public WiFiInterface, public EMACInterface
lypinator 0:bb348c97df44 50
lypinator 0:bb348c97df44 51 {
lypinator 0:bb348c97df44 52 public:
lypinator 0:bb348c97df44 53 /** OdinWiFiInterface lifetime
lypinator 0:bb348c97df44 54 */
lypinator 0:bb348c97df44 55 OdinWiFiInterface();
lypinator 0:bb348c97df44 56
lypinator 0:bb348c97df44 57 OdinWiFiInterface(bool debug);
lypinator 0:bb348c97df44 58
lypinator 0:bb348c97df44 59 ~OdinWiFiInterface();
lypinator 0:bb348c97df44 60
lypinator 0:bb348c97df44 61 /** Set the WiFi network credentials
lypinator 0:bb348c97df44 62 *
lypinator 0:bb348c97df44 63 * @param ssid Name of the network to connect to
lypinator 0:bb348c97df44 64 * @param pass Security passphrase to connect to the network
lypinator 0:bb348c97df44 65 * @param security Type of encryption for connection
lypinator 0:bb348c97df44 66 * (defaults to NSAPI_SECURITY_NONE)
lypinator 0:bb348c97df44 67 * @return 0 on success, or error code on failure
lypinator 0:bb348c97df44 68 */
lypinator 0:bb348c97df44 69 virtual nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
lypinator 0:bb348c97df44 70
lypinator 0:bb348c97df44 71 /** Set the WiFi network channel
lypinator 0:bb348c97df44 72 *
lypinator 0:bb348c97df44 73 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
lypinator 0:bb348c97df44 74 * @return 0 on success, or error code on failure
lypinator 0:bb348c97df44 75 */
lypinator 0:bb348c97df44 76 virtual nsapi_error_t set_channel(uint8_t channel);
lypinator 0:bb348c97df44 77
lypinator 0:bb348c97df44 78 /** Start the interface
lypinator 0:bb348c97df44 79 *
lypinator 0:bb348c97df44 80 * Attempts to connect to a WiFi network.
lypinator 0:bb348c97df44 81 *
lypinator 0:bb348c97df44 82 * @param ssid Name of the network to connect to
lypinator 0:bb348c97df44 83 * @param pass Security passphrase to connect to the network
lypinator 0:bb348c97df44 84 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
lypinator 0:bb348c97df44 85 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
lypinator 0:bb348c97df44 86 * @return 0 on success, or error code on failure
lypinator 0:bb348c97df44 87 */
lypinator 0:bb348c97df44 88 virtual nsapi_error_t connect(
lypinator 0:bb348c97df44 89 const char *ssid,
lypinator 0:bb348c97df44 90 const char *pass,
lypinator 0:bb348c97df44 91 nsapi_security_t security = NSAPI_SECURITY_NONE,
lypinator 0:bb348c97df44 92 uint8_t channel = 0);
lypinator 0:bb348c97df44 93
lypinator 0:bb348c97df44 94 /** Start the interface
lypinator 0:bb348c97df44 95 *
lypinator 0:bb348c97df44 96 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
lypinator 0:bb348c97df44 97 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
lypinator 0:bb348c97df44 98 *
lypinator 0:bb348c97df44 99 * @return 0 on success, negative error code on failure
lypinator 0:bb348c97df44 100 */
lypinator 0:bb348c97df44 101 virtual nsapi_error_t connect();
lypinator 0:bb348c97df44 102
lypinator 0:bb348c97df44 103 /** Stop the interface
lypinator 0:bb348c97df44 104 *
lypinator 0:bb348c97df44 105 * @return 0 on success, or error code on failure
lypinator 0:bb348c97df44 106 */
lypinator 0:bb348c97df44 107 virtual nsapi_error_t disconnect();
lypinator 0:bb348c97df44 108
lypinator 0:bb348c97df44 109
lypinator 0:bb348c97df44 110
lypinator 0:bb348c97df44 111
lypinator 0:bb348c97df44 112 /** Gets the current radio signal strength for active connection
lypinator 0:bb348c97df44 113 *
lypinator 0:bb348c97df44 114 * @return Connection strength in dBm (negative value),
lypinator 0:bb348c97df44 115 * or 0 if measurement impossible
lypinator 0:bb348c97df44 116 */
lypinator 0:bb348c97df44 117 virtual int8_t get_rssi();
lypinator 0:bb348c97df44 118
lypinator 0:bb348c97df44 119 /** Scan for available networks
lypinator 0:bb348c97df44 120 *
lypinator 0:bb348c97df44 121 * If the network interface is set to non-blocking mode, scan will attempt to scan
lypinator 0:bb348c97df44 122 * for WiFi networks asynchronously and return NSAPI_ERROR_WOULD_BLOCK. If a callback
lypinator 0:bb348c97df44 123 * is attached, the callback will be called when the operation has completed.
lypinator 0:bb348c97df44 124 *
lypinator 0:bb348c97df44 125 * @param ap Pointer to allocated array to store discovered AP
lypinator 0:bb348c97df44 126 * @param count Size of allocated @a res array, or 0 to only count available AP
lypinator 0:bb348c97df44 127 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
lypinator 0:bb348c97df44 128 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
lypinator 0:bb348c97df44 129 * see @a nsapi_error
lypinator 0:bb348c97df44 130 */
lypinator 0:bb348c97df44 131 virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count);
lypinator 0:bb348c97df44 132
lypinator 0:bb348c97df44 133 /** Sets timeout for connection setup. Note that the time for DHCP retrieval is not included.
lypinator 0:bb348c97df44 134 *
lypinator 0:bb348c97df44 135 * @param timeout Timeout in ms. Use 0 for waiting forever. The timeout might take up to X sec longer than
lypinator 0:bb348c97df44 136 * specified since the Wi-Fi driver might need some time to finish and cleanup.
lypinator 0:bb348c97df44 137 * @return 0 on success, negative error code on failure
lypinator 0:bb348c97df44 138 */
lypinator 0:bb348c97df44 139 virtual nsapi_error_t set_timeout(int ms);
lypinator 0:bb348c97df44 140
lypinator 0:bb348c97df44 141 private:
lypinator 0:bb348c97df44 142
lypinator 0:bb348c97df44 143 enum OdinWifiState {
lypinator 0:bb348c97df44 144 S_NOT_INITIALISED = 1,
lypinator 0:bb348c97df44 145 S_WAIT_START,
lypinator 0:bb348c97df44 146 S_STARTED,
lypinator 0:bb348c97df44 147 S_WAIT_STOP,
lypinator 0:bb348c97df44 148
lypinator 0:bb348c97df44 149 S_STA_IDLE,
lypinator 0:bb348c97df44 150 S_STA_WAIT_CONNECT,
lypinator 0:bb348c97df44 151 S_STA_CONNECTED,
lypinator 0:bb348c97df44 152 S_STA_DISCONNECTED_WAIT_CONNECT,
lypinator 0:bb348c97df44 153 S_STA_CONNECTION_FAIL_WAIT_DISCONNECT,
lypinator 0:bb348c97df44 154 //S_STA_LINK_LOSS_WAIT_DISCONNECT,
lypinator 0:bb348c97df44 155 S_STA_WAIT_DISCONNECT,
lypinator 0:bb348c97df44 156
lypinator 0:bb348c97df44 157 S_AP_IDLE,
lypinator 0:bb348c97df44 158 S_AP_WAIT_START,
lypinator 0:bb348c97df44 159 S_AP_STARTED,
lypinator 0:bb348c97df44 160 S_AP_WAIT_STOP,
lypinator 0:bb348c97df44 161 S_AP_FAIL_WAIT_STOP,
lypinator 0:bb348c97df44 162 S_AP_WAIT_DRV_STOP,
lypinator 0:bb348c97df44 163 S_AP_WAIT_DRV_START,
lypinator 0:bb348c97df44 164
lypinator 0:bb348c97df44 165 S_INVALID
lypinator 0:bb348c97df44 166 };
lypinator 0:bb348c97df44 167
lypinator 0:bb348c97df44 168 struct sta_s {
lypinator 0:bb348c97df44 169 const char *ssid;
lypinator 0:bb348c97df44 170 const char *passwd;
lypinator 0:bb348c97df44 171 nsapi_security_t security;
lypinator 0:bb348c97df44 172 uint8_t channel;
lypinator 0:bb348c97df44 173 int timeout_ms;
lypinator 0:bb348c97df44 174 };
lypinator 0:bb348c97df44 175
lypinator 0:bb348c97df44 176 struct ap_s {
lypinator 0:bb348c97df44 177 const char *ssid;
lypinator 0:bb348c97df44 178 const char *passwd;
lypinator 0:bb348c97df44 179 nsapi_security_t security;
lypinator 0:bb348c97df44 180 uint8_t channel;
lypinator 0:bb348c97df44 181 bool use_dhcp;
lypinator 0:bb348c97df44 182
lypinator 0:bb348c97df44 183 char ip_address[IPADDR_STRLEN_MAX];
lypinator 0:bb348c97df44 184 char netmask[IPADDR_STRLEN_MAX];
lypinator 0:bb348c97df44 185 char gateway[IPADDR_STRLEN_MAX];
lypinator 0:bb348c97df44 186
lypinator 0:bb348c97df44 187 int cnt_connected;
lypinator 0:bb348c97df44 188
lypinator 0:bb348c97df44 189 nsapi_error_t error_code;
lypinator 0:bb348c97df44 190 uint16_t beacon_interval;
lypinator 0:bb348c97df44 191 };
lypinator 0:bb348c97df44 192
lypinator 0:bb348c97df44 193 struct scan_cache_s {
lypinator 0:bb348c97df44 194 int count;
lypinator 0:bb348c97df44 195 uint8_t last_channel;
lypinator 0:bb348c97df44 196 cbWLAN_MACAddress bssid[ODIN_WIFI_SCAN_CACHE_SIZE];
lypinator 0:bb348c97df44 197 };
lypinator 0:bb348c97df44 198
lypinator 0:bb348c97df44 199 OdinWifiState entry_connect_fail_wait_disconnect();
lypinator 0:bb348c97df44 200 OdinWifiState entry_wait_connect();
lypinator 0:bb348c97df44 201 OdinWifiState entry_wait_disconnect();
lypinator 0:bb348c97df44 202 //OdinWifiState entry_link_loss_wait_disconnect(void);
lypinator 0:bb348c97df44 203 OdinWifiState entry_ap_wait_start();
lypinator 0:bb348c97df44 204 OdinWifiState entry_ap_started();
lypinator 0:bb348c97df44 205 OdinWifiState entry_ap_wait_stop();
lypinator 0:bb348c97df44 206 OdinWifiState entry_ap_fail_wait_stop();
lypinator 0:bb348c97df44 207 OdinWifiState entry_ap_wait_drv_stop();
lypinator 0:bb348c97df44 208 OdinWifiState entry_ap_wait_drv_start();
lypinator 0:bb348c97df44 209
lypinator 0:bb348c97df44 210 void handle_in_msg();
lypinator 0:bb348c97df44 211 void handle_cached_msg();
lypinator 0:bb348c97df44 212
lypinator 0:bb348c97df44 213 void handle_user_connect(user_connect_s *user_connect);
lypinator 0:bb348c97df44 214 void handle_user_disconnect();
lypinator 0:bb348c97df44 215 void handle_user_scan(user_scan_s *user_scan);
lypinator 0:bb348c97df44 216 void handle_user_connect_timeout();
lypinator 0:bb348c97df44 217 void handle_user_stop();
lypinator 0:bb348c97df44 218
lypinator 0:bb348c97df44 219 void handle_user_ap_start(user_ap_start_s *user_ap_start);
lypinator 0:bb348c97df44 220 void handle_user_ap_stop();
lypinator 0:bb348c97df44 221
lypinator 0:bb348c97df44 222 void handle_wlan_status_started(wlan_status_started_s *start);
lypinator 0:bb348c97df44 223 void handle_wlan_status_stopped(void);
lypinator 0:bb348c97df44 224 void handle_wlan_status_error(void);
lypinator 0:bb348c97df44 225 void handle_wlan_status_connecting(void);
lypinator 0:bb348c97df44 226 void handle_wlan_status_connected(wlan_status_connected_s *wlan_connect);
lypinator 0:bb348c97df44 227 void handle_wlan_status_connection_failure(wlan_status_connection_failure_s *connect_failure);
lypinator 0:bb348c97df44 228 void handle_wlan_status_disconnected(void);
lypinator 0:bb348c97df44 229 void handle_wlan_scan_indication();
lypinator 0:bb348c97df44 230
lypinator 0:bb348c97df44 231 void handle_wlan_status_ap_up();
lypinator 0:bb348c97df44 232 void handle_wlan_status_ap_down();
lypinator 0:bb348c97df44 233
lypinator 0:bb348c97df44 234 void init(bool debug);
lypinator 0:bb348c97df44 235 nsapi_error_t wlan_set_channel(uint8_t channel);
lypinator 0:bb348c97df44 236 nsapi_error_t wlan_connect(
lypinator 0:bb348c97df44 237 const char *ssid,
lypinator 0:bb348c97df44 238 const char *passwd,
lypinator 0:bb348c97df44 239 nsapi_security_t security);
lypinator 0:bb348c97df44 240 nsapi_error_t wlan_ap_start(
lypinator 0:bb348c97df44 241 const char *ssid,
lypinator 0:bb348c97df44 242 const char *pass,
lypinator 0:bb348c97df44 243 nsapi_security_t security,
lypinator 0:bb348c97df44 244 uint8_t channel,
lypinator 0:bb348c97df44 245 uint16_t beacon_interval);
lypinator 0:bb348c97df44 246
lypinator 0:bb348c97df44 247 void timeout_user_connect();
lypinator 0:bb348c97df44 248 void update_scan_list(cbWLAN_ScanIndicationInfo *scan_info);
lypinator 0:bb348c97df44 249 void send_user_response_msg(unsigned int type, nsapi_error_t error_code);
lypinator 0:bb348c97df44 250 void wlan_status_indication(cbWLAN_StatusIndicationInfo status, void *data);
lypinator 0:bb348c97df44 251 void wlan_scan_indication(cbWLAN_ScanIndicationInfo *scan_info, cb_boolean is_last_result);
lypinator 0:bb348c97df44 252
lypinator 0:bb348c97df44 253 static bool _wlan_initialized; // Controls that cbWLAN is initiated only once
lypinator 0:bb348c97df44 254 static int32_t _target_id;
lypinator 0:bb348c97df44 255
lypinator 0:bb348c97df44 256 OdinWifiState _state;
lypinator 0:bb348c97df44 257 OdinWifiState _state_sta;
lypinator 0:bb348c97df44 258 OdinWifiState _state_ap;
lypinator 0:bb348c97df44 259
lypinator 0:bb348c97df44 260 struct sta_s _sta;
lypinator 0:bb348c97df44 261 struct ap_s _ap;
lypinator 0:bb348c97df44 262 char _mac_addr_str[ODIN_WIFI_MAX_MAC_ADDR_STR];
lypinator 0:bb348c97df44 263
lypinator 0:bb348c97df44 264 cbWLAN_StatusConnectedInfo _wlan_status_connected_info;
lypinator 0:bb348c97df44 265 cbWLAN_StatusDisconnectedInfo _wlan_status_disconnected_info;
lypinator 0:bb348c97df44 266
lypinator 0:bb348c97df44 267 bool _scan_active;
lypinator 0:bb348c97df44 268 WiFiAccessPoint *_scan_list;
lypinator 0:bb348c97df44 269 nsapi_size_t _scan_list_size;
lypinator 0:bb348c97df44 270 nsapi_size_t _scan_list_cnt;
lypinator 0:bb348c97df44 271 struct scan_cache_s _scan_cache;
lypinator 0:bb348c97df44 272
lypinator 0:bb348c97df44 273 friend struct wlan_callb_s;
lypinator 0:bb348c97df44 274
lypinator 0:bb348c97df44 275 Mutex _mutex;
lypinator 0:bb348c97df44 276 Queue<odin_wifi_msg_s, 6> _in_queue;
lypinator 0:bb348c97df44 277 Queue<odin_wifi_msg_s, 1> _out_queue;
lypinator 0:bb348c97df44 278 Queue<odin_wifi_msg_s, 1> _cache_queue;
lypinator 0:bb348c97df44 279 MemoryPool<odin_wifi_msg_s, 7> *_msg_pool;
lypinator 0:bb348c97df44 280 Thread _thread;
lypinator 0:bb348c97df44 281 //Timeout _timeout; //Randomly lost interrupts/callbacks; replaced by Timer
lypinator 0:bb348c97df44 282 Timer _timer;
lypinator 0:bb348c97df44 283
lypinator 0:bb348c97df44 284 bool _debug;
lypinator 0:bb348c97df44 285 int _dbg_timeout;
lypinator 0:bb348c97df44 286 };
lypinator 0:bb348c97df44 287
lypinator 0:bb348c97df44 288 #endif