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

 * This program 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 General Public License for more details.

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

using GLib;

using FsoGsm;

/**
 * @class Pdp.PppInternal
 *
 * Pdp Handler implemented with the internal PPP stack
 **/
class Pdp.PppInternal : FsoGsm.PdpHandler
{
    public const string MODULE_NAME = "fsogsm.pdp_ppp_internal";

    private FsoFramework.Transport transport;
    private ThirdParty.At.PPP ppp;
    private IOChannel iochannel;

    public override string repr()
    {
        return "<>";
    }

    construct
    {
    }

    public void onDebugFromAtPPP( string str )
    {
        assert( logger.debug( @"ThirdParty.At.PPP: $str" ) );
    }

    public void onConnectFromAtPPPP( string iface, string local, string peer, string dns1, string dns2 )
    {
        logger.info( @"PPP stack now online via $iface. Local IP is $local, remote IP is $peer, DNS1 is $dns1, DNS2 is $dns2" );
        connectedWithNewDefaultRoute( iface, local, "255.255.255.0", peer, dns1, dns2 );
    }

    public void onDisconnectFromAtPPP( ThirdParty.At.PPP.DisconnectReason reason )
    {
        logger.info( @"PPP stack now offline. Disconnect reason is $reason" );
        disconnected();
    }

    public async override void sc_activate() throws FreeSmartphone.GSM.Error, FreeSmartphone.Error
    {
        var m = theModem as FsoGsm.AbstractModem;

        var data = theModem.data();

        if ( data.contextParams == null )
        {
            throw new FreeSmartphone.Error.INTERNAL_ERROR( "Context parameters not set" );
        }

        if ( data.contextParams.apn == null )
        {
            throw new FreeSmartphone.Error.INTERNAL_ERROR( "APN not set" );
        }

        if ( ( (FsoGsm.AbstractModem) theModem ).data_transport != "serial" &&  ( (FsoGsm.AbstractModem) theModem ).data_transport != "tcp" )
        {
            throw new FreeSmartphone.Error.INTERNAL_ERROR( "ippp only supports data transport types 'serial' and 'tcp' for now" );
        }

        // DEBUG only, remove in production code
        theModem.channel( "main" ).transport.freeze();

        transport = FsoFramework.Transport.create( m.data_transport, m.data_port, m.data_speed );
        var channel = new AtChannel( null, transport, new FsoGsm.StateBasedAtParser() );

        if ( !yield channel.open() )
        {
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't open data channel or transport" );
        }

        var response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "E0" );

        response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "+CGDCONT=1,\"IP\",\"%s\"".printf( data.contextParams.apn ) );
        if ( response[0].strip() != "OK" )
        {
            yield channel.close();
            transport = null;
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't initialize data transport" );
        }
        response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "+CGACT=1" );
        if ( response[0].strip() != "OK" )
        {
            yield channel.close();
            transport = null;
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't initialize data transport" );
        }
        response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "+CGDATA=\"PPP\"" );
        if ( response[0].strip() != "CONNECT" )
        {
            yield channel.close();
            transport = null;
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't initialize data transport" );
        }

        iochannel = new IOChannel.unix_new( transport.freeze() );<commit_msg>fsogsmd: pdp_ppp_internal: set status to disconnected, if we don't succeed in establishing the context<commit_after>/*
 * Copyright (C) 2011 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program 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 General Public License for more details.

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

using GLib;

using FsoGsm;

/**
 * @class Pdp.PppInternal
 *
 * Pdp Handler implemented with the internal PPP stack
 **/
class Pdp.PppInternal : FsoGsm.PdpHandler
{
    public const string MODULE_NAME = "fsogsm.pdp_ppp_internal";

    private FsoFramework.Transport transport;
    private ThirdParty.At.PPP ppp;
    private IOChannel iochannel;

    public override string repr()
    {
        return "<>";
    }

    construct
    {
    }

    public void onDebugFromAtPPP( string str )
    {
        assert( logger.debug( @"ThirdParty.At.PPP: $str" ) );
    }

    public void onConnectFromAtPPPP( string iface, string local, string peer, string dns1, string dns2 )
    {
        logger.info( @"PPP stack now online via $iface. Local IP is $local, remote IP is $peer, DNS1 is $dns1, DNS2 is $dns2" );
        connectedWithNewDefaultRoute( iface, local, "255.255.255.0", peer, dns1, dns2 );
    }

    public void onDisconnectFromAtPPP( ThirdParty.At.PPP.DisconnectReason reason )
    {
        logger.info( @"PPP stack now offline. Disconnect reason is $reason" );
        disconnected();
    }

    public async override void sc_activate() throws FreeSmartphone.GSM.Error, FreeSmartphone.Error
    {
        var m = theModem as FsoGsm.AbstractModem;

        var data = theModem.data();

        if ( data.contextParams == null )
        {
            disconnected();
            throw new FreeSmartphone.Error.INTERNAL_ERROR( "Context parameters not set" );
        }

        if ( data.contextParams.apn == null )
        {
            disconnected();
            throw new FreeSmartphone.Error.INTERNAL_ERROR( "APN not set" );
        }

        if ( ( (FsoGsm.AbstractModem) theModem ).data_transport != "serial" &&  ( (FsoGsm.AbstractModem) theModem ).data_transport != "tcp" )
        {
            disconnected();
            throw new FreeSmartphone.Error.INTERNAL_ERROR( "ippp only supports data transport types 'serial' and 'tcp' for now" );
        }

        transport = FsoFramework.Transport.create( m.data_transport, m.data_port, m.data_speed );
        var channel = new AtChannel( null, transport, new FsoGsm.StateBasedAtParser() );

        if ( !yield channel.open() )
        {
            disconnected();
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't open data channel or transport" );
        }

        var response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "E0" );

        response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "+CGDCONT=1,\"IP\",\"%s\"".printf( data.contextParams.apn ) );
        if ( response[0].strip() != "OK" )
        {
            yield channel.close();
            transport = null;
            disconnected();
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't initialize data transport" );
        }
        response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "+CGACT=1" );
        if ( response[0].strip() != "OK" )
        {
            yield channel.close();
            transport = null;
            disconnected();
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't initialize data transport" );
        }
        response = yield channel.enqueueAsync( new FsoGsm.CustomAtCommand(), "D*99***1#" );
        if ( response[0].strip() != "CONNECT" )
        {
            yield channel.close();
            transport = null;
            disconnected();
            throw new FreeSmartphone.Error.SYSTEM_ERROR( "Can't initialize data transport" );
        }

        iochannel = new IOChannel.unix_new( transport.freeze() );
{'fraction_non_alphanumeric': 0.10390054744525548, 'fraction_numerical': 0.00946624087591241, 'mean_word_length': 2.619067271976888, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5519320', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2484, 'n_words': 785}