Skip to content

Commit

Permalink
Merge pull request #37 from monich/sig_usb_config_ind
Browse files Browse the repository at this point in the history
Add "sig_usb_config_ind" D-Bus signal
  • Loading branch information
philippedeswert committed May 27, 2015
2 parents daa2edf + 6f28e0e commit de9faa9
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 27 deletions.
5 changes: 5 additions & 0 deletions src/com.meego.usb_moded.xml
Expand Up @@ -40,5 +40,10 @@
<signal name="sig_usb_supported_modes_ind">
<arg name="modes" type="s"/>
</signal>
<signal name="sig_usb_config_ind">
<arg name="section" type="s"/>
<arg name="key" type="s"/>
<arg name="value" type="s"/>
</signal>
</interface>
</node>
5 changes: 3 additions & 2 deletions src/usb_moded-config-private.h
@@ -1,5 +1,6 @@
/*
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Copyright (C) 2012-2015 Jolla. All rights reserved.
Author: Philippe De Swert <philippe.de-swert@nokia.com>
Expand All @@ -24,6 +25,6 @@
/*============================================================================= */

char * get_mode_setting(void);
int set_mode_setting(const char *mode);
int set_network_setting(const char *config, const char *setting);
set_config_result_t set_mode_setting(const char *mode);
set_config_result_t set_network_setting(const char *config, const char *setting);
char *get_network_setting(const char *config);
68 changes: 49 additions & 19 deletions src/usb_moded-config.c
Expand Up @@ -2,6 +2,7 @@
@file usb_moded-config.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Copyright (C) 2012-2015 Jolla. All rights reserved.
@author: Philippe De Swert <philippe.de-swert@nokia.com>
Expand Down Expand Up @@ -330,16 +331,30 @@ char * get_mode_setting(void)
return(get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY));
}

int set_config_setting(const char *entry, const char *key, const char *value)
set_config_result_t set_config_setting(const char *entry, const char *key, const char *value)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
int ret = 0;
set_config_result_t ret = SET_CONFIG_ERROR;
gchar *keyfile;

settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
if(test)
{
char *old = g_key_file_get_string(settingsfile, entry, key, NULL);
if (old)
{
gboolean unchanged = (g_strcmp0(old, value) == 0);
g_free(old);
if (unchanged)
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
}
else
{
log_debug("No conffile. Creating.\n");
create_conf_file();
Expand All @@ -351,14 +366,14 @@ int set_config_setting(const char *entry, const char *key, const char *value)
the contents will be correctly written to file afterwards.
Just a precaution. */
g_key_file_free(settingsfile);
ret = g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL);
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
g_free(keyfile);

/* g_file_set_contents returns 1 on succes, since set_mode_settings returns 0 on succes we return the ! value */
return(!ret);
return (ret);
}

int set_mode_setting(const char *mode)
set_config_result_t set_mode_setting(const char *mode)
{
return (set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}
Expand All @@ -367,43 +382,58 @@ int set_mode_setting(const char *mode)
* @param config : the key to be set
* @param setting : The value to be set
*/
int set_network_setting(const char *config, const char *setting)
set_config_result_t set_network_setting(const char *config, const char *setting)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
int ret = 0;
gchar *keyfile;

if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
if(validate_ip(setting) != 0)
return(1);
return SET_CONFIG_ERROR;

settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
{
log_debug("No conffile. Creating.\n");
create_conf_file();
}

if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_INTERFACE_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
{
set_config_result_t ret = SET_CONFIG_ERROR;
if (test)
{
char *old = g_key_file_get_string(settingsfile, NETWORK_ENTRY, config, NULL);
if (old)
{
gboolean unchanged = (g_strcmp0(old, setting) == 0);
g_free(old);
if (unchanged)
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
}
else
{
log_debug("No conffile. Creating.\n");
create_conf_file();
}

g_key_file_set_string(settingsfile, NETWORK_ENTRY, config, setting);
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
/* free the settingsfile before writing things out to be sure
the contents will be correctly written to file afterwards.
Just a precaution. */
g_key_file_free(settingsfile);
ret = g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL);
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
free(keyfile);
return ret;
}
else
{
g_key_file_free(settingsfile);
return(1);
return SET_CONFIG_ERROR;
}
/* g_file_set_contents returns 1 on succes, since set_mode_settings returns 0 on succes we return the ! value */
return(!ret);
}

