Merge remote-tracking branch 'upstream/master' into abo_fw_alt_vel_control
[inav.git] / lib / main / MAVLink / mavlink_get_info.h
blob51cb577f2292bdc5592012583c549aecc2495264
1 #pragma once
3 #ifdef MAVLINK_USE_MESSAGE_INFO
4 #define MAVLINK_HAVE_GET_MESSAGE_INFO
6 /*
7 return the message_info struct for a message
8 */
9 MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info_by_id(uint32_t msgid)
11 static const mavlink_message_info_t mavlink_message_info[] = MAVLINK_MESSAGE_INFO;
13 use a bisection search to find the right entry. A perfect hash may be better
14 Note that this assumes the table is sorted with primary key msgid
16 uint32_t low=0, high=sizeof(mavlink_message_info)/sizeof(mavlink_message_info[0]);
17 while (low < high) {
18 uint32_t mid = (low+1+high)/2;
19 if (msgid < mavlink_message_info[mid].msgid) {
20 high = mid-1;
21 continue;
23 if (msgid > mavlink_message_info[mid].msgid) {
24 low = mid;
25 continue;
27 low = mid;
28 break;
30 if (mavlink_message_info[low].msgid == msgid) {
31 return &mavlink_message_info[low];
33 return NULL;
37 return the message_info struct for a message
39 MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info(const mavlink_message_t *msg)
41 return mavlink_get_message_info_by_id(msg->msgid);
45 return the message_info struct for a message
47 MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info_by_name(const char *name)
49 static const struct { const char *name; uint32_t msgid; } mavlink_message_names[] = MAVLINK_MESSAGE_NAMES;
51 use a bisection search to find the right entry. A perfect hash may be better
52 Note that this assumes the table is sorted with primary key name
54 uint32_t low=0, high=sizeof(mavlink_message_names)/sizeof(mavlink_message_names[0]);
55 while (low < high) {
56 uint32_t mid = (low+1+high)/2;
57 int cmp = strcmp(mavlink_message_names[mid].name, name);
58 if (cmp == 0) {
59 return mavlink_get_message_info_by_id(mavlink_message_names[mid].msgid);
61 if (cmp > 0) {
62 high = mid-1;
63 } else {
64 low = mid;
67 return NULL;
69 #endif // MAVLINK_USE_MESSAGE_INFO