├── .cproject ├── .gitignore ├── .project ├── LICENSE.TXT ├── Makefile ├── Makefile.inc ├── README.TXT ├── doc ├── .gitignore ├── Makefile └── nmea.doxygen ├── include └── nmea │ ├── config.h │ ├── context.h │ ├── generate.h │ ├── generator.h │ ├── gmath.h │ ├── info.h │ ├── nmea.h │ ├── parse.h │ ├── parser.h │ ├── sentence.h │ ├── time.h │ ├── tok.h │ ├── units.h │ └── util.h ├── samples ├── Makefile ├── generate │ └── main.c ├── generator │ └── main.c ├── math │ └── main.c ├── parse │ └── main.c └── parse_file │ ├── gpslog.txt │ └── main.c └── src ├── context.c ├── generate.c ├── generator.c ├── gmath.c ├── info.c ├── parse.c ├── parser.c ├── sentence.c ├── time.c ├── tok.c └── util.c /.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | make 52 | 53 | samples 54 | true 55 | true 56 | true 57 | 58 | 59 | make 60 | 61 | all 62 | true 63 | true 64 | true 65 | 66 | 67 | make 68 | 69 | clean 70 | true 71 | true 72 | true 73 | 74 | 75 | make 76 | 77 | doc 78 | true 79 | true 80 | true 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /lib/ 3 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | nmealib 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | 11 | 12 | ?name? 13 | 14 | 15 | 16 | org.eclipse.cdt.make.core.append_environment 17 | true 18 | 19 | 20 | org.eclipse.cdt.make.core.autoBuildTarget 21 | all 22 | 23 | 24 | org.eclipse.cdt.make.core.buildArguments 25 | 26 | 27 | 28 | org.eclipse.cdt.make.core.buildCommand 29 | make 30 | 31 | 32 | org.eclipse.cdt.make.core.cleanBuildTarget 33 | clean 34 | 35 | 36 | org.eclipse.cdt.make.core.contents 37 | org.eclipse.cdt.make.core.activeConfigSettings 38 | 39 | 40 | org.eclipse.cdt.make.core.enableAutoBuild 41 | true 42 | 43 | 44 | org.eclipse.cdt.make.core.enableCleanBuild 45 | true 46 | 47 | 48 | org.eclipse.cdt.make.core.enableFullBuild 49 | true 50 | 51 | 52 | org.eclipse.cdt.make.core.fullBuildTarget 53 | all 54 | 55 | 56 | org.eclipse.cdt.make.core.stopOnError 57 | true 58 | 59 | 60 | org.eclipse.cdt.make.core.useDefaultBuildCmd 61 | true 62 | 63 | 64 | 65 | 66 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 67 | full,incremental, 68 | 69 | 70 | 71 | 72 | 73 | org.eclipse.cdt.core.cnature 74 | org.eclipse.cdt.core.ccnature 75 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 76 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 77 | 78 | 79 | -------------------------------------------------------------------------------- /LICENSE.TXT: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | include Makefile.inc 2 | 3 | # 4 | # Settings 5 | # 6 | 7 | LIBNAME = libnmea.so 8 | 9 | DESTDIR ?= 10 | 11 | MACHINE=$(shell uname -m) 12 | ifeq ($(strip $(MACHINE)),x86_64) 13 | LIBDIR = usr/lib64 14 | else 15 | LIBDIR = usr/lib 16 | endif 17 | INCLUDEDIR = usr/include 18 | 19 | MODULES = context generate generator gmath info parse parser sentence time tok util 20 | OBJ = $(MODULES:%=build/%.o) 21 | 22 | LIBS = -lm 23 | INCS = -I ./include 24 | 25 | 26 | # 27 | # Targets 28 | # 29 | 30 | all: all-before lib/$(LIBNAME) 31 | 32 | remake: clean all 33 | 34 | lib/$(LIBNAME): $(OBJ) 35 | @echo "[LD] $@" 36 | @$(CC) -shared -Wl,-soname=$(LIBNAME) -o lib/$(LIBNAME) $(OBJ) -lc 37 | 38 | build/%.o: src/%.c Makefile Makefile.inc 39 | @echo "[CC] $<" 40 | @$(CC) $(CCFLAGS) $(INCS) -c $< -o $@ 41 | 42 | samples: all 43 | $(MAKE) -C samples all 44 | 45 | 46 | # 47 | # Phony Targets 48 | # 49 | 50 | .PHONY: all-before clean doc install uninstall 51 | 52 | all-before: 53 | @mkdir -p build lib 54 | 55 | clean: 56 | $(MAKE) -C doc clean 57 | $(MAKE) -C samples clean 58 | rm -fr build lib 59 | 60 | doc: 61 | $(MAKE) -C doc all 62 | 63 | install: all 64 | @mkdir -v -p $(DESTDIR)/$(LIBDIR) $(DESTDIR)/$(INCLUDEDIR) 65 | cp lib/$(LIBNAME) $(DESTDIR)/$(LIBDIR)/$(LIBNAME).$(VERSION) 66 | $(STRIP) $(DESTDIR)/$(LIBDIR)/$(LIBNAME).$(VERSION) 67 | ldconfig -n $(DESTDIR)/$(LIBDIR) 68 | @rm -fr $(DESTDIR)/$(INCLUDEDIR)/nmea 69 | cp -r include/nmea $(DESTDIR)/$(INCLUDEDIR) 70 | 71 | uninstall: 72 | rm -fr $(DESTDIR)/$(INCLUDEDIR)/nmea 73 | rm -f $(DESTDIR)/$(LIBDIR)/$(LIBNAME) $(DESTDIR)/$(LIBDIR)/$(LIBNAME).$(VERSION) 74 | ldconfig -n $(DESTDIR)/$(LIBDIR) 75 | @rmdir -v -p --ignore-fail-on-non-empty $(DESTDIR)/$(LIBDIR) $(DESTDIR)/$(INCLUDEDIR) 76 | ifneq ($(DESTDIR),) 77 | @mkdir -v -p $(DESTDIR) 78 | endif 79 | -------------------------------------------------------------------------------- /Makefile.inc: -------------------------------------------------------------------------------- 1 | ###################### 2 | # 3 | # Highlevel configuration options for all 4 | # 5 | # 6 | 7 | # activate debugging with 1 or deactivate with 0 8 | DEBUG ?= 1 9 | 10 | 11 | ###################### 12 | # 13 | # Lowlevel options and rules 14 | # 15 | 16 | ifeq ($(DEBUG),0) 17 | STRIP ?= strip 18 | else 19 | STRIP ?= : 20 | endif 21 | 22 | 23 | GITVERSION = $(shell git describe --dirty='-dirty') 24 | 25 | # we expect the version to be like 'v0.5.3-27-g0c2727a' and then strip the 'v', 26 | # and the '-27-g0c2727a' parts 27 | VERSION=$(shell git describe | \ 28 | sed -r -e 's/^([^[:digit:]]+)(.*)/\2/' \ 29 | -e 's/^([^-]+)(.*)/\1/' | \ 30 | grep -E '[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+') 31 | 32 | # protect against no version number 33 | ifeq ($(strip $(VERSION)),) 34 | VERSION=0.0.0 35 | endif 36 | 37 | CC ?= gcc 38 | CCFLAGS += -fPIC -O2 -Wall -Wextra -Wformat=2 -Winit-self \ 39 | -Wmissing-include-dirs -Wswitch-default -Wswitch-enum -Werror 40 | -------------------------------------------------------------------------------- /README.TXT: -------------------------------------------------------------------------------- 1 | NMEA library 2 | 3 | Disclaimer 4 | 5 | The National Marine Electronics Association (NMEA) has developed a specification 6 | that defines the interface between various pieces of marine electronic 7 | equipment. The standard permits marine electronics to send information to 8 | computers and to other marine equipment. 9 | 10 | Most computer programs that provide real time position information understand 11 | and expect data to be in NMEA format. This data includes the complete PVT 12 | (position, velocity, time) solution computed by the GPS receiver. The idea of 13 | NMEA is to send a line of data called a sentence that is totally self contained 14 | and independent from other sentences. All NMEA sentences are sequences of ACSII 15 | symbols that begin with a '$' and end with a carriage return/line feed sequence 16 | and can be no longer than 80 characters of visible text (plus the line 17 | terminators). 18 | 19 | Introduction 20 | 21 | Features 22 | 23 | - Analysis NMEA sentences and granting GPS data in C structures 24 | - Generate NMEA sentences 25 | - Supported sentences: GPGGA, GPGSA, GPGSV, GPRMC, GPVTG 26 | - Multilevel architecture of algorithms 27 | - Additional functions of geographical mathematics and work with navigation data 28 | 29 | Supported (tested) platforms 30 | 31 | - Linux (GCC) 32 | 33 | Licence: LGPL 34 | -------------------------------------------------------------------------------- /doc/.gitignore: -------------------------------------------------------------------------------- 1 | /html/ 2 | /latex/ 3 | /man/ 4 | /nmealib.pdf 5 | /nmea.doxygen.temp 6 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.inc 2 | 3 | .PHONY: all clean 4 | 5 | clean: 6 | rm -fr html latex man nmealib.pdf 7 | 8 | all: clean nmea.doxygen 9 | @echo "Updating version..." 10 | @sed -r "s/^([[:space:]]*PROJECT_NUMBER[[:space:]]*=).*/\1 $(GITVERSION)/" nmea.doxygen > nmea.doxygen.temp 11 | @echo "Generating HTML and man pages..." 12 | @doxygen nmea.doxygen.temp 13 | @rm nmea.doxygen.temp 14 | @echo "Generating PDF..." 15 | @make -C latex -s > /dev/null 2>&1 16 | @mv latex/refman.pdf nmealib.pdf 17 | @rm -fr latex 18 | @echo "Done" 19 | -------------------------------------------------------------------------------- /include/nmea/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_CONFIG_H__ 22 | #define __NMEA_CONFIG_H__ 23 | 24 | #define NMEA_POSIX(x) (x) 25 | #define NMEA_INLINE inline 26 | 27 | #if !defined(NDEBUG) 28 | #include 29 | #define NMEA_ASSERT(x) assert(x) 30 | #else 31 | #define NMEA_ASSERT(x) 32 | #endif 33 | 34 | #endif /* __NMEA_CONFIG_H__ */ 35 | -------------------------------------------------------------------------------- /include/nmea/context.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_CONTEXT_H__ 22 | #define __NMEA_CONTEXT_H__ 23 | 24 | #define NMEA_DEF_PARSEBUFF (1024) 25 | #define NMEA_MIN_PARSEBUFF (256) 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | typedef void (*nmeaTraceFunc)(const char *str, int str_size); 32 | typedef void (*nmeaErrorFunc)(const char *str, int str_size); 33 | 34 | typedef struct _nmeaPROPERTY { 35 | nmeaTraceFunc trace_func; 36 | nmeaErrorFunc error_func; 37 | int parse_buff_size; 38 | 39 | } nmeaPROPERTY; 40 | 41 | nmeaPROPERTY * nmea_property(void); 42 | 43 | void nmea_trace(const char *str, ...); 44 | void nmea_trace_buff(const char *buff, int buff_size); 45 | void nmea_error(const char *str, ...); 46 | 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #endif /* __NMEA_CONTEXT_H__ */ 52 | -------------------------------------------------------------------------------- /include/nmea/generate.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_GENERATE_H__ 22 | #define __NMEA_GENERATE_H__ 23 | 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | int nmea_generate(char *buff, int buff_sz, const nmeaINFO *info, 32 | int generate_mask); 33 | 34 | int nmea_gen_GPGGA(char *buff, int buff_sz, nmeaGPGGA *pack); 35 | int nmea_gen_GPGSA(char *buff, int buff_sz, nmeaGPGSA *pack); 36 | int nmea_gen_GPGSV(char *buff, int buff_sz, nmeaGPGSV *pack); 37 | int nmea_gen_GPRMC(char *buff, int buff_sz, nmeaGPRMC *pack); 38 | int nmea_gen_GPVTG(char *buff, int buff_sz, nmeaGPVTG *pack); 39 | 40 | void nmea_info2GPGGA(const nmeaINFO *info, nmeaGPGGA *pack); 41 | void nmea_info2GPGSA(const nmeaINFO *info, nmeaGPGSA *pack); 42 | void nmea_info2GPRMC(const nmeaINFO *info, nmeaGPRMC *pack); 43 | void nmea_info2GPVTG(const nmeaINFO *info, nmeaGPVTG *pack); 44 | 45 | int nmea_gsv_npack(int sat_count); 46 | void nmea_info2GPGSV(const nmeaINFO *info, nmeaGPGSV *pack, int pack_idx); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | #endif /* __NMEA_GENERATE_H__ */ 53 | -------------------------------------------------------------------------------- /include/nmea/generator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_GENERATOR_H__ 22 | #define __NMEA_GENERATOR_H__ 23 | 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /* 31 | * high level 32 | */ 33 | 34 | struct _nmeaGENERATOR; 35 | 36 | enum nmeaGENTYPE 37 | { 38 | NMEA_GEN_NOISE = 0, 39 | NMEA_GEN_STATIC, 40 | NMEA_GEN_ROTATE, 41 | 42 | NMEA_GEN_SAT_STATIC, 43 | NMEA_GEN_SAT_ROTATE, 44 | NMEA_GEN_POS_RANDMOVE, 45 | 46 | NMEA_GEN_LAST 47 | }; 48 | 49 | struct _nmeaGENERATOR * nmea_create_generator(int type, nmeaINFO *info); 50 | void nmea_destroy_generator(struct _nmeaGENERATOR *gen); 51 | 52 | int nmea_generate_from( 53 | char *buff, int buff_sz, /* buffer */ 54 | nmeaINFO *info, /* source info */ 55 | struct _nmeaGENERATOR *gen, /* generator */ 56 | int generate_mask /* mask of sentence`s (e.g. GPGGA | GPGSA) */ 57 | ); 58 | 59 | /* 60 | * low level 61 | */ 62 | 63 | typedef int (*nmeaNMEA_GEN_INIT)(struct _nmeaGENERATOR *gen, nmeaINFO *info); 64 | typedef int (*nmeaNMEA_GEN_LOOP)(struct _nmeaGENERATOR *gen, nmeaINFO *info); 65 | typedef int (*nmeaNMEA_GEN_RESET)(struct _nmeaGENERATOR *gen, nmeaINFO *info); 66 | typedef int (*nmeaNMEA_GEN_DESTROY)(struct _nmeaGENERATOR *gen); 67 | 68 | typedef struct _nmeaGENERATOR 69 | { 70 | void *gen_data; 71 | nmeaNMEA_GEN_INIT init_call; 72 | nmeaNMEA_GEN_LOOP loop_call; 73 | nmeaNMEA_GEN_RESET reset_call; 74 | nmeaNMEA_GEN_DESTROY destroy_call; 75 | struct _nmeaGENERATOR *next; 76 | 77 | } nmeaGENERATOR; 78 | 79 | int nmea_gen_init(nmeaGENERATOR *gen, nmeaINFO *info); 80 | int nmea_gen_loop(nmeaGENERATOR *gen, nmeaINFO *info); 81 | int nmea_gen_reset(nmeaGENERATOR *gen, nmeaINFO *info); 82 | void nmea_gen_destroy(nmeaGENERATOR *gen); 83 | void nmea_gen_add(nmeaGENERATOR *to, nmeaGENERATOR *gen); 84 | 85 | #ifdef __cplusplus 86 | } 87 | #endif 88 | 89 | #endif /* __NMEA_GENERATOR_H__ */ 90 | -------------------------------------------------------------------------------- /include/nmea/gmath.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_GMATH_H__ 22 | #define __NMEA_GMATH_H__ 23 | 24 | #include 25 | 26 | #define NMEA_PI (3.141592653589793) /**< PI value */ 27 | #define NMEA_PI180 (NMEA_PI / 180) /**< PI division by 180 */ 28 | #define NMEA_EARTHRADIUS_KM (6378) /**< Earth's mean radius in km */ 29 | #define NMEA_EARTHRADIUS_M (NMEA_EARTHRADIUS_KM * 1000) /**< Earth's mean radius in m */ 30 | #define NMEA_EARTH_SEMIMAJORAXIS_M (6378137.0) /**< Earth's semi-major axis in m according WGS84 */ 31 | #define NMEA_EARTH_SEMIMAJORAXIS_KM (NMEA_EARTHMAJORAXIS_KM / 1000) /**< Earth's semi-major axis in km according WGS 84 */ 32 | #define NMEA_EARTH_FLATTENING (1 / 298.257223563) /**< Earth's flattening according WGS 84 */ 33 | #define NMEA_DOP_FACTOR (5) /**< Factor for translating DOP to meters */ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* 40 | * degree VS radian 41 | */ 42 | 43 | double nmea_degree2radian(double val); 44 | double nmea_radian2degree(double val); 45 | 46 | /* 47 | * NDEG (NMEA degree) 48 | */ 49 | 50 | double nmea_ndeg2degree(double val); 51 | double nmea_degree2ndeg(double val); 52 | 53 | double nmea_ndeg2radian(double val); 54 | double nmea_radian2ndeg(double val); 55 | 56 | /* 57 | * DOP 58 | */ 59 | 60 | double nmea_calc_pdop(double hdop, double vdop); 61 | double nmea_dop2meters(double dop); 62 | double nmea_meters2dop(double meters); 63 | 64 | /* 65 | * positions work 66 | */ 67 | 68 | void nmea_info2pos(const nmeaINFO *info, nmeaPOS *pos); 69 | void nmea_pos2info(const nmeaPOS *pos, nmeaINFO *info); 70 | 71 | double nmea_distance( 72 | const nmeaPOS *from_pos, 73 | const nmeaPOS *to_pos 74 | ); 75 | 76 | double nmea_distance_ellipsoid( 77 | const nmeaPOS *from_pos, 78 | const nmeaPOS *to_pos, 79 | double *from_azimuth, 80 | double *to_azimuth 81 | ); 82 | 83 | int nmea_move_horz( 84 | const nmeaPOS *start_pos, 85 | nmeaPOS *end_pos, 86 | double azimuth, 87 | double distance 88 | ); 89 | 90 | int nmea_move_horz_ellipsoid( 91 | const nmeaPOS *start_pos, 92 | nmeaPOS *end_pos, 93 | double azimuth, 94 | double distance, 95 | double *end_azimuth 96 | ); 97 | 98 | #ifdef __cplusplus 99 | } 100 | #endif 101 | 102 | #endif /* __NMEA_GMATH_H__ */ 103 | -------------------------------------------------------------------------------- /include/nmea/info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /*! \file */ 22 | 23 | #ifndef __NMEA_INFO_H__ 24 | #define __NMEA_INFO_H__ 25 | 26 | #include 27 | #include 28 | 29 | #define NMEA_SIG_BAD (0) 30 | #define NMEA_SIG_LOW (1) 31 | #define NMEA_SIG_MID (2) 32 | #define NMEA_SIG_HIGH (3) 33 | 34 | #define NMEA_FIX_BAD (1) 35 | #define NMEA_FIX_2D (2) 36 | #define NMEA_FIX_3D (3) 37 | 38 | #define NMEA_MAXSAT (12) 39 | #define NMEA_SATINPACK (4) 40 | #define NMEA_NSATPACKS (NMEA_MAXSAT / NMEA_SATINPACK) 41 | 42 | #define NMEA_DEF_LAT (5001.2621) 43 | #define NMEA_DEF_LON (3613.0595) 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | /** 50 | * Position data in fractional degrees or radians 51 | */ 52 | typedef struct _nmeaPOS { 53 | double lat; /**< Latitude */ 54 | double lon; /**< Longitude */ 55 | 56 | } nmeaPOS; 57 | 58 | /** 59 | * Information about satellite 60 | * @see nmeaSATINFO 61 | * @see nmeaGPGSV 62 | */ 63 | typedef struct _nmeaSATELLITE { 64 | int id; /**< Satellite PRN number */ 65 | int in_use; /**< Used in position fix */ 66 | int elv; /**< Elevation in degrees, 90 maximum */ 67 | int azimuth; /**< Azimuth, degrees from true north, 000 to 359 */ 68 | int sig; /**< Signal, 00-99 dB */ 69 | 70 | } nmeaSATELLITE; 71 | 72 | /** 73 | * Information about all satellites in view 74 | * @see nmeaINFO 75 | * @see nmeaGPGSV 76 | */ 77 | typedef struct _nmeaSATINFO { 78 | int inuse; /**< Number of satellites in use (not those in view) */ 79 | int inview; /**< Total number of satellites in view */ 80 | nmeaSATELLITE sat[NMEA_MAXSAT]; /**< Satellites information */ 81 | 82 | } nmeaSATINFO; 83 | 84 | /** 85 | * Summary GPS information from all parsed packets, 86 | * used also for generating NMEA stream 87 | * @see nmea_parse 88 | * @see nmea_GPGGA2info, nmea_...2info 89 | */ 90 | typedef struct _nmeaINFO { 91 | int smask; /**< Mask specifying from which sentences data has been obtained */ 92 | 93 | nmeaTIME utc; /**< UTC of position */ 94 | 95 | int sig; /**< GPS quality indicator (0 = Invalid; 1 = Fix; 2 = Differential, 3 = Sensitive) */ 96 | int fix; /**< Operating mode, used for navigation (1 = Fix not available; 2 = 2D; 3 = 3D) */ 97 | 98 | double PDOP; /**< Position Dilution Of Precision */ 99 | double HDOP; /**< Horizontal Dilution Of Precision */ 100 | double VDOP; /**< Vertical Dilution Of Precision */ 101 | 102 | double lat; /**< Latitude in NDEG - +/-[degree][min].[sec/60] */ 103 | double lon; /**< Longitude in NDEG - +/-[degree][min].[sec/60] */ 104 | double elv; /**< Antenna altitude above/below mean sea level (geoid) in meters */ 105 | double speed; /**< Speed over the ground in kilometers/hour */ 106 | double direction; /**< Track angle in degrees True */ 107 | double declination; /**< Magnetic variation degrees (Easterly var. subtracts from true course) */ 108 | 109 | nmeaSATINFO satinfo; /**< Satellites information */ 110 | } nmeaINFO; 111 | 112 | /** 113 | * Enumeration for the fields names of a nmeaINFO structure 114 | */ 115 | typedef enum _nmeaINFO_FIELD { 116 | SMASK, UTC, SIG, FIX, PDOP, HDOP, VDOP, LAT, LON, ELV, SPEED, DIRECTION, 117 | DECLINATION, SATINFO 118 | } nmeaINFO_FIELD; 119 | 120 | void nmea_zero_INFO(nmeaINFO *info); 121 | 122 | bool nmea_INFO_has_field(int smask, nmeaINFO_FIELD fieldName); 123 | 124 | void nmea_INFO_sanitise(nmeaINFO *nmeaInfo); 125 | 126 | void nmea_INFO_unit_conversion(nmeaINFO * nmeaInfo); 127 | 128 | #ifdef __cplusplus 129 | } 130 | #endif 131 | 132 | #endif /* __NMEA_INFO_H__ */ 133 | -------------------------------------------------------------------------------- /include/nmea/nmea.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_H__ 22 | #define __NMEA_H__ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #endif /* __NMEA_H__ */ 38 | -------------------------------------------------------------------------------- /include/nmea/parse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_PARSE_H__ 22 | #define __NMEA_PARSE_H__ 23 | 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | int nmea_pack_type(const char *buff, int buff_sz); 32 | int nmea_find_tail(const char *buff, int buff_sz, int *res_crc); 33 | 34 | int nmea_parse_GPGGA(const char *buff, int buff_sz, nmeaGPGGA *pack); 35 | int nmea_parse_GPGSA(const char *buff, int buff_sz, nmeaGPGSA *pack); 36 | int nmea_parse_GPGSV(const char *buff, int buff_sz, nmeaGPGSV *pack); 37 | int nmea_parse_GPRMC(const char *buff, int buff_sz, nmeaGPRMC *pack); 38 | int nmea_parse_GPVTG(const char *buff, int buff_sz, nmeaGPVTG *pack); 39 | 40 | void nmea_GPGGA2info(nmeaGPGGA *pack, nmeaINFO *info); 41 | void nmea_GPGSA2info(nmeaGPGSA *pack, nmeaINFO *info); 42 | void nmea_GPGSV2info(nmeaGPGSV *pack, nmeaINFO *info); 43 | void nmea_GPRMC2info(nmeaGPRMC *pack, nmeaINFO *info); 44 | void nmea_GPVTG2info(nmeaGPVTG *pack, nmeaINFO *info); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif /* __NMEA_PARSE_H__ */ 51 | -------------------------------------------------------------------------------- /include/nmea/parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_PARSER_H__ 22 | #define __NMEA_PARSER_H__ 23 | 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /* 31 | * high level 32 | */ 33 | 34 | typedef struct _nmeaPARSER 35 | { 36 | void *top_node; 37 | void *end_node; 38 | unsigned char *buffer; 39 | int buff_size; 40 | int buff_use; 41 | 42 | } nmeaPARSER; 43 | 44 | int nmea_parser_init(nmeaPARSER *parser); 45 | void nmea_parser_destroy(nmeaPARSER *parser); 46 | 47 | int nmea_parse( 48 | nmeaPARSER *parser, 49 | const char *buff, int buff_sz, 50 | nmeaINFO *info 51 | ); 52 | 53 | /* 54 | * low level 55 | */ 56 | 57 | int nmea_parser_push(nmeaPARSER *parser, const char *buff, int buff_sz); 58 | int nmea_parser_top(nmeaPARSER *parser); 59 | int nmea_parser_pop(nmeaPARSER *parser, void **pack_ptr); 60 | int nmea_parser_peek(nmeaPARSER *parser, void **pack_ptr); 61 | int nmea_parser_drop(nmeaPARSER *parser); 62 | int nmea_parser_buff_clear(nmeaPARSER *parser); 63 | int nmea_parser_queue_clear(nmeaPARSER *parser); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif /* __NMEA_PARSER_H__ */ 70 | -------------------------------------------------------------------------------- /include/nmea/sentence.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /*! \file */ 22 | 23 | #ifndef __NMEA_SENTENCE_H__ 24 | #define __NMEA_SENTENCE_H__ 25 | 26 | #include 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /** 34 | * NMEA packets type which parsed and generated by library 35 | */ 36 | enum nmeaPACKTYPE 37 | { 38 | GPNON = 0x0000, /**< Unknown packet type. */ 39 | GPGGA = 0x0001, /**< GGA - Essential fix data which provide 3D location and accuracy data. */ 40 | GPGSA = 0x0002, /**< GSA - GPS receiver operating mode, SVs used for navigation, and DOP values. */ 41 | GPGSV = 0x0004, /**< GSV - Number of SVs in view, PRN numbers, elevation, azimuth & SNR values. */ 42 | GPRMC = 0x0008, /**< RMC - Recommended Minimum Specific GPS/TRANSIT Data. */ 43 | GPVTG = 0x0010 /**< VTG - Actual track made good and speed over ground. */ 44 | }; 45 | 46 | /** 47 | * GGA packet information structure (Global Positioning System Fix Data) 48 | */ 49 | typedef struct _nmeaGPGGA 50 | { 51 | nmeaTIME utc; /**< UTC of position (just time) */ 52 | double lat; /**< Latitude in NDEG - [degree][min].[sec/60] */ 53 | char ns; /**< [N]orth or [S]outh */ 54 | double lon; /**< Longitude in NDEG - [degree][min].[sec/60] */ 55 | char ew; /**< [E]ast or [W]est */ 56 | int sig; /**< GPS quality indicator (0 = Invalid; 1 = Fix; 2 = Differential, 3 = Sensitive) */ 57 | int satinuse; /**< Number of satellites in use (not those in view) */ 58 | double HDOP; /**< Horizontal dilution of precision */ 59 | double elv; /**< Antenna altitude above/below mean sea level (geoid) */ 60 | char elv_units; /**< [M]eters (Antenna height unit) */ 61 | double diff; /**< Geoidal separation (Diff. between WGS-84 earth ellipsoid and mean sea level. '-' = geoid is below WGS-84 ellipsoid) */ 62 | char diff_units; /**< [M]eters (Units of geoidal separation) */ 63 | double dgps_age; /**< Time in seconds since last DGPS update */ 64 | int dgps_sid; /**< DGPS station ID number */ 65 | 66 | } nmeaGPGGA; 67 | 68 | /** 69 | * GSA packet information structure (Satellite status) 70 | */ 71 | typedef struct _nmeaGPGSA 72 | { 73 | char fix_mode; /**< Mode (M = Manual, forced to operate in 2D or 3D; A = Automatic, 3D/2D) */ 74 | int fix_type; /**< Type, used for navigation (1 = Fix not available; 2 = 2D; 3 = 3D) */ 75 | int sat_prn[NMEA_MAXSAT]; /**< PRNs of satellites used in position fix (null for unused fields) */ 76 | double PDOP; /**< Dilution of precision */ 77 | double HDOP; /**< Horizontal dilution of precision */ 78 | double VDOP; /**< Vertical dilution of precision */ 79 | 80 | } nmeaGPGSA; 81 | 82 | /** 83 | * GSV packet information structure (Satellites in view) 84 | */ 85 | typedef struct _nmeaGPGSV 86 | { 87 | int pack_count; /**< Total number of messages of this type in this cycle */ 88 | int pack_index; /**< Message number */ 89 | int sat_count; /**< Total number of satellites in view */ 90 | nmeaSATELLITE sat_data[NMEA_SATINPACK]; 91 | 92 | } nmeaGPGSV; 93 | 94 | /** 95 | * RMC packet information structure (Recommended Minimum sentence C) 96 | */ 97 | typedef struct _nmeaGPRMC 98 | { 99 | nmeaTIME utc; /**< UTC of position */ 100 | char status; /**< Status (A = active or V = void) */ 101 | double lat; /**< Latitude in NDEG - [degree][min].[sec/60] */ 102 | char ns; /**< [N]orth or [S]outh */ 103 | double lon; /**< Longitude in NDEG - [degree][min].[sec/60] */ 104 | char ew; /**< [E]ast or [W]est */ 105 | double speed; /**< Speed over the ground in knots */ 106 | double direction; /**< Track angle in degrees True */ 107 | double declination; /**< Magnetic variation degrees (Easterly var. subtracts from true course) */ 108 | char declin_ew; /**< [E]ast or [W]est */ 109 | char mode; /**< Mode indicator of fix type (A = autonomous, D = differential, E = estimated, N = not valid, S = simulator) */ 110 | 111 | } nmeaGPRMC; 112 | 113 | /** 114 | * VTG packet information structure (Track made good and ground speed) 115 | */ 116 | typedef struct _nmeaGPVTG 117 | { 118 | double dir; /**< True track made good (degrees) */ 119 | char dir_t; /**< Fixed text 'T' indicates that track made good is relative to true north */ 120 | double dec; /**< Magnetic track made good */ 121 | char dec_m; /**< Fixed text 'M' */ 122 | double spn; /**< Ground speed, knots */ 123 | char spn_n; /**< Fixed text 'N' indicates that speed over ground is in knots */ 124 | double spk; /**< Ground speed, kilometers per hour */ 125 | char spk_k; /**< Fixed text 'K' indicates that speed over ground is in kilometers/hour */ 126 | 127 | } nmeaGPVTG; 128 | 129 | void nmea_zero_GPGGA(nmeaGPGGA *pack); 130 | void nmea_zero_GPGSA(nmeaGPGSA *pack); 131 | void nmea_zero_GPGSV(nmeaGPGSV *pack); 132 | void nmea_zero_GPRMC(nmeaGPRMC *pack); 133 | void nmea_zero_GPVTG(nmeaGPVTG *pack); 134 | 135 | #ifdef __cplusplus 136 | } 137 | #endif 138 | 139 | #endif /* __NMEA_SENTENCE_H__ */ 140 | -------------------------------------------------------------------------------- /include/nmea/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /*! \file */ 22 | 23 | #ifndef __NMEA_TIME_H__ 24 | #define __NMEA_TIME_H__ 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /** 31 | * Date and time data 32 | * @see nmea_time_now 33 | */ 34 | typedef struct _nmeaTIME { 35 | int year; /**< Years since 1900 */ 36 | int mon; /**< Months since January - [0,11] */ 37 | int day; /**< Day of the month - [1,31] */ 38 | int hour; /**< Hours since midnight - [0,23] */ 39 | int min; /**< Minutes after the hour - [0,59] */ 40 | int sec; /**< Seconds after the minute - [0,59] */ 41 | int hsec; /**< Hundredth part of second - [0,99] */ 42 | 43 | } nmeaTIME; 44 | 45 | /** 46 | * \brief Get time now to nmeaTIME structure 47 | */ 48 | void nmea_time_now(nmeaTIME *t); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /* __NMEA_TIME_H__ */ 55 | -------------------------------------------------------------------------------- /include/nmea/tok.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_TOK_H__ 22 | #define __NMEA_TOK_H__ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | int nmea_calc_crc(const char *buff, int buff_sz); 29 | int nmea_atoi(const char *str, int str_sz, int radix); 30 | double nmea_atof(const char *str, int str_sz); 31 | int nmea_printf(char *buff, int buff_sz, const char *format, ...); 32 | int nmea_scanf(const char *buff, int buff_sz, const char *format, ...); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif /* __NMEA_TOK_H__ */ 39 | -------------------------------------------------------------------------------- /include/nmea/units.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #ifndef __NMEA_UNITS_H__ 22 | #define __NMEA_UNITS_H__ 23 | 24 | /* 25 | * Distance units 26 | */ 27 | 28 | #define NMEA_TUD_YARDS (1.0936) /**< Yards, meter * NMEA_TUD_YARDS = yard */ 29 | #define NMEA_TUD_KNOTS (1.852) /**< Knots, kilometer / NMEA_TUD_KNOTS = knot */ 30 | #define NMEA_TUD_MILES (1.609) /**< Miles, kilometer / NMEA_TUD_MILES = mile */ 31 | 32 | /* 33 | * Speed units 34 | */ 35 | 36 | #define NMEA_TUS_MS (3.6) /**< Meters per seconds, (k/h) / NMEA_TUS_MS= (m/s) */ 37 | 38 | #endif /* __NMEA_UNITS_H__ */ 39 | -------------------------------------------------------------------------------- /include/nmea/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2011 Ferry Huberts 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifndef __NMEA_UTIL_H__ 21 | #define __NMEA_UTIL_H__ 22 | 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | bool nmea_string_has_invalid_chars(const char * str, const char * strName, 31 | char * report, size_t reportSize); 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif /* __NMEA_UTIL_H__ */ 38 | -------------------------------------------------------------------------------- /samples/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.inc 2 | 3 | # 4 | # Settings 5 | # 6 | 7 | SAMPLES = generate generator math parse parse_file 8 | SMPLS = $(SAMPLES:%=../build/samples/%) 9 | SMPLOBJ = $(SAMPLES:%=%/main.o) 10 | 11 | LIBS = -lm -L../lib -lnmea 12 | INCS = -I ../include 13 | 14 | 15 | # 16 | # Targets 17 | # 18 | 19 | all: all-before samples 20 | 21 | remake: clean all 22 | 23 | samples: $(SMPLS) 24 | 25 | ../build/samples/%: %/main.o 26 | @echo "[LD] $@" 27 | @$(CC) $(CCFLAGS) $< $(LIBS) -o $@ 28 | 29 | %/main.o: %/main.c Makefile ../Makefile.inc 30 | @echo "[CC] $<" 31 | @$(CC) $(CCFLAGS) $(INCS) -c $< -o $@ 32 | 33 | 34 | # 35 | # Phony Targets 36 | # 37 | 38 | .PHONY: all-before clean 39 | 40 | all-before: 41 | @mkdir -p ../build/samples 42 | 43 | clean: 44 | rm -f $(SMPLOBJ) $(SMPLS) 45 | -------------------------------------------------------------------------------- /samples/generate/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | nmeaINFO info; 29 | char buff[2048]; 30 | int gen_sz; 31 | int it; 32 | 33 | nmea_zero_INFO(&info); 34 | 35 | info.sig = 3; 36 | info.fix = 3; 37 | info.lat = 5000.0; 38 | info.lon = 3600.0; 39 | info.speed = 2.14 * NMEA_TUS_MS; 40 | info.elv = 10.86; 41 | 42 | info.satinfo.inuse = 1; 43 | info.satinfo.inview = 1; 44 | 45 | /* 46 | info.satinfo.sat[0].id = 1; 47 | info.satinfo.sat[0].in_use = 1; 48 | info.satinfo.sat[0].elv = 50; 49 | info.satinfo.sat[0].azimuth = 0; 50 | info.satinfo.sat[0].sig = 99; 51 | */ 52 | 53 | for(it = 0; it < 10; ++it) 54 | { 55 | gen_sz = nmea_generate( 56 | &buff[0], 2048, &info, 57 | GPGGA | GPGSA | GPGSV | GPRMC | GPVTG 58 | ); 59 | 60 | buff[gen_sz] = 0; 61 | printf("%s\n", &buff[0]); 62 | 63 | usleep(500000); 64 | 65 | info.speed += .1; 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /samples/generator/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | nmeaGENERATOR *gen; 29 | nmeaINFO info; 30 | char buff[2048]; 31 | int gen_sz; 32 | int it; 33 | 34 | nmea_zero_INFO(&info); 35 | 36 | if(0 == (gen = nmea_create_generator(NMEA_GEN_ROTATE, &info))) 37 | return -1; 38 | 39 | for(it = 0; it < 10000; ++it) 40 | { 41 | gen_sz = nmea_generate_from( 42 | &buff[0], 2048, &info, gen, 43 | GPGGA | GPGSA | GPGSV | GPRMC | GPVTG 44 | ); 45 | 46 | buff[gen_sz] = 0; 47 | printf("%s\n", &buff[0]); 48 | 49 | usleep(500000); 50 | } 51 | 52 | nmea_gen_destroy(gen); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /samples/math/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | #define NUM_POINTS 4 27 | 28 | int main() 29 | { 30 | const char *buff[] = { 31 | "$GPRMC,213916.199,A,4221.0377,N,07102.9778,W,0.00,,010207,,,A*6A\r\n", 32 | "$GPRMC,213917.199,A,4221.0510,N,07102.9549,W,0.23,175.43,010207,,,A*77\r\n", 33 | "$GPRMC,213925.000,A,4221.1129,N,07102.9146,W,0.00,,010207,,,A*68\r\n", 34 | "$GPRMC,111609.14,A,5001.27,N,3613.06,E,11.2,0.0,261206,0.0,E*50\r\n" 35 | }; 36 | 37 | nmeaPOS pos[NUM_POINTS], pos_moved[NUM_POINTS][2]; 38 | double dist[NUM_POINTS][2]; 39 | double azimuth[NUM_POINTS][2], azimuth_moved[NUM_POINTS]; 40 | int result[2]; 41 | int it = 0; 42 | 43 | nmeaPARSER parser; 44 | nmea_parser_init(&parser); 45 | 46 | for(it = 0; it < NUM_POINTS; ++it) 47 | { 48 | nmeaINFO info; 49 | nmea_zero_INFO(&info); 50 | (void)nmea_parse(&parser, buff[it], (int)strlen(buff[it]), &info); 51 | nmea_info2pos(&info, &pos[it]); 52 | } 53 | 54 | nmea_parser_destroy(&parser); 55 | 56 | for(it = 0; it < NUM_POINTS; ++it) 57 | { 58 | dist[it][0] = nmea_distance(&pos[0], &pos[it]); 59 | dist[it][1] = nmea_distance_ellipsoid( 60 | &pos[0], &pos[it], &azimuth[it][0], &azimuth[it][1] 61 | ); 62 | } 63 | 64 | for(it = 0; it < NUM_POINTS; ++it) 65 | { 66 | result[0] = nmea_move_horz(&pos[0], &pos_moved[it][0], azimuth[it][0], dist[it][0]); 67 | result[1] = nmea_move_horz_ellipsoid( 68 | &pos[0], &pos_moved[it][1], azimuth[it][0], dist[it][0], &azimuth_moved[it] 69 | ); 70 | 71 | } 72 | 73 | /* Output of results */ 74 | printf("Coordinate points:\n"); 75 | for(it = 0; it < NUM_POINTS; ++it) 76 | { 77 | printf( 78 | "P%d in radians: lat:%9.6lf lon:%9.6lf \tin degree: lat:%+010.6lf° lon:%+011.6lf°\n", 79 | it, pos[it].lat, pos[it].lon, nmea_radian2degree(pos[it].lat), nmea_radian2degree(pos[it].lon) 80 | ); 81 | } 82 | 83 | printf("\nCalculation results:\n"); 84 | for(it = 0; it < NUM_POINTS; ++it) 85 | { 86 | printf("\n"); 87 | printf("Distance P0 to P%d\ton spheroid: %14.3lf m\n", it, dist[it][0]); 88 | printf("Distance P0 to P%d\ton ellipsoid: %14.3lf m\n", it, dist[it][1]); 89 | printf("Azimuth P0 to P%d\tat start: %8.3lf°\tat end: %8.3lf°\n", it, nmea_radian2degree(azimuth[it][0]), nmea_radian2degree(azimuth[it][1])); 90 | printf("Move P0 to P%d\t \tAzimuth at end: %8.3lf°\n", it, nmea_radian2degree(azimuth_moved[it])); 91 | printf("Move P0 to P%d\ton spheroid: %3s lat:%+010.6lf° lon:%+011.6lf°\n", it, result[0] == 1 ? "OK" : "nOK", nmea_radian2degree(pos_moved[it][0].lat), nmea_radian2degree(pos_moved[it][0].lon)); 92 | printf("Move P0 to P%d\ton ellipsoid: %3s lat:%+010.6lf° lon:%+011.6lf°\n", it, result[0] == 1 ? "OK" : "nOK", nmea_radian2degree(pos_moved[it][1].lat), nmea_radian2degree(pos_moved[it][1].lon)); 93 | printf("Move P0 to P%d\toriginal: lat:%+010.6lf° lon:%+011.6lf°\n", it, nmea_radian2degree(pos[it].lat), nmea_radian2degree(pos[it].lon)); 94 | } 95 | 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /samples/parse/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | const char *buff[] = { 29 | "$GPRMC,173843,A,3349.896,N,11808.521,W,000.0,360.0,230108,013.4,E*69\r\n", 30 | "$GPGGA,111609.14,5001.27,N,3613.06,E,3,08,0.0,10.2,M,0.0,M,0.0,0000*70\r\n", 31 | "$GPGSV,2,1,08,01,05,005,80,02,05,050,80,03,05,095,80,04,05,140,80*7f\r\n", 32 | "$GPGSV,2,2,08,05,05,185,80,06,05,230,80,07,05,275,80,08,05,320,80*71\r\n", 33 | "$GPGSA,A,3,01,02,03,04,05,06,07,08,00,00,00,00,0.0,0.0,0.0*3a\r\n", 34 | "$GPRMC,111609.14,A,5001.27,N,3613.06,E,11.2,0.0,261206,0.0,E*50\r\n", 35 | "$GPVTG,217.5,T,208.8,M,000.00,N,000.01,K*4C\r\n" 36 | }; 37 | 38 | int it; 39 | nmeaINFO info; 40 | nmeaPARSER parser; 41 | nmeaPOS dpos; 42 | 43 | nmea_zero_INFO(&info); 44 | nmea_parser_init(&parser); 45 | 46 | for(it = 0; it < 6; ++it) 47 | { 48 | nmea_parse(&parser, buff[it], (int)strlen(buff[it]), &info); 49 | 50 | nmea_info2pos(&info, &dpos); 51 | printf("%03d, Lat: %f, Lon: %f, Sig: %d, Fix: %d\n", it, dpos.lat, 52 | dpos.lon, info.sig, info.fix); 53 | } 54 | 55 | nmea_parser_destroy(&parser); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /samples/parse_file/gpslog.txt: -------------------------------------------------------------------------------- 1 | $PSRFTXTVersion GSW3.2.1PAT_3.1.00.12-SDK001P1.00c *3F 2 | $PSRFTXTHTC GPS_ART_321000_GEN*20 3 | $PSRFTXTTOW: 258712*32 4 | $PSRFTXTWK: 1412*4F 5 | $PSRFTXTPOS: 1518885 -4470072 4274168*24 6 | $PSRFTXTCLK: 94817*02 7 | $PSRFTXTCHNL: 12*5F 8 | $PSRFTXTBaud rate: 57600 *51 9 | $GPGGA,213638.949,,,,,0,00,,,M,0.0,M,,0000*5F 10 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 11 | $GPRMC,213638.949,V,,,,,,,010207,,,N*40 12 | $GPGGA,213639.897,,,,,0,00,,,M,0.0,M,,0000*5C 13 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 14 | $GPRMC,213639.897,V,,,,,,,010207,,,N*43 15 | $GPGGA,213640.886,,,,,0,00,,,M,0.0,M,,0000*52 16 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 17 | $GPRMC,213640.886,V,,,,,,,010207,,,N*4D 18 | $GPGGA,213641.886,,,,,0,00,,,M,0.0,M,,0000*53 19 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 20 | $GPRMC,213641.886,V,,,,,,,010207,,,N*4C 21 | $GPGGA,213642.897,,,,,0,00,,,M,0.0,M,,0000*50 22 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 23 | $GPGSV,3,1,12,20,00,000,,10,00,000,,25,00,000,,27,00,000,*79 24 | $GPGSV,3,2,12,22,00,000,,07,00,000,,21,00,000,,24,00,000,*79 25 | $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78 26 | $GPRMC,213642.897,V,,,,,,,010207,,,N*4F 27 | $GPGGA,213643.886,,,,,0,00,,,M,0.0,M,,0000*51 28 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 29 | $GPRMC,213643.886,V,,,,,,,010207,,,N*4E 30 | $GPGGA,213644.886,,,,,0,00,,,M,0.0,M,,0000*56 31 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 32 | $GPRMC,213644.886,V,,,,,,,010207,,,N*49 33 | $GPGGA,213645.897,,,,,0,00,,,M,0.0,M,,0000*57 34 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 35 | $GPRMC,213645.897,V,,,,,,,010207,,,N*48 36 | $GPGGA,213646.886,,,,,0,00,,,M,0.0,M,,0000*54 37 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 38 | $GPRMC,213646.886,V,,,,,,,010207,,,N*4B 39 | $GPGGA,213647.886,,,,,0,00,,,M,0.0,M,,0000*55 40 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 41 | $GPGSV,3,1,12,20,00,000,,10,00,000,,25,00,000,,27,00,000,*79 42 | $GPGSV,3,2,12,22,00,000,,07,00,000,,21,00,000,,24,00,000,*79 43 | $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78 44 | $GPRMC,213647.886,V,,,,,,,010207,,,N*4A 45 | $GPGGA,213648.897,,,,,0,00,,,M,0.0,M,,0000*5A 46 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 47 | $GPRMC,213648.897,V,,,,,,,010207,,,N*45 48 | $GPGGA,213649.886,,,,,0,00,,,M,0.0,M,,0000*5B 49 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 50 | $GPRMC,213649.886,V,,,,,,,010207,,,N*44 51 | $GPGGA,213650.886,,,,,0,00,,,M,0.0,M,,0000*53 52 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 53 | $GPRMC,213650.886,V,,,,,,,010207,,,N*4C 54 | $GPGGA,213651.897,,,,,0,00,,,M,0.0,M,,0000*52 55 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 56 | $GPRMC,213651.897,V,,,,,,,010207,,,N*4D 57 | $GPGGA,213652.886,,,,,0,00,,,M,0.0,M,,0000*51 58 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 59 | $GPGSV,3,1,12,20,00,000,,10,00,000,,25,00,000,,27,00,000,*79 60 | $GPGSV,3,2,12,22,00,000,,07,00,000,,21,00,000,,24,00,000,*79 61 | $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78 62 | $GPRMC,213652.886,V,,,,,,,010207,,,N*4E 63 | $GPGGA,213653.886,,,,,0,00,,,M,0.0,M,,0000*50 64 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 65 | $GPRMC,213653.886,V,,,,,,,010207,,,N*4F 66 | $GPGGA,213654.898,,,,,0,00,,,M,0.0,M,,0000*58 67 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 68 | $GPRMC,213654.898,V,,,,,,,010207,,,N*47 69 | $GPGGA,213655.886,,,,,0,00,,,M,0.0,M,,0000*56 70 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 71 | $GPRMC,213655.886,V,,,,,,,010207,,,N*49 72 | $GPGGA,213656.886,,,,,0,00,,,M,0.0,M,,0000*55 73 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 74 | $GPRMC,213656.886,V,,,,,,,010207,,,N*4A 75 | $GPGGA,213657.897,,,,,0,00,,,M,0.0,M,,0000*54 76 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 77 | $GPGSV,3,1,12,20,00,000,,10,00,000,,25,00,000,,27,00,000,*79 78 | $GPGSV,3,2,12,22,00,000,,07,00,000,,21,00,000,,24,00,000,*79 79 | $GPGSV,3,3,12,16,00,000,,28,00,000,,26,00,000,,29,00,000,*78 80 | $GPRMC,213657.897,V,,,,,,,010207,,,N*4B 81 | $GPGGA,213658.886,,,,,0,00,,,M,0.0,M,,0000*5B 82 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 83 | $GPRMC,213658.886,V,,,,,,,010207,,,N*44 84 | $GPGGA,213659.886,,,,,0,00,,,M,0.0,M,,0000*5A 85 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 86 | $GPRMC,213659.886,V,,,,,,,010207,,,N*45 87 | $GPGGA,213700.880,,,,,0,00,,,M,0.0,M,,0000*51 88 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 89 | $GPRMC,213700.880,V,,,,,,,010207,,,N*4E 90 | $GPGGA,213701.880,,,,,0,00,,,M,0.0,M,,0000*50 91 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 92 | $GPRMC,213701.880,V,,,,,,,010207,,,N*4F 93 | $GPGGA,213702.880,,,,,0,00,,,M,0.0,M,,0000*53 94 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 95 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,,10,46,055,*70 96 | $GPGSV,3,2,10,06,41,282,,02,23,120,,21,21,303,,08,16,068,*73 97 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 98 | $GPRMC,213702.880,V,,,,,,,010207,,,N*4C 99 | $GPGGA,213703.880,,,,,0,00,,,M,0.0,M,,0000*52 100 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 101 | $GPRMC,213703.880,V,,,,,,,010207,,,N*4D 102 | $GPGGA,213704.880,,,,,0,00,,,M,0.0,M,,0000*55 103 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 104 | $GPRMC,213704.880,V,,,,,,,010207,,,N*4A 105 | $GPGGA,213705.879,,,,,0,00,,,M,0.0,M,,0000*52 106 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 107 | $GPRMC,213705.879,V,,,,,,,010207,,,N*4D 108 | $GPGGA,213706.879,,,,,0,00,,,M,0.0,M,,0000*51 109 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 110 | $GPRMC,213706.879,V,,,,,,,010207,,,N*4E 111 | $GPGGA,213707.879,,,,,0,00,,,M,0.0,M,,0000*50 112 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 113 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,,10,46,055,*70 114 | $GPGSV,3,2,10,06,41,282,,02,23,120,,21,21,303,,08,16,068,*73 115 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 116 | $GPRMC,213707.879,V,,,,,,,010207,,,N*4F 117 | $GPGGA,213708.879,,,,,0,00,,,M,0.0,M,,0000*5F 118 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 119 | $GPRMC,213708.879,V,,,,,,,010207,,,N*40 120 | $GPGGA,213709.879,,,,,0,00,,,M,0.0,M,,0000*5E 121 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 122 | $GPRMC,213709.879,V,,,,,,,010207,,,N*41 123 | $GPGGA,213710.879,,,,,0,00,,,M,0.0,M,,0000*56 124 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 125 | $GPRMC,213710.879,V,,,,,,,010207,,,N*49 126 | $GPGGA,213711.879,,,,,0,00,,,M,0.0,M,,0000*57 127 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 128 | $GPRMC,213711.879,V,,,,,,,010207,,,N*48 129 | $GPGGA,213712.879,,,,,0,00,,,M,0.0,M,,0000*54 130 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 131 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,,10,46,055,*70 132 | $GPGSV,3,2,10,06,41,282,,02,23,120,,21,21,303,,08,16,068,*73 133 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 134 | $GPRMC,213712.879,V,,,,,,,010207,,,N*4B 135 | $GPGGA,213713.879,,,,,0,00,,,M,0.0,M,,0000*55 136 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 137 | $GPRMC,213713.879,V,,,,,,,010207,,,N*4A 138 | $GPGGA,213714.879,,,,,0,00,,,M,0.0,M,,0000*52 139 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 140 | $GPRMC,213714.879,V,,,,,,,010207,,,N*4D 141 | $GPGGA,213715.879,,,,,0,00,,,M,0.0,M,,0000*53 142 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 143 | $GPRMC,213715.879,V,,,,,,,010207,,,N*4C 144 | $GPGGA,213716.879,,,,,0,00,,,M,0.0,M,,0000*50 145 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 146 | $GPRMC,213716.879,V,,,,,,,010207,,,N*4F 147 | $GPGGA,213717.879,,,,,0,00,,,M,0.0,M,,0000*51 148 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 149 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,25,10,46,055,*77 150 | $GPGSV,3,2,10,06,41,282,35,02,23,120,,21,21,303,,08,16,068,*75 151 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 152 | $GPRMC,213717.879,V,,,,,,,010207,,,N*4E 153 | $GPGGA,213718.879,,,,,0,00,,,M,0.0,M,,0000*5E 154 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 155 | $GPRMC,213718.879,V,,,,,,,010207,,,N*41 156 | $GPGGA,213719.879,,,,,0,00,,,M,0.0,M,,0000*5F 157 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 158 | $GPRMC,213719.879,V,,,,,,,010207,,,N*40 159 | $GPGGA,213720.879,,,,,0,00,,,M,0.0,M,,0000*55 160 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 161 | $GPRMC,213720.879,V,,,,,,,010207,,,N*4A 162 | $GPGGA,213721.879,,,,,0,00,,,M,0.0,M,,0000*54 163 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 164 | $GPRMC,213721.879,V,,,,,,,010207,,,N*4B 165 | $GPGGA,213722.879,,,,,0,00,,,M,0.0,M,,0000*57 166 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 167 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,31,10,46,055,*72 168 | $GPGSV,3,2,10,06,41,282,34,02,23,120,,21,21,303,,08,16,068,*74 169 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 170 | $GPRMC,213722.879,V,,,,,,,010207,,,N*48 171 | $GPGGA,213723.990,,,,,0,00,,,M,0.0,M,,0000*50 172 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 173 | $GPRMC,213723.990,V,,,,,,,010207,,,N*4F 174 | $GPGGA,213724.990,,,,,0,00,,,M,0.0,M,,0000*57 175 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 176 | $GPRMC,213724.990,V,,,,,,,010207,,,N*48 177 | $GPGGA,213725.990,,,,,0,00,,,M,0.0,M,,0000*56 178 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 179 | $GPRMC,213725.990,V,,,,,,,010207,,,N*49 180 | $GPGGA,213726.990,,,,,0,00,,,M,0.0,M,,0000*55 181 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 182 | $GPRMC,213726.990,V,,,,,,,010207,,,N*4A 183 | $GPGGA,213727.990,,,,,0,00,,,M,0.0,M,,0000*54 184 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 185 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,30,10,46,055,*73 186 | $GPGSV,3,2,10,06,41,282,34,02,23,120,,21,21,303,,08,16,068,*74 187 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 188 | $GPRMC,213727.990,V,,,,,,,010207,,,N*4B 189 | $GPGGA,213728.990,,,,,0,00,,,M,0.0,M,,0000*5B 190 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 191 | $GPRMC,213728.990,V,,,,,,,010207,,,N*44 192 | $GPGGA,213729.990,,,,,0,00,,,M,0.0,M,,0000*5A 193 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 194 | $GPRMC,213729.990,V,,,,,,,010207,,,N*45 195 | $GPGGA,213730.990,,,,,0,00,,,M,0.0,M,,0000*52 196 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 197 | $GPRMC,213730.990,V,,,,,,,010207,,,N*4D 198 | $GPGGA,213731.990,,,,,0,00,,,M,0.0,M,,0000*53 199 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 200 | $GPRMC,213731.990,V,,,,,,,010207,,,N*4C 201 | $GPGGA,213732.990,,,,,0,00,,,M,0.0,M,,0000*50 202 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 203 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,27,10,46,055,*75 204 | $GPGSV,3,2,10,06,41,282,35,02,23,120,,21,21,303,,08,16,068,*75 205 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 206 | $GPRMC,213732.990,V,,,,,,,010207,,,N*4F 207 | $GPGGA,213733.990,,,,,0,00,,,M,0.0,M,,0000*51 208 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 209 | $GPRMC,213733.990,V,,,,,,,010207,,,N*4E 210 | $GPGGA,213734.990,,,,,0,00,,,M,0.0,M,,0000*56 211 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 212 | $GPRMC,213734.990,V,,,,,,,010207,,,N*49 213 | $GPGGA,213735.990,,,,,0,00,,,M,0.0,M,,0000*57 214 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 215 | $GPRMC,213735.990,V,,,,,,,010207,,,N*48 216 | $GPGGA,213736.990,,,,,0,00,,,M,0.0,M,,0000*54 217 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 218 | $GPRMC,213736.990,V,,,,,,,010207,,,N*4B 219 | $GPGGA,213737.990,,,,,0,00,,,M,0.0,M,,0000*55 220 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 221 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,28,10,46,055,*7A 222 | $GPGSV,3,2,10,06,41,282,34,02,23,120,,21,21,303,,08,16,068,*74 223 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 224 | $GPRMC,213737.990,V,,,,,,,010207,,,N*4A 225 | $GPGGA,213738.990,,,,,0,00,,,M,0.0,M,,0000*5A 226 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 227 | $GPRMC,213738.990,V,,,,,,,010207,,,N*45 228 | $GPGGA,213739.990,,,,,0,00,,,M,0.0,M,,0000*5B 229 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 230 | $GPRMC,213739.990,V,,,,,,,010207,,,N*44 231 | $GPGGA,213740.990,,,,,0,00,,,M,0.0,M,,0000*55 232 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 233 | $GPRMC,213740.990,V,,,,,,,010207,,,N*4A 234 | $GPGGA,213741.990,,,,,0,00,,,M,0.0,M,,0000*54 235 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 236 | $GPRMC,213741.990,V,,,,,,,010207,,,N*4B 237 | $GPGGA,213742.990,,,,,0,00,,,M,0.0,M,,0000*57 238 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 239 | $GPGSV,3,1,10,24,83,052,,29,63,170,,26,55,179,28,10,46,055,*7A 240 | $GPGSV,3,2,10,06,41,282,34,02,23,120,,21,21,303,,08,16,068,*74 241 | $GPGSV,3,3,10,27,10,042,,18,08,257,*7B 242 | $GPRMC,213742.990,V,,,,,,,010207,,,N*48 243 | $GPGGA,213743.990,,,,,0,00,,,M,0.0,M,,0000*56 244 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 245 | $GPRMC,213743.990,V,,,,,,,010207,,,N*49 246 | $GPGGA,213744.990,,,,,0,00,,,M,0.0,M,,0000*51 247 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 248 | $GPRMC,213744.990,V,,,,,,,010207,,,N*4E 249 | $GPGGA,213745.990,,,,,0,00,,,M,0.0,M,,0000*50 250 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 251 | $GPRMC,213745.990,V,,,,,,,010207,,,N*4F 252 | $GPGGA,213746.990,,,,,0,00,,,M,0.0,M,,0000*53 253 | $PSRFTXTVersion GSW3.2.1PAT_3.1.00.12-SDK001P1.00c *3F 254 | $PSRFTXTHTC GPS_ART_321000_GEN*20 255 | $PSRFTXTTOW: 423546*3B 256 | $PSRFTXTWK: 1412*4F 257 | $PSRFTXTPOS: 1533096 -4464909 4274442*2A 258 | $PSRFTXTCLK: 94810*05 259 | $PSRFTXTCHNL: 12*5F 260 | $PSRFTXTBaud rate: 57600 *51 261 | $GPGGA,213912.270,,,,,0,00,,,M,0.0,M,,0000*59 262 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 263 | $GPRMC,213912.270,V,,,,,,,010207,,,N*46 264 | $GPGGA,213913.211,,,,,0,00,,,M,0.0,M,,0000*5F 265 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 266 | $GPRMC,213913.211,V,,,,,,,010207,,,N*40 267 | $GPGGA,213914.200,,,,,0,00,,,M,0.0,M,,0000*58 268 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 269 | $GPRMC,213914.200,V,,,,,,,010207,,,N*47 270 | $GPGGA,213915.196,,,,,0,00,,,M,0.0,M,,0000*55 271 | $GPGSA,A,1,,,,,,,,,,,,,,,*1E 272 | $GPRMC,213915.196,V,,,,,,,010207,,,N*4A 273 | $GPGGA,213916.199,4221.0377,N,07102.9778,W,1,03,13.4,-32.4,M,-33.7,M,,0000*45 274 | $GPGSA,A,2,26,07,06,,,,,,,,,,13.4,13.4,1.0*37 275 | $GPGSV,3,1,11,26,55,180,28,06,46,258,33,07,43,272,30,21,22,304,*77 276 | $GPGSV,3,2,11,24,83,061,,29,64,170,,10,45,056,,02,23,121,*7F 277 | $GPGSV,3,3,11,08,17,067,,27,10,041,,18,05,257,*4B 278 | $GPRMC,213916.199,A,4221.0377,N,07102.9778,W,0.00,,010207,,,A*6A 279 | $GPGGA,213917.199,4221.0510,N,07102.9549,W,1,04,3.9,-65.2,M,-33.7,M,,0000*7C 280 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.1,3.9,1.0*3A 281 | $GPRMC,213917.199,A,4221.0510,N,07102.9549,W,0.23,175.43,010207,,,A*77 282 | $GPGGA,213918.199,4221.0853,N,07102.9382,W,1,04,3.9,50.0,M,-33.7,M,,0000*51 283 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.1,3.9,1.0*3A 284 | $GPRMC,213918.199,A,4221.0853,N,07102.9382,W,0.95,326.55,010207,,,A*7D 285 | $GPGGA,213919.000,4221.0975,N,07102.9300,W,1,04,3.9,86.2,M,-33.7,M,,0000*57 286 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.1,3.9,1.0*3A 287 | $GPRMC,213919.000,A,4221.0975,N,07102.9300,W,0.55,332.53,010207,,,A*7D 288 | $GPGGA,213920.000,4221.1129,N,07102.9146,W,1,04,3.9,129.7,M,-33.7,M,,0000*6C 289 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.1,3.9,1.0*3A 290 | $GPRMC,213920.000,A,4221.1129,N,07102.9146,W,0.00,,010207,,,A*6D 291 | $GPGGA,213921.000,4221.1129,N,07102.9146,W,1,04,3.9,129.7,M,-33.7,M,,0000*6D 292 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.1,3.9,1.0*3A 293 | $GPGSV,3,1,11,26,55,180,29,06,46,258,26,07,43,272,33,21,22,304,26*75 294 | $GPGSV,3,2,11,24,83,061,,29,64,170,,10,45,056,,02,23,121,*7F 295 | $GPGSV,3,3,11,08,17,067,28,27,10,041,,18,05,257,*41 296 | $GPRMC,213921.000,A,4221.1129,N,07102.9146,W,0.00,,010207,,,A*6C 297 | $GPGGA,213922.000,4221.1129,N,07102.9146,W,1,04,3.9,129.7,M,-33.7,M,,0000*6E 298 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.1,3.9,1.0*3A 299 | $GPRMC,213922.000,A,4221.1129,N,07102.9146,W,0.00,,010207,,,A*6F 300 | $GPGGA,213923.000,4221.1129,N,07102.9146,W,1,04,3.9,129.7,M,-33.7,M,,0000*6F 301 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.1,3.9,1.0*3A 302 | $GPRMC,213923.000,A,4221.1129,N,07102.9146,W,0.00,,010207,,,A*6E 303 | $GPGGA,213924.000,4221.1129,N,07102.9146,W,1,04,3.9,129.7,M,-33.7,M,,0000*68 304 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.0,3.9,1.0*3B 305 | $GPRMC,213924.000,A,4221.1129,N,07102.9146,W,0.00,,010207,,,A*69 306 | $GPGGA,213925.000,4221.1129,N,07102.9146,W,1,04,3.9,129.7,M,-33.7,M,,0000*69 307 | $GPGSA,A,3,26,07,06,21,,,,,,,,,4.0,3.9,1.0*3B 308 | $GPRMC,213925.000,A,4221.1129,N,07102.9146,W,0.00,,010207,,,A*68 309 | $GPGGA,213926.000,4221.1112,N,07102.9177,W,1,04,3.9,136.5,M,-33.7,M,,0000*6C 310 | 311 | 312 | -------------------------------------------------------------------------------- /samples/parse_file/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | void trace(const char *str, int str_size) 29 | { 30 | printf("Trace: "); 31 | write(1, str, str_size); 32 | printf("\n"); 33 | } 34 | void error(const char *str, int str_size) 35 | { 36 | printf("Error: "); 37 | write(1, str, str_size); 38 | printf("\n"); 39 | } 40 | 41 | int main() 42 | { 43 | static const char * filename = "../../samples/parse_file/gpslog.txt"; 44 | nmeaINFO info; 45 | nmeaPARSER parser; 46 | FILE *file; 47 | char buff[2048]; 48 | int size, it = 0; 49 | nmeaPOS dpos; 50 | 51 | file = fopen(filename, "rb"); 52 | 53 | if(!file) { 54 | printf("Could not open file %s\n", filename); 55 | return -1; 56 | } 57 | 58 | nmea_property()->trace_func = &trace; 59 | nmea_property()->error_func = &error; 60 | 61 | nmea_zero_INFO(&info); 62 | nmea_parser_init(&parser); 63 | 64 | /* 65 | while(1) 66 | { 67 | */ 68 | 69 | while(!feof(file)) 70 | { 71 | size = (int)fread(&buff[0], 1, 100, file); 72 | 73 | nmea_parse(&parser, &buff[0], size, &info); 74 | 75 | nmea_info2pos(&info, &dpos); 76 | 77 | printf( 78 | "%03d, Lat: %f, Lon: %f, Sig: %d, Fix: %d\n", 79 | it++, dpos.lat, dpos.lon, info.sig, info.fix 80 | ); 81 | } 82 | 83 | fseek(file, 0, SEEK_SET); 84 | 85 | /* 86 | } 87 | */ 88 | 89 | nmea_parser_destroy(&parser); 90 | fclose(file); 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /src/context.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | nmeaPROPERTY * nmea_property() 29 | { 30 | static nmeaPROPERTY prop = { 31 | 0, 0, NMEA_DEF_PARSEBUFF 32 | }; 33 | 34 | return ∝ 35 | } 36 | 37 | void nmea_trace(const char *str, ...) 38 | { 39 | int size; 40 | va_list arg_list; 41 | char buff[NMEA_DEF_PARSEBUFF]; 42 | nmeaTraceFunc func = nmea_property()->trace_func; 43 | 44 | if(func) 45 | { 46 | va_start(arg_list, str); 47 | size = NMEA_POSIX(vsnprintf)(&buff[0], NMEA_DEF_PARSEBUFF - 1, str, arg_list); 48 | va_end(arg_list); 49 | 50 | if(size > 0) 51 | (*func)(&buff[0], size); 52 | } 53 | } 54 | 55 | void nmea_trace_buff(const char *buff, int buff_size) 56 | { 57 | nmeaTraceFunc func = nmea_property()->trace_func; 58 | if(func && buff_size) 59 | (*func)(buff, buff_size); 60 | } 61 | 62 | void nmea_error(const char *str, ...) 63 | { 64 | int size; 65 | va_list arg_list; 66 | char buff[NMEA_DEF_PARSEBUFF]; 67 | nmeaErrorFunc func = nmea_property()->error_func; 68 | 69 | if(func) 70 | { 71 | va_start(arg_list, str); 72 | size = NMEA_POSIX(vsnprintf)(&buff[0], NMEA_DEF_PARSEBUFF - 1, str, arg_list); 73 | va_end(arg_list); 74 | 75 | if(size > 0) 76 | (*func)(&buff[0], size); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/generate.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | int nmea_gen_GPGGA(char *buff, int buff_sz, nmeaGPGGA *pack) 29 | { 30 | return nmea_printf(buff, buff_sz, 31 | "$GPGGA,%02d%02d%02d.%02d,%09.4f,%C,%010.4f,%C,%1d,%02d,%03.1f,%03.1f,%C,%03.1f,%C,%03.1f,%04d", 32 | pack->utc.hour, pack->utc.min, pack->utc.sec, pack->utc.hsec, 33 | pack->lat, pack->ns, pack->lon, pack->ew, 34 | pack->sig, pack->satinuse, pack->HDOP, pack->elv, pack->elv_units, 35 | pack->diff, pack->diff_units, pack->dgps_age, pack->dgps_sid); 36 | } 37 | 38 | int nmea_gen_GPGSA(char *buff, int buff_sz, nmeaGPGSA *pack) 39 | { 40 | return nmea_printf(buff, buff_sz, 41 | "$GPGSA,%C,%1d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,%03.1f,%03.1f,%03.1f", 42 | pack->fix_mode, pack->fix_type, 43 | pack->sat_prn[0], pack->sat_prn[1], pack->sat_prn[2], pack->sat_prn[3], pack->sat_prn[4], pack->sat_prn[5], 44 | pack->sat_prn[6], pack->sat_prn[7], pack->sat_prn[8], pack->sat_prn[9], pack->sat_prn[10], pack->sat_prn[11], 45 | pack->PDOP, pack->HDOP, pack->VDOP); 46 | } 47 | 48 | int nmea_gen_GPGSV(char *buff, int buff_sz, nmeaGPGSV *pack) 49 | { 50 | return nmea_printf(buff, buff_sz, 51 | "$GPGSV,%1d,%1d,%02d," 52 | "%02d,%02d,%03d,%02d," 53 | "%02d,%02d,%03d,%02d," 54 | "%02d,%02d,%03d,%02d," 55 | "%02d,%02d,%03d,%02d", 56 | pack->pack_count, pack->pack_index + 1, pack->sat_count, 57 | pack->sat_data[0].id, pack->sat_data[0].elv, pack->sat_data[0].azimuth, pack->sat_data[0].sig, 58 | pack->sat_data[1].id, pack->sat_data[1].elv, pack->sat_data[1].azimuth, pack->sat_data[1].sig, 59 | pack->sat_data[2].id, pack->sat_data[2].elv, pack->sat_data[2].azimuth, pack->sat_data[2].sig, 60 | pack->sat_data[3].id, pack->sat_data[3].elv, pack->sat_data[3].azimuth, pack->sat_data[3].sig); 61 | } 62 | 63 | int nmea_gen_GPRMC(char *buff, int buff_sz, nmeaGPRMC *pack) 64 | { 65 | return nmea_printf(buff, buff_sz, 66 | "$GPRMC,%02d%02d%02d.%02d,%C,%09.4f,%C,%010.4f,%C,%03.1f,%03.1f,%02d%02d%02d,%03.1f,%C,%C", 67 | pack->utc.hour, pack->utc.min, pack->utc.sec, pack->utc.hsec, 68 | pack->status, pack->lat, pack->ns, pack->lon, pack->ew, 69 | pack->speed, pack->direction, 70 | pack->utc.day, pack->utc.mon + 1, pack->utc.year - 100, 71 | pack->declination, pack->declin_ew, pack->mode); 72 | } 73 | 74 | int nmea_gen_GPVTG(char *buff, int buff_sz, nmeaGPVTG *pack) 75 | { 76 | return nmea_printf(buff, buff_sz, 77 | "$GPVTG,%.1f,%C,%.1f,%C,%.1f,%C,%.1f,%C", 78 | pack->dir, pack->dir_t, 79 | pack->dec, pack->dec_m, 80 | pack->spn, pack->spn_n, 81 | pack->spk, pack->spk_k); 82 | } 83 | 84 | void nmea_info2GPGGA(const nmeaINFO *info, nmeaGPGGA *pack) 85 | { 86 | nmea_zero_GPGGA(pack); 87 | 88 | pack->utc = info->utc; 89 | pack->lat = fabs(info->lat); 90 | pack->ns = ((info->lat > 0)?'N':'S'); 91 | pack->lon = fabs(info->lon); 92 | pack->ew = ((info->lon > 0)?'E':'W'); 93 | pack->sig = info->sig; 94 | pack->satinuse = info->satinfo.inuse; 95 | pack->HDOP = info->HDOP; 96 | pack->elv = info->elv; 97 | } 98 | 99 | void nmea_info2GPGSA(const nmeaINFO *info, nmeaGPGSA *pack) 100 | { 101 | int it; 102 | 103 | nmea_zero_GPGSA(pack); 104 | 105 | pack->fix_type = info->fix; 106 | pack->PDOP = info->PDOP; 107 | pack->HDOP = info->HDOP; 108 | pack->VDOP = info->VDOP; 109 | 110 | for(it = 0; it < NMEA_MAXSAT; ++it) 111 | { 112 | pack->sat_prn[it] = 113 | ((info->satinfo.sat[it].in_use)?info->satinfo.sat[it].id:0); 114 | } 115 | } 116 | 117 | int nmea_gsv_npack(int sat_count) 118 | { 119 | int pack_count = (int)ceil(((double)sat_count) / NMEA_SATINPACK); 120 | 121 | if(0 == pack_count) 122 | pack_count = 1; 123 | 124 | return pack_count; 125 | } 126 | 127 | void nmea_info2GPGSV(const nmeaINFO *info, nmeaGPGSV *pack, int pack_idx) 128 | { 129 | int sit, pit; 130 | 131 | nmea_zero_GPGSV(pack); 132 | 133 | pack->sat_count = (info->satinfo.inview <= NMEA_MAXSAT)?info->satinfo.inview:NMEA_MAXSAT; 134 | pack->pack_count = nmea_gsv_npack(pack->sat_count); 135 | 136 | if(pack->pack_count == 0) 137 | pack->pack_count = 1; 138 | 139 | if(pack_idx >= pack->pack_count) 140 | pack->pack_index = pack_idx % pack->pack_count; 141 | else 142 | pack->pack_index = pack_idx; 143 | 144 | for(pit = 0, sit = pack->pack_index * NMEA_SATINPACK; pit < NMEA_SATINPACK; ++pit, ++sit) 145 | pack->sat_data[pit] = info->satinfo.sat[sit]; 146 | } 147 | 148 | void nmea_info2GPRMC(const nmeaINFO *info, nmeaGPRMC *pack) 149 | { 150 | nmea_zero_GPRMC(pack); 151 | 152 | pack->utc = info->utc; 153 | pack->status = ((info->sig > 0)?'A':'V'); 154 | pack->lat = fabs(info->lat); 155 | pack->ns = ((info->lat > 0)?'N':'S'); 156 | pack->lon = fabs(info->lon); 157 | pack->ew = ((info->lon > 0)?'E':'W'); 158 | pack->speed = info->speed / NMEA_TUD_KNOTS; 159 | pack->direction = info->direction; 160 | pack->declination = info->declination; 161 | pack->declin_ew = 'E'; 162 | pack->mode = ((info->sig > 0)?'A':'N'); 163 | } 164 | 165 | void nmea_info2GPVTG(const nmeaINFO *info, nmeaGPVTG *pack) 166 | { 167 | nmea_zero_GPVTG(pack); 168 | 169 | pack->dir = info->direction; 170 | pack->dec = info->declination; 171 | pack->spn = info->speed / NMEA_TUD_KNOTS; 172 | pack->spk = info->speed; 173 | } 174 | 175 | int nmea_generate( 176 | char *buff, int buff_sz, 177 | const nmeaINFO *info, 178 | int generate_mask 179 | ) 180 | { 181 | int gen_count = 0, gsv_it, gsv_count; 182 | int pack_mask = generate_mask; 183 | 184 | nmeaGPGGA gga; 185 | nmeaGPGSA gsa; 186 | nmeaGPGSV gsv; 187 | nmeaGPRMC rmc; 188 | nmeaGPVTG vtg; 189 | 190 | if(!buff) 191 | return 0; 192 | 193 | while(pack_mask) 194 | { 195 | if(pack_mask & GPGGA) 196 | { 197 | nmea_info2GPGGA(info, &gga); 198 | gen_count += nmea_gen_GPGGA(buff + gen_count, buff_sz - gen_count, &gga); 199 | pack_mask &= ~GPGGA; 200 | } 201 | else if(pack_mask & GPGSA) 202 | { 203 | nmea_info2GPGSA(info, &gsa); 204 | gen_count += nmea_gen_GPGSA(buff + gen_count, buff_sz - gen_count, &gsa); 205 | pack_mask &= ~GPGSA; 206 | } 207 | else if(pack_mask & GPGSV) 208 | { 209 | gsv_count = nmea_gsv_npack(info->satinfo.inview); 210 | for(gsv_it = 0; gsv_it < gsv_count && buff_sz - gen_count > 0; ++gsv_it) 211 | { 212 | nmea_info2GPGSV(info, &gsv, gsv_it); 213 | gen_count += nmea_gen_GPGSV(buff + gen_count, buff_sz - gen_count, &gsv); 214 | } 215 | pack_mask &= ~GPGSV; 216 | } 217 | else if(pack_mask & GPRMC) 218 | { 219 | nmea_info2GPRMC(info, &rmc); 220 | gen_count += nmea_gen_GPRMC(buff + gen_count, buff_sz - gen_count, &rmc); 221 | pack_mask &= ~GPRMC; 222 | } 223 | else if(pack_mask & GPVTG) 224 | { 225 | nmea_info2GPVTG(info, &vtg); 226 | gen_count += nmea_gen_GPVTG(buff + gen_count, buff_sz - gen_count, &vtg); 227 | pack_mask &= ~GPVTG; 228 | } 229 | else 230 | break; 231 | 232 | if(buff_sz - gen_count <= 0) 233 | break; 234 | } 235 | 236 | return gen_count; 237 | } 238 | -------------------------------------------------------------------------------- /src/generator.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | double nmea_random(double min, double max) 31 | { 32 | static double rand_max = RAND_MAX; 33 | double rand_val = rand(); 34 | double bounds = max - min; 35 | return min + (rand_val * bounds) / rand_max; 36 | } 37 | 38 | /* 39 | * low level 40 | */ 41 | 42 | int nmea_gen_init(nmeaGENERATOR *gen, nmeaINFO *info) 43 | { 44 | int RetVal = 1; int smask = info->smask; 45 | nmeaGENERATOR *igen = gen; 46 | 47 | nmea_zero_INFO(info); 48 | info->smask = smask; 49 | 50 | info->lat = NMEA_DEF_LAT; 51 | info->lon = NMEA_DEF_LON; 52 | 53 | while(RetVal && igen) 54 | { 55 | if(igen->init_call) 56 | RetVal = (*igen->init_call)(igen, info); 57 | igen = igen->next; 58 | } 59 | 60 | return RetVal; 61 | } 62 | 63 | int nmea_gen_loop(nmeaGENERATOR *gen, nmeaINFO *info) 64 | { 65 | int RetVal = 1; 66 | 67 | if(gen->loop_call) 68 | RetVal = (*gen->loop_call)(gen, info); 69 | 70 | if(RetVal && gen->next) 71 | RetVal = nmea_gen_loop(gen->next, info); 72 | 73 | return RetVal; 74 | } 75 | 76 | int nmea_gen_reset(nmeaGENERATOR *gen, nmeaINFO *info) 77 | { 78 | int RetVal = 1; 79 | 80 | if(gen->reset_call) 81 | RetVal = (*gen->reset_call)(gen, info); 82 | 83 | return RetVal; 84 | } 85 | 86 | void nmea_gen_destroy(nmeaGENERATOR *gen) 87 | { 88 | if(gen->next) 89 | { 90 | nmea_gen_destroy(gen->next); 91 | gen->next = 0; 92 | } 93 | 94 | if(gen->destroy_call) 95 | (*gen->destroy_call)(gen); 96 | 97 | free(gen); 98 | } 99 | 100 | void nmea_gen_add(nmeaGENERATOR *to, nmeaGENERATOR *gen) 101 | { 102 | if(to->next) 103 | nmea_gen_add(to->next, gen); 104 | else 105 | to->next = gen; 106 | } 107 | 108 | int nmea_generate_from( 109 | char *buff, int buff_sz, 110 | nmeaINFO *info, 111 | nmeaGENERATOR *gen, 112 | int generate_mask 113 | ) 114 | { 115 | int retval; 116 | 117 | if(0 != (retval = nmea_gen_loop(gen, info))) 118 | retval = nmea_generate(buff, buff_sz, info, generate_mask); 119 | 120 | return retval; 121 | } 122 | 123 | /* 124 | * NOISE generator 125 | */ 126 | 127 | int nmea_igen_noise_init(nmeaGENERATOR *gen __attribute__ ((unused)), 128 | nmeaINFO *info __attribute__ ((unused))) 129 | { 130 | return 1; 131 | } 132 | 133 | int nmea_igen_noise_loop(nmeaGENERATOR *gen __attribute__ ((unused)), 134 | nmeaINFO *info) 135 | { 136 | int it; 137 | int in_use; 138 | 139 | info->sig = (int)nmea_random(1, 3); 140 | info->PDOP = nmea_random(0, 9); 141 | info->HDOP = nmea_random(0, 9); 142 | info->VDOP = nmea_random(0, 9); 143 | info->fix = (int)nmea_random(2, 3); 144 | info->lat = nmea_random(0, 100); 145 | info->lon = nmea_random(0, 100); 146 | info->speed = nmea_random(0, 100); 147 | info->direction = nmea_random(0, 360); 148 | info->declination = nmea_random(0, 360); 149 | info->elv = (int)nmea_random(-100, 100); 150 | 151 | info->satinfo.inuse = 0; 152 | info->satinfo.inview = 0; 153 | 154 | for(it = 0; it < 12; ++it) 155 | { 156 | info->satinfo.sat[it].id = it; 157 | info->satinfo.sat[it].in_use = in_use = (int)nmea_random(0, 3); 158 | info->satinfo.sat[it].elv = (int)nmea_random(0, 90); 159 | info->satinfo.sat[it].azimuth = (int)nmea_random(0, 359); 160 | info->satinfo.sat[it].sig = (int)(in_use?nmea_random(40, 99):nmea_random(0, 40)); 161 | 162 | if(in_use) 163 | info->satinfo.inuse++; 164 | if(info->satinfo.sat[it].sig > 0) 165 | info->satinfo.inview++; 166 | } 167 | 168 | return 1; 169 | } 170 | 171 | int nmea_igen_noise_reset(nmeaGENERATOR *gen __attribute__ ((unused)), 172 | nmeaINFO *info __attribute__ ((unused))) 173 | { 174 | return 1; 175 | } 176 | 177 | /* 178 | * STATIC generator 179 | */ 180 | 181 | int nmea_igen_static_loop(nmeaGENERATOR *gen __attribute__ ((unused)), 182 | nmeaINFO *info) 183 | { 184 | nmea_time_now(&info->utc); 185 | return 1; 186 | }; 187 | 188 | int nmea_igen_static_reset(nmeaGENERATOR *gen __attribute__ ((unused)), 189 | nmeaINFO *info) 190 | { 191 | info->satinfo.inuse = 4; 192 | info->satinfo.inview = 4; 193 | 194 | info->satinfo.sat[0].id = 1; 195 | info->satinfo.sat[0].in_use = 1; 196 | info->satinfo.sat[0].elv = 50; 197 | info->satinfo.sat[0].azimuth = 0; 198 | info->satinfo.sat[0].sig = 99; 199 | 200 | info->satinfo.sat[1].id = 2; 201 | info->satinfo.sat[1].in_use = 1; 202 | info->satinfo.sat[1].elv = 50; 203 | info->satinfo.sat[1].azimuth = 90; 204 | info->satinfo.sat[1].sig = 99; 205 | 206 | info->satinfo.sat[2].id = 3; 207 | info->satinfo.sat[2].in_use = 1; 208 | info->satinfo.sat[2].elv = 50; 209 | info->satinfo.sat[2].azimuth = 180; 210 | info->satinfo.sat[2].sig = 99; 211 | 212 | info->satinfo.sat[3].id = 4; 213 | info->satinfo.sat[3].in_use = 1; 214 | info->satinfo.sat[3].elv = 50; 215 | info->satinfo.sat[3].azimuth = 270; 216 | info->satinfo.sat[3].sig = 99; 217 | 218 | return 1; 219 | } 220 | 221 | int nmea_igen_static_init(nmeaGENERATOR *gen, nmeaINFO *info) 222 | { 223 | info->sig = 3; 224 | info->fix = 3; 225 | 226 | nmea_igen_static_reset(gen, info); 227 | 228 | return 1; 229 | } 230 | 231 | /* 232 | * SAT_ROTATE generator 233 | */ 234 | 235 | int nmea_igen_rotate_loop(nmeaGENERATOR *gen __attribute__ ((unused)), 236 | nmeaINFO *info) 237 | { 238 | int it; 239 | int count = info->satinfo.inview; 240 | double deg = 360 / (count?count:1); 241 | double srt = (count?(info->satinfo.sat[0].azimuth):0) + 5; 242 | 243 | nmea_time_now(&info->utc); 244 | 245 | for(it = 0; it < count; ++it) 246 | { 247 | info->satinfo.sat[it].azimuth = 248 | (int)((srt >= 360)?srt - 360:srt); 249 | srt += deg; 250 | } 251 | 252 | return 1; 253 | }; 254 | 255 | int nmea_igen_rotate_reset(nmeaGENERATOR *gen __attribute__ ((unused)), 256 | nmeaINFO *info) 257 | { 258 | int it; 259 | double deg = 360 / 8; 260 | double srt = 0; 261 | 262 | info->satinfo.inuse = 8; 263 | info->satinfo.inview = 8; 264 | 265 | for(it = 0; it < info->satinfo.inview; ++it) 266 | { 267 | info->satinfo.sat[it].id = it + 1; 268 | info->satinfo.sat[it].in_use = 1; 269 | info->satinfo.sat[it].elv = 5; 270 | info->satinfo.sat[it].azimuth = (int)srt; 271 | info->satinfo.sat[it].sig = 80; 272 | srt += deg; 273 | } 274 | 275 | return 1; 276 | } 277 | 278 | int nmea_igen_rotate_init(nmeaGENERATOR *gen, nmeaINFO *info) 279 | { 280 | info->sig = 3; 281 | info->fix = 3; 282 | 283 | nmea_igen_rotate_reset(gen, info); 284 | 285 | return 1; 286 | } 287 | 288 | /* 289 | * POS_RANDMOVE generator 290 | */ 291 | 292 | int nmea_igen_pos_rmove_init(nmeaGENERATOR *gen __attribute__ ((unused)), 293 | nmeaINFO *info) 294 | { 295 | info->sig = 3; 296 | info->fix = 3; 297 | info->direction = info->declination = 0; 298 | info->speed = 20; 299 | return 1; 300 | } 301 | 302 | int nmea_igen_pos_rmove_loop(nmeaGENERATOR *gen __attribute__ ((unused)), 303 | nmeaINFO *info) 304 | { 305 | nmeaPOS crd; 306 | 307 | info->direction += nmea_random(-10, 10); 308 | info->speed += nmea_random(-2, 3); 309 | 310 | if(info->direction < 0) 311 | info->direction = 359 + info->direction; 312 | if(info->direction > 359) 313 | info->direction -= 359; 314 | 315 | if(info->speed > 40) 316 | info->speed = 40; 317 | if(info->speed < 1) 318 | info->speed = 1; 319 | 320 | nmea_info2pos(info, &crd); 321 | nmea_move_horz(&crd, &crd, info->direction, info->speed / 3600); 322 | nmea_pos2info(&crd, info); 323 | 324 | info->declination = info->direction; 325 | 326 | return 1; 327 | }; 328 | 329 | int nmea_igen_pos_rmove_destroy(nmeaGENERATOR *gen __attribute__ ((unused))) 330 | { 331 | return 1; 332 | }; 333 | 334 | /* 335 | * generator create 336 | */ 337 | 338 | nmeaGENERATOR * __nmea_create_generator(int type, nmeaINFO *info) 339 | { 340 | nmeaGENERATOR *gen = 0; 341 | 342 | switch(type) 343 | { 344 | case NMEA_GEN_NOISE: 345 | if(0 == (gen = malloc(sizeof(nmeaGENERATOR)))) 346 | nmea_error("Insufficient memory!"); 347 | else 348 | { 349 | memset(gen, 0, sizeof(nmeaGENERATOR)); 350 | gen->init_call = &nmea_igen_noise_init; 351 | gen->loop_call = &nmea_igen_noise_loop; 352 | gen->reset_call = &nmea_igen_noise_reset; 353 | } 354 | break; 355 | case NMEA_GEN_STATIC: 356 | case NMEA_GEN_SAT_STATIC: 357 | if(0 == (gen = malloc(sizeof(nmeaGENERATOR)))) 358 | nmea_error("Insufficient memory!"); 359 | else 360 | { 361 | memset(gen, 0, sizeof(nmeaGENERATOR)); 362 | gen->init_call = &nmea_igen_static_init; 363 | gen->loop_call = &nmea_igen_static_loop; 364 | gen->reset_call = &nmea_igen_static_reset; 365 | } 366 | break; 367 | case NMEA_GEN_SAT_ROTATE: 368 | if(0 == (gen = malloc(sizeof(nmeaGENERATOR)))) 369 | nmea_error("Insufficient memory!"); 370 | else 371 | { 372 | memset(gen, 0, sizeof(nmeaGENERATOR)); 373 | gen->init_call = &nmea_igen_rotate_init; 374 | gen->loop_call = &nmea_igen_rotate_loop; 375 | gen->reset_call = &nmea_igen_rotate_reset; 376 | } 377 | break; 378 | case NMEA_GEN_POS_RANDMOVE: 379 | if(0 == (gen = malloc(sizeof(nmeaGENERATOR)))) 380 | nmea_error("Insufficient memory!"); 381 | else 382 | { 383 | memset(gen, 0, sizeof(nmeaGENERATOR)); 384 | gen->init_call = &nmea_igen_pos_rmove_init; 385 | gen->loop_call = &nmea_igen_pos_rmove_loop; 386 | gen->destroy_call = &nmea_igen_pos_rmove_destroy; 387 | } 388 | break; 389 | default: 390 | /* case NMEA_GEN_ROTATE: */ 391 | gen = __nmea_create_generator(NMEA_GEN_SAT_ROTATE, info); 392 | nmea_gen_add(gen, __nmea_create_generator(NMEA_GEN_POS_RANDMOVE, info)); 393 | break; 394 | }; 395 | 396 | return gen; 397 | } 398 | 399 | nmeaGENERATOR * nmea_create_generator(int type, nmeaINFO *info) 400 | { 401 | nmeaGENERATOR *gen = __nmea_create_generator(type, info); 402 | 403 | if(gen) 404 | nmea_gen_init(gen, info); 405 | 406 | return gen; 407 | } 408 | 409 | void nmea_destroy_generator(nmeaGENERATOR *gen) 410 | { 411 | nmea_gen_destroy(gen); 412 | } 413 | -------------------------------------------------------------------------------- /src/gmath.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /*! \file gmath.h */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | #include 28 | 29 | /** 30 | * \brief Convert degree to radian 31 | */ 32 | double nmea_degree2radian(double val) 33 | { return (val * NMEA_PI180); } 34 | 35 | /** 36 | * \brief Convert radian to degree 37 | */ 38 | double nmea_radian2degree(double val) 39 | { return (val / NMEA_PI180); } 40 | 41 | /** 42 | * \brief Convert NDEG (NMEA degree) to fractional degree 43 | */ 44 | double nmea_ndeg2degree(double val) 45 | { 46 | double deg; 47 | double fra_part = modf(val / 100.0, °); 48 | return (deg + ((fra_part * 100.0) / 60.0)); 49 | } 50 | 51 | /** 52 | * \brief Convert fractional degree to NDEG (NMEA degree) 53 | */ 54 | double nmea_degree2ndeg(double val) 55 | { 56 | double deg; 57 | double fra_part = modf(val, °); 58 | return ((deg * 100.0) + (fra_part * 60.0)); 59 | } 60 | 61 | /** 62 | * \brief Convert NDEG (NMEA degree) to radian 63 | */ 64 | double nmea_ndeg2radian(double val) 65 | { return nmea_degree2radian(nmea_ndeg2degree(val)); } 66 | 67 | /** 68 | * \brief Convert radian to NDEG (NMEA degree) 69 | */ 70 | double nmea_radian2ndeg(double val) 71 | { return nmea_degree2ndeg(nmea_radian2degree(val)); } 72 | 73 | /** 74 | * \brief Calculate PDOP (Position Dilution Of Precision) factor 75 | */ 76 | double nmea_calc_pdop(double hdop, double vdop) 77 | { 78 | return sqrt(pow(hdop, 2) + pow(vdop, 2)); 79 | } 80 | 81 | double nmea_dop2meters(double dop) 82 | { return (dop * NMEA_DOP_FACTOR); } 83 | 84 | double nmea_meters2dop(double meters) 85 | { return (meters / NMEA_DOP_FACTOR); } 86 | 87 | /** 88 | * \brief Calculate distance between two points 89 | * \return Distance in meters 90 | */ 91 | double nmea_distance( 92 | const nmeaPOS *from_pos, /**< From position in radians */ 93 | const nmeaPOS *to_pos /**< To position in radians */ 94 | ) 95 | { 96 | double dist = ((double)NMEA_EARTHRADIUS_M) * acos( 97 | sin(to_pos->lat) * sin(from_pos->lat) + 98 | cos(to_pos->lat) * cos(from_pos->lat) * cos(to_pos->lon - from_pos->lon) 99 | ); 100 | return dist; 101 | } 102 | 103 | /** 104 | * \brief Calculate distance between two points 105 | * This function uses an algorithm for an oblate spheroid earth model. 106 | * The algorithm is described here: 107 | * http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf 108 | * \return Distance in meters 109 | */ 110 | double nmea_distance_ellipsoid( 111 | const nmeaPOS *from_pos, /**< From position in radians */ 112 | const nmeaPOS *to_pos, /**< To position in radians */ 113 | double *from_azimuth, /**< (O) azimuth at "from" position in radians */ 114 | double *to_azimuth /**< (O) azimuth at "to" position in radians */ 115 | ) 116 | { 117 | /* All variables */ 118 | double f, a, b, sqr_a, sqr_b; 119 | double L, phi1, phi2, U1, U2, sin_U1, sin_U2, cos_U1, cos_U2; 120 | double sigma, sin_sigma, cos_sigma, cos_2_sigmam, sqr_cos_2_sigmam, sqr_cos_alpha, lambda, sin_lambda, cos_lambda, delta_lambda; 121 | int remaining_steps; 122 | double sqr_u, A, B, delta_sigma; 123 | 124 | /* Check input */ 125 | NMEA_ASSERT(from_pos != 0); 126 | NMEA_ASSERT(to_pos != 0); 127 | 128 | if ((from_pos->lat == to_pos->lat) && (from_pos->lon == to_pos->lon)) 129 | { /* Identical points */ 130 | if ( from_azimuth != 0 ) 131 | *from_azimuth = 0; 132 | if ( to_azimuth != 0 ) 133 | *to_azimuth = 0; 134 | return 0; 135 | } /* Identical points */ 136 | 137 | /* Earth geometry */ 138 | f = NMEA_EARTH_FLATTENING; 139 | a = NMEA_EARTH_SEMIMAJORAXIS_M; 140 | b = (1 - f) * a; 141 | sqr_a = a * a; 142 | sqr_b = b * b; 143 | 144 | /* Calculation */ 145 | L = to_pos->lon - from_pos->lon; 146 | phi1 = from_pos->lat; 147 | phi2 = to_pos->lat; 148 | U1 = atan((1 - f) * tan(phi1)); 149 | U2 = atan((1 - f) * tan(phi2)); 150 | sin_U1 = sin(U1); 151 | sin_U2 = sin(U2); 152 | cos_U1 = cos(U1); 153 | cos_U2 = cos(U2); 154 | 155 | /* Initialize iteration */ 156 | sigma = 0; 157 | sin_sigma = sin(sigma); 158 | cos_sigma = cos(sigma); 159 | cos_2_sigmam = 0; 160 | sqr_cos_2_sigmam = cos_2_sigmam * cos_2_sigmam; 161 | sqr_cos_alpha = 0; 162 | lambda = L; 163 | sin_lambda = sin(lambda); 164 | cos_lambda = cos(lambda); 165 | delta_lambda = lambda; 166 | remaining_steps = 20; 167 | 168 | while ((delta_lambda > 1e-12) && (remaining_steps > 0)) 169 | { /* Iterate */ 170 | /* Variables */ 171 | double tmp1, tmp2, sin_alpha, cos_alpha, C, lambda_prev; 172 | 173 | /* Calculation */ 174 | tmp1 = cos_U2 * sin_lambda; 175 | tmp2 = cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda; 176 | sin_sigma = sqrt(tmp1 * tmp1 + tmp2 * tmp2); 177 | cos_sigma = sin_U1 * sin_U2 + cos_U1 * cos_U2 * cos_lambda; 178 | sin_alpha = cos_U1 * cos_U2 * sin_lambda / sin_sigma; 179 | cos_alpha = cos(asin(sin_alpha)); 180 | sqr_cos_alpha = cos_alpha * cos_alpha; 181 | cos_2_sigmam = cos_sigma - 2 * sin_U1 * sin_U2 / sqr_cos_alpha; 182 | sqr_cos_2_sigmam = cos_2_sigmam * cos_2_sigmam; 183 | C = f / 16 * sqr_cos_alpha * (4 + f * (4 - 3 * sqr_cos_alpha)); 184 | lambda_prev = lambda; 185 | sigma = asin(sin_sigma); 186 | lambda = L + 187 | (1 - C) * f * sin_alpha 188 | * (sigma + C * sin_sigma * (cos_2_sigmam + C * cos_sigma * (-1 + 2 * sqr_cos_2_sigmam))); 189 | delta_lambda = lambda_prev - lambda; 190 | if ( delta_lambda < 0 ) delta_lambda = -delta_lambda; 191 | sin_lambda = sin(lambda); 192 | cos_lambda = cos(lambda); 193 | remaining_steps--; 194 | } /* Iterate */ 195 | 196 | /* More calculation */ 197 | sqr_u = sqr_cos_alpha * (sqr_a - sqr_b) / sqr_b; 198 | A = 1 + sqr_u / 16384 * (4096 + sqr_u * (-768 + sqr_u * (320 - 175 * sqr_u))); 199 | B = sqr_u / 1024 * (256 + sqr_u * (-128 + sqr_u * (74 - 47 * sqr_u))); 200 | delta_sigma = B * sin_sigma * ( 201 | cos_2_sigmam + B / 4 * ( 202 | cos_sigma * (-1 + 2 * sqr_cos_2_sigmam) - 203 | B / 6 * cos_2_sigmam * (-3 + 4 * sin_sigma * sin_sigma) * (-3 + 4 * sqr_cos_2_sigmam) 204 | )); 205 | 206 | /* Calculate result */ 207 | if ( from_azimuth != 0 ) 208 | { 209 | double tan_alpha_1 = cos_U2 * sin_lambda / (cos_U1 * sin_U2 - sin_U1 * cos_U2 * cos_lambda); 210 | *from_azimuth = atan(tan_alpha_1); 211 | } 212 | if ( to_azimuth != 0 ) 213 | { 214 | double tan_alpha_2 = cos_U1 * sin_lambda / (-sin_U1 * cos_U2 + cos_U1 * sin_U2 * cos_lambda); 215 | *to_azimuth = atan(tan_alpha_2); 216 | } 217 | 218 | return b * A * (sigma - delta_sigma); 219 | } 220 | 221 | /** 222 | * \brief Horizontal move of point position 223 | */ 224 | int nmea_move_horz( 225 | const nmeaPOS *start_pos, /**< Start position in radians */ 226 | nmeaPOS *end_pos, /**< Result position in radians */ 227 | double azimuth, /**< Azimuth (degree) [0, 359] */ 228 | double distance /**< Distance (km) */ 229 | ) 230 | { 231 | nmeaPOS p1 = *start_pos; 232 | int RetVal = 1; 233 | 234 | distance /= NMEA_EARTHRADIUS_KM; /* Angular distance covered on earth's surface */ 235 | azimuth = nmea_degree2radian(azimuth); 236 | 237 | end_pos->lat = asin( 238 | sin(p1.lat) * cos(distance) + cos(p1.lat) * sin(distance) * cos(azimuth)); 239 | end_pos->lon = p1.lon + atan2( 240 | sin(azimuth) * sin(distance) * cos(p1.lat), cos(distance) - sin(p1.lat) * sin(end_pos->lat)); 241 | 242 | if(NMEA_POSIX(isnan)(end_pos->lat) || NMEA_POSIX(isnan)(end_pos->lon)) 243 | { 244 | end_pos->lat = 0; end_pos->lon = 0; 245 | RetVal = 0; 246 | } 247 | 248 | return RetVal; 249 | } 250 | 251 | /** 252 | * \brief Horizontal move of point position 253 | * This function uses an algorithm for an oblate spheroid earth model. 254 | * The algorithm is described here: 255 | * http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf 256 | */ 257 | int nmea_move_horz_ellipsoid( 258 | const nmeaPOS *start_pos, /**< Start position in radians */ 259 | nmeaPOS *end_pos, /**< (O) Result position in radians */ 260 | double azimuth, /**< Azimuth in radians */ 261 | double distance, /**< Distance (km) */ 262 | double *end_azimuth /**< (O) Azimuth at end position in radians */ 263 | ) 264 | { 265 | /* Variables */ 266 | double f, a, b, sqr_a, sqr_b; 267 | double phi1, tan_U1, sin_U1, cos_U1, s, alpha1, sin_alpha1, cos_alpha1; 268 | double sigma1, sin_alpha, sqr_cos_alpha, sqr_u, A, B; 269 | double sigma_initial, sigma, sigma_prev, sin_sigma, cos_sigma, cos_2_sigmam, sqr_cos_2_sigmam, delta_sigma; 270 | int remaining_steps; 271 | double tmp1, phi2, lambda, C, L; 272 | 273 | /* Check input */ 274 | NMEA_ASSERT(start_pos != 0); 275 | NMEA_ASSERT(end_pos != 0); 276 | 277 | if (fabs(distance) < 1e-12) 278 | { /* No move */ 279 | *end_pos = *start_pos; 280 | if ( end_azimuth != 0 ) *end_azimuth = azimuth; 281 | return ! (NMEA_POSIX(isnan)(end_pos->lat) || NMEA_POSIX(isnan)(end_pos->lon)); 282 | } /* No move */ 283 | 284 | /* Earth geometry */ 285 | f = NMEA_EARTH_FLATTENING; 286 | a = NMEA_EARTH_SEMIMAJORAXIS_M; 287 | b = (1 - f) * a; 288 | sqr_a = a * a; 289 | sqr_b = b * b; 290 | 291 | /* Calculation */ 292 | phi1 = start_pos->lat; 293 | tan_U1 = (1 - f) * tan(phi1); 294 | cos_U1 = 1 / sqrt(1 + tan_U1 * tan_U1); 295 | sin_U1 = tan_U1 * cos_U1; 296 | s = distance; 297 | alpha1 = azimuth; 298 | sin_alpha1 = sin(alpha1); 299 | cos_alpha1 = cos(alpha1); 300 | sigma1 = atan2(tan_U1, cos_alpha1); 301 | sin_alpha = cos_U1 * sin_alpha1; 302 | sqr_cos_alpha = 1 - sin_alpha * sin_alpha; 303 | sqr_u = sqr_cos_alpha * (sqr_a - sqr_b) / sqr_b; 304 | A = 1 + sqr_u / 16384 * (4096 + sqr_u * (-768 + sqr_u * (320 - 175 * sqr_u))); 305 | B = sqr_u / 1024 * (256 + sqr_u * (-128 + sqr_u * (74 - 47 * sqr_u))); 306 | 307 | /* Initialize iteration */ 308 | sigma_initial = s / (b * A); 309 | sigma = sigma_initial; 310 | sin_sigma = sin(sigma); 311 | cos_sigma = cos(sigma); 312 | cos_2_sigmam = cos(2 * sigma1 + sigma); 313 | sqr_cos_2_sigmam = cos_2_sigmam * cos_2_sigmam; 314 | delta_sigma = 0; 315 | sigma_prev = 2 * NMEA_PI; 316 | remaining_steps = 20; 317 | 318 | while ((fabs(sigma - sigma_prev) > 1e-12) && (remaining_steps > 0)) 319 | { /* Iterate */ 320 | cos_2_sigmam = cos(2 * sigma1 + sigma); 321 | sqr_cos_2_sigmam = cos_2_sigmam * cos_2_sigmam; 322 | sin_sigma = sin(sigma); 323 | cos_sigma = cos(sigma); 324 | delta_sigma = B * sin_sigma * ( 325 | cos_2_sigmam + B / 4 * ( 326 | cos_sigma * (-1 + 2 * sqr_cos_2_sigmam) - 327 | B / 6 * cos_2_sigmam * (-3 + 4 * sin_sigma * sin_sigma) * (-3 + 4 * sqr_cos_2_sigmam) 328 | )); 329 | sigma_prev = sigma; 330 | sigma = sigma_initial + delta_sigma; 331 | remaining_steps --; 332 | } /* Iterate */ 333 | 334 | /* Calculate result */ 335 | tmp1 = (sin_U1 * sin_sigma - cos_U1 * cos_sigma * cos_alpha1); 336 | phi2 = atan2( 337 | sin_U1 * cos_sigma + cos_U1 * sin_sigma * cos_alpha1, 338 | (1 - f) * sqrt(sin_alpha * sin_alpha + tmp1 * tmp1) 339 | ); 340 | lambda = atan2( 341 | sin_sigma * sin_alpha1, 342 | cos_U1 * cos_sigma - sin_U1 * sin_sigma * cos_alpha1 343 | ); 344 | C = f / 16 * sqr_cos_alpha * (4 + f * (4 - 3 * sqr_cos_alpha)); 345 | L = lambda - 346 | (1 - C) * f * sin_alpha * ( 347 | sigma + C * sin_sigma * 348 | (cos_2_sigmam + C * cos_sigma * (-1 + 2 * sqr_cos_2_sigmam)) 349 | ); 350 | 351 | /* Result */ 352 | end_pos->lon = start_pos->lon + L; 353 | end_pos->lat = phi2; 354 | if ( end_azimuth != 0 ) 355 | { 356 | *end_azimuth = atan2( 357 | sin_alpha, -sin_U1 * sin_sigma + cos_U1 * cos_sigma * cos_alpha1 358 | ); 359 | } 360 | return ! (NMEA_POSIX(isnan)(end_pos->lat) || NMEA_POSIX(isnan)(end_pos->lon)); 361 | } 362 | 363 | /** 364 | * \brief Convert position from INFO to radians position 365 | */ 366 | void nmea_info2pos(const nmeaINFO *info, nmeaPOS *pos) 367 | { 368 | pos->lat = nmea_ndeg2radian(info->lat); 369 | pos->lon = nmea_ndeg2radian(info->lon); 370 | } 371 | 372 | /** 373 | * \brief Convert radians position to INFOs position 374 | */ 375 | void nmea_pos2info(const nmeaPOS *pos, nmeaINFO *info) 376 | { 377 | info->lat = nmea_radian2ndeg(pos->lat); 378 | info->lon = nmea_radian2ndeg(pos->lon); 379 | } 380 | -------------------------------------------------------------------------------- /src/info.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | void nmea_zero_INFO(nmeaINFO *info) 31 | { 32 | if (!info) { 33 | return; 34 | } 35 | 36 | memset(info, 0, sizeof(nmeaINFO)); 37 | nmea_time_now(&info->utc); 38 | info->sig = NMEA_SIG_BAD; 39 | info->fix = NMEA_FIX_BAD; 40 | } 41 | 42 | /** 43 | * Determine whether a given nmeaINFO structure has a certain field. 44 | * 45 | * nmeaINFO dependencies: 46 |
 47 |  field/sentence GPGGA   GPGSA   GPGSV   GPRMC   GPVTG
 48 |  smask:         x       x       x       x       x
 49 |  utc:           x                       x
 50 |  sig:           x                       x
 51 |  fix:                   x               x
 52 |  PDOP:                  x
 53 |  HDOP:          x       x
 54 |  VDOP:                  x
 55 |  lat:           x                       x
 56 |  lon:           x                       x
 57 |  elv:           x
 58 |  speed:                                 x       x
 59 |  direction:                             x       x
 60 |  declination:                                   x
 61 |  satinfo:               x       x
 62 |  
