Skip to content

Commit

Permalink
Merge branch 'hide-mode' into 'master'
Browse files Browse the repository at this point in the history
[modes] Add possibility to hide modes from the UI



See merge request !2
  • Loading branch information
philippedeswert committed Mar 22, 2016
2 parents df78d2f + 9c3e0f2 commit 1bcc68e
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 15 deletions.
2 changes: 1 addition & 1 deletion configure.ac
@@ -1,4 +1,4 @@
AC_INIT([usb_moded], [0.85.2])
AC_INIT([usb_moded], [0.85.3])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])

AM_CONFIG_HEADER([config.h])
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
@@ -1,3 +1,9 @@
usb-moded (0.85.2) unstable; urgency=low

* [hidden modes] Add support for hidden modes. Fixes: MER#1540

-- Philippe De Swert <philippedeswert@gmail.com> Tue 22 Mar 23:24:01 EET 2016

usb-moded (0.85.2) unstable; urgency=low

* [devel] Fix DIAG_MODE definition. Fixes: JB#32805
Expand Down
24 changes: 21 additions & 3 deletions docs/usb_moded-doc.txt
Expand Up @@ -14,9 +14,11 @@ Thus if you need this functionality, usb_moded needs to be started
with the session.
(functionality not verified yet on multi-user setups. See TODO)

Usb cable detection is supported by several different methods.
Old deprecated hal, through Nokia's own battery monitoring bme,
or the preferred udev option. (This has to be chosen at compile time)
Usb cable detection is handled with udev, it even tries to guess
which is the correct power_supply entry to use.

A Qt lib to interact with usb-moded is available here:
https://git.merproject.org/mer-core/libusb-moded-qt

Starting usb_moded
-------------------
Expand Down Expand Up @@ -398,3 +400,19 @@ For this are the

Network options. nat_interface documents which interfaces the internet facing modem. noroaming when set to 1
will prohibit enabling the modem interface in case you are roaming (this requires ofono).


hidden modes
------------

Sometimes it is useful to hide modes from the ui (the mode can be queried through dbus).
To hide modes this can be done by adding a hide= key to the
[usbmode] section. If you want to hide more modes this needs
to be a comma separated list.

For example
[usbmode]
hide=developer_mode,bastard_mode

Modes can also be set and removed through dbus (one mode at a time)
See usb_moded_util (-i and -u)
2 changes: 2 additions & 0 deletions src/usb_moded-config-private.h
Expand Up @@ -26,4 +26,6 @@

char * get_mode_setting(void);
set_config_result_t set_mode_setting(const char *mode);
set_config_result_t set_hide_mode_setting(const char *mode);
set_config_result_t set_unhide_mode_setting(const char *mode);
set_config_result_t set_network_setting(const char *config, const char *setting);
80 changes: 79 additions & 1 deletion src/usb_moded-config.c
Expand Up @@ -409,6 +409,73 @@ set_config_result_t set_mode_setting(const char *mode)
return (set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}

/* Builds the string used for hidden modes, when hide set to one builds the
new string of hidden modes when adding one, otherwise it will remove one */
static const char * make_hidden_modes_string(const char *hidden, int hide)
{
GString *modelist_str;
char *hidden_modes_list;
gchar **hidden_mode_split;
int i;


hidden_modes_list = get_hidden_modes();
if(hidden_modes_list)
{
hidden_mode_split = g_strsplit(hidden_modes_list, ",", 0);
}
else
{
/* no hidden modes yet. So just return the original string */
return hidden;
}

modelist_str = g_string_new(NULL);

for(i = 0; hidden_mode_split[i] != NULL; i++)
{
if(!strcmp(hidden_mode_split[i], hidden))
{
/* if hiding a mode that is already hidden do nothing */
if(hide)
return(NULL);
if(!hide)
continue;
}
modelist_str = g_string_append(modelist_str, hidden_mode_split[i]);
if(hidden_mode_split[i+1] != NULL)
modelist_str = g_string_append(modelist_str, ",");
}
if(hide)
{
modelist_str = g_string_append(modelist_str, ",");
modelist_str = g_string_append(modelist_str, hidden);
}

g_strfreev(hidden_mode_split);
return(g_string_free(modelist_str, FALSE));
}

