├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── Readme.md ├── docs └── mavkit_interfaces.jpg ├── include └── mavkit │ ├── MavMessengerInterface.h │ ├── MavlinkDisplay.h │ ├── MavlinkLogReader.h │ ├── MavlinkLogWriter.h │ ├── MavlinkSerial.h │ ├── MavlinkTCP.h │ ├── MavlinkUDP.h │ └── Messengers.h └── src ├── mavkit.cpp └── mavkit ├── MavlinkDisplay.cpp ├── MavlinkLogReader.cpp ├── MavlinkLogWriter.cpp ├── MavlinkSerial.cpp ├── MavlinkTCP.cpp └── MavlinkUDP.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | *~ 3 | *.gch 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "include/mavlink"] 2 | path = include/mavlink 3 | url = git@github.com:mavlink/mavlink.git 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(mavkit) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | ################################################################################ 7 | #GENERATE MAVLINK HEADERS 8 | set(MAVLINK_DIR ../include/mavlink) 9 | set(MAVLINK_VERSION 2.0) 10 | set(MAVLINK_HEADERS_DIR generated/mavlink_headers) 11 | execute_process(COMMAND python ${MAVLINK_DIR}/pymavlink/tools/mavgen.py --lang=C --wire-protocol=${MAVLINK_VERSION} --output=${MAVLINK_HEADERS_DIR} ${MAVLINK_DIR}/message_definitions/v1.0/ardupilotmega.xml RESULT_VARIABLE retval) 12 | if(NOT retval EQUAL 0) 13 | message( FATAL_ERROR "MAVLink headers generation failed") 14 | endif() 15 | 16 | ################################################################################ 17 | #PACKAGES 18 | find_package(Boost REQUIRED COMPONENTS regex) 19 | find_package(Threads REQUIRED) 20 | 21 | ################################################################################ 22 | #INCLUDES 23 | include_directories( 24 | include 25 | ${CMAKE_BINARY_DIR}/generated 26 | ${Boost_INCLUDE_DIRS} 27 | ) 28 | 29 | ################################################################################ 30 | #SOURCES 31 | file(GLOB_RECURSE MAVKIT_SRC "src/mavkit/*.cpp") 32 | 33 | ################################################################################ 34 | #LINK 35 | LIST(APPEND LINK_LIBS 36 | ${Boost_LIBRARIES} 37 | ${CMAKE_THREAD_LIBS_INIT} 38 | ) 39 | 40 | ################################################################################ 41 | #EXECUTABLE 42 | add_executable(${PROJECT_NAME} ${MAVKIT_SRC} src/mavkit.cpp) 43 | target_link_libraries(${PROJECT_NAME} ${LINK_LIBS}) 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # MAVKit 2 | 3 | ## Introduction 4 | 5 | MAVkit is a C++ toolbox for **MAVLink 2.0** in command line. 6 | 7 | It can **display**, **redirect**, **broadcast**, **log**, **replay** data from and to different types of interface (**Serial**, **UDP**, **TCP**, **File**). 8 | 9 | MAVkit is also a framework for MAVLink programmers. 10 | It is built on a modular architecture where each module can be used independently inside other projects. 11 | In that case, it provides a simple MAVLink block that can be replaced according to communication hardware. 12 | 13 | Designed with low latency in mind, it keeps IO buffers safe from overflows (leading to data loss) by using a dual thread processing loop per interface (fast read and process). 14 | 15 | ## Build 16 | 17 | ### Submodule update 18 | >Note : Be sure to use the following recursive command, there is two submodule levels in mavlink. 19 | 20 | ```Shell 21 | git submodule update --init --recursive 22 | ``` 23 | 24 | ### Compilation 25 | ***Dependencies*** : Boost + MAVLink usual dependencies 26 | 27 | ```shell 28 | mkdir build 29 | cd build 30 | cmake .. 31 | make 32 | ``` 33 | 34 | ## How it works ? 35 | 36 | Mavkit is organised in modules called **messengers**. A messenger can receive and send messages to others in a fully concurrent way. 37 | 38 | Each messenger can be either a serial link, an UDP socket, a log file,... 39 | 40 | you have to specify at least one messenger (the first) for the role of **master**. 41 | This particular one will be duplex connected to every other messenger created then. 42 | On the contrary, secondary messengers won't be connected between them. 43 | 44 | Here is an example of two MAVkit instances, composed of respectively 4 and 2 messengers and linked between them through UDP. Note that masters are connected to all other messengers, while those are not linked between them. 45 | 46 | 47 | 48 | ## How to use it ? 49 | 50 | Once you've built it, you can run MAVKit from command line by specifying messengers one after the other. 51 | 52 | The first messenger will take on the role of master. 53 | 54 | ### Serial 55 | ***Arguments :*** 56 | * ***device*** -> path of a tty device. 57 | * ***baudrate*** -> baudrate. 58 | 59 | ex : `--tty /dev/ttyUSB0 57600` 60 | 61 | ### UDP server 62 | ***Arguments :*** 63 | * ***port*** -> listening port. 64 | 65 | ex : `--udp_server 14550` 66 | 67 | ### UDP client 68 | ***Arguments :*** 69 | * ***IP address*** -> target IP address. 70 | * ***port*** -> target port. 71 | 72 | `--udp_client 192.168.1.10 14550` 73 | 74 | ### TCP server 75 | ***Arguments :*** 76 | * ***port*** -> listening port. 77 | 78 | ex : `--tcp_server 14550` 79 | 80 | ### TCP client 81 | ***Arguments :*** 82 | * ***IP address*** -> target IP address. 83 | * ***port*** -> target port. 84 | 85 | `--tcp_client 192.168.1.10 14550` 86 | 87 | ### Logger 88 | Save received messages in .raw and .ts file (.raw contains the mavlink raw data, .ts contains timestamp for each message). 89 | 90 | ***No arguments.*** 91 | 92 | `--log` 93 | 94 | ### File 95 | Replay content of .raw and .ts files. 96 | 97 | ***Arguments :*** 98 | * ***file prefix*** -> logs are made of two files (.raw and .ts), both should be located on the same place for mavkit to find them. Example : dir/log will look for dir/log.raw and dir/log.ts files. 99 | * ***speed multiplier*** -> floating point multiplier for the reading speed. 100 | * ***starting time*** -> time to start in the file in seconds. 101 | 102 | ex : `--file ../myLog 2.0 50` 103 | 104 | ### display 105 | Output messages to stdout. 106 | 107 | ***No arguments.*** 108 | 109 | `--display` 110 | 111 | --- 112 | You can combine almost as many messengers as you want in command line. 113 | 114 | Example : 115 | 116 | ```Shell 117 | ./mavkit --tty 57600 /dev/ttyACM0 --udp_server 14550 --display --log --tcp_client 127.0.0.1 14551 118 | ``` 119 | -------------------------------------------------------------------------------- /docs/mavkit_interfaces.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/proto3/MAVkit/32a38acda1df4e29cf8c30de8a7cd5b772b7d748/docs/mavkit_interfaces.jpg -------------------------------------------------------------------------------- /include/mavkit/MavMessengerInterface.h: -------------------------------------------------------------------------------- 1 | #ifndef MAV_MESSENGER_INTERFACE_H 2 | #define MAV_MESSENGER_INTERFACE_H 3 | 4 | #include 5 | 6 | class MavMessengerInterface 7 | { 8 | public: 9 | virtual ~MavMessengerInterface(){}; 10 | 11 | //return true if the message is sent 12 | //false is an has error occured 13 | virtual bool send_message(mavlink_message_t &msg) = 0; 14 | 15 | //add a MavMessengerInterface to the listener list 16 | //listeners are sent any message that is received by this MavMessengerInterface 17 | virtual void append_listener(MavMessengerInterface* listener) = 0; 18 | 19 | //start internal thread(s) that read interface continuously 20 | //no effect if implementation has no thread 21 | virtual void start() = 0; 22 | 23 | //join internal thread 24 | //return immediately if implementation has no thread (ex: logger) 25 | virtual void join() = 0; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/mavkit/MavlinkDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef MAVLINK_DISPLAY_H 2 | #define MAVLINK_DISPLAY_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class MavlinkDisplay : public MavMessengerInterface 9 | { 10 | public: 11 | MavlinkDisplay(); 12 | ~MavlinkDisplay(); 13 | 14 | bool send_message(mavlink_message_t &msg); 15 | void append_listener(MavMessengerInterface* listener); 16 | void start(); 17 | void join(); 18 | 19 | private: 20 | std::mutex mutex; 21 | std::string _name; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /include/mavkit/MavlinkLogReader.h: -------------------------------------------------------------------------------- 1 | #ifndef MAVLINK_LOG_READER_H 2 | #define MAVLINK_LOG_READER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class MavlinkLogReader : public MavMessengerInterface 10 | { 11 | public: 12 | MavlinkLogReader(std::string log_file, float speed_multiplier, float target_time_sec); 13 | ~MavlinkLogReader(); 14 | 15 | static bool is_valid_file(const char* path); 16 | bool send_message(mavlink_message_t &msg); 17 | void append_listener(MavMessengerInterface* listener); 18 | void start(); 19 | void join(); 20 | 21 | private: 22 | std::thread *reading_thread; 23 | std::vector listeners; 24 | void read_loop(); 25 | 26 | const float _speed_multiplier; 27 | int create_log_files(std::string path); 28 | int raw_fd, ts_fd; 29 | struct timespec start_time; 30 | float _target_time_sec; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/mavkit/MavlinkLogWriter.h: -------------------------------------------------------------------------------- 1 | #ifndef MAVLINK_LOG_WRITER_H 2 | #define MAVLINK_LOG_WRITER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class MavlinkLogWriter : public MavMessengerInterface 11 | { 12 | public: 13 | MavlinkLogWriter(std::string log_path); 14 | ~MavlinkLogWriter(); 15 | 16 | bool send_message(mavlink_message_t &msg); 17 | void append_listener(MavMessengerInterface* listener); 18 | void start(); 19 | void join(); 20 | 21 | private: 22 | std::mutex mutex; 23 | 24 | int create_log_files(std::string path); 25 | int in_fd, out_raw_fd, out_ts_fd; 26 | struct timespec start_time; 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /include/mavkit/MavlinkSerial.h: -------------------------------------------------------------------------------- 1 | #ifndef MAVLINK_SERIAL_H 2 | #define MAVLINK_SERIAL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | class MavlinkSerial : public MavMessengerInterface 12 | { 13 | public: 14 | MavlinkSerial(std::string port, int baudrate); 15 | ~MavlinkSerial(); 16 | 17 | static bool is_valid_tty(const char* path); 18 | bool send_message(mavlink_message_t &msg); 19 | void append_listener(MavMessengerInterface* listener); 20 | void start(); 21 | void join(); 22 | 23 | private: 24 | std::thread *reading_thread; 25 | std::mutex mutex; 26 | std::vector listeners; 27 | void read_loop(); 28 | 29 | int serial_fd; 30 | std::thread *buffering_thread; 31 | int storage_pipe[2]; 32 | void bufferize(); 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/mavkit/MavlinkTCP.h: -------------------------------------------------------------------------------- 1 | #ifndef MAVLINK_TCP_H 2 | #define MAVLINK_TCP_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MavlinkTCP : public MavMessengerInterface 12 | { 13 | public: 14 | //Client constructor 15 | MavlinkTCP(std::string target_ip, int target_port); 16 | //Server constructor 17 | MavlinkTCP(int local_port); 18 | ~MavlinkTCP(); 19 | 20 | static bool is_valid_ip(const char* ip); 21 | bool send_message(mavlink_message_t &msg); 22 | void append_listener(MavMessengerInterface* listener); 23 | void start(); 24 | void join(); 25 | 26 | private: 27 | std::thread *reading_thread; 28 | std::mutex mutex; 29 | std::vector listeners; 30 | void read_loop(); 31 | 32 | bool connect_client(); 33 | void connect_server(); 34 | void reconnect(); 35 | bool is_server; 36 | bool connected; 37 | std::string _target_ip; 38 | int _target_port; 39 | int csock; 40 | int sock; 41 | struct sockaddr_in gcAddr; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/mavkit/MavlinkUDP.h: -------------------------------------------------------------------------------- 1 | #ifndef MAVLINK_UDP_H 2 | #define MAVLINK_UDP_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class MavlinkUDP : public MavMessengerInterface 12 | { 13 | public: 14 | //Client constructor 15 | MavlinkUDP(std::string target_ip, int target_port); 16 | //Server constructor 17 | MavlinkUDP(int local_port); 18 | ~MavlinkUDP(); 19 | 20 | static bool is_valid_ip(const char* ip); 21 | bool send_message(mavlink_message_t &msg); 22 | void append_listener(MavMessengerInterface* listener); 23 | void start(); 24 | void join(); 25 | 26 | private: 27 | std::thread *reading_thread; 28 | std::mutex mutex; 29 | std::vector listeners; 30 | void read_loop(); 31 | 32 | int sock; 33 | struct sockaddr_in gcAddr; 34 | std::thread *buffering_thread; 35 | int storage_pipe[2]; 36 | void bufferize(); 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/mavkit/Messengers.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | -------------------------------------------------------------------------------- /src/mavkit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | enum messenger_type { 7 | TTY, 8 | UDP_CLT, 9 | UDP_SRV, 10 | TCP_CLT, 11 | TCP_SRV, 12 | FILE_W, 13 | FILE_R, 14 | DISPLAY, 15 | HELP 16 | }; 17 | 18 | MavMessengerInterface* master = NULL; 19 | std::vector outputs; 20 | 21 | //----------------------------------------------------------------------------// 22 | void usage() 23 | { 24 | std::cout << "Usage:\n"; 25 | std::cout << " mavkit ...\n\n"; 26 | 27 | std::cout << " Master messenger is duplex connected to every secondary connector.\n"; 28 | std::cout << " Secondary messengers are not connected between them.\n\n"; 29 | 30 | std::cout << " List of connectors :\n"; 31 | std::cout << " --tty \n"; 32 | std::cout << " --udp_client
\n"; 33 | std::cout << " --udp_server \n"; 34 | std::cout << " --tcp_client
\n"; 35 | std::cout << " --tcp_server \n"; 36 | std::cout << " --log\n"; 37 | std::cout << " --replay \n"; 38 | std::cout << " --display" << std::endl; 39 | } 40 | //----------------------------------------------------------------------------// 41 | void add_messenger(MavMessengerInterface* messenger) 42 | { 43 | if(master == NULL) 44 | { 45 | master = messenger; 46 | } 47 | else 48 | { 49 | outputs.push_back(messenger); 50 | master->append_listener(messenger); 51 | messenger->append_listener(master); 52 | } 53 | } 54 | //----------------------------------------------------------------------------// 55 | bool isdigit(char *str) 56 | { 57 | char c = str[0]; 58 | int i = 1; 59 | while(c != '\0') 60 | { 61 | if(!isdigit(c)) 62 | { 63 | return false; 64 | } 65 | c = str[i]; 66 | i++; 67 | } 68 | return i > 1; 69 | } 70 | //----------------------------------------------------------------------------// 71 | int parse_messenger_type(char *arg) 72 | { 73 | std::map type_map = { 74 | { "--tty", TTY }, 75 | { "--udp_client", UDP_CLT }, 76 | { "--udp_server", UDP_SRV }, 77 | { "--tcp_client", TCP_CLT }, 78 | { "--tcp_server", TCP_SRV }, 79 | { "--log", FILE_W }, 80 | { "--replay", FILE_R }, 81 | { "--display", DISPLAY }, 82 | 83 | { "-h", HELP }, 84 | { "--h", HELP }, 85 | { "-help", HELP }, 86 | { "--help", HELP }, 87 | { "-usage", HELP }, 88 | { "--usage", HELP } 89 | }; 90 | 91 | if(type_map.find(arg) != type_map.end()) 92 | { 93 | return type_map[arg]; 94 | } 95 | else 96 | { 97 | return -1; 98 | } 99 | } 100 | //----------------------------------------------------------------------------// 101 | char* get_next_arg(int &index, int argc, char** argv) 102 | { 103 | if(index < argc) 104 | return argv[index++]; 105 | 106 | return NULL; 107 | } 108 | //----------------------------------------------------------------------------// 109 | bool create_tty(int &index, int argc, char** argv) 110 | { 111 | char* baudrate_str = get_next_arg(index, argc, argv); 112 | if(baudrate_str == NULL) 113 | { 114 | std::cout << "Error : Missing baudrate." << std::endl; 115 | usage(); 116 | return false; 117 | } 118 | char* device_str = get_next_arg(index, argc, argv); 119 | if(device_str == NULL) 120 | { 121 | std::cout << "Error : Missing device." << std::endl; 122 | usage(); 123 | return false; 124 | } 125 | 126 | if(isdigit(baudrate_str)) 127 | { 128 | add_messenger(new MavlinkSerial(device_str, atoi(baudrate_str))); 129 | return true; 130 | } 131 | else 132 | { 133 | std::cout << "Error : Baudrate not a number." << std::endl; 134 | usage(); 135 | return false; 136 | } 137 | } 138 | //----------------------------------------------------------------------------// 139 | bool create_udp_client(int &index, int argc, char** argv) 140 | { 141 | char* ip_str = get_next_arg(index, argc, argv); 142 | if(ip_str == NULL) 143 | { 144 | std::cout << "Error : Missing IP." << std::endl; 145 | usage(); 146 | return false; 147 | } 148 | char* port_str = get_next_arg(index, argc, argv); 149 | if(port_str == NULL) 150 | { 151 | std::cout << "Error : Missing port." << std::endl; 152 | usage(); 153 | return false; 154 | } 155 | 156 | if(isdigit(port_str)) 157 | { 158 | add_messenger(new MavlinkUDP(ip_str, atoi(port_str))); 159 | return true; 160 | } 161 | else 162 | { 163 | std::cout << "Error : Port not a number." << std::endl; 164 | usage(); 165 | return false; 166 | } 167 | } 168 | //----------------------------------------------------------------------------// 169 | bool create_udp_server(int &index, int argc, char** argv) 170 | { 171 | char* port_str = get_next_arg(index, argc, argv); 172 | if(port_str == NULL) 173 | { 174 | std::cout << "Error : Missing port." << std::endl; 175 | usage(); 176 | return false; 177 | } 178 | 179 | if(isdigit(port_str)) 180 | { 181 | add_messenger(new MavlinkUDP(atoi(port_str))); 182 | return true; 183 | } 184 | else 185 | { 186 | std::cout << "Error : Port not a number." << std::endl; 187 | usage(); 188 | return false; 189 | } 190 | } 191 | //----------------------------------------------------------------------------// 192 | bool create_tcp_client(int &index, int argc, char** argv) 193 | { 194 | char* ip_str = get_next_arg(index, argc, argv); 195 | if(ip_str == NULL) 196 | { 197 | std::cout << "Error : Missing IP." << std::endl; 198 | usage(); 199 | return false; 200 | } 201 | char* port_str = get_next_arg(index, argc, argv); 202 | if(port_str == NULL) 203 | { 204 | std::cout << "Error : Missing port." << std::endl; 205 | usage(); 206 | return false; 207 | } 208 | 209 | if(isdigit(port_str)) 210 | { 211 | add_messenger(new MavlinkTCP(ip_str, atoi(port_str))); 212 | return true; 213 | } 214 | else 215 | { 216 | std::cout << "Error : Port not a number." << std::endl; 217 | usage(); 218 | return false; 219 | } 220 | } 221 | //----------------------------------------------------------------------------// 222 | bool create_tcp_server(int &index, int argc, char** argv) 223 | { 224 | char* port_str = get_next_arg(index, argc, argv); 225 | if(port_str == NULL) 226 | { 227 | std::cout << "Error : Missing port." << std::endl; 228 | usage(); 229 | return false; 230 | } 231 | 232 | if(isdigit(port_str)) 233 | { 234 | add_messenger(new MavlinkTCP(atoi(port_str))); 235 | return true; 236 | } 237 | else 238 | { 239 | std::cout << "Error : Port not a number." << std::endl; 240 | usage(); 241 | return false; 242 | } 243 | } 244 | //----------------------------------------------------------------------------// 245 | bool create_file(int &index, int argc, char** argv) 246 | { 247 | char* file_str = get_next_arg(index, argc, argv); 248 | if(file_str == NULL) 249 | { 250 | std::cout << "Error : Missing filename." << std::endl; 251 | usage(); 252 | return false; 253 | } 254 | 255 | char* speed_str = get_next_arg(index, argc, argv); 256 | if(speed_str == NULL) 257 | { 258 | std::cout << "Error : Missing reading speed." << std::endl; 259 | usage(); 260 | return false; 261 | } 262 | 263 | char* start_str = get_next_arg(index, argc, argv); 264 | if(start_str == NULL) 265 | { 266 | std::cout << "Error : Missing starting time." << std::endl; 267 | usage(); 268 | return false; 269 | } 270 | 271 | add_messenger(new MavlinkLogReader(file_str, atof(speed_str), atof(start_str))); 272 | return true; 273 | } 274 | //----------------------------------------------------------------------------// 275 | bool create_log(int &index, int argc, char** argv) 276 | { 277 | add_messenger(new MavlinkLogWriter("/log")); 278 | return true; 279 | } 280 | //----------------------------------------------------------------------------// 281 | bool create_display(int &index, int argc, char** argv) 282 | { 283 | add_messenger(new MavlinkDisplay()); 284 | return true; 285 | } 286 | //----------------------------------------------------------------------------// 287 | int main(int argc, char* argv[]) 288 | { 289 | if(argc <= 1) 290 | { 291 | usage(); 292 | return 0; 293 | } 294 | 295 | int arg_i = 1; 296 | while(arg_i < argc) 297 | { 298 | char* messenger_str = get_next_arg(arg_i, argc, argv); 299 | int messenger_type = parse_messenger_type(messenger_str); 300 | 301 | if(messenger_type == -1) 302 | { 303 | std::cout << "Error : " + std::string(messenger_str) + " not a valid messenger." << std::endl; 304 | usage(); 305 | return -1; 306 | } 307 | 308 | switch (messenger_type) 309 | { 310 | case TTY: 311 | create_tty(arg_i, argc, argv); 312 | break; 313 | case UDP_CLT: 314 | create_udp_client(arg_i, argc, argv); 315 | break; 316 | case UDP_SRV: 317 | create_udp_server(arg_i, argc, argv); 318 | break; 319 | case TCP_CLT: 320 | create_tcp_client(arg_i, argc, argv); 321 | break; 322 | case TCP_SRV: 323 | create_tcp_server(arg_i, argc, argv); 324 | break; 325 | case FILE_R: 326 | create_file(arg_i, argc, argv); 327 | break; 328 | case FILE_W: 329 | create_log(arg_i, argc, argv); 330 | break; 331 | case DISPLAY: 332 | create_display(arg_i, argc, argv); 333 | break; 334 | case HELP: 335 | usage(); 336 | return 0; 337 | } 338 | } 339 | 340 | if(master == NULL) 341 | return -1; 342 | 343 | master->start(); 344 | for(int i = 0 ; i < outputs.size() ; i++) 345 | outputs[i]->start(); 346 | 347 | // send Mavlink2 heartbeat to force protocol version 348 | while(true) 349 | { 350 | mavlink_message_t msg; 351 | mavlink_msg_heartbeat_pack(1, 2, &msg, MAV_TYPE_GENERIC, MAV_AUTOPILOT_INVALID, 0, 0, MAV_STATE_STANDBY); 352 | master->send_message(msg); 353 | usleep(1000000); 354 | } 355 | // master->join(); 356 | 357 | return 0; 358 | } 359 | //----------------------------------------------------------------------------// 360 | -------------------------------------------------------------------------------- /src/mavkit/MavlinkDisplay.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | //----------------------------------------------------------------------------// 6 | MavlinkDisplay::MavlinkDisplay() 7 | {} 8 | //----------------------------------------------------------------------------// 9 | MavlinkDisplay::~MavlinkDisplay() 10 | {} 11 | //----------------------------------------------------------------------------// 12 | void MavlinkDisplay::append_listener(MavMessengerInterface* listener) 13 | {} 14 | //----------------------------------------------------------------------------// 15 | void MavlinkDisplay::start() 16 | {} 17 | //----------------------------------------------------------------------------// 18 | void MavlinkDisplay::join() 19 | {} 20 | //----------------------------------------------------------------------------// 21 | bool MavlinkDisplay::send_message(mavlink_message_t &msg) 22 | { 23 | mutex.lock(); 24 | switch(msg.msgid) 25 | { 26 | // case MAVLINK_MSG_ID_HEARTBEAT: 27 | // { 28 | // mavlink_heartbeat_t heartbeat; 29 | // mavlink_msg_heartbeat_decode(&msg, &heartbeat); 30 | // std::cout << "heartbeat" << std::endl; 31 | // 32 | // // access message specific fields 33 | // // std::cout << " type: " << (uint)heartbeat.type << std::endl; 34 | // // std::cout << " autopilot: " << (uint)heartbeat.autopilot << std::endl; 35 | // // std::cout << " base_mode: " << (uint)heartbeat.base_mode << std::endl; 36 | // // std::cout << " custom_mode: " << (uint)heartbeat.custom_mode << std::endl; 37 | // // std::cout << " system_status: " << (uint)heartbeat.system_status << std::endl; 38 | // // std::cout << " mavlink_version: " << (uint)heartbeat.mavlink_version << std::endl; 39 | // break; 40 | // } 41 | // case MAVLINK_MSG_ID_PARAM_REQUEST_LIST: 42 | // { 43 | // mavlink_param_request_list_t param_request_list; 44 | // mavlink_msg_param_request_list_decode(&msg, ¶m_request_list); 45 | // std::cout << "param request list" << std::endl; 46 | // break; 47 | // } 48 | // case MAVLINK_MSG_ID_LOCAL_POSITION_NED: 49 | // { 50 | // mavlink_local_position_ned_t local_position_ned; 51 | // mavlink_msg_local_position_ned_decode(&msg, &local_position_ned); 52 | // std::cout << "local position ned" << std::endl; 53 | // break; 54 | // } 55 | // case MAVLINK_MSG_ID_MISSION_SET_CURRENT: 56 | // { 57 | // mavlink_mission_set_current_t mission_set_current; 58 | // mavlink_msg_mission_set_current_decode(&msg, &mission_set_current); 59 | // std::cout << "mission set current" << std::endl; 60 | // break; 61 | // } 62 | // case MAVLINK_MSG_ID_REQUEST_DATA_STREAM: 63 | // { 64 | // mavlink_request_data_stream_t request_data_stream; 65 | // mavlink_msg_request_data_stream_decode(&msg, &request_data_stream); 66 | // std::cout << "request data stream" << std::endl; 67 | // break; 68 | // } 69 | // case MAVLINK_MSG_ID_NAMED_VALUE_FLOAT: 70 | // { 71 | // mavlink_named_value_float_t named_value_float; 72 | // mavlink_msg_named_value_float_decode(&msg, &named_value_float); 73 | // std::cout << "named value float" << std::endl; 74 | // break; 75 | // } 76 | // case MAVLINK_MSG_ID_STATUSTEXT: 77 | // { 78 | // mavlink_statustext_t statustext; 79 | // mavlink_msg_statustext_decode(&msg, &statustext); 80 | // std::cout << "status text" << std::endl; 81 | // std::cout << statustext.text << std::endl; 82 | // break; 83 | // } 84 | default: 85 | { 86 | // std::cout << "Unsupported packet -> "; 87 | std::cout << "SYS: " << (int)msg.sysid; 88 | std::cout << ", COMP: " << (int)msg.compid; 89 | std::cout << ", SEQ: " << (int)msg.seq; 90 | std::cout << ", LEN: " << (int)msg.len; 91 | std::cout << ", MSG ID: " << (int)msg.msgid << std::endl; 92 | break; 93 | } 94 | } 95 | std::cout << "----------------------" << std::endl; 96 | mutex.unlock(); 97 | 98 | return true; 99 | } 100 | //----------------------------------------------------------------------------// 101 | -------------------------------------------------------------------------------- /src/mavkit/MavlinkLogReader.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //----------------------------------------------------------------------------// 9 | MavlinkLogReader::MavlinkLogReader(std::string log_file, float speed_multiplier, float target_time_sec) 10 | : _speed_multiplier(speed_multiplier), 11 | reading_thread(NULL), 12 | _target_time_sec(target_time_sec) 13 | { 14 | if(speed_multiplier <= 0.0) 15 | { 16 | throw std::logic_error(std::string("LogReader speed_multiplier cannot be negative or zero")); 17 | } 18 | 19 | std::string raw_file = log_file + ".raw"; 20 | std::string ts_file = log_file + ".ts"; 21 | 22 | raw_fd = open(raw_file.c_str(), O_RDONLY); 23 | ts_fd = open(ts_file.c_str(), O_RDONLY); 24 | 25 | if(raw_fd == -1) 26 | throw std::logic_error(std::string("cannot open ") + raw_file + ": " + strerror(errno)); 27 | 28 | if(ts_fd == -1) 29 | throw std::logic_error(std::string("cannot open ") + ts_file + ": " + strerror(errno)); 30 | 31 | clock_gettime(CLOCK_MONOTONIC_RAW, &start_time); 32 | } 33 | //----------------------------------------------------------------------------// 34 | MavlinkLogReader::~MavlinkLogReader() 35 | { 36 | close(raw_fd); 37 | close(ts_fd); 38 | } 39 | //----------------------------------------------------------------------------// 40 | bool MavlinkLogReader::is_valid_file(const char* path) 41 | { 42 | std::string raw_file = path; 43 | raw_file += ".raw"; 44 | return access(raw_file.c_str(), F_OK) != -1; 45 | } 46 | //----------------------------------------------------------------------------// 47 | void MavlinkLogReader::append_listener(MavMessengerInterface* listener) 48 | { 49 | if(listener != NULL) 50 | listeners.push_back(listener); 51 | } 52 | //----------------------------------------------------------------------------// 53 | void MavlinkLogReader::start() 54 | { 55 | if(reading_thread == NULL) 56 | reading_thread = new std::thread(&MavlinkLogReader::read_loop, this); 57 | } 58 | //----------------------------------------------------------------------------// 59 | void MavlinkLogReader::join() 60 | { 61 | reading_thread->join(); 62 | } 63 | //----------------------------------------------------------------------------// 64 | void MavlinkLogReader::read_loop() 65 | { 66 | size_t length = 256; 67 | uint8_t buffer[length]; 68 | mavlink_status_t status; 69 | mavlink_message_t msg; 70 | 71 | uint64_t time_offset = 0; 72 | 73 | while(true) 74 | { 75 | ssize_t nb_read = read(raw_fd, buffer, length); 76 | if(nb_read == -1) 77 | throw std::logic_error("Unable to read from file."); 78 | 79 | for(int i=0;i _target_time_sec * 1000000) 100 | { 101 | if(msg_time_mult > elapsed_time) 102 | usleep(msg_time_mult - elapsed_time); 103 | } 104 | else 105 | { 106 | time_offset = msg_time_mult - elapsed_time + time_offset; 107 | drop_msg = true; 108 | } 109 | } 110 | if(!drop_msg) 111 | { 112 | std::vector::iterator it = listeners.begin(); 113 | for(;it != listeners.end();++it) 114 | { 115 | (*it)->send_message(msg); 116 | } 117 | } 118 | } 119 | } 120 | } 121 | } 122 | //----------------------------------------------------------------------------// 123 | bool MavlinkLogReader::send_message(mavlink_message_t &msg) 124 | { 125 | return false; 126 | } 127 | //----------------------------------------------------------------------------// 128 | -------------------------------------------------------------------------------- /src/mavkit/MavlinkLogWriter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | //----------------------------------------------------------------------------// 13 | MavlinkLogWriter::MavlinkLogWriter(std::string log_path) 14 | { 15 | int log_id = create_log_files(log_path); 16 | 17 | std::string out_raw = log_path + "log" + std::to_string(log_id) + ".raw"; 18 | std::string out_ts = log_path + "log" + std::to_string(log_id) + ".ts"; 19 | 20 | out_raw_fd = open(out_raw.c_str(), O_WRONLY | O_CREAT, 0666); 21 | out_ts_fd = open(out_ts.c_str(), O_WRONLY | O_CREAT, 0666); 22 | 23 | if(out_raw_fd == -1) 24 | throw std::logic_error(std::string("cannot open ") + out_raw + ": " + strerror(errno)); 25 | 26 | if(out_ts_fd == -1) 27 | throw std::logic_error(std::string("cannot open ") + out_ts + ": " + strerror(errno)); 28 | 29 | clock_gettime(CLOCK_MONOTONIC_RAW, &start_time); 30 | } 31 | //----------------------------------------------------------------------------// 32 | MavlinkLogWriter::~MavlinkLogWriter() 33 | { 34 | close(out_raw_fd); 35 | close(out_ts_fd); 36 | } 37 | //----------------------------------------------------------------------------// 38 | void MavlinkLogWriter::append_listener(MavMessengerInterface* listener) 39 | {} 40 | //----------------------------------------------------------------------------// 41 | void MavlinkLogWriter::start() 42 | {} 43 | //----------------------------------------------------------------------------// 44 | void MavlinkLogWriter::join() 45 | {} 46 | //----------------------------------------------------------------------------// 47 | bool MavlinkLogWriter::send_message(mavlink_message_t &msg) 48 | { 49 | uint16_t length = mavlink_msg_get_send_buffer_length(&msg); 50 | uint8_t buffer[length]; 51 | mavlink_msg_to_send_buffer(buffer, &msg); 52 | 53 | mutex.lock(); 54 | 55 | int bytes_sent = write(out_raw_fd, buffer, length); 56 | struct timespec now; 57 | clock_gettime(CLOCK_MONOTONIC_RAW, &now); 58 | uint64_t delta_us = (uint64_t)(now.tv_sec - start_time.tv_sec) * 1000000 + (now.tv_nsec - start_time.tv_nsec) / 1000; 59 | write(out_ts_fd, (uint8_t*)&delta_us, 8); 60 | 61 | mutex.unlock(); 62 | 63 | return length == bytes_sent; 64 | } 65 | //----------------------------------------------------------------------------// 66 | int MavlinkLogWriter::create_log_files(std::string path) 67 | { 68 | if(path.back() != '/') 69 | path += "/"; 70 | 71 | struct stat info; 72 | if(stat(path.c_str(), &info) == -1) 73 | { 74 | if(mkdir(path.c_str(), 0777) == -1) 75 | { 76 | throw std::logic_error("Cannot create " + path + " directory for log :" + strerror(errno)); 77 | } 78 | } 79 | 80 | int max_id = 0; 81 | DIR *directory = opendir(path.c_str()); 82 | std::string name_prefix = "log"; 83 | if(directory != NULL) 84 | { 85 | boost::regex regex_raw(name_prefix + "[[:digit:]]+" + ".raw"); 86 | boost::regex regex_time(name_prefix + "[[:digit:]]+" + ".ts"); 87 | struct dirent *entry; 88 | //for each file name matching regex 89 | while((entry = readdir(directory)) != NULL) 90 | { 91 | //compute new max id 92 | std::string entry_name(entry->d_name); 93 | if(boost::regex_match(entry_name, regex_raw) || boost::regex_match(entry_name, regex_time)) 94 | { 95 | entry_name.erase(0, name_prefix.length()); 96 | max_id = std::max(max_id, std::stoi(entry_name)); 97 | } 98 | } 99 | closedir(directory); 100 | } 101 | else 102 | { 103 | throw std::logic_error("Cannot open " + path + " directory :" + strerror(errno)); 104 | } 105 | 106 | return max_id + 1; 107 | } 108 | //----------------------------------------------------------------------------// 109 | -------------------------------------------------------------------------------- /src/mavkit/MavlinkSerial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //----------------------------------------------------------------------------// 10 | MavlinkSerial::MavlinkSerial(std::string port , int baudrate) 11 | : reading_thread(NULL), 12 | buffering_thread(NULL) 13 | { 14 | int result = pipe(storage_pipe); 15 | if(result == -1) 16 | throw std::logic_error(std::string("cannot open internal pipe for fast buffering: ") + strerror(errno)); 17 | 18 | serial_fd = open(port.c_str(), O_RDWR); 19 | 20 | if(serial_fd == -1) 21 | throw std::logic_error(std::string("cannot open ") + port + ": " + strerror(errno)); 22 | 23 | if(!isatty(serial_fd)) 24 | throw std::logic_error(port + " is not a tty."); 25 | 26 | //TODO why to reset flags on new fd ? 27 | //aren't they clean ? 28 | fcntl(serial_fd, F_SETFL, 0); 29 | 30 | // Read file descriptor configuration 31 | struct termios config; 32 | if(tcgetattr(serial_fd, &config) < 0) 33 | throw std::logic_error(std::string("Cannot read file descriptor configuration: ") + strerror(errno)); 34 | 35 | // Input flags - Turn off input processing 36 | // convert break to null byte, no CR to NL translation, 37 | // no NL to CR translation, don't mark parity errors or breaks 38 | // no input parity check, don't strip high bit off, 39 | // no XON/XOFF software flow control 40 | config.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON); 41 | 42 | // Output flags - Turn off output processing 43 | // no CR to NL translation, no NL to CR-NL translation, 44 | // no NL to CR translation, no column 0 CR suppression, 45 | // no Ctrl-D suppression, no fill characters, no case mapping, 46 | // no local output processing 47 | config.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OPOST); 48 | 49 | #ifdef OLCUC 50 | config.c_oflag &= ~OLCUC; 51 | #endif 52 | 53 | #ifdef ONOEOT 54 | config.c_oflag &= ~ONOEOT; 55 | #endif 56 | 57 | // No line processing: 58 | // echo off, echo newline off, canonical mode off, 59 | // extended input processing off, signal chars off 60 | config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG); 61 | 62 | // Turn off character processing 63 | // clear current char size mask, no parity checking, 64 | // no output processing, force 8 bit input 65 | config.c_cflag &= ~(CSIZE | PARENB); 66 | config.c_cflag |= CS8; 67 | 68 | // One input byte is enough to return from read() 69 | // Inter-character timer off 70 | config.c_cc[VMIN] = 1; 71 | config.c_cc[VTIME] = 0; 72 | 73 | // Apply baudrate 74 | int baudrate_val; 75 | switch (baudrate) 76 | { 77 | case 9600: 78 | baudrate_val = B9600; 79 | break; 80 | case 19200: 81 | baudrate_val = B19200; 82 | break; 83 | case 38400: 84 | baudrate_val = B38400; 85 | break; 86 | case 57600: 87 | baudrate_val = B57600; 88 | break; 89 | case 115200: 90 | baudrate_val = B115200; 91 | break; 92 | case 230400: 93 | baudrate_val = B230400; 94 | break; 95 | case 460800: 96 | baudrate_val = B460800; 97 | break; 98 | case 921600: 99 | baudrate_val = B921600; 100 | break; 101 | default: 102 | throw std::logic_error("Cannot handle baudrate : " + std::to_string(baudrate)); 103 | break; 104 | } 105 | 106 | if(cfsetispeed(&config, baudrate_val) < 0 || cfsetospeed(&config, baudrate_val) < 0) 107 | throw std::logic_error("Cannot set baudrate"); 108 | 109 | // Finally, apply the configuration 110 | if(tcsetattr(serial_fd, TCSAFLUSH, &config) < 0) 111 | throw std::logic_error("Cannot set file descriptor configuration"); 112 | 113 | std::cout << "Connected to " << port << " " << baudrate << std::endl; 114 | 115 | //Issue in linux kernel, tcflush is not effective immediately after serial port opening 116 | usleep(1000000); 117 | tcflush(serial_fd, TCIOFLUSH); 118 | } 119 | //----------------------------------------------------------------------------// 120 | MavlinkSerial::~MavlinkSerial() 121 | { 122 | close(serial_fd); 123 | close(storage_pipe[0]); 124 | close(storage_pipe[1]); 125 | //TODO stop bufferize thread 126 | } 127 | //----------------------------------------------------------------------------// 128 | bool MavlinkSerial::is_valid_tty(const char* path) 129 | { 130 | bool ret = true; 131 | int fd = open(path, O_RDWR | O_NOCTTY); 132 | 133 | if(fd == -1 || !isatty(fd)) 134 | ret = false; 135 | 136 | close(fd); 137 | return ret; 138 | } 139 | //----------------------------------------------------------------------------// 140 | void MavlinkSerial::append_listener(MavMessengerInterface* listener) 141 | { 142 | if(listener != NULL) 143 | listeners.push_back(listener); 144 | } 145 | //----------------------------------------------------------------------------// 146 | void MavlinkSerial::start() 147 | { 148 | if(reading_thread == NULL && buffering_thread == NULL) 149 | { 150 | reading_thread = new std::thread(&MavlinkSerial::read_loop, this); 151 | buffering_thread = new std::thread(&MavlinkSerial::bufferize, this); 152 | } 153 | } 154 | //----------------------------------------------------------------------------// 155 | void MavlinkSerial::join() 156 | { 157 | reading_thread->join(); 158 | } 159 | //----------------------------------------------------------------------------// 160 | void MavlinkSerial::read_loop() 161 | { 162 | size_t length = 256; 163 | uint8_t buffer[length]; 164 | mavlink_status_t status; 165 | mavlink_message_t msg; 166 | while(true) 167 | { 168 | ssize_t nb_read = read(storage_pipe[0], buffer, length); 169 | if(nb_read == -1) 170 | throw std::logic_error("Unable to read from storage pipe."); 171 | 172 | for(int i=0;i::iterator it = listeners.begin(); 177 | for(;it != listeners.end();++it) 178 | { 179 | (*it)->send_message(msg); 180 | } 181 | } 182 | } 183 | } 184 | } 185 | //----------------------------------------------------------------------------// 186 | // Thread loop to move data from serial RX buffer to RAM as fast as possible 187 | // so as to avoid buffer overflows if RX buffer is small. 188 | void MavlinkSerial::bufferize() 189 | { 190 | size_t length = 256; 191 | uint8_t buffer[length]; 192 | while(true) 193 | { 194 | ssize_t nb_read = read(serial_fd, buffer, length); 195 | if(nb_read == -1) 196 | throw std::logic_error("Unable to read from serial port."); 197 | 198 | ssize_t nb_write = write(storage_pipe[1], buffer, nb_read); 199 | if(nb_write == -1 || nb_write < nb_read) 200 | throw std::logic_error("Unable to write to pipe."); //TODO figure out why the write fail instead of throwing error immediately 201 | } 202 | } 203 | //----------------------------------------------------------------------------// 204 | bool MavlinkSerial::send_message(mavlink_message_t &msg) 205 | { 206 | uint16_t length = mavlink_msg_get_send_buffer_length(&msg); 207 | uint8_t buffer[length]; 208 | mavlink_msg_to_send_buffer(buffer, &msg); 209 | 210 | mutex.lock(); 211 | int bytes_sent = write(serial_fd, buffer, length); 212 | mutex.unlock(); 213 | 214 | return length == bytes_sent; 215 | } 216 | //----------------------------------------------------------------------------// 217 | -------------------------------------------------------------------------------- /src/mavkit/MavlinkTCP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //----------------------------------------------------------------------------// 10 | MavlinkTCP::MavlinkTCP(std::string target_ip, int target_port) 11 | : is_server(false), reading_thread(NULL), _target_ip(target_ip), _target_port(target_port) 12 | { 13 | if(_target_port < 0 || _target_port > 65535) 14 | throw std::logic_error(std::string("Port outside range.")); 15 | 16 | if(!connect_client()) 17 | { 18 | std::cerr << strerror(errno) << std::endl; 19 | exit(-1); 20 | } 21 | 22 | std::cout << "to " << _target_ip << ":" << _target_port << std::endl; 23 | 24 | connected = true; 25 | reading_thread = new std::thread(&MavlinkTCP::read_loop, this); 26 | } 27 | //----------------------------------------------------------------------------// 28 | MavlinkTCP::MavlinkTCP(int local_port) 29 | : is_server(true), reading_thread(NULL) 30 | { 31 | if(local_port < 0 || local_port > 65535) 32 | throw std::logic_error(std::string("Port outside range.")); 33 | 34 | struct sockaddr_in locAddr; 35 | memset(&locAddr, 0, sizeof(struct sockaddr_in)); 36 | locAddr.sin_family = AF_INET; 37 | locAddr.sin_port = htons(local_port); 38 | locAddr.sin_addr.s_addr = htonl(INADDR_ANY); 39 | 40 | //bind socket to local_port 41 | csock = socket(AF_INET, SOCK_STREAM, 0); 42 | if(bind(csock,(struct sockaddr*)&locAddr, sizeof(struct sockaddr)) == -1) 43 | { 44 | close(csock); 45 | throw std::logic_error(std::string("error bind failed: ") + strerror(errno)); 46 | } 47 | 48 | if(listen(csock, 2) == -1) 49 | { 50 | perror("listen()"); 51 | exit(errno); 52 | } 53 | 54 | connect_server(); 55 | 56 | std::cout << "from " << inet_ntoa(locAddr.sin_addr) << ":" << local_port << std::endl; 57 | 58 | connected = true; 59 | reading_thread = new std::thread(&MavlinkTCP::read_loop, this); 60 | } 61 | //----------------------------------------------------------------------------// 62 | void MavlinkTCP::connect_server() 63 | { 64 | struct sockaddr_in csin; 65 | memset(&csin, 0, sizeof(struct sockaddr_in)); 66 | socklen_t sinsize = sizeof(csin); 67 | sock = accept(csock, (struct sockaddr*)&csin, &sinsize); 68 | 69 | if(sock == -1) 70 | { 71 | throw std::logic_error(std::string("error accept failed: ") + strerror(errno)); 72 | exit(errno); 73 | } 74 | 75 | //make socket non blocking 76 | if (fcntl(sock, F_SETFL, FASYNC) < 0) 77 | { 78 | close(sock); 79 | throw std::logic_error(std::string("error setting nonblocking: ") + strerror(errno)); 80 | } 81 | } 82 | //----------------------------------------------------------------------------// 83 | bool MavlinkTCP::connect_client() 84 | { 85 | sock = socket(AF_INET, SOCK_STREAM, 0); 86 | memset(&gcAddr, 0, sizeof(struct sockaddr_in)); 87 | gcAddr.sin_family = AF_INET; 88 | gcAddr.sin_port = htons(_target_port); 89 | gcAddr.sin_addr.s_addr = inet_addr(_target_ip.c_str()); 90 | 91 | return connect(sock, (struct sockaddr*) &gcAddr, sizeof(struct sockaddr)) == 0; 92 | } 93 | //----------------------------------------------------------------------------// 94 | void MavlinkTCP::reconnect() 95 | { 96 | if(is_server) 97 | { 98 | std::cout << "[TCP] Disconnected.\n[TCP] server listening for connection..."<< std::endl; 99 | connect_server(); 100 | } 101 | else 102 | { 103 | std::cout << "[TCP] Disconnected."<< std::endl; 104 | while(!connect_client()) 105 | { 106 | std::cout << "[TCP] Connection retry..."<< std::endl; 107 | usleep(1000000); 108 | } 109 | } 110 | connected = true; 111 | std::cout << "[TCP] Connected."<< std::endl; 112 | } 113 | //----------------------------------------------------------------------------// 114 | MavlinkTCP::~MavlinkTCP() 115 | { 116 | if(is_server) 117 | { 118 | close(csock); 119 | } 120 | close(sock); 121 | } 122 | //----------------------------------------------------------------------------// 123 | bool MavlinkTCP::is_valid_ip(const char* ip) 124 | { 125 | char* dst[INET_ADDRSTRLEN]; 126 | return inet_pton(AF_INET, ip, dst) == 1; 127 | } 128 | //----------------------------------------------------------------------------// 129 | void MavlinkTCP::append_listener(MavMessengerInterface* listener) 130 | { 131 | if(listener != NULL) 132 | listeners.push_back(listener); 133 | } 134 | //----------------------------------------------------------------------------// 135 | void MavlinkTCP::start() 136 | { 137 | if(reading_thread == NULL) 138 | reading_thread = new std::thread(&MavlinkTCP::read_loop, this); 139 | } 140 | //----------------------------------------------------------------------------// 141 | void MavlinkTCP::join() 142 | { 143 | reading_thread->join(); 144 | } 145 | //----------------------------------------------------------------------------// 146 | void MavlinkTCP::read_loop() 147 | { 148 | size_t length = 256; 149 | uint8_t buffer[length]; 150 | mavlink_status_t status; 151 | mavlink_message_t msg; 152 | while(true) 153 | { 154 | if(!connected) 155 | { 156 | close(sock); 157 | reconnect(); 158 | } 159 | 160 | socklen_t fromlen = sizeof(struct sockaddr); 161 | ssize_t nb_read = recvfrom(sock, (void *)buffer, length, 0, (struct sockaddr *)&gcAddr, &fromlen); 162 | 163 | if(nb_read == -1) 164 | throw std::logic_error("Unable to read from TCP socket."); 165 | 166 | for(int i=0;i::iterator it = listeners.begin(); 171 | for(;it != listeners.end();++it) 172 | { 173 | (*it)->send_message(msg); 174 | } 175 | } 176 | } 177 | } 178 | } 179 | //----------------------------------------------------------------------------// 180 | bool MavlinkTCP::send_message(mavlink_message_t &msg) 181 | { 182 | uint8_t buf[512]; 183 | ssize_t bytes_sent = 0; 184 | uint16_t len; 185 | len = mavlink_msg_to_send_buffer(buf, &msg); 186 | 187 | mutex.lock(); 188 | if(connected) 189 | { 190 | bytes_sent = sendto(sock, buf, len, MSG_NOSIGNAL, (struct sockaddr*)&gcAddr, sizeof(struct sockaddr_in)); 191 | connected = (bytes_sent != -1); 192 | } 193 | mutex.unlock(); 194 | 195 | return len == bytes_sent; 196 | } 197 | //----------------------------------------------------------------------------// 198 | -------------------------------------------------------------------------------- /src/mavkit/MavlinkUDP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //----------------------------------------------------------------------------// 10 | MavlinkUDP::MavlinkUDP(std::string target_ip, int target_port) 11 | : reading_thread(NULL), 12 | buffering_thread(NULL) 13 | { 14 | int result = pipe(storage_pipe); 15 | if(result == -1) 16 | throw std::logic_error(std::string("cannot open internal pipe for fast buffering: ") + strerror(errno)); 17 | 18 | if(target_port < 0 || target_port > 65535) 19 | throw std::logic_error(std::string("Port outside range.")); 20 | 21 | sock = socket(AF_INET, SOCK_DGRAM, 0); 22 | memset(&gcAddr, 0, sizeof(struct sockaddr_in)); 23 | gcAddr.sin_family = AF_INET; 24 | gcAddr.sin_port = htons(target_port); 25 | gcAddr.sin_addr.s_addr = inet_addr(target_ip.c_str()); 26 | 27 | std::cout << "to " << target_ip << ":" << target_port << std::endl; 28 | 29 | reading_thread = new std::thread(&MavlinkUDP::read_loop, this); 30 | buffering_thread = new std::thread(&MavlinkUDP::bufferize, this); 31 | } 32 | //----------------------------------------------------------------------------// 33 | MavlinkUDP::MavlinkUDP(int local_port) 34 | : reading_thread(NULL), 35 | buffering_thread(NULL) 36 | { 37 | int result = pipe(storage_pipe); 38 | if(result == -1) 39 | throw std::logic_error(std::string("cannot open internal pipe for fast buffering: ") + strerror(errno)); 40 | 41 | if(local_port < 0 || local_port > 65535) 42 | throw std::logic_error(std::string("Port outside range.")); 43 | 44 | struct sockaddr_in locAddr; 45 | memset(&locAddr, 0, sizeof(struct sockaddr_in)); 46 | locAddr.sin_family = AF_INET; 47 | locAddr.sin_port = htons(local_port); 48 | locAddr.sin_addr.s_addr = htonl(INADDR_ANY); 49 | 50 | //bind socket to local_port 51 | sock = socket(AF_INET, SOCK_DGRAM, 0); 52 | if(bind(sock,(struct sockaddr *)&locAddr, sizeof(struct sockaddr)) == -1) 53 | { 54 | close(sock); 55 | throw std::logic_error(std::string("error bind failed: ") + strerror(errno)); 56 | } 57 | 58 | //make socket non blocking 59 | if (fcntl(sock, F_SETFL, FASYNC) < 0) 60 | { 61 | close(sock); 62 | throw std::logic_error(std::string("error setting nonblocking: ") + strerror(errno)); 63 | } 64 | 65 | std::cout << "from " << inet_ntoa(locAddr.sin_addr) << ":" << local_port << std::endl; 66 | 67 | reading_thread = new std::thread(&MavlinkUDP::read_loop, this); 68 | buffering_thread = new std::thread(&MavlinkUDP::bufferize, this); 69 | } 70 | //----------------------------------------------------------------------------// 71 | MavlinkUDP::~MavlinkUDP() 72 | { 73 | close(sock); 74 | close(storage_pipe[0]); 75 | close(storage_pipe[1]); 76 | //TODO stop bufferize thread 77 | } 78 | //----------------------------------------------------------------------------// 79 | bool MavlinkUDP::is_valid_ip(const char* ip) 80 | { 81 | char* dst[INET_ADDRSTRLEN]; 82 | return inet_pton(AF_INET, ip, dst) == 1; 83 | } 84 | //----------------------------------------------------------------------------// 85 | void MavlinkUDP::append_listener(MavMessengerInterface* listener) 86 | { 87 | if(listener != NULL) 88 | listeners.push_back(listener); 89 | } 90 | //----------------------------------------------------------------------------// 91 | void MavlinkUDP::start() 92 | { 93 | if(reading_thread == NULL && buffering_thread == NULL) 94 | { 95 | reading_thread = new std::thread(&MavlinkUDP::read_loop, this); 96 | buffering_thread = new std::thread(&MavlinkUDP::bufferize, this); 97 | } 98 | } 99 | //----------------------------------------------------------------------------// 100 | void MavlinkUDP::join() 101 | { 102 | reading_thread->join(); 103 | } 104 | //----------------------------------------------------------------------------// 105 | void MavlinkUDP::read_loop() 106 | { 107 | size_t length = 256; 108 | uint8_t buffer[length]; 109 | mavlink_status_t status; 110 | mavlink_message_t msg; 111 | while(true) 112 | { 113 | ssize_t nb_read = read(storage_pipe[0], buffer, length); 114 | if(nb_read == -1) 115 | throw std::logic_error("Unable to read from storage pipe."); 116 | 117 | for(int i=0;i::iterator it = listeners.begin(); 122 | for(;it != listeners.end();++it) 123 | { 124 | (*it)->send_message(msg); 125 | } 126 | } 127 | } 128 | } 129 | } 130 | //----------------------------------------------------------------------------// 131 | // Thread loop to move data from RX buffer to RAM as fast as possible 132 | // so as to avoid buffer overflows if RX buffer is small. 133 | void MavlinkUDP::bufferize() 134 | { 135 | size_t length = 256; 136 | uint8_t buffer[length]; 137 | while(true) 138 | { 139 | socklen_t fromlen = sizeof(struct sockaddr); 140 | ssize_t nb_read = recvfrom(sock, (void *)buffer, length, 0, (struct sockaddr *)&gcAddr, &fromlen); 141 | if(nb_read == -1) 142 | throw std::logic_error("Unable to read from UDP socket."); 143 | 144 | ssize_t nb_write = write(storage_pipe[1], buffer, nb_read); 145 | if(nb_write == -1 || nb_write < nb_read) 146 | throw std::logic_error("Unable to write to pipe."); //TODO figure out why the write fail instead of throwing error immediately 147 | } 148 | } 149 | //----------------------------------------------------------------------------// 150 | bool MavlinkUDP::send_message(mavlink_message_t &msg) 151 | { 152 | uint8_t buf[512]; 153 | ssize_t bytes_sent; 154 | uint16_t len; 155 | len = mavlink_msg_to_send_buffer(buf, &msg); 156 | 157 | mutex.lock(); 158 | bytes_sent = sendto(sock, buf, len, 0, (struct sockaddr*)&gcAddr, sizeof(struct sockaddr_in)); 159 | mutex.unlock(); 160 | 161 | return len == bytes_sent; 162 | } 163 | //----------------------------------------------------------------------------// 164 | --------------------------------------------------------------------------------