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