set_config_result_t set_hide_mode_setting(const char *mode)
{
set_config_result_t ret;

ret = set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, make_hidden_modes_string(mode, 1));
if(ret == SET_CONFIG_UPDATED)
send_supported_modes_signal();
return(ret);
}

set_config_result_t set_unhide_mode_setting(const char *mode)
{
set_config_result_t ret;

ret = set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, make_hidden_modes_string(mode, 0));
if(ret == SET_CONFIG_UPDATED)
send_supported_modes_signal();
return(ret);
}

/*
* @param config : the key to be set
* @param setting : The value to be set
Expand Down Expand Up @@ -515,7 +582,7 @@ int conf_file_merge(void)
{
GDir *confdir;
struct stat fileinfo, dir;
char *mode = 0, *ip = 0, *gateway = 0, *udev = 0;
char *mode = 0, *ip = 0, *gateway = 0, *udev = 0, *hide = 0;
gchar *filename_full;
const gchar *filename;
GString *keyfile_string = NULL;
Expand Down Expand Up @@ -575,8 +642,11 @@ int conf_file_merge(void)
mode = get_mode_setting();
/* store udev path (especially important for the upgrade path */
udev = find_udev_path();
/* store network info */
ip = get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY);
gateway = get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY);
/* store hidden modes */
hide = get_hidden_modes();
continue;
}
/* load contents of file, if it fails skip to next one */
Expand Down Expand Up @@ -617,6 +687,9 @@ int conf_file_merge(void)
set_network_setting(NETWORK_IP_KEY, ip);
if(gateway)
set_network_setting(NETWORK_GATEWAY_KEY, gateway);
/* re-add hidden modes info */
if(hide)
set_hide_mode_setting(hide);
}
else
ret = 1;
Expand Down Expand Up @@ -655,6 +728,11 @@ char * get_android_product_id(void)
return(get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_ID_KEY));
}

char * get_hidden_modes(void)
{
return(get_conf_string(MODE_SETTING_ENTRY, MODE_HIDE_KEY));
}

int check_android_section(void)
{
GKeyFile *settingsfile;
Expand Down
3 changes: 3 additions & 0 deletions src/usb_moded-config.h
Expand Up @@ -57,6 +57,7 @@
#define ANDROID_VENDOR_ID_KEY "idVendor"
#define ANDROID_PRODUCT_KEY "iProduct"
#define ANDROID_PRODUCT_ID_KEY "idProduct"
#define MODE_HIDE_KEY "hide"

const char * find_mounts(void);
int find_sync(void);
Expand All @@ -78,6 +79,8 @@ char * get_android_vendor_id(void);
char * get_android_product(void);
char * get_android_product_id(void);

char * get_hidden_modes(void);

int check_android_section(void);

int is_roaming_not_allowed(void);
Expand Down
46 changes: 46 additions & 0 deletions src/usb_moded-dbus.c
Expand Up @@ -198,6 +198,52 @@ static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessa
}
dbus_error_free(&err);
}
else if(!strcmp(member, USB_MODE_HIDE))
{
char *config = 0;
DBusError err = DBUS_ERROR_INIT;

if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID))
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
else
{
/* error checking is done when setting configuration */
int ret = set_hide_mode_setting(config);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(MODE_SETTING_ENTRY, MODE_HIDE_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);
}
else
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
}
dbus_error_free(&err);
}
else if(!strcmp(member, USB_MODE_UNHIDE))
{
char *config = 0;
DBusError err = DBUS_ERROR_INIT;

if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID))
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
else
{
/* error checking is done when setting configuration */
int ret = set_unhide_mode_setting(config);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(MODE_SETTING_ENTRY, MODE_HIDE_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);
}
else
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
}
dbus_error_free(&err);
}
else if(!strcmp(member, USB_MODE_NETWORK_SET))
{
char *config = 0, *setting = 0;
Expand Down
6 changes: 2 additions & 4 deletions src/usb_moded-dbus.h
Expand Up @@ -43,10 +43,8 @@
#define USB_MODE_RESCUE_OFF "rescue_off" /* turns rescue mode off so normal mode selection is restored */
#define USB_MODE_CONFIG_GET "get_config" /* returns the mode set in the config */
#define USB_MODE_LIST "get_modes" /* returns a comma-separated list of supported modes for ui's */

/**
* @credential usb-moded::USBControl Credential needed to be able to call the set_mode or set_config methods
**/
#define USB_MODE_HIDE "hide_mode" /* hide a mode */
#define USB_MODE_UNHIDE "unhide_mode" /* unhide a mode */
#define USB_MODE_STATE_SET "set_mode" /* set a mode (only works when connected) */
#define USB_MODE_CONFIG_SET "set_config" /* set the mode that needs to be activated in the config file */
#define USB_MODE_NETWORK_SET "net_config" /* set the network config in the config file */
Expand Down
2 changes: 2 additions & 0 deletions src/usb_moded-modes.h
Expand Up @@ -44,3 +44,5 @@
**/
#define MODE_CHARGING_FALLBACK "charging_only_fallback"
#define MODE_CHARGER "dedicated_charger"

void send_supported_modes_signal(void);
2 changes: 2 additions & 0 deletions src/usb_moded-network.c
Expand Up @@ -615,6 +615,8 @@ static int connman_fill_connection_data(DBusMessage *reply, struct ipforward_dat
/* if cellular not online, connect it */
if(strcmp(string, "online"))
{
/* if in ready state connection might be up anyway */
if(strcmp(string, "ready"))
log_debug("Not online. Turning on cellular data connection.\n");
return(1);
}
Expand Down
74 changes: 71 additions & 3 deletions src/usb_moded-util.c
Expand Up @@ -186,6 +186,60 @@ static int set_mode_config (char *mode)
return 1;
}

static int set_hide_mode_config (char *mode)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;

printf("Trying to hide the following mode %s in the config file\n", mode);
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_HIDE)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}

