#include <syslog.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include <unistd.h>
#include <fcntl.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h> /* for glib main loop */

int dbus_monitor(void)
{
   
    DBusConnection *connection;
    DBusError error;
    dbus_error_init(&error);
    syslog(LOG_ERR,"WD--file=[%s]--fun=[%s]--line=[%d]-Connection Error:[%s]",__FILE__,__FUNCTION__,__LINE__,error.message);
    // Connect to session bus
    connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
    if (dbus_error_is_set(&error))
    {
        fprintf(stderr, "Connection Error (%s)\n", error.message);
        syslog(LOG_ERR,"WD--file=[%s]--fun=[%s]--line=[%d]-Connection Error:[%s]",__FILE__,__FUNCTION__,__LINE__,error.message);
        dbus_error_free(&error);
        return 1;
    }

    // Add a rule for incoming signals
    dbus_bus_add_match(connection, "type='signal',interface='org.freedesktop.login1.Manager'", &error);
    dbus_connection_flush(connection);

    if (dbus_error_is_set(&error))
    {
        fprintf(stderr, "Error adding D-Bus match rule (%s)\n", error.message);
        syslog(LOG_ERR,"WD--file=[%s]--fun=[%s]--line=[%d]-Connection Error:[%s]",__FILE__,__FUNCTION__,__LINE__,error.message);
        dbus_error_free(&error);
        return 1;
    }

    // Main event loop
    while (1)
    {
        DBusMessage *message;
        DBusMessageIter args;
        guint16 sigvalue;
        DBusDispatchStatus status = dbus_connection_read_write(connection, 1000); // 设置阻塞时间
        // Blocking call to receive a message
        message = dbus_connection_pop_message(connection);

        if (message != NULL)
        {
            if (dbus_message_is_signal(message, "com.kylin.MaintainMode", "BootModeChanged"))
            {
                // 读取参数
                if (!dbus_message_iter_init(message, &args))
                    fprintf(stderr, "消息没有参数！\n");
                else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args))
                    fprintf(stderr, "参数格式错误！\n");
                else
                {
                    dbus_message_iter_get_basic(&args, &sigvalue);
                    printf("获取信号的值： %d\n", sigvalue);
                    syslog(LOG_ERR, "WD--file=[%s]--fun=[%s]--line=[%d]-获取信号的值:[%d]", __FILE__, __FUNCTION__, __LINE__, sigvalue);
                     char command[255] = {};
                    if (sigvalue == 1) // 是进入运维模式
                    {
                        ////函数处理
                        ////获取到信号，do what you do.....
                        syslog(LOG_INFO, "------进入运维模式-----");
                        snprintf(command, sizeof(command), "%s %s", "/usr/ost-ex.d/boot_handle.sh","in");
                        if (system(command) == 0)
                        {
                            syslog(LOG_INFO, "copy success");
                        }
                        syslog(LOG_INFO, "------进入运维模式boot备份end-----");
                    }else if(sigvalue == 3){
                        syslog(LOG_INFO, "------进入运维模式-----");
                        snprintf(command, sizeof(command), "%s %s", "/usr/ost-ex.d/boot_handle.sh","out");
                        if (system(command) == 0)
                        {
                            syslog(LOG_INFO, "copy success");
                        }
                        syslog(LOG_INFO, "------进入运维模式boot备份end-----");
                    }
                }
            }
            // Free the received message
            dbus_message_unref(message);
        }
    }
    // Close the connection
    dbus_connection_unref(connection);
    return 0;
}