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.
Dependencies: X_NUCLEO_IKS01A1 mbed FP MQTTPacket DnsQuery ATParser
X_NUCLEO_IDW01M1v2/SPWF01SA/SPWFSA01.h@1:042ca9148926, 2017-10-19 (annotated)
- Committer:
- PavelSavyhin
- Date:
- Thu Oct 19 11:36:41 2017 +0000
- Revision:
- 1:042ca9148926
- Parent:
- 0:4cdaf9b1e7d0
Connection times are optimized and logging is extended.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| PavelSavyhin | 0:4cdaf9b1e7d0 | 1 | /* SPWFInterface Example |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 2 | * Copyright (c) 2015 ARM Limited |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 3 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 5 | * you may not use this file except in compliance with the License. |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 6 | * You may obtain a copy of the License at |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 7 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 9 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 10 | * Unless required by applicable law or agreed to in writing, software |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 13 | * See the License for the specific language governing permissions and |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 14 | * limitations under the License. |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 15 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 16 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 17 | #ifndef SPWFSA01_H |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 18 | #define SPWFSA01_H |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 19 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 20 | #include "ATParser.h" |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 21 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 22 | /** SPWFSA01Interface class. |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 23 | This is an interface to a SPWFSA01 module. |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 24 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 25 | class SPWFSA01 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 26 | { |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 27 | public: |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 28 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 29 | SPWFSA01(PinName tx, PinName rx, PinName reset=NC, PinName wakeup=NC, bool debug=false); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 30 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 31 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 32 | * Init the SPWFSA01 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 33 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 34 | * @param mode mode in which to startup |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 35 | * @return true only if SPWFSA01 has started up correctly |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 36 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 37 | bool startup(int mode); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 38 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 39 | void waitSPWFReady(void); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 40 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 41 | * Reset SPWFSA01 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 42 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 43 | * @return true only if SPWFSA01 resets successfully |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 44 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 45 | bool reset(void); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 46 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 47 | bool hw_reset(void); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 48 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 49 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 50 | * Enable/Disable DHCP |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 51 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 52 | * @param mode mode of DHCP 2-softAP, 1-on, 0-off |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 53 | * @return true only if SPWFSA01 enables/disables DHCP successfully |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 54 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 55 | bool dhcp(int mode); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 56 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 57 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 58 | * Connect SPWFSA01 to AP |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 59 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 60 | * @param ap the name of the AP |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 61 | * @param passPhrase the password of AP |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 62 | * @param securityMode the security mode of AP (WPA/WPA2, WEP, Open) |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 63 | * @return true only if SPWFSA01 is connected successfully |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 64 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 65 | bool connect(const char *ap, const char *passPhrase, int securityMode); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 66 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 67 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 68 | * Disconnect SPWFSA01 from AP |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 69 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 70 | * @return true only if SPWFSA01 is disconnected successfully |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 71 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 72 | bool disconnect(void); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 73 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 74 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 75 | * Get the IP address of SPWFSA01 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 76 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 77 | * @return null-teriminated IP address or null if no IP address is assigned |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 78 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 79 | const char *getIPAddress(void); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 80 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 81 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 82 | * Get the MAC address of SPWFSA01 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 83 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 84 | * @return null-terminated MAC address or null if no MAC address is assigned |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 85 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 86 | const char *getMACAddress(void); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 87 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 88 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 89 | * Check if SPWFSA01 is conenected |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 90 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 91 | * @return true only if the chip has an IP address |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 92 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 93 | bool isConnected(void); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 94 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 95 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 96 | * @brief wifi_socket_client_security |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 97 | * Set the security certificates and key for secure socket (TLS) |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 98 | * @param None |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 99 | * @retval return nonzero in case of error |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 100 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 101 | int setSocketClientSecurity(uint8_t* tls_mode, uint8_t* root_ca_server, uint8_t* client_cert, uint8_t* client_key, uint8_t* client_domain, uint32_t tls_epoch_time); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 102 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 103 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 104 | * Open a socketed connection |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 105 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 106 | * @param type the type of socket to open "u" (UDP) or "t" (TCP) |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 107 | * @param id id to get the new socket number, valid 0-7 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 108 | * @param port port to open connection with |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 109 | * @param addr the IP address of the destination |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 110 | * @return true only if socket opened successfully |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 111 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 112 | bool open(const char *type, int* id, const char* addr, int port); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 113 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 114 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 115 | * Sends data to an open socket |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 116 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 117 | * @param id id of socket to send to |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 118 | * @param data data to be sent |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 119 | * @param amount amount of data to be sent - max 1024 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 120 | * @return true only if data sent successfully |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 121 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 122 | bool send(int id, const void *data, uint32_t amount); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 123 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 124 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 125 | * Receives data from an open socket |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 126 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 127 | * @param id id to receive from |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 128 | * @param data placeholder for returned information |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 129 | * @param amount number of bytes to be received |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 130 | * @return the number of bytes received |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 131 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 132 | int32_t recv(int id, void *data, uint32_t amount); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 133 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 134 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 135 | * Closes a socket |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 136 | * |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 137 | * @param id id of socket to close, valid only 0-4 |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 138 | * @return true only if socket is closed successfully |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 139 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 140 | bool close(int id); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 141 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 142 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 143 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 144 | * Checks if data is available |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 145 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 146 | bool readable(); |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 147 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 148 | /** |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 149 | * Checks if data can be written |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 150 | */ |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 151 | bool writeable(); |
| PavelSavyhin | 1:042ca9148926 | 152 | |
| PavelSavyhin | 1:042ca9148926 | 153 | /** Sets debug mode */ |
| PavelSavyhin | 1:042ca9148926 | 154 | inline void set_debug(bool state) { dbg_on = state;_parser.debugOn(state);}; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 155 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 156 | private: |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 157 | BufferedSerial _serial; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 158 | ATParser _parser; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 159 | DigitalInOut _wakeup; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 160 | DigitalInOut _reset; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 161 | char _ip_buffer[16]; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 162 | char _mac_buffer[18]; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 163 | bool dbg_on; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 164 | // int _timeout; // FIXME LICIO we have "virtual" socket tmo, module socket tmo, |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 165 | // AT parser tmo, recv/send tmo, actually used the NetworksocketAPI socket tmo |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 166 | unsigned int _recv_timeout; // see SO_RCVTIMEO setsockopt |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 167 | unsigned int _send_timeout; // see SO_SNDTIMEO setsockopt |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 168 | unsigned int socket_closed; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 169 | }; |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 170 | |
| PavelSavyhin | 0:4cdaf9b1e7d0 | 171 | #endif //SPWFSA01_H |