mavlink_get_info.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #ifdef MAVLINK_USE_MESSAGE_INFO
  3. #define MAVLINK_HAVE_GET_MESSAGE_INFO
  4. /*
  5. return the message_info struct for a message
  6. */
  7. MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info_by_id(uint32_t msgid)
  8. {
  9. static const mavlink_message_info_t mavlink_message_info[] = MAVLINK_MESSAGE_INFO;
  10. /*
  11. use a bisection search to find the right entry. A perfect hash may be better
  12. Note that this assumes the table is sorted with primary key msgid
  13. */
  14. const uint32_t count = sizeof(mavlink_message_info)/sizeof(mavlink_message_info[0]);
  15. if (count == 0) {
  16. return NULL;
  17. }
  18. uint32_t low=0, high=count-1;
  19. while (low < high) {
  20. uint32_t mid = (low+high)/2;
  21. if (msgid < mavlink_message_info[mid].msgid) {
  22. high = mid;
  23. continue;
  24. }
  25. if (msgid > mavlink_message_info[mid].msgid) {
  26. low = mid+1;
  27. continue;
  28. }
  29. return &mavlink_message_info[mid];
  30. }
  31. if (mavlink_message_info[low].msgid == msgid) {
  32. return &mavlink_message_info[low];
  33. }
  34. return NULL;
  35. }
  36. /*
  37. return the message_info struct for a message
  38. */
  39. MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info(const mavlink_message_t *msg)
  40. {
  41. return mavlink_get_message_info_by_id(msg->msgid);
  42. }
  43. /*
  44. return the message_info struct for a message
  45. */
  46. MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info_by_name(const char *name)
  47. {
  48. static const struct { const char *name; uint32_t msgid; } mavlink_message_names[] = MAVLINK_MESSAGE_NAMES;
  49. /*
  50. use a bisection search to find the right entry. A perfect hash may be better
  51. Note that this assumes the table is sorted with primary key name
  52. */
  53. const uint32_t count = sizeof(mavlink_message_names)/sizeof(mavlink_message_names[0]);
  54. if (count == 0) {
  55. return NULL;
  56. }
  57. uint32_t low=0, high=count-1;
  58. while (low < high) {
  59. uint32_t mid = (low+high)/2;
  60. int cmp = strcmp(mavlink_message_names[mid].name, name);
  61. if (cmp > 0) {
  62. high = mid;
  63. continue;
  64. }
  65. if (cmp < 0) {
  66. low = mid+1;
  67. continue;
  68. }
  69. low = mid;
  70. break;
  71. }
  72. if (strcmp(mavlink_message_names[low].name, name) == 0) {
  73. return mavlink_get_message_info_by_id(mavlink_message_names[low].msgid);
  74. }
  75. return NULL;
  76. }
  77. #endif // MAVLINK_USE_MESSAGE_INFO