<commit_before>/**
 * Copyright (C) 2009 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;

namespace Kernel26
{

class Led : FsoFramework.Device.LED, FsoFramework.AbstractObject
{
    FsoFramework.Subsystem subsystem;

    string sysfsnode;
    string brightness;
    string trigger;
    string triggers;

    static uint counter;

    public Led( FsoFramework.Subsystem subsystem, string sysfsnode )
    {
        this.subsystem = subsystem;
        this.sysfsnode = sysfsnode;
        this.brightness = sysfsnode + "/brightness";
        this.trigger = sysfsnode + "/trigger";

        if ( !FsoFramework.FileHandling.isPresent( this.brightness ) ||
             !FsoFramework.FileHandling.isPresent( this.trigger ) )
        {
            logger.error( "^^^ sysfs class is damaged; skipping." );
            return;
        }

        subsystem.registerServiceName( FsoFramework.Device.ServiceDBusName );
        subsystem.registerServiceObject( FsoFramework.Device.ServiceDBusName,
                                         "%s/%u".printf( FsoFramework.Device.LedServicePath, counter++ ),
                                         this );
        // FIXME: remove in release code, can be done lazily
        initTriggers();

        logger.info( "created new Led object." );
    }

    public override string repr()
    {
        return "<FsoFramework.Device.Led @ %s>".printf( sysfsnode );
    }

    public void initTriggers()
    {
        if ( triggers == null )
        {
            triggers = FsoFramework.FileHandling.read( trigger );
            logger.info( "^^^ supports the following triggers: '%s'".printf( triggers ) );
        }
    }

    //
    // FsoFramework.Device.LED
    //
    public string GetName()
    {
        return Path.get_basename( sysfsnode );
    }

    public void SetBrightness( int brightness )
    {
        if ( brightness > 255 )
            brightness = 255;
        if ( brightness < 0 )
            brightness = 0;

        FsoFramework.FileHandling.write( "none", this.trigger );
        FsoFramework.FileHandling.write( brightness.to_string(), this.brightness );
    }

    public void SetBlinking( int delay_on, int delay_off ) throws DBus.Error
    {
        initTriggers();
//         if ( !( "timer" in triggers ) )
//             throw new FsoFramework.Unsupported( "Kernel interface missing" );

        FsoFramework.FileHandling.write( "timer", this.trigger );
        FsoFramework.FileHandling.write( delay_on.to_string(), this.sysfsnode + "/delay_on" );
        FsoFramework.FileHandling.write( delay_off.to_string(), this.sysfsnode + "/delay_off" );

    }

    public void SetNetworking( string iface, string mode ) throws DBus.Error
    {
        initTriggers();

        if ( !FsoFramework.FileHandling.isPresent( "%s/%s".printf( sys_class_net, iface ) ) )
            return;
            //throw new FsoFramework.InvalidParameter( "Interface '%s' not present".printf( iface ) );

            foreach ( var element in mode.split( " " ) )
            {
                if ( element != "link" && element != "rx" && element != "tx" )
                    return;
                    //throw new FsoFramework.InvalidParameter( "Element '%s' not allowed.".printf( element ) );
            }
            FsoFramework.FileHandling.write( "netdev", this.trigger );
            FsoFramework.FileHandling.write( iface, this.sysfsnode + "/device_name" );
            FsoFramework.FileHandling.write( mode, this.sysfsnode + "/mode" );
    }
}

} /* namespace */
<commit_msg>fsodevice: kernel26_leds: throw proper dbus errors<commit_after>/**
 * Copyright (C) 2009 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;

namespace Kernel26
{

class Led : FsoFramework.Device.LED, FsoFramework.AbstractObject
{
    FsoFramework.Subsystem subsystem;

    string sysfsnode;
    string brightness;
    string trigger;
    string triggers;

    static uint counter;

    public Led( FsoFramework.Subsystem subsystem, string sysfsnode )
    {
        this.subsystem = subsystem;
        this.sysfsnode = sysfsnode;
        this.brightness = sysfsnode + "/brightness";
        this.trigger = sysfsnode + "/trigger";

        if ( !FsoFramework.FileHandling.isPresent( this.brightness ) ||
             !FsoFramework.FileHandling.isPresent( this.trigger ) )
        {
            logger.error( "^^^ sysfs class is damaged; skipping." );
            return;
        }

        subsystem.registerServiceName( FsoFramework.Device.ServiceDBusName );
        subsystem.registerServiceObject( FsoFramework.Device.ServiceDBusName,
                                         "%s/%u".printf( FsoFramework.Device.LedServicePath, counter++ ),
                                         this );
        // FIXME: remove in release code, can be done lazily
        initTriggers();

        logger.info( "created new Led object." );
    }

    public override string repr()
    {
        return "<FsoFramework.Device.Led @ %s>".printf( sysfsnode );
    }

    public void initTriggers()
    {
        if ( triggers == null )
        {
            triggers = FsoFramework.FileHandling.read( trigger );
            logger.info( "^^^ supports the following triggers: '%s'".printf( triggers ) );
        }
    }

    //
    // FsoFramework.Device.LED
    //
    public string GetName() throws DBus.Error
    {
        return Path.get_basename( sysfsnode );
    }

    public void SetBrightness( int brightness ) throws DBus.Error
    {
        if ( brightness > 255 )
            brightness = 255;
        if ( brightness < 0 )
            brightness = 0;

        FsoFramework.FileHandling.write( "none", this.trigger );
        FsoFramework.FileHandling.write( brightness.to_string(), this.brightness );
    }

    public void SetBlinking( int delay_on, int delay_off ) throws FsoFramework.OrgFreesmartphone, DBus.Error
    {
        initTriggers();
        if ( !( "timer" in triggers ) )
            throw new FsoFramework.OrgFreesmartphone.Unsupported( "Kernel support for timer led class trigger missing" );

        FsoFramework.FileHandling.write( "timer", this.trigger );
        FsoFramework.FileHandling.write( delay_on.to_string(), this.sysfsnode + "/delay_on" );
        FsoFramework.FileHandling.write( delay_off.to_string(), this.sysfsnode + "/delay_off" );
    }

    public void SetNetworking( string iface, string mode ) throws FsoFramework.OrgFreesmartphone, DBus.Error
    {
        initTriggers();

        if ( !( "netdev" in triggers ) )
            throw new FsoFramework.OrgFreesmartphone.Unsupported( "Kernel support for netdev led class trigger missing" );

        if ( !FsoFramework.FileHandling.isPresent( "%s/%s".printf( sys_class_net, iface ) ) )
            throw new FsoFramework.OrgFreesmartphone.InvalidParameter( "Interface '%s' not present".printf( iface ) );

            foreach ( var element in mode.split( " " ) )
            {
                if ( element != "link" && element != "rx" && element != "tx" )
                    return;
                    //throw new FsoFramework.InvalidParameter( "Element '%s' not allowed.".printf( element ) );
            }
            FsoFramework.FileHandling.write( "netdev", this.trigger );
            FsoFramework.FileHandling.write( iface, this.sysfsnode + "/device_name" );
            FsoFramework.FileHandling.write( mode, this.sysfsnode + "/mode" );
    }
}

} /* namespace */

{'fraction_non_alphanumeric': 0.10188423784271691, 'fraction_numerical': 0.006318402346835158, 'mean_word_length': 2.383206106870229, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3863586', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2407, 'n_words': 702}