MAVLink_routing.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /// @file MAVLink_routing.h
  2. /// @brief handle routing of MAVLink packets by ID
  3. #pragma once
  4. #include <AP_HAL/AP_HAL.h>
  5. #include <AP_Common/AP_Common.h>
  6. #include "GCS_MAVLink.h"
  7. // 20 routes should be enough for now. This may need to increase as
  8. // we make more extensive use of MAVLink forwarding
  9. #define MAVLINK_MAX_ROUTES 20
  10. /*
  11. object to handle MAVLink packet routing
  12. */
  13. class MAVLink_routing
  14. {
  15. friend class GCS_MAVLINK;
  16. public:
  17. MAVLink_routing(void);
  18. /*
  19. forward a MAVLink message to the right port. This also
  20. automatically learns the route for the sender if it is not
  21. already known.
  22. This returns true if the message should be processed locally
  23. */
  24. bool check_and_forward(mavlink_channel_t in_channel, const mavlink_message_t &msg);
  25. /*
  26. send a MAVLink message to all components with this vehicle's system id
  27. This is a no-op if no routes to components have been learned
  28. */
  29. void send_to_components(const mavlink_message_t &msg);
  30. /*
  31. search for the first vehicle or component in the routing table with given mav_type and retrieve it's sysid, compid and channel
  32. returns true if a match is found
  33. */
  34. bool find_by_mavtype(uint8_t mavtype, uint8_t &sysid, uint8_t &compid, mavlink_channel_t &channel);
  35. private:
  36. // a simple linear routing table. We don't expect to have a lot of
  37. // routes, so a scalable structure isn't worthwhile yet.
  38. uint8_t num_routes;
  39. struct route {
  40. uint8_t sysid;
  41. uint8_t compid;
  42. mavlink_channel_t channel;
  43. uint8_t mavtype;
  44. } routes[MAVLINK_MAX_ROUTES];
  45. // a channel mask to block routing as required
  46. uint8_t no_route_mask;
  47. // learn new routes
  48. void learn_route(mavlink_channel_t in_channel, const mavlink_message_t &msg);
  49. // extract target sysid and compid from a message
  50. void get_targets(const mavlink_message_t &msg, int16_t &sysid, int16_t &compid);
  51. // special handling for heartbeat messages
  52. void handle_heartbeat(mavlink_channel_t in_channel, const mavlink_message_t &msg);
  53. };