|
| 1 | +/* WHD implementation of NetworkInterfaceAPI |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 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 WHD_STA_INTERFACE_H |
| 18 | +#define WHD_STA_INTERFACE_H |
| 19 | + |
| 20 | +#include "mbed.h" |
| 21 | +#include "EthernetInterface.h" |
| 22 | +#include "netsocket/OnboardNetworkStack.h" |
| 23 | +#include "whd_emac.h" |
| 24 | +#include "whd_types_int.h" |
| 25 | + |
| 26 | +struct ol_desc; |
| 27 | + |
| 28 | +/** WhdSTAInterface class |
| 29 | + * Implementation of the NetworkStack for the WHD |
| 30 | + */ |
| 31 | +class WhdSTAInterface : public WiFiInterface, public EMACInterface |
| 32 | +{ |
| 33 | +public: |
| 34 | + |
| 35 | + class OlmInterface |
| 36 | + { |
| 37 | + public: |
| 38 | + /** Get the default OLM interface. */ |
| 39 | + static OlmInterface &get_default_instance(); |
| 40 | + |
| 41 | + OlmInterface(struct ol_desc *list = NULL) {} |
| 42 | + |
| 43 | + virtual int init_ols(void *whd, void *ip) { return 0; } |
| 44 | + virtual int sleep() { return 0; } |
| 45 | + virtual int wake() { return 0; } |
| 46 | + |
| 47 | + virtual void deinit_ols(void) {} |
| 48 | + }; |
| 49 | + |
| 50 | + WhdSTAInterface( |
| 51 | + WHD_EMAC &emac = WHD_EMAC::get_instance(), |
| 52 | + OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance(), |
| 53 | + OlmInterface &olm = OlmInterface::get_default_instance()); |
| 54 | + |
| 55 | + static WhdSTAInterface *get_default_instance(); |
| 56 | + |
| 57 | + /* Turn on the wifi device*/ |
| 58 | + void wifi_on(); |
| 59 | + |
| 60 | + /** Start the interface |
| 61 | + * |
| 62 | + * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. |
| 63 | + * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. |
| 64 | + * |
| 65 | + * @return 0 on success, negative error code on failure |
| 66 | + */ |
| 67 | + nsapi_error_t connect(); |
| 68 | + |
| 69 | + /** Start the interface |
| 70 | + * |
| 71 | + * Attempts to connect to a WiFi network. |
| 72 | + * |
| 73 | + * @param ssid Name of the network to connect to |
| 74 | + * @param pass Security passphrase to connect to the network |
| 75 | + * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) |
| 76 | + * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED |
| 77 | + * @return 0 on success, or error code on failure |
| 78 | + */ |
| 79 | + nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, uint8_t channel = 0); |
| 80 | + |
| 81 | + /** Stop the interface |
| 82 | + * @return 0 on success, negative on failure |
| 83 | + */ |
| 84 | + nsapi_error_t disconnect(); |
| 85 | + |
| 86 | + /** Set the WiFi network credentials |
| 87 | + * |
| 88 | + * @param ssid Name of the network to connect to |
| 89 | + * @param pass Security passphrase to connect to the network |
| 90 | + * @param security Type of encryption for connection |
| 91 | + * (defaults to NSAPI_SECURITY_NONE) |
| 92 | + * @return 0 on success, or error code on failure |
| 93 | + */ |
| 94 | + nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); |
| 95 | + |
| 96 | + /** Set the WiFi network channel - NOT SUPPORTED |
| 97 | + * |
| 98 | + * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
| 99 | + * |
| 100 | + * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) |
| 101 | + * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
| 102 | + */ |
| 103 | + nsapi_error_t set_channel(uint8_t channel) { |
| 104 | + if (channel != 0) { |
| 105 | + return NSAPI_ERROR_UNSUPPORTED; |
| 106 | + } |
| 107 | + |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + /** Gets the current radio signal strength for active connection |
| 112 | + * |
| 113 | + * @return Connection strength in dBm (negative value) |
| 114 | + */ |
| 115 | + int8_t get_rssi(); |
| 116 | + |
| 117 | + /** Scan for available networks |
| 118 | + * |
| 119 | + * This function will block. |
| 120 | + * |
| 121 | + * @param ap Pointer to allocated array to store discovered AP |
| 122 | + * @param count Size of allocated @a res array, or 0 to only count available AP |
| 123 | + * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) |
| 124 | + * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error |
| 125 | + * see @a nsapi_error |
| 126 | + */ |
| 127 | + int scan(WiFiAccessPoint *res, unsigned count); |
| 128 | + |
| 129 | + /* is interface connected, if yes return WICED_SUCCESS else WICED_NOT_CONNECTED */ |
| 130 | + int is_interface_connected(); |
| 131 | + |
| 132 | + /* get bssid of the AP if success return WICED_SUCCESS else WICED_ERROR */ |
| 133 | + int get_bssid(uint8_t *bssid); |
| 134 | + |
| 135 | + /* print WHD log (this routine will malloc/free a buffer |
| 136 | + * You need to enable printing with WHD_LOGGING_BUFFER_ENABLE |
| 137 | + */ |
| 138 | + int whd_log_print ( void ); |
| 139 | + |
| 140 | + /* read WHD log */ |
| 141 | + int whd_log_read ( char *buffer, int buffer_size ); |
| 142 | + |
| 143 | + /* Get EDCF AC params */ |
| 144 | + nsapi_error_t wifi_get_ac_params_sta(void * ac_param ); |
| 145 | + |
| 146 | + /* get iovar value */ |
| 147 | + int wifi_get_iovar_value ( const char *iovar, uint32_t *value ); |
| 148 | + |
| 149 | + /* set iovar value */ |
| 150 | + int wifi_set_iovar_value ( const char *iovar, uint32_t value ); |
| 151 | + |
| 152 | + /* set ioctl value */ |
| 153 | + int wifi_set_ioctl_value( uint32_t ioctl, uint32_t value ) ; |
| 154 | + |
| 155 | + /* set wifi interface up */ |
| 156 | + int wifi_set_up (void ); |
| 157 | + |
| 158 | + /* set wifi interface down */ |
| 159 | + int wifi_set_down (void ); |
| 160 | + |
| 161 | + /** Set Offload Manager Information |
| 162 | + * NOTE: Only allowed while disconnected |
| 163 | + * |
| 164 | + * @param olm Offload Manager info structure |
| 165 | + * @return true if completed successfully |
| 166 | + * false if Interface is connected |
| 167 | + */ |
| 168 | + int set_olm(OlmInterface *olm) { |
| 169 | + if (get_connection_status() == NSAPI_STATUS_DISCONNECTED) |
| 170 | + { |
| 171 | + _olm = olm; |
| 172 | + return true; |
| 173 | + } |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + /** Network stack is suspended |
| 178 | + * |
| 179 | + * @return 0 if successful |
| 180 | + */ |
| 181 | + int net_suspended() { |
| 182 | + int ret = _olm->sleep(); |
| 183 | + return ret; |
| 184 | + } |
| 185 | + |
| 186 | + /** Network stack is resuming |
| 187 | + * |
| 188 | + * @return 0 if successful |
| 189 | + */ |
| 190 | + int net_resuming() { |
| 191 | + int ret = _olm->wake(); |
| 192 | + return ret; |
| 193 | + } |
| 194 | +private: |
| 195 | + |
| 196 | + char _ssid[33]; /* The longest possible name (defined in 802.11) +1 for the \0 */ |
| 197 | + char _pass[64]; /* The longest allowed passphrase + 1 */ |
| 198 | + nsapi_security_t _security; |
| 199 | + WHD_EMAC& _whd_emac; |
| 200 | + OlmInterface *_olm; |
| 201 | +}; |
| 202 | + |
| 203 | +extern int wiced_leave_ap ( int interface ); |
| 204 | +#endif |
0 commit comments