为什么 Dart FFI 从这个 C 结构中生成一个不透明的 class?
Why does Dart FFI generate an opaque class from this C struct?
我想使用 MAVLink C library 来解析 dart 中的 MAVLink 数据包,但是 Dart FFI 生成的 mavlink_message_t 是不透明的 class,而其他结构如 mavlink_system_t正常生成时带有所有属性。这种行为的原因是什么,我该如何解决?
mavlink_message_t C 结构
MAVPACKED(
typedef struct __mavlink_message {
uint16_t checksum; ///< sent at end of packet
uint8_t magic; ///< protocol magic marker
uint8_t len; ///< Length of payload
uint8_t incompat_flags; ///< flags that must be understood
uint8_t compat_flags; ///< flags that can be ignored if not understood
uint8_t seq; ///< Sequence of packet
uint8_t sysid; ///< ID of message sender system/aircraft
uint8_t compid; ///< ID of the message sender component
uint32_t msgid:24; ///< ID of message in payload
uint64_t payload64[(MAVLINK_MAX_PAYLOAD_LEN+MAVLINK_NUM_CHECKSUM_BYTES+7)/8];
uint8_t ck[2]; ///< incoming checksum bytes
uint8_t signature[MAVLINK_SIGNATURE_BLOCK_LEN];
}) mavlink_message_t;
mavlink_system_t C 结构
MAVPACKED(
typedef struct __mavlink_system {
uint8_t sysid; ///< Used by the MAVLink message_xx_send() convenience function
uint8_t compid; ///< Used by the MAVLink message_xx_send() convenience function
}) mavlink_system_t;
FFI 生成 Dart classes
class mavlink_message_t extends ffi.Opaque {}
@ffi.Packed(1)
class mavlink_system_t extends ffi.Struct {
@ffi.Uint8()
external int sysid;
@ffi.Uint8()
external int compid;
}
感谢@Richard Heap 的评论,我发现这个问题的根源是 msgid:24
字段定义。由于它是 Dart FFI 的 bit field, which isn't currently supported,因此 ffigen 从中生成了一个不透明的 class 定义。删除 msgid:24
声明后,生成了一个 ffi.Struct 而不是 ffi.Opaque
@ffi.Packed(1)
class mavlink_message_t extends ffi.Struct {
@ffi.Uint16()
external int checksum;
@ffi.Uint8()
external int magic;
@ffi.Uint8()
external int len;
@ffi.Uint8()
external int incompat_flags;
@ffi.Uint8()
external int compat_flags;
@ffi.Uint8()
external int seq;
@ffi.Uint8()
external int sysid;
@ffi.Uint8()
external int compid;
@ffi.Array.multi([33])
external ffi.Array<ffi.Uint64> payload64;
@ffi.Array.multi([2])
external ffi.Array<ffi.Uint8> ck;
@ffi.Array.multi([13])
external ffi.Array<ffi.Uint8> signature;
}
这当然是不够的,因为我需要所有字段,但是有一个 workaround 我会测试一下。
我想使用 MAVLink C library 来解析 dart 中的 MAVLink 数据包,但是 Dart FFI 生成的 mavlink_message_t 是不透明的 class,而其他结构如 mavlink_system_t正常生成时带有所有属性。这种行为的原因是什么,我该如何解决?
mavlink_message_t C 结构
MAVPACKED(
typedef struct __mavlink_message {
uint16_t checksum; ///< sent at end of packet
uint8_t magic; ///< protocol magic marker
uint8_t len; ///< Length of payload
uint8_t incompat_flags; ///< flags that must be understood
uint8_t compat_flags; ///< flags that can be ignored if not understood
uint8_t seq; ///< Sequence of packet
uint8_t sysid; ///< ID of message sender system/aircraft
uint8_t compid; ///< ID of the message sender component
uint32_t msgid:24; ///< ID of message in payload
uint64_t payload64[(MAVLINK_MAX_PAYLOAD_LEN+MAVLINK_NUM_CHECKSUM_BYTES+7)/8];
uint8_t ck[2]; ///< incoming checksum bytes
uint8_t signature[MAVLINK_SIGNATURE_BLOCK_LEN];
}) mavlink_message_t;
mavlink_system_t C 结构
MAVPACKED(
typedef struct __mavlink_system {
uint8_t sysid; ///< Used by the MAVLink message_xx_send() convenience function
uint8_t compid; ///< Used by the MAVLink message_xx_send() convenience function
}) mavlink_system_t;
FFI 生成 Dart classes
class mavlink_message_t extends ffi.Opaque {}
@ffi.Packed(1)
class mavlink_system_t extends ffi.Struct {
@ffi.Uint8()
external int sysid;
@ffi.Uint8()
external int compid;
}
感谢@Richard Heap 的评论,我发现这个问题的根源是 msgid:24
字段定义。由于它是 Dart FFI 的 bit field, which isn't currently supported,因此 ffigen 从中生成了一个不透明的 class 定义。删除 msgid:24
声明后,生成了一个 ffi.Struct 而不是 ffi.Opaque
@ffi.Packed(1)
class mavlink_message_t extends ffi.Struct {
@ffi.Uint16()
external int checksum;
@ffi.Uint8()
external int magic;
@ffi.Uint8()
external int len;
@ffi.Uint8()
external int incompat_flags;
@ffi.Uint8()
external int compat_flags;
@ffi.Uint8()
external int seq;
@ffi.Uint8()
external int sysid;
@ffi.Uint8()
external int compid;
@ffi.Array.multi([33])
external ffi.Array<ffi.Uint64> payload64;
@ffi.Array.multi([2])
external ffi.Array<ffi.Uint8> ck;
@ffi.Array.multi([13])
external ffi.Array<ffi.Uint8> signature;
}
这当然是不够的,因为我需要所有字段,但是有一个 workaround 我会测试一下。