├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── src ├── error.rs ├── lib.rs ├── module.rs ├── network_session.rs ├── parser.rs ├── reader_part.rs ├── softap.rs └── tests.rs └── tests ├── commands.rs └── common └── mod.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | lints: 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - uses: actions/checkout@v2 18 | 19 | - name: Format 20 | run: cargo fmt 21 | 22 | - name: Clippy 23 | run: cargo clippy --verbose 24 | 25 | tests: 26 | 27 | runs-on: ubuntu-latest 28 | 29 | steps: 30 | - uses: actions/checkout@v2 31 | 32 | - name: Install packages 33 | run: | 34 | sudo apt update 35 | sudo apt install libudev-dev 36 | 37 | - name: Build 38 | run: cargo build --verbose 39 | 40 | - name: Run tests 41 | run: cargo test --verbose 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | 3 | ### Created by https://www.gitignore.io 4 | ### Rust ### 5 | # Generated by Cargo 6 | # will have compiled files and executables 7 | **/target/ 8 | 9 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 10 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 11 | Cargo.lock 12 | 13 | ### VisualStudioCode ### 14 | .vscode/* 15 | !.vscode/settings.json 16 | !.vscode/tasks.json 17 | !.vscode/launch.json 18 | *.code-workspace 19 | 20 | ### VisualStudioCode Patch ### 21 | # Ignore all local history of files 22 | .history 23 | .ionide 24 | 25 | ### Krita files 26 | *~ 27 | 28 | ### MacOS files 29 | *.DS_Store 30 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "esp8266-wifi-serial" 3 | version = "0.1.3" 4 | authors = ["Aleksei Sidorov "] 5 | edition = "2018" 6 | 7 | license = "MIT OR Apache-2.0" 8 | description = "A driver to work with the esp8266 module over the serial port." 9 | documentation = "https://docs.rs/esp8266-wifi-serial/" 10 | repository = "https://github.com/alekseysidorov/esp8266-wifi-serial" 11 | keywords = ["no_std", "esp8266", "wifi", "driver", "network"] 12 | categories = ["no-std", "embedded", "network-programming"] 13 | 14 | [dependencies] 15 | embedded-hal = "0.2" 16 | heapless = "0.7" 17 | nb = "1" 18 | no-std-net = "0.5" 19 | no-stdout = "0.1.0" 20 | nom = { version = "6.1", default-features = false } 21 | serde = { version = "1", default-features = false, features = ["derive"] } 22 | simple-clock = "0.1" 23 | 24 | [dev-dependencies] 25 | anyhow = "1.0" 26 | assert_matches = "1" 27 | once_cell = "1" 28 | serialport = "4.0" 29 | 30 | [features] 31 | integration_tests = [] 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Aleksey Sidorov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) Ulrik Sverdrup "bluss" 2015-2017 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Continuous integration](https://github.com/alekseysidorov/esp8266-wifi-serial/actions/workflows/rust.yml/badge.svg)](https://github.com/alekseysidorov/esp8266-wifi-serial/actions/workflows/rust.yml) 2 | [![Crates.io](https://img.shields.io/crates/v/esp8266-wifi-serial)](https://crates.io/crates/esp8266-wifi-serial) 3 | [![API reference](https://docs.rs/esp8266-wifi-serial/badge.svg)](https://docs.rs/esp8266-wifi-serial) 4 | 5 | # esp8266-wifi-serial 6 | 7 | (WIP) Driver to work with the esp8266 module over the serial port. 8 | 9 | By using this module you can join the existing access point or creating your own. After a network creation, the module can both listen to incoming TCP connections or connect to other sockets. 10 | 11 | ```rust 12 | let mut module = Module::new(rx, tx, clock).expect("unable to create module"); 13 | // Create a new access point. 14 | let mut session = SoftApConfig { 15 | ssid: "test_network", 16 | password: "12345678", 17 | channel: 4, 18 | mode: WifiMode::Open, 19 | } 20 | .start(module) 21 | .expect("unable to start network sesstion"); 22 | // Start listening for incoming connections on the specified port. 23 | session.listen(2048).unwrap(); 24 | // Start an event loop. 25 | loop { 26 | let event = nb::block!(session.poll_network_event()).expect("unable to poll network event"); 27 | // Some business logic. 28 | } 29 | ``` 30 | 31 | ***Warning:** this library is not finished yet and it is not worth using it in mission-critical software, it can burn your hamster.* 32 | 33 | The crate was been tested with the `gd32vf103` board. 34 | 35 | I will be happy to see new contributions to the development of this crate. 36 | -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | /// Possible error types that may happen during manipulating the WiFi module. 2 | /// 3 | /// In order to the crate interface simplification, error details have been omitted. 4 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] 5 | pub enum Error { 6 | /// An error occurred during the receiving bytes from the serial port. 7 | ReadBuffer, 8 | /// An error occurred during the sending bytes into the serial port. 9 | WriteBuffer, 10 | /// Reader buffer is full. 11 | BufferFull, 12 | /// Operation timeout reached. 13 | Timeout, 14 | /// Unable to join selected access point. 15 | JoinApError, 16 | } 17 | 18 | /// A specialized result type for the operations with the esp8266 module. 19 | pub type Result = core::result::Result; 20 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(not(test), no_std)] 2 | 3 | //! Driver to working with the esp8266 module over the serial port. 4 | //! 5 | //! # Warning 6 | //! 7 | //! This library is not completed and lack core features and has a lot of bugs and imperfections. 8 | //! And so, it is not ready for production purposes. 9 | 10 | pub use crate::{ 11 | error::{Error, Result}, 12 | module::{AtCommand, Module}, 13 | network_session::{NetworkEvent, NetworkSession}, 14 | reader_part::ReadData, 15 | softap::{JoinApConfig, SoftApConfig, WifiMode}, 16 | }; 17 | pub use no_std_net as net; 18 | 19 | pub use simple_clock as clock; 20 | 21 | mod error; 22 | mod module; 23 | mod network_session; 24 | mod parser; 25 | mod reader_part; 26 | mod softap; 27 | 28 | #[cfg(test)] 29 | mod tests; 30 | -------------------------------------------------------------------------------- /src/module.rs: -------------------------------------------------------------------------------- 1 | use core::fmt::Write; 2 | 3 | use embedded_hal::serial; 4 | use simple_clock::{Deadline, ElapsedTimer, SimpleClock}; 5 | 6 | use crate::{ 7 | error::{Error, Result}, 8 | parser::CifsrResponse, 9 | reader_part::{ReadData, ReaderPart}, 10 | }; 11 | 12 | const RESET_DELAY_US: u64 = 3_000_000; 13 | 14 | /// Raw response to a sent AT command. 15 | pub type RawResponse<'a, const N: usize> = core::result::Result, ReadData<'a, N>>; 16 | 17 | /// The trait describes how to send a certain AT command. 18 | pub trait AtCommand: private::Sealed { 19 | /// Sends the AT command and gets a corresponding response. 20 | #[doc(hidden)] 21 | fn send( 22 | self, 23 | module: &mut Module, 24 | ) -> Result> 25 | where 26 | Rx: serial::Read + 'static, 27 | Tx: serial::Write + 'static, 28 | C: SimpleClock; 29 | } 30 | 31 | impl AtCommand for &str { 32 | fn send( 33 | self, 34 | module: &mut Module, 35 | ) -> Result> 36 | where 37 | Rx: serial::Read + 'static, 38 | Tx: serial::Write + 'static, 39 | C: SimpleClock, 40 | { 41 | module.send_at_command_str(self) 42 | } 43 | } 44 | 45 | impl AtCommand for core::fmt::Arguments<'_> { 46 | fn send( 47 | self, 48 | module: &mut Module, 49 | ) -> Result> 50 | where 51 | Rx: serial::Read + 'static, 52 | Tx: serial::Write + 'static, 53 | C: SimpleClock, 54 | { 55 | module.send_at_command_fmt(self) 56 | } 57 | } 58 | 59 | const NEWLINE: &[u8] = b"\r\n"; 60 | 61 | /// Basic communication interface with the esp8266 module. 62 | /// 63 | /// Provides basic functionality for sending AT commands and getting corresponding responses. 64 | #[derive(Debug)] 65 | pub struct Module 66 | where 67 | Rx: serial::Read + 'static, 68 | Tx: serial::Write + 'static, 69 | C: SimpleClock, 70 | { 71 | pub(crate) reader: ReaderPart, 72 | pub(crate) writer: WriterPart, 73 | pub(crate) clock: C, 74 | pub(crate) timeout: Option, 75 | } 76 | 77 | impl<'a, Rx, Tx, C, const N: usize> Module 78 | where 79 | Rx: serial::Read + 'static, 80 | Tx: serial::Write + 'static, 81 | C: SimpleClock, 82 | { 83 | /// Establishes serial communication with the esp8266 module. 84 | pub fn new(rx: Rx, tx: Tx, clock: C) -> Result { 85 | let mut module = Self { 86 | reader: ReaderPart::new(rx), 87 | writer: WriterPart { tx }, 88 | clock, 89 | timeout: None, 90 | }; 91 | module.init()?; 92 | Ok(module) 93 | } 94 | 95 | fn init(&mut self) -> Result<()> { 96 | self.disable_echo()?; 97 | Ok(()) 98 | } 99 | 100 | fn reset_cmd(&mut self) -> Result<()> { 101 | self.write_command(b"AT+RST")?; 102 | 103 | // Workaround to ignore the framing errors. 104 | let timer = ElapsedTimer::new(&self.clock); 105 | while timer.elapsed() < RESET_DELAY_US { 106 | core::hint::spin_loop(); 107 | } 108 | 109 | self.read_until(ReadyCondition)?; 110 | 111 | Ok(()) 112 | } 113 | 114 | /// Sets the operation timeout to the timeout specified. 115 | /// 116 | /// If the specified value is `None`, the operations will block infinitely. 117 | pub fn set_timeout(&mut self, us: Option) { 118 | self.timeout = us; 119 | } 120 | 121 | /// Performs the module resetting routine. 122 | pub fn reset(&mut self) -> Result<()> { 123 | // FIXME: It is ok to receive errors like "framing" during the reset procedure. 124 | self.reset_cmd().ok(); 125 | // Workaround to catch the framing errors. 126 | for _ in 0..100 { 127 | self.send_at_command_str("ATE1").ok(); 128 | } 129 | 130 | self.disable_echo()?; 131 | Ok(()) 132 | } 133 | 134 | /// Sends an AT command and gets the response for it. 135 | pub fn send_at_command(&mut self, cmd: T) -> Result> { 136 | cmd.send(self) 137 | } 138 | 139 | fn send_at_command_str(&mut self, cmd: &str) -> Result> { 140 | self.write_command(cmd.as_ref())?; 141 | self.read_until(OkCondition) 142 | } 143 | 144 | fn send_at_command_fmt(&mut self, args: core::fmt::Arguments) -> Result> { 145 | self.write_command_fmt(args)?; 146 | self.read_until(OkCondition) 147 | } 148 | 149 | fn disable_echo(&mut self) -> Result<()> { 150 | self.send_at_command_str("ATE0").map(drop) 151 | } 152 | 153 | fn write_command(&mut self, cmd: &[u8]) -> Result<()> { 154 | self.writer.write_bytes(cmd)?; 155 | self.writer.write_bytes(NEWLINE) 156 | } 157 | 158 | pub(crate) fn write_command_fmt(&mut self, args: core::fmt::Arguments) -> Result<()> { 159 | self.writer.write_fmt(args)?; 160 | self.writer.write_bytes(NEWLINE) 161 | } 162 | 163 | pub(crate) fn read_until<'b, T>(&'b mut self, condition: T) -> Result 164 | where 165 | T: Condition<'b, N>, 166 | { 167 | let clock = &self.clock; 168 | let deadline = self.timeout.map(|timeout| Deadline::new(clock, timeout)); 169 | 170 | loop { 171 | match self.reader.read_bytes() { 172 | Ok(_) => { 173 | if self.reader.buf().is_full() { 174 | return Err(Error::BufferFull); 175 | } 176 | } 177 | Err(nb::Error::WouldBlock) => {} 178 | Err(nb::Error::Other(_)) => { 179 | return Err(Error::ReadBuffer); 180 | } 181 | }; 182 | 183 | if condition.is_performed(&self.reader.buf()) { 184 | break; 185 | } 186 | 187 | if let Some(deadline) = deadline.as_ref() { 188 | deadline.reached().map_err(|_| Error::Timeout)?; 189 | } 190 | } 191 | 192 | let read_data = ReadData::new(self.reader.buf_mut()); 193 | Ok(condition.output(read_data)) 194 | } 195 | 196 | pub(crate) fn get_network_info(&mut self) -> Result { 197 | // Get assigned SoftAP address. 198 | let res = self.send_at_command("AT+CIFSR")?; 199 | let raw_resp = res.expect("Malformed command"); 200 | 201 | let resp = CifsrResponse::parse(&raw_resp) 202 | .unwrap_or_else(|| panic!("Unable to parse response: {:?}", raw_resp)) 203 | .1; 204 | Ok(resp) 205 | } 206 | } 207 | 208 | pub(crate) trait Condition<'a, const N: usize>: Copy { 209 | type Output: 'a; 210 | 211 | fn is_performed(self, buf: &[u8]) -> bool; 212 | 213 | fn output(self, buf: ReadData<'a, N>) -> Self::Output; 214 | } 215 | 216 | #[derive(Clone, Copy)] 217 | struct ReadyCondition; 218 | 219 | impl ReadyCondition { 220 | const MSG: &'static [u8] = b"ready\r\n"; 221 | } 222 | 223 | impl<'a, const N: usize> Condition<'a, N> for ReadyCondition { 224 | type Output = ReadData<'a, N>; 225 | 226 | fn is_performed(self, buf: &[u8]) -> bool { 227 | buf.ends_with(Self::MSG) 228 | } 229 | 230 | fn output(self, mut buf: ReadData<'a, N>) -> Self::Output { 231 | buf.subslice(0, buf.len() - Self::MSG.len()); 232 | buf 233 | } 234 | } 235 | 236 | #[derive(Clone, Copy)] 237 | pub(crate) struct CarretCondition; 238 | 239 | impl CarretCondition { 240 | const MSG: &'static [u8] = b"> "; 241 | } 242 | 243 | impl<'a, const N: usize> Condition<'a, N> for CarretCondition { 244 | type Output = ReadData<'a, N>; 245 | 246 | fn is_performed(self, buf: &[u8]) -> bool { 247 | buf.ends_with(Self::MSG) 248 | } 249 | 250 | fn output(self, mut buf: ReadData<'a, N>) -> Self::Output { 251 | buf.subslice(0, buf.len() - Self::MSG.len()); 252 | buf 253 | } 254 | } 255 | 256 | #[derive(Clone, Copy)] 257 | pub(crate) struct OkCondition; 258 | 259 | impl OkCondition { 260 | const OK: &'static [u8] = b"OK\r\n"; 261 | const ERROR: &'static [u8] = b"ERROR\r\n"; 262 | const FAIL: &'static [u8] = b"FAIL\r\n"; 263 | } 264 | 265 | // TODO optimize this condition. 266 | impl<'a, const N: usize> Condition<'a, N> for OkCondition { 267 | type Output = RawResponse<'a, N>; 268 | 269 | fn is_performed(self, buf: &[u8]) -> bool { 270 | buf.ends_with(Self::OK) || buf.ends_with(Self::ERROR) || buf.ends_with(Self::FAIL) 271 | } 272 | 273 | fn output(self, mut buf: ReadData<'a, N>) -> Self::Output { 274 | if buf.ends_with(Self::OK) { 275 | buf.subslice(0, buf.len() - Self::OK.len()); 276 | Ok(buf) 277 | } else if buf.ends_with(Self::ERROR) { 278 | buf.subslice(0, buf.len() - Self::ERROR.len()); 279 | Err(buf) 280 | } else { 281 | buf.subslice(0, buf.len() - Self::FAIL.len()); 282 | Err(buf) 283 | } 284 | } 285 | } 286 | 287 | #[derive(Debug)] 288 | pub struct WriterPart { 289 | tx: Tx, 290 | } 291 | 292 | impl WriterPart 293 | where 294 | Tx: serial::Write + 'static, 295 | { 296 | fn write_fmt(&mut self, args: core::fmt::Arguments) -> Result<()> { 297 | let writer = &mut self.tx as &mut (dyn serial::Write + 'static); 298 | writer.write_fmt(args).map_err(|_| Error::WriteBuffer) 299 | } 300 | 301 | pub(crate) fn write_byte(&mut self, byte: u8) -> nb::Result<(), Error> { 302 | self.tx 303 | .write(byte) 304 | .map_err(|err| err.map(|_| Error::WriteBuffer)) 305 | } 306 | 307 | fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> { 308 | for byte in bytes.iter() { 309 | nb::block!(self.write_byte(*byte))?; 310 | } 311 | Ok(()) 312 | } 313 | } 314 | 315 | mod private { 316 | pub trait Sealed {} 317 | 318 | impl Sealed for &str {} 319 | impl Sealed for core::fmt::Arguments<'_> {} 320 | } 321 | -------------------------------------------------------------------------------- /src/network_session.rs: -------------------------------------------------------------------------------- 1 | use core::format_args; 2 | 3 | use embedded_hal::serial; 4 | use heapless::Vec; 5 | use simple_clock::SimpleClock; 6 | 7 | use crate::{ 8 | module::{CarretCondition, Module, OkCondition}, 9 | net::{IpAddr, SocketAddr}, 10 | parser::CommandResponse, 11 | reader_part::{ReadData, ReaderPart}, 12 | Error, 13 | }; 14 | 15 | /// Network session information. 16 | #[derive(Debug, PartialEq, Eq)] 17 | pub struct SessionInfo { 18 | pub softap_address: Option, 19 | pub listen_address: Option, 20 | } 21 | 22 | /// A session with the typical network operations. 23 | #[derive(Debug)] 24 | pub struct NetworkSession 25 | where 26 | Rx: serial::Read + 'static, 27 | Tx: serial::Write + 'static, 28 | C: SimpleClock, 29 | { 30 | module: Module, 31 | } 32 | 33 | impl NetworkSession 34 | where 35 | Rx: serial::Read + 'static, 36 | Tx: serial::Write + 'static, 37 | C: SimpleClock, 38 | { 39 | pub(crate) fn new(module: Module) -> Self { 40 | Self { module } 41 | } 42 | 43 | /// Begins to listen to the incoming TCP connections on the specified port. 44 | pub fn listen(&mut self, port: u16) -> crate::Result<()> { 45 | // Setup a TCP server. 46 | self.module 47 | .send_at_command(format_args!("AT+CIPSERVER=1,{}", port))? 48 | .expect("Malformed command"); 49 | 50 | Ok(()) 51 | } 52 | 53 | /// Establishes a TCP connection with the specified IP address, link identifier will 54 | /// be associated with the given IP address. 55 | /// Then it will be possible to [send](Self::send) data using this link ID. 56 | pub fn connect(&mut self, link_id: usize, address: SocketAddr) -> crate::Result<()> { 57 | self.module 58 | .send_at_command(format_args!( 59 | "AT+CIPSTART={},\"{}\",\"{}\",{}", 60 | link_id, 61 | "TCP", 62 | address.ip(), 63 | address.port(), 64 | ))? 65 | .expect("Malformed command"); 66 | 67 | Ok(()) 68 | } 69 | 70 | /// Non-blocking polling to get a new network event. 71 | pub fn poll_network_event(&mut self) -> nb::Result, Error> { 72 | let reader = self.reader_mut(); 73 | 74 | let response = 75 | CommandResponse::parse(reader.buf()).map(|(remainder, event)| (remainder.len(), event)); 76 | 77 | if let Some((remaining_bytes, response)) = response { 78 | let pos = reader.buf().len() - remaining_bytes; 79 | truncate_buf(reader.buf_mut(), pos); 80 | 81 | let event = match response { 82 | CommandResponse::Connected { link_id } => NetworkEvent::Connected { link_id }, 83 | CommandResponse::Closed { link_id } => NetworkEvent::Closed { link_id }, 84 | CommandResponse::DataAvailable { link_id, size } => { 85 | let current_pos = reader.buf().len(); 86 | for _ in current_pos..size as usize { 87 | let byte = nb::block!(reader.read_byte())?; 88 | reader.buf_mut().push(byte).map_err(|_| Error::BufferFull)?; 89 | } 90 | 91 | NetworkEvent::DataAvailable { 92 | link_id, 93 | data: ReadData::new(reader.buf_mut()), 94 | } 95 | } 96 | CommandResponse::WifiDisconnect => return Err(nb::Error::WouldBlock), 97 | }; 98 | 99 | return Ok(event); 100 | } 101 | 102 | reader.read_bytes()?; 103 | Err(nb::Error::WouldBlock) 104 | } 105 | 106 | /// Sends data packet via the TCP socket with the link given identifier. 107 | /// 108 | /// # Notes 109 | /// 110 | /// No more than 2048 bytes can be sent at a time. 111 | pub fn send(&mut self, link_id: usize, bytes: I) -> crate::Result<()> 112 | where 113 | I: Iterator + ExactSizeIterator, 114 | { 115 | let bytes_len = bytes.len(); 116 | // TODO Implement sending of the whole bytes by splitting them into chunks. 117 | assert!( 118 | bytes_len < 2048, 119 | "Total packet size should not be greater than the 2048 bytes" 120 | ); 121 | assert!(self.reader().buf().is_empty()); 122 | 123 | self.module 124 | .write_command_fmt(format_args!("AT+CIPSEND={},{}", link_id, bytes_len))?; 125 | self.module.read_until(CarretCondition)?; 126 | 127 | for byte in bytes { 128 | nb::block!(self.module.writer.write_byte(byte))?; 129 | } 130 | 131 | self.module 132 | .read_until(OkCondition)? 133 | .expect("Malformed command"); 134 | Ok(()) 135 | } 136 | 137 | /// Gets network session information. 138 | pub fn get_info(&mut self) -> crate::Result { 139 | let info = self.module.get_network_info()?; 140 | Ok(SessionInfo { 141 | softap_address: info.ap_ip, 142 | listen_address: info.sta_ip, 143 | }) 144 | } 145 | 146 | /// Returns a reference to underlying clock instance. 147 | pub fn clock(&self) -> &C { 148 | &self.module.clock 149 | } 150 | 151 | /// Returns an operations timeout. 152 | pub fn timeout(&self) -> Option { 153 | self.module.timeout 154 | } 155 | 156 | fn reader(&self) -> &ReaderPart { 157 | &self.module.reader 158 | } 159 | 160 | fn reader_mut(&mut self) -> &mut ReaderPart { 161 | &mut self.module.reader 162 | } 163 | } 164 | 165 | /// Incoming network event. 166 | #[derive(Debug)] 167 | pub enum NetworkEvent<'a, const N: usize> { 168 | /// A new peer connected. 169 | Connected { 170 | /// Connection identifier. 171 | link_id: u16, 172 | }, 173 | /// The connection with the peer is closed. 174 | Closed { 175 | /// Connection identifier. 176 | link_id: u16, 177 | }, 178 | /// Bytes received from the peer. 179 | DataAvailable { 180 | /// Connection identifier. 181 | link_id: u16, 182 | /// Received data. 183 | data: ReadData<'a, N>, 184 | }, 185 | } 186 | 187 | // FIXME: Reduce complexity of this operation. 188 | fn truncate_buf(buf: &mut Vec, at: usize) { 189 | let buf_len = buf.len(); 190 | 191 | assert!(at <= buf_len); 192 | 193 | for from in at..buf_len { 194 | let to = from - at; 195 | buf[to] = buf[from]; 196 | } 197 | 198 | // Safety: `u8` is aprimitive type and doesn't have drop implementation so we can just 199 | // modify the buffer length. 200 | unsafe { 201 | buf.set_len(buf_len - at); 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /src/parser.rs: -------------------------------------------------------------------------------- 1 | use core::str::FromStr; 2 | 3 | use nom::{alt, char, character::streaming::digit1, do_parse, named, opt, tag, IResult}; 4 | 5 | use crate::net::{IpAddr, Ipv4Addr}; 6 | 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] 8 | pub enum CommandResponse { 9 | Connected { link_id: u16 }, 10 | Closed { link_id: u16 }, 11 | DataAvailable { link_id: u16, size: u64 }, 12 | WifiDisconnect, 13 | } 14 | 15 | fn parse_error(input: &[u8]) -> nom::Err> { 16 | nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Digit)) 17 | } 18 | 19 | fn atoi(input: &[u8]) -> Result>> { 20 | let s = core::str::from_utf8(input).map_err(|_| parse_error(input))?; 21 | s.parse().map_err(|_| parse_error(input)) 22 | } 23 | 24 | fn parse_link_id(input: &[u8]) -> IResult<&[u8], u16> { 25 | let (input, digits) = digit1(input)?; 26 | let num = atoi(digits)?; 27 | IResult::Ok((input, num)) 28 | } 29 | 30 | fn parse_u64(input: &[u8]) -> IResult<&[u8], u64> { 31 | let (input, digits) = digit1(input)?; 32 | let num = atoi(digits)?; 33 | IResult::Ok((input, num)) 34 | } 35 | 36 | fn parse_u8(input: &[u8]) -> IResult<&[u8], u8> { 37 | let (input, digits) = digit1(input)?; 38 | let num = atoi(digits)?; 39 | IResult::Ok((input, num)) 40 | } 41 | 42 | named!(crlf, tag!("\r\n")); 43 | 44 | named!( 45 | connected, 46 | do_parse!( 47 | opt!(crlf) 48 | >> link_id: parse_link_id 49 | >> tag!(",CONNECT") 50 | >> crlf 51 | >> (CommandResponse::Connected { link_id }) 52 | ) 53 | ); 54 | 55 | named!( 56 | closed, 57 | do_parse!( 58 | opt!(crlf) 59 | >> link_id: parse_link_id 60 | >> tag!(",CLOSED") 61 | >> crlf 62 | >> (CommandResponse::Closed { link_id }) 63 | ) 64 | ); 65 | 66 | named!( 67 | data_available, 68 | do_parse!( 69 | opt!(crlf) 70 | >> tag!("+IPD,") 71 | >> link_id: parse_link_id 72 | >> char!(',') 73 | >> size: parse_u64 74 | >> char!(':') 75 | >> opt!(crlf) 76 | >> (CommandResponse::DataAvailable { link_id, size }) 77 | ) 78 | ); 79 | 80 | named!( 81 | wifi_disconnect, 82 | do_parse!( 83 | opt!(crlf) >> tag!("WIFI DISCONNECT") >> opt!(crlf) >> (CommandResponse::WifiDisconnect) 84 | ) 85 | ); 86 | 87 | named!( 88 | parse, 89 | alt!(connected | closed | data_available | wifi_disconnect) 90 | ); 91 | 92 | impl CommandResponse { 93 | pub fn parse(input: &[u8]) -> Option<(&[u8], Self)> { 94 | parse(input).ok() 95 | } 96 | } 97 | 98 | pub struct CifsrResponse { 99 | pub ap_ip: Option, 100 | pub sta_ip: Option, 101 | } 102 | 103 | named!( 104 | parse_ip4_addr, 105 | do_parse!( 106 | opt!(crlf) 107 | >> a: parse_u8 108 | >> char!('.') 109 | >> b: parse_u8 110 | >> char!('.') 111 | >> c: parse_u8 112 | >> char!('.') 113 | >> d: parse_u8 114 | >> (IpAddr::V4(Ipv4Addr::new(a, b, c, d))) 115 | ) 116 | ); 117 | 118 | named!( 119 | parse_apip, 120 | do_parse!( 121 | opt!(crlf) 122 | >> tag!("+CIFSR:APIP,") 123 | >> char!('"') 124 | >> ip_addr: parse_ip4_addr 125 | >> char!('"') 126 | >> opt!(crlf) 127 | >> (ip_addr) 128 | ) 129 | ); 130 | 131 | named!( 132 | parse_staip, 133 | do_parse!( 134 | opt!(crlf) 135 | >> tag!("+CIFSR:STAIP,") 136 | >> char!('"') 137 | >> ip_addr: parse_ip4_addr 138 | >> char!('"') 139 | >> opt!(crlf) 140 | >> (ip_addr) 141 | ) 142 | ); 143 | 144 | named!( 145 | cifsr_response, 146 | do_parse!( 147 | opt!(crlf) 148 | >> ap_ip: opt!(parse_apip) 149 | >> sta_ip: opt!(parse_staip) 150 | >> (CifsrResponse { ap_ip, sta_ip }) 151 | ) 152 | ); 153 | 154 | impl CifsrResponse { 155 | pub fn parse(input: &[u8]) -> Option<(&[u8], Self)> { 156 | cifsr_response(input).ok() 157 | } 158 | } 159 | 160 | #[test] 161 | fn test_parse_connect() { 162 | let raw = b"1,CONNECT\r\n"; 163 | let event = CommandResponse::parse(raw.as_ref()).unwrap().1; 164 | 165 | assert_eq!(event, CommandResponse::Connected { link_id: 1 }) 166 | } 167 | 168 | #[test] 169 | fn test_parse_close() { 170 | let raw = b"1,CLOSED\r\n"; 171 | let event = CommandResponse::parse(raw.as_ref()).unwrap().1; 172 | 173 | assert_eq!(event, CommandResponse::Closed { link_id: 1 }) 174 | } 175 | 176 | #[test] 177 | fn test_parse_data_available() { 178 | let raw = b"+IPD,12,6:hello\r\n"; 179 | let event = CommandResponse::parse(raw.as_ref()).unwrap().1; 180 | 181 | assert_eq!( 182 | event, 183 | CommandResponse::DataAvailable { 184 | link_id: 12, 185 | size: 6 186 | } 187 | ) 188 | } 189 | -------------------------------------------------------------------------------- /src/reader_part.rs: -------------------------------------------------------------------------------- 1 | //! Reader part of the esp8266 WiFi implementation. 2 | 3 | use core::{ 4 | fmt::{self, Write}, 5 | ops::Deref, 6 | }; 7 | 8 | use embedded_hal::serial; 9 | use heapless::Vec; 10 | 11 | use crate::Error; 12 | 13 | #[derive(Debug)] 14 | pub(crate) struct ReaderPart { 15 | rx: Rx, 16 | buf: Vec, 17 | } 18 | 19 | impl ReaderPart { 20 | pub fn buf(&self) -> &Vec { 21 | &self.buf 22 | } 23 | 24 | pub fn buf_mut(&mut self) -> &mut Vec { 25 | &mut self.buf 26 | } 27 | } 28 | 29 | impl ReaderPart 30 | where 31 | Rx: serial::Read + 'static, 32 | { 33 | pub fn new(rx: Rx) -> Self { 34 | Self { 35 | rx, 36 | buf: Vec::new(), 37 | } 38 | } 39 | 40 | pub fn read_byte(&mut self) -> nb::Result { 41 | self.rx.read().map_err(|x| x.map(|_| Error::ReadBuffer)) 42 | } 43 | 44 | pub fn read_bytes(&mut self) -> nb::Result<(), crate::Error> { 45 | loop { 46 | if self.buf.is_full() { 47 | return Err(nb::Error::WouldBlock); 48 | } 49 | 50 | let byte = self.read_byte()?; 51 | // Safety: we have already checked if this buffer is full, 52 | // a couple of lines above. 53 | unsafe { 54 | self.buf.push_unchecked(byte); 55 | } 56 | } 57 | } 58 | } 59 | 60 | /// Buffer with the incoming data received from the module over the serial port. 61 | /// 62 | /// A user should handle this data, otherwise, it will be discarded. 63 | pub struct ReadData<'a, const N: usize> { 64 | inner: &'a mut Vec, 65 | from: usize, 66 | to: usize, 67 | } 68 | 69 | struct PrintAscii<'a>(&'a [u8]); 70 | 71 | impl<'a> fmt::Debug for PrintAscii<'a> { 72 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 73 | f.write_char('"')?; 74 | for byte in self.0 { 75 | f.write_char(*byte as char)?; 76 | } 77 | f.write_char('"')?; 78 | 79 | Ok(()) 80 | } 81 | } 82 | 83 | impl<'a, const N: usize> fmt::Debug for ReadData<'a, N> { 84 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 85 | f.debug_struct("ReadData") 86 | .field("from", &self.from) 87 | .field("to", &self.to) 88 | .field("data", &PrintAscii(self.as_ref())) 89 | .finish() 90 | } 91 | } 92 | 93 | impl<'a, const N: usize> ReadData<'a, N> { 94 | pub(crate) fn new(inner: &'a mut Vec) -> Self { 95 | let to = inner.len(); 96 | Self { inner, from: 0, to } 97 | } 98 | 99 | pub(crate) fn subslice(&mut self, from: usize, to: usize) { 100 | self.from = from; 101 | self.to = to; 102 | } 103 | } 104 | 105 | impl<'a, const N: usize> AsRef<[u8]> for ReadData<'a, N> { 106 | fn as_ref(&self) -> &[u8] { 107 | &self.inner[self.from..self.to] 108 | } 109 | } 110 | 111 | impl<'a, const N: usize> Drop for ReadData<'a, N> { 112 | fn drop(&mut self) { 113 | self.inner.clear() 114 | } 115 | } 116 | 117 | impl<'a, const N: usize> Deref for ReadData<'a, N> { 118 | type Target = [u8]; 119 | 120 | fn deref(&self) -> &Self::Target { 121 | self.inner.as_ref() 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/softap.rs: -------------------------------------------------------------------------------- 1 | use core::{fmt::Debug, format_args}; 2 | 3 | use embedded_hal::serial; 4 | use serde::{Deserialize, Serialize}; 5 | use simple_clock::SimpleClock; 6 | 7 | use crate::{Error, Module, NetworkSession}; 8 | 9 | /// WiFi modes that supported by this module. 10 | #[repr(u8)] 11 | #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize, Eq)] 12 | pub enum WifiMode { 13 | /// Open network mode without any encryption. 14 | Open = 0, 15 | /// WPA PSK encryption mode. 16 | WpaPsk = 2, 17 | /// WPA2 PSK encryption mode. 18 | Wpa2Psk = 3, 19 | /// Both WPA PSK and WPA2 PSK encryption modes. 20 | WpaWpa2Psk = 4, 21 | } 22 | 23 | /// Software access point configuration parameters. 24 | #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize, Eq)] 25 | pub struct SoftApConfig<'a> { 26 | /// Access point SSID. 27 | pub ssid: &'a str, 28 | /// Access point password. 29 | /// 30 | /// This field will be ignored if WiFi mode is open. 31 | pub password: &'a str, 32 | /// Channel number. 33 | pub channel: u8, 34 | /// WiFi mode. 35 | pub mode: WifiMode, 36 | } 37 | 38 | impl<'a> SoftApConfig<'a> { 39 | /// Creates a software access point with the configuration parameters and establishes 40 | /// a new WiFi session. 41 | pub fn start( 42 | self, 43 | mut module: Module, 44 | ) -> crate::Result> 45 | where 46 | Rx: serial::Read + 'static, 47 | Tx: serial::Write + 'static, 48 | C: SimpleClock, 49 | { 50 | self.init(&mut module)?; 51 | Ok(NetworkSession::new(module)) 52 | } 53 | 54 | fn init( 55 | &self, 56 | module: &mut Module, 57 | ) -> crate::Result<()> 58 | where 59 | Rx: serial::Read + 'static, 60 | Tx: serial::Write + 'static, 61 | C: SimpleClock, 62 | { 63 | // Enable SoftAP+Station mode. 64 | module 65 | .send_at_command("AT+CWMODE=3")? 66 | .expect("Malformed command"); 67 | 68 | // Enable multiple connections. 69 | module 70 | .send_at_command("AT+CIPMUX=1")? 71 | .expect("Malformed command"); 72 | 73 | // Start SoftAP. 74 | module 75 | .send_at_command(format_args!( 76 | "AT+CWSAP=\"{}\",\"{}\",{},{}", 77 | self.ssid, self.password, self.channel, self.mode as u8, 78 | ))? 79 | .expect("Malformed command"); 80 | 81 | Ok(()) 82 | } 83 | } 84 | 85 | /// Configuration parameters describe a connection to the existing access point. 86 | #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] 87 | pub struct JoinApConfig<'a> { 88 | /// Access point SSID. 89 | pub ssid: &'a str, 90 | /// Access point password. 91 | pub password: &'a str, 92 | } 93 | 94 | impl<'a> JoinApConfig<'a> { 95 | /// Joins to the existing access point and establishing a new WiFi session. 96 | pub fn join( 97 | self, 98 | mut module: Module, 99 | ) -> crate::Result> 100 | where 101 | Rx: serial::Read + 'static, 102 | Tx: serial::Write + 'static, 103 | C: SimpleClock, 104 | { 105 | self.init(&mut module)?; 106 | Ok(NetworkSession::new(module)) 107 | } 108 | 109 | fn init( 110 | &self, 111 | module: &mut Module, 112 | ) -> crate::Result<()> 113 | where 114 | Rx: serial::Read + 'static, 115 | Tx: serial::Write + 'static, 116 | C: SimpleClock, 117 | { 118 | // Enable Station mode. 119 | module 120 | .send_at_command("AT+CWMODE=1")? 121 | .expect("Malformed command"); 122 | 123 | // Enable multiple connections. 124 | module 125 | .send_at_command("AT+CIPMUX=1")? 126 | .expect("Malformed command"); 127 | 128 | // Join the given access point. 129 | module 130 | .send_at_command(format_args!( 131 | "AT+CWJAP=\"{}\",\"{}\"", 132 | self.ssid, self.password, 133 | ))? 134 | .map_err(|_| Error::JoinApError)?; 135 | 136 | Ok(()) 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/tests.rs: -------------------------------------------------------------------------------- 1 | use crate::parser::CommandResponse; 2 | 3 | #[test] 4 | fn test_parse_connect() { 5 | let raw = b"1,CONNECT\r\n"; 6 | let event = CommandResponse::parse(raw.as_ref()).unwrap().1; 7 | 8 | assert_eq!(event, CommandResponse::Connected { link_id: 1 }) 9 | } 10 | 11 | #[test] 12 | fn test_parse_close() { 13 | let raw = b"1,CLOSED\r\n"; 14 | let event = CommandResponse::parse(raw.as_ref()).unwrap().1; 15 | 16 | assert_eq!(event, CommandResponse::Closed { link_id: 1 }) 17 | } 18 | 19 | #[test] 20 | fn test_parse_data_available() { 21 | let raw = b"+IPD,12,6:hello\r\n"; 22 | let event = CommandResponse::parse(raw.as_ref()).unwrap().1; 23 | 24 | assert_eq!( 25 | event, 26 | CommandResponse::DataAvailable { 27 | link_id: 12, 28 | size: 6 29 | } 30 | ) 31 | } 32 | -------------------------------------------------------------------------------- /tests/commands.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | io::Write, 3 | net::{IpAddr, SocketAddr, TcpStream}, 4 | str::FromStr, 5 | time::Duration, 6 | }; 7 | 8 | use assert_matches::assert_matches; 9 | use esp8266_wifi_serial::{JoinApConfig, NetworkEvent, SoftApConfig, WifiMode}; 10 | 11 | use common::default_esp8266_serial_module; 12 | 13 | use crate::common::necessary_env_var; 14 | 15 | mod common; 16 | 17 | #[test] 18 | #[cfg_attr( 19 | not(feature = "integration_tests"), 20 | ignore = "feature \"integration_tests\" is disabled." 21 | )] 22 | fn integration_test_init() -> anyhow::Result<()> { 23 | default_esp8266_serial_module().map(drop) 24 | } 25 | 26 | #[test] 27 | #[cfg_attr( 28 | not(feature = "integration_tests"), 29 | ignore = "feature \"integration_tests\" is disabled." 30 | )] 31 | fn integration_test_softap() { 32 | let module = default_esp8266_serial_module().expect("unable to create module"); 33 | 34 | let mut session = SoftApConfig { 35 | ssid: "test_network", 36 | password: "12345678", 37 | channel: 4, 38 | mode: WifiMode::Open, 39 | } 40 | .start(module) 41 | .expect("unable to start network sesstion"); 42 | 43 | session.listen(2048).unwrap(); 44 | } 45 | 46 | #[test] 47 | #[cfg_attr( 48 | not(feature = "integration_tests"), 49 | ignore = "feature \"integration_tests\" is disabled." 50 | )] 51 | fn integration_test_joinap_ok() { 52 | let module = default_esp8266_serial_module().expect("unable to create module"); 53 | 54 | let mut session = JoinApConfig { 55 | ssid: &necessary_env_var("ESP8266_WIFI_SERIAL_SSID"), 56 | password: &necessary_env_var("ESP8266_WIFI_SERIAL_PASSWORD"), 57 | } 58 | .join(module) 59 | .expect("unable to start network sesstion"); 60 | 61 | session.listen(2048).unwrap(); 62 | let info = session.get_info().unwrap(); 63 | // Get some time to a module to establish a TCP listener. 64 | std::thread::sleep(Duration::from_millis(50)); 65 | 66 | let addr = SocketAddr::new( 67 | IpAddr::from_str(&info.listen_address.unwrap().to_string()).unwrap(), 68 | 2048, 69 | ); 70 | let mut socket = TcpStream::connect(addr).expect("unable to connect with device"); 71 | 72 | assert_matches!( 73 | nb::block!(session.poll_network_event()).expect("unable to poll network event"), 74 | NetworkEvent::Connected { .. } 75 | ); 76 | 77 | let msg = b"Hello esp8266\n"; 78 | socket.write_all(msg).unwrap(); 79 | 80 | assert_matches!( 81 | nb::block!(session.poll_network_event()).expect("unable to poll network event"), 82 | NetworkEvent::DataAvailable { data, .. } => { 83 | assert_eq!(data.as_ref(), msg); 84 | } 85 | ); 86 | } 87 | 88 | #[test] 89 | #[cfg_attr( 90 | not(feature = "integration_tests"), 91 | ignore = "feature \"integration_tests\" is disabled." 92 | )] 93 | fn integration_test_joinap_fail() { 94 | let module = default_esp8266_serial_module().expect("unable to create module"); 95 | 96 | let err = JoinApConfig { 97 | ssid: "some weird network", 98 | password: "my password aaaa", 99 | } 100 | .join(module) 101 | .expect_err("joining to the AP should fail"); 102 | 103 | assert_eq!(err, esp8266_wifi_serial::Error::JoinApError); 104 | } 105 | -------------------------------------------------------------------------------- /tests/common/mod.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | cell::{Ref, RefCell, RefMut}, 3 | fmt::{self, Debug}, 4 | io, 5 | rc::Rc, 6 | sync::{Mutex, MutexGuard}, 7 | }; 8 | 9 | use embedded_hal::serial::{Read, Write}; 10 | use esp8266_wifi_serial::{clock::SimpleClock, Module}; 11 | use once_cell::sync::Lazy; 12 | use serialport::SerialPort; 13 | 14 | const BAUD_RATE: u32 = 115200; 15 | const ADAPTER_BUF_CAPACITY: usize = 2048; 16 | 17 | const DEFAULT_TIMEOUT_US: u64 = 30_000_000; 18 | const RESET_TIMEOUT_US: u64 = 200_000; 19 | 20 | #[derive(Debug)] 21 | struct ClockImpl; 22 | 23 | static ONCE_LOCK: Lazy> = Lazy::new(Mutex::default); 24 | 25 | pub fn from_debug(err: impl Debug) -> anyhow::Error { 26 | anyhow::format_err!("{:?}", err) 27 | } 28 | 29 | impl SimpleClock for ClockImpl { 30 | fn now_us(&self) -> u64 { 31 | let time = std::time::SystemTime::now() 32 | .duration_since(std::time::UNIX_EPOCH) 33 | .unwrap(); 34 | 35 | time.as_micros() as u64 36 | } 37 | } 38 | 39 | #[derive(Clone)] 40 | struct SerialPortWrapper { 41 | guard: Rc>, 42 | inner: Rc>>, 43 | } 44 | 45 | impl Debug for SerialPortWrapper { 46 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 47 | f.debug_struct("SerialPortWrapper").finish() 48 | } 49 | } 50 | 51 | impl SerialPortWrapper { 52 | fn new(port: Box) -> Self { 53 | Self { 54 | guard: Rc::new(ONCE_LOCK.lock().unwrap()), 55 | inner: Rc::new(RefCell::new(port)), 56 | } 57 | } 58 | 59 | fn borrow(&self) -> Ref> { 60 | self.inner.borrow() 61 | } 62 | 63 | fn borrow_mut(&self) -> RefMut> { 64 | self.inner.borrow_mut() 65 | } 66 | } 67 | 68 | impl Read for SerialPortWrapper { 69 | type Error = io::Error; 70 | 71 | fn read(&mut self) -> nb::Result { 72 | let bytes_available = self 73 | .borrow() 74 | .bytes_to_read() 75 | .map_err(io::Error::from) 76 | .map_err(nb::Error::from)?; 77 | 78 | if bytes_available == 0 { 79 | return Err(nb::Error::WouldBlock); 80 | } 81 | 82 | let mut buf = [0; 1]; 83 | self.borrow_mut().read(&mut buf).map_err(nb::Error::from)?; 84 | eprint!("{}", buf[0] as char); 85 | Ok(buf[0]) 86 | } 87 | } 88 | 89 | impl Write for SerialPortWrapper { 90 | type Error = io::Error; 91 | 92 | fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { 93 | eprint!("{}", word as char); 94 | self.borrow_mut() 95 | .write(&[word]) 96 | .map_err(nb::Error::Other) 97 | .map(drop) 98 | } 99 | 100 | fn flush(&mut self) -> nb::Result<(), Self::Error> { 101 | self.borrow_mut().flush().map_err(nb::Error::Other) 102 | } 103 | } 104 | 105 | fn default_serial_port() -> anyhow::Result { 106 | let info = serialport::available_ports()? 107 | .into_iter() 108 | .next() 109 | .ok_or_else(|| anyhow::format_err!("There is no available serial device."))?; 110 | 111 | let port = SerialPortWrapper::new(serialport::new(info.port_name, BAUD_RATE).open()?); 112 | Ok(port) 113 | } 114 | 115 | pub fn default_esp8266_serial_module() -> anyhow::Result< 116 | Module< 117 | impl Read + Debug, 118 | impl Write + Debug, 119 | impl SimpleClock + Debug, 120 | ADAPTER_BUF_CAPACITY, 121 | >, 122 | > { 123 | let rx = default_serial_port()?; 124 | let tx = rx.clone(); 125 | 126 | let mut module = Module::new(rx, tx, ClockImpl).map_err(from_debug)?; 127 | module.set_timeout(Some(RESET_TIMEOUT_US)); 128 | module.reset().map_err(from_debug)?; 129 | module.set_timeout(Some(DEFAULT_TIMEOUT_US)); 130 | 131 | Ok(module) 132 | } 133 | 134 | pub fn necessary_env_var(name: &str) -> String { 135 | std::env::var(name).unwrap_or_else(|_| panic!("a `{}` variable is necessary.", name)) 136 | } 137 | --------------------------------------------------------------------------------