Skip to content

Commit

Permalink
Clean up signal sending code as there is too much duplication.
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed Mar 18, 2014
1 parent 8437544 commit 276d66c
Showing 1 changed file with 34 additions and 70 deletions.
104 changes: 34 additions & 70 deletions src/usb_moded-dbus.c
Expand Up @@ -298,27 +298,27 @@ void usb_moded_dbus_cleanup(void)
}

/**
* Send regular usb_moded state signal
* Helper function for sending the different signals
*
* @return 1 on success, 0 on failure
* @param state_ind the signal name
*
* @param signal_type the type of signal (normal, error, ...)
* @@param content string which can be mode name, error, list of modes, ...
*/
int usb_moded_send_signal(const char *state_ind)
static int usb_moded_dbus_signal(const char *signal_type, const char *content)
{
int result = 1;
DBusMessage* msg = 0;

// create a signal and check for errors
msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_SIGNAL_NAME );
msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, signal_type );
if (NULL == msg)
{
log_debug("Message Null\n");
goto EXIT;
}

// append arguments onto signal
if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &state_ind, DBUS_TYPE_INVALID))
if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &content, DBUS_TYPE_INVALID))
{
log_debug("Appending arguments failed. Out Of Memory!\n");
goto EXIT;
Expand All @@ -340,74 +340,38 @@ int usb_moded_send_signal(const char *state_ind)
return result;
}

int usb_moded_send_error_signal(const char *error)
/**
* Send regular usb_moded state signal
*
* @return 1 on success, 0 on failure
* @param state_ind the signal name
*
*/
int usb_moded_send_signal(const char *state_ind)
{
int result = 1;
DBusMessage* msg = 0;

// create a signal and check for errors
msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_ERROR_SIGNAL_NAME );
if (NULL == msg)
{
log_debug("Message Null\n");
goto EXIT;
}

// append arguments onto signal
if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &error, DBUS_TYPE_INVALID))
{
log_debug("Appending arguments failed. Out Of Memory!\n");
goto EXIT;
}

// send the message on the correct bus and flush the connection
if (!dbus_connection_send(dbus_connection_sys, msg, 0))
{
log_debug("Failed sending message. Out Of Memory!\n");
goto EXIT;
}
result = 0;

EXIT:
// free the message
if(msg != 0)
dbus_message_unref(msg);
return(usb_moded_dbus_signal(USB_MODE_SIGNAL_NAME, state_ind));
}

return result;
/**
* Send regular usb_moded error signal
*
* @return 1 on success, 0 on failure
* @param state_ind the signal name
*
*/
int usb_moded_send_error_signal(const char *error)
{
return(usb_moded_dbus_signal(USB_MODE_ERROR_SIGNAL_NAME, error));
}

/**
* Send regular usb_moded mode list signal
*
* @return 1 on success, 0 on failure
* @param state_ind the signal name
*
*/
int usb_moded_send_supported_modes_signal(const char *supported_modes)
{
int result = 1;
DBusMessage* msg = 0;

// create a signal and check for errors
msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_SUPPORTED_MODES_SIGNAL_NAME);
if (NULL == msg)
{
log_debug("Message Null\n");
goto EXIT;
}

// append arguments onto signal
if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &supported_modes, DBUS_TYPE_INVALID))
{
log_debug("Appending arguments failed. Out Of Memory!\n");
goto EXIT;
}

// send the message on the correct bus and flush the connection
if (!dbus_connection_send(dbus_connection_sys, msg, 0))
{
log_debug("Failed sending message. Out Of Memory!\n");
goto EXIT;
}
result = 0;

EXIT:
// free the message
if(msg != 0)
dbus_message_unref(msg);

return result;
return(usb_moded_dbus_signal(USB_MODE_SUPPORTED_MODES_SIGNAL_NAME, supported_modes));
}

0 comments on commit 276d66c

Please sign in to comment.