Skip to content

Commit

Permalink
hide: Allow to hide modes
Browse files Browse the repository at this point in the history
Allow to hide a mode. Also add a dbus method to do so.
(Only one mode atm)

Signed-off-by: Philippe De Swert <philippedeswert@gmail.com>
  • Loading branch information
philippedeswert committed Dec 15, 2015
1 parent 04aa22a commit a47964a
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 8 deletions.
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);
15 changes: 15 additions & 0 deletions src/usb_moded-config.c
Expand Up @@ -409,6 +409,16 @@ set_config_result_t set_mode_setting(const char *mode)
return (set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}

set_config_result_t set_hide_mode_setting(const char *mode)
{
return (set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, mode));
}

set_config_result_t set_unhide_mode_setting(const char *mode)
{
return (set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, ""));
}

/*
* @param config : the key to be set
* @param setting : The value to be set
Expand Down Expand Up @@ -655,6 +665,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
76 changes: 72 additions & 4 deletions src/usb_moded-util.c
Expand Up @@ -181,7 +181,61 @@ static int set_mode_config (char *mode)
printf("mode set in the configuration file = %s\n", ret);
return 0;
}


/* not everything went as planned, return error */
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;
}
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
22 changes: 22 additions & 0 deletions src/usb_moded.c
Expand Up @@ -434,6 +434,16 @@ gchar *get_mode_list(void)
{

GString *modelist_str;
char *hidden_modes_list;
gchar **hidden_mode_split;
int hiddenmode = 0, i;


hidden_modes_list = get_hidden_modes();
if(hidden_modes_list)
{
hidden_mode_split = g_strsplit(hidden_modes_list, ",", 0);
}

modelist_str = g_string_new(NULL);

Expand All @@ -447,6 +457,18 @@ gchar *get_mode_list(void)
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if(hidden_modes_list)
for(i = 0; hidden_mode_split[i] != NULL; i++)
{
if(!strcmp(hidden_mode_split[i], data->mode_name))
hiddenmode = 1;
}

if(hiddenmode)
{
hiddenmode = 0;
continue;
}
modelist_str = g_string_append(modelist_str, data->mode_name);
modelist_str = g_string_append(modelist_str, ", ");
}
Expand Down

0 comments on commit a47964a

Please sign in to comment.