63 | * 64 | * @param smask 65 | * the smask of a nmeaINFO structure 66 | * @param fieldName 67 | * the field name 68 | * 69 | * @return 70 | * - true when the nmeaINFO structure has the field 71 | * - false otherwise 72 | */ 73 | bool nmea_INFO_has_field(int smask, nmeaINFO_FIELD fieldName) { 74 | switch (fieldName) { 75 | case SMASK: 76 | return true; 77 | 78 | case UTC: 79 | case SIG: 80 | case LAT: 81 | case LON: 82 | return ((smask & (GPGGA | GPRMC)) != 0); 83 | 84 | case FIX: 85 | return ((smask & (GPGSA | GPRMC)) != 0); 86 | 87 | case PDOP: 88 | case VDOP: 89 | return ((smask & GPGSA) != 0); 90 | 91 | case HDOP: 92 | return ((smask & (GPGGA | GPGSA)) != 0); 93 | 94 | case ELV: 95 | return ((smask & GPGGA) != 0); 96 | 97 | case SPEED: 98 | case DIRECTION: 99 | return ((smask & (GPRMC | GPVTG)) != 0); 100 | 101 | case DECLINATION: 102 | return ((smask & GPVTG) != 0); 103 | 104 | case SATINFO: 105 | return ((smask & (GPGSA | GPGSV)) != 0); 106 | 107 | default: 108 | return false; 109 | } 110 | } 111 | 112 | /** 113 | * Sanitise the NMEA info, make sure that: 114 | * - latitude is in the range [-9000, 9000], 115 | * - longitude is in the range [-18000, 18000], 116 | * - DOPs are positive, 117 | * - speed is positive, 118 | * - direction is in the range [0, 360>. 119 | * 120 | * Time is set to the current time when not present. 121 | * 122 | * When a field is not present then it is reset to its default (NMEA_SIG_BAD, 123 | * NMEA_FIX_BAD, 0). 124 | * 125 | * Satinfo is not touched. 126 | * 127 | * @param nmeaInfo 128 | * the NMEA info structure to sanitise 129 | */ 130 | void nmea_INFO_sanitise(nmeaINFO *nmeaInfo) { 131 | double lat = 0; 132 | double lon = 0; 133 | double speed = 0; 134 | double direction = 0; 135 | bool latAdjusted = false; 136 | bool lonAdjusted = false; 137 | bool speedAdjusted = false; 138 | bool directionAdjusted = false; 139 | 140 | if (!nmeaInfo) { 141 | return; 142 | } 143 | 144 | if (!nmea_INFO_has_field(nmeaInfo->smask, UTC)) { 145 | nmea_time_now(&nmeaInfo->utc); 146 | } 147 | 148 | if (!nmea_INFO_has_field(nmeaInfo->smask, SIG)) { 149 | nmeaInfo->sig = NMEA_SIG_BAD; 150 | } 151 | 152 | if (!nmea_INFO_has_field(nmeaInfo->smask, FIX)) { 153 | nmeaInfo->fix = NMEA_FIX_BAD; 154 | } 155 | 156 | if (!nmea_INFO_has_field(nmeaInfo->smask, PDOP)) { 157 | nmeaInfo->PDOP = 0; 158 | } else { 159 | nmeaInfo->PDOP = fabs(nmeaInfo->PDOP); 160 | } 161 | 162 | if (!nmea_INFO_has_field(nmeaInfo->smask, HDOP)) { 163 | nmeaInfo->HDOP = 0; 164 | } else { 165 | nmeaInfo->HDOP = fabs(nmeaInfo->HDOP); 166 | } 167 | 168 | if (!nmea_INFO_has_field(nmeaInfo->smask, VDOP)) { 169 | nmeaInfo->VDOP = 0; 170 | } else { 171 | nmeaInfo->VDOP = fabs(nmeaInfo->VDOP); 172 | } 173 | 174 | if (!nmea_INFO_has_field(nmeaInfo->smask, LAT)) { 175 | nmeaInfo->lat = 0; 176 | } 177 | 178 | if (!nmea_INFO_has_field(nmeaInfo->smask, LON)) { 179 | nmeaInfo->lon = 0; 180 | } 181 | 182 | if (!nmea_INFO_has_field(nmeaInfo->smask, ELV)) { 183 | nmeaInfo->elv = 0; 184 | } 185 | 186 | if (!nmea_INFO_has_field(nmeaInfo->smask, SPEED)) { 187 | nmeaInfo->speed = 0; 188 | } 189 | 190 | if (!nmea_INFO_has_field(nmeaInfo->smask, DIRECTION)) { 191 | nmeaInfo->direction = 0; 192 | } 193 | 194 | if (!nmea_INFO_has_field(nmeaInfo->smask, DECLINATION)) { 195 | nmeaInfo->declination = 0; 196 | } 197 | 198 | /* satinfo is not used */ 199 | 200 | /* 201 | * lat 202 | */ 203 | 204 | lat = nmeaInfo->lat; 205 | lon = nmeaInfo->lon; 206 | 207 | /* force lat in [-18000, 18000] */ 208 | while (lat < -18000.0) { 209 | lat += 36000.0; 210 | latAdjusted = true; 211 | } 212 | while (lat > 18000.0) { 213 | lat -= 36000.0; 214 | latAdjusted = true; 215 | } 216 | 217 | /* lat is now in [-18000, 18000] */ 218 | 219 | /* force lat from <9000, 18000] in [9000, 0] */ 220 | if (lat > 9000.0) { 221 | lat = 18000.0 - lat; 222 | lon += 18000.0; 223 | latAdjusted = true; 224 | lonAdjusted = true; 225 | } 226 | 227 | /* force lat from [-18000, -9000> in [0, -9000] */ 228 | if (lat < -9000.0) { 229 | lat = -18000.0 - lat; 230 | lon += 18000.0; 231 | latAdjusted = true; 232 | lonAdjusted = true; 233 | } 234 | 235 | /* lat is now in [-9000, 9000] */ 236 | 237 | if (latAdjusted) { 238 | nmeaInfo->lat = lat; 239 | } 240 | 241 | /* 242 | * lon 243 | */ 244 | 245 | /* force lon in [-18000, 18000] */ 246 | while (lon < -18000.0) { 247 | lon += 36000.0; 248 | lonAdjusted = true; 249 | } 250 | while (lon > 18000.0) { 251 | lon -= 36000.0; 252 | lonAdjusted = true; 253 | } 254 | 255 | /* lon is now in [-18000, 18000] */ 256 | 257 | if (lonAdjusted) { 258 | nmeaInfo->lon = lon; 259 | } 260 | 261 | /* 262 | * speed 263 | */ 264 | 265 | speed = nmeaInfo->speed; 266 | direction = nmeaInfo->direction; 267 | 268 | if (speed < 0.0) { 269 | speed = -speed; 270 | direction += 180.0; 271 | speedAdjusted = true; 272 | directionAdjusted = true; 273 | } 274 | 275 | /* speed is now in [0, max> */ 276 | 277 | if (speedAdjusted) { 278 | nmeaInfo->speed = speed; 279 | } 280 | 281 | /* 282 | * direction 283 | */ 284 | 285 | /* force direction in [0, 360> */ 286 | while (direction < 0.0) { 287 | direction += 360.0; 288 | directionAdjusted = true; 289 | } 290 | while (direction >= 360.0) { 291 | direction -= 360.0; 292 | directionAdjusted = true; 293 | } 294 | 295 | /* direction is now in [0, 360> */ 296 | 297 | if (directionAdjusted) { 298 | nmeaInfo->direction = direction; 299 | } 300 | } 301 | 302 | /** 303 | * Converts the position fields to degrees and DOP fields to meters so that 304 | * all fields use normal metric units. 305 | * 306 | * @param nmeaInfo 307 | * the nmeaINFO 308 | */ 309 | void nmea_INFO_unit_conversion(nmeaINFO * nmeaInfo) { 310 | if (!nmeaInfo) { 311 | return; 312 | } 313 | 314 | /* smask (already in correct format) */ 315 | 316 | /* utc (already in correct format) */ 317 | 318 | /* sig (already in correct format) */ 319 | /* fix (already in correct format) */ 320 | 321 | if (nmea_INFO_has_field(nmeaInfo->smask, PDOP)) { 322 | nmeaInfo->PDOP = nmea_dop2meters(nmeaInfo->PDOP); 323 | } 324 | 325 | if (nmea_INFO_has_field(nmeaInfo->smask, HDOP)) { 326 | nmeaInfo->HDOP = nmea_dop2meters(nmeaInfo->HDOP); 327 | } 328 | 329 | if (nmea_INFO_has_field(nmeaInfo->smask, VDOP)) { 330 | nmeaInfo->VDOP = nmea_dop2meters(nmeaInfo->VDOP); 331 | } 332 | 333 | if (nmea_INFO_has_field(nmeaInfo->smask, LAT)) { 334 | nmeaInfo->lat = nmea_ndeg2degree(nmeaInfo->lat); 335 | } 336 | 337 | if (nmea_INFO_has_field(nmeaInfo->smask, LON)) { 338 | nmeaInfo->lon = nmea_ndeg2degree(nmeaInfo->lon); 339 | } 340 | 341 | /* elv (already in correct format) */ 342 | /* speed (already in correct format) */ 343 | /* direction (already in correct format) */ 344 | /* declination (already in correct format) */ 345 | 346 | /* satinfo (not used) */ 347 | } 348 | -------------------------------------------------------------------------------- /src/parse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /** 22 | * \file parse.h 23 | * \brief Functions of a low level for analysis of 24 | * packages of NMEA stream. 25 | * 26 | * \code 27 | * ... 28 | * ptype = nmea_pack_type( 29 | * (const char *)parser->buffer + nparsed + 1, 30 | * parser->buff_use - nparsed - 1); 31 | * 32 | * if(0 == (node = malloc(sizeof(nmeaParserNODE)))) 33 | * goto mem_fail; 34 | * 35 | * node->pack = 0; 36 | * 37 | * switch(ptype) 38 | * { 39 | * case GPGGA: 40 | * if(0 == (node->pack = malloc(sizeof(nmeaGPGGA)))) 41 | * goto mem_fail; 42 | * node->packType = GPGGA; 43 | * if(!nmea_parse_GPGGA( 44 | * (const char *)parser->buffer + nparsed, 45 | * sen_sz, (nmeaGPGGA *)node->pack)) 46 | * { 47 | * free(node); 48 | * node = 0; 49 | * } 50 | * break; 51 | * case GPGSA: 52 | * if(0 == (node->pack = malloc(sizeof(nmeaGPGSA)))) 53 | * goto mem_fail; 54 | * node->packType = GPGSA; 55 | * if(!nmea_parse_GPGSA( 56 | * (const char *)parser->buffer + nparsed, 57 | * sen_sz, (nmeaGPGSA *)node->pack)) 58 | * { 59 | * free(node); 60 | * node = 0; 61 | * } 62 | * break; 63 | * ... 64 | * \endcode 65 | */ 66 | 67 | #include 68 | 69 | #include 70 | 71 | #include 72 | #include 73 | #include 74 | #include 75 | 76 | #define NMEA_TIMEPARSE_BUF (256) 77 | 78 | int _nmea_parse_time(const char *buff, int buff_sz, nmeaTIME *res) 79 | { 80 | int success = 0; 81 | 82 | switch(buff_sz) 83 | { 84 | case sizeof("hhmmss") - 1: 85 | success = (3 == nmea_scanf(buff, buff_sz, 86 | "%2d%2d%2d", &(res->hour), &(res->min), &(res->sec) 87 | )); 88 | break; 89 | case sizeof("hhmmss.s") - 1: 90 | case sizeof("hhmmss.ss") - 1: 91 | case sizeof("hhmmss.sss") - 1: 92 | success = (4 == nmea_scanf(buff, buff_sz, 93 | "%2d%2d%2d.%d", &(res->hour), &(res->min), &(res->sec), &(res->hsec) 94 | )); 95 | break; 96 | default: 97 | nmea_error("Parse of time error (format error)!"); 98 | success = 0; 99 | break; 100 | } 101 | 102 | return (success?0:-1); 103 | } 104 | 105 | /** 106 | * \brief Define packet type by header (nmeaPACKTYPE). 107 | * @param buff a constant character pointer of packet buffer. 108 | * @param buff_sz buffer size. 109 | * @return The defined packet type 110 | * @see nmeaPACKTYPE 111 | */ 112 | int nmea_pack_type(const char *buff, int buff_sz) 113 | { 114 | static const char *pheads[] = { 115 | "GPGGA", 116 | "GPGSA", 117 | "GPGSV", 118 | "GPRMC", 119 | "GPVTG", 120 | }; 121 | 122 | NMEA_ASSERT(buff); 123 | 124 | if(buff_sz < 5) 125 | return GPNON; 126 | else if(0 == memcmp(buff, pheads[0], 5)) 127 | return GPGGA; 128 | else if(0 == memcmp(buff, pheads[1], 5)) 129 | return GPGSA; 130 | else if(0 == memcmp(buff, pheads[2], 5)) 131 | return GPGSV; 132 | else if(0 == memcmp(buff, pheads[3], 5)) 133 | return GPRMC; 134 | else if(0 == memcmp(buff, pheads[4], 5)) 135 | return GPVTG; 136 | 137 | return GPNON; 138 | } 139 | 140 | /** 141 | * \brief Find tail of packet ("\r\n") in buffer and check control sum (CRC). 142 | * @param buff a constant character pointer of packets buffer. 143 | * @param buff_sz buffer size. 144 | * @param res_crc a integer pointer for return CRC of packet (must be defined). 145 | * @return Number of bytes to packet tail. 146 | */ 147 | int nmea_find_tail(const char *buff, int buff_sz, int *res_crc) 148 | { 149 | static const int tail_sz = 3 /* *[CRC] */ + 2 /* \r\n */; 150 | 151 | const char *end_buff = buff + buff_sz; 152 | int nread = 0; 153 | int crc = 0; 154 | 155 | NMEA_ASSERT(buff && res_crc); 156 | 157 | *res_crc = -1; 158 | 159 | for(;buff < end_buff; ++buff, ++nread) 160 | { 161 | if(('$' == *buff) && nread) 162 | { 163 | buff = 0; 164 | break; 165 | } 166 | else if('*' == *buff) 167 | { 168 | if(buff + tail_sz <= end_buff && '\r' == buff[3] && '\n' == buff[4]) 169 | { 170 | *res_crc = nmea_atoi(buff + 1, 2, 16); 171 | nread = buff_sz - (int)(end_buff - (buff + tail_sz)); 172 | if(*res_crc != crc) 173 | { 174 | *res_crc = -1; 175 | buff = 0; 176 | } 177 | } 178 | 179 | break; 180 | } 181 | else if(nread) 182 | crc ^= (int)*buff; 183 | } 184 | 185 | if(*res_crc < 0 && buff) 186 | nread = 0; 187 | 188 | return nread; 189 | } 190 | 191 | /** 192 | * \brief Parse GGA packet from buffer. 193 | * @param buff a constant character pointer of packet buffer. 194 | * @param buff_sz buffer size. 195 | * @param pack a pointer of packet which will filled by function. 196 | * @return 1 (true) - if parsed successfully or 0 (false) - if fail. 197 | */ 198 | int nmea_parse_GPGGA(const char *buff, int buff_sz, nmeaGPGGA *pack) 199 | { 200 | char time_buff[NMEA_TIMEPARSE_BUF]; 201 | 202 | NMEA_ASSERT(buff && pack); 203 | 204 | memset(pack, 0, sizeof(nmeaGPGGA)); 205 | 206 | nmea_trace_buff(buff, buff_sz); 207 | 208 | if(14 != nmea_scanf(buff, buff_sz, 209 | "$GPGGA,%s,%f,%C,%f,%C,%d,%d,%f,%f,%C,%f,%C,%f,%d*", 210 | &(time_buff[0]), 211 | &(pack->lat), &(pack->ns), &(pack->lon), &(pack->ew), 212 | &(pack->sig), &(pack->satinuse), &(pack->HDOP), &(pack->elv), &(pack->elv_units), 213 | &(pack->diff), &(pack->diff_units), &(pack->dgps_age), &(pack->dgps_sid))) 214 | { 215 | nmea_error("GPGGA parse error!"); 216 | return 0; 217 | } 218 | 219 | if(0 != _nmea_parse_time(&time_buff[0], (int)strlen(&time_buff[0]), &(pack->utc))) 220 | { 221 | nmea_error("GPGGA time parse error!"); 222 | return 0; 223 | } 224 | 225 | return 1; 226 | } 227 | 228 | /** 229 | * \brief Parse GSA packet from buffer. 230 | * @param buff a constant character pointer of packet buffer. 231 | * @param buff_sz buffer size. 232 | * @param pack a pointer of packet which will filled by function. 233 | * @return 1 (true) - if parsed successfully or 0 (false) - if fail. 234 | */ 235 | int nmea_parse_GPGSA(const char *buff, int buff_sz, nmeaGPGSA *pack) 236 | { 237 | NMEA_ASSERT(buff && pack); 238 | 239 | memset(pack, 0, sizeof(nmeaGPGSA)); 240 | 241 | nmea_trace_buff(buff, buff_sz); 242 | 243 | if(17 != nmea_scanf(buff, buff_sz, 244 | "$GPGSA,%C,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f*", 245 | &(pack->fix_mode), &(pack->fix_type), 246 | &(pack->sat_prn[0]), &(pack->sat_prn[1]), &(pack->sat_prn[2]), &(pack->sat_prn[3]), &(pack->sat_prn[4]), &(pack->sat_prn[5]), 247 | &(pack->sat_prn[6]), &(pack->sat_prn[7]), &(pack->sat_prn[8]), &(pack->sat_prn[9]), &(pack->sat_prn[10]), &(pack->sat_prn[11]), 248 | &(pack->PDOP), &(pack->HDOP), &(pack->VDOP))) 249 | { 250 | nmea_error("GPGSA parse error!"); 251 | return 0; 252 | } 253 | 254 | return 1; 255 | } 256 | 257 | /** 258 | * \brief Parse GSV packet from buffer. 259 | * @param buff a constant character pointer of packet buffer. 260 | * @param buff_sz buffer size. 261 | * @param pack a pointer of packet which will filled by function. 262 | * @return 1 (true) - if parsed successfully or 0 (false) - if fail. 263 | */ 264 | int nmea_parse_GPGSV(const char *buff, int buff_sz, nmeaGPGSV *pack) 265 | { 266 | int nsen, nsat; 267 | 268 | NMEA_ASSERT(buff && pack); 269 | 270 | memset(pack, 0, sizeof(nmeaGPGSV)); 271 | 272 | nmea_trace_buff(buff, buff_sz); 273 | 274 | nsen = nmea_scanf(buff, buff_sz, 275 | "$GPGSV,%d,%d,%d," 276 | "%d,%d,%d,%d," 277 | "%d,%d,%d,%d," 278 | "%d,%d,%d,%d," 279 | "%d,%d,%d,%d*", 280 | &(pack->pack_count), &(pack->pack_index), &(pack->sat_count), 281 | &(pack->sat_data[0].id), &(pack->sat_data[0].elv), &(pack->sat_data[0].azimuth), &(pack->sat_data[0].sig), 282 | &(pack->sat_data[1].id), &(pack->sat_data[1].elv), &(pack->sat_data[1].azimuth), &(pack->sat_data[1].sig), 283 | &(pack->sat_data[2].id), &(pack->sat_data[2].elv), &(pack->sat_data[2].azimuth), &(pack->sat_data[2].sig), 284 | &(pack->sat_data[3].id), &(pack->sat_data[3].elv), &(pack->sat_data[3].azimuth), &(pack->sat_data[3].sig)); 285 | 286 | nsat = (pack->pack_index - 1) * NMEA_SATINPACK; 287 | nsat = (nsat + NMEA_SATINPACK > pack->sat_count)?pack->sat_count - nsat:NMEA_SATINPACK; 288 | nsat = nsat * 4 + 3 /* first three sentence`s */; 289 | 290 | if(nsen < nsat || nsen > (NMEA_SATINPACK * 4 + 3)) 291 | { 292 | nmea_error("GPGSV parse error!"); 293 | return 0; 294 | } 295 | 296 | return 1; 297 | } 298 | 299 | /** 300 | * \brief Parse RMC packet from buffer. 301 | * @param buff a constant character pointer of packet buffer. 302 | * @param buff_sz buffer size. 303 | * @param pack a pointer of packet which will filled by function. 304 | * @return 1 (true) - if parsed successfully or 0 (false) - if fail. 305 | */ 306 | int nmea_parse_GPRMC(const char *buff, int buff_sz, nmeaGPRMC *pack) 307 | { 308 | int nsen; 309 | char time_buff[NMEA_TIMEPARSE_BUF]; 310 | 311 | NMEA_ASSERT(buff && pack); 312 | 313 | memset(pack, 0, sizeof(nmeaGPRMC)); 314 | 315 | nmea_trace_buff(buff, buff_sz); 316 | 317 | nsen = nmea_scanf(buff, buff_sz, 318 | "$GPRMC,%s,%C,%f,%C,%f,%C,%f,%f,%2d%2d%2d,%f,%C,%C*", 319 | &(time_buff[0]), 320 | &(pack->status), &(pack->lat), &(pack->ns), &(pack->lon), &(pack->ew), 321 | &(pack->speed), &(pack->direction), 322 | &(pack->utc.day), &(pack->utc.mon), &(pack->utc.year), 323 | &(pack->declination), &(pack->declin_ew), &(pack->mode)); 324 | 325 | if(nsen != 13 && nsen != 14) 326 | { 327 | nmea_error("GPRMC parse error!"); 328 | return 0; 329 | } 330 | 331 | if(0 != _nmea_parse_time(&time_buff[0], (int)strlen(&time_buff[0]), &(pack->utc))) 332 | { 333 | nmea_error("GPRMC time parse error!"); 334 | return 0; 335 | } 336 | 337 | if(pack->utc.year < 90) 338 | pack->utc.year += 100; 339 | pack->utc.mon -= 1; 340 | 341 | return 1; 342 | } 343 | 344 | /** 345 | * \brief Parse VTG packet from buffer. 346 | * @param buff a constant character pointer of packet buffer. 347 | * @param buff_sz buffer size. 348 | * @param pack a pointer of packet which will filled by function. 349 | * @return 1 (true) - if parsed successfully or 0 (false) - if fail. 350 | */ 351 | int nmea_parse_GPVTG(const char *buff, int buff_sz, nmeaGPVTG *pack) 352 | { 353 | NMEA_ASSERT(buff && pack); 354 | 355 | memset(pack, 0, sizeof(nmeaGPVTG)); 356 | 357 | nmea_trace_buff(buff, buff_sz); 358 | 359 | if(8 != nmea_scanf(buff, buff_sz, 360 | "$GPVTG,%f,%C,%f,%C,%f,%C,%f,%C*", 361 | &(pack->dir), &(pack->dir_t), 362 | &(pack->dec), &(pack->dec_m), 363 | &(pack->spn), &(pack->spn_n), 364 | &(pack->spk), &(pack->spk_k))) 365 | { 366 | nmea_error("GPVTG parse error!"); 367 | return 0; 368 | } 369 | 370 | if( pack->dir_t != 'T' || 371 | pack->dec_m != 'M' || 372 | pack->spn_n != 'N' || 373 | pack->spk_k != 'K') 374 | { 375 | nmea_error("GPVTG parse error (format error)!"); 376 | return 0; 377 | } 378 | 379 | return 1; 380 | } 381 | 382 | /** 383 | * \brief Fill nmeaINFO structure by GGA packet data. 384 | * @param pack a pointer of packet structure. 385 | * @param info a pointer of summary information structure. 386 | */ 387 | void nmea_GPGGA2info(nmeaGPGGA *pack, nmeaINFO *info) 388 | { 389 | NMEA_ASSERT(pack && info); 390 | 391 | info->utc.hour = pack->utc.hour; 392 | info->utc.min = pack->utc.min; 393 | info->utc.sec = pack->utc.sec; 394 | info->utc.hsec = pack->utc.hsec; 395 | info->sig = pack->sig; 396 | info->HDOP = pack->HDOP; 397 | info->elv = pack->elv; 398 | info->lat = ((pack->ns == 'N')?pack->lat:-(pack->lat)); 399 | info->lon = ((pack->ew == 'E')?pack->lon:-(pack->lon)); 400 | info->smask |= GPGGA; 401 | } 402 | 403 | /** 404 | * \brief Fill nmeaINFO structure by GSA packet data. 405 | * @param pack a pointer of packet structure. 406 | * @param info a pointer of summary information structure. 407 | */ 408 | void nmea_GPGSA2info(nmeaGPGSA *pack, nmeaINFO *info) 409 | { 410 | int i, j, nuse = 0; 411 | 412 | NMEA_ASSERT(pack && info); 413 | 414 | info->fix = pack->fix_type; 415 | info->PDOP = pack->PDOP; 416 | info->HDOP = pack->HDOP; 417 | info->VDOP = pack->VDOP; 418 | 419 | for(i = 0; i < NMEA_MAXSAT; ++i) 420 | { 421 | for(j = 0; j < info->satinfo.inview; ++j) 422 | { 423 | if(pack->sat_prn[i] && pack->sat_prn[i] == info->satinfo.sat[j].id) 424 | { 425 | info->satinfo.sat[j].in_use = 1; 426 | nuse++; 427 | } 428 | } 429 | } 430 | 431 | info->satinfo.inuse = nuse; 432 | info->smask |= GPGSA; 433 | } 434 | 435 | /** 436 | * \brief Fill nmeaINFO structure by GSV packet data. 437 | * @param pack a pointer of packet structure. 438 | * @param info a pointer of summary information structure. 439 | */ 440 | void nmea_GPGSV2info(nmeaGPGSV *pack, nmeaINFO *info) 441 | { 442 | int isat, isi, nsat; 443 | 444 | NMEA_ASSERT(pack && info); 445 | 446 | if(pack->pack_index > pack->pack_count || 447 | pack->pack_index * NMEA_SATINPACK > NMEA_MAXSAT) 448 | return; 449 | 450 | if(pack->pack_index < 1) 451 | pack->pack_index = 1; 452 | 453 | info->satinfo.inview = pack->sat_count; 454 | 455 | nsat = (pack->pack_index - 1) * NMEA_SATINPACK; 456 | nsat = (nsat + NMEA_SATINPACK > pack->sat_count)?pack->sat_count - nsat:NMEA_SATINPACK; 457 | 458 | for(isat = 0; isat < nsat; ++isat) 459 | { 460 | isi = (pack->pack_index - 1) * NMEA_SATINPACK + isat; 461 | info->satinfo.sat[isi].id = pack->sat_data[isat].id; 462 | info->satinfo.sat[isi].elv = pack->sat_data[isat].elv; 463 | info->satinfo.sat[isi].azimuth = pack->sat_data[isat].azimuth; 464 | info->satinfo.sat[isi].sig = pack->sat_data[isat].sig; 465 | } 466 | 467 | info->smask |= GPGSV; 468 | } 469 | 470 | /** 471 | * \brief Fill nmeaINFO structure by RMC packet data. 472 | * @param pack a pointer of packet structure. 473 | * @param info a pointer of summary information structure. 474 | */ 475 | void nmea_GPRMC2info(nmeaGPRMC *pack, nmeaINFO *info) 476 | { 477 | NMEA_ASSERT(pack && info); 478 | 479 | if('A' == pack->status) 480 | { 481 | if(NMEA_SIG_BAD == info->sig) 482 | info->sig = NMEA_SIG_MID; 483 | if(NMEA_FIX_BAD == info->fix) 484 | info->fix = NMEA_FIX_2D; 485 | } 486 | else if('V' == pack->status) 487 | { 488 | info->sig = NMEA_SIG_BAD; 489 | info->fix = NMEA_FIX_BAD; 490 | } 491 | 492 | info->utc = pack->utc; 493 | info->lat = ((pack->ns == 'N')?pack->lat:-(pack->lat)); 494 | info->lon = ((pack->ew == 'E')?pack->lon:-(pack->lon)); 495 | info->speed = pack->speed * NMEA_TUD_KNOTS; 496 | info->direction = pack->direction; 497 | info->smask |= GPRMC; 498 | } 499 | 500 | /** 501 | * \brief Fill nmeaINFO structure by VTG packet data. 502 | * @param pack a pointer of packet structure. 503 | * @param info a pointer of summary information structure. 504 | */ 505 | void nmea_GPVTG2info(nmeaGPVTG *pack, nmeaINFO *info) 506 | { 507 | NMEA_ASSERT(pack && info); 508 | 509 | info->direction = pack->dir; 510 | info->declination = pack->dec; 511 | info->speed = pack->spk; 512 | info->smask |= GPVTG; 513 | } 514 | -------------------------------------------------------------------------------- /src/parser.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /** 22 | * \file parser.h 23 | */ 24 | 25 | #include 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | typedef struct _nmeaParserNODE 37 | { 38 | int packType; 39 | void *pack; 40 | struct _nmeaParserNODE *next_node; 41 | 42 | } nmeaParserNODE; 43 | 44 | /* 45 | * high level 46 | */ 47 | 48 | /** 49 | * \brief Initialization of parser object 50 | * @return true (1) - success or false (0) - fail 51 | */ 52 | int nmea_parser_init(nmeaPARSER *parser) 53 | { 54 | int resv = 0; 55 | int buff_size = nmea_property()->parse_buff_size; 56 | 57 | NMEA_ASSERT(parser); 58 | 59 | if(buff_size < NMEA_MIN_PARSEBUFF) 60 | buff_size = NMEA_MIN_PARSEBUFF; 61 | 62 | memset(parser, 0, sizeof(nmeaPARSER)); 63 | 64 | if(0 == (parser->buffer = malloc(buff_size))) 65 | nmea_error("Insufficient memory!"); 66 | else 67 | { 68 | parser->buff_size = buff_size; 69 | resv = 1; 70 | } 71 | 72 | return resv; 73 | } 74 | 75 | /** 76 | * \brief Destroy parser object 77 | */ 78 | void nmea_parser_destroy(nmeaPARSER *parser) 79 | { 80 | NMEA_ASSERT(parser); 81 | if (parser->buffer) { 82 | free(parser->buffer); 83 | parser->buffer = NULL; 84 | } 85 | nmea_parser_queue_clear(parser); 86 | memset(parser, 0, sizeof(nmeaPARSER)); 87 | } 88 | 89 | /** 90 | * \brief Analysis of buffer and put results to information structure 91 | * @return Number of packets wos parsed 92 | */ 93 | int nmea_parse( 94 | nmeaPARSER *parser, 95 | const char *buff, int buff_sz, 96 | nmeaINFO *info 97 | ) 98 | { 99 | int ptype, nread = 0; 100 | void *pack = 0; 101 | 102 | NMEA_ASSERT(parser && parser->buffer); 103 | 104 | nmea_parser_push(parser, buff, buff_sz); 105 | 106 | while(GPNON != (ptype = nmea_parser_pop(parser, &pack))) 107 | { 108 | nread++; 109 | 110 | switch(ptype) 111 | { 112 | case GPGGA: 113 | nmea_GPGGA2info((nmeaGPGGA *)pack, info); 114 | break; 115 | case GPGSA: 116 | nmea_GPGSA2info((nmeaGPGSA *)pack, info); 117 | break; 118 | case GPGSV: 119 | nmea_GPGSV2info((nmeaGPGSV *)pack, info); 120 | break; 121 | case GPRMC: 122 | nmea_GPRMC2info((nmeaGPRMC *)pack, info); 123 | break; 124 | case GPVTG: 125 | nmea_GPVTG2info((nmeaGPVTG *)pack, info); 126 | break; 127 | default: 128 | break; 129 | }; 130 | 131 | free(pack); 132 | } 133 | 134 | return nread; 135 | } 136 | 137 | /* 138 | * low level 139 | */ 140 | 141 | int nmea_parser_real_push(nmeaPARSER *parser, const char *buff, int buff_sz) 142 | { 143 | int nparsed = 0, crc, sen_sz, ptype; 144 | nmeaParserNODE *node = 0; 145 | 146 | NMEA_ASSERT(parser && parser->buffer); 147 | 148 | /* clear unuse buffer (for debug) */ 149 | /* 150 | memset( 151 | parser->buffer + parser->buff_use, 0, 152 | parser->buff_size - parser->buff_use 153 | ); 154 | */ 155 | 156 | /* add */ 157 | if(parser->buff_use + buff_sz >= parser->buff_size) 158 | nmea_parser_buff_clear(parser); 159 | 160 | memcpy(parser->buffer + parser->buff_use, buff, buff_sz); 161 | parser->buff_use += buff_sz; 162 | 163 | /* parse */ 164 | for(;;node = 0) 165 | { 166 | sen_sz = nmea_find_tail( 167 | (const char *)parser->buffer + nparsed, 168 | (int)parser->buff_use - nparsed, &crc); 169 | 170 | if(!sen_sz) 171 | { 172 | if(nparsed) 173 | memcpy( 174 | parser->buffer, 175 | parser->buffer + nparsed, 176 | parser->buff_use -= nparsed); 177 | break; 178 | } 179 | else if(crc >= 0) 180 | { 181 | ptype = nmea_pack_type( 182 | (const char *)parser->buffer + nparsed + 1, 183 | parser->buff_use - nparsed - 1); 184 | 185 | if(0 == (node = malloc(sizeof(nmeaParserNODE)))) 186 | goto mem_fail; 187 | 188 | node->pack = 0; 189 | 190 | switch(ptype) 191 | { 192 | case GPGGA: 193 | if(0 == (node->pack = malloc(sizeof(nmeaGPGGA)))) 194 | goto mem_fail; 195 | node->packType = GPGGA; 196 | if(!nmea_parse_GPGGA( 197 | (const char *)parser->buffer + nparsed, 198 | sen_sz, (nmeaGPGGA *)node->pack)) 199 | { 200 | free(node); 201 | node = 0; 202 | } 203 | break; 204 | case GPGSA: 205 | if(0 == (node->pack = malloc(sizeof(nmeaGPGSA)))) 206 | goto mem_fail; 207 | node->packType = GPGSA; 208 | if(!nmea_parse_GPGSA( 209 | (const char *)parser->buffer + nparsed, 210 | sen_sz, (nmeaGPGSA *)node->pack)) 211 | { 212 | free(node); 213 | node = 0; 214 | } 215 | break; 216 | case GPGSV: 217 | if(0 == (node->pack = malloc(sizeof(nmeaGPGSV)))) 218 | goto mem_fail; 219 | node->packType = GPGSV; 220 | if(!nmea_parse_GPGSV( 221 | (const char *)parser->buffer + nparsed, 222 | sen_sz, (nmeaGPGSV *)node->pack)) 223 | { 224 | free(node); 225 | node = 0; 226 | } 227 | break; 228 | case GPRMC: 229 | if(0 == (node->pack = malloc(sizeof(nmeaGPRMC)))) 230 | goto mem_fail; 231 | node->packType = GPRMC; 232 | if(!nmea_parse_GPRMC( 233 | (const char *)parser->buffer + nparsed, 234 | sen_sz, (nmeaGPRMC *)node->pack)) 235 | { 236 | free(node); 237 | node = 0; 238 | } 239 | break; 240 | case GPVTG: 241 | if(0 == (node->pack = malloc(sizeof(nmeaGPVTG)))) 242 | goto mem_fail; 243 | node->packType = GPVTG; 244 | if(!nmea_parse_GPVTG( 245 | (const char *)parser->buffer + nparsed, 246 | sen_sz, (nmeaGPVTG *)node->pack)) 247 | { 248 | free(node); 249 | node = 0; 250 | } 251 | break; 252 | default: 253 | free(node); 254 | node = 0; 255 | break; 256 | }; 257 | 258 | if(node) 259 | { 260 | if(parser->end_node) 261 | ((nmeaParserNODE *)parser->end_node)->next_node = node; 262 | parser->end_node = node; 263 | if(!parser->top_node) 264 | parser->top_node = node; 265 | node->next_node = 0; 266 | } 267 | } 268 | 269 | nparsed += sen_sz; 270 | } 271 | 272 | return nparsed; 273 | 274 | mem_fail: 275 | if(node) 276 | free(node); 277 | 278 | nmea_error("Insufficient memory!"); 279 | 280 | return -1; 281 | } 282 | 283 | /** 284 | * \brief Analysis of buffer and keep results into parser 285 | * @return Number of bytes wos parsed from buffer 286 | */ 287 | int nmea_parser_push(nmeaPARSER *parser, const char *buff, int buff_sz) 288 | { 289 | int nparse, nparsed = 0; 290 | 291 | do 292 | { 293 | if(buff_sz > parser->buff_size) 294 | nparse = parser->buff_size; 295 | else 296 | nparse = buff_sz; 297 | 298 | nparsed += nmea_parser_real_push( 299 | parser, buff, nparse); 300 | 301 | buff_sz -= nparse; 302 | 303 | } while(buff_sz); 304 | 305 | return nparsed; 306 | } 307 | 308 | /** 309 | * \brief Get type of top packet keeped into parser 310 | * @return Type of packet 311 | * @see nmeaPACKTYPE 312 | */ 313 | int nmea_parser_top(nmeaPARSER *parser) 314 | { 315 | int retval = GPNON; 316 | nmeaParserNODE *node = (nmeaParserNODE *)parser->top_node; 317 | 318 | NMEA_ASSERT(parser && parser->buffer); 319 | 320 | if(node) 321 | retval = node->packType; 322 | 323 | return retval; 324 | } 325 | 326 | /** 327 | * \brief Withdraw top packet from parser 328 | * @return Received packet type 329 | * @see nmeaPACKTYPE 330 | */ 331 | int nmea_parser_pop(nmeaPARSER *parser, void **pack_ptr) 332 | { 333 | int retval = GPNON; 334 | nmeaParserNODE *node = (nmeaParserNODE *)parser->top_node; 335 | 336 | NMEA_ASSERT(parser && parser->buffer); 337 | 338 | if(node) 339 | { 340 | *pack_ptr = node->pack; 341 | retval = node->packType; 342 | parser->top_node = node->next_node; 343 | if(!parser->top_node) 344 | parser->end_node = 0; 345 | free(node); 346 | } 347 | 348 | return retval; 349 | } 350 | 351 | /** 352 | * \brief Get top packet from parser without withdraw 353 | * @return Received packet type 354 | * @see nmeaPACKTYPE 355 | */ 356 | int nmea_parser_peek(nmeaPARSER *parser, void **pack_ptr) 357 | { 358 | int retval = GPNON; 359 | nmeaParserNODE *node = (nmeaParserNODE *)parser->top_node; 360 | 361 | NMEA_ASSERT(parser && parser->buffer); 362 | 363 | if(node) 364 | { 365 | *pack_ptr = node->pack; 366 | retval = node->packType; 367 | } 368 | 369 | return retval; 370 | } 371 | 372 | /** 373 | * \brief Delete top packet from parser 374 | * @return Deleted packet type 375 | * @see nmeaPACKTYPE 376 | */ 377 | int nmea_parser_drop(nmeaPARSER *parser) 378 | { 379 | int retval = GPNON; 380 | nmeaParserNODE *node = (nmeaParserNODE *)parser->top_node; 381 | 382 | NMEA_ASSERT(parser && parser->buffer); 383 | 384 | if(node) 385 | { 386 | if(node->pack) 387 | free(node->pack); 388 | retval = node->packType; 389 | parser->top_node = node->next_node; 390 | if(!parser->top_node) 391 | parser->end_node = 0; 392 | free(node); 393 | } 394 | 395 | return retval; 396 | } 397 | 398 | /** 399 | * \brief Clear cache of parser 400 | * @return true (1) - success 401 | */ 402 | int nmea_parser_buff_clear(nmeaPARSER *parser) 403 | { 404 | NMEA_ASSERT(parser && parser->buffer); 405 | parser->buff_use = 0; 406 | return 1; 407 | } 408 | 409 | /** 410 | * \brief Clear packets queue into parser 411 | * @return true (1) - success 412 | */ 413 | int nmea_parser_queue_clear(nmeaPARSER *parser) 414 | { 415 | NMEA_ASSERT(parser); 416 | while(parser->top_node) 417 | nmea_parser_drop(parser); 418 | return 1; 419 | } 420 | -------------------------------------------------------------------------------- /src/sentence.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | 25 | void nmea_zero_GPGGA(nmeaGPGGA *pack) 26 | { 27 | memset(pack, 0, sizeof(nmeaGPGGA)); 28 | nmea_time_now(&pack->utc); 29 | pack->ns = 'N'; 30 | pack->ew = 'E'; 31 | pack->elv_units = 'M'; 32 | pack->diff_units = 'M'; 33 | } 34 | 35 | void nmea_zero_GPGSA(nmeaGPGSA *pack) 36 | { 37 | memset(pack, 0, sizeof(nmeaGPGSA)); 38 | pack->fix_mode = 'A'; 39 | pack->fix_type = NMEA_FIX_BAD; 40 | } 41 | 42 | void nmea_zero_GPGSV(nmeaGPGSV *pack) 43 | { 44 | memset(pack, 0, sizeof(nmeaGPGSV)); 45 | } 46 | 47 | void nmea_zero_GPRMC(nmeaGPRMC *pack) 48 | { 49 | memset(pack, 0, sizeof(nmeaGPRMC)); 50 | nmea_time_now(&pack->utc); 51 | pack->status = 'V'; 52 | pack->ns = 'N'; 53 | pack->ew = 'E'; 54 | pack->declin_ew = 'E'; 55 | } 56 | 57 | void nmea_zero_GPVTG(nmeaGPVTG *pack) 58 | { 59 | memset(pack, 0, sizeof(nmeaGPVTG)); 60 | pack->dir_t = 'T'; 61 | pack->dec_m = 'M'; 62 | pack->spn_n = 'N'; 63 | pack->spk_k = 'K'; 64 | } 65 | -------------------------------------------------------------------------------- /src/time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | void nmea_time_now(nmeaTIME *stm) { 28 | struct timeval tp; 29 | struct tm tt; 30 | 31 | gettimeofday(&tp, NULL); 32 | gmtime_r(&tp.tv_sec, &tt); 33 | 34 | stm->year = tt.tm_year; 35 | stm->mon = tt.tm_mon; 36 | stm->day = tt.tm_mday; 37 | stm->hour = tt.tm_hour; 38 | stm->min = tt.tm_min; 39 | stm->sec = tt.tm_sec; 40 | stm->hsec = (tp.tv_usec / 10000); 41 | } 42 | -------------------------------------------------------------------------------- /src/tok.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2008 Timur Sinitsyn 5 | * Copyright (c) 2011 Ferry Huberts 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | */ 20 | 21 | /*! \file tok.h */ 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #define NMEA_TOKS_COMPARE (1) 34 | #define NMEA_TOKS_PERCENT (2) 35 | #define NMEA_TOKS_WIDTH (3) 36 | #define NMEA_TOKS_TYPE (4) 37 | 38 | #define NMEA_CONVSTR_BUF (256) 39 | 40 | /** 41 | * \brief Calculate control sum of binary buffer 42 | */ 43 | int nmea_calc_crc(const char *buff, int buff_sz) 44 | { 45 | int chsum = 0, 46 | it; 47 | 48 | for(it = 0; it < buff_sz; ++it) 49 | chsum ^= (int)buff[it]; 50 | 51 | return chsum; 52 | } 53 | 54 | /** 55 | * \brief Convert string to number 56 | */ 57 | int nmea_atoi(const char *str, int str_sz, int radix) 58 | { 59 | char *tmp_ptr; 60 | char buff[NMEA_CONVSTR_BUF]; 61 | int res = 0; 62 | 63 | if(str_sz < NMEA_CONVSTR_BUF) 64 | { 65 | memcpy(&buff[0], str, str_sz); 66 | buff[str_sz] = '\0'; 67 | res = strtol(&buff[0], &tmp_ptr, radix); 68 | } 69 | 70 | return res; 71 | } 72 | 73 | /** 74 | * \brief Convert string to fraction number 75 | */ 76 | double nmea_atof(const char *str, int str_sz) 77 | { 78 | char *tmp_ptr; 79 | char buff[NMEA_CONVSTR_BUF]; 80 | double res = 0; 81 | 82 | if(str_sz < NMEA_CONVSTR_BUF) 83 | { 84 | memcpy(&buff[0], str, str_sz); 85 | buff[str_sz] = '\0'; 86 | res = strtod(&buff[0], &tmp_ptr); 87 | } 88 | 89 | return res; 90 | } 91 | 92 | /** 93 | * \brief Formating string (like standart printf) with CRC tail (*CRC) 94 | */ 95 | int nmea_printf(char *buff, int buff_sz, const char *format, ...) 96 | { 97 | int retval, add = 0; 98 | va_list arg_ptr; 99 | 100 | if(buff_sz <= 0) 101 | return 0; 102 | 103 | va_start(arg_ptr, format); 104 | 105 | retval = NMEA_POSIX(vsnprintf)(buff, buff_sz, format, arg_ptr); 106 | 107 | if(retval > 0) 108 | { 109 | add = NMEA_POSIX(snprintf)( 110 | buff + retval, buff_sz - retval, "*%02x\r\n", 111 | nmea_calc_crc(buff + 1, retval - 1)); 112 | } 113 | 114 | retval += add; 115 | 116 | if(retval < 0 || retval > buff_sz) 117 | { 118 | memset(buff, ' ', buff_sz); 119 | retval = buff_sz; 120 | } 121 | 122 | va_end(arg_ptr); 123 | 124 | return retval; 125 | } 126 | 127 | /** 128 | * \brief Analyse string (specificate for NMEA sentences) 129 | */ 130 | int nmea_scanf(const char *buff, int buff_sz, const char *format, ...) 131 | { 132 | const char *beg_tok; 133 | const char *end_buf = buff + buff_sz; 134 | 135 | va_list arg_ptr; 136 | int tok_type = NMEA_TOKS_COMPARE; 137 | int width = 0; 138 | const char *beg_fmt = 0; 139 | int snum = 0, unum = 0; 140 | 141 | int tok_count = 0; 142 | void *parg_target; 143 | 144 | va_start(arg_ptr, format); 145 | 146 | for(; *format && buff < end_buf; ++format) 147 | { 148 | switch(tok_type) 149 | { 150 | case NMEA_TOKS_COMPARE: 151 | if('%' == *format) 152 | tok_type = NMEA_TOKS_PERCENT; 153 | else if(*buff++ != *format) 154 | goto fail; 155 | break; 156 | case NMEA_TOKS_PERCENT: 157 | width = 0; 158 | beg_fmt = format; 159 | tok_type = NMEA_TOKS_WIDTH; 160 | /* no break */ 161 | case NMEA_TOKS_WIDTH: 162 | if(isdigit(*format)) 163 | break; 164 | { 165 | tok_type = NMEA_TOKS_TYPE; 166 | if(format > beg_fmt) 167 | width = nmea_atoi(beg_fmt, (int)(format - beg_fmt), 10); 168 | } 169 | /* no break */ 170 | case NMEA_TOKS_TYPE: 171 | beg_tok = buff; 172 | 173 | if(!width && ('c' == *format || 'C' == *format) && *buff != format[1]) 174 | width = 1; 175 | 176 | if(width) 177 | { 178 | if(buff + width <= end_buf) 179 | buff += width; 180 | else 181 | goto fail; 182 | } 183 | else 184 | { 185 | if(!format[1] || (0 == (buff = (char *)memchr(buff, format[1], end_buf - buff)))) 186 | buff = end_buf; 187 | } 188 | 189 | if(buff > end_buf) 190 | goto fail; 191 | 192 | tok_type = NMEA_TOKS_COMPARE; 193 | tok_count++; 194 | 195 | parg_target = 0; width = (int)(buff - beg_tok); 196 | 197 | switch(*format) 198 | { 199 | case 'c': 200 | case 'C': 201 | parg_target = (void *)va_arg(arg_ptr, char *); 202 | if(width && 0 != (parg_target)) 203 | *((char *)parg_target) = *beg_tok; 204 | break; 205 | case 's': 206 | case 'S': 207 | parg_target = (void *)va_arg(arg_ptr, char *); 208 | if(width && 0 != (parg_target)) 209 | { 210 | memcpy(parg_target, beg_tok, width); 211 | ((char *)parg_target)[width] = '\0'; 212 | } 213 | break; 214 | case 'f': 215 | case 'g': 216 | case 'G': 217 | case 'e': 218 | case 'E': 219 | parg_target = (void *)va_arg(arg_ptr, double *); 220 | if(width && 0 != (parg_target)) 221 | *((double *)parg_target) = nmea_atof(beg_tok, width); 222 | break; 223 | default: 224 | break; 225 | }; 226 | 227 | if(parg_target) 228 | break; 229 | if(0 == (parg_target = (void *)va_arg(arg_ptr, int *))) 230 | break; 231 | if(!width) 232 | break; 233 | 234 | switch(*format) 235 | { 236 | case 'd': 237 | case 'i': 238 | snum = nmea_atoi(beg_tok, width, 10); 239 | memcpy(parg_target, &snum, sizeof(int)); 240 | break; 241 | case 'u': 242 | unum = nmea_atoi(beg_tok, width, 10); 243 | memcpy(parg_target, &unum, sizeof(unsigned int)); 244 | break; 245 | case 'x': 246 | case 'X': 247 | unum = nmea_atoi(beg_tok, width, 16); 248 | memcpy(parg_target, &unum, sizeof(unsigned int)); 249 | break; 250 | case 'o': 251 | unum = nmea_atoi(beg_tok, width, 8); 252 | memcpy(parg_target, &unum, sizeof(unsigned int)); 253 | break; 254 | default: 255 | goto fail; 256 | }; 257 | 258 | break; 259 | 260 | default: 261 | break; 262 | }; 263 | } 264 | 265 | fail: 266 | 267 | va_end(arg_ptr); 268 | 269 | return tok_count; 270 | } 271 | -------------------------------------------------------------------------------- /src/util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of nmealib. 3 | * 4 | * Copyright (c) 2011 Ferry Huberts 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | /** 26 | * Determine whether the given string contains characters that are not allowed 27 | * for fields in an NMEA string. 28 | * 29 | * @param str 30 | * The string to check 31 | * @param strName 32 | * The name of the string to report when invalid characters are encountered 33 | * @param report 34 | * A pointer to a buffer in which to place the report string when an invalid 35 | * nmea character is detected 36 | * @param reportSize 37 | * The size of the report buffer 38 | * 39 | * @return 40 | * - true when the string has invalid characters 41 | * - false otherwise 42 | */ 43 | bool nmea_string_has_invalid_chars(const char * str, const char * strName, 44 | char * report, size_t reportSize) { 45 | static const char invalidChars[] = { '$', '*', ',', '!', '\\', '^', '~' }; 46 | static const char * invalidCharsNames[] = { "sentence delimiter ($)", 47 | "checksum field delimiter (*)", "comma (,)", "exclamation mark (!)", 48 | "backslash (\\)", "^ (^)", "tilde (~)" }; 49 | 50 | size_t i; 51 | size_t j; 52 | 53 | if (!str) { 54 | return false; 55 | } 56 | 57 | for (i = 0; i < strlen(str); i++) { 58 | char c = str[i]; 59 | 60 | if ((c < 32) || (c > 126)) { 61 | if (report) { 62 | snprintf((char*) report, reportSize, "Configured %s (%s)," 63 | " character %lu, can not contain non-printable" 64 | " characters (codes outside the range [32, 126])", 65 | strName, str, (unsigned long)i + 1); 66 | report[reportSize - 1] = '\0'; 67 | } 68 | return true; 69 | } 70 | 71 | for (j = 0; j < sizeof(invalidChars); j++) { 72 | if (c == invalidChars[j]) { 73 | if (report) { 74 | snprintf((char *) report, reportSize, "Configured %s (%s)," 75 | " character %lu, can not contain %s characters", 76 | strName, str, (unsigned long)i + 1, invalidCharsNames[j]); 77 | report[reportSize - 1] = '\0'; 78 | } 79 | return true; 80 | } 81 | } 82 | } 83 | 84 | return false; 85 | } 86 | --------------------------------------------------------------------------------