char * get_network_setting(const char *config)
Expand Down
11 changes: 10 additions & 1 deletion src/usb_moded-config.h
@@ -1,6 +1,7 @@
/*
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Copyright (C) 2012-2015 Jolla. All rights reserved.
author: Philippe De Swert <philippe.de-swert@nokia.com>
Expand Down Expand Up @@ -83,5 +84,13 @@ int check_android_section(void);

int is_roaming_not_allowed(void);

typedef enum set_config_result_t {
SET_CONFIG_ERROR = -1,
SET_CONFIG_UPDATED,
SET_CONFIG_UNCHANGED
} set_config_result_t;

int conf_file_merge(void);
int set_config_setting(const char *entry, const char *key, const char *value);
set_config_result_t set_config_setting(const char *entry, const char *key, const char *value);

#define SET_CONFIG_OK(ret) ((ret) >= SET_CONFIG_UPDATED)
36 changes: 34 additions & 2 deletions src/usb_moded-dbus.c
Expand Up @@ -34,13 +34,34 @@
#include "usb_moded.h"
#include "usb_moded-modes.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-config.h"
#include "usb_moded-config-private.h"
#include "usb_moded-network.h"
#include "usb_moded-log.h"

static DBusConnection *dbus_connection_sys = NULL;
extern gboolean rescue_mode;

/**
* Issues "sig_usb_config_ind" signal.
*/
static void usb_moded_send_config_signal(const char *section, const char *key, const char *value)
{
log_debug(USB_MODE_CONFIG_SIGNAL_NAME ": %s %s %s\n", section, key, value);
if (dbus_connection_sys)
{
DBusMessage* msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_SIGNAL_NAME);
if (msg) {
dbus_message_append_args(msg, DBUS_TYPE_STRING, &section,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID);
dbus_connection_send(dbus_connection_sys, msg, NULL);
dbus_message_unref(msg);
}
}
}

static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessage *const msg, gpointer const user_data)
{
DBusHandlerResult status = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
Expand Down Expand Up @@ -96,6 +117,11 @@ static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessa
" <signal name=\"" USB_MODE_SUPPORTED_MODES_SIGNAL_NAME "\">\n"
" <arg name=\"modes\" type=\"s\"/>\n"
" </signal>\n"
" <signal name=\"" USB_MODE_CONFIG_SIGNAL_NAME "\">\n"
" <arg name=\"section\" type=\"s\"/>\n"
" <arg name=\"key\" type=\"s\"/>\n"
" <arg name=\"value\" type=\"s\"/>\n"
" </signal>\n"
" </interface>\n"
"</node>\n";

Expand Down Expand Up @@ -159,7 +185,10 @@ static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessa
else
{
/* error checking is done when setting configuration */
if(!set_mode_setting(config))
int ret = set_mode_setting(config);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(MODE_SETTING_ENTRY, MODE_SETTING_KEY, config);
if (SET_CONFIG_OK(ret))
{
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID);
Expand All @@ -179,7 +208,10 @@ static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessa
else
{
/* error checking is done when setting configuration */
if(!set_network_setting(config, setting))
int ret = set_network_setting(config, setting);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(NETWORK_ENTRY, config, setting);
if (SET_CONFIG_OK(ret))
{
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_STRING, &setting, DBUS_TYPE_INVALID);
Expand Down
3 changes: 2 additions & 1 deletion src/usb_moded-dbus.h
@@ -1,5 +1,6 @@
/*
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Copyright (C) 2012-2015 Jolla. All rights reserved.
Author: Philippe De Swert <philippe.de-swert@meego.com>
Expand Down Expand Up @@ -33,7 +34,7 @@
* states listed in usb_moded-modes.h.
**/
#define USB_MODE_SIGNAL_NAME "sig_usb_state_ind"

#define USB_MODE_CONFIG_SIGNAL_NAME "sig_usb_config_ind"
#define USB_MODE_ERROR_SIGNAL_NAME "sig_usb_state_error_ind"
#define USB_MODE_SUPPORTED_MODES_SIGNAL_NAME "sig_usb_supported_modes_ind"

Expand Down
3 changes: 1 addition & 2 deletions src/usb_moded-modules.c
Expand Up @@ -2,7 +2,7 @@
@file usb_moded-modules.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Copyright (C) 2012 Jolla. All rights reserved.
Copyright (C) 2012-2015 Jolla. All rights reserved.
@author: Philippe De Swert <philippe.de-swert@nokia.com>
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
Expand Down Expand Up @@ -38,7 +38,6 @@
#include "usb_moded-config.h"
#include "usb_moded-dbus.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded-config.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-modes.h"

Expand Down

0 comments on commit de9faa9

Please sign in to comment.