Skip to content

Commit

Permalink
[usb-moded] Normalize backend startup/cleanup functionality
Browse files Browse the repository at this point in the history
Use backend_init() and backend_quit() functions for settings up and
cleaning up all backends.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Feb 21, 2019
1 parent 9ae49bb commit 97107d1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/usb_moded-android.c
Expand Up @@ -42,7 +42,8 @@
bool android_in_use (void);
static bool android_probe (void);
gchar *android_get_serial (void);
bool android_init_values (void);
bool android_init (void);
void android_quit (void);
bool android_set_enabled (bool enable);
bool android_set_charging_mode(void);
bool android_set_function (const char *function);
Expand Down Expand Up @@ -161,9 +162,11 @@ android_get_serial(void)
}

/** initialize the basic android values
*
* @return true if android usb backend is ready for use, false otherwise
*/
bool
android_init_values(void)
android_init(void)
{
LOG_REGISTER_CONTEXT;

Expand Down Expand Up @@ -224,6 +227,14 @@ android_init_values(void)
return android_in_use();
}

/** Cleanup resources allocated by android usb backend
*/
void
android_quit(void)
{
/* For now this exists for symmetry with other backends only */
}

bool
android_set_enabled(bool enable)
{
Expand Down
3 changes: 2 additions & 1 deletion src/usb_moded-android.h
Expand Up @@ -48,7 +48,8 @@

bool android_in_use (void);
gchar *android_get_serial (void);
bool android_init_values (void);
bool android_init (void);
void android_quit (void);
bool android_set_enabled (bool enable);
bool android_set_charging_mode(void);
bool android_set_function (const char *function);
Expand Down
45 changes: 43 additions & 2 deletions src/usb_moded-configfs.c
Expand Up @@ -88,7 +88,8 @@ static bool configfs_write_file (const char *path, const char
static bool configfs_read_file (const char *path, char *buff, size_t size);
static bool configfs_write_udc (const char *text);
bool configfs_set_udc (bool enable);
bool configfs_init_values (void);
bool configfs_init (void);
void configfs_quit (void);
bool configfs_set_charging_mode (void);
bool configfs_set_productid (const char *id);
bool configfs_set_vendorid (const char *id);
Expand Down Expand Up @@ -740,9 +741,11 @@ configfs_set_udc(bool enable)
}

/** initialize the basic configfs values
*
* @return true if configfs backend is ready for use, false otherwise
*/
bool
configfs_init_values(void)
configfs_init(void)
{
LOG_REGISTER_CONTEXT;

Expand Down Expand Up @@ -802,6 +805,44 @@ configfs_init_values(void)
return configfs_in_use();
}

/** Cleanup resources allocated by configfs backend
*/
void
configfs_quit(void)
{
g_free(GADGET_BASE_DIRECTORY),
GADGET_BASE_DIRECTORY = 0;
g_free(GADGET_FUNC_DIRECTORY),
GADGET_FUNC_DIRECTORY = 0;
g_free(GADGET_CONF_DIRECTORY),
GADGET_CONF_DIRECTORY = 0;

g_free(GADGET_CTRL_UDC),
GADGET_CTRL_UDC = 0;
g_free(GADGET_CTRL_ID_VENDOR),
GADGET_CTRL_ID_VENDOR= 0;
g_free(GADGET_CTRL_ID_PRODUCT),
GADGET_CTRL_ID_PRODUCT= 0;
g_free(GADGET_CTRL_MANUFACTURER),
GADGET_CTRL_MANUFACTURER= 0;
g_free(GADGET_CTRL_PRODUCT),
GADGET_CTRL_PRODUCT = 0;
g_free(GADGET_CTRL_SERIAL),
GADGET_CTRL_SERIAL = 0;

g_free(FUNCTION_MASS_STORAGE),
FUNCTION_MASS_STORAGE = 0;
g_free(FUNCTION_RNDIS),
FUNCTION_RNDIS = 0;
g_free(FUNCTION_MTP),
FUNCTION_MTP = 0;

g_free(RNDIS_CTRL_WCEIS),
RNDIS_CTRL_WCEIS = 0;
g_free(RNDIS_CTRL_ETHADDR),
RNDIS_CTRL_ETHADDR= 0;
}

/* Set a charging mode for the configfs gadget
*
* @return true if successful, false on failure
Expand Down
3 changes: 2 additions & 1 deletion src/usb_moded-configfs.h
Expand Up @@ -33,7 +33,8 @@

bool configfs_in_use (void);
bool configfs_set_udc (bool enable);
bool configfs_init_values (void);
bool configfs_init (void);
void configfs_quit (void);
bool configfs_set_charging_mode (void);
bool configfs_set_productid (const char *id);
bool configfs_set_vendorid (const char *id);
Expand Down
5 changes: 4 additions & 1 deletion src/usb_moded-modules.c
Expand Up @@ -135,7 +135,10 @@ static bool modules_probe(void)
return modules_in_use();
}

/* kmod module init */
/** kmod module init
*
* @return true if modules backend is ready for use, false otherwise
*/
bool modules_init(void)
{
LOG_REGISTER_CONTEXT;
Expand Down
Empty file modified src/usb_moded-modules.h
Whitespace-only changes.
8 changes: 5 additions & 3 deletions src/usb_moded.c
Expand Up @@ -566,10 +566,10 @@ static bool usbmoded_init(void)
* while waiting.
*/
for( int i = 10; ; ) {
if( configfs_init_values() )
if( configfs_init() )
break;

if( android_init_values() )
if( android_init() )
break;

/* Must probe / poll since we're not yet running mainloop */
Expand Down Expand Up @@ -680,8 +680,10 @@ static void usbmoded_cleanup(void)
/* Stop udev listener */
umudev_quit();

/* Undo modules_init() */
/* Do backend specific cleanup */
modules_quit();
android_quit();
configfs_quit();

/* Undo trigger_init() */
trigger_stop();
Expand Down

0 comments on commit 97107d1

Please sign in to comment.