You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. uint32_t low=0, high=sizeof(mavlink_message_info)/sizeof(mavlink_message_info[0]);
  15. while (low < high) {
  16. uint32_t mid = (low+1+high)/2;
  17. if (msgid < mavlink_message_info[mid].msgid) {
  18. high = mid-1;
  19. continue;
  20. }
  21. if (msgid > mavlink_message_info[mid].msgid) {
  22. low = mid;
  23. continue;
  24. }
  25. low = mid;
  26. break;
  27. }
  28. if (mavlink_message_info[low].msgid == msgid) {
  29. return &mavlink_message_info[low];
  30. }
  31. return NULL;
  32. }
  33. /*
  34. return the message_info struct for a message
  35. */
  36. MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info(const mavlink_message_t *msg)
  37. {
  38. return mavlink_get_message_info_by_id(msg->msgid);
  39. }
  40. /*
  41. return the message_info struct for a message
  42. */
  43. MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info_by_name(const char *name)
  44. {
  45. static const struct { const char *name; uint32_t msgid; } mavlink_message_names[] = MAVLINK_MESSAGE_NAMES;
  46. /*
  47. use a bisection search to find the right entry. A perfect hash may be better
  48. Note that this assumes the table is sorted with primary key name
  49. */
  50. uint32_t low=0, high=sizeof(mavlink_message_names)/sizeof(mavlink_message_names[0]);
  51. while (low < high) {
  52. uint32_t mid = (low+1+high)/2;
  53. int cmp = strcmp(mavlink_message_names[mid].name, name);
  54. if (cmp == 0) {
  55. return mavlink_get_message_info_by_id(mavlink_message_names[mid].msgid);
  56. }
  57. if (cmp > 0) {
  58. high = mid-1;
  59. } else {
  60. low = mid;
  61. }
  62. }
  63. return NULL;
  64. }
  65. #endif // MAVLINK_USE_MESSAGE_INFO