@@ -1550,6 +1550,47 @@ fpi_device_list_complete (FpDevice *device,
15501550 fpi_device_return_task_in_idle (device , FP_DEVICE_TASK_RETURN_ERROR , error );
15511551}
15521552
1553+ static int
1554+ update_attr (const char * attr , const char * value )
1555+ {
1556+ int fd , err ;
1557+ gssize r ;
1558+ char buf [50 ] = { 0 };
1559+
1560+ fd = open (attr , O_RDONLY );
1561+ err = - errno ;
1562+ if (fd < 0 )
1563+ return - err ;
1564+
1565+ r = read (fd , buf , sizeof (buf ) - 1 );
1566+ err = errno ;
1567+ close (fd );
1568+ if (r < 0 )
1569+ return - err ;
1570+
1571+ g_strchomp (buf );
1572+ if (g_strcmp0 (buf , value ) == 0 )
1573+ return 0 ;
1574+
1575+ /* O_TRUNC makes things work in the umockdev environment */
1576+ fd = open (attr , O_WRONLY | O_TRUNC );
1577+ err = errno ;
1578+ if (fd < 0 )
1579+ return - err ;
1580+
1581+ r = write (fd , value , strlen (value ));
1582+ err = - errno ;
1583+ close (fd );
1584+ if (r < 0 )
1585+ {
1586+ /* Write failures are weird, and are worth a warning */
1587+ g_warning ("Could not write %s to %s" , value , attr );
1588+ return - err ;
1589+ }
1590+
1591+ return 0 ;
1592+ }
1593+
15531594void
15541595fpi_device_configure_wakeup (FpDevice * device , gboolean enabled )
15551596{
@@ -1565,8 +1606,7 @@ fpi_device_configure_wakeup (FpDevice *device, gboolean enabled)
15651606 guint8 bus , port ;
15661607 g_autofree gchar * sysfs_wakeup = NULL ;
15671608 g_autofree gchar * sysfs_persist = NULL ;
1568- gssize r ;
1569- int fd ;
1609+ int res ;
15701610
15711611 ports = g_string_new (NULL );
15721612 bus = g_usb_device_get_bus (priv -> usb_device );
@@ -1582,41 +1622,19 @@ fpi_device_configure_wakeup (FpDevice *device, gboolean enabled)
15821622 g_string_set_size (ports , ports -> len - 1 );
15831623
15841624 sysfs_wakeup = g_strdup_printf ("/sys/bus/usb/devices/%d-%s/power/wakeup" , bus , ports -> str );
1585- fd = open (sysfs_wakeup , O_WRONLY );
1586-
1587- if (fd < 0 )
1588- {
1589- /* Wakeup not existing appears to be relatively normal. */
1590- g_debug ("Failed to open %s" , sysfs_wakeup );
1591- }
1592- else
1593- {
1594- r = write (fd , wakeup_command , strlen (wakeup_command ));
1595- if (r < 0 )
1596- g_warning ("Could not configure wakeup to %s by writing %s" , wakeup_command , sysfs_wakeup );
1597- close (fd );
1598- }
1625+ res = update_attr (sysfs_wakeup , wakeup_command );
1626+ if (res < 0 )
1627+ g_debug ("Failed to set %s to %s" , sysfs_wakeup , wakeup_command );
15991628
16001629 /* Persist means that the kernel tries to keep the USB device open
16011630 * in case it is "replugged" due to suspend.
16021631 * This is not helpful, as it will receive a reset and will be in a bad
16031632 * state. Instead, seeing an unplug and a new device makes more sense.
16041633 */
16051634 sysfs_persist = g_strdup_printf ("/sys/bus/usb/devices/%d-%s/power/persist" , bus , ports -> str );
1606- fd = open (sysfs_persist , O_WRONLY );
1607-
1608- if (fd < 0 )
1609- {
1610- g_warning ("Failed to open %s" , sysfs_persist );
1611- return ;
1612- }
1613- else
1614- {
1615- r = write (fd , "0" , 1 );
1616- if (r < 0 )
1617- g_message ("Could not disable USB persist by writing to %s" , sysfs_persist );
1618- close (fd );
1619- }
1635+ res = update_attr (sysfs_persist , "0" );
1636+ if (res < 0 )
1637+ g_warning ("Failed to disable USB persist by writing to %s" , sysfs_persist );
16201638
16211639 break ;
16221640 }
0 commit comments