Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[android] Refactor android usb handling module. JB#41748
Make it logically match what is available in configfs module.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Aug 24, 2018
1 parent 4f53cf4 commit 4f9ffa6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 23 deletions.
105 changes: 83 additions & 22 deletions src/usb_moded-android.c
Expand Up @@ -41,11 +41,13 @@

/* -- android -- */

bool android_in_use (void);
bool android_in_use (void);
static bool android_probe (void);
gchar *android_get_serial (void);
bool android_init_values (void);
bool android_set_enabled (bool enable);
bool android_set_charging_mode(void);
bool android_set_function (const char *function);
bool android_set_productid (const char *id);
bool android_set_vendorid (const char *id);

Expand All @@ -59,15 +61,17 @@ static int android_probed = -1;
* Functions
* ========================================================================= */

bool android_in_use(void)
bool
android_in_use(void)
{
if( android_probed < 0 )
log_debug("android_in_use() called before android_probe()");

return android_probed > 0;
}

static bool android_probe(void)
static bool
android_probe(void)
{
if( android_probed <= 0 ) {
android_probed = access(ANDROID0_ENABLE, F_OK) == 0;
Expand Down Expand Up @@ -128,15 +132,16 @@ android_get_serial(void)

/** initialize the basic android values
*/
bool android_init_values(void)
bool
android_init_values(void)
{
gchar *text;

if( !android_probe() )
goto EXIT;

/* Disable */
write_to_file(ANDROID0_ENABLE, "0");
android_set_enabled(false);

/* Configure */
if( (text = android_get_serial()) )
Expand All @@ -154,7 +159,7 @@ bool android_init_values(void)
text = config_get_android_vendor_id();
if(text)
{
write_to_file(ANDROID0_ID_VENDOR, text);
android_set_vendorid(text);
g_free(text);
}
text = config_get_android_product();
Expand All @@ -166,7 +171,7 @@ bool android_init_values(void)
text = config_get_android_product_id();
if(text)
{
write_to_file(ANDROID0_ID_PRODUCT, text);
android_set_productid(text);
g_free(text);
}
text = mac_read_mac();
Expand All @@ -182,32 +187,87 @@ bool android_init_values(void)
return android_in_use();
}

bool
android_set_enabled(bool enable)
{
bool ack = false;
if( android_in_use() ) {
const char *val = enable ? "1" : "0";
ack = write_to_file(ANDROID0_ENABLE, val) != -1;
}
log_debug("ANDROID %s(%d) -> %d", __func__, enable, ack);
return ack;
}

/* Set a charging mode for the android gadget
*
* @return 0 if successful, 1 on failure
* @return true if successful, false on failure
*/
bool android_set_charging_mode(void)
bool
android_set_charging_mode(void)
{
bool ack = false;
if( android_in_use() ) {
/* disable, set functions to "mass_storage", re-enable */
write_to_file(ANDROID0_ENABLE, "0");
write_to_file(ANDROID0_ID_PRODUCT, "0AFE"); /* TODO: make configurable */
write_to_file(ANDROID0_FUNCTIONS, "mass_storage");
ack = write_to_file(ANDROID0_ENABLE, "1") != -1;
}

if( !android_in_use() )
goto EXIT;

if( !android_set_function("mass_storage") )
goto EXIT;

/* TODO: make configurable */
if( !android_set_productid("0AFE") )
goto EXIT;

if( !android_set_enabled(true) )
goto EXIT;

ack = true;

EXIT:
log_debug("ANDROID %s() -> %d", __func__, ack);
return ack;
}

/* Set a function for the android gadget
*
* @return true if successful, false on failure
*/
bool
android_set_function(const char *function)
{
bool ack = false;

if( !function )
goto EXIT;

if( !android_in_use() )
goto EXIT;

if( !android_set_enabled(false) )
goto EXIT;

if( write_to_file(ANDROID0_FUNCTIONS, function) == -1 )
goto EXIT;

/* Leave disabled, so that caller can adjust attributes
* etc before enabling */

ack = true;
EXIT:

log_debug("ANDROID %s(%s) -> %d", __func__, function, ack);
return ack;
}

/* Set a product id for the android gadget
*
* @return 0 if successful, 1 on failure
* @return true if successful, false on failure
*/
bool android_set_productid(const char *id)
bool
android_set_productid(const char *id)
{
bool ack = false;
if( android_in_use() ) {
if( id && android_in_use() ) {
ack = write_to_file(ANDROID0_ID_PRODUCT, id) != -1;
}
log_debug("ANDROID %s(%s) -> %d", __func__, id, ack);
Expand All @@ -216,12 +276,13 @@ bool android_set_productid(const char *id)

/* Set a vendor id for the android gadget
*
* @return 0 if successful, 1 on failure
* @return true if successful, false on failure
*/
bool android_set_vendorid(const char *id)
bool
android_set_vendorid(const char *id)
{
bool ack = false;
if( android_in_use() ) {
if( id && android_in_use() ) {
ack = write_to_file(ANDROID0_ID_VENDOR, id) != -1;
}
log_debug("ANDROID %s(%s) -> %d", __func__, id, ack);
Expand Down
4 changes: 3 additions & 1 deletion src/usb_moded-android.h
Expand Up @@ -45,10 +45,12 @@

/* -- android -- */

bool android_in_use (void);
gchar *android_get_serial (void);
bool android_init_values (void);
bool android_in_use (void);
bool android_set_enabled (bool enable);
bool android_set_charging_mode(void);
bool android_set_function (const char *function);
bool android_set_productid (const char *id);
bool android_set_vendorid (const char *id);

Expand Down

0 comments on commit 4f9ffa6

Please sign in to comment.