if(ret)
{
printf("mode hidden = %s\n", ret);
return 0;
}

/* not everything went as planned, return error */
return 1;
}

static int set_unhide_mode_config (char *mode)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;

printf("Trying to unhide the following mode %s in the config file\n", mode);
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_UNHIDE)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}

if(ret)
{
printf("mode unhidden = %s\n", ret);
return 0;
}

/* not everything went as planned, return error */
return 1;
}

static int handle_network(char *network)
{
char *operation = 0, *setting = 0, *value = 0;
Expand Down Expand Up @@ -260,7 +314,7 @@ static int handle_network(char *network)
int main (int argc, char *argv[])
{
int query = 0, network = 0, setmode = 0, config = 0;
int modelist = 0, mode_configured = 0;
int modelist = 0, mode_configured = 0, hide = 0, unhide = 0;
int res = 1, opt, rescue = 0;
char *option = 0;

Expand All @@ -270,7 +324,7 @@ int main (int argc, char *argv[])
exit(1);
}

while ((opt = getopt(argc, argv, "c:dhmn:qrs:")) != -1)
while ((opt = getopt(argc, argv, "c:dhi:mn:qrs:u:")) != -1)
{
switch (opt) {
case 'c':
Expand All @@ -280,6 +334,10 @@ int main (int argc, char *argv[])
case 'd':
mode_configured = 1;
break;
case 'i':
hide = 1;
option = optarg;
break;
case 'm':
modelist = 1;
break;
Expand All @@ -297,18 +355,24 @@ int main (int argc, char *argv[])
setmode = 1;
option = optarg;
break;
case 'u':
unhide = 1;
option = optarg;
break;
case 'h':
default:
fprintf(stderr, "\nUsage: %s -<option> <args>\n\n \
Options are: \n \
\t-c to set a mode in the config file,\n \
\t-d to get the default mode set in the configuration, \n \
\t-h to get this help, \n \
\t-i hide a mode,\n \
\t-n to get/set network configuration. Use get:${config}/set:${config},${value}\n \
\t-m to get the list of supported modes, \n \
\t-q to query the current mode,\n \
\t-r turn rescue mode off,\n \
\t-s to set/activate a mode\n",
\t-s to set/activate a mode\n \
\t-u unhide a mode\n",
argv[0]);
exit(1);
}
Expand Down Expand Up @@ -339,6 +403,10 @@ int main (int argc, char *argv[])
res = handle_network(option);
else if (rescue)
res = unset_rescue();
else if (hide)
res = set_hide_mode_config(option);
else if (unhide)
res = set_unhide_mode_config(option);

/* subfunctions will return 1 if an error occured, print message */
if(res)
Expand Down

0 comments on commit 1bcc68e

Please sign in to comment.