diff --git a/generator/C/include_v2.0/mavlink_get_info.h b/generator/C/include_v2.0/mavlink_get_info.h index fa98d09665652a3d5f3ece796cce6c74ed6f5468..302ec8bd6b94f2f8635f9be6b7fdf13fd4c8f18c 100644 --- a/generator/C/include_v2.0/mavlink_get_info.h +++ b/generator/C/include_v2.0/mavlink_get_info.h @@ -9,32 +9,31 @@ MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info_by_id(uint32_t msgid) { static const mavlink_message_info_t mavlink_message_info[] = MAVLINK_MESSAGE_INFO; - /* + /* use a bisection search to find the right entry. A perfect hash may be better Note that this assumes the table is sorted with primary key msgid */ - const uint32_t count = sizeof(mavlink_message_info)/sizeof(mavlink_message_info[0]); - if (count == 0) { - return NULL; - } - uint32_t low=0, high=count-1; - while (low <= high) { - uint32_t mid = (low+1+high)/2; - if (msgid < mavlink_message_info[mid].msgid) { - high = mid-1; - continue; - } - if (msgid > mavlink_message_info[mid].msgid) { - low = mid; - continue; - } - low = mid; - break; - } - if (mavlink_message_info[low].msgid == msgid) { - return &mavlink_message_info[low]; - } - return NULL; + const uint32_t count = sizeof(mavlink_message_info)/sizeof(mavlink_message_info[0]); + if (count == 0) { + return NULL; + } + uint32_t low=0, high=count-1; + while (low < high) { + uint32_t mid = (low+high)/2; + if (msgid < mavlink_message_info[mid].msgid) { + high = mid; + continue; + } + if (msgid > mavlink_message_info[mid].msgid) { + low = mid+1; + continue; + } + return &mavlink_message_info[mid]; + } + if (mavlink_message_info[low].msgid == msgid) { + return &mavlink_message_info[low]; + } + return NULL; } /* @@ -51,28 +50,33 @@ MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info(const mavl MAVLINK_HELPER const mavlink_message_info_t *mavlink_get_message_info_by_name(const char *name) { static const struct { const char *name; uint32_t msgid; } mavlink_message_names[] = MAVLINK_MESSAGE_NAMES; - /* + /* use a bisection search to find the right entry. A perfect hash may be better Note that this assumes the table is sorted with primary key name */ - const uint32_t count = sizeof(mavlink_message_names)/sizeof(mavlink_message_names[0]); - if (count == 0) { - return NULL; - } - uint32_t low=0, high=count-1; - while (low <= high) { - uint32_t mid = (low+1+high)/2; - int cmp = strcmp(mavlink_message_names[mid].name, name); - if (cmp == 0) { - return mavlink_get_message_info_by_id(mavlink_message_names[mid].msgid); - } - if (cmp > 0) { - high = mid-1; - } else { - low = mid; - } - } - return NULL; + const uint32_t count = sizeof(mavlink_message_names)/sizeof(mavlink_message_names[0]); + if (count == 0) { + return NULL; + } + uint32_t low=0, high=count-1; + while (low < high) { + uint32_t mid = (low+high)/2; + int cmp = strcmp(mavlink_message_names[mid].name, name); + if (cmp > 0) { + high = mid; + continue; + } + if (cmp < 0) { + low = mid+1; + continue; + } + low = mid; + break; + } + if (strcmp(mavlink_message_names[low].name, name) == 0) { + return mavlink_get_message_info_by_id(mavlink_message_names[low].msgid); + } + return NULL; } #endif // MAVLINK_USE_MESSAGE_INFO diff --git a/generator/C/test/posix/testmav.c b/generator/C/test/posix/testmav.c index cc6d7ff88365ee4e41715bfe6d066c4ac9cd3637..de827d60aad07a598b9626a5ae5786da502306f7 100644 --- a/generator/C/test/posix/testmav.c +++ b/generator/C/test/posix/testmav.c @@ -186,6 +186,86 @@ static void comm_send_ch(mavlink_channel_t chan, uint8_t c) } } +#ifdef MAVLINK_HAVE_GET_MESSAGE_INFO +static const mavlink_message_info_t *dumb_search_info(const mavlink_message_info_t *msgs, uint32_t num_ids, uint32_t id) +{ + for (uint32_t i=0; i<num_ids; i++) { + if (msgs[i].msgid == id) { + return &msgs[i]; + } + } + return NULL; +} + +static void test_get_message_info_by_id() +{ + const mavlink_message_info_t *msgs = mavlink_get_message_info_by_id(0); + static const mavlink_msg_entry_t crcs[] = MAVLINK_MESSAGE_CRCS; + const uint32_t num_msgs = sizeof(crcs)/sizeof(crcs[0]); + for (uint32_t i=0; i<70000; i++) { + const mavlink_message_info_t *m1 = mavlink_get_message_info_by_id(i); + const mavlink_message_info_t *m2 = dumb_search_info(msgs, num_msgs, i); + if (m1 != m2) { + printf("Search error for id %u\n", (unsigned)i); + error_count++; + } + } +} + +static const mavlink_message_info_t *dumb_search_name(const mavlink_message_info_t *msgs, uint32_t num_ids, const char *name) +{ + for (uint32_t i=0; i<num_ids; i++) { + if (strcmp(msgs[i].name, name) == 0) { + return &msgs[i]; + } + } + return NULL; +} + +static void test_get_message_info_by_name() +{ + static const char *test_names[] = { "HEARTBEAT", "STATUS_TEXT", "ATTITUDE", "FOOBLAH", "SILLY_NAME" }; + const uint8_t num_names = sizeof(test_names)/sizeof(test_names[0]); + const mavlink_message_info_t *msgs = mavlink_get_message_info_by_id(0); + static const mavlink_msg_entry_t crcs[] = MAVLINK_MESSAGE_CRCS; + const uint32_t num_msgs = sizeof(crcs)/sizeof(crcs[0]); + for (uint32_t i=0; i<num_names; i++) { + const mavlink_message_info_t *m1 = mavlink_get_message_info_by_name(test_names[i]); + const mavlink_message_info_t *m2 = dumb_search_name(msgs, num_msgs, test_names[i]); + if (m1 != m2) { + printf("Search error for id %s\n", test_names[i]); + error_count++; + } + } +} + + +static const mavlink_msg_entry_t *dumb_search_entry(const mavlink_msg_entry_t *msgs, uint32_t num_ids, uint32_t id) +{ + for (uint32_t i=0; i<num_ids; i++) { + if (msgs[i].msgid == id) { + return &msgs[i]; + } + } + return NULL; +} + +static void test_get_msg_entry() +{ + const mavlink_msg_entry_t *msgs = mavlink_get_msg_entry(0); + static const mavlink_msg_entry_t crcs[] = MAVLINK_MESSAGE_CRCS; + const uint32_t num_msgs = sizeof(crcs)/sizeof(crcs[0]); + for (uint32_t i=0; i<70000; i++) { + const mavlink_msg_entry_t *m1 = mavlink_get_msg_entry(i); + const mavlink_msg_entry_t *m2 = dumb_search_entry(msgs, num_msgs, i); + if (m1 != m2) { + printf("Search error for entry id %u\n", (unsigned)i); + error_count++; + } + } +} +#endif // MAVLINK_HAVE_GET_MESSAGE_INFO + int main(void) { mavlink_channel_t chan; @@ -272,7 +352,12 @@ int main(void) printf("No errors detected\n"); #endif - +#ifdef MAVLINK_HAVE_GET_MESSAGE_INFO + test_get_message_info_by_id(); + test_get_message_info_by_name(); + test_get_msg_entry(); +#endif + return 0; }