├── .editorconfig ├── .gitignore ├── .travis.yml ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── blocks.rs ├── codewords ├── address_short.rs ├── biw1.rs ├── biw2.rs ├── biw3.rs ├── biw4.rs ├── codeword.rs ├── fiw.rs ├── message_alpha.rs ├── message_alpha_content.rs ├── message_alpha_header.rs ├── message_alpha_signature.rs ├── mod.rs └── vector_alpha.rs ├── frame.rs ├── helper ├── apply_bch_and_parity.rs ├── bch_calculator.rs ├── fourbit_checksum.rs ├── mod.rs └── parity.rs ├── lib.rs ├── main.rs └── message.rs /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | charset = utf-8 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | indent_style = space 9 | indent_size = 4 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /doc/ 3 | **/*.rs.bk 4 | .vscode 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | script: 3 | - cargo build --verbose --all 4 | - cargo test --verbose --all -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "bit_reverse" 3 | version = "0.1.7" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "chrono" 8 | version = "0.4.0" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | dependencies = [ 11 | "num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 12 | "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", 13 | ] 14 | 15 | [[package]] 16 | name = "dtoa" 17 | version = "0.4.2" 18 | source = "registry+https://github.com/rust-lang/crates.io-index" 19 | 20 | [[package]] 21 | name = "flex" 22 | version = "0.1.0" 23 | dependencies = [ 24 | "bit_reverse 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", 25 | "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 26 | "getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", 27 | "serde 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", 28 | "serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", 29 | "serde_json 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", 30 | ] 31 | 32 | [[package]] 33 | name = "getopts" 34 | version = "0.2.15" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | 37 | [[package]] 38 | name = "itoa" 39 | version = "0.3.4" 40 | source = "registry+https://github.com/rust-lang/crates.io-index" 41 | 42 | [[package]] 43 | name = "kernel32-sys" 44 | version = "0.2.2" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | dependencies = [ 47 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 48 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 49 | ] 50 | 51 | [[package]] 52 | name = "libc" 53 | version = "0.2.33" 54 | source = "registry+https://github.com/rust-lang/crates.io-index" 55 | 56 | [[package]] 57 | name = "num" 58 | version = "0.1.40" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | dependencies = [ 61 | "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", 62 | "num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", 63 | "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 64 | ] 65 | 66 | [[package]] 67 | name = "num-integer" 68 | version = "0.1.35" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | dependencies = [ 71 | "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 72 | ] 73 | 74 | [[package]] 75 | name = "num-iter" 76 | version = "0.1.34" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | dependencies = [ 79 | "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", 80 | "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 81 | ] 82 | 83 | [[package]] 84 | name = "num-traits" 85 | version = "0.1.40" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | 88 | [[package]] 89 | name = "quote" 90 | version = "0.3.15" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | 93 | [[package]] 94 | name = "redox_syscall" 95 | version = "0.1.31" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | 98 | [[package]] 99 | name = "serde" 100 | version = "1.0.20" 101 | source = "registry+https://github.com/rust-lang/crates.io-index" 102 | 103 | [[package]] 104 | name = "serde_derive" 105 | version = "1.0.21" 106 | source = "registry+https://github.com/rust-lang/crates.io-index" 107 | dependencies = [ 108 | "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 109 | "serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", 110 | "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", 111 | ] 112 | 113 | [[package]] 114 | name = "serde_derive_internals" 115 | version = "0.17.0" 116 | source = "registry+https://github.com/rust-lang/crates.io-index" 117 | dependencies = [ 118 | "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", 119 | "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", 120 | ] 121 | 122 | [[package]] 123 | name = "serde_json" 124 | version = "1.0.6" 125 | source = "registry+https://github.com/rust-lang/crates.io-index" 126 | dependencies = [ 127 | "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 128 | "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 129 | "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 130 | "serde 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", 131 | ] 132 | 133 | [[package]] 134 | name = "syn" 135 | version = "0.11.11" 136 | source = "registry+https://github.com/rust-lang/crates.io-index" 137 | dependencies = [ 138 | "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 139 | "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", 140 | "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 141 | ] 142 | 143 | [[package]] 144 | name = "synom" 145 | version = "0.11.3" 146 | source = "registry+https://github.com/rust-lang/crates.io-index" 147 | dependencies = [ 148 | "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 149 | ] 150 | 151 | [[package]] 152 | name = "time" 153 | version = "0.1.38" 154 | source = "registry+https://github.com/rust-lang/crates.io-index" 155 | dependencies = [ 156 | "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 157 | "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", 158 | "redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", 159 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 160 | ] 161 | 162 | [[package]] 163 | name = "unicode-xid" 164 | version = "0.0.4" 165 | source = "registry+https://github.com/rust-lang/crates.io-index" 166 | 167 | [[package]] 168 | name = "winapi" 169 | version = "0.2.8" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | 172 | [[package]] 173 | name = "winapi-build" 174 | version = "0.1.1" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | 177 | [metadata] 178 | "checksum bit_reverse 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5e97e02db5a2899c0377f3d6031d5da8296ca2b47abef6ed699de51b9e40a28c" 179 | "checksum chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c20ebe0b2b08b0aeddba49c609fe7957ba2e33449882cb186a180bc60682fa9" 180 | "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" 181 | "checksum getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)" = "65922871abd2f101a2eb0eaebadc66668e54a87ad9c3dd82520b5f86ede5eff9" 182 | "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" 183 | "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" 184 | "checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2" 185 | "checksum num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "a311b77ebdc5dd4cf6449d81e4135d9f0e3b153839ac90e648a8ef538f923525" 186 | "checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba" 187 | "checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01" 188 | "checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0" 189 | "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" 190 | "checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509" 191 | "checksum serde 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "5a2b181dd2b4a6353e828e44807269a761d3ecbc388a1f5ed3998ea69a516d9c" 192 | "checksum serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "652bc323d694dc925829725ec6c890156d8e70ae5202919869cb00fe2eff3788" 193 | "checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab" 194 | "checksum serde_json 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e4586746d1974a030c48919731ecffd0ed28d0c40749d0d18d43b3a7d6c9b20e" 195 | "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" 196 | "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" 197 | "checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520" 198 | "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" 199 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 200 | "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" 201 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "flex" 3 | version = "0.1.0" 4 | authors = ["chris "] 5 | 6 | [lib] 7 | name = "flexencoder" 8 | path = "src/lib.rs" 9 | 10 | [[bin]] 11 | name = "flex" 12 | path = "src/main.rs" 13 | 14 | [dependencies] 15 | bit_reverse = "0.1.7" 16 | getopts = "0.2.15" 17 | serde = "1.0" 18 | serde_json = "1.0" 19 | serde_derive = "1.0" 20 | chrono = "0.4.0" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flex 2 | 3 | [![Build Status](https://travis-ci.org/chris007de/flex.svg?branch=master)](https://travis-ci.org/chris007de/flex/builds) 4 | 5 | This is an Open-Source implementation of the FLEX paging protocol encoder by DL1COM and DC1MIL. 6 | 7 | This coder is based on US patent US55555183 and intended for non-commercial use only. 8 | All patent-related information is property of their respective owners, if still applicable. 9 | 10 | ## Implementation 11 | 12 | Current state of implementation: 13 | 14 | - Speed: 1600 15 | - Block Information Words 16 | - BIW 1 17 | - LocalID/Timezone 18 | - Day/Month/Year 19 | - Hour/Minute/Second 20 | - (Time and Date is currently hardcoded) 21 | - Message Types: 22 | - AlphaNum 23 | - Only "Short Address" CAPCODES supported 24 | - No inter-frame fragmentation possible, messages which do not fit into the current are sent in the next frame. 25 | Currently, it is possible to generate a FLEX bytestream from a JSON input file. 26 | 27 | ## Usage 28 | 29 | ``` 30 | flex --MODE -i INPUT -o OUTPUT 31 | 32 | Options: 33 | Modes: 34 | --single generate only frames for the messages given 35 | --hour generate a full hour of frames 36 | --continuous generate frames continuously to UDP 37 | 38 | -i, --input FILE.json 39 | set input file (JSON) 40 | -o, --output FILE set output file (FLEX bytestream) 41 | ``` 42 | 43 | ## JSON Message Format 44 | 45 | ```JSON 46 | [ 47 | { 48 | "frame":0, 49 | "msgtype":"AlphaNum", 50 | "capcode":123123, 51 | "data":"test1" 52 | }, 53 | { 54 | "frame":1, 55 | "msgtype":"AlphaNum", 56 | "capcode":123124, 57 | "data":"test2" 58 | } 59 | ] 60 | ``` 61 | 62 | ### Examples 63 | 64 | #### Operation Mode: single 65 | 66 | Generate only the frames for the messages contained in the JSON file. 67 | 68 | ```Shell 69 | flex --single -i messages.json -o messages.bin 70 | ``` 71 | 72 | 73 | #### Operation Mode: hour 74 | 75 | Generate a full hour of FLEX frames, containing the messages from the JSON file in the respective frames. 76 | 77 | ```Shell 78 | flex --hour -i messages.json -o messages.bin 79 | ``` 80 | 81 | #### Operation Mode: continuously 82 | 83 | Generates frames permanently, each 1.875 seconds and sends them to *127.0.0.1:51337*. 84 | There could be a GNU Radio UDP Source waiting to do modulate and send the frames. 85 | 86 | Input for frames could be done using a named pipe, e.g.: 87 | 88 | ```Shell 89 | mkfifo input.pipe 90 | flex --continuous -i input.pipe 91 | 92 | echo '[{"msgtype":"AlphaNum","capcode":123456,"data":"test","frame":1}]' > input.fifo 93 | ``` 94 | 95 | 96 | ### Build instructions 97 | 98 | ```Shell 99 | cargo build 100 | ``` 101 | 102 | ### Test 103 | 104 | 1. Generate FLEX bytestream (see examples above) 105 | 2. Modulate bytestream using 2-FSK modulator (e.g. https://github.com/dl0muc/gr-flexpager) 106 | 3. Send directly to pager using your favorite SDR or dump audio to raw audio file and decode with multimon-ng 107 | 108 | ``` 109 | multimon-ng -t raw -a FLEX -v 3 dump.raw 110 | ``` 111 | 112 | ## Usage of flexencoder crate 113 | 114 | ```Rust 115 | // Create a message 116 | let msg = Message::new(0, MessageType::AlphaNum, 0x123456, String::from("test")).unwrap(); 117 | 118 | // Create a frame 119 | let mut frame = Frame::new(cycle_nr, frame_nr).unwrap(); 120 | 121 | // Check if there is still space left in frame 122 | if msg.get_num_of_message_codewords().unwrap() > frame.space_left() { 123 | panic!("No space left in frame."); 124 | } 125 | 126 | // Add message to frame 127 | frame.add_message(&msg).unwrap(); 128 | 129 | // Get frame bytes to send them 130 | let bytes = frame.get_bytes(); 131 | ``` 132 | 133 | FLEX®, is a registered trademark of Motorola®, Incorporated. 134 | -------------------------------------------------------------------------------- /src/blocks.rs: -------------------------------------------------------------------------------- 1 | 2 | use codewords::codeword::Codeword; 3 | use codewords::biw1::BIW1; 4 | use codewords::biw2::BIW2; 5 | use codewords::biw3::BIW3; 6 | use codewords::biw4::BIW4; 7 | use codewords::address_short::CWAddressShort; 8 | use codewords::vector_alpha::CWVectorAlpha; 9 | use codewords::message_alpha::CWMessageAlpha; 10 | 11 | use message::Message; 12 | 13 | pub struct Blocks {} 14 | 15 | impl Blocks { 16 | pub fn get_bytes(msgs: &Vec, send_time: bool) -> Vec { 17 | 18 | let amount_address_cws = Blocks::count_address_cws(msgs); 19 | 20 | let biw_cws = Blocks::get_biw_cws(amount_address_cws, send_time); 21 | let addr_cws = Blocks::get_addr_cws(msgs); 22 | let (vector_cws, content_cws) = 23 | Blocks::get_vector_and_content_cws(msgs, biw_cws.len(), addr_cws.len()); 24 | 25 | let mut cws = Vec::new(); 26 | cws.extend_from_slice(&biw_cws); 27 | cws.extend_from_slice(&addr_cws); 28 | cws.extend_from_slice(&vector_cws); 29 | cws.extend_from_slice(&content_cws); 30 | 31 | cws = Blocks::fill_up_block_1600(&cws); 32 | 33 | let mut bytes = Vec::new(); 34 | for i in 0..11 { 35 | bytes.extend_from_slice(&Blocks::interleave_codewords_1600(&cws[i * 8..(i + 1) * 8])); 36 | } 37 | 38 | return bytes.to_vec(); 39 | } 40 | 41 | fn count_address_cws(msgs: &Vec) -> usize { 42 | // Currently, only Short Addresses supported, so 43 | // amount of address codwords is equal to amount of messages 44 | return msgs.len(); 45 | } 46 | 47 | fn get_biw_cws(amount_addresses: usize, send_time: bool) -> Vec { 48 | let mut startword_addresses = 0; 49 | if send_time { 50 | startword_addresses = 3; // offset because of BIW 2,3 and 4 51 | } 52 | 53 | let startword_vectors = 1 // BIW 1 54 | + startword_addresses + 55 | amount_addresses; 56 | 57 | let biw1 = BIW1::new( 58 | 0, 59 | startword_addresses as u32, 60 | startword_vectors as u32, 61 | 0, // No carry-on for now 62 | 0, 63 | ).unwrap(); // 0=pager decodes all frames 64 | 65 | let mut biw_cws = Vec::new(); 66 | biw_cws.push(biw1.get_codeword()); 67 | 68 | if send_time { 69 | let biw2 = BIW2::new(0, 0x1).unwrap(); 70 | let biw3 = BIW3::new(23, 05, 1999).unwrap(); 71 | let biw4 = BIW4::new(13, 37, 00).unwrap(); 72 | biw_cws.push(biw2.get_codeword()); 73 | biw_cws.push(biw3.get_codeword()); 74 | biw_cws.push(biw4.get_codeword()); 75 | } 76 | return biw_cws; 77 | } 78 | 79 | fn get_addr_cws(msgs: &Vec) -> Vec { 80 | let mut addr_cws = Vec::new(); 81 | for msg in msgs { 82 | let address = CWAddressShort::new(msg.capcode).unwrap(); 83 | addr_cws.push(address.get_codeword()); 84 | } 85 | return addr_cws; 86 | } 87 | 88 | fn get_vector_and_content_cws( 89 | msgs: &Vec, 90 | biw_cws_size: usize, 91 | addr_cws_size: usize, 92 | ) -> (Vec, Vec) { 93 | let biw_addr_vector_cws_size = biw_cws_size + 2 * addr_cws_size; // Address and Vector CWS 94 | 95 | let mut vector_cws: Vec = Vec::new(); 96 | let mut content_cws: Vec = Vec::new(); 97 | for msg in msgs { 98 | let content_start = biw_addr_vector_cws_size + content_cws.len(); 99 | let content_words = msg.get_num_of_content_codewords() as u32; 100 | 101 | let vector = CWVectorAlpha::new(content_start as u32, content_words).unwrap(); 102 | vector_cws.push(vector.get_codeword()); 103 | 104 | let content = CWMessageAlpha::new(0, msg.data.as_bytes()).unwrap(); 105 | content_cws.extend_from_slice(&content.get_codewords()); 106 | } 107 | 108 | return (vector_cws, content_cws); 109 | } 110 | 111 | fn fill_up_block_1600(cws: &Vec) -> Vec { 112 | let mut filled_cws = Vec::new(); 113 | filled_cws.extend_from_slice(&cws); 114 | while filled_cws.len() < 88 { 115 | if filled_cws.len() % 2 == 0 { 116 | filled_cws.push(0xFFFFFFFF); 117 | } else { 118 | filled_cws.push(0x0); 119 | } 120 | } 121 | return filled_cws; 122 | } 123 | 124 | fn interleave_codewords_1600(input: &[u32]) -> [u8; 32] { 125 | if input.len() != 8 { 126 | panic!("Exactly 8 input codewords required"); 127 | } 128 | 129 | let mut output_data: [u8; 32] = [0; 32]; 130 | for bit_index in 0..32 { 131 | for codeword in 0..8 { 132 | let input_mask = 1 << bit_index; 133 | let masked_input = &input[codeword] & input_mask; 134 | let backshifted_input = (masked_input >> bit_index) & 0x00000001; 135 | let read_bit: u8 = backshifted_input as u8; 136 | let bit_to_write: u8 = read_bit << codeword; 137 | output_data[bit_index] = output_data[bit_index] ^ bit_to_write; 138 | } 139 | } 140 | return output_data; 141 | } 142 | } 143 | 144 | #[cfg(test)] 145 | mod tests { 146 | use super::*; 147 | use message::MessageType; 148 | 149 | #[test] 150 | fn test_get_biw_cws_no_time() { 151 | let cws = Blocks::get_biw_cws(1, false); 152 | assert_eq!(cws.len(), 1); 153 | assert_eq!(cws[0] & 0x1FFFF0, 0x000800); 154 | } 155 | 156 | #[test] 157 | fn test_get_biw_cws_send_time() { 158 | let cws = Blocks::get_biw_cws(1, true); 159 | assert_eq!(cws.len(), 4); 160 | assert_eq!(cws[0] & 0x1FFFF0, 0x001700); 161 | } 162 | 163 | #[test] 164 | fn test_get_addr_cws() { 165 | let msg = Message::new(0, MessageType::AlphaNum, 0x0001, String::from("test")).unwrap(); 166 | let msgs = vec![msg]; 167 | let cws = Blocks::get_addr_cws(&msgs); 168 | assert_eq!(cws.len(), 1); 169 | assert_eq!(cws[0] & 0x1FFFFF, 0x8001); 170 | } 171 | 172 | #[test] 173 | fn test_get_vector_and_content_cws() { 174 | let msg = Message::new(0, MessageType::AlphaNum, 0x8001, String::from("test")).unwrap(); 175 | let msgs = vec![msg]; 176 | let (vector_cws, content_cws) = Blocks::get_vector_and_content_cws(&msgs, 1, msgs.len()); 177 | assert_eq!(vector_cws.len(), 1); 178 | assert_eq!(vector_cws[0] & 0x1FFFF0, 0x00C1D0); 179 | assert_eq!(content_cws.len(), 3); 180 | } 181 | 182 | #[test] 183 | fn test_fill_up_block_1600() { 184 | let cws = vec![0x0]; 185 | let filled_up = Blocks::fill_up_block_1600(&cws); 186 | assert_eq!(filled_up.len(), 88); 187 | } 188 | 189 | #[test] 190 | fn test_interleave_codewords_1600_all_zeroes() { 191 | 192 | let test_data: [u32; 8] = [0; 8]; 193 | let result = Blocks::interleave_codewords_1600(&test_data); 194 | 195 | assert_eq!(result, [0; 32]); 196 | } 197 | 198 | #[test] 199 | fn test_interleave_codewords_1600_all_ones() { 200 | 201 | let test_data: [u32; 8] = [::max_value(); 8]; 202 | let result = Blocks::interleave_codewords_1600(&test_data); 203 | 204 | assert_eq!(result, [::max_value(); 32]); 205 | } 206 | 207 | #[test] 208 | fn test_interleave_codewords_1600_single_one() { 209 | 210 | let test_data: [u32; 8] = [0x00000200, 0, 0, 0, 0, 0, 0, 0]; 211 | let result = Blocks::interleave_codewords_1600(&test_data); 212 | let mut test_helper: [u8; 32] = [0; 32]; 213 | test_helper[9] = 0x01; 214 | assert_eq!(result, test_helper); 215 | } 216 | 217 | #[test] 218 | fn test_interleave_codewords_1600_all_as() { 219 | 220 | let test_data: [u32; 8] = [0xaaaaaaaa; 8]; 221 | let result = Blocks::interleave_codewords_1600(&test_data); 222 | 223 | assert_eq!( 224 | result, 225 | [ 226 | 0x00, 227 | 0xff, 228 | 0x00, 229 | 0xff, 230 | 0x00, 231 | 0xff, 232 | 0x00, 233 | 0xff, 234 | 0x00, 235 | 0xff, 236 | 0x00, 237 | 0xff, 238 | 0x00, 239 | 0xff, 240 | 0x00, 241 | 0xff, 242 | 0x00, 243 | 0xff, 244 | 0x00, 245 | 0xff, 246 | 0x00, 247 | 0xff, 248 | 0x00, 249 | 0xff, 250 | 0x00, 251 | 0xff, 252 | 0x00, 253 | 0xff, 254 | 0x00, 255 | 0xff, 256 | 0x00, 257 | 0xff, 258 | ] 259 | ); 260 | } 261 | } 262 | -------------------------------------------------------------------------------- /src/codewords/address_short.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | 4 | const SHORT_ADDRESS_CAPCODE_OFFSET: u32 = 0x8000; 5 | 6 | pub struct CWAddressShort { 7 | capcode: u32, 8 | } 9 | 10 | impl CWAddressShort { 11 | pub fn new(capcode: u32) -> Result { 12 | if capcode >= 0x0001 && capcode <= 0x1EA7FF { 13 | Ok(CWAddressShort { capcode: capcode }) 14 | } else { 15 | Err("CAPCODE for short address out of range") 16 | } 17 | } 18 | } 19 | 20 | impl Codeword for CWAddressShort { 21 | fn get_codeword(&self) -> u32 { 22 | let mut cw: u32 = 0x0; 23 | cw |= self.capcode + SHORT_ADDRESS_CAPCODE_OFFSET; 24 | cw = apply_bch_and_parity(cw); 25 | return cw; 26 | } 27 | } 28 | 29 | #[cfg(test)] 30 | mod tests { 31 | use super::*; 32 | 33 | #[test] 34 | fn test_cw_address_short_get_codeword_when_first_capcode() { 35 | let cw_address_short = CWAddressShort::new(0x0001).unwrap(); 36 | assert_eq!(cw_address_short.get_codeword() & 0x1FFFFF, 0x008001) 37 | } 38 | 39 | #[test] 40 | fn test_cw_address_short_get_codeword_when_typical_with_crc() { 41 | let cw_address_short = CWAddressShort::new(0x0204).unwrap(); 42 | assert_eq!(cw_address_short.get_codeword(), 0xBF008204) 43 | } 44 | 45 | #[test] 46 | fn test_cw_address_short_get_codeword_when_highest_capcode_with_crc() { 47 | let cw_address_short = CWAddressShort::new(0x1EA7FF).unwrap(); 48 | assert_eq!(cw_address_short.get_codeword(), 0x13DF27FF) 49 | } 50 | 51 | #[test] 52 | fn test_cw_address_short_get_codeword_when_zero_capcode() { 53 | assert_eq!(CWAddressShort::new(0x0).is_err(), true); 54 | } 55 | 56 | #[test] 57 | fn test_cw_address_short_get_codeword_when_invalid_capcode() { 58 | assert_eq!(CWAddressShort::new(0x1EA800).is_err(), true); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/codewords/biw1.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | use helper::fourbit_checksum::apply_4bit_checksum; 4 | 5 | pub struct BIW1 { 6 | priority_addr: u32, 7 | end_of_block: u32, 8 | vector_field_start: u32, 9 | carry_on: u32, 10 | frame_id_collapse_mark: u32, 11 | } 12 | 13 | impl BIW1 { 14 | pub fn new( 15 | priority_addr: u32, 16 | end_of_block: u32, 17 | vector_field_start: u32, 18 | carry_on: u32, 19 | frame_id_collapse_mark: u32, 20 | ) -> Result { 21 | let biw1 = BIW1 { 22 | priority_addr: priority_addr & 0xF, 23 | end_of_block: end_of_block & 0x3, 24 | vector_field_start: vector_field_start & 0x3F, 25 | carry_on: carry_on & 0x3, 26 | frame_id_collapse_mark: frame_id_collapse_mark & 0x7, 27 | }; 28 | Ok(biw1) 29 | } 30 | } 31 | 32 | impl Codeword for BIW1 { 33 | fn get_codeword(&self) -> u32 { 34 | let mut cw: u32 = 0x0; 35 | cw |= self.priority_addr << 4; 36 | cw |= self.end_of_block << 8; 37 | cw |= self.vector_field_start << 10; 38 | cw |= self.carry_on << 16; 39 | cw |= self.frame_id_collapse_mark << 18; 40 | cw = apply_4bit_checksum(cw); 41 | cw = apply_bch_and_parity(cw); 42 | return cw; 43 | } 44 | } 45 | 46 | #[cfg(test)] 47 | mod tests { 48 | use super::*; 49 | 50 | #[test] 51 | fn test_codeword_biw1() { 52 | let biw1 = BIW1::new(10, 2, 60, 1, 6).unwrap(); 53 | assert_eq!(biw1.get_codeword() & 0x1FFFF0, 0x19F2A0); 54 | } 55 | 56 | #[test] 57 | fn test_codeword_biw1_crc() { 58 | let biw1 = BIW1::new(0, 0, 2, 0, 0).unwrap(); 59 | assert_eq!(biw1.get_codeword(), 0x19400807); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/codewords/biw2.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | use helper::fourbit_checksum::apply_4bit_checksum; 4 | 5 | pub struct BIW2 { 6 | local_id: u32, 7 | timezone: u32, 8 | } 9 | 10 | impl BIW2 { 11 | pub fn new(local_id: u32, timezone: u32) -> Result { 12 | let biw2 = BIW2 { 13 | local_id: local_id & 0x1FF, 14 | timezone: timezone & 0x1F, 15 | }; 16 | Ok(biw2) 17 | } 18 | } 19 | 20 | impl Codeword for BIW2 { 21 | fn get_codeword(&self) -> u32 { 22 | let mut cw: u32 = 0x0; 23 | cw |= self.timezone << 7; 24 | cw |= self.local_id << 12; 25 | cw = apply_4bit_checksum(cw); 26 | cw = apply_bch_and_parity(cw); 27 | return cw; 28 | } 29 | } 30 | 31 | #[cfg(test)] 32 | mod tests { 33 | use super::*; 34 | 35 | #[test] 36 | fn test_codeword_biw2() { 37 | let biw2 = BIW2::new(0x1FF, 1).unwrap(); 38 | assert_eq!(biw2.get_codeword() & 0x1FFFF0, 0x1FF080); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/codewords/biw3.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | use helper::fourbit_checksum::apply_4bit_checksum; 4 | 5 | pub struct BIW3 { 6 | year: u32, 7 | month: u32, 8 | day: u32, 9 | } 10 | 11 | impl BIW3 { 12 | pub fn new(day: u32, month: u32, year: u32) -> Result { 13 | if !(BIW3::check_day(day) && BIW3::check_month(month) && BIW3::check_year(year)) { 14 | Err("BIW3: Invalid date given") 15 | } else { 16 | let biw3 = BIW3 { 17 | year: year - 1994, 18 | month: month, 19 | day: day, 20 | }; 21 | Ok(biw3) 22 | } 23 | } 24 | 25 | fn check_day(day: u32) -> bool { 26 | return day >= 1 && day <= 31; 27 | } 28 | 29 | fn check_month(month: u32) -> bool { 30 | return month >= 1 && month <= 12; 31 | } 32 | 33 | fn check_year(year: u32) -> bool { 34 | return year >= 1994 && year <= 2025; 35 | } 36 | } 37 | 38 | impl Codeword for BIW3 { 39 | fn get_codeword(&self) -> u32 { 40 | let mut cw: u32 = 0x0; 41 | cw |= 0x1 << 4; 42 | cw |= self.year << 7; 43 | cw |= self.day << 12; 44 | cw |= self.month << 17; 45 | cw = apply_4bit_checksum(cw); 46 | cw = apply_bch_and_parity(cw); 47 | return cw; 48 | } 49 | } 50 | 51 | #[cfg(test)] 52 | mod tests { 53 | use super::*; 54 | 55 | #[test] 56 | fn test_codeword_biw3() { 57 | let biw3 = BIW3::new(31, 12, 1999).unwrap(); 58 | assert_eq!(biw3.get_codeword() & 0x1FFFF0, 0x19F290); 59 | } 60 | 61 | #[test] 62 | fn test_codeword_biw3_invalid_day() { 63 | assert_eq!(BIW3::new(32, 12, 1999).is_err(), true); 64 | } 65 | 66 | #[test] 67 | fn test_codeword_biw3_invalid_month() { 68 | assert_eq!(BIW3::new(31, 13, 1999).is_err(), true); 69 | } 70 | 71 | #[test] 72 | fn test_codeword_biw3_invalid_day_0() { 73 | assert_eq!(BIW3::new(0, 12, 1999).is_err(), true); 74 | } 75 | 76 | #[test] 77 | fn test_codeword_biw3_invalid_month_0() { 78 | assert_eq!(BIW3::new(31, 0, 1999).is_err(), true); 79 | } 80 | 81 | #[test] 82 | fn test_codeword_biw3_invalid_year_low() { 83 | assert_eq!(BIW3::new(1, 1, 1993).is_err(), true); 84 | } 85 | 86 | #[test] 87 | fn test_codeword_biw3_invalid_year_high() { 88 | assert_eq!(BIW3::new(1, 1, 2026).is_err(), true); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/codewords/biw4.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | use helper::fourbit_checksum::apply_4bit_checksum; 4 | 5 | pub struct BIW4 { 6 | hour: u32, 7 | minute: u32, 8 | second: u32, 9 | } 10 | 11 | impl BIW4 { 12 | pub fn new(hour: u32, minute: u32, second: u32) -> Result { 13 | if !(BIW4::check_minute(minute) && BIW4::check_hour(hour)) { 14 | Err("BIW3: Invalid time given") 15 | } else { 16 | let biw4 = BIW4 { 17 | hour: hour, 18 | minute: minute, 19 | second: (second as f32 / 7.5) as u32 & 0x7, 20 | }; 21 | Ok(biw4) 22 | } 23 | } 24 | 25 | fn check_minute(minute: u32) -> bool { 26 | return minute <= 59; 27 | } 28 | 29 | fn check_hour(hour: u32) -> bool { 30 | return hour <= 23; 31 | } 32 | } 33 | 34 | impl Codeword for BIW4 { 35 | fn get_codeword(&self) -> u32 { 36 | let mut cw: u32 = 0x0; 37 | cw |= 0x2 << 4; 38 | cw |= self.hour << 7; 39 | cw |= self.minute << 12; 40 | cw |= self.second << 18; 41 | cw = apply_4bit_checksum(cw); 42 | cw = apply_bch_and_parity(cw); 43 | return cw; 44 | } 45 | } 46 | 47 | #[cfg(test)] 48 | mod tests { 49 | use super::*; 50 | 51 | #[test] 52 | fn test_codeword_biw4_1() { 53 | let biw4 = BIW4::new(23, 59, 59).unwrap(); 54 | assert_eq!(biw4.get_codeword() & 0x1FFFF0, 0x1FBBA0); 55 | } 56 | 57 | #[test] 58 | fn test_codeword_biw4_2() { 59 | let biw4 = BIW4::new(23, 59, 31).unwrap(); 60 | assert_eq!(biw4.get_codeword() & 0x1FFFF0, 0x13BBA0); 61 | } 62 | 63 | #[test] 64 | fn test_codeword_biw4_3() { 65 | let biw4 = BIW4::new(0, 0, 0).unwrap(); 66 | assert_eq!(biw4.get_codeword() & 0x1FFFF0, 0x000020); 67 | } 68 | 69 | #[test] 70 | fn test_codeword_biw4_invalid_hour() { 71 | assert_eq!(BIW4::new(24, 59, 59).is_err(), true); 72 | } 73 | 74 | #[test] 75 | fn test_codeword_biw4_invalid_minute() { 76 | assert_eq!(BIW4::new(23, 60, 59).is_err(), true); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/codewords/codeword.rs: -------------------------------------------------------------------------------- 1 | 2 | pub trait Codeword { 3 | fn get_codeword(&self) -> u32; 4 | } 5 | -------------------------------------------------------------------------------- /src/codewords/fiw.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | use helper::fourbit_checksum::apply_4bit_checksum; 4 | 5 | pub struct FIW { 6 | cycle_number: u32, 7 | frame_number: u32, 8 | repeat_paging: u32, 9 | low_traffic_flags: u32, 10 | } 11 | 12 | impl FIW { 13 | pub fn new( 14 | cycle_number: u32, 15 | frame_number: u32, 16 | repeat_paging: u32, 17 | low_traffic_flags: u32, 18 | ) -> Result { 19 | // Currently handling only low_traffic_flag for 1600 bps 20 | if cycle_number <= 14 && frame_number <= 127 && repeat_paging <= 1 { 21 | Ok(FIW { 22 | cycle_number: cycle_number, 23 | frame_number: frame_number, 24 | repeat_paging: repeat_paging, 25 | low_traffic_flags: low_traffic_flags, 26 | }) 27 | } else { 28 | Err("Frame Information Word: Parameter out of range.") 29 | } 30 | } 31 | } 32 | 33 | impl Codeword for FIW { 34 | fn get_codeword(&self) -> u32 { 35 | let mut cw: u32 = 0x0; 36 | cw |= self.cycle_number << 4; 37 | cw |= self.frame_number << 8; 38 | cw |= self.repeat_paging << 16; 39 | cw |= self.low_traffic_flags << 17; 40 | cw = apply_4bit_checksum(cw); 41 | cw = apply_bch_and_parity(cw); 42 | return cw; 43 | } 44 | } 45 | 46 | #[cfg(test)] 47 | mod tests { 48 | use super::*; 49 | 50 | #[test] 51 | fn test_codeword_fiw() { 52 | let fiw = FIW::new(3, 60, 0, 8).unwrap(); 53 | assert_eq!(fiw.get_codeword() & 0x1FFFF0, 0x103C30); 54 | } 55 | 56 | #[test] 57 | fn test_codeword_fiw_out_of_range_cycle() { 58 | assert_eq!(FIW::new(15, 60, 0, 8).is_err(), true); 59 | } 60 | 61 | #[test] 62 | fn test_codeword_fiw_out_of_range_frame() { 63 | assert_eq!(FIW::new(14, 128, 0, 8).is_err(), true); 64 | } 65 | 66 | #[test] 67 | fn test_codeword_fiw_out_of_range_repeat() { 68 | assert_eq!(FIW::new(14, 60, 2, 8).is_err(), true); 69 | } 70 | 71 | #[test] 72 | fn test_codeword_fiw_test_crc() { 73 | let fiw = FIW::new(3, 107, 0, 0).unwrap(); 74 | assert_eq!(fiw.get_codeword(), 0xE4A06B3B); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/codewords/message_alpha.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use super::message_alpha_header::CWMessageAlphaHeader; 3 | use super::message_alpha_signature::CWMessageAlphaSignature; 4 | use super::message_alpha_content::CWMessageAlphaContent; 5 | 6 | use helper::apply_bch_and_parity::apply_bch_and_parity; 7 | 8 | pub struct CWMessageAlpha { 9 | header: CWMessageAlphaHeader, 10 | signature: CWMessageAlphaSignature, 11 | content: Vec, 12 | } 13 | 14 | impl CWMessageAlpha { 15 | pub fn new(message_number: u32, chars: &[u8]) -> Result { 16 | // Not supported for now: 17 | // - Fragmented messages 18 | // - Message retrieval flag 19 | // - Mail Drop flag 20 | 21 | let header = CWMessageAlphaHeader::new(0, 3, message_number, 0, 0).unwrap(); 22 | let mut chars_filled = chars.to_vec(); 23 | CWMessageAlpha::fill_up_chars(&mut chars_filled); 24 | let signature = CWMessageAlphaSignature::new( 25 | CWMessageAlpha::calculate_signature(chars), 26 | &chars_filled[0..2], 27 | ).unwrap(); 28 | 29 | let mut content = Vec::new(); 30 | for i in 0..((chars_filled.len() - 2) / 3) { 31 | content.push( 32 | CWMessageAlphaContent::new(&chars_filled[2 + i * 3..5 + i * 3]).unwrap(), 33 | ); 34 | } 35 | 36 | return Ok(CWMessageAlpha { 37 | header: header, 38 | signature: signature, 39 | content: content, 40 | }); 41 | } 42 | 43 | fn fill_up_chars(chars: &mut Vec) { 44 | while chars.len() % 3 != 2 { 45 | chars.push(0x3); // ETX 46 | } 47 | } 48 | 49 | fn calculate_signature(chars: &[u8]) -> u32 { 50 | let mut sum: u32 = 0; 51 | for chr in chars.to_vec() { 52 | sum += chr as u32 & 0x7F; 53 | } 54 | sum ^= 0xFFFFFFFF; 55 | return sum & 0x7F; 56 | } 57 | 58 | fn calculate_fragment_check(codewords: &Vec) -> u32 { 59 | let mut fragment_check: u32 = 0x00; 60 | 61 | for codeword in codewords { 62 | fragment_check += CWMessageAlpha::get_bitgroup_sum(*codeword); 63 | } 64 | 65 | fragment_check ^= 0xFFFFFFFF; 66 | return fragment_check & 0x3FF; 67 | } 68 | 69 | fn get_bitgroup_sum(codeword: u32) -> u32 { 70 | let mut sum: u32 = 0x0; 71 | sum += codeword & 0xFF; 72 | sum += (codeword >> 8) & 0xFF; 73 | sum += (codeword >> 16) & 0x1F; 74 | return sum; 75 | } 76 | 77 | pub fn get_codewords(&self) -> Vec { 78 | let mut cws = Vec::new(); 79 | cws.push(self.header.get_codeword()); 80 | cws.push(self.signature.get_codeword()); 81 | for content in &self.content { 82 | cws.push(content.get_codeword()); 83 | } 84 | 85 | let fragment_check = CWMessageAlpha::calculate_fragment_check(&cws); 86 | let header = cws[0] | fragment_check; 87 | cws[0] = apply_bch_and_parity(header & 0x1FFFFF); 88 | return cws; 89 | } 90 | } 91 | 92 | #[cfg(test)] 93 | mod tests { 94 | use super::*; 95 | 96 | #[test] 97 | fn test_message_alpha_calculate_signature_1() { 98 | let chars: [u8; 1] = [0x7F]; 99 | let msg_signature = CWMessageAlpha::calculate_signature(&chars); 100 | assert_eq!(msg_signature, 0x0); 101 | } 102 | 103 | #[test] 104 | fn test_message_alpha_calculate_signature_3() { 105 | let chars: [u8; 3] = [0x7F; 3]; 106 | let msg_signature = CWMessageAlpha::calculate_signature(&chars); 107 | assert_eq!(msg_signature, 0x2); 108 | } 109 | 110 | #[test] 111 | fn test_fill_up_chars_1() { 112 | let mut chars = vec![0x00]; 113 | CWMessageAlpha::fill_up_chars(&mut chars); 114 | assert_eq!(chars.len(), 2); 115 | } 116 | 117 | #[test] 118 | fn test_fill_up_chars_3() { 119 | let mut chars = vec![0x00, 0x01, 0x03]; 120 | CWMessageAlpha::fill_up_chars(&mut chars); 121 | assert_eq!(chars.len(), 5); 122 | } 123 | 124 | #[test] 125 | fn test_message_alpha_get_codewords() { 126 | let text_vec = Vec::from("Gurkensalat"); 127 | let msg_alpha = CWMessageAlpha::new(23, &text_vec).unwrap(); 128 | assert_eq!(msg_alpha.get_codewords()[0] & 0x1FFA00, 0x02F800); 129 | assert_eq!(msg_alpha.get_codewords()[0] & 0x3FF, 0x14F); 130 | assert_eq!(msg_alpha.get_codewords()[1] & 0x1FFF80, 0x1D6380); // Gu 131 | assert_eq!(msg_alpha.get_codewords()[2] & 0x1FFFFF, 0x1975F2); // rke 132 | assert_eq!(msg_alpha.get_codewords()[3] & 0x1FFFFF, 0x1879EE); // nsa 133 | assert_eq!(msg_alpha.get_codewords()[4] & 0x1FFFFF, 0x1D30EC); // lat 134 | } 135 | 136 | #[test] 137 | fn test_message_alpha_get_codewords_fillup() { 138 | let text_vec = Vec::from("Gurken"); 139 | let msg_alpha = CWMessageAlpha::new(23, &text_vec).unwrap(); 140 | assert_eq!(msg_alpha.get_codewords()[1] & 0x1FFF80, 0x1D6380); // Gu 141 | assert_eq!(msg_alpha.get_codewords()[2] & 0x1FFFFF, 0x1975F2); // rke 142 | assert_eq!(msg_alpha.get_codewords()[3] & 0x1FFFFF, 0x00C1EE); // nETXETX 143 | } 144 | 145 | #[test] 146 | fn test_get_bitgroup_sum() { 147 | assert_eq!(CWMessageAlpha::get_bitgroup_sum(0x1FFFFF), 0x21D); 148 | } 149 | 150 | #[test] 151 | fn test_calculate_fragment_check_1() { 152 | let codewords = vec![0x1FFFFF]; 153 | assert_eq!(CWMessageAlpha::calculate_fragment_check(&codewords), 0x1E2); 154 | } 155 | 156 | #[test] 157 | fn test_calculate_fragment_check_2() { 158 | let codewords = vec![0x1FFFFF, 0x1FFFFF]; 159 | assert_eq!(CWMessageAlpha::calculate_fragment_check(&codewords), 0x3C5); 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/codewords/message_alpha_content.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | 4 | use std::str; 5 | 6 | pub struct CWMessageAlphaContent { 7 | chars: Vec, 8 | } 9 | 10 | impl CWMessageAlphaContent { 11 | pub fn new(chars: &[u8]) -> Result { 12 | if chars.len() != 3 { 13 | return Err("Alphanumeric Message Content: 3 chars required."); 14 | } 15 | return Ok(CWMessageAlphaContent { chars: chars.to_vec() }); 16 | } 17 | } 18 | 19 | impl Codeword for CWMessageAlphaContent { 20 | fn get_codeword(&self) -> u32 { 21 | let mut cw: u32 = 0x0; 22 | cw |= (self.chars[0] & 0x7F) as u32; 23 | cw |= ((self.chars[1] & 0x7F) as u32) << 7; 24 | cw |= ((self.chars[2] & 0x7F) as u32) << 14; 25 | cw = apply_bch_and_parity(cw); 26 | return cw; 27 | } 28 | } 29 | 30 | #[cfg(test)] 31 | mod tests { 32 | use super::*; 33 | 34 | #[test] 35 | fn test_message_alpha_content() { 36 | let chars = vec![0x42, 0x23, 0x05]; 37 | let msg_chars = CWMessageAlphaContent::new(&chars).unwrap(); 38 | assert_eq!(msg_chars.get_codeword() & 0x1FFFFF, 0x151C2); 39 | } 40 | 41 | #[test] 42 | fn test_message_alpha_content_too_few_chars() { 43 | let chars = vec![0x42, 0x23]; 44 | assert_eq!(CWMessageAlphaContent::new(&chars).is_err(), true); 45 | } 46 | 47 | #[test] 48 | fn test_message_alpha_content_too_many_chars() { 49 | let chars = vec![0x42, 0x23, 0x05, 0x10]; 50 | assert_eq!(CWMessageAlphaContent::new(&chars).is_err(), true); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/codewords/message_alpha_header.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | 4 | pub struct CWMessageAlphaHeader { 5 | message_continued_flag: u32, 6 | fragment_number: u32, 7 | message_number: u32, 8 | retrieval_flag: u32, 9 | mail_drop_flag: u32, 10 | } 11 | 12 | impl CWMessageAlphaHeader { 13 | pub fn new( 14 | message_continued_flag: u32, 15 | fragment_number: u32, 16 | message_number: u32, 17 | retrieval_flag: u32, 18 | mail_drop_flag: u32, 19 | ) -> Result { 20 | if message_continued_flag <= 1 && fragment_number <= 3 && message_number <= 63 && 21 | retrieval_flag <= 1 && mail_drop_flag <= 1 22 | { 23 | Ok(CWMessageAlphaHeader { 24 | message_continued_flag: message_continued_flag, 25 | fragment_number: fragment_number, 26 | message_number: message_number, 27 | retrieval_flag: retrieval_flag, 28 | mail_drop_flag: mail_drop_flag, 29 | }) 30 | } else { 31 | Err("Alphanumeric Message Header: Invalid Parameter.") 32 | } 33 | } 34 | } 35 | 36 | impl Codeword for CWMessageAlphaHeader { 37 | fn get_codeword(&self) -> u32 { 38 | let mut cw: u32 = 0x0; 39 | cw |= self.message_continued_flag << 10; 40 | cw |= self.fragment_number << 11; 41 | cw |= self.message_number << 13; 42 | cw |= self.retrieval_flag << 19; 43 | cw |= self.mail_drop_flag << 20; 44 | cw = apply_bch_and_parity(cw); 45 | return cw; 46 | } 47 | } 48 | 49 | #[cfg(test)] 50 | mod tests { 51 | use super::*; 52 | 53 | #[test] 54 | fn test_message_alpha_header_1() { 55 | let msg_header = CWMessageAlphaHeader::new(0, 3, 63, 1, 0).unwrap(); 56 | assert_eq!(msg_header.get_codeword() & 0x1FFA00, 0x0FF800); 57 | } 58 | 59 | #[test] 60 | fn test_message_alpha_header_2() { 61 | let msg_header = CWMessageAlphaHeader::new(0, 3, 23, 0, 0).unwrap(); 62 | assert_eq!(msg_header.get_codeword() & 0x1FFA00, 0x2F800); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/codewords/message_alpha_signature.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | 4 | pub struct CWMessageAlphaSignature { 5 | signature: u32, 6 | chars: Vec, 7 | } 8 | 9 | impl CWMessageAlphaSignature { 10 | pub fn new(signature: u32, chars: &[u8]) -> Result { 11 | if chars.len() != 2 { 12 | return Err("Alphanumeric Message Signature: 2 chars required."); 13 | } 14 | 15 | if signature <= 0x7F { 16 | return Ok(CWMessageAlphaSignature { 17 | signature: signature, 18 | chars: chars.to_vec(), 19 | }); 20 | } else { 21 | return Err("Alphanumeric Message Signature: Invalid Parameter."); 22 | } 23 | } 24 | } 25 | 26 | impl Codeword for CWMessageAlphaSignature { 27 | fn get_codeword(&self) -> u32 { 28 | let mut cw: u32 = 0x0; 29 | cw |= self.signature; 30 | cw |= ((self.chars[0] & 0x7F) as u32) << 7; 31 | cw |= ((self.chars[1] & 0x7F) as u32) << 14; 32 | cw = apply_bch_and_parity(cw); 33 | return cw; 34 | } 35 | } 36 | 37 | #[cfg(test)] 38 | mod tests { 39 | use super::*; 40 | 41 | #[test] 42 | fn test_message_alpha_signature() { 43 | let chars = vec![0x42, 0x23]; 44 | let msg_signature = CWMessageAlphaSignature::new(0x7F, &chars).unwrap(); 45 | assert_eq!(msg_signature.get_codeword() & 0x1FFFFF, 0x8E17F); 46 | } 47 | 48 | #[test] 49 | fn test_message_alpha_signature_invalid() { 50 | let chars = vec![0x42, 0x23]; 51 | assert_eq!(CWMessageAlphaSignature::new(0x800, &chars).is_err(), true); 52 | } 53 | 54 | #[test] 55 | fn test_message_alpha_signature_too_few_chars() { 56 | let chars = vec![0x42]; 57 | assert_eq!(CWMessageAlphaSignature::new(0x7FF, &chars).is_err(), true); 58 | } 59 | 60 | #[test] 61 | fn test_message_alpha_signature_too_many_chars() { 62 | let chars = vec![0x42, 0x23, 0x05]; 63 | assert_eq!(CWMessageAlphaSignature::new(0x7FF, &chars).is_err(), true); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/codewords/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod codeword; 2 | pub mod fiw; 3 | pub mod biw1; 4 | pub mod biw2; 5 | pub mod biw3; 6 | pub mod biw4; 7 | pub mod address_short; 8 | pub mod vector_alpha; 9 | mod message_alpha_header; 10 | mod message_alpha_signature; 11 | mod message_alpha_content; 12 | pub mod message_alpha; 13 | -------------------------------------------------------------------------------- /src/codewords/vector_alpha.rs: -------------------------------------------------------------------------------- 1 | use super::codeword::Codeword; 2 | use helper::apply_bch_and_parity::apply_bch_and_parity; 3 | use helper::fourbit_checksum::apply_4bit_checksum; 4 | 5 | pub struct CWVectorAlpha { 6 | message_start: u32, 7 | message_words: u32, 8 | } 9 | 10 | impl CWVectorAlpha { 11 | pub fn new(message_start: u32, message_words: u32) -> Result { 12 | if message_start >= 3 && message_start <= 87 && message_words >= 1 && message_words <= 85 { 13 | Ok(CWVectorAlpha { 14 | message_start: message_start, 15 | message_words: message_words, 16 | }) 17 | } else { 18 | Err("CWVectoralpha: Parameters out of range.") 19 | } 20 | } 21 | } 22 | 23 | impl Codeword for CWVectorAlpha { 24 | fn get_codeword(&self) -> u32 { 25 | let mut cw: u32 = 0x0; 26 | cw |= 0x5 << 4; // Type: Alpha Message Vector 27 | cw |= self.message_start << 7; 28 | cw |= self.message_words << 14; 29 | cw = apply_4bit_checksum(cw); 30 | cw = apply_bch_and_parity(cw); 31 | return cw; 32 | } 33 | } 34 | 35 | #[cfg(test)] 36 | mod tests { 37 | use super::*; 38 | 39 | #[test] 40 | fn test_cw_vector_alphanum_lowest() { 41 | let cw_vector_alphanum = CWVectorAlpha::new(3, 1).unwrap(); 42 | assert_eq!(cw_vector_alphanum.get_codeword() & 0x1FFFF0, 0x0041D0) 43 | } 44 | 45 | #[test] 46 | fn test_cw_vector_alphanum_highest() { 47 | let cw_vector_alphanum = CWVectorAlpha::new(87, 85).unwrap(); 48 | assert_eq!(cw_vector_alphanum.get_codeword() & 0x1FFFF0, 0x156BD0) 49 | } 50 | 51 | #[test] 52 | fn test_cw_vector_alphanum_crc() { 53 | let cw_vector_alphanum = CWVectorAlpha::new(3, 49).unwrap(); 54 | assert_eq!(cw_vector_alphanum.get_codeword(), 0xD98C41D1) 55 | } 56 | 57 | #[test] 58 | fn test_cw_vector_alphanum_out_of_range_1() { 59 | assert_eq!(CWVectorAlpha::new(0, 1).is_err(), true); 60 | } 61 | 62 | #[test] 63 | fn test_cw_vector_alphanum_out_of_range_2() { 64 | assert_eq!(CWVectorAlpha::new(88, 1).is_err(), true); 65 | } 66 | 67 | #[test] 68 | fn test_cw_vector_alphanum_out_of_range_3() { 69 | assert_eq!(CWVectorAlpha::new(3, 0).is_err(), true); 70 | } 71 | 72 | #[test] 73 | fn test_cw_vector_alphanum_out_of_range_4() { 74 | assert_eq!(CWVectorAlpha::new(3, 86).is_err(), true); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/frame.rs: -------------------------------------------------------------------------------- 1 | 2 | use codewords::fiw::FIW; 3 | use blocks::Blocks; 4 | use codewords::codeword::Codeword; 5 | use message::Message; 6 | 7 | const PATTERN_BS1 : u32 = 0x55555555; 8 | const PATTERN_A1 : u32 = 0x9C9ACF1E; // A1: 1600 / 2 FM 9 | const PATTERN_B : u16 = 0xAAAA; 10 | const PATTERN_BS2 : u8 = 0x05; 11 | const PATTERN_C : u16 = 0x21B7; 12 | 13 | const MAX_CODEWORDS_PER_BLOCK_1600 : usize = 88; 14 | 15 | pub struct Frame { 16 | fiw: FIW, 17 | num_cws: usize, 18 | send_time: bool, 19 | msgs: Vec 20 | } 21 | 22 | impl Frame { 23 | pub fn new (cycle_number: u32, 24 | frame_number: u32) -> Result 25 | { 26 | let fiw = FIW::new(cycle_number, 27 | frame_number, 28 | 0, 29 | 0x00)?; 30 | 31 | let mut num_cws = 1; // BIW1 32 | let mut send_time = false; 33 | if frame_number == 0 { 34 | send_time = true; // send BIW2, 3 and 4 35 | num_cws += 3; 36 | } 37 | 38 | return Ok(Frame{fiw: fiw, 39 | num_cws: num_cws, 40 | send_time, 41 | msgs: Vec::new()}); 42 | } 43 | 44 | fn get_sync1() -> Vec { 45 | let mut sync1 = Vec::new(); 46 | sync1.extend_from_slice(&Frame::u32_to_4_u8(PATTERN_BS1)); 47 | sync1.extend_from_slice(&Frame::u32_to_4_u8(PATTERN_A1)); 48 | sync1.extend_from_slice(&Frame::u16_to_2_u8(PATTERN_B)); 49 | sync1.extend_from_slice(&Frame::u32_to_4_u8(PATTERN_A1 ^ 0xFFFFFFFF)); 50 | return sync1; 51 | } 52 | 53 | fn get_sync2() -> Vec { 54 | let mut sync2 = Vec::new(); 55 | let mut tmp: u32 = 0x0; 56 | tmp |= (PATTERN_BS2 & 0xF) as u32; 57 | tmp |= (PATTERN_C as u32) << 4; 58 | tmp |= (((PATTERN_BS2 ^ 0xF) & 0xF) as u32) << 20; 59 | tmp |= ((PATTERN_C ^ 0xFFFF) as u32) << 24; 60 | sync2.extend_from_slice(&Frame::u32_to_4_u8(tmp)); 61 | sync2.push(((PATTERN_C ^ 0xFFFF) >> 8) as u8); 62 | return sync2; 63 | } 64 | 65 | fn get_header(&self) -> Vec { 66 | let mut header = Vec::new(); 67 | header.extend_from_slice(&Frame::get_sync1()); 68 | header.extend_from_slice(&Frame::u32_to_4_u8(self.fiw.get_codeword())); 69 | header.extend_from_slice(&Frame::get_sync2()); 70 | return header; 71 | } 72 | 73 | pub fn get_bytes(&self) -> Vec { 74 | let mut bytes = Vec::new(); 75 | bytes.extend_from_slice(&self.get_header()); 76 | bytes.extend_from_slice(&Blocks::get_bytes(&self.msgs, self.send_time)); 77 | return bytes; 78 | } 79 | 80 | pub fn add_message(&mut self, msg: &Message) -> Result { 81 | let size_new_msg = msg.get_num_of_message_codewords()?; 82 | 83 | if size_new_msg < self.space_left() { 84 | self.msgs.push(msg.clone()); 85 | self.num_cws += size_new_msg; 86 | return Ok(self.num_cws); 87 | } 88 | return Err("could not add message to frame"); 89 | } 90 | 91 | pub fn space_left(&self) -> usize { 92 | return MAX_CODEWORDS_PER_BLOCK_1600 - self.num_cws; 93 | } 94 | 95 | pub fn calculate_cycle_and_frame(minutes: u32, seconds: u32) -> (u32,u32) { 96 | let cycle = minutes / 4; 97 | let frame = ((minutes % 4) * 60 + seconds) as f64 / 1.875; 98 | return (cycle,frame as u32) 99 | } 100 | 101 | fn u32_to_4_u8(var: u32) -> [u8; 4] { 102 | let mut array: [u8; 4] = [0; 4]; 103 | array[0] = (var & 0xFF) as u8; 104 | array[1] = (var >> 8 & 0xFF) as u8; 105 | array[2] = (var >> 16 & 0xFF) as u8; 106 | array[3] = (var >> 24 & 0xFF) as u8; 107 | return array; 108 | } 109 | 110 | fn u16_to_2_u8(var: u16) -> [u8; 2] { 111 | let mut array: [u8; 2] = [0; 2]; 112 | array[0] = (var & 0xFF) as u8; 113 | array[1] = (var >> 8 & 0xFF) as u8; 114 | return array; 115 | } 116 | } 117 | 118 | #[cfg(test)] 119 | mod tests { 120 | use super::*; 121 | use message::MessageType; 122 | 123 | #[test] 124 | fn test_frame_add_message() { 125 | let mut frame = Frame::new(0, 1).unwrap(); 126 | let msg = Message::new(0, 127 | MessageType::AlphaNum, 128 | 0x8001, 129 | String::from("test")).unwrap(); 130 | assert_eq!(frame.add_message(&msg).unwrap(), 6); 131 | } 132 | 133 | #[test] 134 | fn test_frame_add_message_86() { 135 | let mut frame = Frame::new(0, 1).unwrap(); 136 | let msg = Message::new(0, 137 | MessageType::AlphaNum, 138 | 0x8001, 139 | String::from("test")).unwrap(); 140 | for _ in 0..16 { 141 | frame.add_message(&msg).unwrap(); 142 | } 143 | 144 | assert_eq!(frame.add_message(&msg).unwrap(), 86); 145 | } 146 | 147 | #[test] 148 | fn test_frame_add_message_91() { 149 | let mut frame = Frame::new(0, 1).unwrap(); 150 | let msg = Message::new(0, 151 | MessageType::AlphaNum, 152 | 0x8001, 153 | String::from("test")).unwrap(); 154 | for _ in 0..17 { 155 | frame.add_message(&msg).unwrap(); 156 | } 157 | 158 | assert_eq!(frame.add_message(&msg).is_err(), true); 159 | } 160 | 161 | #[test] 162 | fn test_frame_get_header() { 163 | let frame = Frame::new(3, 107).unwrap(); 164 | assert_eq!(frame.get_header(), 165 | [0x55, 0x55, 0x55, 0x55, 0x1E, 0xCF, 0x9A, 0x9C, // sync1 166 | 0xAA, 0xAA, 0xE1, 0x30, 0x65, 0x63, 167 | 0x3B, 0x6B, 0xA0, 0xE4, // FIW 168 | 0x75, 0x1B, 0xA2, 0x48, 0xDE]); // Sync2 169 | } 170 | 171 | #[test] 172 | fn test_get_sync1() { 173 | assert_eq!(Frame::get_sync1(), 174 | [0x55, 0x55, 0x55, 0x55, 0x1E, 0xCF, 0x9A, 0x9C, 175 | 0xAA, 0xAA, 0xE1, 0x30, 0x65, 0x63]); 176 | } 177 | 178 | #[test] 179 | fn test_get_sync2() { 180 | assert_eq!(Frame::get_sync2(), 181 | [0x75, 0x1B, 0xA2, 0x48, 0xDE]); 182 | } 183 | 184 | #[test] 185 | fn test_u32_to_4_u8() { 186 | assert_eq!(Frame::u32_to_4_u8(0x12345678), 187 | [0x78, 0x56, 0x34, 0x12]); 188 | } 189 | 190 | #[test] 191 | fn test_u16_to_2_u8() { 192 | assert_eq!(Frame::u16_to_2_u8(0x1234), 193 | [0x34, 0x12]); 194 | } 195 | 196 | #[test] 197 | fn test_calculate_cycle_and_frame_lowest() { 198 | assert_eq!(Frame::calculate_cycle_and_frame(0, 0), (0,0)); 199 | } 200 | 201 | #[test] 202 | fn test_calculate_cycle_and_frame_highest() { 203 | assert_eq!(Frame::calculate_cycle_and_frame(59, 59), (14,127)); 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /src/helper/apply_bch_and_parity.rs: -------------------------------------------------------------------------------- 1 | use super::bch_calculator::apply_bch_checksum; 2 | use super::parity::check_and_set_parity; 3 | 4 | pub fn apply_bch_and_parity(codeword: u32) -> u32 { 5 | let mut cw = codeword; 6 | cw = apply_bch_checksum(cw); 7 | check_and_set_parity(&mut cw); 8 | return cw; 9 | } 10 | -------------------------------------------------------------------------------- /src/helper/bch_calculator.rs: -------------------------------------------------------------------------------- 1 | pub fn apply_bch_checksum(codeword: u32) -> u32 { 2 | let mut crc = codeword; 3 | for i in 0..21 { 4 | if (crc & (0x00000001 << i)) != 0 { 5 | crc ^= 0x4B7 << i; 6 | } 7 | } 8 | return codeword | crc; 9 | } 10 | 11 | #[cfg(test)] 12 | mod tests { 13 | use super::*; 14 | 15 | #[test] 16 | fn test_calculate_crc_all_zeroes() { 17 | 18 | let test_codeword: u32 = 0; 19 | let expected_crc = 0; 20 | let result = apply_bch_checksum(test_codeword); 21 | 22 | assert_eq!(result, expected_crc); 23 | } 24 | 25 | #[test] 26 | fn test_calculate_crc_all_ones() { 27 | 28 | let test_codeword: u32 = 0x001FFFFF; 29 | let expected_crc = 0x7FFFFFFF; 30 | let result = apply_bch_checksum(test_codeword); 31 | 32 | assert_eq!(result, expected_crc); 33 | } 34 | 35 | #[test] 36 | fn test_calculate_crc_1() { 37 | 38 | let test_codeword: u32 = 0x1D40CD; 39 | let expected_crc: u32 = 0x1EDD40CD; 40 | let result = apply_bch_checksum(test_codeword); 41 | 42 | assert_eq!(result, expected_crc); 43 | } 44 | 45 | #[test] 46 | fn test_calculate_crc_2() { 47 | 48 | let test_codeword: u32 = 0x87523; 49 | let expected_crc: u32 = 0x38C87523; 50 | let result = apply_bch_checksum(test_codeword); 51 | 52 | assert_eq!(result, expected_crc); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/helper/fourbit_checksum.rs: -------------------------------------------------------------------------------- 1 | pub fn apply_4bit_checksum(codeword: u32) -> u32 { 2 | let mut sum: u32 = 0x0; 3 | sum += (codeword >> 4) & 0xF; 4 | sum += (codeword >> 8) & 0xF; 5 | sum += (codeword >> 12) & 0xF; 6 | sum += (codeword >> 16) & 0xF; 7 | sum += (codeword >> 20) & 0x1; 8 | sum ^= 0xF; 9 | sum &= 0xF; 10 | return codeword | sum; 11 | } 12 | 13 | #[cfg(test)] 14 | mod tests { 15 | use super::*; 16 | 17 | #[test] 18 | fn test_calculate_4bit_checksum_all_zeroes() { 19 | 20 | let test_codeword: u32 = 0x00000000; 21 | let expected_crc: u32 = 0x0000000F; 22 | let result = apply_4bit_checksum(test_codeword); 23 | 24 | assert_eq!(result, expected_crc); 25 | } 26 | 27 | #[test] 28 | fn test_calculate_4bit_checksum_all_ones() { 29 | 30 | let test_codeword: u32 = 0x001FFFF0; 31 | let expected_crc: u32 = 0x001FFFF2; 32 | let result = apply_4bit_checksum(test_codeword); 33 | 34 | assert_eq!(result, expected_crc); 35 | } 36 | 37 | #[test] 38 | fn test_calculate_4bit_checksum() { 39 | 40 | let test_codeword: u32 = 0x00139C50; 41 | let expected_crc: u32 = 0x00139C51; 42 | let result = apply_4bit_checksum(test_codeword); 43 | 44 | assert_eq!(result, expected_crc); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/helper/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod fourbit_checksum; 2 | pub mod apply_bch_and_parity; 3 | mod bch_calculator; 4 | mod parity; -------------------------------------------------------------------------------- /src/helper/parity.rs: -------------------------------------------------------------------------------- 1 | 2 | // Think of using crate hamming to use hamming weight for this 3 | pub fn check_and_set_parity(codeword: &mut u32) -> bool { 4 | 5 | let ones = count_ones(*codeword); 6 | 7 | let odd = (ones % 2) != 0; 8 | if odd { 9 | set_parity(codeword); 10 | } else { 11 | clear_parity(codeword); 12 | } 13 | 14 | return odd; 15 | } 16 | 17 | fn count_ones(codeword: u32) -> u32 { 18 | let mut ones = 0; 19 | for i in 0..31 { 20 | let mask = 1 << i; 21 | ones += (codeword & mask) >> i; 22 | } 23 | return ones; 24 | } 25 | 26 | fn set_parity(codeword: &mut u32) { 27 | let parity_bit = 0x80000000; 28 | *codeword = *codeword | parity_bit; 29 | } 30 | 31 | fn clear_parity(codeword: &mut u32) { 32 | *codeword = *codeword & 0x7FFFFFFF; 33 | } 34 | 35 | 36 | #[cfg(test)] 37 | mod tests { 38 | use super::*; 39 | 40 | #[test] 41 | fn test_parity_all_zeroes() { 42 | 43 | let mut test_data: u32 = 0x0; 44 | let result: bool = check_and_set_parity(&mut test_data); 45 | 46 | assert_eq!(result, false); 47 | assert_eq!(test_data, test_data); 48 | } 49 | 50 | #[test] 51 | fn test_parity_all_ones() { 52 | 53 | let mut test_data: u32 = 0x7FFFFFFF; 54 | let result: bool = check_and_set_parity(&mut test_data); 55 | 56 | assert_eq!(result, true); 57 | assert_eq!(test_data, 0xFFFFFFFF); 58 | } 59 | 60 | #[test] 61 | fn test_parity_even() { 62 | 63 | let mut test_data: u32 = 0x00000003; 64 | let result: bool = check_and_set_parity(&mut test_data); 65 | 66 | assert_eq!(result, false); 67 | assert_eq!(test_data, test_data); 68 | } 69 | 70 | #[test] 71 | fn test_parity_illegal_paritybit_even() { 72 | 73 | let mut test_data: u32 = 0x80000003; 74 | let result: bool = check_and_set_parity(&mut test_data); 75 | 76 | assert_eq!(result, false); 77 | assert_eq!(test_data, 0x00000003); 78 | } 79 | 80 | #[test] 81 | fn test_parity_illegal_paritybit_odd() { 82 | 83 | let mut test_data: u32 = 0x80000001; 84 | let result: bool = check_and_set_parity(&mut test_data); 85 | 86 | assert_eq!(result, true); 87 | assert_eq!(test_data, 0x80000001); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate serde_derive; 3 | 4 | mod blocks; 5 | mod codewords; 6 | mod helper; 7 | 8 | pub mod message; 9 | pub mod frame; 10 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::time; 2 | use std::thread; 3 | use std::sync::{Arc, Mutex}; 4 | 5 | extern crate chrono; 6 | use chrono::prelude::*; 7 | 8 | extern crate flexencoder; 9 | use flexencoder::frame::Frame; 10 | use flexencoder::message::*; 11 | 12 | use std::fs::File; 13 | use std::io::prelude::*; 14 | 15 | extern crate getopts; 16 | use getopts::Options; 17 | use std::env; 18 | 19 | extern crate bit_reverse; 20 | use bit_reverse::ParallelReverse; 21 | 22 | extern crate serde; 23 | extern crate serde_json; 24 | 25 | use std::net::UdpSocket; 26 | 27 | use std::process::exit; 28 | 29 | const CYCLES_PER_HOUR: u32 = 15; 30 | const FRAMES_PER_CYCLE: u32 = 128; 31 | 32 | enum OperationMode { 33 | Single, 34 | Hour, 35 | Continuous, 36 | } 37 | 38 | fn print_usage(program: &str, opts: Options) { 39 | let brief = format!("Usage: {} --MODE -i INPUT -o OUTPUT", program); 40 | print!("{}", opts.usage(&brief)); 41 | } 42 | 43 | fn parse_arguments(args: &Vec) -> Result<(String, String, OperationMode), &'static str> { 44 | let program = args[0].clone(); 45 | 46 | let mut opts = Options::new(); 47 | opts.optopt("i", "input", "set input file (JSON)", "FILE.json"); 48 | opts.optopt("o", "output", "set output file (FLEX bytestream)", "FILE"); 49 | opts.optflag( 50 | "", 51 | "single", 52 | "MODE: generate only frames for the messages given", 53 | ); 54 | opts.optflag("", "hour", "MODE: generate a full hour of frames"); 55 | opts.optflag( 56 | "", 57 | "continuous", 58 | "MODE: generate frames continuously to UDP", 59 | ); 60 | opts.optflag("h", "help", "print this help menu"); 61 | let matches = match opts.parse(&args[1..]) { 62 | Ok(m) => m, 63 | Err(f) => panic!(f.to_string()), 64 | }; 65 | if matches.opt_present("h") { 66 | print_usage(&program, opts); 67 | exit(0); 68 | } 69 | 70 | let in_file; 71 | match matches.opt_str("i") { 72 | Some(x) => in_file = x, 73 | None => return Err("No input file given."), 74 | } 75 | 76 | let mut out_file = "/dev/null".to_string(); 77 | if matches.opt_present("o") { 78 | out_file = matches.opt_str("o").unwrap(); 79 | } 80 | 81 | let mode: OperationMode; 82 | if matches.opt_present("hour") { 83 | mode = OperationMode::Hour; 84 | } else if matches.opt_present("single") { 85 | mode = OperationMode::Single; 86 | } else if matches.opt_present("continuous") { 87 | mode = OperationMode::Continuous; 88 | } else { 89 | return Err("No operation mode given."); 90 | } 91 | 92 | return Ok((in_file, out_file, mode)); 93 | } 94 | 95 | fn generate_hour(in_file: String, out_file: String) { 96 | let file = File::open(in_file).unwrap(); 97 | let msg_vec: Vec = serde_json::from_reader(file).unwrap(); 98 | 99 | let mut frames = Vec::new(); 100 | for cycle_nr in 0..CYCLES_PER_HOUR { 101 | for frame_nr in 0..FRAMES_PER_CYCLE { 102 | let mut frame = Frame::new(cycle_nr, frame_nr).unwrap(); 103 | for msg in &msg_vec { 104 | if msg.frame == frame_nr { 105 | println!("added {:?}", msg); 106 | frame.add_message(msg).unwrap(); 107 | } 108 | } 109 | frames.push(frame); 110 | } 111 | } 112 | write_frames_to_file(&frames, &out_file); 113 | } 114 | 115 | fn generate_single_frames(in_file: String, out_file: String) { 116 | let file = File::open(in_file).unwrap(); 117 | let msg_vec: Vec = serde_json::from_reader(file).unwrap(); 118 | 119 | let mut frames = Vec::new(); 120 | for msg in &msg_vec { 121 | let mut frame = Frame::new(0, msg.frame).unwrap(); 122 | frame.add_message(msg).unwrap(); 123 | frames.push(frame); 124 | println!("added {:?}", msg); 125 | } 126 | write_frames_to_file(&frames, &out_file); 127 | } 128 | 129 | fn write_frames_to_file(frames: &Vec, filename: &String) { 130 | let mut file = File::create(filename).unwrap(); 131 | file.write_all(&frames_hton(frames)).unwrap(); 132 | } 133 | 134 | fn frames_hton(frames: &Vec) -> Vec { 135 | let mut reversed_bytes = Vec::new(); 136 | for frame in frames { 137 | let bytes = frame.get_bytes(); 138 | for byte in bytes { 139 | reversed_bytes.push(byte.swap_bits()); 140 | } 141 | } 142 | return reversed_bytes; 143 | } 144 | 145 | fn generate_continuously(in_file: String) { 146 | let share_msg_input_0 = Arc::new(Mutex::new(Vec::new())); 147 | let share_msg_input_1 = share_msg_input_0.clone(); 148 | 149 | let input_thread = thread::spawn(move || loop { 150 | let file = File::open(&in_file).unwrap(); 151 | let mut msg_vec: Vec = serde_json::from_reader(file).unwrap(); 152 | 153 | println!("Queuing message(s): {:?}", msg_vec); 154 | let mut shared = share_msg_input_0.lock().unwrap(); 155 | shared.append(&mut msg_vec); 156 | }); 157 | 158 | let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address"); 159 | 160 | let mut msg_vec: Vec = Vec::new(); 161 | let encoder_thread = thread::spawn(move || loop { 162 | msg_vec.append(&mut share_msg_input_1.lock().unwrap()); 163 | 164 | let dt = Utc::now(); 165 | let (cycle_nr, frame_nr) = Frame::calculate_cycle_and_frame(dt.minute(), dt.second()); 166 | println!( 167 | "Time: {}:{}, Cycle: {}, Frame: {} - Size of MsgQueue: {}", 168 | dt.minute(), 169 | dt.second(), 170 | cycle_nr, 171 | frame_nr, 172 | msg_vec.len() 173 | ); 174 | 175 | let mut frame = Frame::new(cycle_nr, frame_nr).unwrap(); 176 | for i in 0..msg_vec.len() { 177 | if msg_vec[i].frame == frame_nr { 178 | if msg_vec[i].get_num_of_message_codewords().unwrap() < frame.space_left() { 179 | frame.add_message(&msg_vec[i]).unwrap(); 180 | msg_vec.remove(i); 181 | } 182 | } 183 | } 184 | 185 | socket 186 | .send_to(&frames_hton(&vec![frame]), "127.0.0.1:51337") 187 | .expect("couldn't send data"); 188 | 189 | thread::sleep(time::Duration::from_millis(1875)); 190 | }); 191 | 192 | let _ = input_thread.join(); 193 | let _ = encoder_thread.join(); 194 | } 195 | 196 | fn main() { 197 | 198 | let args: Vec = env::args().collect(); 199 | let (in_file, out_file, mode) = parse_arguments(&args).unwrap(); 200 | 201 | match mode { 202 | OperationMode::Single => generate_single_frames(in_file, out_file), 203 | OperationMode::Hour => generate_hour(in_file, out_file), 204 | OperationMode::Continuous => generate_continuously(in_file), 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /src/message.rs: -------------------------------------------------------------------------------- 1 | 2 | #[derive(Debug, Clone, Deserialize)] 3 | pub enum MessageType { 4 | AlphaNum 5 | } 6 | 7 | #[derive(Debug, PartialEq)] 8 | pub enum CapcodeType { 9 | ShortAddress, 10 | Invalid 11 | } 12 | 13 | #[derive(Debug, Clone, Deserialize)] 14 | pub struct Message { 15 | pub frame: u32, 16 | msgtype: MessageType, 17 | pub capcode: u32, 18 | pub data: String, 19 | } 20 | 21 | impl Message { 22 | pub fn new(frame: u32, 23 | msgtype: MessageType, 24 | capcode: u32, 25 | data: String) -> Result { 26 | 27 | if Message::get_capcode_type(capcode) == CapcodeType::Invalid { 28 | return Err("Invalid CAPCODE"); 29 | } 30 | 31 | if frame > 128 { 32 | return Err("Invalid Frame number"); 33 | } 34 | 35 | return Ok(Message{frame: frame, 36 | msgtype: msgtype, 37 | capcode: capcode, 38 | data: data}); 39 | } 40 | 41 | pub fn get_num_of_message_codewords(&self) -> Result { 42 | let mut size = 0; 43 | match Message::get_capcode_type(self.capcode) { 44 | CapcodeType::ShortAddress => size += 2, // Address Word + Vector Word 45 | CapcodeType::Invalid => return Err("Invalid CAPCODE given") 46 | } 47 | 48 | match self.msgtype { 49 | MessageType::AlphaNum => size += self.get_num_of_content_codewords() 50 | } 51 | 52 | return Ok(size); 53 | } 54 | 55 | pub fn get_num_of_content_codewords(&self) -> usize { 56 | let mut size: usize = 0; 57 | size += 2; // Message Header and Signature 58 | size += (self.data.len()-2) / 3; // 3 chars per Content codeword 59 | if (self.data.len()-2) % 3 > 0 { 60 | size += 1; 61 | } 62 | return size; 63 | } 64 | 65 | fn get_capcode_type(capcode: u32) -> CapcodeType { 66 | if capcode >= 0x0001 && capcode <= 0x1EA7FF { 67 | return CapcodeType::ShortAddress; 68 | } 69 | return CapcodeType::Invalid; 70 | } 71 | } 72 | 73 | 74 | #[cfg(test)] 75 | mod tests { 76 | use super::*; 77 | 78 | #[test] 79 | fn test_message_when_typical() { 80 | let msg = Message::new(0, 81 | MessageType::AlphaNum, 82 | 0x0001, 83 | String::from("test")); 84 | assert_eq!(msg.is_err(), false); 85 | } 86 | 87 | #[test] 88 | fn test_message_when_invalid_capcode() { 89 | let msg = Message::new(0, 90 | MessageType::AlphaNum, 91 | 0x0000, 92 | String::from("test")); 93 | assert_eq!(msg.is_err(), true); 94 | } 95 | 96 | #[test] 97 | fn test_message_get_capcode_type_when_short_address() { 98 | assert_eq!(Message::get_capcode_type(0x8001), CapcodeType::ShortAddress); 99 | } 100 | 101 | #[test] 102 | fn test_message_get_capcode_type_when_zero_address() { 103 | assert_eq!(Message::get_capcode_type(0x0), CapcodeType::Invalid); 104 | } 105 | 106 | #[test] 107 | fn test_message_get_capcode_type_when_invalid_address() { 108 | assert_eq!(Message::get_capcode_type(0x1EB000), CapcodeType::Invalid); 109 | } 110 | 111 | #[test] 112 | fn test_message_get_num_of_content_codewords_when_five_character() { 113 | let msg = Message::new(0, 114 | MessageType::AlphaNum, 115 | 0x8001, 116 | String::from("abcde")).unwrap(); 117 | assert_eq!(msg.get_num_of_content_codewords(), 3); 118 | } 119 | 120 | #[test] 121 | fn test_message_get_num_of_message_codewords_when_two_character() { 122 | let msg = Message::new(0, 123 | MessageType::AlphaNum, 124 | 0x8001, 125 | String::from("ab")).unwrap(); 126 | assert_eq!(msg.get_num_of_message_codewords().unwrap(), 4); 127 | } 128 | 129 | #[test] 130 | fn test_message_get_num_of_message_codewords_when_four_character() { 131 | let msg = Message::new(0, 132 | MessageType::AlphaNum, 133 | 0x8001, 134 | String::from("abcd")).unwrap(); 135 | assert_eq!(msg.get_num_of_message_codewords().unwrap(), 5); 136 | } 137 | } --------------------------------------------------------------------------------