Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ISM43362Interface.h
00001 /* ISM43362 implementation of NetworkInterfaceAPI 00002 * Copyright (c) STMicroelectronics 2017 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef ISM43362_INTERFACE_H 00018 #define ISM43362_INTERFACE_H 00019 00020 #include "mbed.h" 00021 #include "ISM43362.h" 00022 00023 00024 #define ISM43362_SOCKET_COUNT 4 00025 00026 /** ISM43362Interface class 00027 * Implementation of the NetworkStack for the ISM43362 00028 */ 00029 class ISM43362Interface : public NetworkStack, public WiFiInterface 00030 { 00031 public: 00032 /** ISM43362Interface lifetime 00033 * @param debug Enable debugging 00034 */ 00035 ISM43362Interface(bool debug = MBED_CONF_ISM43362_WIFI_DEBUG); 00036 00037 /** Start the interface 00038 * 00039 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. 00040 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. 00041 * 00042 * @return 0 on success, negative error code on failure 00043 */ 00044 virtual nsapi_error_t connect(); 00045 00046 /** Start the interface 00047 * 00048 * Attempts to connect to a WiFi network. 00049 * 00050 * @param ssid Name of the network to connect to 00051 * @param pass Security passphrase to connect to the network 00052 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) 00053 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED 00054 * @return 0 on success, or error code on failure 00055 */ 00056 virtual nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, 00057 uint8_t channel = 0); 00058 00059 /** Translates a hostname to an IP address with specific version 00060 * 00061 * The hostname may be either a domain name or an IP address. If the 00062 * hostname is an IP address, no network transactions will be performed. 00063 * 00064 * 00065 * @param host Hostname to resolve 00066 * @param address Destination for the host SocketAddress 00067 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00068 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00069 * @return 0 on success, negative error code on failure 00070 */ 00071 virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC); 00072 00073 /** Set the WiFi network credentials 00074 * 00075 * @param ssid Name of the network to connect to 00076 * @param pass Security passphrase to connect to the network 00077 * @param security Type of encryption for connection 00078 * (defaults to NSAPI_SECURITY_NONE) 00079 * @return 0 on success, or error code on failure 00080 */ 00081 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); 00082 00083 /** Set the WiFi network channel - NOT SUPPORTED 00084 * 00085 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00086 * 00087 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) 00088 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00089 */ 00090 virtual int set_channel(uint8_t channel); 00091 00092 /** Stop the interface 00093 * @return 0 on success, negative on failure 00094 */ 00095 virtual nsapi_error_t disconnect(); 00096 00097 /** Get the internally stored IP address 00098 * @return IP address of the interface or null if not yet connected 00099 */ 00100 virtual const char *get_ip_address(); 00101 00102 /** Get the internally stored MAC address 00103 * @return MAC address of the interface 00104 */ 00105 virtual const char *get_mac_address(); 00106 00107 /** Get the local gateway 00108 * 00109 * @return Null-terminated representation of the local gateway 00110 * or null if no network mask has been recieved 00111 */ 00112 virtual const char *get_gateway(); 00113 00114 /** Get the local network mask 00115 * 00116 * @return Null-terminated representation of the local network mask 00117 * or null if no network mask has been recieved 00118 */ 00119 virtual const char *get_netmask(); 00120 00121 /** Gets the current radio signal strength for active connection 00122 * 00123 * @return Connection strength in dBm (negative value) 00124 */ 00125 virtual int8_t get_rssi(); 00126 00127 /** Scan for available networks 00128 * 00129 * This function will block. 00130 * 00131 * @param ap Pointer to allocated array to store discovered AP 00132 * @param count Size of allocated @a res array, or 0 to only count available AP 00133 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error 00134 * see @a nsapi_error 00135 */ 00136 virtual int scan(WiFiAccessPoint *res, unsigned count); 00137 00138 /** Translates a hostname to an IP address with specific version 00139 * 00140 * The hostname may be either a domain name or an IP address. If the 00141 * hostname is an IP address, no network transactions will be performed. 00142 * 00143 * If no stack-specific DNS resolution is provided, the hostname 00144 * will be resolve using a UDP socket on the stack. 00145 * 00146 * @param address Destination for the host SocketAddress 00147 * @param host Hostname to resolve 00148 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00149 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00150 * @return 0 on success, negative error code on failure 00151 */ 00152 using NetworkInterface::gethostbyname; 00153 00154 /** Add a domain name server to list of servers to query 00155 * 00156 * @param addr Destination for the host address 00157 * @return 0 on success, negative error code on failure 00158 */ 00159 using NetworkInterface::add_dns_server; 00160 00161 /** Register callback for status reporting 00162 * 00163 * The specified status callback function will be called on status changes 00164 * on the network. The parameters on the callback are the event type and 00165 * event-type dependent reason parameter. 00166 * 00167 * @param status_cb The callback for status changes 00168 */ 00169 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); 00170 00171 /** Get the connection status 00172 * 00173 * @return The connection status according to ConnectionStatusType 00174 */ 00175 virtual nsapi_connection_status_t get_connection_status() const; 00176 00177 protected: 00178 /** Open a socket 00179 * @param handle Handle in which to store new socket 00180 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP 00181 * @return 0 on success, negative on failure 00182 */ 00183 virtual int socket_open(void **handle, nsapi_protocol_t proto); 00184 00185 /** Close the socket 00186 * @param handle Socket handle 00187 * @return 0 on success, negative on failure 00188 * @note On failure, any memory associated with the socket must still 00189 * be cleaned up 00190 */ 00191 virtual int socket_close(void *handle); 00192 00193 /** Bind a server socket to a specific port 00194 * @param handle Socket handle 00195 * @param address Local address to listen for incoming connections on 00196 * @return 0 on success, negative on failure. 00197 */ 00198 virtual int socket_bind(void *handle, const SocketAddress &address); 00199 00200 /** Start listening for incoming connections 00201 * @param handle Socket handle 00202 * @param backlog Number of pending connections that can be queued up at any 00203 * one time [Default: 1] 00204 * @return 0 on success, negative on failure 00205 */ 00206 virtual int socket_listen(void *handle, int backlog); 00207 00208 /** Connects this TCP socket to the server 00209 * @param handle Socket handle 00210 * @param address SocketAddress to connect to 00211 * @return 0 on success, negative on failure 00212 */ 00213 virtual int socket_connect(void *handle, const SocketAddress &address); 00214 00215 /** Accept a new connection. 00216 * @param handle Handle in which to store new socket 00217 * @param server Socket handle to server to accept from 00218 * @return 0 on success, negative on failure 00219 * @note This call is not-blocking, if this call would block, must 00220 * immediately return NSAPI_ERROR_WOULD_WAIT 00221 */ 00222 virtual int socket_accept(void *handle, void **socket, SocketAddress *address); 00223 00224 /** Send data to the remote host 00225 * @param handle Socket handle 00226 * @param data The buffer to send to the host 00227 * @param size The length of the buffer to send 00228 * @return Number of written bytes on success, negative on failure 00229 * @note This call is not-blocking, if this call would block, must 00230 * immediately return NSAPI_ERROR_WOULD_WAIT 00231 */ 00232 virtual int socket_send(void *handle, const void *data, unsigned size); 00233 00234 /** Receive data from the remote host 00235 * @param handle Socket handle 00236 * @param data The buffer in which to store the data received from the host 00237 * @param size The maximum length of the buffer 00238 * @return Number of received bytes on success, negative on failure 00239 * @note This call is not-blocking, if this call would block, must 00240 * immediately return NSAPI_ERROR_WOULD_WAIT 00241 */ 00242 virtual int socket_recv(void *handle, void *data, unsigned size); 00243 00244 /** Send a packet to a remote endpoint 00245 * @param handle Socket handle 00246 * @param address The remote SocketAddress 00247 * @param data The packet to be sent 00248 * @param size The length of the packet to be sent 00249 * @return The number of written bytes on success, negative on failure 00250 * @note This call is not-blocking, if this call would block, must 00251 * immediately return NSAPI_ERROR_WOULD_WAIT 00252 */ 00253 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); 00254 00255 /** Receive a packet from a remote endpoint 00256 * @param handle Socket handle 00257 * @param address Destination for the remote SocketAddress or null 00258 * @param buffer The buffer for storing the incoming packet data 00259 * If a packet is too long to fit in the supplied buffer, 00260 * excess bytes are discarded 00261 * @param size The length of the buffer 00262 * @return The number of received bytes on success, negative on failure 00263 * @note This call is not-blocking, if this call would block, must 00264 * immediately return NSAPI_ERROR_WOULD_WAIT 00265 */ 00266 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); 00267 00268 /** Register a callback on state change of the socket 00269 * @param handle Socket handle 00270 * @param callback Function to call on state change 00271 * @param data Argument to pass to callback 00272 * @note Callback may be called in an interrupt context. 00273 */ 00274 virtual void socket_attach(void *handle, void (*callback)(void *), void *data); 00275 00276 /** Provide access to the NetworkStack object 00277 * 00278 * @return The underlying NetworkStack object 00279 */ 00280 virtual NetworkStack *get_stack() 00281 { 00282 return this; 00283 } 00284 00285 private: 00286 ISM43362 _ism; 00287 bool _ids[ISM43362_SOCKET_COUNT]; 00288 uint32_t _socket_obj[ISM43362_SOCKET_COUNT]; // store addresses of socket handles 00289 Mutex _mutex; 00290 Thread thread_read_socket; 00291 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ 00292 ism_security_t ap_sec; 00293 uint8_t ap_ch; 00294 char ap_pass[64]; /* The longest allowed passphrase */ 00295 nsapi_error_t _connect_status ; 00296 00297 bool _ism_debug; 00298 uint32_t _FwVersion; 00299 00300 void event(); 00301 struct { 00302 void (*callback)(void *); 00303 void *data; 00304 } _cbs[ISM43362_SOCKET_COUNT]; 00305 00306 /** Function called by the socket read thread to check if data is available on the wifi module 00307 * 00308 */ 00309 virtual void socket_check_read(); 00310 int socket_send_nolock(void *handle, const void *data, unsigned size); 00311 int socket_connect_nolock(void *handle, const SocketAddress &addr); 00312 00313 // Connection state reporting to application 00314 void update_conn_state_cb(); 00315 nsapi_connection_status_t _conn_stat; 00316 mbed::Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb; 00317 00318 00319 }; 00320 00321 #endif
Generated on Mon Aug 29 2022 19:53:39 by
