dbus 信号中收到的消息与发送的数据不匹配

Posted

技术标签:

【中文标题】dbus 信号中收到的消息与发送的数据不匹配【英文标题】:Message received in dbus singal does not match sent data 【发布时间】:2021-11-20 22:04:09 【问题描述】:

我正在尝试通过 dbus 信号接收消息。当我查看 dbus-monitor 时,发送的字节是正确的。但是我从信号中收到的消息内容有时是 3 或 4 条消息。我目前的假设是我需要释放一些东西或者我需要在接收信号之间刷新缓存,但是如果 sd_bus_unref pMessage 出现故障。还有什么我应该重置的吗?

我的回调:

int dbusCallback(sd_bus_message * pMessage, void * userdata, sd_bus_error * ret_error)

    int                   sdbusErr = 0;
    uint8_t *             pReceivedArray;
    eNetworkStateEvents_t returnState;

    /* Read the message */
    sdbusErr = sd_bus_message_read_array(pMessage, 'y',  (const void**) &pReceivedArray, &rxDecodedFrameLength);
        printf("Rx buffer %x %x", pReceivedArray[0], pReceivedArray[1]);

    memcpy(packetDecodedRxBuffer, pReceivedArray, rxDecodedFrameLength);

    // packetDecodedRxBuffer and rxDecodedFrameLength are both global
    return 1;

Dbus 线程:

    // Subscribe to the signal
    sd_bus_match_signal(bus,                                   // Bus
                        NULL,                                  // Slot
                        SERVICE_NAME,                          // Sender
                        OBJECT_PATH,                           // Path
                        INTERFACE_NAME,                        // Interface
                        MESSAGE_FROM_SIGNAL,                   // Member
                        dbusCallback,                          // Callback
                        NULL);                                 // Userdata

    for (;;)
    
        SysMonitorSignalThreadGood(SYSMONITOR_DBUS_THREAD_WATCHDOG_FLAG);

        // Process D-Bus requests
        sdbusErr = sd_bus_process(bus, NULL);

        if (0 > sdbusErr)
        
            printf("D-Bus: Failed to process bus: %s\n", strerror(-sdbusErr));
            continue;
        

        // Wait for the next D-Bus request to process
        sdbusErr = sd_bus_wait(bus, 100);

        if (0 > sdbusErr)
        
            printf("D-Bus: Failed to wait on bus: %s\n", strerror(-sdbusErr));
        

        tx_thread_sleep(MS_TO_TICKS(30u));
    

    sd_bus_unref(bus);

我的输出,包括 dbus-monitor 的输出

method call time=1632862915.524359 sender=:1.104 -> destination=com.example.example.Module serial=9 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 31 40 07 f5 02 4e
]
signal time=1632862915.528742 sender=:1.68 -> destination=(null destination) serial=1279 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 31 01 58 3a 9f 4d
]
method return time=1632862915.528998 sender=:1.68 -> destination=:1.104 serial=1280 reply_serial=9
Rx buffer 2 31 // Good!



method call time=1632862915.553558 sender=:1.104 -> destination=com.example.example.Module serial=10 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 33 01 01 11
]
signal time=1632862915.563459 sender=:1.68 -> destination=(null destination) serial=1281 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 33 01 da 58 a9 7f
]
method return time=1632862915.563786 sender=:1.68 -> destination=:1.104 serial=1282 reply_serial=10
ERROR: D-Bus: Bus call failed: Interrupted system call
ERROR: SD-BUS error occurred: System.Error.EINTR  Interrupted system call
method call time=1632862915.565927 sender=:1.104 -> destination=com.example.example.Module serial=11 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 33 01 01 11
]
SetDataRecord Received!!
signal time=1632862915.570788 sender=:1.68 -> destination=(null destination) serial=1283 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 33 01 da 58 a9 7f
]
method return time=1632862915.571083 sender=:1.68 -> destination=:1.104 serial=1284 reply_serial=11
Rx buffer 2 31 //Stale :(



method call time=1632862915.613063 sender=:1.104 -> destination=com.example.example.Module serial=12 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 33 01 01 11
]
signal time=1632862915.614794 sender=:1.68 -> destination=(null destination) serial=1285 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 33 01 da 58 a9 7f
]
method return time=1632862915.614949 sender=:1.68 -> destination=:1.104 serial=1286 reply_serial=12
Rx buffer 2 33 // Good!!


