first
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* MAVLink v2 协议链路 —— 公共 API。
|
||||
*
|
||||
* 驱动独占一个 UART,用内置的官方 MAVLink C 库(c_library_v2)解析收到的
|
||||
* 字节流,并把完整消息分发给已注册的回调;发送消息也由同一库序列化后写 UART。
|
||||
*
|
||||
* 组包/解析消息时,除本头文件外还需包含方言头:
|
||||
*
|
||||
* #include <mavlink/common/mavlink.h>
|
||||
* #include <zephyr/drivers/mavlink.h>
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_DRIVERS_MAVLINK_H_
|
||||
#define ZEPHYR_INCLUDE_DRIVERS_MAVLINK_H_
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <mavlink/mavlink_types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** 注册回调时表示"匹配任意消息"的消息 id。 */
|
||||
#define MAVLINK_MSG_ID_ANY 0xFFFFFFFFu
|
||||
|
||||
/**
|
||||
* RX 消息回调。
|
||||
* 在驱动 RX 线程里对每条匹配的消息调用;回调须短小,重活请转交其它
|
||||
* 线程/队列。
|
||||
* @param dev MAVLink 设备。
|
||||
* @param msg 解析后的消息(仅在回调期间有效)。
|
||||
* @param user_data 注册时传入的用户指针。
|
||||
*/
|
||||
typedef void (*mavlink_rx_cb_t)(const struct device *dev,
|
||||
const mavlink_message_t *msg,
|
||||
void *user_data);
|
||||
|
||||
/** 链路统计。 */
|
||||
struct mavlink_stats {
|
||||
/** UART 收到的字节数。 */
|
||||
uint32_t rx_bytes;
|
||||
/** UART 发出的字节数。 */
|
||||
uint32_t tx_bytes;
|
||||
/** 完整解析且校验通过的消息数。 */
|
||||
uint32_t rx_messages;
|
||||
/** mavlink_send()/mavlink_send_raw() 成功次数。 */
|
||||
uint32_t tx_messages;
|
||||
/** 因 RX 环形缓冲满而丢弃的字节数。 */
|
||||
uint32_t rx_dropped;
|
||||
/** 解析器丢弃的帧数(长度/CRC 错误等)。 */
|
||||
uint32_t parse_errors;
|
||||
};
|
||||
|
||||
/**
|
||||
* 启动接收引擎(使能 UART RX 中断)。
|
||||
* 开启 CONFIG_MAVLINK_AUTO_START 时开机自动调用。
|
||||
* @param dev MAVLink 设备。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int mavlink_start(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 停止接收引擎(关闭 UART RX 中断)。
|
||||
* @param dev MAVLink 设备。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int mavlink_stop(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 查询接收引擎是否在运行。
|
||||
* @param dev MAVLink 设备。
|
||||
* @return true 表示在运行。
|
||||
*/
|
||||
bool mavlink_is_running(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 序列化并发送一条 MAVLink 消息。
|
||||
* 消息须先用 c_library_v2 的 pack/finalize 助手组好。线程安全;最多阻塞
|
||||
* CONFIG_MAVLINK_TX_TIMEOUT_MS。
|
||||
* @param dev MAVLink 设备。
|
||||
* @param msg 待发送消息。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int mavlink_send(const struct device *dev, const mavlink_message_t *msg);
|
||||
|
||||
/**
|
||||
* 原样发送已组帧的字节(必须是完整 MAVLink 帧)。
|
||||
* 主要用于转发抓到的帧;线程安全。
|
||||
* @param dev MAVLink 设备。
|
||||
* @param data 完整帧字节。
|
||||
* @param len 字节数。
|
||||
* @return 0 成功,负值为错误码。
|
||||
*/
|
||||
int mavlink_send_raw(const struct device *dev, const uint8_t *data,
|
||||
size_t len);
|
||||
|
||||
/**
|
||||
* 为某个消息 id 注册回调。
|
||||
* @param dev MAVLink 设备。
|
||||
* @param msgid 要匹配的消息 id,或 MAVLINK_MSG_ID_ANY 表示全部。
|
||||
* @param cb 回调(不能为 NULL)。
|
||||
* @param user_data 回传给 cb 的用户指针。
|
||||
* @return 0 成功,-EINVAL 参数非法,-ENOMEM 无空闲槽位。
|
||||
*/
|
||||
int mavlink_register_handler(const struct device *dev, uint32_t msgid,
|
||||
mavlink_rx_cb_t cb, void *user_data);
|
||||
|
||||
/**
|
||||
* 注销之前注册的回调。
|
||||
* @param dev MAVLink 设备。
|
||||
* @param msgid 注册时的消息 id。
|
||||
* @param cb 注册时的回调。
|
||||
* @return 0 成功,-ENOENT 无匹配注册。
|
||||
*/
|
||||
int mavlink_unregister_handler(const struct device *dev, uint32_t msgid,
|
||||
mavlink_rx_cb_t cb);
|
||||
|
||||
/**
|
||||
* 读取本节点配置的 MAVLink system id。
|
||||
* @param dev MAVLink 设备。
|
||||
* @return system id。
|
||||
*/
|
||||
uint8_t mavlink_get_system_id(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 读取本节点配置的 MAVLink component id。
|
||||
* @param dev MAVLink 设备。
|
||||
* @return component id。
|
||||
*/
|
||||
uint8_t mavlink_get_component_id(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 读取默认目标 system id(255 = 广播)。
|
||||
* @param dev MAVLink 设备。
|
||||
* @return 目标 system id。
|
||||
*/
|
||||
uint8_t mavlink_get_target_system(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 读取默认目标 component id(0 = 广播)。
|
||||
* @param dev MAVLink 设备。
|
||||
* @return 目标 component id。
|
||||
*/
|
||||
uint8_t mavlink_get_target_component(const struct device *dev);
|
||||
|
||||
/**
|
||||
* 拷贝一份链路统计快照。
|
||||
* @param dev MAVLink 设备。
|
||||
* @param stats 输出统计。
|
||||
*/
|
||||
void mavlink_get_stats(const struct device *dev, struct mavlink_stats *stats);
|
||||
|
||||
/**
|
||||
* 清零链路统计。
|
||||
* @param dev MAVLink 设备。
|
||||
*/
|
||||
void mavlink_reset_stats(const struct device *dev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ZEPHYR_INCLUDE_DRIVERS_MAVLINK_H_ */
|
||||
Reference in New Issue
Block a user