diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.lock | 154 | ||||
-rw-r--r-- | Cargo.toml | 13 | ||||
-rw-r--r-- | LICENSE | 340 | ||||
-rw-r--r-- | README.md | 28 | ||||
-rw-r--r-- | build.rs | 19 | ||||
-rw-r--r-- | src/kiss.rs | 183 | ||||
-rw-r--r-- | src/kissattach.c | 137 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 111 | ||||
-rw-r--r-- | src/rfm95.rs | 656 |
11 files changed, 1650 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f8710f8 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,154 @@ +[[package]] +name = "bitflags" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byteorder" +version = "1.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bytes" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cc" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "gcc" +version = "0.3.54" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "iovec" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.43" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nix" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nix" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "raspi-rfm95-kiss" +version = "0.1.0" +dependencies = [ + "cc 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "spidev 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sysfs_gpio 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc_version" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "spidev" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sysfs_gpio" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nix 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "32866f4d103c4e438b1db1158aa1b1a80ee078e5d77a59a2f906fd62a577389c" +"checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3" +"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90492c5858dd7d2e78691cfb89f90d273a2800fc11d98f60786e5d87e2f83781" +"checksum bytes 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e178b8e0e239e844b083d5a0d4a156b2654e67f9f80144d48398fcd736a24fb8" +"checksum cc 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)" = "4a6007c146fdd28d4512a794b07ffe9d8e89e6bf86e2e0c4ddff2e1fb54a0007" +"checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3" +"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" +"checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" +"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" +"checksum nix 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b7fd5681d13fda646462cfbd4e5f2051279a89a544d50eb98c365b507246839f" +"checksum nix 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a7bb1da2be7da3cbffda73fc681d509ffd9e665af478d2bee1907cee0bc64b2" +"checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084" +"checksum semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac" +"checksum spidev 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1ba01d3ef92a37e898fecac76cd3e1b33c999395e2d70787608d9678c4293e04" +"checksum sysfs_gpio 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3d68f2cae3c7d39f54ce8a858cc31ffb01974744ee65e5b4999b6037cd691e3f" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5827d1c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "raspi-rfm95-kiss" +version = "0.1.0" +authors = ["Matthias P. Braendli <matthias.braendli@mpb.li>"] + +[build-dependencies] +cc = "1.0" + +[dependencies] +libc = "0.2" +#wiringpi = "0.2" +spidev = "0.3.0" +sysfs_gpio = "0.5" @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..bfbfcd5 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +raspi-rfm95-kiss +================ + +Use an RFM95 LoRa module connected to spidev0.0 as an AX.25 interface. The whole chain this code implements is: + +AX.25 kernel interface - KISS protocol over a PTY pair - KISS protocol implementation - RFM95 driver - SPI + +TODO +==== + +* [ ] KISS protocol implementation +* [ ] RFM95 send +* [ ] RFM95 recv in polling mode +* [ ] RFM95 recv in GPIO interrupt mode + + +License +------- + +Parts of this code are taken from the ax25-tools, which are GPL-2.0. Other parts are MIT or Apache 2.0 licensed. + +New code is Copyright 2018 Matthias P. Braendli, GPL-2.0-only + + +Misc remarks +------------ + +1. (Not needed) To build with wiringpi in dev mode: `cargo build --features wiringpi/development` diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..5e14209 --- /dev/null +++ b/build.rs @@ -0,0 +1,19 @@ +// Copyright 2018 Matthias P. Braendli +// SPDX-License-Identifier: GPL-2.0-only + +extern crate cc; + +fn main() { + cc::Build::new() + .file("src/kissattach.c") + .compile("kissattach"); + + println!("cargo:rustc-link-lib=ax25"); + + /* + .file("bar.c") + .define("FOO", Some("bar")) + .include("src") + .compile("foo"); + */ +} diff --git a/src/kiss.rs b/src/kiss.rs new file mode 100644 index 0000000..397922d --- /dev/null +++ b/src/kiss.rs @@ -0,0 +1,183 @@ +// Taken from ax25-rs, which is +// SPDX-License-Identifier: Apache-2.0 +use std::io; +use std::io::prelude::*; +use std::io::{Read, Write}; + +const FEND: u8 = 0xC0; +const FESC: u8 = 0xDB; +const TFEND: u8 = 0xDC; +const TFESC: u8 = 0xDD; + +pub struct KissDecoder<'a> { + stream: &'a Read, + buffer: Vec<u8> +} + +impl<'a> KissDecoder<'a> { + pub fn new<T>(stream: &T) -> io::Result<KissDecoder> + where T: Read + { + Ok(KissDecoder { + stream: stream, + buffer: Vec::new() + }) + } + + pub fn receive_frame(&mut self) -> io::Result<Vec<u8>> { + loop { + if let Some(frame) = make_frame_from_buffer(&mut self.buffer) { + return Ok(frame); + } + let mut buf = vec![0u8; 1024]; + let n_bytes = self.stream.read(&mut buf)?; + self.buffer.extend(buf.iter().take(n_bytes)); + } + } +} + +/* +pub struct KissEncoder { + stream: Write, + buffer: Vec<u8> +} + +impl KissEncoder { + pub fn new(stream: Write) -> io::Result<KissEncoder> { + Ok(KissEncoder { + stream: stream, + buffer: Vec::new() + }) + } + + pub fn send_frame(&mut self, frame: &[u8]) -> io::Result<()> { + // 0x00 is the KISS command byte, which is two nybbles + // port = 0 + // command = 0 (all following bytes are a data frame to transmit) + self.stream.write(&[FEND, 0x00])?; + self.stream.write(frame)?; + self.stream.write(&[FEND])?; + self.stream.flush()?; + Ok(()) + } +} + +fn make_frame_from_buffer(buffer: &mut Vec<u8>) -> Option<Vec<u8>> { + let mut possible_frame = Vec::new(); + + enum Scan { + LookingForStartMarker, + Data, + Escaped + } + let mut state = Scan::LookingForStartMarker; + let mut final_idx = 0; + + // Check for possible frame read-only until we know we have a complete frame + // If we take one out, clear out buffer up to the final index + for (idx, &c) in buffer.iter().enumerate() { + match state { + Scan::LookingForStartMarker => { + if c == FEND { + state = Scan::Data; + } + }, + Scan::Data => { + if c == FEND { + if !possible_frame.is_empty() { + // Successfully read a non-zero-length frame + final_idx = idx; + break; + } + } else if c == FESC { + state = Scan::Escaped; + } else { + possible_frame.push(c); + } + }, + Scan::Escaped => { + if c == TFEND { + possible_frame.push(FEND); + } else if c == TFESC { + possible_frame.push(FESC); + } else if c == FEND { + if !possible_frame.is_empty() { + // Successfully read a non-zero-length frame + final_idx = idx; + break; + } + } + state = Scan::Data; + } + } + } + + match final_idx { + 0 => None, + n => { + // Draining up to "n" will leave the final FEND in place + // This way we can use it as the start marker for the next frame + buffer.drain(0..n); + Some(possible_frame) + } + } +} + +#[test] +fn test_normal_frame() { + let mut rx = vec![FEND, 0x01, 0x02, FEND]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, 0x02])); + assert_eq!(rx, vec![FEND]); +} + +#[test] +fn test_trailing_data() { + let mut rx = vec![FEND, 0x01, 0x02, FEND, 0x03, 0x04]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, 0x02])); + assert_eq!(rx, vec![FEND, 0x03, 0x04]); +} + +#[test] +fn test_leading_data() { + let mut rx = vec![0x03, 0x04, FEND, 0x01, 0x02, FEND]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, 0x02])); + assert_eq!(rx, vec![FEND]); +} + +#[test] +fn test_consecutive_marker() { + let mut rx = vec![FEND, FEND, FEND, 0x01, 0x02, FEND]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, 0x02])); + assert_eq!(rx, vec![FEND]); +} + +#[test] +fn test_escapes() { + let mut rx = vec![FEND, 0x01, FESC, TFESC, 0x02, FESC, TFEND, 0x03, FEND]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, FESC, 0x02, FEND, 0x03])); + assert_eq!(rx, vec![FEND]); +} + +#[test] +fn test_incorrect_escape_skipped() { + let mut rx = vec![FEND, 0x01, FESC, 0x04, TFESC /* passes normally without leading FESC */, 0x02, FEND]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, TFESC, 0x02])); + assert_eq!(rx, vec![FEND]); +} + +#[test] +fn test_two_frames_single_fend() { + let mut rx = vec![FEND, 0x01, 0x02, FEND, 0x03, 0x04, FEND]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, 0x02])); + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x03, 0x04])); + assert_eq!(rx, vec![FEND]); +} + +#[test] +fn test_two_frames_double_fend() { + let mut rx = vec![FEND, 0x01, 0x02, FEND, FEND, 0x03, 0x04, FEND]; + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x01, 0x02])); + assert_eq!(make_frame_from_buffer(&mut rx), Some(vec![0x03, 0x04])); + assert_eq!(rx, vec![FEND]); +} +*/ diff --git a/src/kissattach.c b/src/kissattach.c new file mode 100644 index 0000000..74d4efb --- /dev/null +++ b/src/kissattach.c @@ -0,0 +1,137 @@ +// Taken from ax25-tools 0.0.10-rc4 kissattach.c +// SPDX-License-Identifier: GPL-2.0-only +#include <stdio.h> +#define __USE_XOPEN +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <termios.h> +#include <fcntl.h> +#include <signal.h> +#include <ctype.h> +#include <netdb.h> +#include <syslog.h> +#include <errno.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <sys/ioctl.h> + +#include <net/if.h> +#include <netax25/ax25.h> +#include <netrose/rose.h> + +#include <netax25/daemon.h> +#include <netax25/axlib.h> +#include <netax25/ttyutils.h> + + +static int setifcall(int fd, char *name) +{ + char call[7]; + + if (ax25_aton_entry(name, call) == -1) + return FALSE; + + if (ioctl(fd, SIOCSIFHWADDR, call) != 0) { + close(fd); + perror("SIOCSIFHWADDR"); + return FALSE; + } + + return TRUE; +} + + +static int startiface(char *dev, int mtu, int allow_broadcast) +{ + struct ifreq ifr; + int fd; + + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("socket"); + return FALSE; + } + + strcpy(ifr.ifr_name, dev); + + ifr.ifr_mtu = mtu; + + if (ioctl(fd, SIOCSIFMTU, &ifr) < 0) { + perror("SIOCSIFMTU"); + return FALSE; + } + + if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) { + perror("SIOCGIFFLAGS"); + return FALSE; + } + + ifr.ifr_flags &= IFF_NOARP; + ifr.ifr_flags |= IFF_UP; + ifr.ifr_flags |= IFF_RUNNING; + if (allow_broadcast) + ifr.ifr_flags |= IFF_BROADCAST; /* samba broadcasts are a pain.. */ + else + ifr.ifr_flags &= ~(IFF_BROADCAST); /* samba broadcasts are a pain.. */ + + if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) { + perror("SIOCSIFFLAGS"); + return FALSE; + } + + close(fd); + + return TRUE; +} + +int32_t kissattach( + char *callsign, + int32_t speed, + int32_t mtu, + char *kttyname, + int32_t allow_broadcast) +{ + int fd; + int disc = N_AX25; + char dev[64]; + + if ((fd = open(kttyname, O_RDONLY | O_NONBLOCK)) == -1) { + perror("open"); + return 0; + } + + if (speed != 0 && !tty_speed(fd, speed)) + return 0; + + if (ioctl(fd, TIOCSETD, &disc) == -1) { + //Error setting line discipline + perror("TIOCSETD"); + fprintf(stderr, "Are you sure you have enabled %s support in the kernel\n", + disc == N_AX25 ? "MKISS" : "6PACK"); + fprintf(stderr, "or, if you made it a module, that the module is loaded?\n"); + return 0; + } + + if (ioctl(fd, SIOCGIFNAME, dev) == -1) { + perror("SIOCGIFNAME"); + return 0; + } + + if (!setifcall(fd, callsign)) + return 0; + + /* Now set the encapsulation */ + int v = 4; + if (ioctl(fd, SIOCSIFENCAP, &v) == -1) { + perror("SIOCSIFENCAP"); + return 0; + } + + /* ax25 ifaces should not really need to have an IP address assigned to */ + if (!startiface(dev, mtu, allow_broadcast)) + return 0; + + return 1; +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..15e4720 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,7 @@ +// Copyright 2018 Matthias P. Braendli +// SPDX-License-Identifier: GPL-2.0-only +extern crate spidev; +extern crate sysfs_gpio; + +pub mod rfm95; +pub mod kiss; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9073975 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,111 @@ +// Copyright 2018 Matthias P. Braendli +// SPDX-License-Identifier: GPL-2.0-only +extern crate libc; + +extern crate raspi_rfm95_kiss; +use raspi_rfm95_kiss::rfm95::{RF95, Bandwidth, CodingRate, SpreadingFactor}; +use std::fs::{OpenOptions, File}; +use std::ffi::{CStr, CString}; +use std::os::unix::io::AsRawFd; + +const MAX_MTU: usize = 251; + +extern { + fn kissattach( + callsign: * const libc::c_char, + speed: libc::int32_t, + mtu: libc::int32_t, + kttyname: * const libc::c_char, + allow_broadcast: libc::int32_t) -> libc::int32_t; +} + +fn create_pts_pair() -> std::io::Result<File> { + let master_file = OpenOptions::new() + .read(true) + .write(true) + .open("/dev/ptmx")?; + + unsafe { + let master_fd = master_file.as_raw_fd(); + if libc::grantpt(master_fd) == -1 { + return Err(std::io::Error::last_os_error()); + } + if libc::unlockpt(master_fd) == -1 { + return Err(std::io::Error::last_os_error()); + } + } + + Ok(master_file) +} + +fn main() { + println!("Creating PTY pair"); + + let master_file = match create_pts_pair() { + Ok(fd) => fd, + Err(e) => panic!("create_pts_pair failed: {}", e) + }; + + let slavename; + unsafe { + slavename = libc::ptsname(master_file.as_raw_fd()); + } + + if slavename.is_null() { + panic!("Cannot get PTS slave name"); + } + unsafe { + let slice = CStr::from_ptr(slavename); + println!("PTS slave: {:?}", slice); + } + + match RF95::new(Bandwidth::Bw250, CodingRate::Cr8, SpreadingFactor::Sf10) { + Ok(mut radio) => { + radio.reset().expect("radio reset"); + + if let Ok(ver) = radio.get_version() { + println!("Device version: {:02x}", ver); + } + else { + println!("Cannot read device version"); + } + }, + Err(e) => panic!("Cannot create lora radio object, {}", e) + }; + + let callsign = CString::new("HB9EGM-1").expect("Failed to convert callsign to CString"); + let speed : i32 = 9600; + let mtu : i32 = 251; + let allow_broadcast : i32 = 1; + + let success = unsafe { + kissattach( + callsign.as_ptr(), + speed, + mtu, + slavename, + allow_broadcast) + }; + + if success == 0 { + panic!("kissattach failed"); + } + + let todo = "Read/Write from/to TTY and RFM95"; + + /* + let master_tty = Box::new(master_file); + + let writer = thread::spawn(|| { + loop { + let mut buffer = [0; MAX_MTU]; + match *master_tty.read(&mut buffer[..]) { + Ok(n) => radio.write_buffer(buffer), + Err(e) => panic!("TTY Read error {}", e); + } + } + }); + + writer.join().unwrap(); + */ +} diff --git a/src/rfm95.rs b/src/rfm95.rs new file mode 100644 index 0000000..f4da72b --- /dev/null +++ b/src/rfm95.rs @@ -0,0 +1,656 @@ +// Taken from rfm95_rust_pi, which is +// SPDX-License-Identifier: MIT +use std; +use sysfs_gpio; +use sysfs_gpio::{Direction, Edge, Pin}; +use spidev::{Spidev, SpidevOptions, SPI_MODE_0}; +use std::io; +use std::thread; +use std::time::Duration; +use std::io::{Read, Write, ErrorKind}; +use std::sync::mpsc::{Receiver}; +use std::sync::mpsc; +use std::sync::atomic::{AtomicBool, Ordering}; + +const RST_BCM_PIN : u64 = 17; +const DIO_BCM_PIN : u64 = 4; +const CS_BCM_PIN : u64 = 25; + +#[derive(Copy, Clone)] +pub enum LoraRegister { + RegFifo = 0x00, + RegOpMode = 0x01, + // reserved : 0x02 - 0x05 + RegFrfMsb = 0x06, + RegFrfMid = 0x07, + RegFrfLsb = 0x08, + RegPaConfig = 0x09, + RegPaRamp = 0x0A, + RegOcp = 0x0B, + RegLna = 0x0C, + RegFifoAddrPtr = 0x0D, + RegFifoTxBaseAddr = 0x0E, + RegFifoRxBaseAddr = 0x0F, + RegFifoRxCurrentAddr = 0x10, + RegIrqFlagsMask = 0x11, + RegIrqFlags = 0x12, + RegRxNbBytes = 0x13, + RegRxHeaderCntValueMsb = 0x14, + RegRxHeaderCntValueLsb = 0x15, + RegRxPacketCntValueMsb = 0x16, + RegRxPacketCntValueLsb = 0x17, + RegModemStat = 0x18, + RegPktSnrValue = 0x19, + RegPktRssiValue = 0x1A, + RegRssiValue = 0x1B, + RegHopChannel = 0x1C, + RegModemConfig1 = 0x1D, + RegModemConfig2 = 0x1E, + RegSymbTimeoutLsb = 0x1F, + RegPreambleMsb = 0x20, + RegPreambleLsb = 0x21, + RegPayloadLength = 0x22, + RegMaxPayloadLength = 0x23, + RegHopPeriod = 0x24, + RegFifoRxByteAddr = 0x25, + RegModemConfig3 = 0x26, + // reserved : 0x27-0x3F + RegDioMapping1 = 0x40, + RegDioMapping2 = 0x41, + RegVersion = 0x42, + // unused 0x44 + RegTcxo = 0x4B, + RegPaDac = 0x4D, + RegFormerTemp = 0x5B, + RegAgcRef = 0x61, + RegAgcThresh1 = 0x62, + RegAgcThresh2 = 0x63, + RegAgcThresh3 = 0x64, +} + +impl LoraRegister { + pub fn as_u8(&self) -> u8 { + *self as u8 + } +} + +#[derive(Copy, Clone)] +pub enum FskOokRegister { + RegFifo = 0x00, + RegOpMode = 0x01, + RegBitRateMsb = 0x02, + RegBitRateLsb = 0x03, + RegFdevMsb = 0x04, + RegFdevLsb = 0x05, + RegFrfMsb = 0x06, + RegFrfMid = 0x07, + RegFrfLsb = 0x08, + RegPaConfig = 0x09, + RegPaRamp = 0x0A, + RegOcp = 0x0B, + RegLna = 0x0C, + RegRxConfig = 0x0D, + RegRssiConfig = 0x0E, + RegRssiCollision = 0x0F, + RegRssiThresh = 0x10, + RegRssiValue = 0x11, + RegRxBw = 0x12, + RegAfcBw = 0x13, + RegOokPeak = 0x14, + RegOokFix = 0x15, + RegOokAvg = 0x16, + RegAfcFei = 0x1A, + RegAfcMsb = 0x1B, + RegAfcLsb = 0x1C, + RegFeiMsb = 0x1D, + RegFeiLsb = 0x1E, + RegPreambleDetect = 0x1F, + RegRxTimeout1 = 0x20, + RegRxTimeout2 = 0x21, + RegRxTimeout3 = 0x22, + RegRxDelay = 0x23, + RegOsc = 0x24, + RegPreambleMsb = 0x25, + RegPreambleLsb = 0x26, + RegSyncConfig = 0x27, + RegSyncValue1 = 0x28, + RegSyncValue2 = 0x29, + RegSyncValue3 = 0x2A, + RegSyncValue4 = 0x2B, + RegSyncValue5 = 0x2C, + RegSyncValue6 = 0x2D, + RegSyncValue7 = 0x2E, + RegSyncValue8 = 0x2F, + RegPacketconfig1 = 0x30, + RegPacketConfig2 = 0x31, + RegPayloadLength = 0x32, + RegNodeAdrs = 0x33, + RegBroadcastAdrs = 0x34, + RegFifoThresh = 0x35, + RegSeqConfig1 = 0x36, + RegSeqConfig2 = 0x37, + RegTimerResol = 0x38, + RegTimer1Coef = 0x39, + RegTimer2Coef = 0x3A, + RegImageCal = 0x3B, + RegTemp = 0x3C, + RegLowBat = 0x3D, + RegIrqFlags1 = 0x3E, + RegIrqFlags2 = 0x3F, + RegDioMapping1 = 0x40, + RegDioMapping2 = 0x41, + RegVersion = 0x42, + RegPllHop = 0x44, + RegTxco = 0x4B, + RegPaDac = 0x4D, + RegFormerTemp = 0x5B, + RegBitRateFrac = 0x5D, + RegAgcRef = 0x61, + RegAgcThresh1 = 0x62, + RegAgcThresh2 = 0x63, + RegAgcThresh3 = 0x64, +} + +impl FskOokRegister { + pub fn as_u8(&self) -> u8 { + *self as u8 + } +} + +#[derive(Copy, Clone)] +pub enum Channel { + Ch10 = 0xD84CCC, + Ch11 = 0xD86000, + Ch12 = 0xD87333, + Ch13 = 0xD88666, + Ch14 = 0xD89999, + Ch15 = 0xD8ACCC, + Ch16 = 0xD8C000, + Ch17 = 0xD90000, +} + +impl Channel { + pub fn msb(&self) -> u8 { + let chan_as_u8 = *self as u32; + ((chan_as_u8 >> 16) & 0x000000FF) as u8 + } + + pub fn mid(&self) -> u8 { + let chan_as_u8 = *self as u32; + ((chan_as_u8 >> 8) & 0x000000FF) as u8 + } + + pub fn lsb(&self) -> u8 { + let chan_as_u8 = *self as u32; + (chan_as_u8 & 0x000000FF) as u8 + } +} + +#[derive(Copy, Clone)] +pub enum Bandwidth { + Bw125 = 0x07, + Bw250 = 0x08, + Bw500 = 0x09, +} + +impl Bandwidth { + pub fn as_u8(&self) -> u8 { + *self as u8 + } +} + +#[derive(Copy, Clone)] +pub enum CodingRate { + Cr5 = 0x01, + Cr6 = 0x02, + Cr7 = 0x03, + Cr8 = 0x04, +} + +impl CodingRate { + pub fn as_u8(&self) -> u8 { + *self as u8 + } +} + +#[derive(Copy, Clone)] +pub enum SpreadingFactor { + Sf7 = 0x07, + Sf8 = 0x08, + Sf9 = 0x09, + Sf10 = 0x0A, + Sf11 = 0x0B, + Sf12 = 0x0C, +} + +impl SpreadingFactor { + pub fn as_u8(&self) -> u8 { + *self as u8 + } +} + +#[derive(Copy, Clone)] +pub enum LoraMode { + Sleep = 0x80, + Standby = 0x81, + Tx = 0x83, + Rx = 0x85, + RxSingle = 0x86, + CadDetection = 0x87, + StandbyOokFsk = 0xC1, +} + +impl LoraMode { + pub fn as_u8(&self) -> u8 { + *self as u8 + } +} + +#[derive(Copy, Clone)] +pub enum DioFunction { + RxDone = 0x00, + TxDone = 0x01, +} + +impl DioFunction { + pub fn as_u8(&self) -> u8 { *self as u8 } +} + +trait ToFlag { + fn flag_enabled(&self, f : IrqFlagMasks) -> bool; +} + +#[derive(Copy, Clone)] +enum IrqFlagMasks { + CadDetected = 0x01, + FhssChangeChannel = 0x02, + CadDone = 0x04, + TxDone = 0x08, + ValidHeader = 0x10, + PayloadCrcError = 0x20, + RxDone = 0x40, + RxTimeout = 0x80, +} + +impl IrqFlagMasks { + pub fn as_u8(&self) -> u8 { *self as u8 } +} + +impl ToFlag for u8 { + fn flag_enabled(&self, f : IrqFlagMasks) -> bool { + self & f.as_u8() != 0 + } +} + +pub enum RF95EventType { + None, + DataReceived, + DataSent, + ErrorPinConfig, + ErrorTimedOut, + ErrorWrongCrc, + ErrorCommBus, +} + +pub struct RF95 { + ch : Channel, + bw : Bandwidth, + cr : CodingRate, + sf : SpreadingFactor, + crc_check_enabled : bool, + implicit_header_enabled : bool, + pwr_db : u8, + thread_run : std::sync::Arc<AtomicBool>, + thread_handle : Option<std::thread::JoinHandle<()>>, + + rst_pin : Pin, +} + +impl RF95 { + pub fn new(bw : Bandwidth, cr : CodingRate, sf : SpreadingFactor) -> io::Result<RF95> { + let tmp_rst_pin = Pin::new(RST_BCM_PIN); + tmp_rst_pin.set_direction(Direction::Low).unwrap(); + thread::sleep(Duration::from_millis(10)); + tmp_rst_pin.set_value(1).unwrap(); + + Ok( + RF95 { + ch : Channel::Ch10, + bw, + cr, + sf, + crc_check_enabled : false, + implicit_header_enabled : false, + pwr_db : 0, + thread_run : std::sync::Arc::new(AtomicBool::new(false)), + thread_handle : None, + rst_pin : tmp_rst_pin, + } + ) + } + + pub fn get_version(&mut self) -> io::Result<u8> { + let device_version = RF95::read_register(LoraRegister::RegVersion)?; + Ok(device_version) + } + + pub fn set_main_parameters(&mut self, + ch : Channel, + bw : Bandwidth, + cr : CodingRate, + sf : SpreadingFactor) -> io::Result<()> { + self.set_channel(ch)?; + self.set_bandwidth(bw)?; + self.set_coding_rate(cr)?; + self.set_spreading_factor(sf) + } + + pub fn set_channel(&mut self, ch : Channel) -> io::Result<()> { + self.set_mode(LoraMode::Sleep)?; + self.ch = ch; + RF95::write_register(LoraRegister::RegFrfMsb, ch.msb())?; + RF95::write_register(LoraRegister::RegFrfMid, ch.mid())?; + RF95::write_register(LoraRegister::RegFrfLsb, ch.lsb()) + } + + pub fn set_spreading_factor(&mut self, sf : SpreadingFactor) -> io::Result<()> { + self.set_mode(LoraMode::Sleep)?; + self.sf = sf; + let mut tmp = RF95::read_register(LoraRegister::RegModemConfig2)?; + tmp &= 0x0F; + tmp |= sf.as_u8() << 4; + RF95::write_register(LoraRegister::RegModemConfig2, tmp) + } + + pub fn set_bandwidth(&mut self, bw : Bandwidth) -> io::Result<()> { + self.set_mode(LoraMode::Sleep)?; + self.bw = bw; + let mut tmp = RF95::read_register(LoraRegister::RegModemConfig1)?; + tmp &= 0x0F; + tmp |= bw.as_u8() << 4; + RF95::write_register(LoraRegister::RegModemConfig1, tmp)?; + Ok(()) + } + + pub fn set_coding_rate(&mut self, cr : CodingRate) -> io::Result<()> { + self.set_mode(LoraMode::Sleep)?; + self.cr = cr; + let mut tmp = RF95::read_register(LoraRegister::RegModemConfig1)?; + tmp &= 0xF1; + tmp |= cr.as_u8() << 1; + RF95::write_register(LoraRegister::RegModemConfig1, tmp) + } + + pub fn enable_crc_check(&mut self, en : bool) -> io::Result<()> { + let mut tmp = RF95::read_register(LoraRegister::RegModemConfig2)?; + if en { + tmp |= 1 << 2; + } else { + tmp &= !(1 << 2); + } + self.crc_check_enabled = en; + RF95::write_register(LoraRegister::RegModemConfig2, tmp) + } + + pub fn enable_implicit_header(&mut self, en : bool) -> io::Result<()> { + let mut tmp = RF95::read_register(LoraRegister::RegModemConfig1)?; + if en { + tmp |= 0x01; + } else { + tmp &= !0x01; + } + self.implicit_header_enabled = en; + RF95::write_register(LoraRegister::RegModemConfig1, tmp) + } + + pub fn set_output_power(&mut self, pwr : u8) -> io::Result<()> { + let mut pwr = pwr; + if pwr > 20 { + pwr = 20; + } + self.pwr_db = pwr; + let out = (pwr - 2) & 0x0F; + let mut tmp = RF95::read_register(LoraRegister::RegPaConfig)?; + tmp |= 0x80; + tmp &= 0xF0; + tmp |= out & 0x0F; + RF95::write_register(LoraRegister::RegPaConfig, tmp) + } + + pub fn set_mode(&mut self, m : LoraMode) -> io::Result<()> { + RF95::write_register(LoraRegister::RegOpMode, m.as_u8()) + } + + pub fn listen_timed(&mut self, timeout : u32) -> io::Result<Receiver<RF95EventType>> { + let (sender, receiver) = mpsc::channel(); + let input = Pin::new(DIO_BCM_PIN); + + + + self.thread_run.store(true, Ordering::SeqCst); + let run = self.thread_run.clone(); + + self.thread_handle = Some(thread::spawn(move || { + input.with_exported(|| { + let timeout = std::time::Duration::from_secs(timeout as u64); + let start = std::time::Instant::now(); + match input.set_direction(Direction::In) { + Ok(_) => (), + Err(e) => { + sender.send(RF95EventType::ErrorPinConfig).unwrap(); + panic!("Error while setting DI0 pin direction : {:?}", e); + }, + }; + + match input.set_edge(Edge::RisingEdge) { + Ok(_) => (), + Err(e) => { + sender.send(RF95EventType::ErrorPinConfig).unwrap(); + panic!("Error while setting DI0 pin edge detection : {:?}", e); + } + }; + + let mut poller = match input.get_poller() { + Ok(p) => p, + Err(e) => { + sender.send(RF95EventType::ErrorPinConfig).unwrap(); + panic!("Error while creating poller on DI0 : {:?}", e); + } + }; + + while run.load(Ordering::SeqCst) { + match poller.poll(10).unwrap() { + Some(_) => { + let mut regv = match RF95::read_register(LoraRegister::RegIrqFlags) { + Ok(r) => r, + Err(_) => { + sender.send(RF95EventType::ErrorCommBus).unwrap(); + return Err(sysfs_gpio::Error::from(io::Error::new(io::ErrorKind::Other, format!("Reading irq flag register")))); + }, + }; + if regv.flag_enabled(IrqFlagMasks::CadDetected) { + regv = regv & !IrqFlagMasks::CadDetected.as_u8(); + } + if regv.flag_enabled(IrqFlagMasks::CadDone) { + regv = regv & !IrqFlagMasks::CadDone.as_u8(); + } + if regv.flag_enabled(IrqFlagMasks::FhssChangeChannel) { + regv = regv & !IrqFlagMasks::FhssChangeChannel.as_u8(); + } + if regv.flag_enabled(IrqFlagMasks::PayloadCrcError) { + sender.send(RF95EventType::ErrorWrongCrc).unwrap(); + regv = regv & !IrqFlagMasks::PayloadCrcError.as_u8(); + } + if regv.flag_enabled(IrqFlagMasks::RxDone) { + sender.send(RF95EventType::DataSent).unwrap(); + regv = regv & !IrqFlagMasks::RxDone.as_u8(); + } + if regv.flag_enabled(IrqFlagMasks::TxDone) { + sender.send(RF95EventType::DataReceived).unwrap(); + regv = regv & !IrqFlagMasks::TxDone.as_u8(); + } + if regv.flag_enabled(IrqFlagMasks::RxTimeout) { + sender.send(RF95EventType::ErrorTimedOut).unwrap(); + regv = regv & !IrqFlagMasks::RxTimeout.as_u8(); + } + if regv.flag_enabled(IrqFlagMasks::ValidHeader) { + regv = regv & !IrqFlagMasks::ValidHeader.as_u8(); + } + match RF95::write_register(LoraRegister::RegIrqFlags, regv) { + Ok(_) => (), + Err(_) => { + sender.send(RF95EventType::ErrorCommBus).unwrap(); + return Err(sysfs_gpio::Error::from(io::Error::new(io::ErrorKind::Other, format!("Reading irq flag register")))); + }, + }; + }, + None => (), + }; + if timeout.as_secs() > 0 { + if std::time::Instant::now().duration_since(start) > timeout { + break; + } + } + } + Ok(()) + }).expect("Cannot export gpio"); + })); + + Ok(receiver) + } + + pub fn listen_continuous(&mut self) -> io::Result<Receiver<RF95EventType>> { + self.listen_timed(0) + } + + pub fn stop_listening(&mut self) -> Result<(), String> { + self.thread_run.store(false, Ordering::SeqCst); + match self.thread_handle.take() { + Some(th) => { + match th.join() { + Ok(_) => return Ok(()), + Err(_) => return Err(format!("Cannot join spawned thread")), + }; + }, + None => return Err(format!("No thread spawned")), + }; + } + + pub fn get_snr(&mut self) -> io::Result<i16> { + Ok((RF95::read_register(LoraRegister::RegPktSnrValue)? as i16) / 4) + } + + pub fn get_packet_rssi(&mut self) -> io::Result<i16> { + Ok((RF95::read_register(LoraRegister::RegRssiValue)? as i16) - 137) + } + + pub fn get_rssi(&mut self) -> io::Result<i16> { + Ok((RF95::read_register(LoraRegister::RegRssiValue)? as i16) - 137) + } + + pub fn reset(&mut self) -> io::Result<()> { + match self.rst_pin.set_direction(Direction::Low) { + Ok(_) => (), + Err(_) => return Err(std::io::Error::new(ErrorKind::Other, "Problem setting value on gpio")), + }; + std::thread::sleep(Duration::new(0, 10000000)); + match self.rst_pin.set_value(1) { + Ok(_) => return Ok(()), + Err(_) => return Err(std::io::Error::new(ErrorKind::Other, "Problem setting value on gpio")), + }; + } + + pub fn set_dio_mapping(&mut self, df : DioFunction) -> io::Result<()> { + let prev_mode = RF95::read_register(LoraRegister::RegOpMode)?; + self.set_mode(LoraMode::StandbyOokFsk)?; + let mut tmp = RF95::read_register(LoraRegister::RegDioMapping1)?; + tmp &= 0x3F; + if df.as_u8() == DioFunction::TxDone.as_u8() { + tmp |= 0x40; + } + RF95::write_register(LoraRegister::RegDioMapping1, tmp)?; + RF95::write_register(LoraRegister::RegOpMode, prev_mode) + } + + fn write_register(reg : LoraRegister, data : u8) -> io::Result<()> { + let mut spi = Spidev::open("/dev/spidev0.0")?; + let spi_options = SpidevOptions::new() + .bits_per_word(8) + .max_speed_hz(5_000_000) + .mode(SPI_MODE_0) + .build(); + spi.configure(&spi_options)?; + + match spi.write(&[reg.as_u8(), data]) { + Ok(_) => Ok(()), + Err(_) => Err(io::Error::new(ErrorKind::Other, "Problem while writing to device")) + } + } + + fn write_buffer(reg : LoraRegister, buffer : Vec<u8>) -> io::Result<()> { + let mut spi = Spidev::open("/dev/spidev0.0")?; + let spi_options = SpidevOptions::new() + .bits_per_word(8) + .max_speed_hz(5_000_000) + .mode(SPI_MODE_0) + .build(); + spi.configure(&spi_options)?; + + let cs_pin = Pin::new(CS_BCM_PIN); + cs_pin.set_direction(Direction::High).unwrap(); + + let mut v2 = buffer; + v2.insert(0, reg.as_u8()); + cs_pin.set_value(0).unwrap(); + spi.write(&v2)?; + match cs_pin.set_value(1) { + Ok(_) => Ok(()), + Err(_) => Err(std::io::Error::new(ErrorKind::Other, "Problem setting value on gpio")), + } + } + + fn read_register(reg : LoraRegister) -> io::Result<u8> { + let mut spi = Spidev::open("/dev/spidev0.0")?; + let spi_options = SpidevOptions::new() + .bits_per_word(8) + .max_speed_hz(5_000_000) + .mode(SPI_MODE_0) + .build(); + spi.configure(&spi_options)?; + + let cs_pin = Pin::new(CS_BCM_PIN); + cs_pin.set_direction(Direction::High).unwrap(); + + let mut ret: [u8; 1] = [0; 1]; + cs_pin.set_value(0).unwrap(); + spi.write(&[reg.as_u8()])?; + spi.read(&mut ret).unwrap(); + match cs_pin.set_value(1) { + Ok(_) => (), + Err(_) => return Err(io::Error::new(ErrorKind::Other, "Problem setting value on gpio")), + }; + Ok(ret[0]) + } + + fn read_buffer(reg : LoraRegister, buffer : &mut Vec<u8>, length : u8) -> io::Result<()> { + let mut spi = Spidev::open("/dev/spidev0.0")?; + let spi_options = SpidevOptions::new() + .bits_per_word(8) + .max_speed_hz(5_000_000) + .mode(SPI_MODE_0) + .build(); + spi.configure(&spi_options)?; + + let cs_pin = Pin::new(CS_BCM_PIN); + cs_pin.set_direction(Direction::High).unwrap(); + + let mut tmp : Vec<u8> = Vec::with_capacity(length as usize); + cs_pin.set_value(0).unwrap(); + spi.write(&[reg.as_u8()])?; + spi.read(tmp.as_mut_slice())?; + buffer.clear(); + buffer.write(&tmp)?; + cs_pin.set_value(1).unwrap(); + Ok(()) + } +} |