method call time=1632862915.643871 sender=:1.104 -> destination=com.example.example.Module serial=13 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 42 07 01
]
signal time=1632862915.648303 sender=:1.68 -> destination=(null destination) serial=1287 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 42 01 6d 10 8d 49
]
method return time=1632862915.648444 sender=:1.68 -> destination=:1.104 serial=1288 reply_serial=13
ERROR: Bus call failed: Interrupted system call
ERROR: SD-BUS error occurred: System.Error.EINTR  Interrupted system call
method call time=1632862915.650201 sender=:1.104 -> destination=com.example.example.Module serial=14 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 42 07 01
]
SetInstrumentDataStatus Received!!
signal time=1632862915.656238 sender=:1.68 -> destination=(null destination) serial=1289 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 42 01 6d 10 8d 49
]
method return time=1632862915.656621 sender=:1.68 -> destination=:1.104 serial=1290 reply_serial=14
Rx buffer 2 33 // Stale :(


method call time=1632862915.714133 sender=:1.104 -> destination=com.example.example.Module serial=15 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 42 07 01 00 02 0f 00 00 00 52 69 3d 72
]
signal time=1632862915.719213 sender=:1.68 -> destination=(null destination) serial=1291 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 42 01 6d 10 8d 49
]
method return time=1632862915.719308 sender=:1.68 -> destination=:1.104 serial=1292 reply_serial=15
Rx buffer 2 33 // Stale :(



method call time=1632862915.745547 sender=:1.104 -> destination=com.example.example.Module serial=16 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 42 07 01 00 02 0f 00 00 00 52 69 3d 72
]
signal time=1632862915.749602 sender=:1.68 -> destination=(null destination) serial=1293 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 42 01 6d 10 8d 49
]
method return time=1632862915.749657 sender=:1.68 -> destination=:1.104 serial=1294 reply_serial=16
Rx buffer 2 42 // Good!!



method call time=1632862915.777391 sender=:1.104 -> destination=com.example.example.Module serial=17 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 31 03 2d e5 d7 a1
]
signal time=1632862915.780942 sender=:1.68 -> destination=(null destination) serial=1295 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 31 01 58 3a 9f 4d
]
method return time=1632862915.781538 sender=:1.68 -> destination=:1.104 serial=1296 reply_serial=17
Rx buffer 2 42 // Stale :(



method call time=1632862915.857045 sender=:1.104 -> destination=com.example.example.Module serial=18 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 31 03 2d e5 d7 a1
]
signal time=1632862915.857262 sender=:1.68 -> destination=(null destination) serial=1297 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 31 01 58 3a 9f 4d
]
method return time=1632862915.857288 sender=:1.68 -> destination=:1.104 serial=1298 reply_serial=18
Rx buffer 2 42 // Stale :(



method call time=1632862915.887795 sender=:1.104 -> destination=com.example.example.Module serial=19 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 31 03 2d e5 d7 a1
]
signal time=1632862915.890850 sender=:1.68 -> destination=(null destination) serial=1299 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 31 01 58 3a 9f 4d
]
method return time=1632862915.891610 sender=:1.68 -> destination=:1.104 serial=1300 reply_serial=19
Rx buffer 2 42 // Stale :(



method call time=1632862915.916836 sender=:1.104 -> destination=com.example.example.Module serial=20 path=/; interface=example.DBus.Module; member=message_to_server
array of bytes [
    01 31 03 2d e5 d7 a1
]
signal time=1632862915.920036 sender=:1.68 -> destination=(null destination) serial=1301 path=/; interface=example.DBus.Module; member=message_from_server
array of bytes [
    02 31 01 58 3a 9f 4d
]
method return time=1632862915.920089 sender=:1.68 -> destination=:1.104 serial=1302 reply_serial=20
Rx buffer 2 31 //Good

【问题讨论】:

【参考方案1】:

我的问题最终是当 dbus 调用成功发送时我有时会收到 System.Error.EINTR 错误。由于这个错误,我假设失败并再次发送,因此有多个回复。忽略这些错误会阻止我发送多个相同的请求并解决我的问题。

有一个sd_bus_flush 调用,但这是为了确保在关闭总线之前发送消息,对于接收消息,没有缓存或刷新机制。

【讨论】:

以上是关于dbus 信号中收到的消息与发送的数据不匹配的主要内容,如果未能解决你的问题,请参考以下文章

使用 dbus 在 Python 中发送消息

Firebase消息传递/不匹配的凭据

获取由消息发送者的“dbus_request_name”设置的总线名称

dbus-send to QDBus 程序示例

DBus 与其他进程间通信方式的区别

如何确定 DBus 消息中“结构数组”的长度?